[821] | 1 | /* 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>,
|
---|
| 6 | * Adapted for busybox David Kimdon <dwhedon@gordian.com>
|
---|
[1765] | 7 | *
|
---|
[2725] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 9 | */
|
---|
| 10 |
|
---|
[2725] | 11 | /*
|
---|
| 12 | This is how it is supposed to work:
|
---|
[821] | 13 |
|
---|
[2725] | 14 | start-stop-daemon [OPTIONS] [--start|--stop] [[--] arguments...]
|
---|
| 15 |
|
---|
| 16 | One (only) of these must be given:
|
---|
| 17 | -S,--start Start
|
---|
| 18 | -K,--stop Stop
|
---|
| 19 |
|
---|
| 20 | Search for matching processes.
|
---|
| 21 | If --stop is given, stop all matching processes (by sending a signal).
|
---|
| 22 | If --start is given, start a new process unless a matching process was found.
|
---|
| 23 |
|
---|
| 24 | Options controlling process matching
|
---|
| 25 | (if multiple conditions are specified, all must match):
|
---|
| 26 | -u,--user USERNAME|UID Only consider this user's processes
|
---|
| 27 | -n,--name PROCESS_NAME Look for processes by matching PROCESS_NAME
|
---|
| 28 | with comm field in /proc/$PID/stat.
|
---|
| 29 | Only basename is compared:
|
---|
| 30 | "ntpd" == "./ntpd" == "/path/to/ntpd".
|
---|
| 31 | [TODO: can PROCESS_NAME be a full pathname? Should we require full match then
|
---|
| 32 | with /proc/$PID/exe or argv[0] (comm can't be matched, it never contains path)]
|
---|
| 33 | -x,--exec EXECUTABLE Look for processes that were started with this
|
---|
| 34 | command in /proc/$PID/cmdline.
|
---|
| 35 | Unlike -n, we match against the full path:
|
---|
| 36 | "ntpd" != "./ntpd" != "/path/to/ntpd"
|
---|
| 37 | -p,--pidfile PID_FILE Look for processes with PID from this file
|
---|
| 38 |
|
---|
| 39 | Options which are valid for --start only:
|
---|
| 40 | -x,--exec EXECUTABLE Program to run (1st arg of execvp). Mandatory.
|
---|
| 41 | -a,--startas NAME argv[0] (defaults to EXECUTABLE)
|
---|
| 42 | -b,--background Put process into background
|
---|
| 43 | -N,--nicelevel N Add N to process' nice level
|
---|
| 44 | -c,--chuid USER[:[GRP]] Change to specified user [and group]
|
---|
| 45 | -m,--make-pidfile Write PID to the pidfile
|
---|
| 46 | (both -m and -p must be given!)
|
---|
| 47 |
|
---|
| 48 | Options which are valid for --stop only:
|
---|
| 49 | -s,--signal SIG Signal to send (default:TERM)
|
---|
| 50 | -t,--test Exit with status 0 if process is found
|
---|
| 51 | (we don't actually start or stop daemons)
|
---|
| 52 |
|
---|
| 53 | Misc options:
|
---|
| 54 | -o,--oknodo Exit with status 0 if nothing is done
|
---|
| 55 | -q,--quiet Quiet
|
---|
| 56 | -v,--verbose Verbose
|
---|
| 57 | */
|
---|
| 58 |
|
---|
[1765] | 59 | #include <sys/resource.h>
|
---|
| 60 |
|
---|
| 61 | /* Override ENABLE_FEATURE_PIDFILE */
|
---|
| 62 | #define WANT_PIDFILE 1
|
---|
| 63 | #include "libbb.h"
|
---|
| 64 |
|
---|
[821] | 65 | struct pid_list {
|
---|
| 66 | struct pid_list *next;
|
---|
| 67 | pid_t pid;
|
---|
| 68 | };
|
---|
| 69 |
|
---|
[2725] | 70 | enum {
|
---|
| 71 | CTX_STOP = (1 << 0),
|
---|
| 72 | CTX_START = (1 << 1),
|
---|
| 73 | OPT_BACKGROUND = (1 << 2), // -b
|
---|
| 74 | OPT_QUIET = (1 << 3), // -q
|
---|
| 75 | OPT_TEST = (1 << 4), // -t
|
---|
| 76 | OPT_MAKEPID = (1 << 5), // -m
|
---|
| 77 | OPT_a = (1 << 6), // -a
|
---|
| 78 | OPT_n = (1 << 7), // -n
|
---|
| 79 | OPT_s = (1 << 8), // -s
|
---|
| 80 | OPT_u = (1 << 9), // -u
|
---|
| 81 | OPT_c = (1 << 10), // -c
|
---|
| 82 | OPT_x = (1 << 11), // -x
|
---|
| 83 | OPT_p = (1 << 12), // -p
|
---|
| 84 | OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o
|
---|
| 85 | OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v
|
---|
| 86 | OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N
|
---|
| 87 | };
|
---|
| 88 | #define QUIET (option_mask32 & OPT_QUIET)
|
---|
| 89 | #define TEST (option_mask32 & OPT_TEST)
|
---|
[821] | 90 |
|
---|
[2725] | 91 | struct globals {
|
---|
| 92 | struct pid_list *found_procs;
|
---|
| 93 | char *userspec;
|
---|
| 94 | char *cmdname;
|
---|
| 95 | char *execname;
|
---|
| 96 | char *pidfile;
|
---|
| 97 | char *execname_cmpbuf;
|
---|
| 98 | unsigned execname_sizeof;
|
---|
| 99 | int user_id;
|
---|
| 100 | smallint signal_nr;
|
---|
| 101 | } FIX_ALIASING;
|
---|
| 102 | #define G (*(struct globals*)&bb_common_bufsiz1)
|
---|
| 103 | #define userspec (G.userspec )
|
---|
| 104 | #define cmdname (G.cmdname )
|
---|
| 105 | #define execname (G.execname )
|
---|
| 106 | #define pidfile (G.pidfile )
|
---|
| 107 | #define user_id (G.user_id )
|
---|
| 108 | #define signal_nr (G.signal_nr )
|
---|
| 109 | #define INIT_G() do { \
|
---|
| 110 | user_id = -1; \
|
---|
| 111 | signal_nr = 15; \
|
---|
| 112 | } while (0)
|
---|
| 113 |
|
---|
| 114 | #ifdef OLDER_VERSION_OF_X
|
---|
| 115 | /* -x,--exec EXECUTABLE
|
---|
| 116 | * Look for processes with matching /proc/$PID/exe.
|
---|
| 117 | * Match is performed using device+inode.
|
---|
| 118 | */
|
---|
| 119 | static int pid_is_exec(pid_t pid)
|
---|
[821] | 120 | {
|
---|
[2725] | 121 | struct stat st;
|
---|
| 122 | char buf[sizeof("/proc/%u/exe") + sizeof(int)*3];
|
---|
[821] | 123 |
|
---|
[2725] | 124 | sprintf(buf, "/proc/%u/exe", (unsigned)pid);
|
---|
| 125 | if (stat(buf, &st) < 0)
|
---|
| 126 | return 0;
|
---|
| 127 | if (st.st_dev == execstat.st_dev
|
---|
| 128 | && st.st_ino == execstat.st_ino)
|
---|
| 129 | return 1;
|
---|
| 130 | return 0;
|
---|
| 131 | }
|
---|
| 132 | #endif
|
---|
[821] | 133 |
|
---|
[2725] | 134 | static int pid_is_exec(pid_t pid)
|
---|
| 135 | {
|
---|
| 136 | ssize_t bytes;
|
---|
| 137 | char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
|
---|
| 138 |
|
---|
| 139 | sprintf(buf, "/proc/%u/cmdline", (unsigned)pid);
|
---|
| 140 | bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof);
|
---|
| 141 | if (bytes > 0) {
|
---|
| 142 | G.execname_cmpbuf[bytes] = '\0';
|
---|
| 143 | return strcmp(execname, G.execname_cmpbuf) == 0;
|
---|
| 144 | }
|
---|
| 145 | return 0;
|
---|
[821] | 146 | }
|
---|
| 147 |
|
---|
[2725] | 148 | static int pid_is_name(pid_t pid)
|
---|
[821] | 149 | {
|
---|
[2725] | 150 | /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */
|
---|
| 151 | char buf[32]; /* should be enough */
|
---|
| 152 | char *p, *pe;
|
---|
[821] | 153 |
|
---|
[2725] | 154 | sprintf(buf, "/proc/%u/stat", (unsigned)pid);
|
---|
| 155 | if (open_read_close(buf, buf, sizeof(buf) - 1) < 0)
|
---|
[821] | 156 | return 0;
|
---|
[2725] | 157 | buf[sizeof(buf) - 1] = '\0'; /* paranoia */
|
---|
| 158 | p = strchr(buf, '(');
|
---|
| 159 | if (!p)
|
---|
| 160 | return 0;
|
---|
| 161 | pe = strrchr(++p, ')');
|
---|
| 162 | if (!pe)
|
---|
| 163 | return 0;
|
---|
| 164 | *pe = '\0';
|
---|
| 165 | /* we require comm to match and to not be truncated */
|
---|
| 166 | /* in Linux, if comm is 15 chars, it may be a truncated
|
---|
| 167 | * name, so we don't allow that to match */
|
---|
| 168 | if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */
|
---|
| 169 | return 0;
|
---|
| 170 | return strcmp(p, cmdname) == 0;
|
---|
[821] | 171 | }
|
---|
| 172 |
|
---|
[2725] | 173 | static int pid_is_user(int pid)
|
---|
[821] | 174 | {
|
---|
[2725] | 175 | struct stat sb;
|
---|
| 176 | char buf[sizeof("/proc/") + sizeof(int)*3];
|
---|
[821] | 177 |
|
---|
[2725] | 178 | sprintf(buf, "/proc/%u", (unsigned)pid);
|
---|
| 179 | if (stat(buf, &sb) != 0)
|
---|
| 180 | return 0;
|
---|
| 181 | return (sb.st_uid == (uid_t)user_id);
|
---|
[821] | 182 | }
|
---|
| 183 |
|
---|
| 184 | static void check(int pid)
|
---|
| 185 | {
|
---|
[1765] | 186 | struct pid_list *p;
|
---|
| 187 |
|
---|
[2725] | 188 | if (execname && !pid_is_exec(pid)) {
|
---|
[821] | 189 | return;
|
---|
| 190 | }
|
---|
[2725] | 191 | if (cmdname && !pid_is_name(pid)) {
|
---|
[821] | 192 | return;
|
---|
| 193 | }
|
---|
[2725] | 194 | if (userspec && !pid_is_user(pid)) {
|
---|
[821] | 195 | return;
|
---|
| 196 | }
|
---|
[1765] | 197 | p = xmalloc(sizeof(*p));
|
---|
[2725] | 198 | p->next = G.found_procs;
|
---|
[1765] | 199 | p->pid = pid;
|
---|
[2725] | 200 | G.found_procs = p;
|
---|
[821] | 201 | }
|
---|
| 202 |
|
---|
| 203 | static void do_pidfile(void)
|
---|
| 204 | {
|
---|
| 205 | FILE *f;
|
---|
[1765] | 206 | unsigned pid;
|
---|
[821] | 207 |
|
---|
[2725] | 208 | f = fopen_for_read(pidfile);
|
---|
[821] | 209 | if (f) {
|
---|
[1765] | 210 | if (fscanf(f, "%u", &pid) == 1)
|
---|
[821] | 211 | check(pid);
|
---|
| 212 | fclose(f);
|
---|
| 213 | } else if (errno != ENOENT)
|
---|
| 214 | bb_perror_msg_and_die("open pidfile %s", pidfile);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | static void do_procinit(void)
|
---|
| 218 | {
|
---|
| 219 | DIR *procdir;
|
---|
| 220 | struct dirent *entry;
|
---|
[2725] | 221 | int pid;
|
---|
[821] | 222 |
|
---|
| 223 | if (pidfile) {
|
---|
| 224 | do_pidfile();
|
---|
| 225 | return;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[1765] | 228 | procdir = xopendir("/proc");
|
---|
[821] | 229 |
|
---|
[2725] | 230 | pid = 0;
|
---|
| 231 | while (1) {
|
---|
| 232 | errno = 0; /* clear any previous error */
|
---|
| 233 | entry = readdir(procdir);
|
---|
| 234 | // TODO: this check is too generic, it's better
|
---|
| 235 | // to check for exact errno(s) which mean that we got stale entry
|
---|
| 236 | if (errno) /* Stale entry, process has died after opendir */
|
---|
| 237 | continue;
|
---|
| 238 | if (!entry) /* EOF, no more entries */
|
---|
| 239 | break;
|
---|
[1765] | 240 | pid = bb_strtou(entry->d_name, NULL, 10);
|
---|
[2725] | 241 | if (errno) /* NaN */
|
---|
[821] | 242 | continue;
|
---|
| 243 | check(pid);
|
---|
| 244 | }
|
---|
| 245 | closedir(procdir);
|
---|
[2725] | 246 | if (!pid)
|
---|
[1765] | 247 | bb_error_msg_and_die("nothing in /proc - not mounted?");
|
---|
[821] | 248 | }
|
---|
| 249 |
|
---|
| 250 | static int do_stop(void)
|
---|
| 251 | {
|
---|
[1765] | 252 | char *what;
|
---|
[821] | 253 | struct pid_list *p;
|
---|
| 254 | int killed = 0;
|
---|
| 255 |
|
---|
[1765] | 256 | if (cmdname) {
|
---|
| 257 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname);
|
---|
| 258 | if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname;
|
---|
| 259 | } else if (execname) {
|
---|
| 260 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname);
|
---|
| 261 | if (!ENABLE_FEATURE_CLEAN_UP) what = execname;
|
---|
[2725] | 262 | } else if (pidfile) {
|
---|
[1765] | 263 | what = xasprintf("process in pidfile '%s'", pidfile);
|
---|
[2725] | 264 | } else if (userspec) {
|
---|
[1765] | 265 | what = xasprintf("process(es) owned by '%s'", userspec);
|
---|
[2725] | 266 | } else {
|
---|
[1765] | 267 | bb_error_msg_and_die("internal error, please report");
|
---|
[2725] | 268 | }
|
---|
[821] | 269 |
|
---|
[2725] | 270 | if (!G.found_procs) {
|
---|
| 271 | if (!QUIET)
|
---|
[1765] | 272 | printf("no %s found; none killed\n", what);
|
---|
| 273 | killed = -1;
|
---|
| 274 | goto ret;
|
---|
[821] | 275 | }
|
---|
[2725] | 276 | for (p = G.found_procs; p; p = p->next) {
|
---|
| 277 | if (TEST || kill(p->pid, signal_nr) == 0) {
|
---|
[821] | 278 | killed++;
|
---|
| 279 | } else {
|
---|
[2725] | 280 | p->pid = 0;
|
---|
| 281 | bb_perror_msg("warning: killing process %u", (unsigned)p->pid);
|
---|
[821] | 282 | }
|
---|
| 283 | }
|
---|
[2725] | 284 | if (!QUIET && killed) {
|
---|
[821] | 285 | printf("stopped %s (pid", what);
|
---|
[2725] | 286 | for (p = G.found_procs; p; p = p->next)
|
---|
| 287 | if (p->pid)
|
---|
| 288 | printf(" %u", (unsigned)p->pid);
|
---|
[1765] | 289 | puts(")");
|
---|
[821] | 290 | }
|
---|
[1765] | 291 | ret:
|
---|
[821] | 292 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
[1765] | 293 | free(what);
|
---|
[821] | 294 | return killed;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
|
---|
[1765] | 298 | static const char start_stop_daemon_longopts[] ALIGN1 =
|
---|
| 299 | "stop\0" No_argument "K"
|
---|
| 300 | "start\0" No_argument "S"
|
---|
| 301 | "background\0" No_argument "b"
|
---|
| 302 | "quiet\0" No_argument "q"
|
---|
[2725] | 303 | "test\0" No_argument "t"
|
---|
[1765] | 304 | "make-pidfile\0" No_argument "m"
|
---|
[821] | 305 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
[1765] | 306 | "oknodo\0" No_argument "o"
|
---|
| 307 | "verbose\0" No_argument "v"
|
---|
| 308 | "nicelevel\0" Required_argument "N"
|
---|
[821] | 309 | #endif
|
---|
[1765] | 310 | "startas\0" Required_argument "a"
|
---|
| 311 | "name\0" Required_argument "n"
|
---|
| 312 | "signal\0" Required_argument "s"
|
---|
| 313 | "user\0" Required_argument "u"
|
---|
| 314 | "chuid\0" Required_argument "c"
|
---|
| 315 | "exec\0" Required_argument "x"
|
---|
| 316 | "pidfile\0" Required_argument "p"
|
---|
[821] | 317 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
[1765] | 318 | "retry\0" Required_argument "R"
|
---|
[821] | 319 | #endif
|
---|
[1765] | 320 | ;
|
---|
[821] | 321 | #endif
|
---|
| 322 |
|
---|
[2725] | 323 | int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 324 | int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 325 | {
|
---|
[1765] | 326 | unsigned opt;
|
---|
| 327 | char *signame;
|
---|
| 328 | char *startas;
|
---|
| 329 | char *chuid;
|
---|
[2725] | 330 | #ifdef OLDER_VERSION_OF_X
|
---|
| 331 | struct stat execstat;
|
---|
| 332 | #endif
|
---|
[821] | 333 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
| 334 | // char *retry_arg = NULL;
|
---|
| 335 | // int retries = -1;
|
---|
[1765] | 336 | char *opt_N;
|
---|
[821] | 337 | #endif
|
---|
[2725] | 338 |
|
---|
| 339 | INIT_G();
|
---|
| 340 |
|
---|
[821] | 341 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
|
---|
[1765] | 342 | applet_long_options = start_stop_daemon_longopts;
|
---|
[821] | 343 | #endif
|
---|
| 344 |
|
---|
[2725] | 345 | /* -K or -S is required; they are mutually exclusive */
|
---|
| 346 | /* -p is required if -m is given */
|
---|
| 347 | /* -xpun (at least one) is required if -K is given */
|
---|
| 348 | /* -xa (at least one) is required if -S is given */
|
---|
| 349 | /* -q turns off -v */
|
---|
| 350 | opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"
|
---|
| 351 | IF_FEATURE_START_STOP_DAEMON_FANCY("q-v");
|
---|
| 352 | opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:"
|
---|
| 353 | IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"),
|
---|
[1765] | 354 | &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile
|
---|
[2725] | 355 | IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
|
---|
| 356 | /* We accept and ignore -R <param> / --retry <param> */
|
---|
| 357 | IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL)
|
---|
[1765] | 358 | );
|
---|
[821] | 359 |
|
---|
[1765] | 360 | if (opt & OPT_s) {
|
---|
| 361 | signal_nr = get_signum(signame);
|
---|
| 362 | if (signal_nr < 0) bb_show_usage();
|
---|
[821] | 363 | }
|
---|
| 364 |
|
---|
[1765] | 365 | if (!(opt & OPT_a))
|
---|
[821] | 366 | startas = execname;
|
---|
[2725] | 367 | if (!execname) /* in case -a is given and -x is not */
|
---|
| 368 | execname = startas;
|
---|
| 369 | if (execname) {
|
---|
| 370 | G.execname_sizeof = strlen(execname) + 1;
|
---|
| 371 | G.execname_cmpbuf = xmalloc(G.execname_sizeof + 1);
|
---|
| 372 | }
|
---|
[821] | 373 |
|
---|
[2725] | 374 | // IF_FEATURE_START_STOP_DAEMON_FANCY(
|
---|
[821] | 375 | // if (retry_arg)
|
---|
[2725] | 376 | // retries = xatoi_positive(retry_arg);
|
---|
[821] | 377 | // )
|
---|
[2725] | 378 | //argc -= optind;
|
---|
[821] | 379 | argv += optind;
|
---|
| 380 |
|
---|
[1765] | 381 | if (userspec) {
|
---|
| 382 | user_id = bb_strtou(userspec, NULL, 10);
|
---|
| 383 | if (errno)
|
---|
| 384 | user_id = xuname2uid(userspec);
|
---|
| 385 | }
|
---|
[2725] | 386 | /* Both start and stop need to know current processes */
|
---|
| 387 | do_procinit();
|
---|
[821] | 388 |
|
---|
[1765] | 389 | if (opt & CTX_STOP) {
|
---|
[821] | 390 | int i = do_stop();
|
---|
[1765] | 391 | return (opt & OPT_OKNODO) ? 0 : (i <= 0);
|
---|
[821] | 392 | }
|
---|
| 393 |
|
---|
[2725] | 394 | if (G.found_procs) {
|
---|
| 395 | if (!QUIET)
|
---|
| 396 | printf("%s is already running\n%u\n", execname, (unsigned)G.found_procs->pid);
|
---|
[1765] | 397 | return !(opt & OPT_OKNODO);
|
---|
[821] | 398 | }
|
---|
[2725] | 399 |
|
---|
| 400 | #ifdef OLDER_VERSION_OF_X
|
---|
| 401 | if (execname)
|
---|
| 402 | xstat(execname, &execstat);
|
---|
| 403 | #endif
|
---|
| 404 |
|
---|
[821] | 405 | *--argv = startas;
|
---|
[1765] | 406 | if (opt & OPT_BACKGROUND) {
|
---|
| 407 | #if BB_MMU
|
---|
[2725] | 408 | bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS);
|
---|
| 409 | /* DAEMON_DEVNULL_STDIO is superfluous -
|
---|
| 410 | * it's always done by bb_daemonize() */
|
---|
[1765] | 411 | #else
|
---|
[2725] | 412 | pid_t pid = xvfork();
|
---|
[1765] | 413 | if (pid != 0) {
|
---|
| 414 | /* parent */
|
---|
| 415 | /* why _exit? the child may have changed the stack,
|
---|
| 416 | * so "return 0" may do bad things */
|
---|
[2725] | 417 | _exit(EXIT_SUCCESS);
|
---|
[1765] | 418 | }
|
---|
[2725] | 419 | /* Child */
|
---|
[1765] | 420 | setsid(); /* detach from controlling tty */
|
---|
| 421 | /* Redirect stdio to /dev/null, close extra FDs.
|
---|
| 422 | * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */
|
---|
[2725] | 423 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO
|
---|
[1765] | 424 | + DAEMON_CLOSE_EXTRA_FDS
|
---|
| 425 | + DAEMON_ONLY_SANITIZE,
|
---|
| 426 | NULL /* argv, unused */ );
|
---|
| 427 | #endif
|
---|
[821] | 428 | }
|
---|
[1765] | 429 | if (opt & OPT_MAKEPID) {
|
---|
[2725] | 430 | /* User wants _us_ to make the pidfile */
|
---|
[1765] | 431 | write_pidfile(pidfile);
|
---|
[821] | 432 | }
|
---|
[1765] | 433 | if (opt & OPT_c) {
|
---|
[2725] | 434 | struct bb_uidgid_t ugid = { -1, -1 };
|
---|
[1765] | 435 | parse_chown_usergroup_or_die(&ugid, chuid);
|
---|
| 436 | if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid);
|
---|
| 437 | if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid);
|
---|
| 438 | }
|
---|
| 439 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
| 440 | if (opt & OPT_NICELEVEL) {
|
---|
| 441 | /* Set process priority */
|
---|
| 442 | int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
|
---|
| 443 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
|
---|
| 444 | bb_perror_msg_and_die("setpriority(%d)", prio);
|
---|
| 445 | }
|
---|
| 446 | }
|
---|
| 447 | #endif
|
---|
[2725] | 448 | execvp(startas, argv);
|
---|
| 449 | bb_perror_msg_and_die("can't execute '%s'", startas);
|
---|
[821] | 450 | }
|
---|