blob: c6b704329da27aea8bab83370b61f2db36260821 [file] [log] [blame]
Eric Andersenc2af1ee2001-10-18 19:33:06 +00001/* 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 Andersen1a10eec2001-10-24 17:19:38 +000019#include "pwd.h"
Eric Andersenc2af1ee2001-10-18 19:33:06 +000020
21#include "busybox.h"
22
23static int start = 0;
24static int stop = 0;
25static int signal_nr = 15;
26static int user_id = -1;
27static const char *userspec = NULL;
28static const char *cmdname = NULL;
29static char *execname = NULL;
30static char *startas = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000031
Eric Andersen950d8b42001-10-31 09:55:39 +000032typedef struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000033 struct pid_list *next;
34 int pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000035} pid_list;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000036
Eric Andersen950d8b42001-10-31 09:55:39 +000037static pid_list *found = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000038
Eric Andersen950d8b42001-10-31 09:55:39 +000039static inline void
40push(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000041{
Eric Andersen950d8b42001-10-31 09:55:39 +000042 pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000043
44 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000045 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000046 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000047 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000048}
49
50
51static void
52parse_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 Andersenc2af1ee2001-10-18 19:33:06 +000086 }
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
103static int
104pid_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
119static int
120pid_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
132static int
133pid_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
157static void
158check(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 Andersen950d8b42001-10-31 09:55:39 +0000169 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000170}
171
172
173
174static void
175do_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
198static void
199do_stop(void)
200{
201 char what[1024];
Eric Andersen950d8b42001-10-31 09:55:39 +0000202 pid_list *p;
203 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000204
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 Andersen950d8b42001-10-31 09:55:39 +0000216 return;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000217 }
218 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000219 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 Andersenc2af1ee2001-10-18 19:33:06 +0000225 }
226 if (killed) {
227 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000228 for (p = found; p; p = p->next)
229 if(p->pid < 0)
230 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000231 printf(").\n");
232 }
233}
234
235
236int
237start_stop_daemon_main(int argc, char **argv)
238{
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000239 parse_options(argc, argv);
240 argc -= optind;
241 argv += optind;
242
Eric Andersen950d8b42001-10-31 09:55:39 +0000243 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
244 user_id = my_getpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000245
246 do_procfs();
247
248 if (stop) {
249 do_stop();
Eric Andersen950d8b42001-10-31 09:55:39 +0000250 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000251 }
252
253 if (found) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000254 printf("%s already running.\n%d\n", execname ,found->pid);
255 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000256 }
257 *--argv = startas;
258 execv(startas, argv);
259 perror_msg_and_die ("unable to start %s", startas);
260}
261