Module Name: src Committed By: mrg Date: Mon Dec 6 04:00:12 UTC 2010
Modified Files: src/usr.bin/pkill: pkill.1 pkill.c Log Message: add a "prenice <priority> <names>" command. use it like "prenice -4 mplayer". To generate a diff of this commit: cvs rdiff -u -r1.18 -r1.19 src/usr.bin/pkill/pkill.1 cvs rdiff -u -r1.25 -r1.26 src/usr.bin/pkill/pkill.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/pkill/pkill.1 diff -u src/usr.bin/pkill/pkill.1:1.18 src/usr.bin/pkill/pkill.1:1.19 --- src/usr.bin/pkill/pkill.1:1.18 Sat Feb 28 19:21:42 2009 +++ src/usr.bin/pkill/pkill.1 Mon Dec 6 04:00:11 2010 @@ -1,4 +1,4 @@ -.\" $NetBSD: pkill.1,v 1.18 2009/02/28 19:21:42 wiz Exp $ +.\" $NetBSD: pkill.1,v 1.19 2010/12/06 04:00:11 mrg Exp $ .\" .\" Copyright (c) 2002 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -56,6 +56,9 @@ .Op Fl U Ar uid .Op Fl u Ar euid .Op Ar pattern ... +.Nm prenice +.Op Ar priority +.Op Ar pattern ... .Sh DESCRIPTION The .Nm pgrep @@ -68,7 +71,15 @@ command searches the process table on the running system and signals all processes that match the criteria given on the command line. .Pp -The following options are available: +The +.Nm prenice +command searches the process table on the running system and sets the +priority of all processes that match the criteria given on the command line. +.Pp +The following options are available for +.Nm pkill +and +.Nm pgrep : .Bl -tag -width xxxxxxxx .It Fl d Ar delim Specify a delimiter to be printed between each process ID. @@ -166,9 +177,10 @@ process will never consider itself or system processes (kernel threads) as a potential match. .Sh EXIT STATUS -.Nm pgrep -and +.Nm pgrep , .Nm pkill +and +.Nm prenice return one of the following values upon exit: .Bl -tag -width foo .It 0 @@ -187,6 +199,7 @@ .Xr kill 2 , .Xr sigaction 2 , .Xr re_format 7 , +.Xr renice 1 , .Xr signal 7 .Sh HISTORY .Nm pkill @@ -196,3 +209,7 @@ .Nx 1.6 . They are modelled after utilities of the same name that appeared in Sun Solaris 7. +.Pp +.Nm prenice +was introduced in +.Nx 6.0 . Index: src/usr.bin/pkill/pkill.c diff -u src/usr.bin/pkill/pkill.c:1.25 src/usr.bin/pkill/pkill.c:1.26 --- src/usr.bin/pkill/pkill.c:1.25 Mon Apr 13 00:12:16 2009 +++ src/usr.bin/pkill/pkill.c Mon Dec 6 04:00:11 2010 @@ -1,4 +1,4 @@ -/* $NetBSD: pkill.c,v 1.25 2009/04/13 00:12:16 lukem Exp $ */ +/* $NetBSD: pkill.c,v 1.26 2010/12/06 04:00:11 mrg Exp $ */ /*- * Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -31,7 +31,7 @@ #include <sys/cdefs.h> #ifndef lint -__RCSID("$NetBSD: pkill.c,v 1.25 2009/04/13 00:12:16 lukem Exp $"); +__RCSID("$NetBSD: pkill.c,v 1.26 2010/12/06 04:00:11 mrg Exp $"); #endif /* !lint */ #include <sys/types.h> @@ -82,7 +82,9 @@ static const char *delim = "\n"; static int nproc; static int pgrep; +static int prenice; static int signum = SIGTERM; +static int nicenum; static int newest; static int inverse; static int longfmt; @@ -103,6 +105,7 @@ int main(int, char **); static void usage(void) __dead; static int killact(const struct kinfo_proc2 *); +static int reniceact(const struct kinfo_proc2 *); static int grepact(const struct kinfo_proc2 *); static void makelist(struct listhead *, enum listtype, char *); @@ -124,6 +127,20 @@ if (strcmp(getprogname(), "pgrep") == 0) { action = grepact; pgrep = 1; + } else if (strcmp(getprogname(), "prenice") == 0) { + prenice = 1; + if (argc < 2) + usage(); + action = reniceact; + p = argv[1]; + + i = (int)strtol(p, &q, 10); + if (*q == '\0') { + nicenum = i; + argv++; + argc--; + } else + usage(); } else { action = killact; p = argv[1]; @@ -152,64 +169,66 @@ criteria = 0; - while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilns:t:u:vx")) != -1) - switch (ch) { - case 'G': - makelist(&rgidlist, LT_GROUP, optarg); - criteria = 1; - break; - case 'P': - makelist(&ppidlist, LT_GENERIC, optarg); - criteria = 1; - break; - case 'U': - makelist(&ruidlist, LT_USER, optarg); - criteria = 1; - break; - case 'd': - if (!pgrep) + if (!prenice) { + while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilns:t:u:vx")) != -1) + switch (ch) { + case 'G': + makelist(&rgidlist, LT_GROUP, optarg); + criteria = 1; + break; + case 'P': + makelist(&ppidlist, LT_GENERIC, optarg); + criteria = 1; + break; + case 'U': + makelist(&ruidlist, LT_USER, optarg); + criteria = 1; + break; + case 'd': + if (!pgrep) + usage(); + delim = optarg; + break; + case 'f': + matchargs = 1; + break; + case 'g': + makelist(&pgrplist, LT_PGRP, optarg); + criteria = 1; + break; + case 'i': + cflags |= REG_ICASE; + break; + case 'l': + longfmt = 1; + break; + case 'n': + newest = 1; + criteria = 1; + break; + case 's': + makelist(&sidlist, LT_SID, optarg); + criteria = 1; + break; + case 't': + makelist(&tdevlist, LT_TTY, optarg); + criteria = 1; + break; + case 'u': + makelist(&euidlist, LT_USER, optarg); + criteria = 1; + break; + case 'v': + inverse = 1; + break; + case 'x': + fullmatch = 1; + break; + default: usage(); - delim = optarg; - break; - case 'f': - matchargs = 1; - break; - case 'g': - makelist(&pgrplist, LT_PGRP, optarg); - criteria = 1; - break; - case 'i': - cflags |= REG_ICASE; - break; - case 'l': - longfmt = 1; - break; - case 'n': - newest = 1; - criteria = 1; - break; - case 's': - makelist(&sidlist, LT_SID, optarg); - criteria = 1; - break; - case 't': - makelist(&tdevlist, LT_TTY, optarg); - criteria = 1; - break; - case 'u': - makelist(&euidlist, LT_USER, optarg); - criteria = 1; - break; - case 'v': - inverse = 1; - break; - case 'x': - fullmatch = 1; - break; - default: - usage(); - /* NOTREACHED */ - } + /* NOTREACHED */ + } + } argc -= optind; argv += optind; @@ -407,15 +426,21 @@ { const char *ustr; - if (pgrep) - ustr = "[-filnvx] [-d delim]"; - else - ustr = "[-signal] [-filnvx]"; - - (void)fprintf(stderr, - "Usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid] [-t tty]\n" - " [-U uid] [-u euid] pattern ...\n", getprogname(), - ustr); + if (prenice) + fprintf(stderr, "Usage: %s priority pattern ...\n", + getprogname()); + else { + if (pgrep) + ustr = "[-filnvx] [-d delim]"; + else + ustr = "[-signal] [-filnvx]"; + + (void)fprintf(stderr, + "Usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid] " + "[-t tty]\n" + " [-U uid] [-u euid] pattern ...\n", + getprogname(), ustr); + } exit(STATUS_BADUSAGE); } @@ -447,6 +472,32 @@ } static int +reniceact(const struct kinfo_proc2 *kp) +{ + int oldprio; + + if (longfmt) + grepact(kp); + + errno = 0; + if ((oldprio = getpriority(PRIO_PROCESS, kp->p_pid)) == -1 && + errno != 0) { + warn("%d: getpriority", kp->p_pid); + return 0; + } + + if (setpriority(PRIO_PROCESS, kp->p_pid, nicenum) == -1) { + warn("%d: setpriority", kp->p_pid); + return 0; + } + + (void)printf("%d: old priority %d, new priority %d\n", + kp->p_pid, oldprio, nicenum); + + return 1; +} + +static int grepact(const struct kinfo_proc2 *kp) { char **argv;