blob: 6f4b6b2bb5a1a47c4746e2a3ce2df7416c82a0fe [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
Rob Landleyd921b2e2006-08-03 15:41:12 +000014#include <getopt.h>
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000015#include <sys/resource.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +000016
Denis Vlasenko1caca342007-08-02 10:14:29 +000017/* Override ENABLE_FEATURE_PIDFILE */
18#define WANT_PIDFILE 1
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
20
Eric Andersen63a1a7a2004-03-13 08:33:10 +000021struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000022 struct pid_list *next;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000023 pid_t pid;
24};
Eric Andersenc2af1ee2001-10-18 19:33:06 +000025
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000026
27struct globals {
28 struct pid_list *found;
29 char *userspec;
30 char *cmdname;
31 char *execname;
32 char *pidfile;
33 int user_id;
34 smallint quiet;
35 smallint signal_nr;
36};
37#define G (*(struct globals*)&bb_common_bufsiz1)
38#define found (G.found )
39#define userspec (G.userspec )
40#define cmdname (G.cmdname )
41#define execname (G.execname )
42#define pidfile (G.pidfile )
43#define user_id (G.user_id )
44#define quiet (G.quiet )
45#define signal_nr (G.signal_nr )
46#define INIT_G() \
47 do { \
48 user_id = -1; \
49 signal_nr = 15; \
50 } while (0)
51
Eric Andersenc2af1ee2001-10-18 19:33:06 +000052
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000053static int pid_is_exec(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000054{
Denis Vlasenko61126ab2006-11-18 22:03:26 +000055 char buf[sizeof("/proc//exe") + sizeof(int)*3];
56 char *execbuf;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000057 int n;
Eric Andersen63a1a7a2004-03-13 08:33:10 +000058
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000059 sprintf(buf, "/proc/%u/exe", pid);
60 n = strlen(name) + 1;
61 execbuf = xzalloc(n + 1);
62 readlink(buf, execbuf, n);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000063 /* if readlink fails because link target is longer than strlen(name),
64 * execbuf still contains "", and strcmp will return !0. */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000065 n = strcmp(execbuf, name);
Rob Landley8cedaba2006-09-04 18:59:39 +000066 if (ENABLE_FEATURE_CLEAN_UP)
67 free(execbuf);
Denis Vlasenko1caca342007-08-02 10:14:29 +000068 return !n; /* nonzero (true) if execbuf == name */
Eric Andersenc2af1ee2001-10-18 19:33:06 +000069}
70
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000071static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000072{
73 struct stat sb;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000074 char buf[sizeof("/proc/") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +000075
Denis Vlasenkob131b272006-12-17 17:30:01 +000076 sprintf(buf, "/proc/%u", pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000077 if (stat(buf, &sb) != 0)
78 return 0;
79 return (sb.st_uid == uid);
80}
81
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000082static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000083{
Denis Vlasenkob131b272006-12-17 17:30:01 +000084 char fname[sizeof("/proc//stat") + sizeof(int)*3];
85 char *buf;
86 int r = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000087
Denis Vlasenkob131b272006-12-17 17:30:01 +000088 sprintf(fname, "/proc/%u/stat", pid);
89 buf = xmalloc_open_read_close(fname, NULL);
90 if (buf) {
91 char *p = strchr(buf, '(');
92 if (p) {
93 char *pe = strrchr(++p, ')');
94 if (pe) {
95 *pe = '\0';
96 r = !strcmp(p, name);
97 }
98 }
99 free(buf);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000100 }
Denis Vlasenkob131b272006-12-17 17:30:01 +0000101 return r;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000102}
103
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000104static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000105{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000106 struct pid_list *p;
107
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000108 if (execname && !pid_is_exec(pid, execname)) {
109 return;
110 }
111 if (userspec && !pid_is_user(pid, user_id)) {
112 return;
113 }
114 if (cmdname && !pid_is_cmd(pid, cmdname)) {
115 return;
116 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000117 p = xmalloc(sizeof(*p));
118 p->next = found;
119 p->pid = pid;
120 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000121}
122
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000123static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000124{
125 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000126 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000127
Eric Andersen625da9d2004-04-13 18:28:46 +0000128 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000129 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000130 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000131 check(pid);
132 fclose(f);
133 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000134 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000135}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000136
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000137static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000138{
139 DIR *procdir;
140 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000141 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000142
Eric Andersen625da9d2004-04-13 18:28:46 +0000143 if (pidfile) {
144 do_pidfile();
145 return;
146 }
147
Rob Landleyd921b2e2006-08-03 15:41:12 +0000148 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000149
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000150 pid = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000151 while ((entry = readdir(procdir)) != NULL) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000152 pid = bb_strtou(entry->d_name, NULL, 10);
153 if (errno)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000154 continue;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000155 check(pid);
156 }
157 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000158 if (!pid)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000159 bb_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000160}
161
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000162static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000163{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000164 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000165 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000166 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000167
Eric Andersen625da9d2004-04-13 18:28:46 +0000168 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000169
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000170 if (cmdname) {
171 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
172 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
173 } else if (execname) {
174 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
175 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
176 } else if (pidfile)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000177 what = xasprintf("process in pidfile '%s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000178 else if (userspec)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000179 what = xasprintf("process(es) owned by '%s'", userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000180 else
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000181 bb_error_msg_and_die("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000182
183 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000184 if (!quiet)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000185 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000186 killed = -1;
187 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000188 }
189 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000190 if (kill(p->pid, signal_nr) == 0) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000191 p->pid = - p->pid;
Eric Andersen950d8b42001-10-31 09:55:39 +0000192 killed++;
193 } else {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000194 bb_perror_msg("warning: killing process %u", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000195 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000196 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000197 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000198 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000199 for (p = found; p; p = p->next)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000200 if (p->pid < 0)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000201 printf(" %u", - p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000202 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000203 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000204 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000205 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000206 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000207 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000208}
209
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000210#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000211static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000212 "stop\0" No_argument "K"
213 "start\0" No_argument "S"
214 "background\0" No_argument "b"
215 "quiet\0" No_argument "q"
216 "make-pidfile\0" No_argument "m"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000217#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000218 "oknodo\0" No_argument "o"
219 "verbose\0" No_argument "v"
220 "nicelevel\0" Required_argument "N"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000221#endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000222 "startas\0" Required_argument "a"
223 "name\0" Required_argument "n"
224 "signal\0" Required_argument "s"
225 "user\0" Required_argument "u"
226 "chuid\0" Required_argument "c"
227 "exec\0" Required_argument "x"
228 "pidfile\0" Required_argument "p"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000229#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000230 "retry\0" Required_argument "R"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000231#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000232 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000233#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000234
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000235enum {
236 CTX_STOP = 0x1,
237 CTX_START = 0x2,
Denis Vlasenkocce38582007-02-26 22:47:42 +0000238 OPT_BACKGROUND = 0x4, // -b
239 OPT_QUIET = 0x8, // -q
240 OPT_MAKEPID = 0x10, // -m
241 OPT_a = 0x20, // -a
242 OPT_n = 0x40, // -n
243 OPT_s = 0x80, // -s
244 OPT_u = 0x100, // -u
245 OPT_c = 0x200, // -c
246 OPT_x = 0x400, // -x
247 OPT_p = 0x800, // -p
248 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
249 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
250 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000251};
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000252
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000253int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000254int start_stop_daemon_main(int argc, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000255{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000256 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000257 char *signame;
258 char *startas;
259 char *chuid;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000260#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
261// char *retry_arg = NULL;
262// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000263 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000264#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000265
266 INIT_G();
267
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000268#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000269 applet_long_options = start_stop_daemon_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000270#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000271
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000272 /* Check required one context option was given */
Denis Vlasenko09196572007-07-21 13:27:44 +0000273 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000274 opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:"
Denis Vlasenkocce38582007-02-26 22:47:42 +0000275 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
276// USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
277 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000278 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000279// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000280 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000281
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000282 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000283
Denis Vlasenkocce38582007-02-26 22:47:42 +0000284 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000285 signal_nr = get_signum(signame);
286 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000287 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000288
Denis Vlasenkocce38582007-02-26 22:47:42 +0000289 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000290 startas = execname;
291
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000292// USE_FEATURE_START_STOP_DAEMON_FANCY(
293// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000294// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000295// )
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000296 argc -= optind;
297 argv += optind;
298
Denis Vlasenkob131b272006-12-17 17:30:01 +0000299 if (userspec) {
300 user_id = bb_strtou(userspec, NULL, 10);
301 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000302 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000303 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000304
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000305 if (opt & CTX_STOP) {
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000306 int i = do_stop();
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000307 return (opt & OPT_OKNODO) ? 0 : (i <= 0);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000308 }
309
Eric Andersen625da9d2004-04-13 18:28:46 +0000310 do_procinit();
311
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000312 if (found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000313 if (!quiet)
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000314 printf("%s already running\n%d\n", execname, found->pid);
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000315 return !(opt & OPT_OKNODO);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000316 }
317 *--argv = startas;
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000318 if (opt & OPT_BACKGROUND) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000319#if BB_MMU
Denis Vlasenko5a142022007-03-26 13:20:54 +0000320 bb_daemonize(0);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000321#else
322 pid_t pid = vfork();
323 if (pid < 0) /* error */
324 bb_perror_msg_and_die("vfork");
Denis Vlasenko1caca342007-08-02 10:14:29 +0000325 if (pid != 0) {
326 /* parent */
327 /* why _exit? the child may have changed the stack,
328 * so "return 0" may do bad things */
329 _exit(0);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000330 }
331 /* child */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000332 setsid(); /* detach from controlling tty */
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000333 /* Redirect stdio to /dev/null, close extra FDs.
334 * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
335 bb_daemonize_or_rexec(
336 DAEMON_DEVNULL_STDIO
337 + DAEMON_CLOSE_EXTRA_FDS
338 + DAEMON_ONLY_SANITIZE,
339 NULL /* argv, unused */ );
340#endif
Eric Andersen53a22992002-01-26 09:04:45 +0000341 }
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000342 if (opt & OPT_MAKEPID) {
Eric Andersen625da9d2004-04-13 18:28:46 +0000343 /* user wants _us_ to make the pidfile */
Denis Vlasenko1caca342007-08-02 10:14:29 +0000344 write_pidfile(pidfile);
Eric Andersen625da9d2004-04-13 18:28:46 +0000345 }
Denis Vlasenkocce38582007-02-26 22:47:42 +0000346 if (opt & OPT_c) {
347 struct bb_uidgid_t ugid;
348 parse_chown_usergroup_or_die(&ugid, chuid);
349 if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
350 if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
Rob Landleyf0623a22006-07-17 00:35:07 +0000351 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000352#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000353 if (opt & OPT_NICELEVEL) {
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000354 /* Set process priority */
355 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
356 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
357 bb_perror_msg_and_die("setpriority(%d)", prio);
358 }
359 }
360#endif
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000361 execv(startas, argv);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000362 bb_perror_msg_and_die("cannot start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000363}