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