blob: dfa33e1f9c18bf2517d578cfffba2fa3c650082f [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 Andersenaa820db2003-07-26 09:10:35 +000019#include <getopt.h>
Eric Andersenc2af1ee2001-10-18 19:33:06 +000020
21#include "busybox.h"
Eric Andersen887ca792002-07-03 23:19:26 +000022#include "pwd_.h"
Eric Andersenc2af1ee2001-10-18 19:33:06 +000023
Eric Andersenc2af1ee2001-10-18 19:33:06 +000024static int signal_nr = 15;
25static int user_id = -1;
Eric Andersenaa820db2003-07-26 09:10:35 +000026static char *userspec = NULL;
27static char *cmdname = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000028static char *execname = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000029
Eric Andersen950d8b42001-10-31 09:55:39 +000030typedef struct pid_list {
Eric Andersenc2af1ee2001-10-18 19:33:06 +000031 struct pid_list *next;
32 int pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000033} pid_list;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000034
Eric Andersen950d8b42001-10-31 09:55:39 +000035static pid_list *found = NULL;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000036
Eric Andersen950d8b42001-10-31 09:55:39 +000037static inline void
38push(int pid)
Eric Andersenc2af1ee2001-10-18 19:33:06 +000039{
Eric Andersen950d8b42001-10-31 09:55:39 +000040 pid_list *p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000041
42 p = xmalloc(sizeof(*p));
Eric Andersen950d8b42001-10-31 09:55:39 +000043 p->next = found;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000044 p->pid = pid;
Eric Andersen950d8b42001-10-31 09:55:39 +000045 found = p;
Eric Andersenc2af1ee2001-10-18 19:33:06 +000046}
47
Eric Andersenc2af1ee2001-10-18 19:33:06 +000048static int
49pid_is_exec(int pid, const char *exec)
50{
51 char buf[PATH_MAX];
52 FILE *fp;
53
54 sprintf(buf, "/proc/%d/cmdline", pid);
55 fp = fopen(buf, "r");
56 if (fp && fgets (buf, sizeof (buf), fp) ) {
Eric Andersenff7661d2002-06-05 07:11:32 +000057 fclose(fp);
Eric Andersenc2af1ee2001-10-18 19:33:06 +000058 if (strncmp (buf, exec, strlen(exec)) == 0)
59 return 1;
60 }
61 return 0;
62}
63
64
65static int
66pid_is_user(int pid, int uid)
67{
68 struct stat sb;
69 char buf[32];
70
71 sprintf(buf, "/proc/%d", pid);
72 if (stat(buf, &sb) != 0)
73 return 0;
74 return (sb.st_uid == uid);
75}
76
77
78static int
79pid_is_cmd(int pid, const char *name)
80{
81 char buf[32];
82 FILE *f;
83 int c;
84
85 sprintf(buf, "/proc/%d/stat", pid);
86 f = fopen(buf, "r");
87 if (!f)
88 return 0;
89 while ((c = getc(f)) != EOF && c != '(')
90 ;
91 if (c != '(') {
92 fclose(f);
93 return 0;
94 }
95 /* this hopefully handles command names containing ')' */
96 while ((c = getc(f)) != EOF && c == *name)
97 name++;
98 fclose(f);
99 return (c == ')' && *name == '\0');
100}
101
102
103static void
104check(int pid)
105{
106 if (execname && !pid_is_exec(pid, execname)) {
107 return;
108 }
109 if (userspec && !pid_is_user(pid, user_id)) {
110 return;
111 }
112 if (cmdname && !pid_is_cmd(pid, cmdname)) {
113 return;
114 }
Eric Andersen950d8b42001-10-31 09:55:39 +0000115 push(pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000116}
117
118
119
120static void
121do_procfs(void)
122{
123 DIR *procdir;
124 struct dirent *entry;
125 int foundany, pid;
126
127 procdir = opendir("/proc");
128 if (!procdir)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_perror_msg_and_die ("opendir /proc");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000130
131 foundany = 0;
132 while ((entry = readdir(procdir)) != NULL) {
133 if (sscanf(entry->d_name, "%d", &pid) != 1)
134 continue;
135 foundany++;
136 check(pid);
137 }
138 closedir(procdir);
139 if (!foundany)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 bb_error_msg_and_die ("nothing in /proc - not mounted?");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000141}
142
143
144static void
145do_stop(void)
146{
147 char what[1024];
Eric Andersen950d8b42001-10-31 09:55:39 +0000148 pid_list *p;
149 int killed = 0;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000150
151 if (cmdname)
152 strcpy(what, cmdname);
153 else if (execname)
154 strcpy(what, execname);
155 else if (userspec)
156 sprintf(what, "process(es) owned by `%s'", userspec);
157 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 bb_error_msg_and_die ("internal error, please report");
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000159
160 if (!found) {
161 printf("no %s found; none killed.\n", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000162 return;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000163 }
164 for (p = found; p; p = p->next) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000165 if (kill(p->pid, signal_nr) == 0) {
166 p->pid = -p->pid;
167 killed++;
168 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 bb_perror_msg("warning: failed to kill %d:", p->pid);
Eric Andersen950d8b42001-10-31 09:55:39 +0000170 }
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000171 }
172 if (killed) {
173 printf("stopped %s (pid", what);
Eric Andersen950d8b42001-10-31 09:55:39 +0000174 for (p = found; p; p = p->next)
175 if(p->pid < 0)
176 printf(" %d", -p->pid);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000177 printf(").\n");
178 }
179}
180
181
Eric Andersenaa820db2003-07-26 09:10:35 +0000182static const struct option ssd_long_options[] = {
183 { "stop", 0, NULL, 'K' },
184 { "start", 0, NULL, 'S' },
185 { "background", 0, NULL, 'b' },
186 { "startas", 1, NULL, 'a' },
187 { "name", 1, NULL, 'n' },
188 { "signal", 1, NULL, 's' },
189 { "user", 1, NULL, 'u' },
190 { "exec", 1, NULL, 'x' },
191 { 0, 0, 0, 0 }
192};
193
Glenn L McGrath8d441782004-01-22 09:04:58 +0000194#define SSD_CTX_STOP 1
195#define SSD_CTX_START 2
196#define SSD_OPT_BACKGROUND 4
197
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000198int
199start_stop_daemon_main(int argc, char **argv)
200{
Glenn L McGrath8d441782004-01-22 09:04:58 +0000201 unsigned long opt;
Eric Andersen08804ce2003-07-30 08:29:56 +0000202 char *signame = NULL;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000203 char *startas = NULL;
204
Eric Andersenaa820db2003-07-26 09:10:35 +0000205 bb_applet_long_options = ssd_long_options;
206
Glenn L McGrath2d016a32004-01-23 21:43:49 +0000207 bb_opt_complementaly = "K~S:S~K";
Glenn L McGrath8d441782004-01-22 09:04:58 +0000208 opt = bb_getopt_ulflags(argc, argv, "KSba:n:s:u:x:",
Eric Andersenaa820db2003-07-26 09:10:35 +0000209 &startas, &cmdname, &signame, &userspec, &execname);
210
Glenn L McGrath8d441782004-01-22 09:04:58 +0000211 /* Check one and only one context option was given */
Glenn L McGrath2d016a32004-01-23 21:43:49 +0000212 if ((opt & 0x80000000UL) || (opt & (SSD_CTX_STOP | SSD_CTX_START)) == 0) {
Glenn L McGrath8d441782004-01-22 09:04:58 +0000213 bb_show_usage();
214 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000215
Eric Andersen08804ce2003-07-30 08:29:56 +0000216 if (signame) {
217 signal_nr = bb_xgetlarg(signame, 10, 0, NSIG);
218 }
Eric Andersenaa820db2003-07-26 09:10:35 +0000219
Eric Andersenaa820db2003-07-26 09:10:35 +0000220 if (!execname && !userspec)
221 bb_error_msg_and_die ("need at least one of -x or -u");
222
223 if (!startas)
224 startas = execname;
225
Glenn L McGrath8d441782004-01-22 09:04:58 +0000226 if ((opt & SSD_CTX_START) && !startas)
Eric Andersenaa820db2003-07-26 09:10:35 +0000227 bb_error_msg_and_die ("-S needs -x or -a");
228
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000229 argc -= optind;
230 argv += optind;
231
Eric Andersen950d8b42001-10-31 09:55:39 +0000232 if (userspec && sscanf(userspec, "%d", &user_id) != 1)
233 user_id = my_getpwnam(userspec);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000234
235 do_procfs();
236
Glenn L McGrath8d441782004-01-22 09:04:58 +0000237 if (opt & SSD_CTX_STOP) {
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000238 do_stop();
Eric Andersen950d8b42001-10-31 09:55:39 +0000239 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000240 }
241
242 if (found) {
Eric Andersen950d8b42001-10-31 09:55:39 +0000243 printf("%s already running.\n%d\n", execname ,found->pid);
244 return EXIT_SUCCESS;
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000245 }
246 *--argv = startas;
Glenn L McGrath8d441782004-01-22 09:04:58 +0000247 if (opt & SSD_OPT_BACKGROUND) {
Eric Andersen53a22992002-01-26 09:04:45 +0000248 if (daemon(0, 0) == -1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000249 bb_perror_msg_and_die ("unable to fork");
Eric Andersen53a22992002-01-26 09:04:45 +0000250 }
251 setsid();
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000252 execv(startas, argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000253 bb_perror_msg_and_die ("unable to start %s", startas);
Eric Andersenc2af1ee2001-10-18 19:33:06 +0000254}