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 | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 11 | /* NB: we have a problem here with /proc/NN/exe usage, similar to |
| 12 | * one fixed in killall/pidof */ |
| 13 | |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 14 | #include <sys/resource.h> |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 16 | /* Override ENABLE_FEATURE_PIDFILE */ |
| 17 | #define WANT_PIDFILE 1 |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
| 19 | |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 20 | struct pid_list { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 21 | struct pid_list *next; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 22 | pid_t pid; |
| 23 | }; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 25 | |
| 26 | struct globals { |
| 27 | struct pid_list *found; |
| 28 | char *userspec; |
| 29 | char *cmdname; |
| 30 | char *execname; |
| 31 | char *pidfile; |
| 32 | int user_id; |
| 33 | smallint quiet; |
| 34 | smallint signal_nr; |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame^] | 35 | struct stat execstat; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 36 | }; |
| 37 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 38 | #define found (G.found ) |
| 39 | #define userspec (G.userspec ) |
| 40 | #define cmdname (G.cmdname ) |
| 41 | #define execname (G.execname ) |
| 42 | #define pidfile (G.pidfile ) |
| 43 | #define user_id (G.user_id ) |
| 44 | #define quiet (G.quiet ) |
| 45 | #define signal_nr (G.signal_nr ) |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame^] | 46 | #define execstat (G.execstat ) |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 47 | #define INIT_G() \ |
| 48 | do { \ |
| 49 | user_id = -1; \ |
| 50 | signal_nr = 15; \ |
| 51 | } while (0) |
| 52 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame^] | 54 | static int pid_is_exec(pid_t pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 55 | { |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame^] | 56 | struct stat st; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 57 | char buf[sizeof("/proc//exe") + sizeof(int)*3]; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 59 | sprintf(buf, "/proc/%u/exe", pid); |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame^] | 60 | if (stat(buf, &st) < 0) |
| 61 | return 0; |
| 62 | if (st.st_dev == execstat.st_dev |
| 63 | && st.st_ino == execstat.st_ino) |
| 64 | return 1; |
| 65 | return 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 68 | static int pid_is_user(int pid, int uid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 69 | { |
| 70 | struct stat sb; |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 71 | char buf[sizeof("/proc/") + sizeof(int)*3]; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 73 | sprintf(buf, "/proc/%u", pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 74 | if (stat(buf, &sb) != 0) |
| 75 | return 0; |
| 76 | return (sb.st_uid == uid); |
| 77 | } |
| 78 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 79 | static int pid_is_cmd(pid_t pid, const char *name) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 80 | { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 81 | char fname[sizeof("/proc//stat") + sizeof(int)*3]; |
| 82 | char *buf; |
| 83 | int r = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 84 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 85 | sprintf(fname, "/proc/%u/stat", pid); |
| 86 | buf = xmalloc_open_read_close(fname, NULL); |
| 87 | if (buf) { |
| 88 | char *p = strchr(buf, '('); |
| 89 | if (p) { |
| 90 | char *pe = strrchr(++p, ')'); |
| 91 | if (pe) { |
| 92 | *pe = '\0'; |
| 93 | r = !strcmp(p, name); |
| 94 | } |
| 95 | } |
| 96 | free(buf); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 97 | } |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 98 | return r; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 101 | static void check(int pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 102 | { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 103 | struct pid_list *p; |
| 104 | |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame^] | 105 | if (execname && !pid_is_exec(pid)) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | if (userspec && !pid_is_user(pid, user_id)) { |
| 109 | return; |
| 110 | } |
| 111 | if (cmdname && !pid_is_cmd(pid, cmdname)) { |
| 112 | return; |
| 113 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 114 | p = xmalloc(sizeof(*p)); |
| 115 | p->next = found; |
| 116 | p->pid = pid; |
| 117 | found = p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 120 | static void do_pidfile(void) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 121 | { |
| 122 | FILE *f; |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 123 | unsigned pid; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 124 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 125 | f = fopen(pidfile, "r"); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 126 | if (f) { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 127 | if (fscanf(f, "%u", &pid) == 1) |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 128 | check(pid); |
| 129 | fclose(f); |
| 130 | } else if (errno != ENOENT) |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 131 | bb_perror_msg_and_die("open pidfile %s", pidfile); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 132 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 133 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 134 | static void do_procinit(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 135 | { |
| 136 | DIR *procdir; |
| 137 | struct dirent *entry; |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 138 | int pid; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 139 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 140 | if (pidfile) { |
| 141 | do_pidfile(); |
| 142 | return; |
| 143 | } |
| 144 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 145 | procdir = xopendir("/proc"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 147 | pid = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 148 | while ((entry = readdir(procdir)) != NULL) { |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 149 | pid = bb_strtou(entry->d_name, NULL, 10); |
| 150 | if (errno) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 151 | continue; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 152 | check(pid); |
| 153 | } |
| 154 | closedir(procdir); |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 155 | if (!pid) |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 156 | bb_error_msg_and_die("nothing in /proc - not mounted?"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 159 | static int do_stop(void) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 160 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 161 | char *what; |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 162 | struct pid_list *p; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 163 | int killed = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 164 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 165 | do_procinit(); |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 166 | |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 167 | if (cmdname) { |
| 168 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(cmdname); |
| 169 | if (!ENABLE_FEATURE_CLEAN_UP) what = cmdname; |
| 170 | } else if (execname) { |
| 171 | if (ENABLE_FEATURE_CLEAN_UP) what = xstrdup(execname); |
| 172 | if (!ENABLE_FEATURE_CLEAN_UP) what = execname; |
| 173 | } else if (pidfile) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 174 | what = xasprintf("process in pidfile '%s'", pidfile); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 175 | else if (userspec) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 176 | what = xasprintf("process(es) owned by '%s'", userspec); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 177 | else |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 178 | bb_error_msg_and_die("internal error, please report"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 179 | |
| 180 | if (!found) { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 181 | if (!quiet) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 182 | printf("no %s found; none killed\n", what); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 183 | killed = -1; |
| 184 | goto ret; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 185 | } |
| 186 | for (p = found; p; p = p->next) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 187 | if (kill(p->pid, signal_nr) == 0) { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 188 | p->pid = - p->pid; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 189 | killed++; |
| 190 | } else { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 191 | bb_perror_msg("warning: killing process %u", p->pid); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 192 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 193 | } |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 194 | if (!quiet && killed) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 195 | printf("stopped %s (pid", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 196 | for (p = found; p; p = p->next) |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 197 | if (p->pid < 0) |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 198 | printf(" %u", - p->pid); |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 199 | puts(")"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 200 | } |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 201 | ret: |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 202 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 203 | free(what); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 204 | return killed; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 207 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 208 | static const char start_stop_daemon_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 209 | "stop\0" No_argument "K" |
| 210 | "start\0" No_argument "S" |
| 211 | "background\0" No_argument "b" |
| 212 | "quiet\0" No_argument "q" |
| 213 | "make-pidfile\0" No_argument "m" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 214 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 215 | "oknodo\0" No_argument "o" |
| 216 | "verbose\0" No_argument "v" |
| 217 | "nicelevel\0" Required_argument "N" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 218 | #endif |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 219 | "startas\0" Required_argument "a" |
| 220 | "name\0" Required_argument "n" |
| 221 | "signal\0" Required_argument "s" |
| 222 | "user\0" Required_argument "u" |
| 223 | "chuid\0" Required_argument "c" |
| 224 | "exec\0" Required_argument "x" |
| 225 | "pidfile\0" Required_argument "p" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 226 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 227 | "retry\0" Required_argument "R" |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 228 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 229 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 230 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 231 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 232 | enum { |
| 233 | CTX_STOP = 0x1, |
| 234 | CTX_START = 0x2, |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 235 | OPT_BACKGROUND = 0x4, // -b |
| 236 | OPT_QUIET = 0x8, // -q |
| 237 | OPT_MAKEPID = 0x10, // -m |
| 238 | OPT_a = 0x20, // -a |
| 239 | OPT_n = 0x40, // -n |
| 240 | OPT_s = 0x80, // -s |
| 241 | OPT_u = 0x100, // -u |
| 242 | OPT_c = 0x200, // -c |
| 243 | OPT_x = 0x400, // -x |
| 244 | OPT_p = 0x800, // -p |
| 245 | OPT_OKNODO = 0x1000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -o |
| 246 | OPT_VERBOSE = 0x2000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -v |
| 247 | OPT_NICELEVEL = 0x4000 * ENABLE_FEATURE_START_STOP_DAEMON_FANCY, // -N |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 248 | }; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 249 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 250 | int start_stop_daemon_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 5a96c3e | 2008-04-19 17:40:29 +0000 | [diff] [blame] | 251 | int start_stop_daemon_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 252 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 253 | unsigned opt; |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 254 | char *signame; |
| 255 | char *startas; |
| 256 | char *chuid; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 257 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
| 258 | // char *retry_arg = NULL; |
| 259 | // int retries = -1; |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 260 | char *opt_N; |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 261 | #endif |
Denis Vlasenko | 04bb2d2 | 2008-02-26 16:08:02 +0000 | [diff] [blame] | 262 | |
| 263 | INIT_G(); |
| 264 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 265 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 266 | applet_long_options = start_stop_daemon_longopts; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 267 | #endif |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 268 | |
"Vladimir N. Oleynik" | 064f04e | 2005-10-11 14:38:01 +0000 | [diff] [blame] | 269 | /* Check required one context option was given */ |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 270 | opt_complementary = "K:S:K--S:S--K:m?p:K?xpun:S?xa"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 271 | opt = getopt32(argv, "KSbqma:n:s:u:c:x:p:" |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 272 | USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:"), |
| 273 | // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:"), |
| 274 | &startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 275 | USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N) |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 276 | // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg) |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 277 | ); |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 278 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 279 | quiet = (opt & OPT_QUIET) && !(opt & OPT_VERBOSE); |
Paul Fox | bb9a0ad | 2005-07-29 14:58:09 +0000 | [diff] [blame] | 280 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 281 | if (opt & OPT_s) { |
Rob Landley | 8479063 | 2006-08-28 20:30:27 +0000 | [diff] [blame] | 282 | signal_nr = get_signum(signame); |
| 283 | if (signal_nr < 0) bb_show_usage(); |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 284 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 285 | |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 286 | if (!(opt & OPT_a)) |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 287 | startas = execname; |
| 288 | |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 289 | // USE_FEATURE_START_STOP_DAEMON_FANCY( |
| 290 | // if (retry_arg) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 291 | // retries = xatoi_u(retry_arg); |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 292 | // ) |
Denis Vlasenko | 5a96c3e | 2008-04-19 17:40:29 +0000 | [diff] [blame] | 293 | //argc -= optind; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 294 | argv += optind; |
| 295 | |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 296 | if (userspec) { |
| 297 | user_id = bb_strtou(userspec, NULL, 10); |
| 298 | if (errno) |
Denis Vlasenko | 9a44c4f | 2006-12-28 05:44:47 +0000 | [diff] [blame] | 299 | user_id = xuname2uid(userspec); |
Denis Vlasenko | b131b27 | 2006-12-17 17:30:01 +0000 | [diff] [blame] | 300 | } |
Denis Vlasenko | d9c51e9 | 2008-04-19 19:06:23 +0000 | [diff] [blame^] | 301 | if (execname) |
| 302 | xstat(execname, &execstat); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 303 | |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 304 | if (opt & CTX_STOP) { |
Bernhard Reutner-Fischer | a926f8e | 2006-06-11 17:24:01 +0000 | [diff] [blame] | 305 | int i = do_stop(); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 306 | return (opt & OPT_OKNODO) ? 0 : (i <= 0); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 309 | do_procinit(); |
| 310 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 311 | if (found) { |
Eric Andersen | 63a1a7a | 2004-03-13 08:33:10 +0000 | [diff] [blame] | 312 | if (!quiet) |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 313 | printf("%s already running\n%d\n", execname, found->pid); |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 314 | return !(opt & OPT_OKNODO); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 315 | } |
| 316 | *--argv = startas; |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 317 | if (opt & OPT_BACKGROUND) { |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 318 | #if BB_MMU |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 319 | bb_daemonize(0); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 320 | #else |
| 321 | pid_t pid = vfork(); |
| 322 | if (pid < 0) /* error */ |
| 323 | bb_perror_msg_and_die("vfork"); |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 324 | if (pid != 0) { |
| 325 | /* parent */ |
| 326 | /* why _exit? the child may have changed the stack, |
| 327 | * so "return 0" may do bad things */ |
| 328 | _exit(0); |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 329 | } |
| 330 | /* child */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 331 | setsid(); /* detach from controlling tty */ |
Denis Vlasenko | a1b16f4 | 2007-07-31 17:09:44 +0000 | [diff] [blame] | 332 | /* Redirect stdio to /dev/null, close extra FDs. |
| 333 | * We do not actually daemonize because of DAEMON_ONLY_SANITIZE */ |
| 334 | bb_daemonize_or_rexec( |
| 335 | DAEMON_DEVNULL_STDIO |
| 336 | + DAEMON_CLOSE_EXTRA_FDS |
| 337 | + DAEMON_ONLY_SANITIZE, |
| 338 | NULL /* argv, unused */ ); |
| 339 | #endif |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 340 | } |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 341 | if (opt & OPT_MAKEPID) { |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 342 | /* user wants _us_ to make the pidfile */ |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 343 | write_pidfile(pidfile); |
Eric Andersen | 625da9d | 2004-04-13 18:28:46 +0000 | [diff] [blame] | 344 | } |
Denis Vlasenko | cce3858 | 2007-02-26 22:47:42 +0000 | [diff] [blame] | 345 | if (opt & OPT_c) { |
| 346 | struct bb_uidgid_t ugid; |
| 347 | parse_chown_usergroup_or_die(&ugid, chuid); |
| 348 | if (ugid.gid != (gid_t) -1) xsetgid(ugid.gid); |
| 349 | if (ugid.uid != (uid_t) -1) xsetuid(ugid.uid); |
Rob Landley | f0623a2 | 2006-07-17 00:35:07 +0000 | [diff] [blame] | 350 | } |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 351 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY |
Denis Vlasenko | b8c77b5 | 2006-12-17 19:43:10 +0000 | [diff] [blame] | 352 | if (opt & OPT_NICELEVEL) { |
Denis Vlasenko | ca3c981 | 2006-10-08 23:36:17 +0000 | [diff] [blame] | 353 | /* Set process priority */ |
| 354 | int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2); |
| 355 | if (setpriority(PRIO_PROCESS, 0, prio) < 0) { |
| 356 | bb_perror_msg_and_die("setpriority(%d)", prio); |
| 357 | } |
| 358 | } |
| 359 | #endif |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 360 | execv(startas, argv); |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 361 | bb_perror_msg_and_die("cannot start %s", startas); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 362 | } |