Home | History | Annotate | Download | only in posix
      1 /* renice.c - renice process
      2  *
      3  * Copyright 2013 CE Strake <strake888 at gmail.com>
      4  *
      5  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/renice.html
      6 
      7 USE_RENICE(NEWTOY(renice, "<1gpun#|", TOYFLAG_USR|TOYFLAG_BIN))
      8 
      9 config RENICE
     10   bool "renice"
     11   default y
     12   help
     13     usage: renice [-gpu] -n increment ID ...
     14 */
     15 
     16 #define FOR_renice
     17 #include "toys.h"
     18 
     19 GLOBALS(
     20   long nArgu;
     21 )
     22 
     23 void renice_main(void) {
     24   int which = (toys.optflags & FLAG_g) ? PRIO_PGRP :
     25               ((toys.optflags & FLAG_u) ? PRIO_USER : PRIO_PROCESS);
     26   char **arg;
     27 
     28   for (arg = toys.optargs; *arg; arg++) {
     29     char *s = *arg;
     30     int id = -1;
     31 
     32     if (toys.optflags & FLAG_u) {
     33       struct passwd *p = getpwnam(s);
     34       if (p) id = p->pw_uid;
     35     } else {
     36       id = strtol(s, &s, 10);
     37       if (*s) id = -1;
     38     }
     39 
     40     if (id < 0) {
     41       error_msg("bad '%s'", *arg);
     42       continue;
     43     }
     44 
     45     if (setpriority(which, id, getpriority(which, id)+TT.nArgu) < 0)
     46       perror_msg("setpriority %d", id);
     47   }
     48 }
     49