]>
git.wh0rd.org - sysvinit.git/blob - contrib/start-stop-daemon.c
2 * A rewrite of the original Debian's start-stop-daemon Perl script
3 * in C (faster - it is executed many times during system startup).
5 * Written by Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>,
21 #define VERSION "version 0.3, 1996-06-05"
23 static int testmode
= 0;
24 static int quietmode
= 0;
25 static int exitnodo
= 1;
28 static int signal_nr
= 15;
29 static int user_id
= -1;
30 static const char *userspec
= NULL
;
31 static const char *cmdname
= NULL
;
32 static char *execname
= NULL
;
33 static char *startas
= NULL
;
34 static const char *pidfile
= NULL
;
35 static const char *progname
= "";
37 static struct stat exec_stat
;
40 struct pid_list
*next
;
44 static struct pid_list
*found
= NULL
;
45 static struct pid_list
*killed
= NULL
;
47 static void *xmalloc(int size
);
48 static void push(struct pid_list
**list
, int pid
);
49 static void do_help(void);
50 static void parse_options(int argc
, char * const *argv
);
51 static int pid_is_exec(int pid
, const struct stat
*esb
);
52 static int pid_is_user(int pid
, int uid
);
53 static int pid_is_cmd(int pid
, const char *name
);
54 static void check(int pid
);
55 static void do_pidfile(const char *name
);
56 static void do_procfs(void);
57 static void do_stop(void);
60 static void fatal(const char *format
, ...)
61 __attribute__((noreturn
, format(printf
, 1, 2)));
62 static void badusage(const char *msg
)
63 __attribute__((noreturn
));
65 static void fatal(const char *format
, ...);
66 static void badusage(const char *msg
);
70 fatal(const char *format
, ...)
74 fprintf(stderr
, "%s: ", progname
);
75 va_start(arglist
, format
);
76 vfprintf(stderr
, format
, arglist
);
91 fatal("malloc(%d) failed", size
);
96 push(struct pid_list
**list
, int pid
)
100 p
= xmalloc(sizeof(*p
));
111 start-stop-daemon for Debian Linux - small and fast C version written by\n\
112 Marek Michalkiewicz <marekm@i17linuxb.ists.pwr.wroc.pl>, public domain.\n"
116 start-stop-daemon -S|--start options ... -- arguments ...\n\
117 start-stop-daemon -K|--stop options ...\n\
118 start-stop-daemon -H|--help\n\
119 start-stop-daemon -V|--version\n\
121 Options (at least one of --exec|--pidfile|--user is required):
122 -x|--exec <executable> program to start/check if it is running\n\
123 -p|--pidfile <pid-file> pid file to check\n\
124 -u|--user <username>|<uid> stop this user's processes\n\
125 -n|--name <process-name> stop processes with this name\n\
126 -s|--signal <signal> signal to send (default 15)\n\
127 -a|--startas <pathname> program to start (default <executable>)\n\
128 -t|--test test mode, don't do anything\n\
129 -o|--oknodo exit status 0 (not 1) if nothing done\n\
130 -q|--quiet | -v, --verbose\n\
132 Exit status: 0 = done 1 = nothing done (=> 0 if --oknodo) 2 = trouble\n");
137 badusage(const char *msg
)
140 fprintf(stderr
, "%s: %s\n", progname
, msg
);
141 fprintf(stderr
, "Try `%s --help' for more information.\n", progname
);
147 parse_options(int argc
, char * const *argv
)
149 static struct option longopts
[] = {
150 { "help", 0, NULL
, 'H'},
151 { "stop", 0, NULL
, 'K'},
152 { "start", 0, NULL
, 'S'},
153 { "version", 0, NULL
, 'V'},
154 { "startas", 1, NULL
, 'a'},
155 { "name", 1, NULL
, 'n'},
156 { "oknodo", 0, NULL
, 'o'},
157 { "pidfile", 1, NULL
, 'p'},
158 { "quiet", 0, NULL
, 'q'},
159 { "signal", 1, NULL
, 's'},
160 { "test", 0, NULL
, 't'},
161 { "user", 1, NULL
, 'u'},
162 { "verbose", 0, NULL
, 'v'},
163 { "exec", 1, NULL
, 'x'},
169 c
= getopt_long(argc
, argv
, "HKSVa:n:op:qs:tu:vx:",
170 longopts
, (int *) 0);
174 case 'H': /* --help */
177 case 'K': /* --stop */
180 case 'S': /* --start */
183 case 'V': /* --version */
184 printf("start-stop-daemon " VERSION
"\n");
186 case 'a': /* --startas <pathname> */
189 case 'n': /* --name <process-name> */
192 case 'o': /* --oknodo */
195 case 'p': /* --pidfile <pid-file> */
198 case 'q': /* --quiet */
201 case 's': /* --signal <signal> */
202 if (sscanf(optarg
, "%d", &signal_nr
) != 1)
203 badusage("--signal takes a numeric argument");
205 case 't': /* --test */
208 case 'u': /* --user <username>|<uid> */
211 case 'v': /* --verbose */
214 case 'x': /* --exec <executable> */
218 badusage(""); /* message printed by getopt */
223 badusage("need one of --start or --stop");
225 if (!execname
&& !pidfile
&& !userspec
)
226 badusage("need at least one of --exec, --pidfile or --user");
231 if (start
&& !startas
)
232 badusage("--start needs --exec or --startas");
237 pid_is_exec(int pid
, const struct stat
*esb
)
242 sprintf(buf
, "/proc/%d/exe", pid
);
243 if (stat(buf
, &sb
) != 0)
245 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
250 pid_is_user(int pid
, int uid
)
255 sprintf(buf
, "/proc/%d", pid
);
256 if (stat(buf
, &sb
) != 0)
258 return (sb
.st_uid
== uid
);
263 pid_is_cmd(int pid
, const char *name
)
269 sprintf(buf
, "/proc/%d/stat", pid
);
273 while ((c
= getc(f
)) != EOF
&& c
!= '(')
279 /* this hopefully handles command names containing ')' */
280 while ((c
= getc(f
)) != EOF
&& c
== *name
)
283 return (c
== ')' && *name
== '\0');
290 if (execname
&& !pid_is_exec(pid
, &exec_stat
))
292 if (userspec
&& !pid_is_user(pid
, user_id
))
294 if (cmdname
&& !pid_is_cmd(pid
, cmdname
))
301 do_pidfile(const char *name
)
306 f
= fopen(name
, "r");
308 if (fscanf(f
, "%d", &pid
) == 1)
319 struct dirent
*entry
;
322 procdir
= opendir("/proc");
324 fatal("opendir /proc: %s", strerror(errno
));
327 while ((entry
= readdir(procdir
)) != NULL
) {
328 if (sscanf(entry
->d_name
, "%d", &pid
) != 1)
335 fatal("nothing in /proc - not mounted?");
346 strcpy(what
, cmdname
);
348 strcpy(what
, execname
);
350 sprintf(what
, "process in pidfile `%s'", pidfile
);
352 sprintf(what
, "process(es) owned by `%s'", userspec
);
354 fatal("internal error, please report");
358 printf("no %s found; none killed.\n", what
);
361 for (p
= found
; p
; p
= p
->next
) {
363 printf("would send signal %d to %d.\n",
365 else if (kill(p
->pid
, signal_nr
) == 0)
366 push(&killed
, p
->pid
);
368 printf("%s: warning: failed to kill %d: %s\n",
369 progname
, p
->pid
, strerror(errno
));
371 if (quietmode
< 0 && killed
) {
372 printf("stopped %s (pid", what
);
373 for (p
= killed
; p
; p
= p
->next
)
374 printf(" %d", p
->pid
);
381 main(int argc
, char **argv
)
385 parse_options(argc
, argv
);
389 if (execname
&& stat(execname
, &exec_stat
))
390 fatal("stat %s: %s", execname
, strerror(errno
));
392 if (userspec
&& sscanf(userspec
, "%d", &user_id
) != 1) {
395 pw
= getpwnam(userspec
);
397 fatal("user `%s' not found\n", userspec
);
399 user_id
= pw
->pw_uid
;
414 printf("%s already running.\n", execname
);
418 printf("would start %s ", startas
);
420 printf("%s ", *argv
++);
425 printf("starting %s ...\n", startas
);
427 execv(startas
, argv
);
428 fatal("unable to start %s: %s", startas
, strerror(errno
));