Skip site navigation (1)Skip section navigation (2)
Date:      10 Apr 2002 04:16:02 -0000
From:      Peter Avalos <pavalos@theshell.com>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   standards/36950: Add -n to renice(8)
Message-ID:  <20020410041602.29265.qmail@theshell.com>

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

>Number:         36950
>Category:       standards
>Synopsis:       Add -n to renice(8)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Apr 09 21:20:02 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Peter Avalos
>Release:        FreeBSD 4.5-STABLE i386
>Organization:
>Environment:
System: FreeBSD arsenic.theshell.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Tue Mar 26 11:15:29 PST 2002 pavalos@arsenic.theshell.com:/usr/obj/usr/src/sys/ARSENIC i386


	
>Description:
	Please review the following patch.

o Accept the -n option.
o Make usage() and SYNOPSIS style(9) compliant.
o Accept only one -g, -p, or -u option, and clarify how they affect
  the interpretation of the following IDs.
o Update the EXAMPLES section to match this new behavior.
o Note that it is IEEE Std 1003.1-2001 compliant, but we still accept
  a nice value for POLA.
o Use __FBSDID.
o Use static for local functions. 
o Change K&R declarations.
o Remove register.
o int main(int argc, char *argv[])
o style(9) variable declaration.
o Allow user to specify both user names and user ids.
o Use sysexits when appropriate.

>How-To-Repeat:
>Fix:


Index: renice.8
===================================================================
RCS file: /cvsroot/fbsd/src/usr.bin/renice/renice.8,v
retrieving revision 1.9
diff -u -r1.9 renice.8
--- renice.8	5 Aug 2001 22:07:27 -0000	1.9
+++ renice.8	7 Mar 2002 09:26:45 -0000
@@ -40,90 +40,103 @@
 .Nd alter priority of running processes
 .Sh SYNOPSIS
 .Nm
-.Ar priority
+.Ar nice_value
 .Oo
-.Op Fl p
-.Ar pid ...
+.Fl g |
+.Fl p
+|
+.Fl u
 .Oc
+.Ar ID ...
+.Nm
+.Fl n Ar increment
 .Oo
-.Op Fl g
-.Ar pgrp ...
-.Oc
-.Oo
-.Op Fl u
-.Ar user ...
+.Fl g |
+.Fl p
+|
+.Fl u
 .Oc
+.Ar ID ...
 .Sh DESCRIPTION
 .Nm Renice
 alters the
 scheduling priority of one or more running processes.
-The following
-.Ar who
-parameters are interpreted as process ID's, process group
-ID's, or user names.
-.Nm Renice Ns 'ing
+By default, the processes to be affected are specified by
+their process ID's.
+Specifying
 a process group causes all processes in the process group
 to have their scheduling priority altered.
-.Nm Renice Ns 'ing
+Specifying
 a user causes all processes owned by the user to have
 their scheduling priority altered.
-By default, the processes to be affected are specified by
-their process ID's.
+.Pp
+If the requested priority or increment would exceed PRIO_MIN (\-20)
+or PRIO_MAX (20), the limit that was exceeded is used.
+Only the superuser may affect the scheduling priority of processes
+owned by another user.
+Only the superuser may increase the scheduling priority of a process
+(decrease the nice_value).
 .Pp
 Options supported by
 .Nm :
 .Bl -tag -width Ds
+.It Fl n Ar increment
+Increment the scheduling priority of the specified process.
+Positive values will decrease the scheduling priority, and negative
+values will increase the scheduling priority.
+Only the superuser may specify a negative value.
 .It Fl g
-Force
-.Ar who
-parameters to be interpreted as process group ID's.
-.It Fl u
-Force the
-.Ar who
-parameters to be interpreted as user names.
+Interpret all
+.Ar ID
+arguments as unsigned decimal integer process group ID's.
 .It Fl p
-Resets the
-.Ar who
-interpretation to be (the default) process ID's.
+Interpret all
+.Ar ID
+arguments as unsigned decimal integer process ID's.
+This is the default if no options are specified.
+.It Fl u
+Interpret all
+.Ar ID
+arguments as users.
+The
+.Ar ID
+may be a valid user name or an unsigned decimal integer user ID.
 .El
 .Pp
-For example,
-.Bd -literal -offset
-renice +1 987 -u daemon root -p 32
-.Ed
-.Pp
-would change the priority of process ID's 987 and 32, and
-all processes owned by users daemon and root.
-.Pp
-Users other than the super-user may only alter the priority of
-processes they own,
-and can only monotonically increase their ``nice value''
-within the range 0 to
-.Dv PRIO_MAX
-(20).
-(This prevents overriding administrative fiats.)
-The super-user
-may alter the priority of any process
-and set the priority to any value in the range
-.Dv PRIO_MIN
-(\-20)
-to
-.Dv PRIO_MAX .
-Useful priorities are:
+Useful nice_values are:
 20 (the affected processes will run only when nothing else
 in the system wants to),
 0 (the ``base'' scheduling priority),
 anything negative (to make things go very fast).
-.Sh FILES
-.Bl -tag -width /etc/passwd -compact
-.It Pa /etc/passwd
-to map user names to user ID's
-.El
+.Sh EXAMPLES
+Adjust the system scheduling priority so group
+ID's 69 and 77 have a lower priority:
+.Pp
+.Dl % renice -n 5 -g 69 77
+.Pp
+Adjust the system scheduling priority so process
+ID's 2987 and 85723 have the lowest priority:
+.Pp
+.Dl % renice 20 2987 85723
+.Pp
+Adjust the system scheduling priority so user
+foo and user ID 2000 have higher priority:
+.Pp
+.Dl # renice -n -5 -u foo 2000
+.Pp
 .Sh SEE ALSO
 .Xr nice 1 ,
 .Xr rtprio 1 ,
 .Xr getpriority 2 ,
 .Xr setpriority 2
+.Sh STANDARDS
+The
+.Nm
+utility conforms to
+.St -p1003.1-2001
+Note: Specifying
+.Ar nice_value
+is deprecated, but it is accepted due to its historical significance.
 .Sh BUGS
 Non super-users cannot increase scheduling priorities of their own processes,
 even if they were the ones that decreased the priorities in the first place.
Index: renice.c
===================================================================
RCS file: /cvsroot/fbsd/src/usr.bin/renice/renice.c,v
retrieving revision 1.8
diff -u -r1.8 renice.c
--- renice.c	22 Mar 2002 01:33:21 -0000	1.8
+++ renice.c	10 Apr 2002 04:01:08 -0000
@@ -32,18 +32,19 @@
  */
 
 #ifndef lint
-static const char copyright[] =
+static char copyright[] =
 "@(#) Copyright (c) 1983, 1989, 1993\n\
 	The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
-#ifndef lint
 #if 0
+#ifndef lint
 static char sccsid[] = "@(#)renice.c	8.1 (Berkeley) 6/9/93";
-#endif
-static const char rcsid[] =
-  "$FreeBSD: src/usr.bin/renice/renice.c,v 1.8 2002/03/22 01:33:21 imp Exp $";
 #endif /* not lint */
+#endif
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -51,12 +52,14 @@
 
 #include <err.h>
 #include <errno.h>
+#include <limits.h>
+#include <pwd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <pwd.h>
+#include <sysexits.h>
 
-int donice(int, int, int);
+static int donice(int, int, int, int);
 static void usage(void);
 
 /*
@@ -65,66 +68,79 @@
  * running.
  */
 int
-main(argc, argv)
-	int argc;
-	char **argv;
+main(int argc, char *argv[])
 {
-	int which = PRIO_PROCESS;
-	int who = 0, prio, errs = 0;
+	struct passwd *pwd;
+	char *endptr;
+	int errs, nflag, prio, which, who;
+
+	which = PRIO_PROCESS;
+	errs = 0, nflag = 0, who = 0;
 
 	argc--, argv++;
 	if (argc < 2)
 		usage();
-	prio = atoi(*argv);
+
+	/* Parse priority or -n increment. */
+	if (strcmp(*argv, "-n") == 0) {
+		nflag = 1;
+		argc--, argv++;
+		if (argc < 2)
+			usage();
+	}
+	prio = strtol(*argv, &endptr, 10);
+	if (*endptr ||
+	    ((prio == LONG_MAX || prio == LONG_MIN) && errno == ERANGE))
+		errx(EX_DATAERR, "Invalid input: %s", *argv);
 	argc--, argv++;
-	if (prio > PRIO_MAX)
-		prio = PRIO_MAX;
-	if (prio < PRIO_MIN)
-		prio = PRIO_MIN;
+
+	/*
+	 * Accept -g, -p, -u, or a number. If it's a number, default
+	 * to -p (PRIO_PROCESS).
+	 */
+	if (strcmp(*argv, "-g") == 0) {
+		which = PRIO_PGRP;
+		argc--, argv++;
+	} else if (strcmp(*argv, "-u") == 0) {
+		which = PRIO_USER;
+		argc--, argv++;
+	} else if (strcmp(*argv, "-p") == 0)
+		argc--, argv++;
+
 	for (; argc > 0; argc--, argv++) {
-		if (strcmp(*argv, "-g") == 0) {
-			which = PRIO_PGRP;
-			continue;
-		}
-		if (strcmp(*argv, "-u") == 0) {
-			which = PRIO_USER;
-			continue;
-		}
-		if (strcmp(*argv, "-p") == 0) {
-			which = PRIO_PROCESS;
+		who = strtoul(*argv, &endptr, 10);
+
+		/* If argv is not a number, then we should be PRIO_USER. */
+		if ((*endptr && which != PRIO_USER) ||
+		    ((who == LONG_MAX || who == LONG_MIN) && errno == ERANGE)) {
+			warnx("Invalid input: %s", *argv);
 			continue;
 		}
-		if (which == PRIO_USER) {
-			register struct passwd *pwd = getpwnam(*argv);
+		if (which == PRIO_USER && *endptr) {
+			pwd = getpwnam(*argv);
 
 			if (pwd == NULL) {
 				warnx("%s: unknown user", *argv);
 				continue;
 			}
 			who = pwd->pw_uid;
-		} else {
-			who = atoi(*argv);
-			if (who < 0) {
-				warnx("%s: bad value", *argv);
-				continue;
-			}
 		}
-		errs += donice(which, who, prio);
+		errs += donice(which, who, prio, nflag);
 	}
-	exit(errs != 0);
+	exit(errs);
 }
 
 static void
-usage()
+usage(void)
 {
-	fprintf(stderr,
-"usage: renice priority [ [ -p ] pids ] [ [ -g ] pgrps ] [ [ -u ] users ]\n");
-	exit(1);
+
+	fprintf(stderr, "usage: renice nice_value [-g | -p | -u] ID ...\n");
+	fprintf(stderr, "       renice -n increment [-g | -p | -u] ID ...\n");
+	exit(EX_USAGE);
 }
 
-int
-donice(which, who, prio)
-	int which, who, prio;
+static int
+donice(int which, int who, int prio, int nflag)
 {
 	int oldprio;
 
@@ -133,10 +149,12 @@
 		warn("%d: getpriority", who);
 		return (1);
 	}
+	if (nflag)
+		prio += oldprio; /* Possible over/underflow here. */
 	if (setpriority(which, who, prio) < 0) {
 		warn("%d: setpriority", who);
 		return (1);
 	}
-	printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
+	printf("%d: old nice_value %d, new nice_value %d\n", who, oldprio, prio);
 	return (0);
 }

>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-standards" in the body of the message




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