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