diff options
Diffstat (limited to 'urunlevel/runlevel/killproc.c')
-rw-r--r-- | urunlevel/runlevel/killproc.c | 173 |
1 files changed, 81 insertions, 92 deletions
diff --git a/urunlevel/runlevel/killproc.c b/urunlevel/runlevel/killproc.c index fd91c10..2d802bd 100644 --- a/urunlevel/runlevel/killproc.c +++ b/urunlevel/runlevel/killproc.c | |||
@@ -3,6 +3,10 @@ | |||
3 | * | 3 | * |
4 | * Copyright (C) 2005 by David Seikel won_fang@yahoo.com.au | 4 | * Copyright (C) 2005 by David Seikel won_fang@yahoo.com.au |
5 | * | 5 | * |
6 | * | ||
7 | * Clean room implementation of LSB init.d specs. | ||
8 | * A tiny bit was stolen from procps/kill.c in busybox. | ||
9 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | 10 | * 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 | 11 | * 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 | 12 | * the Free Software Foundation; either version 2 of the License, or |
@@ -31,61 +35,53 @@ | |||
31 | 35 | ||
32 | int killproc(char *pidfile, char *pathname, int my_signal) | 36 | int killproc(char *pidfile, char *pathname, int my_signal) |
33 | { | 37 | { |
34 | char *pids = NULL; | 38 | char *pids = NULL; |
35 | int status = pidofproc(pidfile, pathname, &pids); | 39 | int status = pidofproc(pidfile, pathname, &pids); |
40 | |||
41 | if (status == INIT_D_STATUS_OK) { | ||
42 | int i; | ||
43 | char *pid; | ||
44 | char *strtok_temp; | ||
45 | |||
46 | pid = strtok_r(pids, " ", &strtok_temp); | ||
47 | for (i = 0; pid != NULL; i++) { | ||
48 | int pid_num; | ||
49 | |||
50 | if (!isdigit(*pid)) | ||
51 | bb_error_msg("Bad PID '%s'", pid); | ||
52 | pid_num = strtol(pid, NULL, 0); | ||
53 | if (my_signal != 0) { | ||
54 | if (kill(pid_num, my_signal) != 0) | ||
55 | bb_perror_msg("Could not kill pid '%d'", pid_num); | ||
56 | } else { | ||
57 | if (kill(pid_num, SIGTERM) != 0) | ||
58 | bb_perror_msg("Could not kill pid '%d'", pid_num); | ||
59 | sleep(1); | ||
60 | status = pidofproc(pidfile, pathname, &pids); | ||
61 | if (status == INIT_D_STATUS_OK) { | ||
62 | sleep(5); | ||
63 | status = pidofproc(pidfile, pathname, &pids); | ||
64 | if (status == INIT_D_STATUS_OK) { | ||
65 | if (kill(pid_num, SIGKILL) != 0) | ||
66 | bb_perror_msg("Could not kill pid '%d'", pid_num); | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | pid = strtok_r(NULL, " ", &strtok_temp); | ||
72 | } | ||
36 | 73 | ||
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); | 74 | status = pidofproc(pidfile, pathname, &pids); |
62 | if (status == INIT_D_STATUS_OK) | 75 | if (status == INIT_D_STATUS_DEAD_PID) { |
63 | { | 76 | if (pidfile != NULL) |
64 | sleep(5); | 77 | remove_file(pidfile, FILEUTILS_FORCE); |
65 | status = pidofproc(pidfile, pathname, &pids); | 78 | status = INIT_D_STATUS_NOT_RUNNING; |
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 | } | 79 | } |
72 | } | 80 | if ((my_signal == 0) && (status == INIT_D_STATUS_NOT_RUNNING)) |
73 | 81 | status = INIT_D_STATUS_OK; | |
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 | } | 82 | } |
84 | if ((my_signal == 0) && (status == INIT_D_STATUS_NOT_RUNNING)) | ||
85 | status = INIT_D_STATUS_OK; | ||
86 | } | ||
87 | 83 | ||
88 | return status; | 84 | return status; |
89 | } | 85 | } |
90 | 86 | ||
91 | 87 | ||
@@ -111,52 +107,45 @@ killproc [-p pidfile] pathname [signal] | |||
111 | */ | 107 | */ |
112 | 108 | ||
113 | 109 | ||
110 | /* getopt not used, because I don't think it can handle [signal] */ | ||
114 | int killproc_main(int argc, char **argv) | 111 | int killproc_main(int argc, char **argv) |
115 | { | 112 | { |
116 | int i; | 113 | int i; |
117 | int my_signal = 0; | 114 | int my_signal = 0; |
118 | char *pidfile = NULL; | 115 | char *pidfile = NULL; |
119 | char *pathname = NULL; | 116 | char *pathname = NULL; |
120 | char *signame = NULL; | 117 | char *signame = NULL; |
121 | 118 | ||
122 | for (i = 1; i < argc; i++) | 119 | for (i = 1; i < argc; i++) { |
123 | { | 120 | char *p = argv[i]; |
124 | char *p = argv[i]; | 121 | |
125 | 122 | if (*p == '-') { | |
126 | if (*p == '-') | 123 | p++; |
127 | { | 124 | if (*p == 'p') { |
128 | p++; | 125 | if ((i + 1) < argc) |
129 | if (*p == 'p') | 126 | pidfile = argv[++i]; |
130 | { | 127 | else |
131 | if ((i + 1) < argc) | 128 | bb_show_usage(); |
132 | pidfile = argv[++i]; | 129 | } else |
130 | signame = &argv[i][1]; | ||
131 | } else if (pathname == NULL) | ||
132 | pathname = p; | ||
133 | else if (signame == NULL) | ||
134 | signame = p; | ||
133 | else | 135 | else |
134 | bb_show_usage(); | 136 | 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 | } | 137 | } |
158 | } | ||
159 | 138 | ||
139 | if (pathname == NULL) | ||
140 | bb_show_usage(); | ||
141 | |||
142 | if (signame != NULL) { | ||
143 | u_signal_names(signame, &my_signal, 1); | ||
144 | if (my_signal == 0) { | ||
145 | log_failure_msg("unknown signal."); | ||
146 | bb_show_usage(); | ||
147 | } | ||
148 | } | ||
160 | //bb_printf("ARGS - %s %s %d = %s\n", pidfile, pathname, my_signal, u_signal_names(NULL, &signal, 1)); | 149 | //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); | 150 | return killproc(pidfile, pathname, my_signal); |
162 | } | 151 | } |