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