Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Oct 2004 16:38:10 -0400 (EDT)
From:      Robert Watson <rwatson@FreeBSD.org>
To:        Peter Pentchev <roam@ringlet.net>
Cc:        freebsd-hackers@FreeBSD.org
Subject:   Re: [CFR] Specify the lock(1) timeout unit
Message-ID:  <Pine.NEB.3.96L.1041021163636.10079R-100000@fledge.watson.org>
In-Reply-To: <20041021113709.GB7732@straylight.m.ringlet.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 21 Oct 2004, Peter Pentchev wrote:

> 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. 

I think the normal convention here (see also shutdown(8), etc) is to have
the unit be specified as part of the time specification rather than as a
separate argument.  I.e., lock -t 5m rather than lock -t 5 -u m.  If we
don't already have it, maybe we need humanize and dehumanize functions for
time as well as disk storage?

Robert N M Watson             FreeBSD Core Team, TrustedBSD Projects
robert@fledge.watson.org      Principal Research Scientist, McAfee Research


> 
> 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@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?
> 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.NEB.3.96L.1041021163636.10079R-100000>