xref: /aosp_15_r20/external/toybox/toys/posix/renice.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
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     -g	Group ids
16     -p	Process ids (default)
17     -u	User ids
18 */
19 
20 #define FOR_renice
21 #include "toys.h"
22 
GLOBALS(long n;)23 GLOBALS(
24   long n;
25 )
26 
27 void renice_main(void) {
28   int which = FLAG(g) ? PRIO_PGRP : (FLAG(u) ? PRIO_USER : PRIO_PROCESS);
29   char **arg;
30 
31   for (arg = toys.optargs; *arg; arg++) {
32     char *s = *arg;
33     int id = -1;
34 
35     if (FLAG(u)) {
36       struct passwd *p = getpwnam(s);
37       if (p) id = p->pw_uid;
38     } else {
39       id = strtol(s, &s, 10);
40       if (*s) id = -1;
41     }
42 
43     if (id < 0) {
44       error_msg("bad '%s'", *arg);
45       continue;
46     }
47 
48     if (setpriority(which, id, getpriority(which, id)+TT.n) < 0)
49       perror_msg("setpriority %d", id);
50   }
51 }
52