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>, |
| 6 | * public domain. |
| 7 | * Adapted for busybox David Kimdon <dwhedon@gordian.com> |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <stdarg.h> |
| 14 | #include <signal.h> |
| 15 | #include <errno.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <dirent.h> |
| 18 | #include <unistd.h> |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 19 | #include <getopt.h> |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 20 | |
| 21 | #include "busybox.h" |
Eric Andersen | 887ca79 | 2002-07-03 23:19:26 +0000 | [diff] [blame] | 22 | #include "pwd_.h" |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 23 | |
| 24 | static int start = 0; |
| 25 | static int stop = 0; |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 26 | static int fork_before_exec = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 27 | static int signal_nr = 15; |
| 28 | static int user_id = -1; |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 29 | static char *userspec = NULL; |
| 30 | static char *cmdname = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 31 | static char *execname = NULL; |
| 32 | static char *startas = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 33 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 34 | typedef struct pid_list { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 35 | struct pid_list *next; |
| 36 | int pid; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 37 | } pid_list; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 38 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 39 | static pid_list *found = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 40 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 41 | static inline void |
| 42 | push(int pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 43 | { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 44 | pid_list *p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 45 | |
| 46 | p = xmalloc(sizeof(*p)); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 47 | p->next = found; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 48 | p->pid = pid; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 49 | found = p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 52 | static int |
| 53 | pid_is_exec(int pid, const char *exec) |
| 54 | { |
| 55 | char buf[PATH_MAX]; |
| 56 | FILE *fp; |
| 57 | |
| 58 | sprintf(buf, "/proc/%d/cmdline", pid); |
| 59 | fp = fopen(buf, "r"); |
| 60 | if (fp && fgets (buf, sizeof (buf), fp) ) { |
Eric Andersen | ff7661d | 2002-06-05 07:11:32 +0000 | [diff] [blame] | 61 | fclose(fp); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 62 | if (strncmp (buf, exec, strlen(exec)) == 0) |
| 63 | return 1; |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static int |
| 70 | pid_is_user(int pid, int uid) |
| 71 | { |
| 72 | struct stat sb; |
| 73 | char buf[32]; |
| 74 | |
| 75 | sprintf(buf, "/proc/%d", pid); |
| 76 | if (stat(buf, &sb) != 0) |
| 77 | return 0; |
| 78 | return (sb.st_uid == uid); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static int |
| 83 | pid_is_cmd(int pid, const char *name) |
| 84 | { |
| 85 | char buf[32]; |
| 86 | FILE *f; |
| 87 | int c; |
| 88 | |
| 89 | sprintf(buf, "/proc/%d/stat", pid); |
| 90 | f = fopen(buf, "r"); |
| 91 | if (!f) |
| 92 | return 0; |
| 93 | while ((c = getc(f)) != EOF && c != '(') |
| 94 | ; |
| 95 | if (c != '(') { |
| 96 | fclose(f); |
| 97 | return 0; |
| 98 | } |
| 99 | /* this hopefully handles command names containing ')' */ |
| 100 | while ((c = getc(f)) != EOF && c == *name) |
| 101 | name++; |
| 102 | fclose(f); |
| 103 | return (c == ')' && *name == '\0'); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static void |
| 108 | check(int pid) |
| 109 | { |
| 110 | if (execname && !pid_is_exec(pid, execname)) { |
| 111 | return; |
| 112 | } |
| 113 | if (userspec && !pid_is_user(pid, user_id)) { |
| 114 | return; |
| 115 | } |
| 116 | if (cmdname && !pid_is_cmd(pid, cmdname)) { |
| 117 | return; |
| 118 | } |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 119 | push(pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | static void |
| 125 | do_procfs(void) |
| 126 | { |
| 127 | DIR *procdir; |
| 128 | struct dirent *entry; |
| 129 | int foundany, pid; |
| 130 | |
| 131 | procdir = opendir("/proc"); |
| 132 | if (!procdir) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | bb_perror_msg_and_die ("opendir /proc"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 134 | |
| 135 | foundany = 0; |
| 136 | while ((entry = readdir(procdir)) != NULL) { |
| 137 | if (sscanf(entry->d_name, "%d", &pid) != 1) |
| 138 | continue; |
| 139 | foundany++; |
| 140 | check(pid); |
| 141 | } |
| 142 | closedir(procdir); |
| 143 | if (!foundany) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 144 | bb_error_msg_and_die ("nothing in /proc - not mounted?"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | static void |
| 149 | do_stop(void) |
| 150 | { |
| 151 | char what[1024]; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 152 | pid_list *p; |
| 153 | int killed = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 154 | |
| 155 | if (cmdname) |
| 156 | strcpy(what, cmdname); |
| 157 | else if (execname) |
| 158 | strcpy(what, execname); |
| 159 | else if (userspec) |
| 160 | sprintf(what, "process(es) owned by `%s'", userspec); |
| 161 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 162 | bb_error_msg_and_die ("internal error, please report"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 163 | |
| 164 | if (!found) { |
| 165 | printf("no %s found; none killed.\n", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 166 | return; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 167 | } |
| 168 | for (p = found; p; p = p->next) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 169 | if (kill(p->pid, signal_nr) == 0) { |
| 170 | p->pid = -p->pid; |
| 171 | killed++; |
| 172 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | bb_perror_msg("warning: failed to kill %d:", p->pid); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 174 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 175 | } |
| 176 | if (killed) { |
| 177 | printf("stopped %s (pid", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 178 | for (p = found; p; p = p->next) |
| 179 | if(p->pid < 0) |
| 180 | printf(" %d", -p->pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 181 | printf(").\n"); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 186 | static const struct option ssd_long_options[] = { |
| 187 | { "stop", 0, NULL, 'K' }, |
| 188 | { "start", 0, NULL, 'S' }, |
| 189 | { "background", 0, NULL, 'b' }, |
| 190 | { "startas", 1, NULL, 'a' }, |
| 191 | { "name", 1, NULL, 'n' }, |
| 192 | { "signal", 1, NULL, 's' }, |
| 193 | { "user", 1, NULL, 'u' }, |
| 194 | { "exec", 1, NULL, 'x' }, |
| 195 | { 0, 0, 0, 0 } |
| 196 | }; |
| 197 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 198 | int |
| 199 | start_stop_daemon_main(int argc, char **argv) |
| 200 | { |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 201 | int flags; |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 202 | char *signame = NULL; |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 203 | bb_applet_long_options = ssd_long_options; |
| 204 | |
| 205 | flags = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:", |
| 206 | &startas, &cmdname, &signame, &userspec, &execname); |
| 207 | |
| 208 | /* Be sneaky and avoid branching */ |
| 209 | stop = (flags & 1); |
| 210 | start = (flags & 2); |
| 211 | fork_before_exec = (flags & 4); |
| 212 | |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 213 | if (signame) { |
| 214 | signal_nr = bb_xgetlarg(signame, 10, 0, NSIG); |
| 215 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 216 | |
| 217 | if (start == stop) |
| 218 | bb_error_msg_and_die ("need exactly one of -S or -K"); |
| 219 | |
| 220 | if (!execname && !userspec) |
| 221 | bb_error_msg_and_die ("need at least one of -x or -u"); |
| 222 | |
| 223 | if (!startas) |
| 224 | startas = execname; |
| 225 | |
| 226 | if (start && !startas) |
| 227 | bb_error_msg_and_die ("-S needs -x or -a"); |
| 228 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 229 | argc -= optind; |
| 230 | argv += optind; |
| 231 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 232 | if (userspec && sscanf(userspec, "%d", &user_id) != 1) |
| 233 | user_id = my_getpwnam(userspec); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 234 | |
| 235 | do_procfs(); |
| 236 | |
| 237 | if (stop) { |
| 238 | do_stop(); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 239 | return EXIT_SUCCESS; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if (found) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 243 | printf("%s already running.\n%d\n", execname ,found->pid); |
| 244 | return EXIT_SUCCESS; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 245 | } |
| 246 | *--argv = startas; |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 247 | if (fork_before_exec) { |
| 248 | if (daemon(0, 0) == -1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 249 | bb_perror_msg_and_die ("unable to fork"); |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 250 | } |
| 251 | setsid(); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 252 | execv(startas, argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 253 | bb_perror_msg_and_die ("unable to start %s", startas); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 254 | } |
| 255 | |