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