1 1.20 wiz /* $NetBSD: renice.c,v 1.20 2020/10/23 16:16:10 wiz Exp $ */ 2 1.4 tls 3 1.1 cgd /* 4 1.4 tls * Copyright (c) 1983, 1989, 1993 5 1.4 tls * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.13 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 cgd * may be used to endorse or promote products derived from this software 17 1.1 cgd * without specific prior written permission. 18 1.1 cgd * 19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 cgd * SUCH DAMAGE. 30 1.1 cgd */ 31 1.1 cgd 32 1.5 lukem #include <sys/cdefs.h> 33 1.1 cgd #ifndef lint 34 1.18 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993\ 35 1.18 lukem The Regents of the University of California. All rights reserved."); 36 1.1 cgd #endif /* not lint */ 37 1.1 cgd 38 1.1 cgd #ifndef lint 39 1.4 tls /*static char sccsid[] = "from: @(#)renice.c 8.1 (Berkeley) 6/9/93";*/ 40 1.20 wiz __RCSID("$NetBSD: renice.c,v 1.20 2020/10/23 16:16:10 wiz Exp $"); 41 1.1 cgd #endif /* not lint */ 42 1.1 cgd 43 1.1 cgd #include <sys/resource.h> 44 1.4 tls 45 1.5 lukem #include <err.h> 46 1.10 simonb #include <errno.h> 47 1.10 simonb #include <limits.h> 48 1.5 lukem #include <pwd.h> 49 1.1 cgd #include <stdio.h> 50 1.5 lukem #include <stdlib.h> 51 1.7 matt #include <string.h> 52 1.16 christos #include <sysexits.h> 53 1.6 christos 54 1.10 simonb static int getnum(const char *, const char *, int *); 55 1.16 christos static int donice(int, id_t, int, int); 56 1.17 perry static void usage(void) __dead; 57 1.5 lukem 58 1.1 cgd /* 59 1.1 cgd * Change the priority (nice) of processes 60 1.1 cgd * or groups of processes which are already 61 1.1 cgd * running. 62 1.1 cgd */ 63 1.5 lukem int 64 1.10 simonb main(int argc, char **argv) 65 1.1 cgd { 66 1.1 cgd int which = PRIO_PROCESS; 67 1.16 christos int prio, errs = 0, incr = 0; 68 1.16 christos id_t who = 0; 69 1.1 cgd 70 1.1 cgd argc--, argv++; 71 1.6 christos if (argc < 2) 72 1.6 christos usage(); 73 1.6 christos if (strcmp(*argv, "-n") == 0) { 74 1.6 christos incr = 1; 75 1.6 christos argc--, argv++; 76 1.19 dholland if (argc < 2) 77 1.6 christos usage(); 78 1.1 cgd } 79 1.6 christos if (getnum("priority", *argv, &prio)) 80 1.6 christos return 1; 81 1.1 cgd argc--, argv++; 82 1.1 cgd for (; argc > 0; argc--, argv++) { 83 1.1 cgd if (strcmp(*argv, "-g") == 0) { 84 1.1 cgd which = PRIO_PGRP; 85 1.1 cgd continue; 86 1.1 cgd } 87 1.1 cgd if (strcmp(*argv, "-u") == 0) { 88 1.1 cgd which = PRIO_USER; 89 1.1 cgd continue; 90 1.1 cgd } 91 1.1 cgd if (strcmp(*argv, "-p") == 0) { 92 1.1 cgd which = PRIO_PROCESS; 93 1.1 cgd continue; 94 1.1 cgd } 95 1.1 cgd if (which == PRIO_USER) { 96 1.6 christos struct passwd *pwd = getpwnam(*argv); 97 1.1 cgd 98 1.1 cgd if (pwd == NULL) { 99 1.5 lukem warnx("%s: unknown user", *argv); 100 1.16 christos errs++; 101 1.1 cgd continue; 102 1.1 cgd } 103 1.16 christos who = (id_t)pwd->pw_uid; 104 1.1 cgd } else { 105 1.16 christos int twho; 106 1.16 christos if (getnum("pid", *argv, &twho)) { 107 1.16 christos errs++; 108 1.6 christos continue; 109 1.16 christos } 110 1.16 christos if (twho < 0) { 111 1.5 lukem warnx("%s: bad value", *argv); 112 1.16 christos errs++; 113 1.1 cgd continue; 114 1.1 cgd } 115 1.16 christos who = (id_t)twho; 116 1.1 cgd } 117 1.6 christos errs += donice(which, who, prio, incr); 118 1.1 cgd } 119 1.16 christos return errs == 0 ? EXIT_SUCCESS : EXIT_FAILURE; 120 1.1 cgd } 121 1.1 cgd 122 1.6 christos static int 123 1.10 simonb getnum(const char *com, const char *str, int *val) 124 1.6 christos { 125 1.6 christos long v; 126 1.6 christos char *ep; 127 1.6 christos 128 1.12 lukem errno = 0; 129 1.14 fvdl v = strtol(str, &ep, 0); 130 1.6 christos 131 1.6 christos if (*ep) { 132 1.6 christos warnx("Bad %s argument: %s", com, str); 133 1.6 christos return 1; 134 1.6 christos } 135 1.6 christos if ((v == LONG_MIN || v == LONG_MAX) && errno == ERANGE) { 136 1.6 christos warn("Invalid %s argument: %s", com, str); 137 1.6 christos return 1; 138 1.6 christos } 139 1.6 christos 140 1.6 christos *val = (int)v; 141 1.6 christos return 0; 142 1.6 christos } 143 1.6 christos 144 1.6 christos static int 145 1.16 christos donice(int which, id_t who, int prio, int incr) 146 1.1 cgd { 147 1.1 cgd int oldprio; 148 1.1 cgd 149 1.8 kleink errno = 0; 150 1.8 kleink if ((oldprio = getpriority(which, who)) == -1 && errno != 0) { 151 1.5 lukem warn("%d: getpriority", who); 152 1.16 christos return 1; 153 1.1 cgd } 154 1.6 christos 155 1.6 christos if (incr) 156 1.6 christos prio = oldprio + prio; 157 1.6 christos 158 1.6 christos if (prio > PRIO_MAX) 159 1.6 christos prio = PRIO_MAX; 160 1.6 christos if (prio < PRIO_MIN) 161 1.6 christos prio = PRIO_MIN; 162 1.6 christos 163 1.6 christos if (setpriority(which, who, prio) == -1) { 164 1.5 lukem warn("%d: setpriority", who); 165 1.16 christos return 1; 166 1.1 cgd } 167 1.16 christos (void)printf("%d: old priority %d, new priority %d\n", 168 1.16 christos who, oldprio, prio); 169 1.16 christos return 0; 170 1.6 christos } 171 1.6 christos 172 1.6 christos static void 173 1.10 simonb usage(void) 174 1.6 christos { 175 1.6 christos 176 1.16 christos (void)fprintf(stderr, "Usage: %s [<priority> | -n <incr>] ", 177 1.16 christos getprogname()); 178 1.20 wiz (void)fprintf(stderr, "[[-gpu] who ...] ...\n"); 179 1.6 christos exit(1); 180 1.1 cgd } 181