blob: a2679f33112d39d20ae0541509ff45821014d7ed [file] [log] [blame]
Eric Andersenc2af1ee2001-10-18 19:33:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini start-stop-daemon implementation(s) for busybox
4 *
5 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
Eric Andersenc2af1ee2001-10-18 19:33:06 +00006 * Adapted for busybox David Kimdon <dwhedon@gordian.com>
Rob Landleyd921b2e2006-08-03 15:41:12 +00007 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenc2af1ee2001-10-18 19:33:06 +00009 */
10
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000011/* NB: we have a problem here with /proc/NN/exe usage, similar to
12 * one fixed in killall/pidof */
13
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000014#include <sys/resource.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +000015
Denis Vlasenko1caca342007-08-02 10:14:29 +000016/* Override ENABLE_FEATURE_PIDFILE */
17#define WANT_PIDFILE 1
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
19
Eric Andersen63a1a7a2004-03-13 08:33:10 +000020struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000021 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000022 pid_t pid;
23};
Eric Andersenc2af1ee2001-10-18 19:33:06 +000024
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000025
26struct globals {
27 struct pid_list *found;
28 char *userspec;
29 char *cmdname;
30 char *execname;
31 char *pidfile;
32 int user_id;
33 smallint quiet;
34 smallint signal_nr;
35};
36#define G (*(struct globals*)&bb_common_bufsiz1)
37#define found (G.found )
38#define userspec (G.userspec )
39#define cmdname (G.cmdname )
40#define execname (G.execname )
41#define pidfile (G.pidfile )
42#define user_id (G.user_id )
43#define quiet (G.quiet )
44#define signal_nr (G.signal_nr )
45#define INIT_G() \
46 do { \
47 user_id = -1; \
48 signal_nr = 15; \
49 } while (0)
50
Eric Andersenc2af1ee2001-10-18 19:33:06 +000051
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000052static int pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000053{
Denis Vlasenko61126ab2006-11-18 22:03:26 +000054 char buf[sizeof("/proc//exe") + sizeof(int)*3];
55 char *execbuf;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000056 int n;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000057
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000058 sprintf(buf, "/proc/%u/exe", pid);
59 n = strlen(name) + 1;
60 execbuf = xzalloc(n + 1);
61 readlink(buf, execbuf, n);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000062 /* if readlink fails because link target is longer than strlen(name),
63 * execbuf still contains "", and strcmp will return !0. */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000064 n = strcmp(execbuf, name);
Rob Landley8cedaba2006-09-04 18:59:39 +000065 if (ENABLE_FEATURE_CLEAN_UP)
66 free(execbuf);
Denis Vlasenko1caca342007-08-02 10:14:29 +000067 return !n; /* nonzero (true) if execbuf == name */
Eric Andersenc2af1ee2001-10-18 19:33:06 +000068}
69
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000070static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000071{
72 struct stat sb;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000073 char buf[sizeof("/proc/") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +000074
Denis Vlasenkob131b272006-12-17 17:30:01 +000075 sprintf(buf, "/proc/%u", pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000076 if (stat(buf, &sb) != 0)
77 return 0;
78 return (sb.st_uid == uid);
79}
80
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000081static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000082{
Denis Vlasenkob131b272006-12-17 17:30:01 +000083 char fname[sizeof("/proc//stat") + sizeof(int)*3];
84 char *buf;
85 int r = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000086
Denis Vlasenkob131b272006-12-17 17:30:01 +000087 sprintf(fname, "/proc/%u/stat", pid);
88 buf = xmalloc_open_read_close(fname, NULL);
89 if (buf) {
90 char *p = strchr(buf, '(');
91 if (p) {
92 char *pe = strrchr(++p, ')');
93 if (pe) {
94 *pe = '\0';
95 r = !strcmp(p, name);
96 }
97 }
98 free(buf);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000099 }
Denis Vlasenkob131b272006-12-17 17:30:01 +0000100 return r;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000101}
102
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000103static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000104{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000105 struct pid_list *p;
106
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000107 if (execname && !pid_is_exec(pid, execname)) {
108 return;
109 }
110 if (userspec && !pid_is_user(pid, user_id)) {
111 return;
112 }
113 if (cmdname && !pid_is_cmd(pid, cmdname)) {
114 return;
115 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000116 p = xmalloc(sizeof(*p));
117 p->next = found;
118 p->pid = pid;
119 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000120}
121
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000122static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000123{
124 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000125 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000126
Eric Andersen625da9d2004-04-13 18:28:46 +0000127 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000128 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000129 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000130 check(pid);
131 fclose(f);
132 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000133 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000134}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000135
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000136static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000137{
138 DIR *procdir;
139 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000140 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000141
Eric Andersen625da9d2004-04-13 18:28:46 +0000142 if (pidfile) {
143 do_pidfile();
144 return;
145 }
146
Rob Landleyd921b2e2006-08-03 15:41:12 +0000147 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000148
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000149 pid = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000150 while ((entry = readdir(procdir)) != NULL) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000151 pid = bb_strtou(entry->d_name, NULL, 10);
152 if (errno)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000153 continue;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000154 check(pid);
155 }
156 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000157 if (!pid)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000158 bb_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000159}
160
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000161static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000162{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000163 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000164 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000165 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000166
Eric Andersen625da9d2004-04-13 18:28:46 +0000167 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000168
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000169 if (cmdname) {
170 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
171 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
172 } else if (execname) {
173 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
174 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
175 } else if (pidfile)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000176 what = xasprintf("process in pidfile '%s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000177 else if (userspec)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000178 what = xasprintf("process(es) owned by '%s'", userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000179 else
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000180 bb_error_msg_and_die("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000181
182 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000183 if (!quiet)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000184 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000185 killed = -1;
186 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000187 }
188 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000189 if (kill(p->pid, signal_nr) == 0) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000190 p->pid = - p->pid;
Eric Andersen950d8b42001-10-31 09:55:39 +0000191 killed++;
192 } else {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000193 bb_perror_msg("warning: killing process %u", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000194 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000195 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000196 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000197 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000198 for (p = found; p; p = p->next)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000199 if (p->pid < 0)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000200 printf(" %u", - p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000201 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000202 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000203 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000204 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000205 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000206 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000207}
208
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000209#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000210static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000211 "stop\0" No_argument "K"
212 "start\0" No_argument "S"
213 "background\0" No_argument "b"
214 "quiet\0" No_argument "q"
215 "make-pidfile\0" No_argument "m"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000216#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000217 "oknodo\0" No_argument "o"
218 "verbose\0" No_argument "v"
219 "nicelevel\0" Required_argument "N"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000220#endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000221 "startas\0" Required_argument "a"
222 "name\0" Required_argument "n"
223 "signal\0" Required_argument "s"
224 "user\0" Required_argument "u"
225 "chuid\0" Required_argument "c"
226 "exec\0" Required_argument "x"
227 "pidfile\0" Required_argument "p"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000228#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000229 "retry\0" Required_argument "R"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000230#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000231 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000232#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000233
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000234enum {
235 CTX_STOP = 0x1,
236 CTX_START = 0x2,
Denis Vlasenkocce38582007-02-26 22:47:42 +0000237 OPT_BACKGROUND = 0x4, // -b
238 OPT_QUIET = 0x8, // -q
239 OPT_MAKEPID = 0x10, // -m
240 OPT_a = 0x20, // -a
241 OPT_n = 0x40, // -n
242 OPT_s = 0x80, // -s
243 OPT_u = 0x100, // -u
244 OPT_c = 0x200, // -c
245 OPT_x = 0x400, // -x
246 OPT_p = 0x800, // -p
247 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
248 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
249 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000250};
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000251
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000252int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000253int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000254{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000255 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000256 char *signame;
257 char *startas;
258 char *chuid;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000259#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
260// char *retry_arg = NULL;
261// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000262 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000263#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000264
265 INIT_G();
266
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000267#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000268 applet_long_options = start_stop_daemon_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000269#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000270
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000271 /* Check required one context option was given */
Denis Vlasenko09196572007-07-21 13:27:44 +0000272 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000273 opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:"
Denis Vlasenkocce38582007-02-26 22:47:42 +0000274 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
275// USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
276 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000277 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000278// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000279 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000280
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000281 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000282
Denis Vlasenkocce38582007-02-26 22:47:42 +0000283 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000284 signal_nr = get_signum(signame);
285 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000286 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000287
Denis Vlasenkocce38582007-02-26 22:47:42 +0000288 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000289 startas = execname;
290
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000291// USE_FEATURE_START_STOP_DAEMON_FANCY(
292// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000293// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000294// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000295 argc -= optind;
296 argv += optind;
297
Denis Vlasenkob131b272006-12-17 17:30:01 +0000298 if (userspec) {
299 user_id = bb_strtou(userspec, NULL, 10);
300 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000301 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000302 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000303
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000304 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000305 int i = do_stop();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000306 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000307 }
308
Eric Andersen625da9d2004-04-13 18:28:46 +0000309 do_procinit();
310
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000311 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000312 if (!quiet)
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000313 printf("%s already running\n%d\n", execname, found->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000314 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000315 }
316 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000317 if (opt & OPT_BACKGROUND) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000318#if BB_MMU
Denis Vlasenko5a142022007-03-26 13:20:54 +0000319 bb_daemonize(0);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000320#else
321 pid_t pid = vfork();
322 if (pid < 0) /* error */
323 bb_perror_msg_and_die("vfork");
Denis Vlasenko1caca342007-08-02 10:14:29 +0000324 if (pid != 0) {
325 /* parent */
326 /* why _exit? the child may have changed the stack,
327 * so "return 0" may do bad things */
328 _exit(0);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000329 }
330 /* child */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000331 setsid(); /* detach from controlling tty */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000332 /* Redirect stdio to /dev/null, close extra FDs.
333 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
334 bb_daemonize_or_rexec(
335 DAEMON_DEVNULL_STDIO
336 + DAEMON_CLOSE_EXTRA_FDS
337 + DAEMON_ONLY_SANITIZE,
338 NULL /* argv, unused */ );
339#endif
Eric Andersen53a22992002-01-26 09:04:45 +0000340 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000341 if (opt & OPT_MAKEPID) {
Eric Andersen625da9d2004-04-13 18:28:46 +0000342 /* user wants _us_ to make the pidfile */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000343 write_pidfile(pidfile);
Eric Andersen625da9d2004-04-13 18:28:46 +0000344 }
Denis Vlasenkocce38582007-02-26 22:47:42 +0000345 if (opt & OPT_c) {
346 struct bb_uidgid_t ugid;
347 parse_chown_usergroup_or_die(&ugid, chuid);
348 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
349 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
Rob Landleyf0623a22006-07-17 00:35:07 +0000350 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000351#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000352 if (opt & OPT_NICELEVEL) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000353 /* Set process priority */
354 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
355 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
356 bb_perror_msg_and_die("setpriority(%d)", prio);
357 }
358 }
359#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000360 execv(startas, argv);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000361 bb_perror_msg_and_die("cannot start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000362}