1 /* vi: set sw=4 ts=4: */
3 * Mini start-stop-daemon implementation(s) for busybox
5 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
6 * Adapted for busybox David Kimdon <dwhedon@gordian.com>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 static int signal_nr = 15;
15 static int user_id = -1;
17 static char *userspec;
20 static char *execname;
24 struct pid_list *next;
28 static struct pid_list *found = NULL;
30 static inline void push(pid_t pid)
34 p = xmalloc(sizeof(*p));
40 static int pid_is_exec(pid_t pid, const char *name)
42 char buf[32], *execbuf;
45 sprintf(buf, "/proc/%d/exe", pid);
46 execbuf = xstrdup(name);
47 readlink(buf, execbuf, strlen(name)+1);
49 equal = ! strcmp(execbuf, name);
50 if (ENABLE_FEATURE_CLEAN_UP)
55 static int pid_is_user(int pid, int uid)
60 sprintf(buf, "/proc/%d", pid);
61 if (stat(buf, &sb) != 0)
63 return (sb.st_uid == uid);
66 static int pid_is_cmd(pid_t pid, const char *name)
72 sprintf(buf, "/proc/%d/stat", pid);
76 while ((c = getc(f)) != EOF && c != '(')
82 /* this hopefully handles command names containing ')' */
83 while ((c = getc(f)) != EOF && c == *name)
86 return (c == ')' && *name == '\0');
90 static void check(int pid)
92 if (execname && !pid_is_exec(pid, execname)) {
95 if (userspec && !pid_is_user(pid, user_id)) {
98 if (cmdname && !pid_is_cmd(pid, cmdname)) {
105 static void do_pidfile(void)
110 f = fopen(pidfile, "r");
112 if (fscanf(f, "%d", &pid) == 1)
115 } else if (errno != ENOENT)
116 bb_perror_msg_and_die("open pidfile %s", pidfile);
120 static void do_procinit(void)
123 struct dirent *entry;
131 procdir = xopendir("/proc");
134 while ((entry = readdir(procdir)) != NULL) {
135 if (sscanf(entry->d_name, "%d", &pid) != 1)
142 bb_error_msg_and_die ("nothing in /proc - not mounted?");
146 static int do_stop(void)
148 RESERVE_CONFIG_BUFFER(what, 1024);
155 strcpy(what, cmdname);
157 strcpy(what, execname);
159 sprintf(what, "process in pidfile `%.200s'", pidfile);
161 sprintf(what, "process(es) owned by `%s'", userspec);
163 bb_error_msg_and_die ("internal error, please report");
167 printf("no %s found; none killed.\n", what);
168 if (ENABLE_FEATURE_CLEAN_UP)
169 RELEASE_CONFIG_BUFFER(what);
172 for (p = found; p; p = p->next) {
173 if (kill(p->pid, signal_nr) == 0) {
177 bb_perror_msg("warning: failed to kill %d", p->pid);
180 if (!quiet && killed) {
181 printf("stopped %s (pid", what);
182 for (p = found; p; p = p->next)
184 printf(" %d", -p->pid);
187 if (ENABLE_FEATURE_CLEAN_UP)
188 RELEASE_CONFIG_BUFFER(what);
192 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
193 static const struct option ssd_long_options[] = {
194 { "stop", 0, NULL, 'K' },
195 { "start", 0, NULL, 'S' },
196 { "background", 0, NULL, 'b' },
197 { "quiet", 0, NULL, 'q' },
198 { "make-pidfile", 0, NULL, 'm' },
199 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
200 { "oknodo", 0, NULL, 'o' },
201 { "verbose", 0, NULL, 'v' },
203 { "startas", 1, NULL, 'a' },
204 { "name", 1, NULL, 'n' },
205 { "signal", 1, NULL, 's' },
206 { "user", 1, NULL, 'u' },
207 { "chuid", 1, NULL, 'c' },
208 { "exec", 1, NULL, 'x' },
209 { "pidfile", 1, NULL, 'p' },
210 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
211 { "retry", 1, NULL, 'R' },
217 #define SSD_CTX_STOP 1
218 #define SSD_CTX_START 2
219 #define SSD_OPT_BACKGROUND 4
220 #define SSD_OPT_QUIET 8
221 #define SSD_OPT_MAKEPID 16
222 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
223 #define SSD_OPT_OKNODO 32
224 #define SSD_OPT_VERBOSE 64
228 int start_stop_daemon_main(int argc, char **argv)
231 char *signame = NULL;
232 char *startas = NULL;
233 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
234 // char *retry_arg = NULL;
237 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
238 applet_long_options = ssd_long_options;
241 /* Check required one context option was given */
242 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
243 opt = getopt32(argc, argv, "KSbqm"
244 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
245 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
247 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
248 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
250 quiet = (opt & SSD_OPT_QUIET)
251 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
254 signal_nr = get_signum(signame);
255 if (signal_nr < 0) bb_show_usage();
261 // USE_FEATURE_START_STOP_DAEMON_FANCY(
263 // retries = xatoi_u(retry_arg);
268 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
269 user_id = bb_xgetpwnam(userspec);
271 if (opt & SSD_CTX_STOP) {
274 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
282 printf("%s already running.\n%d\n", execname ,found->pid);
283 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
284 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
287 if (opt & SSD_OPT_BACKGROUND) {
291 if (opt & SSD_OPT_MAKEPID) {
292 /* user wants _us_ to make the pidfile */
293 FILE *pidf = xfopen(pidfile, "w");
295 pid_t pidt = getpid();
296 fprintf(pidf, "%d\n", pidt);
300 if(sscanf(chuid, "%d", &user_id) != 1)
301 user_id = bb_xgetpwnam(chuid);
304 execv(startas, argv);
305 bb_perror_msg_and_die ("unable to start %s", startas);