[BACK]Return to renice.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / renice

Annotation of src/usr.bin/renice/renice.c, Revision 1.11

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