1 /* $OpenBSD: renice.c,v 1.6 2001/07/06 21:54:57 pvalchev Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1983, 1989, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)renice.c 8.1 (Berkeley) 6/9/93"; 45 #else 46 static char rcsid[] = "$OpenBSD: renice.c,v 1.6 2001/07/06 21:54:57 pvalchev Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/types.h> 51 #include <sys/time.h> 52 #include <sys/resource.h> 53 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <pwd.h> 57 #include <err.h> 58 #include <errno.h> 59 60 int main __P((int, char **)); 61 int donice __P((int, uid_t, int)); 62 void usage __P((void)); 63 64 /* 65 * Change the priority (nice) of processes 66 * or groups of processes which are already 67 * running. 68 */ 69 int 70 main(argc, argv) 71 int argc; 72 char **argv; 73 { 74 int which = PRIO_PROCESS; 75 int errs = 0; 76 long prio, who = 0; 77 char *ep; 78 79 argc--, argv++; 80 if (argc < 2) 81 usage(); 82 prio = strtol(*argv, &ep, 10); 83 if (*ep != NULL) 84 usage(); 85 argc--, argv++; 86 if (prio > PRIO_MAX) 87 prio = PRIO_MAX; 88 if (prio < PRIO_MIN) 89 prio = PRIO_MIN; 90 for (; argc > 0; argc--, argv++) { 91 if (strcmp(*argv, "-g") == 0) { 92 which = PRIO_PGRP; 93 continue; 94 } 95 if (strcmp(*argv, "-u") == 0) { 96 which = PRIO_USER; 97 continue; 98 } 99 if (strcmp(*argv, "-p") == 0) { 100 which = PRIO_PROCESS; 101 continue; 102 } 103 if (which == PRIO_USER) { 104 register struct passwd *pwd = getpwnam(*argv); 105 106 if (pwd == NULL) { 107 warnx("%s: unknown user", *argv); 108 continue; 109 } 110 who = pwd->pw_uid; 111 } else { 112 who = strtol(*argv, &ep, 10); 113 if (*ep != NULL || who < 0) { 114 warnx("%s: bad value", *argv); 115 continue; 116 } 117 } 118 errs += donice(which, (uid_t)who, (int)prio); 119 } 120 exit(errs != 0); 121 } 122 123 int 124 donice(which, who, prio) 125 int which; 126 uid_t who; 127 int prio; 128 { 129 int oldprio; 130 131 errno = 0, oldprio = getpriority(which, who); 132 if (oldprio == -1 && errno) { 133 warn("getpriority: %d", who); 134 return (1); 135 } 136 if (setpriority(which, who, prio) < 0) { 137 warn("setpriority: %d", who); 138 return (1); 139 } 140 printf("%d: old priority %d, new priority %d\n", who, oldprio, prio); 141 return (0); 142 } 143 144 void 145 usage() 146 { 147 extern char *__progname; 148 149 fprintf(stderr, "usage: %s priority [[-p] pid ...] [[-g] pgrp ...] " 150 "[[-u] user ...]\n", __progname); 151 exit(1); 152 } 153