summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/killproc.c
diff options
context:
space:
mode:
Diffstat (limited to 'urunlevel/runlevel/killproc.c')
-rw-r--r--urunlevel/runlevel/killproc.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/urunlevel/runlevel/killproc.c b/urunlevel/runlevel/killproc.c
new file mode 100644
index 0000000..fd91c10
--- /dev/null
+++ b/urunlevel/runlevel/killproc.c
@@ -0,0 +1,162 @@
1/*
2 * Mini killproc implementation for busybox.
3 *
4 * Copyright (C) 2005 by David Seikel won_fang@yahoo.com.au
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 *
21 */
22
23#include <ctype.h>
24#include <signal.h>
25#include <string.h>
26#include <unistd.h>
27
28#include "busybox.h"
29#include "lib_init_d.h"
30
31
32int killproc(char *pidfile, char *pathname, int my_signal)
33{
34 char *pids = NULL;
35 int status = pidofproc(pidfile, pathname, &pids);
36
37 if (status == INIT_D_STATUS_OK)
38 {
39 int i;
40 char *pid;
41 char *strtok_temp;
42
43 pid = strtok_r(pids, " ", &strtok_temp);
44 for (i = 0; pid != NULL; i++)
45 {
46 int pid_num;
47
48 if (!isdigit(*pid))
49 bb_error_msg( "Bad PID '%s'", pid);
50 pid_num = strtol(pid, NULL, 0);
51 if (my_signal != 0)
52 {
53 if (kill(pid_num, my_signal) != 0)
54 bb_perror_msg( "Could not kill pid '%d'", pid_num);
55 }
56 else
57 {
58 if (kill(pid_num, SIGTERM) != 0)
59 bb_perror_msg( "Could not kill pid '%d'", pid_num);
60 sleep(1);
61 status = pidofproc(pidfile, pathname, &pids);
62 if (status == INIT_D_STATUS_OK)
63 {
64 sleep(5);
65 status = pidofproc(pidfile, pathname, &pids);
66 if (status == INIT_D_STATUS_OK)
67 {
68 if (kill(pid_num, SIGKILL) != 0)
69 bb_perror_msg( "Could not kill pid '%d'", pid_num);
70 }
71 }
72 }
73
74 pid = strtok_r(NULL, " ", &strtok_temp);
75 }
76
77 status = pidofproc(pidfile, pathname, &pids);
78 if (status == INIT_D_STATUS_DEAD_PID)
79 {
80 if (pidfile != NULL)
81 remove_file(pidfile, FILEUTILS_FORCE);
82 status = INIT_D_STATUS_NOT_RUNNING;
83 }
84 if ((my_signal == 0) && (status == INIT_D_STATUS_NOT_RUNNING))
85 status = INIT_D_STATUS_OK;
86 }
87
88 return status;
89}
90
91
92/*
93killproc [-p pidfile] pathname [signal]
94 This stops the specified program. The program is
95 found using the algorithm given above. If a
96 signal is specified, using the
97 -signal_name or -signal_number syntaxes
98 as specified by the kill command,
99 the program is sent that signal.
100 Otherwise, a SIGTERM followed by a SIGKILL
101 after some number of seconds shall be sent.
102 If a program has been terminated, the pidfile should be removed if the
103 terminated process has not already done so.
104 Compliant applications may use the basename instead of the
105 pathname.
106
107 killproc should return the LSB defined exit status codes. If called
108 without a signal, it shall return 0 if the program has been stopped or
109 is not running and not 0 otherwise. If a signal is given, it shall return 0
110 only if the program is running.
111*/
112
113
114int killproc_main(int argc, char **argv)
115{
116 int i;
117 int my_signal = 0;
118 char *pidfile = NULL;
119 char *pathname = NULL;
120 char *signame = NULL;
121
122 for (i = 1; i < argc; i++)
123 {
124 char *p = argv[i];
125
126 if (*p == '-')
127 {
128 p++;
129 if (*p == 'p')
130 {
131 if ((i + 1) < argc)
132 pidfile = argv[++i];
133 else
134 bb_show_usage();
135 }
136 else
137 signame = &argv[i][1];
138 }
139 else if (pathname == NULL)
140 pathname = p;
141 else if (signame == NULL)
142 signame = p;
143 else
144 bb_show_usage();
145 }
146
147 if (pathname == NULL)
148 bb_show_usage();
149
150 if (signame != NULL)
151 {
152 u_signal_names(signame, &my_signal, 1);
153 if (my_signal == 0)
154 {
155 log_failure_msg("unknown signal.");
156 bb_show_usage();
157 }
158 }
159
160//bb_printf("ARGS - %s %s %d = %s\n", pidfile, pathname, my_signal, u_signal_names(NULL, &signal, 1));
161 return killproc(pidfile, pathname, my_signal);
162}