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