Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Oct 2004 14:37:10 +0300
From:      Peter Pentchev <roam@ringlet.net>
To:        freebsd-hackers@FreeBSD.org
Subject:   [CFR] Specify the lock(1) timeout unit
Message-ID:  <20041021113709.GB7732@straylight.m.ringlet.net>

next in thread | raw e-mail | index | archive | help

--KFztAG8eRSV9hGtP
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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 =3D "minute";
+int		timeout_mul =3D 60;
+
+struct timeout_spec {
+	char		 spec;
+	int		 mul;
+	const char	*str;
+} timeout_spec[] =3D {
+	{'s',     1, "second"},
+	{'m',    60, "minute"},
+	{'h',  3600, "hour"},
+	{'d', 86400, "day"},
+	{'\0',    0, NULL},
+};
=20
 /*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;
=20
 	openlog("lock", LOG_ODELAY, LOG_AUTH);
=20
 	sectimeout =3D TIMEOUT;
+	timeout_mul =3D 60;
 	mypw =3D NULL;
 	usemine =3D 0;
 	no_timeout =3D 0;
 	vtylock =3D 0;
-	while ((ch =3D getopt(argc, argv, "npt:v")) !=3D -1)
+	while ((ch =3D getopt(argc, argv, "npt:u:v")) !=3D -1)
 		switch((char)ch) {
 		case 't':
 			if ((sectimeout =3D atoi(optarg)) <=3D 0)
 				errx(1, "illegal timeout value");
 			break;
+		case 'u':
+			for (ts =3D timeout_spec; ts->spec !=3D '\0'; ts++)
+				if (ts->spec =3D=3D optarg[0])
+					break;
+			if (ts->spec =3D=3D '\0')
+				errx(1, "illegal timeout unit specifier");
+			timeout_mul =3D ts->mul;
+			timeout_str =3D ts->str;
+			break;
 		case 'p':
 			usemine =3D 1;
 			if (!(pw =3D getpwuid(getuid())))
@@ -128,7 +153,7 @@
 		default:
 			usage();
 		}
-	timeout.tv_sec =3D sectimeout * 60;
+	timeout.tv_sec =3D sectimeout * timeout_mul;
=20
 	setuid(getuid());		/* discard privs */
=20
@@ -139,7 +164,7 @@
 		errx(1, "not a terminal?");
 	if (gettimeofday(&timval, (struct timezone *)NULL))
 		err(1, "gettimeofday");
-	nexttime =3D timval.tv_sec + (sectimeout * 60);
+	nexttime =3D timval.tv_sec + (sectimeout * timeout_mul);
 	timval_sec =3D timval.tv_sec;
 	timp =3D localtime(&timval_sec);
 	ap =3D asctime(timp);
@@ -200,8 +225,8 @@
 	if (no_timeout)
 		(void)printf(" no timeout.");
 	else
-		(void)printf(" timeout in %d minute%s.", sectimeout,
-		    sectimeout !=3D 1 ? "s" : "");
+		(void)printf(" timeout in %d %s%s.", sectimeout,
+		    timeout_str, sectimeout !=3D 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);
 }
=20

--=20
Peter Pentchev	roam@ringlet.net    roam@cnsys.bg    roam@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?

--KFztAG8eRSV9hGtP
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (FreeBSD)

iD8DBQFBd59l7Ri2jRYZRVMRAtbsAKC2Vyv4b8rruBt4uRQW+eFuY+tI2QCZATdU
79nx74bZnL8qfU0+ai3maaI=
=C7+u
-----END PGP SIGNATURE-----

--KFztAG8eRSV9hGtP--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041021113709.GB7732>