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 | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 11 | /* |
| 12 | This is how it is supposed to work: |
| 13 | |
| 14 | start-stop-daemon [OPTIONS] [--start|--stop] [[--] arguments...] |
| 15 | |
| 16 | One (only) of these must be given: |
| 17 | -S,--start Start |
| 18 | -K,--stop Stop |
| 19 | |
| 20 | Search for matching processes. |
| 21 | If --stop is given, stop all matching processes (by sending a signal). |
Denis Vlasenko | b67004b | 2008-06-20 18:24:14 +0000 | [diff] [blame] | 22 | If --start is given, start a new process unless a matching process was found. |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | b67004b | 2008-06-20 18:24:14 +0000 | [diff] [blame] | 24 | Options controlling process matching |
| 25 | (if multiple conditions are specified, all must match): |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 26 | -u,--user USERNAME|UID Only consider this user's processes |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 27 | -n,--name PROCESS_NAME Look for processes by matching PROCESS_NAME |
| 28 | with comm field in /proc/$PID/stat. |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 29 | Only basename is compared: |
| 30 | "ntpd" == "./ntpd" == "/path/to/ntpd". |
| 31 | [TODO: can PROCESS_NAME be a full pathname? Should we require full match then |
| 32 | with /proc/$PID/exe or argv[0] (comm can't be matched, it never contains path)] |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 33 | -x,--exec EXECUTABLE Look for processes that were started with this |
| 34 | command in /proc/$PID/cmdline. |
| 35 | Unlike -n, we match against the full path: |
| 36 | "ntpd" != "./ntpd" != "/path/to/ntpd" |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 37 | -p,--pidfile PID_FILE Look for processes with PID from this file |
| 38 | |
| 39 | Options which are valid for --start only: |
| 40 | -x,--exec EXECUTABLE Program to run (1st arg of execvp). Mandatory. |
| 41 | -a,--startas NAME argv[0] (defaults to EXECUTABLE) |
| 42 | -b,--background Put process into background |
| 43 | -N,--nicelevel N Add N to process' nice level |
| 44 | -c,--chuid USER[:[GRP]] Change to specified user [and group] |
| 45 | -m,--make-pidfile Write PID to the pidfile |
| 46 | (both -m and -p must be given!) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 47 | |
| 48 | Options which are valid for --stop only: |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 49 | -s,--signal SIG Signal to send (default:TERM) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 50 | -t,--test Exit with status 0 if process is found |
| 51 | (we don't actually start or stop daemons) |
| 52 | |
| 53 | Misc options: |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 54 | -o,--oknodo Exit with status 0 if nothing is done |
| 55 | -q,--quiet Quiet |
| 56 | -v,--verbose Verbose |
| 57 | */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 59 | #include <sys/resource.h> |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 61 | /* Override ENABLE_FEATURE_PIDFILE */ |
| 62 | #define WANT_PIDFILE 1 |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 63 | #include "libbb.h" |
| 64 | |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 65 | struct pid_list { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 66 | struct pid_list *next; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 67 | pid_t pid; |
| 68 | }; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 70 | enum { |
| 71 | CTX_STOP = (1 << 0), |
| 72 | CTX_START = (1 << 1), |
| 73 | OPT_BACKGROUND = (1 << 2), // -b |
| 74 | OPT_QUIET = (1 << 3), // -q |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 75 | OPT_TEST = (1 << 4), // -t |
| 76 | OPT_MAKEPID = (1 << 5), // -m |
| 77 | OPT_a = (1 << 6), // -a |
| 78 | OPT_n = (1 << 7), // -n |
| 79 | OPT_s = (1 << 8), // -s |
| 80 | OPT_u = (1 << 9), // -u |
| 81 | OPT_c = (1 << 10), // -c |
| 82 | OPT_x = (1 << 11), // -x |
| 83 | OPT_p = (1 << 12), // -p |
| 84 | OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o |
| 85 | OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v |
| 86 | OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 87 | }; |
| 88 | #define QUIET (option_mask32 & OPT_QUIET) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 89 | #define TEST (option_mask32 & OPT_TEST) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 90 | |
| 91 | struct globals { |
| 92 | struct pid_list *found; |
| 93 | char *userspec; |
| 94 | char *cmdname; |
| 95 | char *execname; |
| 96 | char *pidfile; |
| 97 | int user_id; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 98 | smallint signal_nr; |
| 99 | }; |
| 100 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 101 | #define found (G.found ) |
| 102 | #define userspec (G.userspec ) |
| 103 | #define cmdname (G.cmdname ) |
| 104 | #define execname (G.execname ) |
| 105 | #define pidfile (G.pidfile ) |
| 106 | #define user_id (G.user_id ) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 107 | #define signal_nr (G.signal_nr ) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 108 | #define INIT_G() do { \ |
| 109 | user_id = -1; \ |
| 110 | signal_nr = 15; \ |
| 111 | } while (0) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 113 | #ifdef OLDER_VERSION_OF_X |
| 114 | /* -x,--exec EXECUTABLE |
| 115 | * Look for processes with matching /proc/$PID/exe. |
| 116 | * Match is performed using device+inode. |
| 117 | */ |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame] | 118 | static int pid_is_exec(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 119 | { |
Denis Vlasenko | fe49347 | 2008-04-20 14:25:26 +0000 | [diff] [blame] | 120 | struct stat st; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 121 | char buf[sizeof("/proc//exe") + sizeof(int)*3]; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 122 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 123 | sprintf(buf, "/proc/%u/exe", (unsigned)pid); |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame] | 124 | if (stat(buf, &st) < 0) |
| 125 | return 0; |
| 126 | if (st.st_dev == execstat.st_dev |
| 127 | && st.st_ino == execstat.st_ino) |
| 128 | return 1; |
| 129 | return 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 130 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 131 | #endif |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 132 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 133 | static int pid_is_exec(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 134 | { |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 135 | ssize_t bytes; |
| 136 | char buf[PATH_MAX]; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 137 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 138 | sprintf(buf, "/proc/%u/cmdline", (unsigned)pid); |
| 139 | bytes = open_read_close(buf, buf, sizeof(buf) - 1); |
| 140 | if (bytes > 0) { |
| 141 | buf[bytes] = '\0'; |
| 142 | return strcmp(buf, execname) == 0; |
| 143 | } |
| 144 | return 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 147 | static int pid_is_name(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 148 | { |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 149 | /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */ |
| 150 | char buf[32]; /* should be enough */ |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 151 | char *p, *pe; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 152 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 153 | sprintf(buf, "/proc/%u/stat", (unsigned)pid); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 154 | if (open_read_close(buf, buf, sizeof(buf) - 1) < 0) |
| 155 | return 0; |
| 156 | buf[sizeof(buf) - 1] = '\0'; /* paranoia */ |
| 157 | p = strchr(buf, '('); |
| 158 | if (!p) |
| 159 | return 0; |
| 160 | pe = strrchr(++p, ')'); |
| 161 | if (!pe) |
| 162 | return 0; |
| 163 | *pe = '\0'; |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 164 | /* we require comm to match and to not be truncated */ |
| 165 | /* in Linux, if comm is 15 chars, it may be a truncated |
| 166 | * name, so we don't allow that to match */ |
| 167 | if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */ |
| 168 | return 0; |
| 169 | return strcmp(p, cmdname) == 0; |
| 170 | } |
| 171 | |
| 172 | static int pid_is_user(int pid) |
| 173 | { |
| 174 | struct stat sb; |
| 175 | char buf[sizeof("/proc/") + sizeof(int)*3]; |
| 176 | |
| 177 | sprintf(buf, "/proc/%u", (unsigned)pid); |
| 178 | if (stat(buf, &sb) != 0) |
| 179 | return 0; |
| 180 | return (sb.st_uid == (uid_t)user_id); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 183 | static void check(int pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 184 | { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 185 | struct pid_list *p; |
| 186 | |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame] | 187 | if (execname && !pid_is_exec(pid)) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 188 | return; |
| 189 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 190 | if (cmdname && !pid_is_name(pid)) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 191 | return; |
| 192 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 193 | if (userspec && !pid_is_user(pid)) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 194 | return; |
| 195 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 196 | p = xmalloc(sizeof(*p)); |
| 197 | p->next = found; |
| 198 | p->pid = pid; |
| 199 | found = p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 202 | static void do_pidfile(void) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 203 | { |
| 204 | FILE *f; |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 205 | unsigned pid; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 206 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 207 | f = fopen_for_read(pidfile); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 208 | if (f) { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 209 | if (fscanf(f, "%u", &pid) == 1) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 210 | check(pid); |
| 211 | fclose(f); |
| 212 | } else if (errno != ENOENT) |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 213 | bb_perror_msg_and_die("open pidfile %s", pidfile); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 214 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 215 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 216 | static void do_procinit(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 217 | { |
| 218 | DIR *procdir; |
| 219 | struct dirent *entry; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 220 | int pid; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 221 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 222 | if (pidfile) { |
| 223 | do_pidfile(); |
| 224 | return; |
| 225 | } |
| 226 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 227 | procdir = xopendir("/proc"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 228 | |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 229 | pid = 0; |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 230 | while (1) { |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 231 | errno = 0; /* clear any previous error */ |
| 232 | entry = readdir(procdir); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 233 | // TODO: this check is too generic, it's better |
| 234 | // to check for exact errno(s) which mean that we got stale entry |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 235 | if (errno) /* Stale entry, process has died after opendir */ |
| 236 | continue; |
| 237 | if (!entry) /* EOF, no more entries */ |
| 238 | break; |
| 239 | pid = bb_strtou(entry->d_name, NULL, 10); |
| 240 | if (errno) /* NaN */ |
| 241 | continue; |
| 242 | check(pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 243 | } |
| 244 | closedir(procdir); |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 245 | if (!pid) |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 246 | bb_error_msg_and_die("nothing in /proc - not mounted?"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 249 | static int do_stop(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 250 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 251 | char *what; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 252 | struct pid_list *p; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 253 | int killed = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 255 | if (cmdname) { |
| 256 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname); |
| 257 | if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname; |
| 258 | } else if (execname) { |
| 259 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname); |
| 260 | if (!ENABLE_FEATURE_CLEAN_UP) what = execname; |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 261 | } else if (pidfile) { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 262 | what = xasprintf("process in pidfile '%s'", pidfile); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 263 | } else if (userspec) { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 264 | what = xasprintf("process(es) owned by '%s'", userspec); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 265 | } else { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 266 | bb_error_msg_and_die("internal error, please report"); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 267 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 268 | |
| 269 | if (!found) { |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 270 | if (!QUIET) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 271 | printf("no %s found; none killed\n", what); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 272 | killed = -1; |
| 273 | goto ret; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 274 | } |
| 275 | for (p = found; p; p = p->next) { |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 276 | if (TEST || kill(p->pid, signal_nr) == 0) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 277 | killed++; |
| 278 | } else { |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 279 | p->pid = 0; |
| 280 | bb_perror_msg("warning: killing process %u", (unsigned)p->pid); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 281 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 282 | } |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 283 | if (!QUIET && killed) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 284 | printf("stopped %s (pid", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 285 | for (p = found; p; p = p->next) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 286 | if (p->pid) |
| 287 | printf(" %u", (unsigned)p->pid); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 288 | puts(")"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 289 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 290 | ret: |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 291 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 292 | free(what); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 293 | return killed; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 296 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 297 | static const char start_stop_daemon_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 298 | "stop\0" No_argument "K" |
| 299 | "start\0" No_argument "S" |
| 300 | "background\0" No_argument "b" |
| 301 | "quiet\0" No_argument "q" |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 302 | "test\0" No_argument "t" |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 303 | "make-pidfile\0" No_argument "m" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 304 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 305 | "oknodo\0" No_argument "o" |
| 306 | "verbose\0" No_argument "v" |
| 307 | "nicelevel\0" Required_argument "N" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 308 | #endif |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 309 | "startas\0" Required_argument "a" |
| 310 | "name\0" Required_argument "n" |
| 311 | "signal\0" Required_argument "s" |
| 312 | "user\0" Required_argument "u" |
| 313 | "chuid\0" Required_argument "c" |
| 314 | "exec\0" Required_argument "x" |
| 315 | "pidfile\0" Required_argument "p" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 316 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 317 | "retry\0" Required_argument "R" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 318 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 319 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 320 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 321 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 322 | int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 323 | int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 324 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 325 | unsigned opt; |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 326 | char *signame; |
| 327 | char *startas; |
| 328 | char *chuid; |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 329 | #ifdef OLDER_VERSION_OF_X |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 330 | struct stat execstat; |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 331 | #endif |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 332 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 333 | // char *retry_arg = NULL; |
| 334 | // int retries = -1; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 335 | char *opt_N; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 336 | #endif |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 337 | |
| 338 | INIT_G(); |
| 339 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 340 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 341 | applet_long_options = start_stop_daemon_longopts; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 342 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 344 | /* -K or -S is required; they are mutually exclusive */ |
| 345 | /* -p is required if -m is given */ |
| 346 | /* -xpun (at least one) is required if -K is given */ |
| 347 | /* -xa (at least one) is required if -S is given */ |
| 348 | /* -q turns off -v */ |
| 349 | opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa" |
| 350 | USE_FEATURE_START_STOP_DAEMON_FANCY("q-v"); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 351 | opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:" |
Denis Vlasenko | 75897ea | 2008-09-27 01:05:13 +0000 | [diff] [blame] | 352 | USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"), |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 353 | &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 354 | USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) |
Denis Vlasenko | 75897ea | 2008-09-27 01:05:13 +0000 | [diff] [blame] | 355 | /* We accept and ignore -R <param> / --retry <param> */ |
| 356 | USE_FEATURE_START_STOP_DAEMON_FANCY(,NULL) |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 357 | ); |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 358 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 359 | if (opt & OPT_s) { |
Rob Landley | 8479063 | 2006-08-28 20:30:27 +0000 | [diff] [blame] | 360 | signal_nr = get_signum(signame); |
| 361 | if (signal_nr < 0) bb_show_usage(); |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 362 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 363 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 364 | if (!(opt & OPT_a)) |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 365 | startas = execname; |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 366 | if (!execname) /* in case -a is given and -x is not */ |
| 367 | execname = startas; |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 368 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 369 | // USE_FEATURE_START_STOP_DAEMON_FANCY( |
| 370 | // if (retry_arg) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 371 | // retries = xatoi_u(retry_arg); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 372 | // ) |
Denis Vlasenko | 5a96c3e | 2008-04-19 17:40:29 +0000 | [diff] [blame] | 373 | //argc -= optind; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 374 | argv += optind; |
| 375 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 376 | if (userspec) { |
| 377 | user_id = bb_strtou(userspec, NULL, 10); |
| 378 | if (errno) |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 379 | user_id = xuname2uid(userspec); |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 380 | } |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 381 | /* Both start and stop need to know current processes */ |
| 382 | do_procinit(); |
Denis Vlasenko | 85d788e | 2008-04-19 21:30:52 +0000 | [diff] [blame] | 383 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 384 | if (opt & CTX_STOP) { |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 385 | int i = do_stop(); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 386 | return (opt & OPT_OKNODO) ? 0 : (i <= 0); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (found) { |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 390 | if (!QUIET) |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 391 | printf("%s is already running\n%u\n", execname, (unsigned)found->pid); |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 392 | return !(opt & OPT_OKNODO); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 393 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 394 | |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 395 | #ifdef OLDER_VERSION_OF_X |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 396 | if (execname) |
| 397 | xstat(execname, &execstat); |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 398 | #endif |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 399 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 400 | *--argv = startas; |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 401 | if (opt & OPT_BACKGROUND) { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 402 | #if BB_MMU |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 403 | bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS); |
| 404 | /* DAEMON_DEVNULL_STDIO is superfluous - |
| 405 | * it's always done by bb_daemonize() */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 406 | #else |
Denis Vlasenko | 82604e9 | 2008-07-01 15:59:42 +0000 | [diff] [blame] | 407 | pid_t pid = vfork(); |
| 408 | if (pid < 0) /* error */ |
| 409 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 410 | if (pid != 0) { |
| 411 | /* parent */ |
| 412 | /* why _exit? the child may have changed the stack, |
| 413 | * so "return 0" may do bad things */ |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 414 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 415 | } |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 416 | /* Child */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 417 | setsid(); /* detach from controlling tty */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 418 | /* Redirect stdio to /dev/null, close extra FDs. |
| 419 | * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */ |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 420 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO |
Denis Vlasenko | 148f67a | 2008-07-01 10:05:12 +0000 | [diff] [blame] | 421 | + DAEMON_CLOSE_EXTRA_FDS |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 422 | + DAEMON_ONLY_SANITIZE, |
| 423 | NULL /* argv, unused */ ); |
| 424 | #endif |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 425 | } |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 426 | if (opt & OPT_MAKEPID) { |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 427 | /* User wants _us_ to make the pidfile */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 428 | write_pidfile(pidfile); |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 429 | } |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 430 | if (opt & OPT_c) { |
Denis Vlasenko | 8d89bed | 2008-09-07 23:22:08 +0000 | [diff] [blame] | 431 | struct bb_uidgid_t ugid = { -1, -1 }; |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 432 | parse_chown_usergroup_or_die(&ugid, chuid); |
| 433 | if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid); |
| 434 | if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid); |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 435 | } |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 436 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 437 | if (opt & OPT_NICELEVEL) { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 438 | /* Set process priority */ |
| 439 | int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2); |
| 440 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
| 441 | bb_perror_msg_and_die("setpriority(%d)", prio); |
| 442 | } |
| 443 | } |
| 444 | #endif |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 445 | execvp(startas, argv); |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 446 | bb_perror_msg_and_die("cannot start %s", startas); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 447 | } |