[CFR] Specify the lock(1) timeout unit
Peter Pentchev
roam at ringlet.net
Thu Oct 21 04:39:17 PDT 2004
Hi,
Here's a little patch that teaches lock(1) about timeouts specified in
seconds, hours, or days in addition to the minutes it currently assumes.
I could commit this in a week if there are no objections.
G'luck,
Peter
Index: src/usr.bin/lock/lock.1
===================================================================
RCS file: /home/ncvs/src/usr.bin/lock/lock.1,v
retrieving revision 1.11
diff -u -r1.11 lock.1
--- src/usr.bin/lock/lock.1 2 Jul 2004 22:22:27 -0000 1.11
+++ src/usr.bin/lock/lock.1 21 Oct 2004 10:39:13 -0000
@@ -42,6 +42,7 @@
.Nm
.Op Fl npv
.Op Fl t Ar timeout
+.Op Fl u Ar unit
.Sh DESCRIPTION
The
.Nm
@@ -63,7 +64,22 @@
.It Fl t Ar timeout
The time limit (default 15 minutes) is changed to
.Ar timeout
-minutes.
+minutes, or units specified by the
+.Fl u
+option.
+.It Fl u Ar unit
+Specify the time measurement unit for the time limit.
+The
+.Ar unit
+argument may be one of
+.Sq s
+for seconds,
+.Sq m
+for minutes (the default),
+.Sq h
+for hours, or
+.Sq d
+for days.
.It Fl v
Disable switching virtual terminals while this terminal is locked.
This option is implemented in a way similar to the
Index: src/usr.bin/lock/lock.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/lock/lock.c,v
retrieving revision 1.18
diff -u -r1.18 lock.c
--- src/usr.bin/lock/lock.c 22 Jan 2004 04:24:15 -0000 1.18
+++ src/usr.bin/lock/lock.c 21 Oct 2004 11:07:36 -0000
@@ -85,6 +85,20 @@
long nexttime; /* keep the timeout time */
int no_timeout; /* lock terminal forever */
int vtyunlock; /* Unlock flag and code. */
+const char *timeout_str = "minute";
+int timeout_mul = 60;
+
+struct timeout_spec {
+ char spec;
+ int mul;
+ const char *str;
+} timeout_spec[] = {
+ {'s', 1, "second"},
+ {'m', 60, "minute"},
+ {'h', 3600, "hour"},
+ {'d', 86400, "day"},
+ {'\0', 0, NULL},
+};
/*ARGSUSED*/
int
@@ -98,20 +112,31 @@
int ch, failures, sectimeout, usemine, vtylock;
char *ap, *mypw, *ttynam, *tzn;
char hostname[MAXHOSTNAMELEN], s[BUFSIZ], s1[BUFSIZ];
+ struct timeout_spec *ts;
openlog("lock", LOG_ODELAY, LOG_AUTH);
sectimeout = TIMEOUT;
+ timeout_mul = 60;
mypw = NULL;
usemine = 0;
no_timeout = 0;
vtylock = 0;
- while ((ch = getopt(argc, argv, "npt:v")) != -1)
+ while ((ch = getopt(argc, argv, "npt:u:v")) != -1)
switch((char)ch) {
case 't':
if ((sectimeout = atoi(optarg)) <= 0)
errx(1, "illegal timeout value");
break;
+ case 'u':
+ for (ts = timeout_spec; ts->spec != '\0'; ts++)
+ if (ts->spec == optarg[0])
+ break;
+ if (ts->spec == '\0')
+ errx(1, "illegal timeout unit specifier");
+ timeout_mul = ts->mul;
+ timeout_str = ts->str;
+ break;
case 'p':
usemine = 1;
if (!(pw = getpwuid(getuid())))
@@ -128,7 +153,7 @@
default:
usage();
}
- timeout.tv_sec = sectimeout * 60;
+ timeout.tv_sec = sectimeout * timeout_mul;
setuid(getuid()); /* discard privs */
@@ -139,7 +164,7 @@
errx(1, "not a terminal?");
if (gettimeofday(&timval, (struct timezone *)NULL))
err(1, "gettimeofday");
- nexttime = timval.tv_sec + (sectimeout * 60);
+ nexttime = timval.tv_sec + (sectimeout * timeout_mul);
timval_sec = timval.tv_sec;
timp = localtime(&timval_sec);
ap = asctime(timp);
@@ -200,8 +225,8 @@
if (no_timeout)
(void)printf(" no timeout.");
else
- (void)printf(" timeout in %d minute%s.", sectimeout,
- sectimeout != 1 ? "s" : "");
+ (void)printf(" timeout in %d %s%s.", sectimeout,
+ timeout_str, sectimeout != 1 ? "s" : "");
if (vtylock)
(void)printf(" vty locked.");
(void)printf("\ntime now is %.20s%s%s", ap, tzn, ap + 19);
@@ -243,7 +268,7 @@
static void
usage(void)
{
- (void)fprintf(stderr, "usage: lock [-npv] [-t timeout]\n");
+ (void)fprintf(stderr, "usage: lock [-npv] [-t timeout] [-u unit]\n");
exit(1);
}
--
Peter Pentchev roam at ringlet.net roam at cnsys.bg roam at FreeBSD.org
PGP key: http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint FDBA FD79 C26F 3C51 C95E DF9E ED18 B68D 1619 4553
If I were you, who would be reading this sentence?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20041021/a9fe6017/attachment.bin
More information about the freebsd-hackers
mailing list