1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <time.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <dhcpmsg.h>
#include "script_handler.h"
/*
* scripts are directly managed by a script helper process. dhcpagent creates
* the helper process and it, in turn, creates a process to run the script
* dhcpagent owns one end of a pipe and the helper process owns the other end
* the helper process calls waitpid to wait for the script to exit. an alarm
* is set for SCRIPT_TIMEOUT seconds. If the alarm fires, SIGTERM is sent to
* the script process and a second alarm is set for SCRIPT_TIMEOUT_GRACE. if
* the second alarm fires, SIGKILL is sent to forcefully kill the script. when
* script exits, the helper process notifies dhcpagent by closing its end
* of the pipe.
*/
unsigned int script_count;
/*
* the signal to send to the script process. it is a global variable
* to this file as sigterm_handler needs it.
*/
static int script_signal = SIGTERM;
/*
* script's absolute timeout value. the first timeout is set to SCRIPT_TIMEOUT
* seconds from the time it is started. SIGTERM is sent on the first timeout
* the second timeout is set to SCRIPT_TIMEOUT_GRACE from the first timeout
* and SIGKILL is sent on the second timeout.
*/
static time_t timeout;
/*
* sigalarm_handler(): signal handler for SIGARLM
*
* input: int: signal the handler was called with
* output: void
*/
/* ARGSUSED */
static void
sigalarm_handler(int sig)
{
time_t now;
/* set a another alarm if it fires too early */
now = time(NULL);
if (now < timeout)
(void) alarm(timeout - now);
}
/*
* sigterm_handler(): signal handler for SIGTERM, fired when dhcpagent wants
* to stop the script
* input: int: signal the handler was called with
* output: void
*/
/* ARGSUSED */
static void
sigterm_handler(int sig)
{
if (script_signal != SIGKILL) {
/* send SIGKILL SCRIPT_TIMEOUT_GRACE seconds from now */
script_signal = SIGKILL;
timeout = time(NULL) + SCRIPT_TIMEOUT_GRACE;
(void) alarm(SCRIPT_TIMEOUT_GRACE);
}
}
/*
* run_script(): it forks a process to execute the script
*
* input: struct ifslist *: the interface
* const char *: the event name
* int: the pipe end owned by the script helper process
* output: void
*/
static void
run_script(struct ifslist *ifsp, const char *event, int fd)
{
int n;
char c;
char *name;
pid_t pid;
time_t now;
extern int errno;
if ((pid = fork()) == -1) {
return;
}
if (pid == 0) {
name = strrchr(SCRIPT_PATH, '/') + 1;
/* close all files */
closefrom(0);
/* redirect stdin, stdout and stderr to /dev/null */
if ((n = open("/dev/null", O_RDWR)) < 0)
exit(-1);
(void) dup2(n, STDOUT_FILENO);
(void) dup2(n, STDERR_FILENO);
(void) execl(SCRIPT_PATH, name, ifsp->if_name, event, NULL);
_exit(127);
}
/*
* the first timeout fires SCRIPT_TIMEOUT seconds from now.
*/
timeout = time(NULL) + SCRIPT_TIMEOUT;
(void) sigset(SIGALRM, sigalarm_handler);
(void) alarm(SCRIPT_TIMEOUT);
/*
* pass script's pid to dhcpagent.
*/
(void) write(fd, &pid, sizeof (pid));
for (;;) {
if (waitpid(pid, NULL, 0) >= 0) {
/* script has exited */
c = SCRIPT_OK;
break;
}
if (errno != EINTR) {
return;
}
now = time(NULL);
if (now >= timeout) {
(void) kill(pid, script_signal);
if (script_signal == SIGKILL) {
c = SCRIPT_KILLED;
break;
}
script_signal = SIGKILL;
timeout = now + SCRIPT_TIMEOUT_GRACE;
(void) alarm(SCRIPT_TIMEOUT_GRACE);
}
}
(void) write(fd, &c, 1);
}
/*
* script_cleanup(): cleanup helper function
*
* input: struct ifslist *: the interface
* output: void
*/
static void
script_cleanup(struct ifslist *ifsp)
{
ifsp->if_script_helper_pid = -1;
ifsp->if_script_pid = -1;
if (ifsp->if_script_fd != -1) {
assert(ifsp->if_script_event_id != -1);
assert(ifsp->if_script_callback != NULL);
(void) iu_unregister_event(eh, ifsp->if_script_event_id, NULL);
(void) close(ifsp->if_script_fd);
ifsp->if_script_event_id = -1;
ifsp->if_script_fd = -1;
ifsp->if_script_callback(ifsp, ifsp->if_callback_msg);
ifsp->if_script_callback = NULL;
ifsp->if_script_event = NULL;
ifsp->if_callback_msg = NULL;
(void) release_ifs(ifsp);
script_count--;
}
}
/*
* script_exit(): does cleanup and invokes callback when script exits
*
* input: eh_t *: unused
* int: the end of pipe owned by dhcpagent
* short: unused
* eh_event_id_t: unused
* void *: the interface
* output: void
*/
/* ARGSUSED */
static void
script_exit(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
{
char c;
if (read(fd, &c, 1) <= 0) {
c = SCRIPT_FAILED;
}
if (c == SCRIPT_OK) {
dhcpmsg(MSG_DEBUG, "script ok");
} else if (c == SCRIPT_KILLED) {
dhcpmsg(MSG_DEBUG, "script killed");
} else {
dhcpmsg(MSG_DEBUG, "script failed");
}
script_cleanup(arg);
}
/*
* script_start(): tries to run the script
*
* input: struct ifslist *: the interface
* const char *: the event name
* script_callback_t: callback function
* void *: data to the callback function
* output: int: 1 if script starts successfully
* int *: the returned value of the callback function if script
* starts unsuccessfully
*/
int
script_start(struct ifslist *ifsp, const char *event,
script_callback_t *callback, const char *msg, int *status)
{
int n;
int fds[2];
pid_t pid;
iu_event_id_t event_id;
assert(callback != NULL);
if (access(SCRIPT_PATH, X_OK) == -1) {
/* script does not exist */
goto out;
}
if (ifsp->if_script_pid != -1) {
/* script is running, stop it */
dhcpmsg(MSG_ERROR, "script_start: stop script");
script_stop(ifsp);
}
/*
* dhcpagent owns one end of the pipe and script helper process
* owns the other end. dhcpagent reads on the pipe; and the helper
* process notifies it when the script exits.
*/
if (pipe(fds) < 0) {
dhcpmsg(MSG_ERROR, "script_start: can't create pipe");
goto out;
}
if ((pid = fork()) < 0) {
dhcpmsg(MSG_ERROR, "script_start: can't fork");
(void) close(fds[0]);
(void) close(fds[1]);
goto out;
}
if (pid == 0) {
/*
* SIGCHLD is ignored in dhcpagent, the helper process
* needs it. it calls waitpid to wait for the script to exit.
*/
(void) close(fds[0]);
(void) sigset(SIGCHLD, SIG_DFL);
(void) sigset(SIGTERM, sigterm_handler);
run_script(ifsp, event, fds[1]);
exit(0);
}
(void) close(fds[1]);
/* get the script's pid */
if (read(fds[0], &ifsp->if_script_pid, sizeof (pid_t)) !=
sizeof (pid_t)) {
(void) kill(pid, SIGKILL);
ifsp->if_script_pid = -1;
(void) close(fds[0]);
goto out;
}
ifsp->if_script_helper_pid = pid;
event_id = iu_register_event(eh, fds[0], POLLIN, script_exit, ifsp);
if (event_id == -1) {
(void) close(fds[0]);
script_stop(ifsp);
goto out;
}
script_count++;
ifsp->if_script_event_id = event_id;
ifsp->if_script_callback = callback;
ifsp->if_script_event = event;
ifsp->if_callback_msg = msg;
ifsp->if_script_fd = fds[0];
hold_ifs(ifsp);
return (1);
out:
/* callback won't be called in script_exit, so call it here */
n = callback(ifsp, msg);
if (status != NULL)
*status = n;
return (0);
}
/*
* script_stop(): stops the script if it is running
*
* input: struct ifslist *: the interface
* output: void
*/
void
script_stop(struct ifslist *ifsp)
{
if (ifsp->if_script_pid != -1) {
assert(ifsp->if_script_helper_pid != -1);
/*
* sends SIGTERM to the script and asks the helper process
* to send SIGKILL if it does not exit after
* SCRIPT_TIMEOUT_GRACE seconds.
*/
(void) kill(ifsp->if_script_pid, SIGTERM);
(void) kill(ifsp->if_script_helper_pid, SIGTERM);
}
script_cleanup(ifsp);
}
|