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>, |
| 6 | * public domain. |
| 7 | * Adapted for busybox David Kimdon <dwhedon@gordian.com> |
| 8 | */ |
| 9 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <signal.h> |
| 16 | #include <errno.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <dirent.h> |
| 19 | #include <unistd.h> |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 20 | #include <getopt.h> /* struct option */ |
Eric Andersen | 887ca79 | 2002-07-03 23:19:26 +0000 | [diff] [blame] | 21 | #include "pwd_.h" |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 22 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 23 | static int signal_nr = 15; |
| 24 | static int user_id = -1; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 25 | static int quiet; |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 26 | static char *userspec = NULL; |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 27 | static char *chuid = NULL; |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 28 | static char *cmdname = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 29 | static char *execname = NULL; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 30 | static char *pidfile = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 31 | |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 32 | struct pid_list { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 33 | struct pid_list *next; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 34 | pid_t pid; |
| 35 | }; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 37 | static struct pid_list *found = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 38 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 39 | static inline void push(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 40 | { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 41 | struct pid_list *p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 42 | |
| 43 | p = xmalloc(sizeof(*p)); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 44 | p->next = found; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 45 | p->pid = pid; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 46 | found = p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 49 | static int pid_is_exec(pid_t pid, const char *name) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 50 | { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 51 | char buf[32]; |
| 52 | struct stat sb, exec_stat; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 53 | |
Rob Landley | c5b1d4d | 2006-03-13 15:45:16 +0000 | [diff] [blame] | 54 | if (name) |
| 55 | xstat(name, &exec_stat); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 56 | |
| 57 | sprintf(buf, "/proc/%d/exe", pid); |
| 58 | if (stat(buf, &sb) != 0) |
| 59 | return 0; |
| 60 | return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 63 | static int pid_is_user(int pid, int uid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 64 | { |
| 65 | struct stat sb; |
| 66 | char buf[32]; |
| 67 | |
| 68 | sprintf(buf, "/proc/%d", pid); |
| 69 | if (stat(buf, &sb) != 0) |
| 70 | return 0; |
| 71 | return (sb.st_uid == uid); |
| 72 | } |
| 73 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 74 | static int pid_is_cmd(pid_t pid, const char *name) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 75 | { |
| 76 | char buf[32]; |
| 77 | FILE *f; |
| 78 | int c; |
| 79 | |
| 80 | sprintf(buf, "/proc/%d/stat", pid); |
| 81 | f = fopen(buf, "r"); |
| 82 | if (!f) |
| 83 | return 0; |
| 84 | while ((c = getc(f)) != EOF && c != '(') |
| 85 | ; |
| 86 | if (c != '(') { |
| 87 | fclose(f); |
| 88 | return 0; |
| 89 | } |
| 90 | /* this hopefully handles command names containing ')' */ |
| 91 | while ((c = getc(f)) != EOF && c == *name) |
| 92 | name++; |
| 93 | fclose(f); |
| 94 | return (c == ')' && *name == '\0'); |
| 95 | } |
| 96 | |
| 97 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 98 | static void check(int pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 99 | { |
| 100 | if (execname && !pid_is_exec(pid, execname)) { |
| 101 | return; |
| 102 | } |
| 103 | if (userspec && !pid_is_user(pid, user_id)) { |
| 104 | return; |
| 105 | } |
| 106 | if (cmdname && !pid_is_cmd(pid, cmdname)) { |
| 107 | return; |
| 108 | } |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 109 | push(pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 113 | static void do_pidfile(void) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 114 | { |
| 115 | FILE *f; |
| 116 | pid_t pid; |
| 117 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 118 | f = fopen(pidfile, "r"); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 119 | if (f) { |
| 120 | if (fscanf(f, "%d", &pid) == 1) |
| 121 | check(pid); |
| 122 | fclose(f); |
| 123 | } else if (errno != ENOENT) |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 124 | bb_perror_msg_and_die("open pidfile %s", pidfile); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 125 | |
| 126 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 127 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 128 | static void do_procinit(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 129 | { |
| 130 | DIR *procdir; |
| 131 | struct dirent *entry; |
| 132 | int foundany, pid; |
| 133 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 134 | if (pidfile) { |
| 135 | do_pidfile(); |
| 136 | return; |
| 137 | } |
| 138 | |
Bernhard Reutner-Fischer | cb44816 | 2006-04-12 07:35:12 +0000 | [diff] [blame] | 139 | procdir = bb_xopendir("/proc"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 140 | |
| 141 | foundany = 0; |
| 142 | while ((entry = readdir(procdir)) != NULL) { |
| 143 | if (sscanf(entry->d_name, "%d", &pid) != 1) |
| 144 | continue; |
| 145 | foundany++; |
| 146 | check(pid); |
| 147 | } |
| 148 | closedir(procdir); |
| 149 | if (!foundany) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | bb_error_msg_and_die ("nothing in /proc - not mounted?"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 154 | static int do_stop(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 155 | { |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 156 | RESERVE_CONFIG_BUFFER(what, 1024); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 157 | struct pid_list *p; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 158 | int killed = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 159 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 160 | do_procinit(); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 161 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 162 | if (cmdname) |
Bernhard Reutner-Fischer | 2f6a317 | 2006-06-12 14:34:01 +0000 | [diff] [blame] | 163 | strcpy(what, cmdname); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 164 | else if (execname) |
| 165 | strcpy(what, execname); |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 166 | else if (pidfile) |
| 167 | sprintf(what, "process in pidfile `%.200s'", pidfile); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 168 | else if (userspec) |
| 169 | sprintf(what, "process(es) owned by `%s'", userspec); |
| 170 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 171 | bb_error_msg_and_die ("internal error, please report"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 172 | |
| 173 | if (!found) { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 174 | if (!quiet) |
| 175 | printf("no %s found; none killed.\n", what); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 176 | if (ENABLE_FEATURE_CLEAN_UP) |
| 177 | RELEASE_CONFIG_BUFFER(what); |
| 178 | return -1; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 179 | } |
| 180 | for (p = found; p; p = p->next) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 181 | if (kill(p->pid, signal_nr) == 0) { |
| 182 | p->pid = -p->pid; |
| 183 | killed++; |
| 184 | } else { |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 185 | bb_perror_msg("warning: failed to kill %d", p->pid); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 186 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 187 | } |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 188 | if (!quiet && killed) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 189 | printf("stopped %s (pid", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 190 | for (p = found; p; p = p->next) |
| 191 | if(p->pid < 0) |
| 192 | printf(" %d", -p->pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 193 | printf(").\n"); |
| 194 | } |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 195 | if (ENABLE_FEATURE_CLEAN_UP) |
| 196 | RELEASE_CONFIG_BUFFER(what); |
| 197 | return killed; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 200 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 201 | static const struct option ssd_long_options[] = { |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 202 | { "stop", 0, NULL, 'K' }, |
| 203 | { "start", 0, NULL, 'S' }, |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 204 | { "background", 0, NULL, 'b' }, |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 205 | { "quiet", 0, NULL, 'q' }, |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 206 | { "make-pidfile", 0, NULL, 'm' }, |
| 207 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 208 | { "oknodo", 0, NULL, 'o' }, |
| 209 | { "verbose", 0, NULL, 'v' }, |
| 210 | #endif |
| 211 | { "startas", 1, NULL, 'a' }, |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 212 | { "name", 1, NULL, 'n' }, |
| 213 | { "signal", 1, NULL, 's' }, |
| 214 | { "user", 1, NULL, 'u' }, |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 215 | { "chuid", 1, NULL, 'c' }, |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 216 | { "exec", 1, NULL, 'x' }, |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 217 | { "pidfile", 1, NULL, 'p' }, |
| 218 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 219 | { "retry", 1, NULL, 'R' }, |
| 220 | #endif |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 221 | { 0, 0, 0, 0 } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 222 | }; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 223 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 224 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 225 | #define SSD_CTX_STOP 1 |
| 226 | #define SSD_CTX_START 2 |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 227 | #define SSD_OPT_BACKGROUND 4 |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 228 | #define SSD_OPT_QUIET 8 |
| 229 | #define SSD_OPT_MAKEPID 16 |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 230 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 231 | #define SSD_OPT_OKNODO 32 |
| 232 | #define SSD_OPT_VERBOSE 64 |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 233 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
| 236 | int start_stop_daemon_main(int argc, char **argv) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 237 | { |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 238 | unsigned long opt; |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 239 | char *signame = NULL; |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 240 | char *startas = NULL; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 241 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 242 | // char *retry_arg = NULL; |
| 243 | // int retries = -1; |
| 244 | #endif |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 245 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 246 | bb_applet_long_options = ssd_long_options; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 247 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 248 | |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 249 | /* Check required one context option was given */ |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 250 | bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa"; |
| 251 | opt = bb_getopt_ulflags(argc, argv, "KSbqm" |
| 252 | // USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:") |
| 253 | USE_FEATURE_START_STOP_DAEMON_FANCY("ov") |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 254 | "a:n:s:u:c:x:p:" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 255 | // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg) |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 256 | ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile); |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 257 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 258 | quiet = (opt & SSD_OPT_QUIET) |
| 259 | USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE)); |
Paul Fox | bb9a0ad | 2005-07-29 14:58:09 +0000 | [diff] [blame] | 260 | |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 261 | if (signame) { |
| 262 | signal_nr = bb_xgetlarg(signame, 10, 0, NSIG); |
| 263 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 264 | |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 265 | if (!startas) |
| 266 | startas = execname; |
| 267 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 268 | // USE_FEATURE_START_STOP_DAEMON_FANCY( |
| 269 | // if (retry_arg) |
| 270 | // retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX); |
| 271 | // ) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 272 | argc -= optind; |
| 273 | argv += optind; |
| 274 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 275 | if (userspec && sscanf(userspec, "%d", &user_id) != 1) |
Bernhard Reutner-Fischer | d5bd137 | 2005-09-20 21:06:17 +0000 | [diff] [blame] | 276 | user_id = bb_xgetpwnam(userspec); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 277 | |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 278 | if (opt & SSD_CTX_STOP) { |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 279 | int i = do_stop(); |
| 280 | return |
| 281 | USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO) |
| 282 | ? 0 :) !!(i<=0); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 285 | do_procinit(); |
| 286 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 287 | if (found) { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 288 | if (!quiet) |
| 289 | printf("%s already running.\n%d\n", execname ,found->pid); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 290 | USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);) |
| 291 | SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 292 | } |
| 293 | *--argv = startas; |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 294 | if (opt & SSD_OPT_BACKGROUND) { |
Bernhard Reutner-Fischer | 2c99851 | 2006-04-12 18:09:26 +0000 | [diff] [blame] | 295 | bb_xdaemon(0, 0); |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 296 | setsid(); |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 297 | } |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 298 | if (opt & SSD_OPT_MAKEPID) { |
| 299 | /* user wants _us_ to make the pidfile */ |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 300 | FILE *pidf = bb_xfopen(pidfile, "w"); |
| 301 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 302 | pid_t pidt = getpid(); |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 303 | fprintf(pidf, "%d\n", pidt); |
| 304 | fclose(pidf); |
| 305 | } |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 306 | if(chuid) { |
| 307 | if(sscanf(chuid, "%d", &user_id) != 1) |
| 308 | user_id = bb_xgetpwnam(chuid); |
| 309 | setuid(user_id); |
| 310 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 311 | execv(startas, argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 312 | bb_perror_msg_and_die ("unable to start %s", startas); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 313 | } |