blob: bcaa94cf1b57f07d954917416d1cd1475fdb8106 [file] [log] [blame]
Manuel Novoa III 2c511602005-02-13 20:14:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbf960f52000-07-21 21:32:12 +00002/*
Manuel Novoa III 2c511602005-02-13 20:14:05 +00003 * renice implementation for busybox
Eric Andersenbf960f52000-07-21 21:32:12 +00004 *
Manuel Novoa III 2c511602005-02-13 20:14:05 +00005 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersenbf960f52000-07-21 21:32:12 +00006 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenbf960f52000-07-21 21:32:12 +00008 */
9
Manuel Novoa III 2c511602005-02-13 20:14:05 +000010/* Notes:
11 * Setting an absolute priority was obsoleted in SUSv2 and removed
12 * in SUSv3. However, the common linux version of renice does
13 * absolute and not relative. So we'll continue supporting absolute,
14 * although the stdout logging has been removed since both SUSv2 and
15 * SUSv3 specify that stdout isn't used.
16 *
17 * This version is lenient in that it doesn't require any IDs. The
18 * options -p, -g, and -u are treated as mode switches for the
19 * following IDs (if any). Multiple switches are allowed.
20 */
21
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000022#include "busybox.h"
Eric Andersenbf960f52000-07-21 21:32:12 +000023#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000024#include <stdlib.h>
Manuel Novoa III 2c511602005-02-13 20:14:05 +000025#include <string.h>
26#include <limits.h>
27#include <errno.h>
28#include <unistd.h>
Eric Andersenbf960f52000-07-21 21:32:12 +000029#include <sys/resource.h>
30
Manuel Novoa III 2c511602005-02-13 20:14:05 +000031#if (PRIO_PROCESS < CHAR_MIN) || (PRIO_PROCESS > CHAR_MAX)
32#error Assumption violated : PRIO_PROCESS value
33#endif
34#if (PRIO_PGRP < CHAR_MIN) || (PRIO_PGRP > CHAR_MAX)
35#error Assumption violated : PRIO_PGRP value
36#endif
37#if (PRIO_USER < CHAR_MIN) || (PRIO_USER > CHAR_MAX)
38#error Assumption violated : PRIO_USER value
39#endif
Eric Andersenbf960f52000-07-21 21:32:12 +000040
Manuel Novoa III 2c511602005-02-13 20:14:05 +000041int renice_main(int argc, char **argv)
42{
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000043 static const char Xetpriority_msg[] = "%cetpriority";
Manuel Novoa III 2c511602005-02-13 20:14:05 +000044
45 int retval = EXIT_SUCCESS;
46 int which = PRIO_PROCESS; /* Default 'which' value. */
47 int use_relative = 0;
48 int adjustment, new_priority;
Denis Vlasenko13858992006-10-08 12:49:22 +000049 unsigned who;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000050 char *arg;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000051
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000052 arg = *++argv;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000053
54 /* Check if we are using a relative adjustment. */
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000055 if (arg && arg[0] == '-' && arg[1] == 'n') {
Manuel Novoa III 2c511602005-02-13 20:14:05 +000056 use_relative = 1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000057 if (!arg[2])
58 arg = *++argv;
59 else
60 arg += 2;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000061 }
62
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000063 if (!arg) { /* No args? Then show usage. */
Manuel Novoa III 2c511602005-02-13 20:14:05 +000064 bb_show_usage();
65 }
66
67 /* Get the priority adjustment (absolute or relative). */
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000068 adjustment = xatoi_range(arg, INT_MIN/2, INT_MAX/2);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000069
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000070 while ((arg = *++argv) != NULL) {
Manuel Novoa III 2c511602005-02-13 20:14:05 +000071 /* Check for a mode switch. */
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000072 if (arg[0] == '-' && arg[1]) {
Manuel Novoa III 2c511602005-02-13 20:14:05 +000073 static const char opts[]
74 = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER };
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000075 const char *p = strchr(opts, arg[1]);
Denis Vlasenko13858992006-10-08 12:49:22 +000076 if (p) {
Manuel Novoa III 2c511602005-02-13 20:14:05 +000077 which = p[4];
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000078 if (!arg[2])
79 continue;
80 arg += 2;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000081 }
82 }
83
84 /* Process an ID arg. */
85 if (which == PRIO_USER) {
86 struct passwd *p;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000087 p = getpwnam(arg);
Denis Vlasenko13858992006-10-08 12:49:22 +000088 if (!p) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000089 bb_error_msg("unknown user: %s", arg);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000090 goto HAD_ERROR;
91 }
92 who = p->pw_uid;
93 } else {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000094 if (safe_strtou(arg, &who)) {
95 bb_error_msg("bad value: %s", arg);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000096 goto HAD_ERROR;
97 }
98 }
99
100 /* Get priority to use, and set it. */
101 if (use_relative) {
102 int old_priority;
103
104 errno = 0; /* Needed for getpriority error detection. */
105 old_priority = getpriority(which, who);
106 if (errno) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000107 bb_perror_msg(Xetpriority_msg, 'g');
Manuel Novoa III 2c511602005-02-13 20:14:05 +0000108 goto HAD_ERROR;
109 }
110
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000111 new_priority = old_priority + adjustment;
Manuel Novoa III 2c511602005-02-13 20:14:05 +0000112 } else {
113 new_priority = adjustment;
114 }
115
116 if (setpriority(which, who, new_priority) == 0) {
117 continue;
118 }
119
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000120 bb_perror_msg(Xetpriority_msg, 's');
121 HAD_ERROR:
Manuel Novoa III 2c511602005-02-13 20:14:05 +0000122 retval = EXIT_FAILURE;
123 }
124
125 /* No need to check for errors outputing to stderr since, if it
126 * was used, the HAD_ERROR label was reached and retval was set. */
127
128 return retval;
Eric Andersenbf960f52000-07-21 21:32:12 +0000129}