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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 |
Denys Vlasenko | 17eedca | 2012-03-05 16:28:07 +0100 | [diff] [blame] | 34 | command in /proc/$PID/exe and /proc/$PID/cmdline |
| 35 | (/proc/$PID/cmdline is a bbox extension) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 36 | Unlike -n, we match against the full path: |
| 37 | "ntpd" != "./ntpd" != "/path/to/ntpd" |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 38 | -p,--pidfile PID_FILE Look for processes with PID from this file |
| 39 | |
| 40 | Options which are valid for --start only: |
| 41 | -x,--exec EXECUTABLE Program to run (1st arg of execvp). Mandatory. |
| 42 | -a,--startas NAME argv[0] (defaults to EXECUTABLE) |
| 43 | -b,--background Put process into background |
| 44 | -N,--nicelevel N Add N to process' nice level |
| 45 | -c,--chuid USER[:[GRP]] Change to specified user [and group] |
| 46 | -m,--make-pidfile Write PID to the pidfile |
| 47 | (both -m and -p must be given!) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 48 | |
| 49 | Options which are valid for --stop only: |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 50 | -s,--signal SIG Signal to send (default:TERM) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 51 | -t,--test Exit with status 0 if process is found |
| 52 | (we don't actually start or stop daemons) |
| 53 | |
| 54 | Misc options: |
Denis Vlasenko | 647c20c | 2008-05-07 14:52:01 +0000 | [diff] [blame] | 55 | -o,--oknodo Exit with status 0 if nothing is done |
| 56 | -q,--quiet Quiet |
| 57 | -v,--verbose Verbose |
| 58 | */ |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 59 | //config:config START_STOP_DAEMON |
| 60 | //config: bool "start-stop-daemon" |
| 61 | //config: default y |
| 62 | //config: help |
| 63 | //config: start-stop-daemon is used to control the creation and |
| 64 | //config: termination of system-level processes, usually the ones |
| 65 | //config: started during the startup of the system. |
| 66 | //config: |
| 67 | //config:config FEATURE_START_STOP_DAEMON_FANCY |
| 68 | //config: bool "Support additional arguments" |
| 69 | //config: default y |
| 70 | //config: depends on START_STOP_DAEMON |
| 71 | //config: help |
| 72 | //config: Support additional arguments. |
| 73 | //config: -o|--oknodo ignored since we exit with 0 anyway |
| 74 | //config: -v|--verbose |
| 75 | //config: -N|--nicelevel N |
| 76 | //config: |
| 77 | //config:config FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
| 78 | //config: bool "Enable long options" |
| 79 | //config: default y |
| 80 | //config: depends on START_STOP_DAEMON && LONG_OPTS |
| 81 | //config: help |
| 82 | //config: Support long options for the start-stop-daemon applet. |
| 83 | |
| 84 | //applet:IF_START_STOP_DAEMON(APPLET_ODDNAME(start-stop-daemon, start_stop_daemon, BB_DIR_SBIN, BB_SUID_DROP, start_stop_daemon)) |
| 85 | |
| 86 | //kbuild:lib-$(CONFIG_START_STOP_DAEMON) += start_stop_daemon.o |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 87 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 88 | //usage:#define start_stop_daemon_trivial_usage |
| 89 | //usage: "[OPTIONS] [-S|-K] ... [-- ARGS...]" |
| 90 | //usage:#define start_stop_daemon_full_usage "\n\n" |
| 91 | //usage: "Search for matching processes, and then\n" |
| 92 | //usage: "-K: stop all matching processes.\n" |
| 93 | //usage: "-S: start a process unless a matching process is found.\n" |
| 94 | //usage: IF_FEATURE_START_STOP_DAEMON_LONG_OPTIONS( |
| 95 | //usage: "\nProcess matching:" |
| 96 | //usage: "\n -u,--user USERNAME|UID Match only this user's processes" |
| 97 | //usage: "\n -n,--name NAME Match processes with NAME" |
| 98 | //usage: "\n in comm field in /proc/PID/stat" |
| 99 | //usage: "\n -x,--exec EXECUTABLE Match processes with this command" |
Denys Vlasenko | 17eedca | 2012-03-05 16:28:07 +0100 | [diff] [blame] | 100 | //usage: "\n in /proc/PID/{exe,cmdline}" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 101 | //usage: "\n -p,--pidfile FILE Match a process with PID from the file" |
| 102 | //usage: "\n All specified conditions must match" |
| 103 | //usage: "\n-S only:" |
| 104 | //usage: "\n -x,--exec EXECUTABLE Program to run" |
| 105 | //usage: "\n -a,--startas NAME Zeroth argument" |
| 106 | //usage: "\n -b,--background Background" |
| 107 | //usage: IF_FEATURE_START_STOP_DAEMON_FANCY( |
| 108 | //usage: "\n -N,--nicelevel N Change nice level" |
| 109 | //usage: ) |
| 110 | //usage: "\n -c,--chuid USER[:[GRP]] Change to user/group" |
| 111 | //usage: "\n -m,--make-pidfile Write PID to the pidfile specified by -p" |
| 112 | //usage: "\n-K only:" |
| 113 | //usage: "\n -s,--signal SIG Signal to send" |
| 114 | //usage: "\n -t,--test Match only, exit with 0 if a process is found" |
| 115 | //usage: "\nOther:" |
| 116 | //usage: IF_FEATURE_START_STOP_DAEMON_FANCY( |
| 117 | //usage: "\n -o,--oknodo Exit with status 0 if nothing is done" |
| 118 | //usage: "\n -v,--verbose Verbose" |
| 119 | //usage: ) |
| 120 | //usage: "\n -q,--quiet Quiet" |
| 121 | //usage: ) |
| 122 | //usage: IF_NOT_FEATURE_START_STOP_DAEMON_LONG_OPTIONS( |
| 123 | //usage: "\nProcess matching:" |
| 124 | //usage: "\n -u USERNAME|UID Match only this user's processes" |
| 125 | //usage: "\n -n NAME Match processes with NAME" |
| 126 | //usage: "\n in comm field in /proc/PID/stat" |
| 127 | //usage: "\n -x EXECUTABLE Match processes with this command" |
| 128 | //usage: "\n command in /proc/PID/cmdline" |
| 129 | //usage: "\n -p FILE Match a process with PID from the file" |
| 130 | //usage: "\n All specified conditions must match" |
| 131 | //usage: "\n-S only:" |
| 132 | //usage: "\n -x EXECUTABLE Program to run" |
| 133 | //usage: "\n -a NAME Zeroth argument" |
| 134 | //usage: "\n -b Background" |
| 135 | //usage: IF_FEATURE_START_STOP_DAEMON_FANCY( |
| 136 | //usage: "\n -N N Change nice level" |
| 137 | //usage: ) |
| 138 | //usage: "\n -c USER[:[GRP]] Change to user/group" |
| 139 | //usage: "\n -m Write PID to the pidfile specified by -p" |
| 140 | //usage: "\n-K only:" |
| 141 | //usage: "\n -s SIG Signal to send" |
| 142 | //usage: "\n -t Match only, exit with 0 if a process is found" |
| 143 | //usage: "\nOther:" |
| 144 | //usage: IF_FEATURE_START_STOP_DAEMON_FANCY( |
| 145 | //usage: "\n -o Exit with status 0 if nothing is done" |
| 146 | //usage: "\n -v Verbose" |
| 147 | //usage: ) |
| 148 | //usage: "\n -q Quiet" |
| 149 | //usage: ) |
| 150 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 151 | #include <sys/resource.h> |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 152 | |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 153 | /* Override ENABLE_FEATURE_PIDFILE */ |
| 154 | #define WANT_PIDFILE 1 |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 155 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 156 | #include "common_bufsiz.h" |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 157 | |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 158 | struct pid_list { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 159 | struct pid_list *next; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 160 | pid_t pid; |
| 161 | }; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 163 | enum { |
| 164 | CTX_STOP = (1 << 0), |
| 165 | CTX_START = (1 << 1), |
| 166 | OPT_BACKGROUND = (1 << 2), // -b |
| 167 | OPT_QUIET = (1 << 3), // -q |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 168 | OPT_TEST = (1 << 4), // -t |
| 169 | OPT_MAKEPID = (1 << 5), // -m |
| 170 | OPT_a = (1 << 6), // -a |
| 171 | OPT_n = (1 << 7), // -n |
| 172 | OPT_s = (1 << 8), // -s |
| 173 | OPT_u = (1 << 9), // -u |
| 174 | OPT_c = (1 << 10), // -c |
| 175 | OPT_x = (1 << 11), // -x |
| 176 | OPT_p = (1 << 12), // -p |
| 177 | OPT_OKNODO = (1 << 13) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o |
| 178 | OPT_VERBOSE = (1 << 14) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v |
| 179 | OPT_NICELEVEL = (1 << 15) * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 180 | }; |
| 181 | #define QUIET (option_mask32 & OPT_QUIET) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 182 | #define TEST (option_mask32 & OPT_TEST) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 183 | |
| 184 | struct globals { |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 185 | struct pid_list *found_procs; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 186 | char *userspec; |
| 187 | char *cmdname; |
| 188 | char *execname; |
| 189 | char *pidfile; |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 190 | char *execname_cmpbuf; |
| 191 | unsigned execname_sizeof; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 192 | int user_id; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 193 | smallint signal_nr; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 194 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 195 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 196 | #define userspec (G.userspec ) |
| 197 | #define cmdname (G.cmdname ) |
| 198 | #define execname (G.execname ) |
| 199 | #define pidfile (G.pidfile ) |
| 200 | #define user_id (G.user_id ) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 201 | #define signal_nr (G.signal_nr ) |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 202 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 203 | setup_common_bufsiz(); \ |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 204 | user_id = -1; \ |
| 205 | signal_nr = 15; \ |
| 206 | } while (0) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 207 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 208 | #ifdef OLDER_VERSION_OF_X |
| 209 | /* -x,--exec EXECUTABLE |
| 210 | * Look for processes with matching /proc/$PID/exe. |
| 211 | * Match is performed using device+inode. |
| 212 | */ |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame] | 213 | static int pid_is_exec(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 214 | { |
Denis Vlasenko | fe49347 | 2008-04-20 14:25:26 +0000 | [diff] [blame] | 215 | struct stat st; |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 216 | char buf[sizeof("/proc/%u/exe") + sizeof(int)*3]; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 217 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 218 | sprintf(buf, "/proc/%u/exe", (unsigned)pid); |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame] | 219 | if (stat(buf, &st) < 0) |
| 220 | return 0; |
| 221 | if (st.st_dev == execstat.st_dev |
| 222 | && st.st_ino == execstat.st_ino) |
| 223 | return 1; |
| 224 | return 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 225 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 226 | #endif |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 227 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 228 | static int pid_is_exec(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 229 | { |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 230 | ssize_t bytes; |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 231 | char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3]; |
Denys Vlasenko | 17eedca | 2012-03-05 16:28:07 +0100 | [diff] [blame] | 232 | char *procname, *exelink; |
| 233 | int match; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 234 | |
Denys Vlasenko | 17eedca | 2012-03-05 16:28:07 +0100 | [diff] [blame] | 235 | procname = buf + sprintf(buf, "/proc/%u/exe", (unsigned)pid) - 3; |
| 236 | |
| 237 | exelink = xmalloc_readlink(buf); |
| 238 | match = (exelink && strcmp(execname, exelink) == 0); |
| 239 | free(exelink); |
| 240 | if (match) |
| 241 | return match; |
| 242 | |
| 243 | strcpy(procname, "cmdline"); |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 244 | bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 245 | if (bytes > 0) { |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 246 | G.execname_cmpbuf[bytes] = '\0'; |
| 247 | return strcmp(execname, G.execname_cmpbuf) == 0; |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 248 | } |
| 249 | return 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 252 | static int pid_is_name(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 253 | { |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 254 | /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */ |
| 255 | char buf[32]; /* should be enough */ |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 256 | char *p, *pe; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 257 | |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 258 | sprintf(buf, "/proc/%u/stat", (unsigned)pid); |
Denis Vlasenko | 25cfe49 | 2008-04-20 01:27:59 +0000 | [diff] [blame] | 259 | if (open_read_close(buf, buf, sizeof(buf) - 1) < 0) |
| 260 | return 0; |
| 261 | buf[sizeof(buf) - 1] = '\0'; /* paranoia */ |
| 262 | p = strchr(buf, '('); |
| 263 | if (!p) |
| 264 | return 0; |
| 265 | pe = strrchr(++p, ')'); |
| 266 | if (!pe) |
| 267 | return 0; |
| 268 | *pe = '\0'; |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 269 | /* we require comm to match and to not be truncated */ |
| 270 | /* in Linux, if comm is 15 chars, it may be a truncated |
| 271 | * name, so we don't allow that to match */ |
| 272 | if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */ |
| 273 | return 0; |
| 274 | return strcmp(p, cmdname) == 0; |
| 275 | } |
| 276 | |
| 277 | static int pid_is_user(int pid) |
| 278 | { |
| 279 | struct stat sb; |
| 280 | char buf[sizeof("/proc/") + sizeof(int)*3]; |
| 281 | |
| 282 | sprintf(buf, "/proc/%u", (unsigned)pid); |
| 283 | if (stat(buf, &sb) != 0) |
| 284 | return 0; |
| 285 | return (sb.st_uid == (uid_t)user_id); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 288 | static void check(int pid) |
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 | struct pid_list *p; |
| 291 | |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame] | 292 | if (execname && !pid_is_exec(pid)) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 293 | return; |
| 294 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 295 | if (cmdname && !pid_is_name(pid)) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 296 | return; |
| 297 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 298 | if (userspec && !pid_is_user(pid)) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 299 | return; |
| 300 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 301 | p = xmalloc(sizeof(*p)); |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 302 | p->next = G.found_procs; |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 303 | p->pid = pid; |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 304 | G.found_procs = p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 307 | static void do_pidfile(void) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 308 | { |
| 309 | FILE *f; |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 310 | unsigned pid; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 311 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 312 | f = fopen_for_read(pidfile); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 313 | if (f) { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 314 | if (fscanf(f, "%u", &pid) == 1) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 315 | check(pid); |
| 316 | fclose(f); |
| 317 | } else if (errno != ENOENT) |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 318 | bb_perror_msg_and_die("open pidfile %s", pidfile); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 319 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 320 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 321 | static void do_procinit(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 322 | { |
| 323 | DIR *procdir; |
| 324 | struct dirent *entry; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 325 | int pid; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 326 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 327 | if (pidfile) { |
| 328 | do_pidfile(); |
| 329 | return; |
| 330 | } |
| 331 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 332 | procdir = xopendir("/proc"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 333 | |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 334 | pid = 0; |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 335 | while (1) { |
Denis Vlasenko | 1f22898 | 2008-04-22 00:16:29 +0000 | [diff] [blame] | 336 | errno = 0; /* clear any previous error */ |
| 337 | entry = readdir(procdir); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 338 | // TODO: this check is too generic, it's better |
| 339 | // 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] | 340 | if (errno) /* Stale entry, process has died after opendir */ |
| 341 | continue; |
| 342 | if (!entry) /* EOF, no more entries */ |
| 343 | break; |
| 344 | pid = bb_strtou(entry->d_name, NULL, 10); |
| 345 | if (errno) /* NaN */ |
| 346 | continue; |
| 347 | check(pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 348 | } |
| 349 | closedir(procdir); |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 350 | if (!pid) |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 351 | bb_error_msg_and_die("nothing in /proc - not mounted?"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 354 | static int do_stop(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 355 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 356 | char *what; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 357 | struct pid_list *p; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 358 | int killed = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 359 | |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 360 | if (cmdname) { |
| 361 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname); |
| 362 | if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname; |
| 363 | } else if (execname) { |
| 364 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname); |
| 365 | if (!ENABLE_FEATURE_CLEAN_UP) what = execname; |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 366 | } else if (pidfile) { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 367 | what = xasprintf("process in pidfile '%s'", pidfile); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 368 | } else if (userspec) { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 369 | what = xasprintf("process(es) owned by '%s'", userspec); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 370 | } else { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 371 | bb_error_msg_and_die("internal error, please report"); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 372 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 373 | |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 374 | if (!G.found_procs) { |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 375 | if (!QUIET) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 376 | printf("no %s found; none killed\n", what); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 377 | killed = -1; |
| 378 | goto ret; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 379 | } |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 380 | for (p = G.found_procs; p; p = p->next) { |
Denys Vlasenko | 929f63e | 2011-04-04 02:03:35 +0200 | [diff] [blame] | 381 | if (kill(p->pid, TEST ? 0 : signal_nr) == 0) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 382 | killed++; |
| 383 | } else { |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 384 | bb_perror_msg("warning: killing process %u", (unsigned)p->pid); |
Denys Vlasenko | 929f63e | 2011-04-04 02:03:35 +0200 | [diff] [blame] | 385 | p->pid = 0; |
| 386 | if (TEST) { |
| 387 | /* Example: -K --test --pidfile PIDFILE detected |
| 388 | * that PIDFILE's pid doesn't exist */ |
| 389 | killed = -1; |
| 390 | goto ret; |
| 391 | } |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 392 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 393 | } |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 394 | if (!QUIET && killed) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 395 | printf("stopped %s (pid", what); |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 396 | for (p = G.found_procs; p; p = p->next) |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 397 | if (p->pid) |
| 398 | printf(" %u", (unsigned)p->pid); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 399 | puts(")"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 400 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 401 | ret: |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 402 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 403 | free(what); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 404 | return killed; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 407 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 408 | static const char start_stop_daemon_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 409 | "stop\0" No_argument "K" |
| 410 | "start\0" No_argument "S" |
| 411 | "background\0" No_argument "b" |
| 412 | "quiet\0" No_argument "q" |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 413 | "test\0" No_argument "t" |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 414 | "make-pidfile\0" No_argument "m" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 415 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 416 | "oknodo\0" No_argument "o" |
| 417 | "verbose\0" No_argument "v" |
| 418 | "nicelevel\0" Required_argument "N" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 419 | #endif |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 420 | "startas\0" Required_argument "a" |
| 421 | "name\0" Required_argument "n" |
| 422 | "signal\0" Required_argument "s" |
| 423 | "user\0" Required_argument "u" |
| 424 | "chuid\0" Required_argument "c" |
| 425 | "exec\0" Required_argument "x" |
| 426 | "pidfile\0" Required_argument "p" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 427 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 428 | "retry\0" Required_argument "R" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 429 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 430 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 431 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 432 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 433 | int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 434 | int start_stop_daemon_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 435 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 436 | unsigned opt; |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 437 | char *signame; |
| 438 | char *startas; |
| 439 | char *chuid; |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 440 | #ifdef OLDER_VERSION_OF_X |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 441 | struct stat execstat; |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 442 | #endif |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 443 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 444 | // char *retry_arg = NULL; |
| 445 | // int retries = -1; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 446 | char *opt_N; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 447 | #endif |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 448 | |
| 449 | INIT_G(); |
| 450 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 451 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 452 | applet_long_options = start_stop_daemon_longopts; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 453 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 454 | |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 455 | /* -K or -S is required; they are mutually exclusive */ |
| 456 | /* -p is required if -m is given */ |
| 457 | /* -xpun (at least one) is required if -K is given */ |
| 458 | /* -xa (at least one) is required if -S is given */ |
| 459 | /* -q turns off -v */ |
| 460 | 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] | 461 | IF_FEATURE_START_STOP_DAEMON_FANCY("q-v"); |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 462 | opt = getopt32(argv, "KSbqtma:n:s:u:c:x:p:" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 463 | IF_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"), |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 464 | &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 465 | IF_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) |
Denis Vlasenko | 75897ea | 2008-09-27 01:05:13 +0000 | [diff] [blame] | 466 | /* We accept and ignore -R <param> / --retry <param> */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 467 | IF_FEATURE_START_STOP_DAEMON_FANCY(,NULL) |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 468 | ); |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 469 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 470 | if (opt & OPT_s) { |
Rob Landley | 8479063 | 2006-08-28 20:30:27 +0000 | [diff] [blame] | 471 | signal_nr = get_signum(signame); |
| 472 | if (signal_nr < 0) bb_show_usage(); |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 473 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 474 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 475 | if (!(opt & OPT_a)) |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 476 | startas = execname; |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 477 | if (!execname) /* in case -a is given and -x is not */ |
| 478 | execname = startas; |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 479 | if (execname) { |
| 480 | G.execname_sizeof = strlen(execname) + 1; |
| 481 | G.execname_cmpbuf = xmalloc(G.execname_sizeof + 1); |
| 482 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 483 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 484 | // IF_FEATURE_START_STOP_DAEMON_FANCY( |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 485 | // if (retry_arg) |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 486 | // retries = xatoi_positive(retry_arg); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 487 | // ) |
Denis Vlasenko | 5a96c3e | 2008-04-19 17:40:29 +0000 | [diff] [blame] | 488 | //argc -= optind; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 489 | argv += optind; |
| 490 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 491 | if (userspec) { |
| 492 | user_id = bb_strtou(userspec, NULL, 10); |
| 493 | if (errno) |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 494 | user_id = xuname2uid(userspec); |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 495 | } |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 496 | /* Both start and stop need to know current processes */ |
| 497 | do_procinit(); |
Denis Vlasenko | 85d788e | 2008-04-19 21:30:52 +0000 | [diff] [blame] | 498 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 499 | if (opt & CTX_STOP) { |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 500 | int i = do_stop(); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 501 | return (opt & OPT_OKNODO) ? 0 : (i <= 0); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 504 | if (G.found_procs) { |
Denis Vlasenko | e061226 | 2008-04-30 13:58:31 +0000 | [diff] [blame] | 505 | if (!QUIET) |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 506 | 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] | 507 | return !(opt & OPT_OKNODO); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 508 | } |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 509 | |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 510 | #ifdef OLDER_VERSION_OF_X |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 511 | if (execname) |
| 512 | xstat(execname, &execstat); |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 513 | #endif |
Denis Vlasenko | e125a68 | 2008-05-18 21:17:52 +0000 | [diff] [blame] | 514 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 515 | *--argv = startas; |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 516 | if (opt & OPT_BACKGROUND) { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 517 | #if BB_MMU |
Peter Korsgaard | 743edac | 2011-11-09 19:44:37 +0100 | [diff] [blame] | 518 | bb_daemonize(DAEMON_DEVNULL_STDIO + DAEMON_CLOSE_EXTRA_FDS + DAEMON_DOUBLE_FORK); |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 519 | /* DAEMON_DEVNULL_STDIO is superfluous - |
| 520 | * it's always done by bb_daemonize() */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 521 | #else |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 522 | pid_t pid = xvfork(); |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 523 | if (pid != 0) { |
| 524 | /* parent */ |
| 525 | /* why _exit? the child may have changed the stack, |
| 526 | * so "return 0" may do bad things */ |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 527 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 528 | } |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 529 | /* Child */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 530 | setsid(); /* detach from controlling tty */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 531 | /* Redirect stdio to /dev/null, close extra FDs. |
| 532 | * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */ |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 533 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO |
Denis Vlasenko | 148f67a | 2008-07-01 10:05:12 +0000 | [diff] [blame] | 534 | + DAEMON_CLOSE_EXTRA_FDS |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 535 | + DAEMON_ONLY_SANITIZE, |
| 536 | NULL /* argv, unused */ ); |
| 537 | #endif |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 538 | } |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 539 | if (opt & OPT_MAKEPID) { |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 540 | /* User wants _us_ to make the pidfile */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 541 | write_pidfile(pidfile); |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 542 | } |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 543 | if (opt & OPT_c) { |
Denys Vlasenko | 3d0805e | 2015-10-19 04:37:19 +0200 | [diff] [blame] | 544 | struct bb_uidgid_t ugid; |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 545 | parse_chown_usergroup_or_die(&ugid, chuid); |
Denys Vlasenko | 3d0805e | 2015-10-19 04:37:19 +0200 | [diff] [blame] | 546 | if (ugid.uid != (uid_t) -1L) { |
Denys Vlasenko | 585541e | 2011-09-15 18:27:05 +0200 | [diff] [blame] | 547 | struct passwd *pw = xgetpwuid(ugid.uid); |
Denys Vlasenko | 3d0805e | 2015-10-19 04:37:19 +0200 | [diff] [blame] | 548 | if (ugid.gid != (gid_t) -1L) |
Denys Vlasenko | 585541e | 2011-09-15 18:27:05 +0200 | [diff] [blame] | 549 | pw->pw_gid = ugid.gid; |
| 550 | /* initgroups, setgid, setuid: */ |
| 551 | change_identity(pw); |
Denys Vlasenko | 3d0805e | 2015-10-19 04:37:19 +0200 | [diff] [blame] | 552 | } else if (ugid.gid != (gid_t) -1L) { |
Denys Vlasenko | 585541e | 2011-09-15 18:27:05 +0200 | [diff] [blame] | 553 | xsetgid(ugid.gid); |
| 554 | setgroups(1, &ugid.gid); |
| 555 | } |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 556 | } |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 557 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 558 | if (opt & OPT_NICELEVEL) { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 559 | /* Set process priority */ |
| 560 | int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2); |
| 561 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
| 562 | bb_perror_msg_and_die("setpriority(%d)", prio); |
| 563 | } |
| 564 | } |
| 565 | #endif |
Denis Vlasenko | 7987a18 | 2008-07-01 10:00:46 +0000 | [diff] [blame] | 566 | execvp(startas, argv); |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 567 | bb_perror_msg_and_die("can't execute '%s'", startas); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 568 | } |