renice.c - sbase - suckless unix tools
 (HTM) git clone git://git.suckless.org/sbase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       renice.c (1489B)
       ---
            1 /* See LICENSE file for copyright and license details. */
            2 #include <sys/resource.h>
            3 
            4 #include <errno.h>
            5 #include <pwd.h>
            6 #include <stdlib.h>
            7 
            8 #include "util.h"
            9 
           10 #ifndef PRIO_MIN
           11 #define PRIO_MIN -NZERO
           12 #endif
           13 
           14 #ifndef PRIO_MAX
           15 #define PRIO_MAX (NZERO-1)
           16 #endif
           17 
           18 static int
           19 renice(int which, int who, long adj)
           20 {
           21         errno = 0;
           22         adj += getpriority(which, who);
           23         if (errno) {
           24                 weprintf("getpriority %d:", who);
           25                 return 0;
           26         }
           27 
           28         adj = MAX(PRIO_MIN, MIN(adj, PRIO_MAX));
           29         if (setpriority(which, who, (int)adj) < 0) {
           30                 weprintf("setpriority %d:", who);
           31                 return 0;
           32         }
           33 
           34         return 1;
           35 }
           36 
           37 static void
           38 usage(void)
           39 {
           40         eprintf("usage: %s -n num [-g | -p | -u] id ...\n", argv0);
           41 }
           42 
           43 int
           44 main(int argc, char *argv[])
           45 {
           46         const char *adj = NULL;
           47         long val;
           48         int which = PRIO_PROCESS, ret = 0;
           49         struct passwd *pw;
           50         int who;
           51 
           52         ARGBEGIN {
           53         case 'n':
           54                 adj = EARGF(usage());
           55                 break;
           56         case 'g':
           57                 which = PRIO_PGRP;
           58                 break;
           59         case 'p':
           60                 which = PRIO_PROCESS;
           61                 break;
           62         case 'u':
           63                 which = PRIO_USER;
           64                 break;
           65         default:
           66                 usage();
           67         } ARGEND
           68 
           69         if (!argc || !adj)
           70                 usage();
           71 
           72         val = estrtonum(adj, PRIO_MIN, PRIO_MAX);
           73         for (; *argv; argc--, argv++) {
           74                 if (which == PRIO_USER) {
           75                         errno = 0;
           76                         if (!(pw = getpwnam(*argv))) {
           77                                 if (errno)
           78                                         weprintf("getpwnam %s:", *argv);
           79                                 else
           80                                         weprintf("getpwnam %s: no user found\n", *argv);
           81                                 ret = 1;
           82                                 continue;
           83                         }
           84                         who = pw->pw_uid;
           85                 } else {
           86                         who = estrtonum(*argv, 1, INT_MAX);
           87                 }
           88                 if (!renice(which, who, val))
           89                         ret = 1;
           90         }
           91 
           92         return ret;
           93 }