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