Annotation of 43BSDReno/usr.bin/renice/renice.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1983 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: char copyright[] =
        !            22: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
        !            23:  All rights reserved.\n";
        !            24: #endif /* not lint */
        !            25: 
        !            26: #ifndef lint
        !            27: static char sccsid[] = "@(#)renice.c   5.3 (Berkeley) 6/1/90";
        !            28: #endif /* not lint */
        !            29: 
        !            30: #include <sys/time.h>
        !            31: #include <sys/resource.h>
        !            32: #include <stdio.h>
        !            33: #include <pwd.h>
        !            34: 
        !            35: /*
        !            36:  * Change the priority (nice) of processes
        !            37:  * or groups of processes which are already
        !            38:  * running.
        !            39:  */
        !            40: main(argc, argv)
        !            41:        char **argv;
        !            42: {
        !            43:        int which = PRIO_PROCESS;
        !            44:        int who = 0, prio, errs = 0;
        !            45: 
        !            46:        argc--, argv++;
        !            47:        if (argc < 2) {
        !            48:                fprintf(stderr, "usage: renice priority [ [ -p ] pids ] ");
        !            49:                fprintf(stderr, "[ [ -g ] pgrps ] [ [ -u ] users ]\n");
        !            50:                exit(1);
        !            51:        }
        !            52:        prio = atoi(*argv);
        !            53:        argc--, argv++;
        !            54:        if (prio > PRIO_MAX)
        !            55:                prio = PRIO_MAX;
        !            56:        if (prio < PRIO_MIN)
        !            57:                prio = PRIO_MIN;
        !            58:        for (; argc > 0; argc--, argv++) {
        !            59:                if (strcmp(*argv, "-g") == 0) {
        !            60:                        which = PRIO_PGRP;
        !            61:                        continue;
        !            62:                }
        !            63:                if (strcmp(*argv, "-u") == 0) {
        !            64:                        which = PRIO_USER;
        !            65:                        continue;
        !            66:                }
        !            67:                if (strcmp(*argv, "-p") == 0) {
        !            68:                        which = PRIO_PROCESS;
        !            69:                        continue;
        !            70:                }
        !            71:                if (which == PRIO_USER) {
        !            72:                        register struct passwd *pwd = getpwnam(*argv);
        !            73:                        
        !            74:                        if (pwd == NULL) {
        !            75:                                fprintf(stderr, "renice: %s: unknown user\n",
        !            76:                                        *argv);
        !            77:                                continue;
        !            78:                        }
        !            79:                        who = pwd->pw_uid;
        !            80:                } else {
        !            81:                        who = atoi(*argv);
        !            82:                        if (who < 0) {
        !            83:                                fprintf(stderr, "renice: %s: bad value\n",
        !            84:                                        *argv);
        !            85:                                continue;
        !            86:                        }
        !            87:                }
        !            88:                errs += donice(which, who, prio);
        !            89:        }
        !            90:        exit(errs != 0);
        !            91: }
        !            92: 
        !            93: donice(which, who, prio)
        !            94:        int which, who, prio;
        !            95: {
        !            96:        int oldprio;
        !            97:        extern int errno;
        !            98: 
        !            99:        errno = 0, oldprio = getpriority(which, who);
        !           100:        if (oldprio == -1 && errno) {
        !           101:                fprintf(stderr, "renice: %d: ", who);
        !           102:                perror("getpriority");
        !           103:                return (1);
        !           104:        }
        !           105:        if (setpriority(which, who, prio) < 0) {
        !           106:                fprintf(stderr, "renice: %d: ", who);
        !           107:                perror("setpriority");
        !           108:                return (1);
        !           109:        }
        !           110:        printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
        !           111:        return (0);
        !           112: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.