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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This file contains a set of routines used to perform wait based method
* reaping.
*/
#include <wait.h>
#include <sys/param.h>
#include <fcntl.h>
#include <libcontract.h>
#include <errno.h>
#include <libintl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include "inetd_impl.h"
/* inetd's open file limit, set in method_init() */
#define INETD_NOFILE_LIMIT RLIM_INFINITY
/* structure used to represent an active method process */
typedef struct {
int fd; /* fd of process's /proc psinfo file */
/* associated contract id if known, else -1 */
ctid_t cid;
pid_t pid;
instance_t *inst; /* pointer to associated instance */
instance_method_t method; /* the method type running */
/* associated endpoint protocol name if known, else NULL */
char *proto_name;
uu_list_node_t link;
} method_el_t;
static void unregister_method(method_el_t *);
/* list of currently executing method processes */
static uu_list_pool_t *method_pool = NULL;
static uu_list_t *method_list = NULL;
/*
* File limit saved during initialization before modification, so that it can
* be reverted back to for inetd's exec'd methods.
*/
static struct rlimit saved_file_limit;
/*
* Setup structures used for method termination monitoring.
* Returns -1 if an allocation failure occurred, else 0.
*/
int
method_init(void)
{
struct rlimit rl;
/*
* Save aside the old file limit and impose one large enough to support
* all the /proc file handles we could have open.
*/
(void) getrlimit(RLIMIT_NOFILE, &saved_file_limit);
rl.rlim_cur = rl.rlim_max = INETD_NOFILE_LIMIT;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
error_msg("Failed to set file limit: %s", strerror(errno));
return (-1);
}
if ((method_pool = uu_list_pool_create("method_pool",
sizeof (method_el_t), offsetof(method_el_t, link), NULL,
UU_LIST_POOL_DEBUG)) == NULL) {
error_msg("%s: %s", gettext("Failed to create method pool"),
uu_strerror(uu_error()));
return (-1);
}
if ((method_list = uu_list_create(method_pool, NULL, 0)) == NULL) {
error_msg("%s: %s",
gettext("Failed to create method list"),
uu_strerror(uu_error()));
/* let method_fini() clean-up */
return (-1);
}
return (0);
}
/*
* Tear-down structures created in method_init().
*/
void
method_fini(void)
{
if (method_list != NULL) {
method_el_t *me;
while ((me = uu_list_first(method_list)) != NULL)
unregister_method(me);
(void) uu_list_destroy(method_list);
method_list = NULL;
}
if (method_pool != NULL) {
(void) uu_list_pool_destroy(method_pool);
method_pool = NULL;
}
/* revert file limit */
method_preexec();
}
/*
* Revert file limit back to pre-initialization one. This shouldn't fail as
* long as its called *after* descriptor cleanup.
*/
void
method_preexec(void)
{
(void) setrlimit(RLIMIT_NOFILE, &saved_file_limit);
}
/*
* Callback function that handles the timeout of an instance's method.
* 'arg' points at the method_el_t representing the method.
*/
/* ARGSUSED0 */
static void
method_timeout(iu_tq_t *tq, void *arg)
{
method_el_t *mp = arg;
error_msg(gettext("The %s method of instance %s timed-out"),
methods[mp->method].name, mp->inst->fmri);
mp->inst->timer_id = -1;
if (mp->method == IM_START) {
process_start_term(mp->inst, mp->proto_name);
} else {
process_non_start_term(mp->inst, IMRET_FAILURE);
}
unregister_method(mp);
}
/*
* Registers the attributes of a running method passed as arguments so that
* the method's termination is noticed and any further processing of the
* associated instance is carried out. The function also sets up any
* necessary timers so we can detect hung methods.
* Returns -1 if either it failed to open the /proc psinfo file which is used
* to monitor the method process, it failed to setup a required timer or
* memory allocation failed; else 0.
*/
int
register_method(instance_t *ins, pid_t pid, ctid_t cid, instance_method_t mthd,
char *proto_name)
{
char path[MAXPATHLEN];
int fd;
method_el_t *me;
/* open /proc psinfo file of process to listen for POLLHUP events on */
(void) snprintf(path, sizeof (path), "/proc/%u/psinfo", pid);
for (;;) {
if ((fd = open(path, O_RDONLY)) >= 0) {
break;
} else if (errno != EINTR) {
/*
* Don't output an error for ENOENT; we get this
* if a method has gone away whilst we were stopped,
* and we're now trying to re-listen for it.
*/
if (errno != ENOENT) {
error_msg(gettext("Failed to open %s: %s"),
path, strerror(errno));
}
return (-1);
}
}
/* add method record to in-memory list */
if ((me = calloc(1, sizeof (method_el_t))) == NULL) {
error_msg(strerror(errno));
(void) close(fd);
return (-1);
}
me->fd = fd;
me->inst = (instance_t *)ins;
me->method = mthd;
me->pid = pid;
me->cid = cid;
if (proto_name != NULL) {
if ((me->proto_name = strdup(proto_name)) == NULL) {
error_msg(strerror(errno));
free(me);
(void) close(fd);
return (-1);
}
} else
me->proto_name = NULL;
/* register a timeout for the method, if required */
if (mthd != IM_START) {
method_info_t *mi = ins->config->methods[mthd];
if (mi->timeout > 0) {
assert(ins->timer_id == -1);
ins->timer_id = iu_schedule_timer(timer_queue,
mi->timeout, method_timeout, me);
if (ins->timer_id == -1) {
error_msg(gettext(
"Failed to schedule method timeout"));
if (me->proto_name != NULL)
free(me->proto_name);
free(me);
(void) close(fd);
return (-1);
}
}
}
/*
* Add fd of psinfo file to poll set, but pass 0 for events to
* poll for, so we should only get a POLLHUP event on the fd.
*/
if (set_pollfd(fd, 0) == -1) {
cancel_inst_timer(ins);
if (me->proto_name != NULL)
free(me->proto_name);
free(me);
(void) close(fd);
return (-1);
}
uu_list_node_init(me, &me->link, method_pool);
(void) uu_list_insert_after(method_list, NULL, me);
return (0);
}
/*
* A counterpart to register_method(), this function stops the monitoring of a
* method process for its termination.
*/
static void
unregister_method(method_el_t *me)
{
/* cancel any timer associated with the method */
if (me->inst->timer_id != -1)
cancel_inst_timer(me->inst);
/* stop polling on the psinfo file fd */
clear_pollfd(me->fd);
(void) close(me->fd);
/* remove method record from list */
uu_list_remove(method_list, me);
if (me->proto_name != NULL)
free(me->proto_name);
free(me);
}
/*
* Unregister all methods associated with instance 'inst'.
*/
void
unregister_instance_methods(const instance_t *inst)
{
method_el_t *me = uu_list_first(method_list);
while (me != NULL) {
if (me->inst == inst) {
method_el_t *tmp = me;
me = uu_list_next(method_list, me);
unregister_method(tmp);
} else {
me = uu_list_next(method_list, me);
}
}
}
/*
* Process any terminated methods. For each method determined to have
* terminated, the function determines its return value and calls the
* appropriate handling function, depending on the type of the method.
*/
void
process_terminated_methods(void)
{
method_el_t *me = uu_list_first(method_list);
while (me != NULL) {
struct pollfd *pfd;
pid_t pid;
int status;
int ret;
method_el_t *tmp;
pfd = find_pollfd(me->fd);
/*
* We expect to get a POLLHUP back on the fd of the process's
* open psinfo file from /proc when the method terminates.
* A POLLERR could(?) mask a POLLHUP, so handle this
* also.
*/
if ((pfd->revents & (POLLHUP|POLLERR)) == 0) {
me = uu_list_next(method_list, me);
continue;
}
/* get the method's exit code (no need to loop for EINTR) */
pid = waitpid(me->pid, &status, WNOHANG);
switch (pid) {
case 0: /* child still around */
/*
* Either poll() is sending us invalid POLLHUP events
* or is flagging a POLLERR on the fd. Neither should
* happen, but in the event they do, ignore this fd
* this time around and wait out the termination
* of its associated method. This may result in
* inetd swiftly looping in event_loop(), but means
* we don't miss the termination of a method.
*/
me = uu_list_next(method_list, me);
continue;
case -1: /* non-existent child */
assert(errno == ECHILD);
/*
* the method must not be owned by inetd due to it
* persisting over an inetd restart. Let's assume the
* best, that it was successful.
*/
ret = IMRET_SUCCESS;
break;
default: /* child terminated */
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
debug_msg("process %ld of instance %s returned "
"%d", pid, me->inst->fmri, ret);
} else if (WIFSIGNALED(status)) {
/*
* Terminated by signal. This may be due
* to a kill that we sent from a disable or
* offline event. We flag it as a failure, but
* this flagged failure will only be processed
* in the case of non-start methods, or when
* the instance is still enabled.
*/
debug_msg("process %ld of instance %s exited "
"due to signal %d", pid, me->inst->fmri,
WTERMSIG(status));
ret = IMRET_FAILURE;
} else {
/*
* Can we actually get here? Don't think so.
* Treat it as a failure, anyway.
*/
debug_msg("waitpid() for %s method of "
"instance %s returned %d",
methods[me->method].name, me->inst->fmri,
status);
ret = IMRET_FAILURE;
}
}
remove_method_ids(me->inst, me->pid, me->cid, me->method);
/* continue state transition processing of the instance */
if (me->method != IM_START) {
process_non_start_term(me->inst, ret);
} else {
process_start_term(me->inst, me->proto_name);
}
if (me->cid != -1)
(void) abandon_contract(me->cid);
tmp = me;
me = uu_list_next(method_list, me);
unregister_method(tmp);
}
}
|