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 "busybox.h"
|
---|
11 | #include <stdio.h>
|
---|
12 | #include <stdlib.h>
|
---|
13 | #include <string.h>
|
---|
14 | #include <stdarg.h>
|
---|
15 | #include <signal.h>
|
---|
16 | #include <errno.h>
|
---|
17 | #include <sys/stat.h>
|
---|
18 | #include <dirent.h>
|
---|
19 | #include <unistd.h>
|
---|
20 | #include <getopt.h> /* struct option */
|
---|
21 | #include "pwd_.h"
|
---|
22 |
|
---|
23 | static int signal_nr = 15;
|
---|
24 | static int user_id = -1;
|
---|
25 | static int quiet;
|
---|
26 | static char *userspec = NULL;
|
---|
27 | static char *cmdname = NULL;
|
---|
28 | static char *execname = NULL;
|
---|
29 | static char *pidfile = NULL;
|
---|
30 |
|
---|
31 | struct pid_list {
|
---|
32 | struct pid_list *next;
|
---|
33 | pid_t pid;
|
---|
34 | };
|
---|
35 |
|
---|
36 | static struct pid_list *found = NULL;
|
---|
37 |
|
---|
38 | static inline void push(pid_t pid)
|
---|
39 | {
|
---|
40 | struct pid_list *p;
|
---|
41 |
|
---|
42 | p = xmalloc(sizeof(*p));
|
---|
43 | p->next = found;
|
---|
44 | p->pid = pid;
|
---|
45 | found = p;
|
---|
46 | }
|
---|
47 |
|
---|
48 | static int pid_is_exec(pid_t pid, const char *name)
|
---|
49 | {
|
---|
50 | char buf[32];
|
---|
51 | struct stat sb, exec_stat;
|
---|
52 |
|
---|
53 | if (name)
|
---|
54 | xstat(name, &exec_stat);
|
---|
55 |
|
---|
56 | sprintf(buf, "/proc/%d/exe", pid);
|
---|
57 | if (stat(buf, &sb) != 0)
|
---|
58 | return 0;
|
---|
59 | return (sb.st_dev == exec_stat.st_dev && sb.st_ino == exec_stat.st_ino);
|
---|
60 | }
|
---|
61 |
|
---|
62 | static int pid_is_user(int pid, int uid)
|
---|
63 | {
|
---|
64 | struct stat sb;
|
---|
65 | char buf[32];
|
---|
66 |
|
---|
67 | sprintf(buf, "/proc/%d", pid);
|
---|
68 | if (stat(buf, &sb) != 0)
|
---|
69 | return 0;
|
---|
70 | return (sb.st_uid == uid);
|
---|
71 | }
|
---|
72 |
|
---|
73 | static int pid_is_cmd(pid_t pid, const char *name)
|
---|
74 | {
|
---|
75 | char buf[32];
|
---|
76 | FILE *f;
|
---|
77 | int c;
|
---|
78 |
|
---|
79 | sprintf(buf, "/proc/%d/stat", pid);
|
---|
80 | f = fopen(buf, "r");
|
---|
81 | if (!f)
|
---|
82 | return 0;
|
---|
83 | while ((c = getc(f)) != EOF && c != '(')
|
---|
84 | ;
|
---|
85 | if (c != '(') {
|
---|
86 | fclose(f);
|
---|
87 | return 0;
|
---|
88 | }
|
---|
89 | /* this hopefully handles command names containing ')' */
|
---|
90 | while ((c = getc(f)) != EOF && c == *name)
|
---|
91 | name++;
|
---|
92 | fclose(f);
|
---|
93 | return (c == ')' && *name == '\0');
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static void check(int pid)
|
---|
98 | {
|
---|
99 | if (execname && !pid_is_exec(pid, execname)) {
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | if (userspec && !pid_is_user(pid, user_id)) {
|
---|
103 | return;
|
---|
104 | }
|
---|
105 | if (cmdname && !pid_is_cmd(pid, cmdname)) {
|
---|
106 | return;
|
---|
107 | }
|
---|
108 | push(pid);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | static void do_pidfile(void)
|
---|
113 | {
|
---|
114 | FILE *f;
|
---|
115 | pid_t pid;
|
---|
116 |
|
---|
117 | f = fopen(pidfile, "r");
|
---|
118 | if (f) {
|
---|
119 | if (fscanf(f, "%d", &pid) == 1)
|
---|
120 | check(pid);
|
---|
121 | fclose(f);
|
---|
122 | } else if (errno != ENOENT)
|
---|
123 | bb_perror_msg_and_die("open pidfile %s", pidfile);
|
---|
124 |
|
---|
125 | }
|
---|
126 |
|
---|
127 | static void do_procinit(void)
|
---|
128 | {
|
---|
129 | DIR *procdir;
|
---|
130 | struct dirent *entry;
|
---|
131 | int foundany, pid;
|
---|
132 |
|
---|
133 | if (pidfile) {
|
---|
134 | do_pidfile();
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | procdir = bb_xopendir("/proc");
|
---|
139 |
|
---|
140 | foundany = 0;
|
---|
141 | while ((entry = readdir(procdir)) != NULL) {
|
---|
142 | if (sscanf(entry->d_name, "%d", &pid) != 1)
|
---|
143 | continue;
|
---|
144 | foundany++;
|
---|
145 | check(pid);
|
---|
146 | }
|
---|
147 | closedir(procdir);
|
---|
148 | if (!foundany)
|
---|
149 | bb_error_msg_and_die ("nothing in /proc - not mounted?");
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | static int do_stop(void)
|
---|
154 | {
|
---|
155 | RESERVE_CONFIG_BUFFER(what, 1024);
|
---|
156 | struct pid_list *p;
|
---|
157 | int killed = 0;
|
---|
158 |
|
---|
159 | do_procinit();
|
---|
160 |
|
---|
161 | if (cmdname)
|
---|
162 | strcpy(what, cmdname);
|
---|
163 | else if (execname)
|
---|
164 | strcpy(what, execname);
|
---|
165 | else if (pidfile)
|
---|
166 | sprintf(what, "process in pidfile `%.200s'", pidfile);
|
---|
167 | else if (userspec)
|
---|
168 | sprintf(what, "process(es) owned by `%s'", userspec);
|
---|
169 | else
|
---|
170 | bb_error_msg_and_die ("internal error, please report");
|
---|
171 |
|
---|
172 | if (!found) {
|
---|
173 | if (!quiet)
|
---|
174 | printf("no %s found; none killed.\n", what);
|
---|
175 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
176 | RELEASE_CONFIG_BUFFER(what);
|
---|
177 | return -1;
|
---|
178 | }
|
---|
179 | for (p = found; p; p = p->next) {
|
---|
180 | if (kill(p->pid, signal_nr) == 0) {
|
---|
181 | p->pid = -p->pid;
|
---|
182 | killed++;
|
---|
183 | } else {
|
---|
184 | bb_perror_msg("warning: failed to kill %d", p->pid);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | if (!quiet && killed) {
|
---|
188 | printf("stopped %s (pid", what);
|
---|
189 | for (p = found; p; p = p->next)
|
---|
190 | if(p->pid < 0)
|
---|
191 | printf(" %d", -p->pid);
|
---|
192 | printf(").\n");
|
---|
193 | }
|
---|
194 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
195 | RELEASE_CONFIG_BUFFER(what);
|
---|
196 | return killed;
|
---|
197 | }
|
---|
198 |
|
---|
199 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
|
---|
200 | static const struct option ssd_long_options[] = {
|
---|
201 | { "stop", 0, NULL, 'K' },
|
---|
202 | { "start", 0, NULL, 'S' },
|
---|
203 | { "background", 0, NULL, 'b' },
|
---|
204 | { "quiet", 0, NULL, 'q' },
|
---|
205 | { "make-pidfile", 0, NULL, 'm' },
|
---|
206 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
207 | { "oknodo", 0, NULL, 'o' },
|
---|
208 | { "verbose", 0, NULL, 'v' },
|
---|
209 | #endif
|
---|
210 | { "startas", 1, NULL, 'a' },
|
---|
211 | { "name", 1, NULL, 'n' },
|
---|
212 | { "signal", 1, NULL, 's' },
|
---|
213 | { "user", 1, NULL, 'u' },
|
---|
214 | { "exec", 1, NULL, 'x' },
|
---|
215 | { "pidfile", 1, NULL, 'p' },
|
---|
216 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
217 | { "retry", 1, NULL, 'R' },
|
---|
218 | #endif
|
---|
219 | { 0, 0, 0, 0 }
|
---|
220 | };
|
---|
221 | #endif
|
---|
222 |
|
---|
223 | #define SSD_CTX_STOP 1
|
---|
224 | #define SSD_CTX_START 2
|
---|
225 | #define SSD_OPT_BACKGROUND 4
|
---|
226 | #define SSD_OPT_QUIET 8
|
---|
227 | #define SSD_OPT_MAKEPID 16
|
---|
228 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
229 | #define SSD_OPT_OKNODO 32
|
---|
230 | #define SSD_OPT_VERBOSE 64
|
---|
231 |
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | int start_stop_daemon_main(int argc, char **argv)
|
---|
235 | {
|
---|
236 | unsigned long opt;
|
---|
237 | char *signame = NULL;
|
---|
238 | char *startas = NULL;
|
---|
239 | #if ENABLE_FEATURE_START_STOP_DAEMON_FANCY
|
---|
240 | // char *retry_arg = NULL;
|
---|
241 | // int retries = -1;
|
---|
242 | #endif
|
---|
243 | #if ENABLE_FEATURE_START_STOP_DAEMON_LONG_OPTIONS
|
---|
244 | bb_applet_long_options = ssd_long_options;
|
---|
245 | #endif
|
---|
246 |
|
---|
247 | /* Check required one context option was given */
|
---|
248 | bb_opt_complementally = "K:S:?:K--S:S--K:m?p:K?xpun:S?xa";
|
---|
249 | opt = bb_getopt_ulflags(argc, argv, "KSbqm"
|
---|
250 | // USE_FEATURE_START_STOP_DAEMON_FANCY("ovR:")
|
---|
251 | USE_FEATURE_START_STOP_DAEMON_FANCY("ov")
|
---|
252 | "a:n:s:u:x:p:"
|
---|
253 | // USE_FEATURE_START_STOP_DAEMON_FANCY(,&retry_arg)
|
---|
254 | ,&startas, &cmdname, &signame, &userspec, &execname, &pidfile);
|
---|
255 |
|
---|
256 | quiet = (opt & SSD_OPT_QUIET)
|
---|
257 | USE_FEATURE_START_STOP_DAEMON_FANCY(&& !(opt & SSD_OPT_VERBOSE));
|
---|
258 |
|
---|
259 | if (signame) {
|
---|
260 | signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (!startas)
|
---|
264 | startas = execname;
|
---|
265 |
|
---|
266 | // USE_FEATURE_START_STOP_DAEMON_FANCY(
|
---|
267 | // if (retry_arg)
|
---|
268 | // retries = bb_xgetlarg(retry_arg, 10, 0, INT_MAX);
|
---|
269 | // )
|
---|
270 | argc -= optind;
|
---|
271 | argv += optind;
|
---|
272 |
|
---|
273 | if (userspec && sscanf(userspec, "%d", &user_id) != 1)
|
---|
274 | user_id = bb_xgetpwnam(userspec);
|
---|
275 |
|
---|
276 | if (opt & SSD_CTX_STOP) {
|
---|
277 | int i = do_stop();
|
---|
278 | return
|
---|
279 | USE_FEATURE_START_STOP_DAEMON_FANCY((opt & SSD_OPT_OKNODO)
|
---|
280 | ? 0 :) !!(i<=0);
|
---|
281 | }
|
---|
282 |
|
---|
283 | do_procinit();
|
---|
284 |
|
---|
285 | if (found) {
|
---|
286 | if (!quiet)
|
---|
287 | printf("%s already running.\n%d\n", execname ,found->pid);
|
---|
288 | USE_FEATURE_START_STOP_DAEMON_FANCY(return !(opt & SSD_OPT_OKNODO);)
|
---|
289 | SKIP_FEATURE_START_STOP_DAEMON_FANCY(return EXIT_FAILURE;)
|
---|
290 | }
|
---|
291 | *--argv = startas;
|
---|
292 | if (opt & SSD_OPT_BACKGROUND) {
|
---|
293 | bb_xdaemon(0, 0);
|
---|
294 | setsid();
|
---|
295 | }
|
---|
296 | if (opt & SSD_OPT_MAKEPID) {
|
---|
297 | /* user wants _us_ to make the pidfile */
|
---|
298 | FILE *pidf = bb_xfopen(pidfile, "w");
|
---|
299 |
|
---|
300 | pid_t pidt = getpid();
|
---|
301 | fprintf(pidf, "%d\n", pidt);
|
---|
302 | fclose(pidf);
|
---|
303 | }
|
---|
304 | execv(startas, argv);
|
---|
305 | bb_perror_msg_and_die ("unable to start %s", startas);
|
---|
306 | }
|
---|