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