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)
43 struct stat sb, exec_stat;
46 xstat(name, &exec_stat);
48 sprintf(buf, "/proc/%d/exe", pid);
49 if (stat(buf, &sb) != 0)
51 return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
54 static int pid_is_user(int pid, int uid)
59 sprintf(buf, "/proc/%d", pid);
60 if (stat(buf, &sb) != 0)
62 return (sb.st_uid == uid);
65 static int pid_is_cmd(pid_t pid, const char *name)
71 sprintf(buf, "/proc/%d/stat", pid);
75 while ((c = getc(f)) != EOF && c != '(')
81 /* this hopefully handles command names containing ')' */
82 while ((c = getc(f)) != EOF && c == *name)
85 return (c == ')' && *name == '\0');
89 static void check(int pid)
91 if (execname && !pid_is_exec(pid, execname)) {
94 if (userspec && !pid_is_user(pid, user_id)) {
97 if (cmdname && !pid_is_cmd(pid, cmdname)) {
104 static void do_pidfile(void)
109 f = fopen(pidfile, "r");
111 if (fscanf(f, "%d", &pid) == 1)
114 } else if (errno != ENOENT)
115 bb_perror_msg_and_die("open pidfile %s", pidfile);
119 static void do_procinit(void)
122 struct dirent *entry;
130 procdir = xopendir("/proc");
133 while ((entry = readdir(procdir)) != NULL) {
134 if (sscanf(entry->d_name, "%d", &pid) != 1)
141 bb_error_msg_and_die ("nothing in /proc - not mounted?");
145 static int do_stop(void)
147 RESERVE_CONFIG_BUFFER(what, 1024);
154 strcpy(what, cmdname);
156 strcpy(what, execname);
158 sprintf(what, "process in pidfile `%.200s'", pidfile);
160 sprintf(what, "process(es) owned by `%s'", userspec);
162 bb_error_msg_and_die ("internal error, please report");
166 printf("no %s found; none killed.\n", what);
167 if (ENABLE_FEATURE_CLEAN_UP)
168 RELEASE_CONFIG_BUFFER(what);
171 for (p = found; p; p = p->next) {
172 if (kill(p->pid, signal_nr) == 0) {
176 bb_perror_msg("warning: failed to kill %d", p->pid);
179 if (!quiet && killed) {
180 printf("stopped %s (pid", what);
181 for (p = found; p; p = p->next)
183 printf(" %d", -p->pid);
186 if (ENABLE_FEATURE_CLEAN_UP)
187 RELEASE_CONFIG_BUFFER(what);
191 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
192 static const struct option ssd_long_options[] = {
193 { "stop", 0, NULL, 'K' },
194 { "start", 0, NULL, 'S' },
195 { "background", 0, NULL, 'b' },
196 { "quiet", 0, NULL, 'q' },
197 { "make-pidfile", 0, NULL, 'm' },
198 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
199 { "oknodo", 0, NULL, 'o' },
200 { "verbose", 0, NULL, 'v' },
202 { "startas", 1, NULL, 'a' },
203 { "name", 1, NULL, 'n' },
204 { "signal", 1, NULL, 's' },
205 { "user", 1, NULL, 'u' },
206 { "chuid", 1, NULL, 'c' },
207 { "exec", 1, NULL, 'x' },
208 { "pidfile", 1, NULL, 'p' },
209 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
210 { "retry", 1, NULL, 'R' },
216 #define SSD_CTX_STOP 1
217 #define SSD_CTX_START 2
218 #define SSD_OPT_BACKGROUND 4
219 #define SSD_OPT_QUIET 8
220 #define SSD_OPT_MAKEPID 16
221 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
222 #define SSD_OPT_OKNODO 32
223 #define SSD_OPT_VERBOSE 64
227 int start_stop_daemon_main(int argc, char **argv)
230 char *signame = NULL;
231 char *startas = NULL;
232 #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
233 // char *retry_arg = NULL;
236 #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
237 bb_applet_long_options = ssd_long_options;
240 /* Check required one context option was given */
241 bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
242 opt = bb_getopt_ulflags(argc, argv, "KSbqm"
243 // USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
244 USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
246 // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
247 ,&startas, &cmdname, &signame, &userspec, &chuid, &execname, &pidfile);
249 quiet = (opt & SSD_OPT_QUIET)
250 USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
253 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
259 // USE_FEATURE_START_STOP_DAEMON_FANCY(
261 // retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
266 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
267 user_id = bb_xgetpwnam(userspec);
269 if (opt & SSD_CTX_STOP) {
272 USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
280 printf("%s already running.\n%d\n", execname ,found->pid);
281 USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
282 SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
285 if (opt & SSD_OPT_BACKGROUND) {
289 if (opt & SSD_OPT_MAKEPID) {
290 /* user wants _us_ to make the pidfile */
291 FILE *pidf = xfopen(pidfile, "w");
293 pid_t pidt = getpid();
294 fprintf(pidf, "%d\n", pidt);
298 if(sscanf(chuid, "%d", &user_id) != 1)
299 user_id = bb_xgetpwnam(chuid);
302 execv(startas, argv);
303 bb_perror_msg_and_die ("unable to start %s", startas);