blob: 2f8e04a7814d8af317cbd1b55e7ed9fe33e360d8 [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;
Denis Vlasenkod9c51e92008-04-19 19:06:23 +000035 struct stat execstat;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000036};
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 )
Denis Vlasenkod9c51e92008-04-19 19:06:23 +000046#define execstat (G.execstat )
Denis Vlasenko04bb2d22008-02-26 16:08:02 +000047#define INIT_G() \
48 do { \
49 user_id = -1; \
50 signal_nr = 15; \
51 } while (0)
52
Eric Andersenc2af1ee2001-10-18 19:33:06 +000053
Denis Vlasenkod9c51e92008-04-19 19:06:23 +000054static int pid_is_exec(pid_t pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000055{
Denis Vlasenkod9c51e92008-04-19 19:06:23 +000056 struct stat st;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000057 char buf[sizeof("/proc//exe") + sizeof(int)*3];
Eric Andersen63a1a7a2004-03-13 08:33:10 +000058
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +000059 sprintf(buf, "/proc/%u/exe", pid);
Denis Vlasenkod9c51e92008-04-19 19:06:23 +000060 if (stat(buf, &st) < 0)
61 return 0;
62 if (st.st_dev == execstat.st_dev
63 && st.st_ino == execstat.st_ino)
64 return 1;
65 return 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000066}
67
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000068static int pid_is_user(int pid, int uid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000069{
70 struct stat sb;
Denis Vlasenko61126ab2006-11-18 22:03:26 +000071 char buf[sizeof("/proc/") + sizeof(int)*3];
Eric Andersenc2af1ee2001-10-18 19:33:06 +000072
Denis Vlasenkob131b272006-12-17 17:30:01 +000073 sprintf(buf, "/proc/%u", pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000074 if (stat(buf, &sb) != 0)
75 return 0;
76 return (sb.st_uid == uid);
77}
78
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +000079static int pid_is_cmd(pid_t pid, const char *name)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000080{
Denis Vlasenkob131b272006-12-17 17:30:01 +000081 char fname[sizeof("/proc//stat") + sizeof(int)*3];
82 char *buf;
83 int r = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000084
Denis Vlasenkob131b272006-12-17 17:30:01 +000085 sprintf(fname, "/proc/%u/stat", pid);
86 buf = xmalloc_open_read_close(fname, NULL);
87 if (buf) {
88 char *p = strchr(buf, '(');
89 if (p) {
90 char *pe = strrchr(++p, ')');
91 if (pe) {
92 *pe = '\0';
93 r = !strcmp(p, name);
94 }
95 }
96 free(buf);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000097 }
Denis Vlasenkob131b272006-12-17 17:30:01 +000098 return r;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000099}
100
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000101static void check(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000102{
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000103 struct pid_list *p;
104
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000105 if (execname && !pid_is_exec(pid)) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000106 return;
107 }
108 if (userspec && !pid_is_user(pid, user_id)) {
109 return;
110 }
111 if (cmdname && !pid_is_cmd(pid, cmdname)) {
112 return;
113 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000114 p = xmalloc(sizeof(*p));
115 p->next = found;
116 p->pid = pid;
117 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000118}
119
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000120static void do_pidfile(void)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000121{
122 FILE *f;
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000123 unsigned pid;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000124
Eric Andersen625da9d2004-04-13 18:28:46 +0000125 f = fopen(pidfile, "r");
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000126 if (f) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000127 if (fscanf(f, "%u", &pid) == 1)
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000128 check(pid);
129 fclose(f);
130 } else if (errno != ENOENT)
Eric Andersen625da9d2004-04-13 18:28:46 +0000131 bb_perror_msg_and_die("open pidfile %s", pidfile);
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000132}
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000133
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000134static void do_procinit(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000135{
136 DIR *procdir;
137 struct dirent *entry;
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000138 int pid;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000139
Eric Andersen625da9d2004-04-13 18:28:46 +0000140 if (pidfile) {
141 do_pidfile();
142 return;
143 }
144
Rob Landleyd921b2e2006-08-03 15:41:12 +0000145 procdir = xopendir("/proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000146
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000147 pid = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000148 while ((entry = readdir(procdir)) != NULL) {
Denis Vlasenkob131b272006-12-17 17:30:01 +0000149 pid = bb_strtou(entry->d_name, NULL, 10);
150 if (errno)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000151 continue;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000152 check(pid);
153 }
154 closedir(procdir);
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000155 if (!pid)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000156 bb_error_msg_and_die("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000157}
158
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000159static int do_stop(void)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000160{
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000161 char *what;
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000162 struct pid_list *p;
Eric Andersen950d8b42001-10-31 09:55:39 +0000163 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000164
Eric Andersen625da9d2004-04-13 18:28:46 +0000165 do_procinit();
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000166
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000167 if (cmdname) {
168 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
169 if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
170 } else if (execname) {
171 if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
172 if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
173 } else if (pidfile)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000174 what = xasprintf("process in pidfile '%s'", pidfile);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000175 else if (userspec)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000176 what = xasprintf("process(es) owned by '%s'", userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000177 else
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000178 bb_error_msg_and_die("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000179
180 if (!found) {
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000181 if (!quiet)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000182 printf("no %s found; none killed\n", what);
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000183 killed = -1;
184 goto ret;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000185 }
186 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000187 if (kill(p->pid, signal_nr) == 0) {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000188 p->pid = - p->pid;
Eric Andersen950d8b42001-10-31 09:55:39 +0000189 killed++;
190 } else {
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000191 bb_perror_msg("warning: killing process %u", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000192 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000193 }
Eric Andersen63a1a7a2004-03-13 08:33:10 +0000194 if (!quiet && killed) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000195 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000196 for (p = found; p; p = p->next)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000197 if (p->pid < 0)
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000198 printf(" %u", - p->pid);
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000199 puts(")");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000200 }
Denis Vlasenkoa1b16f42007-07-31 17:09:44 +0000201 ret:
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000202 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko61126ab2006-11-18 22:03:26 +0000203 free(what);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000204 return killed;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000205}
206
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000207#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000208static const char start_stop_daemon_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000209 "stop\0" No_argument "K"
210 "start\0" No_argument "S"
211 "background\0" No_argument "b"
212 "quiet\0" No_argument "q"
213 "make-pidfile\0" No_argument "m"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000214#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000215 "oknodo\0" No_argument "o"
216 "verbose\0" No_argument "v"
217 "nicelevel\0" Required_argument "N"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000218#endif
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000219 "startas\0" Required_argument "a"
220 "name\0" Required_argument "n"
221 "signal\0" Required_argument "s"
222 "user\0" Required_argument "u"
223 "chuid\0" Required_argument "c"
224 "exec\0" Required_argument "x"
225 "pidfile\0" Required_argument "p"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000226#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000227 "retry\0" Required_argument "R"
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000228#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000229 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000230#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000231
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000232enum {
233 CTX_STOP = 0x1,
234 CTX_START = 0x2,
Denis Vlasenkocce38582007-02-26 22:47:42 +0000235 OPT_BACKGROUND = 0x4, // -b
236 OPT_QUIET = 0x8, // -q
237 OPT_MAKEPID = 0x10, // -m
238 OPT_a = 0x20, // -a
239 OPT_n = 0x40, // -n
240 OPT_s = 0x80, // -s
241 OPT_u = 0x100, // -u
242 OPT_c = 0x200, // -c
243 OPT_x = 0x400, // -x
244 OPT_p = 0x800, // -p
245 OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
246 OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
247 OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000248};
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000249
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000250int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko5a96c3e2008-04-19 17:40:29 +0000251int start_stop_daemon_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000252{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000253 unsigned opt;
Denis Vlasenkocce38582007-02-26 22:47:42 +0000254 char *signame;
255 char *startas;
256 char *chuid;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000257#if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
258// char *retry_arg = NULL;
259// int retries = -1;
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000260 char *opt_N;
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000261#endif
Denis Vlasenko04bb2d22008-02-26 16:08:02 +0000262
263 INIT_G();
264
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000265#if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000266 applet_long_options = start_stop_daemon_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000267#endif
Eric Andersenaa820db2003-07-26 09:10:35 +0000268
"Vladimir N. Oleynik"064f04e2005-10-11 14:38:01 +0000269 /* Check required one context option was given */
Denis Vlasenko09196572007-07-21 13:27:44 +0000270 opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000271 opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:"
Denis Vlasenkocce38582007-02-26 22:47:42 +0000272 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"),
273// USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
274 &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
Denis Vlasenkoca3c9812006-10-08 23:36:17 +0000275 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000276// USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
Denis Vlasenkocce38582007-02-26 22:47:42 +0000277 );
Eric Andersenaa820db2003-07-26 09:10:35 +0000278
Denis Vlasenkob8c77b52006-12-17 19:43:10 +0000279 quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE);
Paul Foxbb9a0ad2005-07-29 14:58:09 +0000280
Denis Vlasenkocce38582007-02-26 22:47:42 +0000281 if (opt & OPT_s) {
Rob Landley84790632006-08-28 20:30:27 +0000282 signal_nr = get_signum(signame);
283 if (signal_nr < 0) bb_show_usage();
Eric Andersen08804ce2003-07-30 08:29:56 +0000284 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000285
Denis Vlasenkocce38582007-02-26 22:47:42 +0000286 if (!(opt & OPT_a))
Eric Andersenaa820db2003-07-26 09:10:35 +0000287 startas = execname;
288
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000289// USE_FEATURE_START_STOP_DAEMON_FANCY(
290// if (retry_arg)
Denis Vlasenko13858992006-10-08 12:49:22 +0000291// retries = xatoi_u(retry_arg);
Bernhard Reutner-Fischera926f8e2006-06-11 17:24:01 +0000292// )
Denis Vlasenko5a96c3e2008-04-19 17:40:29 +0000293 //argc -= optind;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000294 argv += optind;
295
Denis Vlasenkob131b272006-12-17 17:30:01 +0000296 if (userspec) {
297 user_id = bb_strtou(userspec, NULL, 10);
298 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000299 user_id = xuname2uid(userspec);
Denis Vlasenkob131b272006-12-17 17:30:01 +0000300 }
Denis Vlasenkod9c51e92008-04-19 19:06:23 +0000301 if (execname)
302 xstat(execname, &execstat);
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}