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.
13 #include <sys/resource.h>
15 static int signal_nr = 15;
16 static int user_id = -1;
18 static char *userspec;
21 static char *execname;
25 struct pid_list *next;
29 static struct pid_list *found = NULL;
31 static inline void push(pid_t pid)
35 p = xmalloc(sizeof(*p));
41 static int pid_is_exec(pid_t pid, const char *name)
43 char buf[32], *execbuf;
46 sprintf(buf, "/proc/%d/exe", pid);
47 execbuf = xstrdup(name);
48 readlink(buf, execbuf, strlen(name)+1);
50 equal = ! strcmp(execbuf, name);
51 if (ENABLE_FEATURE_CLEAN_UP)
56 static int pid_is_user(int pid, int uid)
61 sprintf(buf, "/proc/%d", pid);
62 if (stat(buf, &sb) != 0)
64 return (sb.st_uid == uid);
67 static int pid_is_cmd(pid_t pid, const char *name)
73 sprintf(buf, "/proc/%d/stat", pid);
77 while ((c = getc(f)) != EOF && c != '(')
83 /* this hopefully handles command names containing ')' */
84 while ((c = getc(f)) != EOF && c == *name)
87 return (c == ')' && *name == '\0');
91 static void check(int pid)
93 if (execname && !pid_is_exec(pid, execname)) {
96 if (userspec && !pid_is_user(pid, user_id)) {
99 if (cmdname && !pid_is_cmd(pid, cmdname)) {
106 static void do_pidfile(void)
111 f = fopen(pidfile, "r");
113 if (fscanf(f, "%d", &pid) == 1)
116 } else if (errno != ENOENT)
117 bb_perror_msg_and_die("open pidfile %s", pidfile);
121 static void do_procinit(void)
124 struct dirent *entry;
132 procdir = xopendir("/proc");
135 while ((entry = readdir(procdir)) != NULL) {
136 if (sscanf(entry->d_name, "%d", &pid) != 1)
143 bb_error_msg_and_die ("nothing in /proc - not mounted?");
147 static int do_stop(void)
149 RESERVE_CONFIG_BUFFER(what, 1024);
156 strcpy(what, cmdname);
158 strcpy(what, execname);
160 sprintf(what, "process in pidfile `%.200s'", pidfile);
162 sprintf(what, "process(es) owned by `%s'", userspec);
164 bb_error_msg_and_die ("internal error, please report");
168 printf("no %s found; none killed.\n", what);
169 if (ENABLE_FEATURE_CLEAN_UP)
170 RELEASE_CONFIG_BUFFER(what);
173 for (p = found; p; p = p->next) {
174 if (kill(p->pid, signal_nr) == 0) {
178 bb_perror_msg("warning: failed to kill %d", p->pid);
181 if (!quiet && killed) {
182 printf("stopped %s (pid", what);
183 for (p = found; p; p = p->next)
185 printf(" %d", -p->pid);
188 if (ENABLE_FEATURE_CLEAN_UP)
189 RELEASE_CONFIG_BUFFER(what);
193 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
194 static const struct option ssd_long_options[] = {
195 { "stop", 0, NULL, 'K' },
196 { "start", 0, NULL, 'S' },
197 { "background", 0, NULL, 'b' },
198 { "quiet", 0, NULL, 'q' },
199 { "make-pidfile", 0, NULL, 'm' },
200 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
201 { "oknodo", 0, NULL, 'o' },
202 { "verbose", 0, NULL, 'v' },
203 { "nicelevel", 1, NULL, 'N' },
205 { "startas", 1, NULL, 'a' },
206 { "name", 1, NULL, 'n' },
207 { "signal", 1, NULL, 's' },
208 { "user", 1, NULL, 'u' },
209 { "chuid", 1, NULL, 'c' },
210 { "exec", 1, NULL, 'x' },
211 { "pidfile", 1, NULL, 'p' },
212 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
213 { "retry", 1, NULL, 'R' },
219 #define SSD_CTX_STOP 0x1
220 #define SSD_CTX_START 0x2
221 #define SSD_OPT_BACKGROUND 0x4
222 #define SSD_OPT_QUIET 0x8
223 #define SSD_OPT_MAKEPID 0x10
224 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
225 #define SSD_OPT_OKNODO 0x20
226 #define SSD_OPT_VERBOSE 0x40
227 #define SSD_OPT_NICELEVEL 0x80
230 int start_stop_daemon_main(int argc, char **argv)
233 char *signame = NULL;
234 char *startas = NULL;
235 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
236 // char *retry_arg = NULL;
240 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
241 applet_long_options = ssd_long_options;
244 /* Check required one context option was given */
245 opt_complementary = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
246 opt = getopt32(argc, argv, "KSbqm"
247 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:R:")
248 USE_FEATURE_START_STOP_DAEMON_FANCY("ovN:")
250 USE_FEATURE_START_STOP_DAEMON_FANCY(,&opt_N)
251 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
252 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
254 quiet = (opt & SSD_OPT_QUIET)
255 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
258 signal_nr = get_signum(signame);
259 if (signal_nr < 0) bb_show_usage();
265 // USE_FEATURE_START_STOP_DAEMON_FANCY(
267 // retries = xatoi_u(retry_arg);
272 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
273 user_id = bb_xgetpwnam(userspec);
275 if (opt & SSD_CTX_STOP) {
278 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
286 printf("%s already running\n%d\n", execname, found->pid);
287 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
288 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
291 if (opt & SSD_OPT_BACKGROUND) {
295 if (opt & SSD_OPT_MAKEPID) {
296 /* user wants _us_ to make the pidfile */
297 FILE *pidf = xfopen(pidfile, "w");
299 pid_t pidt = getpid();
300 fprintf(pidf, "%d\n", pidt);
304 if (sscanf(chuid, "%d", &user_id) != 1)
305 user_id = bb_xgetpwnam(chuid);
308 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
309 if (opt & SSD_OPT_NICELEVEL) {
310 /* Set process priority */
311 int prio = getpriority(PRIO_PROCESS, 0) + xatoi_range(opt_N, INT_MIN/2, INT_MAX/2);
312 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
313 bb_perror_msg_and_die("setpriority(%d)", prio);
317 execv(startas, argv);
318 bb_perror_msg_and_die ("unable to start %s", startas);