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 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 24 | static int signal_nr = 15; |
| 25 | static int user_id = -1; |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 26 | static char *userspec = NULL; |
| 27 | static char *cmdname = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 28 | static char *execname = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 29 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 30 | typedef struct pid_list { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 31 | struct pid_list *next; |
| 32 | int pid; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 33 | } pid_list; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 34 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 35 | static pid_list *found = NULL; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 37 | static inline void |
| 38 | push(int pid) |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 39 | { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 40 | pid_list *p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 41 | |
| 42 | p = xmalloc(sizeof(*p)); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 43 | p->next = found; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 44 | p->pid = pid; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 45 | found = p; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 48 | static int |
| 49 | pid_is_exec(int pid, const char *exec) |
| 50 | { |
| 51 | char buf[PATH_MAX]; |
| 52 | FILE *fp; |
| 53 | |
| 54 | sprintf(buf, "/proc/%d/cmdline", pid); |
| 55 | fp = fopen(buf, "r"); |
| 56 | if (fp && fgets (buf, sizeof (buf), fp) ) { |
Eric Andersen | ff7661d | 2002-06-05 07:11:32 +0000 | [diff] [blame] | 57 | fclose(fp); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 58 | if (strncmp (buf, exec, strlen(exec)) == 0) |
| 59 | return 1; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static int |
| 66 | pid_is_user(int pid, int uid) |
| 67 | { |
| 68 | struct stat sb; |
| 69 | char buf[32]; |
| 70 | |
| 71 | sprintf(buf, "/proc/%d", pid); |
| 72 | if (stat(buf, &sb) != 0) |
| 73 | return 0; |
| 74 | return (sb.st_uid == uid); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static int |
| 79 | pid_is_cmd(int pid, const char *name) |
| 80 | { |
| 81 | char buf[32]; |
| 82 | FILE *f; |
| 83 | int c; |
| 84 | |
| 85 | sprintf(buf, "/proc/%d/stat", pid); |
| 86 | f = fopen(buf, "r"); |
| 87 | if (!f) |
| 88 | return 0; |
| 89 | while ((c = getc(f)) != EOF && c != '(') |
| 90 | ; |
| 91 | if (c != '(') { |
| 92 | fclose(f); |
| 93 | return 0; |
| 94 | } |
| 95 | /* this hopefully handles command names containing ')' */ |
| 96 | while ((c = getc(f)) != EOF && c == *name) |
| 97 | name++; |
| 98 | fclose(f); |
| 99 | return (c == ')' && *name == '\0'); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static void |
| 104 | check(int pid) |
| 105 | { |
| 106 | if (execname && !pid_is_exec(pid, execname)) { |
| 107 | return; |
| 108 | } |
| 109 | if (userspec && !pid_is_user(pid, user_id)) { |
| 110 | return; |
| 111 | } |
| 112 | if (cmdname && !pid_is_cmd(pid, cmdname)) { |
| 113 | return; |
| 114 | } |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 115 | push(pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | static void |
| 121 | do_procfs(void) |
| 122 | { |
| 123 | DIR *procdir; |
| 124 | struct dirent *entry; |
| 125 | int foundany, pid; |
| 126 | |
| 127 | procdir = opendir("/proc"); |
| 128 | if (!procdir) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | bb_perror_msg_and_die ("opendir /proc"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 130 | |
| 131 | foundany = 0; |
| 132 | while ((entry = readdir(procdir)) != NULL) { |
| 133 | if (sscanf(entry->d_name, "%d", &pid) != 1) |
| 134 | continue; |
| 135 | foundany++; |
| 136 | check(pid); |
| 137 | } |
| 138 | closedir(procdir); |
| 139 | if (!foundany) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | bb_error_msg_and_die ("nothing in /proc - not mounted?"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | static void |
| 145 | do_stop(void) |
| 146 | { |
| 147 | char what[1024]; |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 148 | pid_list *p; |
| 149 | int killed = 0; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 150 | |
| 151 | if (cmdname) |
| 152 | strcpy(what, cmdname); |
| 153 | else if (execname) |
| 154 | strcpy(what, execname); |
| 155 | else if (userspec) |
| 156 | sprintf(what, "process(es) owned by `%s'", userspec); |
| 157 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 158 | bb_error_msg_and_die ("internal error, please report"); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 159 | |
| 160 | if (!found) { |
| 161 | printf("no %s found; none killed.\n", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 162 | return; |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 163 | } |
| 164 | for (p = found; p; p = p->next) { |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 165 | if (kill(p->pid, signal_nr) == 0) { |
| 166 | p->pid = -p->pid; |
| 167 | killed++; |
| 168 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 169 | bb_perror_msg("warning: failed to kill %d:", p->pid); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 170 | } |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 171 | } |
| 172 | if (killed) { |
| 173 | printf("stopped %s (pid", what); |
Eric Andersen | 950d8b4 | 2001-10-31 09:55:39 +0000 | [diff] [blame] | 174 | for (p = found; p; p = p->next) |
| 175 | if(p->pid < 0) |
| 176 | printf(" %d", -p->pid); |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 177 | printf(").\n"); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 182 | static const struct option ssd_long_options[] = { |
| 183 | { "stop", 0, NULL, 'K' }, |
| 184 | { "start", 0, NULL, 'S' }, |
| 185 | { "background", 0, NULL, 'b' }, |
| 186 | { "startas", 1, NULL, 'a' }, |
| 187 | { "name", 1, NULL, 'n' }, |
| 188 | { "signal", 1, NULL, 's' }, |
| 189 | { "user", 1, NULL, 'u' }, |
| 190 | { "exec", 1, NULL, 'x' }, |
| 191 | { 0, 0, 0, 0 } |
| 192 | }; |
| 193 | |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 194 | #define SSD_CTX_STOP 1 |
| 195 | #define SSD_CTX_START 2 |
| 196 | #define SSD_OPT_BACKGROUND 4 |
| 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 | { |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 201 | unsigned long opt; |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 202 | char *signame = NULL; |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 203 | char *startas = NULL; |
| 204 | |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 205 | bb_applet_long_options = ssd_long_options; |
| 206 | |
Glenn L McGrath | 2d016a3 | 2004-01-23 21:43:49 +0000 | [diff] [blame^] | 207 | bb_opt_complementaly = "K~S:S~K"; |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 208 | opt = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:", |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 209 | &startas, &cmdname, &signame, &userspec, &execname); |
| 210 | |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 211 | /* Check one and only one context option was given */ |
Glenn L McGrath | 2d016a3 | 2004-01-23 21:43:49 +0000 | [diff] [blame^] | 212 | if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) { |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 213 | bb_show_usage(); |
| 214 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 215 | |
Eric Andersen | 08804ce | 2003-07-30 08:29:56 +0000 | [diff] [blame] | 216 | if (signame) { |
| 217 | signal_nr = bb_xgetlarg(signame, 10, 0, NSIG); |
| 218 | } |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 219 | |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 226 | if ((opt & SSD_CTX_START) && !startas) |
Eric Andersen | aa820db | 2003-07-26 09:10:35 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 237 | if (opt & SSD_CTX_STOP) { |
Eric Andersen | c2af1ee | 2001-10-18 19:33:06 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | 8d44178 | 2004-01-22 09:04:58 +0000 | [diff] [blame] | 247 | if (opt & SSD_OPT_BACKGROUND) { |
Eric Andersen | 53a2299 | 2002-01-26 09:04:45 +0000 | [diff] [blame] | 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 | } |