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>,
6 * public domain. Based conceptually on start-stop-daemon.pl, by Ian
7 * Jackson <ijackson@gnu.ai.mit.edu>. May be used and distributed
8 * freely for any purpose. Changes by Christian Schwarz
9 * <schwarz@monet.m.isar.de>, to make output conform to the Debian
10 * Console Message Standard, also placed in public domain. Minor
11 * changes by Klee Dienes <klee@debian.org>, also placed in the Public
14 * Changes by Ben Collins <bcollins@debian.org>, added --chuid, --background
15 * and --make-pidfile options, placed in public domain as well.
17 * Port to OpenBSD by Sontri Tomo Huynh <huynh.29@osu.edu>
18 * and Andreas Schuldei <andreas@schuldei.org>
20 * Changes by Ian Jackson: added --retry (and associated rearrangements).
24 /* On at least Solaris <= 11.3 procfs is not compatible with LFS. */
25 #if !DPKG_STRUCTURED_PROCFS_SUPPORTS_LFS
26 #undef _FILE_OFFSET_BITS
30 #include <dpkg/macros.h>
32 #if defined(__linux__)
34 #elif defined(__GNU__)
36 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
38 #elif defined(__NetBSD__)
40 #elif defined(__OpenBSD__)
42 #elif defined(__DragonFly__)
43 # define OS_DragonFlyBSD
44 #elif defined(__APPLE__) && defined(__MACH__)
53 # error Unknown architecture - cannot build start-stop-daemon
56 #if defined(OS_NetBSD)
57 /* NetBSD needs this to expose struct proc. */
59 #elif defined(OS_Solaris)
60 /* Solaris needs this to expose the new structured procfs API. */
61 #define _STRUCTURED_PROC 1
64 #ifdef HAVE_SYS_PARAM_H
65 #include <sys/param.h>
67 #ifdef HAVE_SYS_SYSCALL_H
68 #include <sys/syscall.h>
70 #ifdef HAVE_SYS_SYSCTL_H
71 #include <sys/sysctl.h>
73 #ifdef HAVE_SYS_PROCFS_H
74 #include <sys/procfs.h>
76 #ifdef HAVE_SYS_PROC_H
79 #ifdef HAVE_SYS_USER_H
82 #ifdef HAVE_SYS_PSTAT_H
83 #include <sys/pstat.h>
85 #include <sys/types.h>
89 #include <sys/select.h>
90 #include <sys/ioctl.h>
91 #include <sys/socket.h>
124 #if defined(OS_Darwin)
130 #if defined(OS_FreeBSD)
131 #define KVM_MEMFILE "/dev/null"
133 #define KVM_MEMFILE NULL
137 #if defined(_POSIX_PRIORITY_SCHEDULING) && _POSIX_PRIORITY_SCHEDULING > 0
140 #define SCHED_OTHER -1
141 #define SCHED_FIFO -1
145 /* At least macOS and AIX do not define this. */
146 #ifndef SOCK_NONBLOCK
147 #define SOCK_NONBLOCK 0
150 #if defined(OS_Linux)
151 /* This comes from TASK_COMM_LEN defined in Linux' include/linux/sched.h. */
152 #define PROCESS_NAME_SIZE 15
153 #elif defined(OS_Solaris)
154 #define PROCESS_NAME_SIZE 15
155 #elif defined(OS_Darwin)
156 #define PROCESS_NAME_SIZE 16
157 #elif defined(OS_AIX)
158 /* This comes from PRFNSZ defined in AIX's <sys/procfs.h>. */
159 #define PROCESS_NAME_SIZE 16
160 #elif defined(OS_NetBSD)
161 #define PROCESS_NAME_SIZE 16
162 #elif defined(OS_OpenBSD)
163 #define PROCESS_NAME_SIZE 16
164 #elif defined(OS_FreeBSD)
165 #define PROCESS_NAME_SIZE 19
166 #elif defined(OS_DragonFlyBSD)
167 /* On DragonFlyBSD MAXCOMLEN expands to 16. */
168 #define PROCESS_NAME_SIZE MAXCOMLEN
171 #if defined(SYS_ioprio_set) && defined(linux)
172 #define HAVE_IOPRIO_SET
175 #define IOPRIO_CLASS_SHIFT 13
176 #define IOPRIO_PRIO_VALUE(class, prio) (((class) << IOPRIO_CLASS_SHIFT) | (prio))
177 #define IO_SCHED_PRIO_MIN 0
178 #define IO_SCHED_PRIO_MAX 7
181 IOPRIO_WHO_PROCESS
= 1,
200 enum LIBCOMPAT_ATTR_ENUM_FLAGS match_code
{
204 MATCH_PIDFILE
= 1 << 2,
210 /* Time conversion constants. */
212 NANOSEC_IN_SEC
= 1000000000L,
213 NANOSEC_IN_MILLISEC
= 1000000L,
214 NANOSEC_IN_MICROSEC
= 1000L,
217 /* The minimum polling interval, 20ms. */
218 static const long MIN_POLL_INTERVAL
= 20L * NANOSEC_IN_MILLISEC
;
220 static enum action_code action
;
221 static enum match_code match_mode
;
222 static bool testmode
= false;
223 static int quietmode
= 0;
224 static int exitnodo
= 1;
225 static bool background
= false;
226 static bool close_io
= true;
227 static const char *output_io
;
228 static bool notify_await
= false;
229 static int notify_timeout
= 60;
230 static char *notify_sockdir
;
231 static char *notify_socket
;
232 static bool mpidfile
= false;
233 static bool rpidfile
= false;
234 static int signal_nr
= SIGTERM
;
235 static int user_id
= -1;
236 static int runas_uid
= -1;
237 static int runas_gid
= -1;
238 static const char *userspec
= NULL
;
239 static char *changeuser
= NULL
;
240 static const char *changegroup
= NULL
;
241 static char *changeroot
= NULL
;
242 static const char *changedir
= "/";
243 static const char *cmdname
= NULL
;
244 static char *execname
= NULL
;
245 static char *startas
= NULL
;
246 static pid_t match_pid
= -1;
247 static pid_t match_ppid
= -1;
248 static const char *pidfile
= NULL
;
249 static char *what_stop
= NULL
;
250 static const char *progname
= "";
251 static int nicelevel
= 0;
252 static int umask_value
= -1;
254 static struct stat exec_stat
;
256 static struct proc_stat_list
*procset
= NULL
;
259 /* LSB Init Script process status exit codes. */
262 STATUS_DEAD_PIDFILE
= 1,
263 STATUS_DEAD_LOCKFILE
= 2,
269 struct pid_list
*next
;
273 static struct pid_list
*found
= NULL
;
274 static struct pid_list
*killed
= NULL
;
276 /* Resource scheduling policy. */
277 struct res_schedule
{
278 const char *policy_name
;
283 struct schedule_item
{
288 /* Only seen within parse_schedule and callees. */
291 /* Seconds, signal no., or index into array. */
295 static struct res_schedule
*proc_sched
= NULL
;
296 static struct res_schedule
*io_sched
= NULL
;
298 static int schedule_length
;
299 static struct schedule_item
*schedule
= NULL
;
302 static void LIBCOMPAT_ATTR_PRINTF(1)
303 debug(const char *format
, ...)
310 va_start(arglist
, format
);
311 vprintf(format
, arglist
);
315 static void LIBCOMPAT_ATTR_PRINTF(1)
316 info(const char *format
, ...)
323 va_start(arglist
, format
);
324 vprintf(format
, arglist
);
328 static void LIBCOMPAT_ATTR_PRINTF(1)
329 warning(const char *format
, ...)
333 fprintf(stderr
, "%s: warning: ", progname
);
334 va_start(arglist
, format
);
335 vfprintf(stderr
, format
, arglist
);
339 static void LIBCOMPAT_ATTR_NORET
LIBCOMPAT_ATTR_VPRINTF(2)
340 fatalv(int errno_fatal
, const char *format
, va_list args
)
344 fprintf(stderr
, "%s: ", progname
);
345 va_copy(args_copy
, args
);
346 vfprintf(stderr
, format
, args_copy
);
349 fprintf(stderr
, " (%s)\n", strerror(errno_fatal
));
351 fprintf(stderr
, "\n");
353 if (action
== ACTION_STATUS
)
354 exit(STATUS_UNKNOWN
);
359 static void LIBCOMPAT_ATTR_NORET
LIBCOMPAT_ATTR_PRINTF(1)
360 fatal(const char *format
, ...)
364 va_start(args
, format
);
365 fatalv(0, format
, args
);
366 /* cppcheck-suppress[va_end_missing]:
367 * False positive, fatalv() is non-returning. */
370 static void LIBCOMPAT_ATTR_NORET
LIBCOMPAT_ATTR_PRINTF(1)
371 fatale(const char *format
, ...)
375 va_start(args
, format
);
376 fatalv(errno
, format
, args
);
377 /* cppcheck-suppress[va_end_missing]:
378 * False positive, fatalv() is non-returning. */
381 #define BUG(...) bug(__FILE__, __LINE__, __func__, __VA_ARGS__)
383 static void LIBCOMPAT_ATTR_NORET
LIBCOMPAT_ATTR_PRINTF(4)
384 bug(const char *file
, int line
, const char *func
, const char *format
, ...)
388 fprintf(stderr
, "%s:%s:%d:%s: internal error: ",
389 progname
, file
, line
, func
);
390 va_start(arglist
, format
);
391 vfprintf(stderr
, format
, arglist
);
394 if (action
== ACTION_STATUS
)
395 exit(STATUS_UNKNOWN
);
408 fatale("malloc(%d) failed", size
);
412 xstrndup(const char *str
, size_t n
)
416 new_str
= strndup(str
, n
);
419 fatale("strndup(%s, %zu) failed", str
, n
);
423 timespec_gettime(struct timespec
*ts
)
425 #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && \
426 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK > 0
427 if (clock_gettime(CLOCK_MONOTONIC
, ts
) < 0)
428 fatale("clock_gettime failed");
432 if (gettimeofday(&tv
, NULL
) != 0)
433 fatale("gettimeofday failed");
435 ts
->tv_sec
= tv
.tv_sec
;
436 ts
->tv_nsec
= tv
.tv_usec
* NANOSEC_IN_MICROSEC
;
440 #define timespec_cmp(a, b, OP) \
441 (((a)->tv_sec == (b)->tv_sec) ? \
442 ((a)->tv_nsec OP (b)->tv_nsec) : \
443 ((a)->tv_sec OP (b)->tv_sec))
446 timespec_sub(struct timespec
*a
, struct timespec
*b
, struct timespec
*res
)
448 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
;
449 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
;
450 if (res
->tv_nsec
< 0) {
452 res
->tv_nsec
+= NANOSEC_IN_SEC
;
457 timespec_mul(struct timespec
*a
, int b
)
459 long nsec
= a
->tv_nsec
* b
;
462 a
->tv_sec
+= nsec
/ NANOSEC_IN_SEC
;
463 a
->tv_nsec
= nsec
% NANOSEC_IN_SEC
;
467 newpath(const char *dirname
, const char *filename
)
472 path_len
= strlen(dirname
) + 1 + strlen(filename
) + 1;
473 path
= xmalloc(path_len
);
474 snprintf(path
, path_len
, "%s/%s", dirname
, filename
);
480 parse_unsigned(const char *string
, int base
, int *value_r
)
489 value
= strtol(string
, &endptr
, base
);
490 if (string
== endptr
|| *endptr
!= '\0' || errno
!= 0)
492 if (value
< 0 || value
> INT_MAX
)
500 get_open_fd_max(void)
502 #ifdef HAVE_GETDTABLESIZE
503 return getdtablesize();
505 return sysconf(_SC_OPEN_MAX
);
511 detach_controlling_tty(void)
513 #ifdef HAVE_TIOCNOTTY
516 tty_fd
= open("/dev/tty", O_RDWR
);
518 /* The current process does not have a controlling tty. */
522 if (ioctl(tty_fd
, TIOCNOTTY
, 0) != 0)
523 fatale("unable to detach controlling tty");
532 if (setpgid(0, 0) < 0)
535 detach_controlling_tty();
542 wait_for_child(pid_t pid
)
548 child
= waitpid(pid
, &status
, 0);
549 } while (child
== -1 && errno
== EINTR
);
552 fatal("error waiting for child");
554 if (WIFEXITED(status
)) {
555 int ret
= WEXITSTATUS(status
);
558 fatal("child returned error exit status %d", ret
);
559 } else if (WIFSIGNALED(status
)) {
560 int signo
= WTERMSIG(status
);
562 fatal("child was killed by signal %d", signo
);
564 fatal("unexpected status %d waiting for child", status
);
569 cleanup_socket_dir(void)
571 (void)unlink(notify_socket
);
572 (void)rmdir(notify_sockdir
);
576 setup_socket_name(const char *suffix
)
580 if (getuid() == 0 && access(RUNSTATEDIR
, F_OK
) == 0) {
581 basedir
= RUNSTATEDIR
;
583 basedir
= getenv("TMPDIR");
588 if (asprintf(¬ify_sockdir
, "%s/%s.XXXXXX", basedir
, suffix
) < 0)
589 fatale("cannot allocate socket directory name");
591 if (mkdtemp(notify_sockdir
) == NULL
)
592 fatale("cannot create socket directory %s", notify_sockdir
);
594 atexit(cleanup_socket_dir
);
596 if (chown(notify_sockdir
, runas_uid
, runas_gid
))
597 fatale("cannot change socket directory ownership");
599 if (asprintf(¬ify_socket
, "%s/notify", notify_sockdir
) < 0)
600 fatale("cannot allocate socket name");
602 setenv("NOTIFY_SOCKET", notify_socket
, 1);
604 return notify_socket
;
608 set_socket_passcred(int fd
)
611 static const int enable
= 1;
613 (void)setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &enable
, sizeof(enable
));
618 create_notify_socket(void)
620 const char *sockname
;
621 struct sockaddr_un su
;
624 /* Create notification socket. */
625 fd
= socket(AF_UNIX
, SOCK_DGRAM
| SOCK_NONBLOCK
, 0);
627 fatale("cannot create notification socket");
629 /* We could set SOCK_CLOEXEC instead, but then we would need to
630 * check whether the socket call failed, try and then do this anyway,
631 * when we have no threading problems to worry about. */
632 flags
= fcntl(fd
, F_GETFD
);
634 fatale("cannot read fd flags for notification socket");
635 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
636 fatale("cannot set close-on-exec flag for notification socket");
638 sockname
= setup_socket_name(".s-s-d-notify");
640 /* Bind to a socket in a temporary directory, selected based on
642 memset(&su
, 0, sizeof(su
));
643 su
.sun_family
= AF_UNIX
;
644 strncpy(su
.sun_path
, sockname
, sizeof(su
.sun_path
) - 1);
646 rc
= bind(fd
, (struct sockaddr
*)&su
, sizeof(su
));
648 fatale("cannot bind to notification socket");
650 rc
= chmod(su
.sun_path
, 0660);
652 fatale("cannot change notification socket permissions");
654 rc
= chown(su
.sun_path
, runas_uid
, runas_gid
);
656 fatale("cannot change notification socket ownership");
658 /* XXX: Verify we are talking to an expected child? Although it is not
659 * clear whether this is feasible given the knowledge we have got. */
660 set_socket_passcred(fd
);
666 wait_for_notify(int fd
)
668 struct timespec startat
, now
, elapsed
, timeout
, timeout_orig
;
671 timeout
.tv_sec
= notify_timeout
;
673 timeout_orig
= timeout
;
675 timespec_gettime(&startat
);
677 while (timeout
.tv_sec
>= 0 && timeout
.tv_nsec
>= 0) {
683 /* Wait for input. */
684 debug("Waiting for notifications... (timeout %lusec %lunsec)\n",
685 timeout
.tv_sec
, timeout
.tv_nsec
);
686 rc
= pselect(fd
+ 1, &fdrs
, NULL
, NULL
, &timeout
, NULL
);
688 /* Catch non-restartable errors, that is, not signals nor
689 * kernel out of resources. */
690 if (rc
< 0 && (errno
!= EINTR
&& errno
!= EAGAIN
))
691 fatale("cannot monitor notification socket for activity");
695 fatal("timed out waiting for a notification");
697 /* Update the timeout, as should not rely on pselect() having
698 * done that for us, which is an unportable assumption. */
699 timespec_gettime(&now
);
700 timespec_sub(&now
, &startat
, &elapsed
);
701 timespec_sub(&timeout_orig
, &elapsed
, &timeout
);
703 /* Restartable error, a signal or kernel out of resources. */
707 /* Parse it and check for a supported notification message,
708 * once we get a READY=1, we exit. */
712 char *line
, *line_next
;
714 nrecv
= recv(fd
, buf
, sizeof(buf
), 0);
715 if (nrecv
< 0 && (errno
!= EINTR
&& errno
!= EAGAIN
))
716 fatale("cannot receive notification packet");
722 for (line
= buf
; *line
; line
= line_next
) {
723 line_next
= strchrnul(line
, '\n');
724 if (*line_next
== '\n')
727 debug("Child sent some notification...\n");
728 if (strncmp(line
, "EXTEND_TIMEOUT_USEC=", 20) == 0) {
731 if (parse_unsigned(line
+ 20, 10, &extend_usec
) != 0)
732 fatale("cannot parse extended timeout notification %s", line
);
734 /* Reset the current timeout. */
735 timeout
.tv_sec
= extend_usec
/ 1000L;
736 timeout
.tv_nsec
= (extend_usec
% 1000L) *
738 timeout_orig
= timeout
;
740 timespec_gettime(&startat
);
741 } else if (strncmp(line
, "ERRNO=", 6) == 0) {
744 if (parse_unsigned(line
+ 6, 10, &suberrno
) != 0)
745 fatale("cannot parse errno notification %s", line
);
747 fatale("program failed to initialize");
748 } else if (strcmp(line
, "READY=1") == 0) {
749 debug("-> Notification => ready for service.\n");
752 debug("-> Notification line '%s' received\n", line
);
760 write_pidfile(const char *filename
, pid_t pid
)
765 fd
= open(filename
, O_CREAT
| O_WRONLY
| O_TRUNC
| O_NOFOLLOW
, 0666);
769 fp
= fdopen(fd
, "w");
772 fatale("unable to open pidfile '%s' for writing", filename
);
774 fprintf(fp
, "%d\n", pid
);
777 fatale("unable to close pidfile '%s'", filename
);
781 remove_pidfile(const char *filename
)
783 if (unlink(filename
) < 0 && errno
!= ENOENT
)
784 fatale("cannot remove pidfile '%s'", filename
);
795 debug("Detaching to start %s...\n", startas
);
797 /* Block SIGCHLD to allow waiting for the child process while it is
798 * performing actions, such as creating a pidfile. */
800 sigaddset(&mask
, SIGCHLD
);
801 if (sigprocmask(SIG_BLOCK
, &mask
, &oldmask
) == -1)
802 fatale("cannot block SIGCHLD");
805 notify_fd
= create_notify_socket();
809 fatale("unable to do first fork");
810 else if (pid
) { /* First Parent. */
811 /* Wait for the second parent to exit, so that if we need to
812 * perform any actions there, like creating a pidfile, we do
813 * not suffer from race conditions on return. */
817 /* Wait for a readiness notification from the second
818 * child, so that we can safely exit when the service
820 wait_for_notify(notify_fd
);
822 cleanup_socket_dir();
828 /* Close the notification socket, even though it is close-on-exec. */
832 /* Create a new session. */
834 fatale("cannot set session ID");
838 fatale("unable to do second fork");
839 else if (pid
) { /* Second parent. */
840 /* Set a default umask for dumb programs, which might get
841 * overridden by the --umask option later on, so that we get
842 * a defined umask when creating the pidfile. */
845 if (mpidfile
&& pidfile
!= NULL
)
846 /* User wants _us_ to make the pidfile. */
847 write_pidfile(pidfile
, pid
);
852 if (sigprocmask(SIG_SETMASK
, &oldmask
, NULL
) == -1)
853 fatale("cannot restore signal mask");
855 debug("Detaching complete...\n");
859 pid_list_push(struct pid_list
**list
, pid_t pid
)
863 p
= xmalloc(sizeof(*p
));
870 pid_list_free(struct pid_list
**list
)
872 struct pid_list
*here
, *next
;
874 for (here
= *list
; here
!= NULL
; here
= next
) {
886 "Usage: start-stop-daemon [<option>...] <command>\n"
891 " -S, --start -- <argument>... start a program and pass <arguments> to it\n"
892 " -K, --stop stop a program\n"
893 " -T, --status get the program status\n"
894 " -H, --help print help information\n"
895 " -V, --version print version\n"
899 "Matching options (at least one is required):\n"
900 " --pid <pid> pid to check\n"
901 " --ppid <ppid> parent pid to check\n"
902 " -p, --pidfile <pid-file> pid file to check\n"
903 " -x, --exec <executable> program to start/check if it is running\n"
904 " -n, --name <process-name> process name to check\n"
905 " -u, --user <username|uid> process owner to check\n"
910 " -g, --group <group|gid> run process as this group\n"
911 " -c, --chuid <name|uid[:group|gid]>\n"
912 " change to this user/group before starting\n"
914 " -s, --signal <signal> signal to send (default TERM)\n"
915 " -a, --startas <pathname> program to start (default is <executable>)\n"
916 " -r, --chroot <directory> chroot to <directory> before starting\n"
917 " -d, --chdir <directory> change to <directory> (default is /)\n"
918 " -N, --nicelevel <incr> add incr to the process' nice level\n"
919 " -P, --procsched <policy[:prio]>\n"
920 " use <policy> with <prio> for the kernel\n"
921 " process scheduler (default prio is 0)\n"
922 " -I, --iosched <class[:prio]> use <class> with <prio> to set the IO\n"
923 " scheduler (default prio is 4)\n"
924 " -k, --umask <mask> change the umask to <mask> before starting\n"
925 " -b, --background force the process to detach\n"
926 " --notify-await wait for a readiness notification\n"
927 " --notify-timeout <int> timeout after <int> seconds of notify wait\n"
928 " -C, --no-close do not close any file descriptor\n"
929 " -O, --output <filename> send stdout and stderr to <filename>\n"
930 " -m, --make-pidfile create the pidfile before starting\n"
931 " --remove-pidfile delete the pidfile after stopping\n"
932 " -R, --retry <schedule> check whether processes die, and retry\n"
933 " -t, --test test mode, don't do anything\n"
934 " -o, --oknodo exit status 0 (not 1) if nothing done\n"
935 " -q, --quiet be more quiet\n"
936 " -v, --verbose be more verbose\n"
940 "Retry <schedule> is <item>|/<item>/... where <item> is one of\n"
941 " -<signal-num>|[-]<signal-name> send that signal\n"
942 " <timeout> wait that many seconds\n"
943 " forever repeat remainder forever\n"
944 "or <schedule> may be just <timeout>, meaning <signal>/<timeout>/KILL/<timeout>\n"
948 "The process scheduler <policy> can be one of:\n"
949 " other, fifo or rr\n"
953 "The IO scheduler <class> can be one of:\n"
954 " real-time, best-effort or idle\n"
960 " 1 = nothing done (=> 0 if --oknodo)\n"
961 " 2 = with --retry, processes would not die\n"
963 "Exit status with --status:\n"
964 " 0 = program is running\n"
965 " 1 = program is not running and the pid file exists\n"
966 " 3 = program is not running\n"
967 " 4 = unable to determine status\n");
973 printf("start-stop-daemon %s for Debian\n\n", VERSION
);
975 printf("Written by Marek Michalkiewicz, public domain.\n");
978 static void LIBCOMPAT_ATTR_NORET
979 badusage(const char *msg
)
982 fprintf(stderr
, "%s: %s\n", progname
, msg
);
983 fprintf(stderr
, "Try '%s --help' for more information.\n", progname
);
985 if (action
== ACTION_STATUS
)
986 exit(STATUS_UNKNOWN
);
996 static const struct sigpair siglist
[] = {
1003 { "KILL", SIGKILL
},
1004 { "PIPE", SIGPIPE
},
1005 { "QUIT", SIGQUIT
},
1006 { "SEGV", SIGSEGV
},
1007 { "TERM", SIGTERM
},
1008 { "USR1", SIGUSR1
},
1009 { "USR2", SIGUSR2
},
1010 { "CHLD", SIGCHLD
},
1011 { "CONT", SIGCONT
},
1012 { "STOP", SIGSTOP
},
1013 { "TSTP", SIGTSTP
},
1014 { "TTIN", SIGTTIN
},
1019 parse_pid(const char *pid_str
, int *pid_num
)
1021 if (parse_unsigned(pid_str
, 10, pid_num
) != 0)
1030 parse_signal(const char *sig_str
, int *sig_num
)
1034 if (parse_unsigned(sig_str
, 10, sig_num
) == 0)
1037 for (i
= 0; i
< array_count(siglist
); i
++) {
1038 if (strcmp(sig_str
, siglist
[i
].name
) == 0) {
1039 *sig_num
= siglist
[i
].signal
;
1047 parse_umask(const char *string
, int *value_r
)
1049 return parse_unsigned(string
, 0, value_r
);
1053 validate_proc_schedule(void)
1055 #if defined(_POSIX_PRIORITY_SCHEDULING) && _POSIX_PRIORITY_SCHEDULING > 0
1056 int prio_min
, prio_max
;
1058 prio_min
= sched_get_priority_min(proc_sched
->policy
);
1059 prio_max
= sched_get_priority_max(proc_sched
->policy
);
1061 if (proc_sched
->priority
< prio_min
)
1062 badusage("process scheduler priority less than min");
1063 if (proc_sched
->priority
> prio_max
)
1064 badusage("process scheduler priority greater than max");
1069 parse_proc_schedule(const char *string
)
1075 policy_len
= strcspn(string
, ":");
1076 policy_str
= xstrndup(string
, policy_len
);
1078 if (string
[policy_len
] == ':' &&
1079 parse_unsigned(string
+ policy_len
+ 1, 10, &prio
) != 0)
1080 fatale("invalid process scheduler priority");
1082 proc_sched
= xmalloc(sizeof(*proc_sched
));
1083 proc_sched
->policy_name
= policy_str
;
1085 if (strcmp(policy_str
, "other") == 0) {
1086 proc_sched
->policy
= SCHED_OTHER
;
1087 proc_sched
->priority
= 0;
1088 } else if (strcmp(policy_str
, "fifo") == 0) {
1089 proc_sched
->policy
= SCHED_FIFO
;
1090 proc_sched
->priority
= prio
;
1091 } else if (strcmp(policy_str
, "rr") == 0) {
1092 proc_sched
->policy
= SCHED_RR
;
1093 proc_sched
->priority
= prio
;
1095 badusage("invalid process scheduler policy");
1097 validate_proc_schedule();
1101 parse_io_schedule(const char *string
)
1107 class_len
= strcspn(string
, ":");
1108 class_str
= xstrndup(string
, class_len
);
1110 if (string
[class_len
] == ':' &&
1111 parse_unsigned(string
+ class_len
+ 1, 10, &prio
) != 0)
1112 fatale("invalid IO scheduler priority");
1114 io_sched
= xmalloc(sizeof(*io_sched
));
1115 io_sched
->policy_name
= class_str
;
1117 if (strcmp(class_str
, "real-time") == 0) {
1118 io_sched
->policy
= IOPRIO_CLASS_RT
;
1119 io_sched
->priority
= prio
;
1120 } else if (strcmp(class_str
, "best-effort") == 0) {
1121 io_sched
->policy
= IOPRIO_CLASS_BE
;
1122 io_sched
->priority
= prio
;
1123 } else if (strcmp(class_str
, "idle") == 0) {
1124 io_sched
->policy
= IOPRIO_CLASS_IDLE
;
1125 io_sched
->priority
= 7;
1127 badusage("invalid IO scheduler policy");
1129 if (io_sched
->priority
< IO_SCHED_PRIO_MIN
)
1130 badusage("IO scheduler priority less than min");
1131 if (io_sched
->priority
> IO_SCHED_PRIO_MAX
)
1132 badusage("IO scheduler priority greater than max");
1136 set_proc_schedule(struct res_schedule
*sched
)
1138 #if defined(_POSIX_PRIORITY_SCHEDULING) && _POSIX_PRIORITY_SCHEDULING > 0
1139 struct sched_param param
;
1141 param
.sched_priority
= sched
->priority
;
1143 if (sched_setscheduler(getpid(), sched
->policy
, ¶m
) == -1)
1144 fatale("unable to set process scheduler");
1148 #ifdef HAVE_IOPRIO_SET
1150 ioprio_set(int which
, int who
, int ioprio
)
1152 return syscall(SYS_ioprio_set
, which
, who
, ioprio
);
1157 set_io_schedule(struct res_schedule
*sched
)
1159 #ifdef HAVE_IOPRIO_SET
1162 io_sched_mask
= IOPRIO_PRIO_VALUE(sched
->policy
, sched
->priority
);
1163 if (ioprio_set(IOPRIO_WHO_PROCESS
, getpid(), io_sched_mask
) == -1)
1164 warning("unable to alter IO priority to mask %i (%s)\n",
1165 io_sched_mask
, strerror(errno
));
1170 parse_schedule_item(const char *string
, struct schedule_item
*item
)
1172 const char *after_hyph
;
1174 if (strcmp(string
, "forever") == 0) {
1175 item
->type
= sched_forever
;
1176 } else if (isdigit(string
[0])) {
1177 item
->type
= sched_timeout
;
1178 if (parse_unsigned(string
, 10, &item
->value
) != 0)
1179 badusage("invalid timeout value in schedule");
1180 } else if ((after_hyph
= string
+ (string
[0] == '-')) &&
1181 parse_signal(after_hyph
, &item
->value
) == 0) {
1182 item
->type
= sched_signal
;
1184 badusage("invalid schedule item (must be [-]<signal-name>, "
1185 "-<signal-number>, <timeout> or 'forever'");
1190 parse_schedule(const char *schedule_str
)
1196 for (slash
= schedule_str
; *slash
; slash
++)
1200 schedule_length
= (count
== 0) ? 4 : count
+ 1;
1201 schedule
= xmalloc(sizeof(*schedule
) * schedule_length
);
1204 schedule
[0].type
= sched_signal
;
1205 schedule
[0].value
= signal_nr
;
1206 parse_schedule_item(schedule_str
, &schedule
[1]);
1207 if (schedule
[1].type
!= sched_timeout
) {
1208 badusage("--retry takes timeout, or schedule list"
1209 " of at least two items");
1211 schedule
[2].type
= sched_signal
;
1212 schedule
[2].value
= SIGKILL
;
1213 schedule
[3] = schedule
[1];
1219 while (*schedule_str
) {
1223 slash
= strchrnul(schedule_str
, '/');
1224 str_len
= (size_t)(slash
- schedule_str
);
1225 if (str_len
>= sizeof(item_buf
))
1226 badusage("invalid schedule item: far too long"
1227 " (you must delimit items with slashes)");
1228 memcpy(item_buf
, schedule_str
, str_len
);
1229 item_buf
[str_len
] = '\0';
1230 schedule_str
= *slash
? slash
+ 1 : slash
;
1232 parse_schedule_item(item_buf
, &schedule
[count
]);
1233 if (schedule
[count
].type
== sched_forever
) {
1235 badusage("invalid schedule: 'forever'"
1236 " appears more than once");
1242 if (repeatat
== count
)
1243 badusage("invalid schedule: 'forever' appears last, "
1244 "nothing to repeat");
1245 if (repeatat
>= 0) {
1246 schedule
[count
].type
= sched_goto
;
1247 schedule
[count
].value
= repeatat
;
1250 if (count
!= schedule_length
)
1251 BUG("count=%d != schedule_length=%d",
1252 count
, schedule_length
);
1257 set_action(enum action_code new_action
)
1259 if (action
== new_action
)
1262 if (action
!= ACTION_NONE
)
1263 badusage("only one command can be specified");
1265 action
= new_action
;
1269 #define OPT_PPID 501
1270 #define OPT_RM_PIDFILE 502
1271 #define OPT_NOTIFY_AWAIT 503
1272 #define OPT_NOTIFY_TIMEOUT 504
1275 parse_options(int argc
, char * const *argv
)
1277 static struct option longopts
[] = {
1278 { "help", 0, NULL
, 'H'},
1279 { "stop", 0, NULL
, 'K'},
1280 { "start", 0, NULL
, 'S'},
1281 { "status", 0, NULL
, 'T'},
1282 { "version", 0, NULL
, 'V'},
1283 { "startas", 1, NULL
, 'a'},
1284 { "name", 1, NULL
, 'n'},
1285 { "oknodo", 0, NULL
, 'o'},
1286 { "pid", 1, NULL
, OPT_PID
},
1287 { "ppid", 1, NULL
, OPT_PPID
},
1288 { "pidfile", 1, NULL
, 'p'},
1289 { "quiet", 0, NULL
, 'q'},
1290 { "signal", 1, NULL
, 's'},
1291 { "test", 0, NULL
, 't'},
1292 { "user", 1, NULL
, 'u'},
1293 { "group", 1, NULL
, 'g'},
1294 { "chroot", 1, NULL
, 'r'},
1295 { "verbose", 0, NULL
, 'v'},
1296 { "exec", 1, NULL
, 'x'},
1297 { "chuid", 1, NULL
, 'c'},
1298 { "nicelevel", 1, NULL
, 'N'},
1299 { "procsched", 1, NULL
, 'P'},
1300 { "iosched", 1, NULL
, 'I'},
1301 { "umask", 1, NULL
, 'k'},
1302 { "background", 0, NULL
, 'b'},
1303 { "notify-await", 0, NULL
, OPT_NOTIFY_AWAIT
},
1304 { "notify-timeout", 1, NULL
, OPT_NOTIFY_TIMEOUT
},
1305 { "no-close", 0, NULL
, 'C'},
1306 { "output", 1, NULL
, 'O'},
1307 { "make-pidfile", 0, NULL
, 'm'},
1308 { "remove-pidfile", 0, NULL
, OPT_RM_PIDFILE
},
1309 { "retry", 1, NULL
, 'R'},
1310 { "chdir", 1, NULL
, 'd'},
1311 { NULL
, 0, NULL
, 0 }
1313 const char *pid_str
= NULL
;
1314 const char *ppid_str
= NULL
;
1315 const char *umask_str
= NULL
;
1316 const char *signal_str
= NULL
;
1317 const char *schedule_str
= NULL
;
1318 const char *proc_schedule_str
= NULL
;
1319 const char *io_schedule_str
= NULL
;
1320 const char *notify_timeout_str
= NULL
;
1321 size_t changeuser_len
;
1325 c
= getopt_long(argc
, argv
,
1326 "HKSVTa:n:op:qr:s:tu:vx:c:N:P:I:k:bCO:mR:g:d:",
1331 case 'H': /* --help */
1334 case 'K': /* --stop */
1335 set_action(ACTION_STOP
);
1337 case 'S': /* --start */
1338 set_action(ACTION_START
);
1340 case 'T': /* --status */
1341 set_action(ACTION_STATUS
);
1343 case 'V': /* --version */
1346 case 'a': /* --startas <pathname> */
1349 case 'n': /* --name <process-name> */
1350 match_mode
|= MATCH_NAME
;
1353 case 'o': /* --oknodo */
1356 case OPT_PID
: /* --pid <pid> */
1357 match_mode
|= MATCH_PID
;
1360 case OPT_PPID
: /* --ppid <ppid> */
1361 match_mode
|= MATCH_PPID
;
1364 case 'p': /* --pidfile <pid-file> */
1365 match_mode
|= MATCH_PIDFILE
;
1368 case 'q': /* --quiet */
1371 case 's': /* --signal <signal> */
1372 signal_str
= optarg
;
1374 case 't': /* --test */
1377 case 'u': /* --user <username>|<uid> */
1378 match_mode
|= MATCH_USER
;
1381 case 'v': /* --verbose */
1384 case 'x': /* --exec <executable> */
1385 match_mode
|= MATCH_EXEC
;
1388 case 'c': /* --chuid <username>|<uid> */
1390 /* We copy the string just in case we need the
1391 * argument later. */
1392 changeuser_len
= strcspn(optarg
, ":");
1393 changeuser
= xstrndup(optarg
, changeuser_len
);
1394 if (optarg
[changeuser_len
] == ':') {
1395 if (optarg
[changeuser_len
+ 1] == '\0')
1396 fatal("missing group name");
1397 changegroup
= optarg
+ changeuser_len
+ 1;
1400 case 'g': /* --group <group>|<gid> */
1401 changegroup
= optarg
;
1403 case 'r': /* --chroot /new/root */
1404 changeroot
= optarg
;
1406 case 'N': /* --nice */
1407 nicelevel
= atoi(optarg
);
1409 case 'P': /* --procsched */
1410 proc_schedule_str
= optarg
;
1412 case 'I': /* --iosched */
1413 io_schedule_str
= optarg
;
1415 case 'k': /* --umask <mask> */
1418 case 'b': /* --background */
1421 case OPT_NOTIFY_AWAIT
:
1422 notify_await
= true;
1424 case OPT_NOTIFY_TIMEOUT
:
1425 notify_timeout_str
= optarg
;
1427 case 'C': /* --no-close */
1430 case 'O': /* --outout <filename> */
1433 case 'm': /* --make-pidfile */
1436 case OPT_RM_PIDFILE
: /* --remove-pidfile */
1439 case 'R': /* --retry <schedule>|<timeout> */
1440 schedule_str
= optarg
;
1442 case 'd': /* --chdir /new/dir */
1446 /* Message printed by getopt. */
1451 if (pid_str
!= NULL
) {
1452 if (parse_pid(pid_str
, &match_pid
) != 0)
1453 badusage("pid value must be a number greater than 0");
1456 if (ppid_str
!= NULL
) {
1457 if (parse_pid(ppid_str
, &match_ppid
) != 0)
1458 badusage("ppid value must be a number greater than 0");
1461 if (signal_str
!= NULL
) {
1462 if (parse_signal(signal_str
, &signal_nr
) != 0)
1463 badusage("signal value must be numeric or name"
1464 " of signal (KILL, INT, ...)");
1467 if (schedule_str
!= NULL
) {
1468 parse_schedule(schedule_str
);
1471 if (proc_schedule_str
!= NULL
)
1472 parse_proc_schedule(proc_schedule_str
);
1474 if (io_schedule_str
!= NULL
)
1475 parse_io_schedule(io_schedule_str
);
1477 if (umask_str
!= NULL
) {
1478 if (parse_umask(umask_str
, &umask_value
) != 0)
1479 badusage("umask value must be a positive number");
1482 if (output_io
!= NULL
&& output_io
[0] != '/')
1483 badusage("--output file needs to be an absolute filename");
1485 if (notify_timeout_str
!= NULL
)
1486 if (parse_unsigned(notify_timeout_str
, 10, ¬ify_timeout
) != 0)
1487 badusage("invalid notify timeout value");
1489 if (action
== ACTION_NONE
)
1490 badusage("need one of --start or --stop or --status");
1492 if (match_mode
== MATCH_NONE
||
1493 (!execname
&& !cmdname
&& !userspec
&&
1494 !pid_str
&& !ppid_str
&& !pidfile
))
1495 badusage("need at least one of --exec, --pid, --ppid, --pidfile, --user or --name");
1497 #ifdef PROCESS_NAME_SIZE
1498 if (cmdname
&& strlen(cmdname
) > PROCESS_NAME_SIZE
)
1499 warning("this system is not able to track process names\n"
1500 "longer than %d characters, please use --exec "
1501 "instead of --name.\n", PROCESS_NAME_SIZE
);
1507 if (action
== ACTION_START
&& !startas
)
1508 badusage("--start needs --exec or --startas");
1510 if (mpidfile
&& pidfile
== NULL
)
1511 badusage("--make-pidfile requires --pidfile");
1512 if (rpidfile
&& pidfile
== NULL
)
1513 badusage("--remove-pidfile requires --pidfile");
1515 if (pid_str
&& pidfile
)
1516 badusage("need either --pid or --pidfile, not both");
1518 if (background
&& action
!= ACTION_START
)
1519 badusage("--background is only relevant with --start");
1521 if (!close_io
&& !background
)
1522 badusage("--no-close is only relevant with --background");
1523 if (output_io
&& !background
)
1524 badusage("--output is only relevant with --background");
1526 if (close_io
&& output_io
== NULL
)
1527 output_io
= "/dev/null";
1536 /* If it's a relative path, normalize it. */
1537 if (execname
[0] != '/')
1538 execname
= newpath(changedir
, execname
);
1541 fullexecname
= newpath(changeroot
, execname
);
1543 fullexecname
= execname
;
1545 if (stat(fullexecname
, &exec_stat
))
1546 fatale("unable to stat %s", fullexecname
);
1548 if (fullexecname
!= execname
)
1552 if (userspec
&& parse_unsigned(userspec
, 10, &user_id
) < 0) {
1555 pw
= getpwnam(userspec
);
1557 fatale("user '%s' not found", userspec
);
1559 user_id
= pw
->pw_uid
;
1562 if (changegroup
&& parse_unsigned(changegroup
, 10, &runas_gid
) < 0) {
1565 gr
= getgrnam(changegroup
);
1567 fatale("group '%s' not found", changegroup
);
1568 changegroup
= gr
->gr_name
;
1569 runas_gid
= gr
->gr_gid
;
1575 if (parse_unsigned(changeuser
, 10, &runas_uid
) == 0)
1576 pw
= getpwuid(runas_uid
);
1578 pw
= getpwnam(changeuser
);
1580 fatale("user '%s' not found", changeuser
);
1581 changeuser
= pw
->pw_name
;
1582 runas_uid
= pw
->pw_uid
;
1583 if (changegroup
== NULL
) {
1584 /* Pass the default group of this user. */
1585 changegroup
= ""; /* Just empty. */
1586 runas_gid
= pw
->pw_gid
;
1588 if (stat(pw
->pw_dir
, &st
) == 0)
1589 setenv("HOME", pw
->pw_dir
, 1);
1593 #if defined(OS_Linux)
1595 proc_status_field(pid_t pid
, const char *field
)
1597 static char *line
= NULL
;
1598 static size_t line_size
= 0;
1604 size_t field_len
= strlen(field
);
1606 sprintf(filename
, "/proc/%d/status", pid
);
1607 fp
= fopen(filename
, "r");
1610 while ((line_len
= getline(&line
, &line_size
, fp
)) >= 0) {
1611 if (strncasecmp(line
, field
, field_len
) == 0) {
1612 line
[line_len
- 1] = '\0';
1614 value
= line
+ field_len
;
1615 while (isspace(*value
))
1625 #elif (defined(OS_Solaris) || defined(OS_AIX)) && HAVE_STRUCT_PSINFO
1627 proc_get_psinfo(pid_t pid
, struct psinfo
*psinfo
)
1632 sprintf(filename
, "/proc/%d/psinfo", pid
);
1633 fp
= fopen(filename
, "r");
1636 if (fread(psinfo
, sizeof(*psinfo
), 1, fp
) == 0) {
1649 #elif defined(OS_Hurd)
1653 struct ps_context
*context
;
1656 err
= ps_context_create(getproc(), &context
);
1658 error(1, err
, "ps_context_create");
1660 err
= proc_stat_list_create(context
, &procset
);
1662 error(1, err
, "proc_stat_list_create");
1664 err
= proc_stat_list_add_all(procset
, 0, 0);
1666 error(1, err
, "proc_stat_list_add_all");
1669 static struct proc_stat
*
1670 get_proc_stat(pid_t pid
, ps_flags_t flags
)
1672 struct proc_stat
*ps
;
1673 ps_flags_t wanted_flags
= PSTAT_PID
| flags
;
1678 ps
= proc_stat_list_pid_proc_stat(procset
, pid
);
1681 if (proc_stat_set_flags(ps
, wanted_flags
))
1683 if ((proc_stat_flags(ps
) & wanted_flags
) != wanted_flags
)
1688 #elif defined(HAVE_KVM_H)
1693 char errbuf
[_POSIX2_LINE_MAX
];
1695 kd
= kvm_openfiles(NULL
, KVM_MEMFILE
, NULL
, O_RDONLY
, errbuf
);
1697 errx(1, "%s", errbuf
);
1702 static struct kinfo_proc
*
1703 ssd_kvm_get_procs(kvm_t
*kd
, int op
, int arg
, int *count
)
1705 struct kinfo_proc
*kp
;
1712 #if defined(OS_OpenBSD)
1713 kp
= kvm_getprocs(kd
, op
, arg
, sizeof(*kp
), count
);
1715 kp
= kvm_getprocs(kd
, op
, arg
, count
);
1717 if (kp
== NULL
&& errno
!= ESRCH
)
1718 errx(1, "%s", kvm_geterr(kd
));
1724 #if defined(OS_Linux)
1726 pid_is_exec(pid_t pid
, const struct stat
*esb
)
1729 char lcontents
[_POSIX_PATH_MAX
+ 1];
1731 const char deleted
[] = " (deleted)";
1735 sprintf(lname
, "/proc/%d/exe", pid
);
1736 nread
= readlink(lname
, lcontents
, sizeof(lcontents
) - 1);
1740 filename
= lcontents
;
1741 filename
[nread
] = '\0';
1743 /* OpenVZ kernels contain a bogus patch that instead of appending,
1744 * prepends the deleted marker. Workaround those. Otherwise handle
1745 * the normal appended marker. */
1746 if (strncmp(filename
, deleted
, strlen(deleted
)) == 0)
1747 filename
+= strlen(deleted
);
1748 else if (strcmp(filename
+ nread
- strlen(deleted
), deleted
) == 0)
1749 filename
[nread
- strlen(deleted
)] = '\0';
1751 if (stat(filename
, &sb
) != 0)
1754 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
1756 #elif (defined(OS_Solaris) || defined(OS_AIX)) && HAVE_STRUCT_PSINFO
1758 pid_is_exec(pid_t pid
, const struct stat
*esb
)
1763 sprintf(filename
, "/proc/%d/object/a.out", pid
);
1765 if (stat(filename
, &sb
) != 0)
1768 return sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
;
1770 #elif defined(OS_Hurd)
1772 pid_is_exec(pid_t pid
, const struct stat
*esb
)
1774 struct proc_stat
*ps
;
1776 const char *filename
;
1778 ps
= get_proc_stat(pid
, PSTAT_ARGS
);
1782 /* On old Hurd systems we have to use the argv[0] value, because
1783 * there is nothing better. */
1784 filename
= proc_stat_args(ps
);
1786 /* On new Hurd systems we can use the correct value, as long
1787 * as it's not NULL nor empty, as it was the case on the first
1788 * implementation. */
1789 if (proc_stat_set_flags(ps
, PSTAT_EXE
) == 0 &&
1790 proc_stat_flags(ps
) & PSTAT_EXE
&&
1791 proc_stat_exe(ps
) != NULL
&&
1792 proc_stat_exe(ps
)[0] != '\0')
1793 filename
= proc_stat_exe(ps
);
1796 if (stat(filename
, &sb
) != 0)
1799 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
1801 #elif defined(OS_Darwin)
1803 pid_is_exec(pid_t pid
, const struct stat
*esb
)
1806 char pathname
[_POSIX_PATH_MAX
];
1808 if (proc_pidpath(pid
, pathname
, sizeof(pathname
)) < 0)
1811 if (stat(pathname
, &sb
) != 0)
1814 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
1816 #elif defined(OS_HPUX)
1818 pid_is_exec(pid_t pid
, const struct stat
*esb
)
1820 struct pst_status pst
;
1822 if (pstat_getproc(&pst
, sizeof(pst
), (size_t)0, (int)pid
) < 0)
1824 return ((dev_t
)pst
.pst_text
.psf_fsid
.psfs_id
== esb
->st_dev
&&
1825 (ino_t
)pst
.pst_text
.psf_fileid
== esb
->st_ino
);
1827 #elif defined(OS_FreeBSD)
1829 pid_is_exec(pid_t pid
, const struct stat
*esb
)
1834 char pathname
[PATH_MAX
];
1838 mib
[2] = KERN_PROC_PATHNAME
;
1840 len
= sizeof(pathname
);
1842 error
= sysctl(mib
, 4, pathname
, &len
, NULL
, 0);
1843 if (error
!= 0 && errno
!= ESRCH
)
1848 if (stat(pathname
, &sb
) != 0)
1851 return (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
1853 #elif defined(HAVE_KVM_H)
1855 pid_is_exec(pid_t pid
, const struct stat
*esb
)
1859 struct kinfo_proc
*kp
;
1861 char buf
[_POSIX2_LINE_MAX
];
1863 char *start_argv_0_p
, *end_argv_0_p
;
1866 kd
= ssd_kvm_open();
1867 kp
= ssd_kvm_get_procs(kd
, KERN_PROC_PID
, pid
, NULL
);
1871 pid_argv_p
= kvm_getargv(kd
, kp
, argv_len
);
1872 if (pid_argv_p
== NULL
)
1873 errx(1, "%s", kvm_geterr(kd
));
1875 /* Find and compare string. */
1876 start_argv_0_p
= *pid_argv_p
;
1878 /* Find end of argv[0] then copy and cut of str there. */
1879 end_argv_0_p
= strchr(*pid_argv_p
, ' ');
1880 if (end_argv_0_p
== NULL
)
1881 /* There seems to be no space, so we have the command
1882 * already in its desired form. */
1883 start_argv_0_p
= *pid_argv_p
;
1885 /* Tests indicate that this never happens, since
1886 * kvm_getargv itself cuts of tailing stuff. This is
1887 * not what the manual page says, however. */
1888 strncpy(buf
, *pid_argv_p
, (end_argv_0_p
- start_argv_0_p
));
1889 buf
[(end_argv_0_p
- start_argv_0_p
) + 1] = '\0';
1890 start_argv_0_p
= buf
;
1893 if (stat(start_argv_0_p
, &sb
) != 0)
1896 res
= (sb
.st_dev
== esb
->st_dev
&& sb
.st_ino
== esb
->st_ino
);
1905 #if defined(OS_Linux)
1907 pid_is_child(pid_t pid
, pid_t ppid
)
1909 const char *ppid_str
;
1913 ppid_str
= proc_status_field(pid
, "PPid:");
1914 if (ppid_str
== NULL
)
1917 rc
= parse_pid(ppid_str
, &proc_ppid
);
1921 return proc_ppid
== ppid
;
1923 #elif defined(OS_Hurd)
1925 pid_is_child(pid_t pid
, pid_t ppid
)
1927 struct proc_stat
*ps
;
1928 struct procinfo
*pi
;
1930 ps
= get_proc_stat(pid
, PSTAT_PROC_INFO
);
1934 pi
= proc_stat_proc_info(ps
);
1936 return pi
->ppid
== ppid
;
1938 #elif defined(OS_Darwin)
1940 pid_is_child(pid_t pid
, pid_t ppid
)
1942 struct proc_bsdinfo pbi
;
1944 if (proc_pidinfo(pid
, PROC_PIDTBSDINFO
, 0, &pbi
, sizeof(pbi
)) < 0)
1947 return (pid_t
)pbi
.pbi_ppid
== ppid
;
1949 #elif (defined(OS_Solaris) || defined(OS_AIX)) && HAVE_STRUCT_PSINFO
1951 pid_is_child(pid_t pid
, pid_t ppid
)
1955 if (!proc_get_psinfo(pid
, &psi
))
1958 return (pid_t
)psi
.pr_ppid
== ppid
;
1960 #elif defined(OS_HPUX)
1962 pid_is_child(pid_t pid
, pid_t ppid
)
1964 struct pst_status pst
;
1966 if (pstat_getproc(&pst
, sizeof(pst
), (size_t)0, (int)pid
) < 0)
1969 return pst
.pst_ppid
== ppid
;
1971 #elif defined(OS_FreeBSD)
1973 pid_is_child(pid_t pid
, pid_t ppid
)
1975 struct kinfo_proc kp
;
1981 mib
[2] = KERN_PROC_PID
;
1985 rc
= sysctl(mib
, 4, &kp
, &len
, NULL
, 0);
1986 if (rc
!= 0 && errno
!= ESRCH
)
1988 if (len
== 0 || len
!= sizeof(kp
))
1991 return kp
.ki_ppid
== ppid
;
1993 #elif defined(HAVE_KVM_H)
1995 pid_is_child(pid_t pid
, pid_t ppid
)
1998 struct kinfo_proc
*kp
;
2002 kd
= ssd_kvm_open();
2003 kp
= ssd_kvm_get_procs(kd
, KERN_PROC_PID
, pid
, NULL
);
2007 #if defined(OS_FreeBSD)
2008 proc_ppid
= kp
->ki_ppid
;
2009 #elif defined(OS_OpenBSD)
2010 proc_ppid
= kp
->p_ppid
;
2011 #elif defined(OS_DragonFlyBSD)
2012 proc_ppid
= kp
->kp_ppid
;
2014 proc_ppid
= kp
->kp_proc
.p_ppid
;
2017 res
= (proc_ppid
== ppid
);
2026 #if defined(OS_Linux)
2028 pid_is_user(pid_t pid
, uid_t uid
)
2033 sprintf(buf
, "/proc/%d", pid
);
2034 if (stat(buf
, &sb
) != 0)
2036 return (sb
.st_uid
== uid
);
2038 #elif defined(OS_Hurd)
2040 pid_is_user(pid_t pid
, uid_t uid
)
2042 struct proc_stat
*ps
;
2044 ps
= get_proc_stat(pid
, PSTAT_OWNER_UID
);
2045 return ps
&& (uid_t
)proc_stat_owner_uid(ps
) == uid
;
2047 #elif defined(OS_Darwin)
2049 pid_is_user(pid_t pid
, uid_t uid
)
2051 struct proc_bsdinfo pbi
;
2053 if (proc_pidinfo(pid
, PROC_PIDTBSDINFO
, 0, &pbi
, sizeof(pbi
)) < 0)
2056 return pbi
.pbi_ruid
== uid
;
2058 #elif (defined(OS_Solaris) || defined(OS_AIX)) && HAVE_STRUCT_PSINFO
2060 pid_is_user(pid_t pid
, uid_t uid
)
2064 if (!proc_get_psinfo(pid
, &psi
))
2067 return psi
.pr_uid
== uid
;
2069 #elif defined(OS_HPUX)
2071 pid_is_user(pid_t pid
, uid_t uid
)
2073 struct pst_status pst
;
2075 if (pstat_getproc(&pst
, sizeof(pst
), (size_t)0, (int)pid
) < 0)
2077 return ((uid_t
)pst
.pst_uid
== uid
);
2079 #elif defined(OS_FreeBSD)
2081 pid_is_user(pid_t pid
, uid_t uid
)
2083 struct kinfo_proc kp
;
2089 mib
[2] = KERN_PROC_PID
;
2093 rc
= sysctl(mib
, 4, &kp
, &len
, NULL
, 0);
2094 if (rc
!= 0 && errno
!= ESRCH
)
2096 if (len
== 0 || len
!= sizeof(kp
))
2099 return kp
.ki_ruid
== uid
;
2101 #elif defined(HAVE_KVM_H)
2103 pid_is_user(pid_t pid
, uid_t uid
)
2107 struct kinfo_proc
*kp
;
2110 kd
= ssd_kvm_open();
2111 kp
= ssd_kvm_get_procs(kd
, KERN_PROC_PID
, pid
, NULL
);
2115 #if defined(OS_FreeBSD)
2116 proc_uid
= kp
->ki_ruid
;
2117 #elif defined(OS_OpenBSD)
2118 proc_uid
= kp
->p_ruid
;
2119 #elif defined(OS_DragonFlyBSD)
2120 proc_uid
= kp
->kp_ruid
;
2121 #elif defined(OS_NetBSD)
2122 proc_uid
= kp
->kp_eproc
.e_pcred
.p_ruid
;
2124 if (kp
->kp_proc
.p_cred
)
2125 kvm_read(kd
, (u_long
)&(kp
->kp_proc
.p_cred
->p_ruid
),
2126 &proc_uid
, sizeof(uid_t
));
2131 res
= (proc_uid
== (uid_t
)uid
);
2140 #if defined(OS_Linux)
2142 pid_is_cmd(pid_t pid
, const char *name
)
2146 comm
= proc_status_field(pid
, "Name:");
2150 return strcmp(comm
, name
) == 0;
2152 #elif defined(OS_Hurd)
2154 pid_is_cmd(pid_t pid
, const char *name
)
2156 struct proc_stat
*ps
;
2159 const char *binary_name
;
2161 ps
= get_proc_stat(pid
, PSTAT_ARGS
);
2165 argv0
= proc_stat_args(ps
);
2166 argv0_len
= strlen(argv0
) + 1;
2168 binary_name
= basename(argv0
);
2169 if (strcmp(binary_name
, name
) == 0)
2172 /* XXX: This is all kinds of ugly, but on the Hurd there's no way to
2173 * know the command name of a process, so we have to try to match
2174 * also on argv[1] for the case of an interpreted script. */
2175 if (proc_stat_args_len(ps
) > argv0_len
) {
2176 const char *script_name
= basename(argv0
+ argv0_len
);
2178 return strcmp(script_name
, name
) == 0;
2183 #elif (defined(OS_Solaris) || defined(OS_AIX)) && HAVE_STRUCT_PSINFO
2185 pid_is_cmd(pid_t pid
, const char *name
)
2189 if (!proc_get_psinfo(pid
, &psi
))
2192 return strcmp(psi
.pr_fname
, name
) == 0;
2194 #elif defined(OS_HPUX)
2196 pid_is_cmd(pid_t pid
, const char *name
)
2198 struct pst_status pst
;
2200 if (pstat_getproc(&pst
, sizeof(pst
), (size_t)0, (int)pid
) < 0)
2202 return (strcmp(pst
.pst_ucomm
, name
) == 0);
2204 #elif defined(OS_Darwin)
2206 pid_is_cmd(pid_t pid
, const char *name
)
2208 char pathname
[_POSIX_PATH_MAX
];
2210 if (proc_pidpath(pid
, pathname
, sizeof(pathname
)) < 0)
2213 return strcmp(pathname
, name
) == 0;
2215 #elif defined(OS_FreeBSD)
2217 pid_is_cmd(pid_t pid
, const char *name
)
2219 struct kinfo_proc kp
;
2225 mib
[2] = KERN_PROC_PID
;
2229 rc
= sysctl(mib
, 4, &kp
, &len
, NULL
, 0);
2230 if (rc
!= 0 && errno
!= ESRCH
)
2232 if (len
== 0 || len
!= sizeof(kp
))
2235 return strcmp(kp
.ki_comm
, name
) == 0;
2237 #elif defined(HAVE_KVM_H)
2239 pid_is_cmd(pid_t pid
, const char *name
)
2242 struct kinfo_proc
*kp
;
2246 kd
= ssd_kvm_open();
2247 kp
= ssd_kvm_get_procs(kd
, KERN_PROC_PID
, pid
, NULL
);
2251 #if defined(OS_FreeBSD)
2252 process_name
= kp
->ki_comm
;
2253 #elif defined(OS_OpenBSD)
2254 process_name
= kp
->p_comm
;
2255 #elif defined(OS_DragonFlyBSD)
2256 process_name
= kp
->kp_comm
;
2258 process_name
= kp
->kp_proc
.p_comm
;
2261 res
= (strcmp(name
, process_name
) == 0);
2270 #if defined(OS_Hurd)
2272 pid_is_running(pid_t pid
)
2274 return get_proc_stat(pid
, 0) != NULL
;
2276 #else /* !OS_Hurd */
2278 pid_is_running(pid_t pid
)
2280 if (kill(pid
, 0) == 0 || errno
== EPERM
)
2282 else if (errno
== ESRCH
)
2285 fatale("error checking pid %u status", pid
);
2289 static enum status_code
2290 pid_check(pid_t pid
)
2292 if (execname
&& !pid_is_exec(pid
, &exec_stat
))
2294 if (match_ppid
> 0 && !pid_is_child(pid
, match_ppid
))
2296 if (userspec
&& !pid_is_user(pid
, user_id
))
2298 if (cmdname
&& !pid_is_cmd(pid
, cmdname
))
2300 if (action
!= ACTION_STOP
&& !pid_is_running(pid
))
2303 pid_list_push(&found
, pid
);
2308 static enum status_code
2309 do_pidfile(const char *name
)
2312 static pid_t pid
= 0;
2315 return pid_check(pid
);
2317 f
= fopen(name
, "r");
2319 enum status_code pid_status
;
2321 /* If we are only matching on the pidfile, and it is owned by
2322 * a non-root user, then this is a security risk, and the
2323 * contents cannot be trusted, because the daemon might have
2326 * If the pidfile is world-writable we refuse to parse it.
2328 * If we got /dev/null specified as the pidfile, we ignore the
2329 * checks, as this is being used to run processes no matter
2331 if (strcmp(name
, "/dev/null") != 0) {
2335 if (fstat(fd
, &st
) < 0)
2336 fatale("cannot stat pidfile %s", name
);
2338 if (match_mode
== MATCH_PIDFILE
&&
2339 ((st
.st_uid
!= getuid() && st
.st_uid
!= 0) ||
2340 (st
.st_gid
!= getgid() && st
.st_gid
!= 0)))
2341 fatal("matching only on non-root pidfile %s is insecure", name
);
2342 if (st
.st_mode
& 0002)
2343 fatal("matching on world-writable pidfile %s is insecure", name
);
2346 if (fscanf(f
, "%d", &pid
) == 1)
2347 pid_status
= pid_check(pid
);
2349 pid_status
= STATUS_UNKNOWN
;
2352 if (pid_status
== STATUS_DEAD
)
2353 return STATUS_DEAD_PIDFILE
;
2356 } else if (errno
== ENOENT
)
2359 fatale("unable to open pidfile %s", name
);
2362 #if defined(OS_Linux) || defined(OS_Solaris) || defined(OS_AIX)
2363 static enum status_code
2367 struct dirent
*entry
;
2370 enum status_code prog_status
= STATUS_DEAD
;
2372 procdir
= opendir("/proc");
2374 fatale("unable to opendir /proc");
2377 while ((entry
= readdir(procdir
)) != NULL
) {
2378 enum status_code pid_status
;
2380 if (sscanf(entry
->d_name
, "%d", &pid
) != 1)
2384 pid_status
= pid_check(pid
);
2385 if (pid_status
< prog_status
)
2386 prog_status
= pid_status
;
2390 fatal("nothing in /proc - not mounted?");
2394 #elif defined(OS_Hurd)
2396 check_proc_stat(struct proc_stat
*ps
)
2398 pid_check(proc_stat_pid(ps
));
2402 static enum status_code
2408 proc_stat_list_for_each(procset
, check_proc_stat
);
2415 #elif defined(OS_Darwin)
2416 static enum status_code
2420 int i
, npids
, pid_bufsize
;
2421 enum status_code prog_status
= STATUS_DEAD
;
2423 npids
= proc_listallpids(NULL
, 0);
2425 return STATUS_UNKNOWN
;
2427 /* Try to avoid sudden changes in number of PIDs. */
2429 pid_bufsize
= sizeof(pid_t
) * npids
;
2430 pid_buf
= xmalloc(pid_bufsize
);
2432 npids
= proc_listallpids(pid_buf
, pid_bufsize
);
2434 return STATUS_UNKNOWN
;
2436 for (i
= 0; i
< npids
; i
++) {
2437 enum status_code pid_status
;
2439 pid_status
= pid_check(pid_buf
[i
]);
2440 if (pid_status
< prog_status
)
2441 prog_status
= pid_status
;
2448 #elif defined(OS_HPUX)
2449 static enum status_code
2452 struct pst_status pst
[10];
2455 enum status_code prog_status
= STATUS_DEAD
;
2457 while ((count
= pstat_getproc(pst
, sizeof(pst
[0]), 10, idx
)) > 0) {
2458 for (i
= 0; i
< count
; i
++) {
2459 enum status_code pid_status
;
2461 pid_status
= pid_check(pst
[i
].pst_pid
);
2462 if (pid_status
< prog_status
)
2463 prog_status
= pid_status
;
2465 idx
= pst
[count
- 1].pst_idx
+ 1;
2470 #elif defined(OS_FreeBSD)
2471 static enum status_code
2474 struct kinfo_proc
*kp
;
2478 enum status_code prog_status
= STATUS_DEAD
;
2482 mib
[2] = KERN_PROC_PROC
;
2484 rc
= sysctl(mib
, 3, NULL
, &len
, NULL
, 0);
2485 if (rc
!= 0 && errno
!= ESRCH
)
2486 return STATUS_UNKNOWN
;
2488 return STATUS_UNKNOWN
;
2491 rc
= sysctl(mib
, 3, kp
, &len
, NULL
, 0);
2492 if (rc
!= 0 && errno
!= ESRCH
)
2493 return STATUS_UNKNOWN
;
2495 return STATUS_UNKNOWN
;
2496 nentries
= len
/ sizeof(*kp
);
2498 for (i
= 0; i
< nentries
; i
++) {
2499 enum status_code pid_status
;
2501 pid_status
= pid_check(kp
[i
].ki_pid
);
2502 if (pid_status
< prog_status
)
2503 prog_status
= pid_status
;
2510 #elif defined(HAVE_KVM_H)
2511 static enum status_code
2516 struct kinfo_proc
*kp
;
2517 enum status_code prog_status
= STATUS_DEAD
;
2519 kd
= ssd_kvm_open();
2520 kp
= ssd_kvm_get_procs(kd
, KERN_PROC_ALL
, 0, &nentries
);
2522 for (i
= 0; i
< nentries
; i
++) {
2523 enum status_code pid_status
;
2526 #if defined(OS_FreeBSD)
2528 #elif defined(OS_OpenBSD)
2530 #elif defined(OS_DragonFlyBSD)
2533 pid
= kp
[i
].kp_proc
.p_pid
;
2536 pid_status
= pid_check(pid
);
2537 if (pid_status
< prog_status
)
2538 prog_status
= pid_status
;
2547 static enum status_code
2550 pid_list_free(&found
);
2553 return pid_check(match_pid
);
2555 return do_pidfile(pidfile
);
2557 return do_procinit();
2561 do_start(int argc
, char **argv
)
2563 int devnull_fd
= -1;
2571 info("%s already running.\n", execname
? execname
: "process");
2574 if (testmode
&& quietmode
<= 0) {
2575 printf("Would start %s ", startas
);
2577 printf("%s ", *argv
++);
2578 if (changeuser
!= NULL
) {
2579 printf(" (as user %s[%d]", changeuser
, runas_uid
);
2580 if (changegroup
!= NULL
)
2581 printf(", and group %s[%d])", changegroup
, runas_gid
);
2585 if (changeroot
!= NULL
)
2586 printf(" in directory %s", changeroot
);
2588 printf(", and add %i to the priority", nicelevel
);
2590 printf(", with scheduling policy %s with priority %i",
2591 proc_sched
->policy_name
, proc_sched
->priority
);
2593 printf(", with IO scheduling class %s with priority %i",
2594 io_sched
->policy_name
, io_sched
->priority
);
2599 debug("Starting %s...\n", startas
);
2601 if (umask_value
>= 0)
2604 /* Ok, we need to detach this process. */
2606 else if (mpidfile
&& pidfile
!= NULL
)
2607 /* User wants _us_ to make the pidfile, but detach themself! */
2608 write_pidfile(pidfile
, getpid());
2609 if (background
&& close_io
) {
2610 devnull_fd
= open("/dev/null", O_RDONLY
);
2612 fatale("unable to open '%s'", "/dev/null");
2614 if (background
&& output_io
) {
2615 output_fd
= open(output_io
, O_CREAT
| O_WRONLY
| O_APPEND
, 0664);
2617 fatale("unable to open '%s'", output_io
);
2621 if ((nice(nicelevel
) == -1) && (errno
!= 0))
2622 fatale("unable to alter nice level by %i", nicelevel
);
2625 set_proc_schedule(proc_sched
);
2627 set_io_schedule(io_sched
);
2628 if (changeroot
!= NULL
) {
2629 if (chdir(changeroot
) < 0)
2630 fatale("unable to chdir() to %s", changeroot
);
2631 if (chroot(changeroot
) < 0)
2632 fatale("unable to chroot() to %s", changeroot
);
2634 if (chdir(changedir
) < 0)
2635 fatale("unable to chdir() to %s", changedir
);
2639 if (changegroup
!= NULL
) {
2640 if (rgid
!= (gid_t
)runas_gid
)
2641 if (setgid(runas_gid
))
2642 fatale("unable to set gid to %d", runas_gid
);
2644 if (changeuser
!= NULL
) {
2645 /* We assume that if our real user and group are the same as
2646 * the ones we should switch to, the supplementary groups
2647 * will be already in place. */
2648 if (rgid
!= (gid_t
)runas_gid
|| ruid
!= (uid_t
)runas_uid
)
2649 if (initgroups(changeuser
, runas_gid
))
2650 fatale("unable to set initgroups() with gid %d",
2653 if (ruid
!= (uid_t
)runas_uid
)
2654 if (setuid(runas_uid
))
2655 fatale("unable to set uid to %s", changeuser
);
2658 if (background
&& output_fd
>= 0) {
2659 dup2(output_fd
, 1); /* stdout */
2660 dup2(output_fd
, 2); /* stderr */
2662 if (background
&& close_io
) {
2665 dup2(devnull_fd
, 0); /* stdin */
2667 /* Now close all extra fds. */
2668 for (i
= get_open_fd_max() - 1; i
>= 3; --i
)
2671 execv(startas
, argv
);
2672 fatale("unable to start %s", startas
);
2676 do_stop(int sig_num
, int *n_killed
, int *n_notkilled
)
2688 pid_list_free(&killed
);
2690 for (p
= found
; p
; p
= p
->next
) {
2692 info("Would send signal %d to %d.\n", sig_num
, p
->pid
);
2694 } else if (kill(p
->pid
, sig_num
) == 0) {
2695 pid_list_push(&killed
, p
->pid
);
2699 warning("failed to kill %d: %s\n",
2700 p
->pid
, strerror(errno
));
2707 do_stop_summary(int retry_nr
)
2711 if (quietmode
>= 0 || !killed
)
2714 printf("Stopped %s (pid", what_stop
);
2715 for (p
= killed
; p
; p
= p
->next
)
2716 printf(" %d", p
->pid
);
2719 printf(", retry #%d", retry_nr
);
2723 static void LIBCOMPAT_ATTR_PRINTF(1)
2724 set_what_stop(const char *format
, ...)
2729 va_start(arglist
, format
);
2730 rc
= vasprintf(&what_stop
, format
, arglist
);
2734 fatale("cannot allocate formatted string");
2738 * We want to keep polling for the processes, to see if they've exited, or
2739 * until the timeout expires.
2741 * This is a somewhat complicated algorithm to try to ensure that we notice
2742 * reasonably quickly when all the processes have exited, but don't spend
2743 * too much CPU time polling. In particular, on a fast machine with
2744 * quick-exiting daemons we don't want to delay system shutdown too much,
2745 * whereas on a slow one, or where processes are taking some time to exit,
2746 * we want to increase the polling interval.
2748 * The algorithm is as follows: we measure the elapsed time it takes to do
2749 * one poll(), and wait a multiple of this time for the next poll. However,
2750 * if that would put us past the end of the timeout period we wait only as
2751 * long as the timeout period, but in any case we always wait at least
2752 * MIN_POLL_INTERVAL (20ms). The multiple (‘ratio’) starts out as 2, and
2753 * increases by 1 for each poll to a maximum of 10; so we use up to between
2754 * 30% and 10% of the machine's resources (assuming a few reasonable things
2755 * about system performance).
2758 do_stop_timeout(int timeout
, int *n_killed
, int *n_notkilled
)
2760 struct timespec stopat
, before
, after
, interval
, maxinterval
;
2763 timespec_gettime(&stopat
);
2764 stopat
.tv_sec
+= timeout
;
2769 timespec_gettime(&before
);
2770 if (timespec_cmp(&before
, &stopat
, >))
2773 do_stop(0, n_killed
, n_notkilled
);
2777 timespec_gettime(&after
);
2779 if (!timespec_cmp(&after
, &stopat
, <))
2785 timespec_sub(&stopat
, &after
, &maxinterval
);
2786 timespec_sub(&after
, &before
, &interval
);
2787 timespec_mul(&interval
, ratio
);
2789 if (interval
.tv_sec
< 0 || interval
.tv_nsec
< 0)
2790 interval
.tv_sec
= interval
.tv_nsec
= 0;
2792 if (timespec_cmp(&interval
, &maxinterval
, >))
2793 interval
= maxinterval
;
2795 if (interval
.tv_sec
== 0 &&
2796 interval
.tv_nsec
<= MIN_POLL_INTERVAL
)
2797 interval
.tv_nsec
= MIN_POLL_INTERVAL
;
2799 rc
= pselect(0, NULL
, NULL
, NULL
, &interval
, NULL
);
2800 if (rc
< 0 && errno
!= EINTR
)
2801 fatale("select() failed for pause");
2806 finish_stop_schedule(bool anykilled
)
2808 if (rpidfile
&& pidfile
&& !testmode
)
2809 remove_pidfile(pidfile
);
2814 info("No %s found running; none killed.\n", what_stop
);
2820 run_stop_schedule(void)
2822 int position
, n_killed
, n_notkilled
, value
, retry_nr
;
2826 if (schedule
!= NULL
) {
2827 info("Ignoring --retry in test mode\n");
2833 set_what_stop("%s", cmdname
);
2835 set_what_stop("%s", execname
);
2837 set_what_stop("process in pidfile '%s'", pidfile
);
2838 else if (match_pid
> 0)
2839 set_what_stop("process with pid %d", match_pid
);
2840 else if (match_ppid
> 0)
2841 set_what_stop("process(es) with parent pid %d", match_ppid
);
2843 set_what_stop("process(es) owned by '%s'", userspec
);
2845 BUG("no match option, please report");
2852 if (schedule
== NULL
) {
2853 do_stop(signal_nr
, &n_killed
, &n_notkilled
);
2855 if (n_notkilled
> 0)
2856 info("%d pids were not killed\n", n_notkilled
);
2859 return finish_stop_schedule(anykilled
);
2862 for (position
= 0; position
< schedule_length
; position
++) {
2864 value
= schedule
[position
].value
;
2867 switch (schedule
[position
].type
) {
2872 do_stop(value
, &n_killed
, &n_notkilled
);
2873 do_stop_summary(retry_nr
++);
2875 return finish_stop_schedule(anykilled
);
2880 if (do_stop_timeout(value
, &n_killed
, &n_notkilled
))
2881 return finish_stop_schedule(anykilled
);
2885 BUG("schedule[%d].type value %d is not valid",
2886 position
, schedule
[position
].type
);
2890 info("Program %s, %d process(es), refused to die.\n",
2891 what_stop
, n_killed
);
2897 main(int argc
, char **argv
)
2901 parse_options(argc
, argv
);
2907 if (action
== ACTION_START
)
2908 return do_start(argc
, argv
);
2909 else if (action
== ACTION_STOP
)
2910 return run_stop_schedule();
2911 else if (action
== ACTION_STATUS
)
2912 return do_findprocs();