aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/renice.c
blob: 4736d4ea547764bb21fe0c3ef22f437ab65016c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* renice.c - renice process
 *
 * Copyright 2013 CE Strake <strake888 at gmail.com>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/renice.html

USE_RENICE(NEWTOY(renice, "<1gpun#|", TOYFLAG_USR|TOYFLAG_BIN))

config RENICE
  bool "renice"
  default y
  help
    usage: renice [-gpu] -n INCREMENT ID...
*/

#define FOR_renice
#include "toys.h"

GLOBALS(
  long n;
)

void renice_main(void) {
  int which = (toys.optflags & FLAG_g) ? PRIO_PGRP :
              ((toys.optflags & FLAG_u) ? PRIO_USER : PRIO_PROCESS);
  char **arg;

  for (arg = toys.optargs; *arg; arg++) {
    char *s = *arg;
    int id = -1;

    if (toys.optflags & FLAG_u) {
      struct passwd *p = getpwnam(s);
      if (p) id = p->pw_uid;
    } else {
      id = strtol(s, &s, 10);
      if (*s) id = -1;
    }

    if (id < 0) {
      error_msg("bad '%s'", *arg);
      continue;
    }

    if (setpriority(which, id, getpriority(which, id)+TT.n) < 0)
      perror_msg("setpriority %d", id);
  }
}