Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 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>, |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 6 | * Adapted for busybox David Kimdon <dwhedon@gordian.com> |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 7 | * |
| 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 11 | /* NB: we have a problem here with /proc/NN/exe usage, similar to |
| 12 | * one fixed in killall/pidof */ |
| 13 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 14 | #include <getopt.h> |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 15 | #include <sys/resource.h> |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 17 | /* Override ENABLE_FEATURE_PIDFILE */ |
| 18 | #define WANT_PIDFILE 1 |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 19 | #include "libbb.h" |
| 20 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 21 | static int signal_nr = 15; |
| 22 | static int user_id = -1; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 23 | static char *userspec; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 24 | static char *cmdname; |
| 25 | static char *execname; |
| 26 | static char *pidfile; |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 27 | static smallint quiet; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 28 | |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 29 | struct pid_list { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 30 | struct pid_list *next; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 31 | pid_t pid; |
| 32 | }; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 34 | static struct pid_list *found; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 35 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 36 | static int pid_is_exec(pid_t pid, const char *name) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 37 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 38 | char buf[sizeof("/proc//exe") + sizeof(int)*3]; |
| 39 | char *execbuf; |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 40 | int n; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 42 | sprintf(buf, "/proc/%u/exe", pid); |
| 43 | n = strlen(name) + 1; |
| 44 | execbuf = xzalloc(n + 1); |
| 45 | readlink(buf, execbuf, n); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 47 | /* if readlink fails, execbuf still contains "" */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 48 | n = strcmp(execbuf, name); |
Rob Landley | 8cedaba | 2006-09-04 18:59:39 +0000 | [diff] [blame] | 49 | if (ENABLE_FEATURE_CLEAN_UP) |
| 50 | free(execbuf); |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 51 | return !n; /* nonzero (true) if execbuf == name */ |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 54 | static int pid_is_user(int pid, int uid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 55 | { |
| 56 | struct stat sb; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 57 | char buf[sizeof("/proc/") + sizeof(int)*3]; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 59 | sprintf(buf, "/proc/%u", pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 60 | if (stat(buf, &sb) != 0) |
| 61 | return 0; |
| 62 | return (sb.st_uid == uid); |
| 63 | } |
| 64 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 65 | static int pid_is_cmd(pid_t pid, const char *name) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 66 | { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 67 | char fname[sizeof("/proc//stat") + sizeof(int)*3]; |
| 68 | char *buf; |
| 69 | int r = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 71 | sprintf(fname, "/proc/%u/stat", pid); |
| 72 | buf = xmalloc_open_read_close(fname, NULL); |
| 73 | if (buf) { |
| 74 | char *p = strchr(buf, '('); |
| 75 | if (p) { |
| 76 | char *pe = strrchr(++p, ')'); |
| 77 | if (pe) { |
| 78 | *pe = '\0'; |
| 79 | r = !strcmp(p, name); |
| 80 | } |
| 81 | } |
| 82 | free(buf); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 83 | } |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 84 | return r; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 87 | static void check(int pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 88 | { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 89 | struct pid_list *p; |
| 90 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 91 | if (execname && !pid_is_exec(pid, execname)) { |
| 92 | return; |
| 93 | } |
| 94 | if (userspec && !pid_is_user(pid, user_id)) { |
| 95 | return; |
| 96 | } |
| 97 | if (cmdname && !pid_is_cmd(pid, cmdname)) { |
| 98 | return; |
| 99 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 100 | p = xmalloc(sizeof(*p)); |
| 101 | p->next = found; |
| 102 | p->pid = pid; |
| 103 | found = p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 106 | static void do_pidfile(void) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 107 | { |
| 108 | FILE *f; |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 109 | unsigned pid; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 110 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 111 | f = fopen(pidfile, "r"); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 112 | if (f) { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 113 | if (fscanf(f, "%u", &pid) == 1) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 114 | check(pid); |
| 115 | fclose(f); |
| 116 | } else if (errno != ENOENT) |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 117 | bb_perror_msg_and_die("open pidfile %s", pidfile); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 118 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 119 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 120 | static void do_procinit(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 121 | { |
| 122 | DIR *procdir; |
| 123 | struct dirent *entry; |
| 124 | int foundany, pid; |
| 125 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 126 | if (pidfile) { |
| 127 | do_pidfile(); |
| 128 | return; |
| 129 | } |
| 130 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 131 | procdir = xopendir("/proc"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 132 | |
| 133 | foundany = 0; |
| 134 | while ((entry = readdir(procdir)) != NULL) { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 135 | pid = bb_strtou(entry->d_name, NULL, 10); |
| 136 | if (errno) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 137 | continue; |
| 138 | foundany++; |
| 139 | check(pid); |
| 140 | } |
| 141 | closedir(procdir); |
| 142 | if (!foundany) |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 143 | bb_error_msg_and_die("nothing in /proc - not mounted?"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 146 | static int do_stop(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 147 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 148 | char *what; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 149 | struct pid_list *p; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 150 | int killed = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 151 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 152 | do_procinit(); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 153 | |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 154 | if (cmdname) { |
| 155 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname); |
| 156 | if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname; |
| 157 | } else if (execname) { |
| 158 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname); |
| 159 | if (!ENABLE_FEATURE_CLEAN_UP) what = execname; |
| 160 | } else if (pidfile) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 161 | what = xasprintf("process in pidfile '%s'", pidfile); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 162 | else if (userspec) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 163 | what = xasprintf("process(es) owned by '%s'", userspec); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 164 | else |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 165 | bb_error_msg_and_die("internal error, please report"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 166 | |
| 167 | if (!found) { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 168 | if (!quiet) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 169 | printf("no %s found; none killed\n", what); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 170 | killed = -1; |
| 171 | goto ret; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 172 | } |
| 173 | for (p = found; p; p = p->next) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 174 | if (kill(p->pid, signal_nr) == 0) { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 175 | p->pid = - p->pid; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 176 | killed++; |
| 177 | } else { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 178 | bb_perror_msg("warning: killing process %u", p->pid); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 179 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 180 | } |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 181 | if (!quiet && killed) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 182 | printf("stopped %s (pid", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 183 | for (p = found; p; p = p->next) |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 184 | if (p->pid < 0) |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 185 | printf(" %u", - p->pid); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 186 | puts(")"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 187 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 188 | ret: |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 189 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 190 | free(what); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 191 | return killed; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 194 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 195 | static const char start_stop_daemon_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 196 | "stop\0" No_argument "K" |
| 197 | "start\0" No_argument "S" |
| 198 | "background\0" No_argument "b" |
| 199 | "quiet\0" No_argument "q" |
| 200 | "make-pidfile\0" No_argument "m" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 201 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 202 | "oknodo\0" No_argument "o" |
| 203 | "verbose\0" No_argument "v" |
| 204 | "nicelevel\0" Required_argument "N" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 205 | #endif |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 206 | "startas\0" Required_argument "a" |
| 207 | "name\0" Required_argument "n" |
| 208 | "signal\0" Required_argument "s" |
| 209 | "user\0" Required_argument "u" |
| 210 | "chuid\0" Required_argument "c" |
| 211 | "exec\0" Required_argument "x" |
| 212 | "pidfile\0" Required_argument "p" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 213 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 214 | "retry\0" Required_argument "R" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 215 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 216 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 217 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 218 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 219 | enum { |
| 220 | CTX_STOP = 0x1, |
| 221 | CTX_START = 0x2, |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 222 | OPT_BACKGROUND = 0x4, // -b |
| 223 | OPT_QUIET = 0x8, // -q |
| 224 | OPT_MAKEPID = 0x10, // -m |
| 225 | OPT_a = 0x20, // -a |
| 226 | OPT_n = 0x40, // -n |
| 227 | OPT_s = 0x80, // -s |
| 228 | OPT_u = 0x100, // -u |
| 229 | OPT_c = 0x200, // -c |
| 230 | OPT_x = 0x400, // -x |
| 231 | OPT_p = 0x800, // -p |
| 232 | OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o |
| 233 | OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v |
| 234 | OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 235 | }; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 236 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 237 | int start_stop_daemon_main(int argc, char **argv); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 238 | int start_stop_daemon_main(int argc, char **argv) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 239 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 240 | unsigned opt; |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 241 | char *signame; |
| 242 | char *startas; |
| 243 | char *chuid; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 244 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 245 | // char *retry_arg = NULL; |
| 246 | // int retries = -1; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 247 | char *opt_N; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 248 | #endif |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 249 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 250 | applet_long_options = start_stop_daemon_longopts; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 251 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 252 | |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 253 | /* Check required one context option was given */ |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 254 | opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 255 | opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:" |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 256 | USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"), |
| 257 | // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"), |
| 258 | &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 259 | USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 260 | // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg) |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 261 | ); |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 262 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 263 | quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE); |
Paul Fox | bb9a0ad | 2005-07-29 14:58:09 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 265 | if (opt & OPT_s) { |
Rob Landley | 8479063 | 2006-08-28 20:30:27 +0000 | [diff] [blame] | 266 | signal_nr = get_signum(signame); |
| 267 | if (signal_nr < 0) bb_show_usage(); |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 268 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 269 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 270 | if (!(opt & OPT_a)) |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 271 | startas = execname; |
| 272 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 273 | // USE_FEATURE_START_STOP_DAEMON_FANCY( |
| 274 | // if (retry_arg) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 275 | // retries = xatoi_u(retry_arg); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 276 | // ) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 277 | argc -= optind; |
| 278 | argv += optind; |
| 279 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 280 | if (userspec) { |
| 281 | user_id = bb_strtou(userspec, NULL, 10); |
| 282 | if (errno) |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 283 | user_id = xuname2uid(userspec); |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 284 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 285 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 286 | if (opt & CTX_STOP) { |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 287 | int i = do_stop(); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 288 | return (opt & OPT_OKNODO) ? 0 : (i <= 0); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 291 | do_procinit(); |
| 292 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 293 | if (found) { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 294 | if (!quiet) |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 295 | printf("%s already running\n%d\n", execname, found->pid); |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 296 | return !(opt & OPT_OKNODO); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 297 | } |
| 298 | *--argv = startas; |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 299 | if (opt & OPT_BACKGROUND) { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 300 | #if BB_MMU |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 301 | bb_daemonize(0); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 302 | #else |
| 303 | pid_t pid = vfork(); |
| 304 | if (pid < 0) /* error */ |
| 305 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 306 | if (pid != 0) { |
| 307 | /* parent */ |
| 308 | /* why _exit? the child may have changed the stack, |
| 309 | * so "return 0" may do bad things */ |
| 310 | _exit(0); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 311 | } |
| 312 | /* child */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 313 | setsid(); /* detach from controlling tty */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 314 | /* Redirect stdio to /dev/null, close extra FDs. |
| 315 | * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */ |
| 316 | bb_daemonize_or_rexec( |
| 317 | DAEMON_DEVNULL_STDIO |
| 318 | + DAEMON_CLOSE_EXTRA_FDS |
| 319 | + DAEMON_ONLY_SANITIZE, |
| 320 | NULL /* argv, unused */ ); |
| 321 | #endif |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 322 | } |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 323 | if (opt & OPT_MAKEPID) { |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 324 | /* user wants _us_ to make the pidfile */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 325 | write_pidfile(pidfile); |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 326 | } |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 327 | if (opt & OPT_c) { |
| 328 | struct bb_uidgid_t ugid; |
| 329 | parse_chown_usergroup_or_die(&ugid, chuid); |
| 330 | if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid); |
| 331 | if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid); |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 332 | } |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 333 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 334 | if (opt & OPT_NICELEVEL) { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 335 | /* Set process priority */ |
| 336 | int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2); |
| 337 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
| 338 | bb_perror_msg_and_die("setpriority(%d)", prio); |
| 339 | } |
| 340 | } |
| 341 | #endif |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 342 | execv(startas, argv); |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 343 | bb_perror_msg_and_die("cannot start %s", startas); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 344 | } |