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
|
/* libnih
*
* child.c - child process termination handling
*
* Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2009 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/list.h>
#include <nih/logging.h>
#include "child.h"
/**
* WAITOPTS:
*
* Options to pass to waitid().
**/
#define WAITOPTS (WEXITED | WSTOPPED | WCONTINUED)
/**
* nih_child_watches:
*
* This is the list of current child watches, not sorted into any
* particular order. Each item is an NihChildWatch structure.
**/
NihList *nih_child_watches = NULL;
/**
* nih_child_init:
*
* Initialise the list of child watches.
**/
void
nih_child_init (void)
{
if (! nih_child_watches)
nih_child_watches = NIH_MUST (nih_list_new (NULL));
}
/**
* nih_child_add_watch:
* @parent: parent object for new watch,
* @pid: process id to watch or -1,
* @events: events to watch for,
* @handler: function to call on @events,
* @data: pointer to pass to @handler.
*
* Adds @handler to the list of functions that should be called by
* nih_child_poll() if any of the events listed in @events occurs to the
* process with id @pid. If @pid is -1 then @handler is called for all
* children.
*
* The watch structure is allocated using nih_alloc() and stored in a linked
* list; there is no non-allocated version because of this and because it
* will be automatically freed once called if @pid is not -1 and the event
* indicates that the process has terminated.
*
* Removal of the watch can be performed by freeing it.
*
* If @parent is not NULL, it should be a pointer to another object which
* will be used as a parent for the returned watch. When all parents
* of the returned watch are freed, the returned watch will also be
* freed.
*
* Returns: the watch information, or NULL if insufficient memory.
**/
NihChildWatch *
nih_child_add_watch (const void *parent,
pid_t pid,
NihChildEvents events,
NihChildHandler handler,
void *data)
{
NihChildWatch *watch;
nih_assert (pid != 0);
nih_assert (handler != NULL);
nih_child_init ();
watch = nih_new (parent, NihChildWatch);
if (! watch)
return NULL;
nih_list_init (&watch->entry);
nih_alloc_set_destructor (watch, nih_list_destroy);
watch->pid = pid;
watch->events = events;
watch->handler = handler;
watch->data = data;
nih_list_add (nih_child_watches, &watch->entry);
return watch;
}
/**
* nih_child_poll:
*
* Repeatedly call waitid() until there are no children waiting to be
* reaped. For each child that an event occurs for, the list of child
* watches is iterated and the handler function for appropriate entries
* is called.
*
* It is safe for the handler to remove itself.
**/
void
nih_child_poll (void)
{
siginfo_t info;
nih_child_init ();
/* NOTE: there's a strange kernel inconsistency, when the waitid()
* syscall is native, it takes special care to zero this struct
* before returning ... but when it's a compat syscall, it
* specifically *doesn't* zero the struct.
*
* So we have to take care to do it ourselves before every call.
*/
memset (&info, 0, sizeof (info));
while (waitid (P_ALL, 0, &info, WAITOPTS | WNOHANG) == 0) {
pid_t pid;
NihChildEvents event;
int status, free_watch = TRUE;
pid = info.si_pid;
if (! pid)
break;
/* Convert siginfo information to handler function arguments;
* in practice this is mostly just copying, with a few bits
* of lore.
*/
switch (info.si_code) {
case CLD_EXITED:
event = NIH_CHILD_EXITED;
status = info.si_status;
break;
case CLD_KILLED:
event = NIH_CHILD_KILLED;
status = info.si_status;
break;
case CLD_DUMPED:
event = NIH_CHILD_DUMPED;
status = info.si_status;
break;
case CLD_TRAPPED:
if (((info.si_status & 0x7f) == SIGTRAP)
&& (info.si_status & ~0x7f)) {
event = NIH_CHILD_PTRACE;
status = info.si_status >> 8;
} else {
event = NIH_CHILD_TRAPPED;
status = info.si_status;
}
free_watch = FALSE;
break;
case CLD_STOPPED:
event = NIH_CHILD_STOPPED;
status = info.si_status;
free_watch = FALSE;
break;
case CLD_CONTINUED:
event = NIH_CHILD_CONTINUED;
status = info.si_status;
free_watch = FALSE;
break;
default:
nih_assert_not_reached ();
}
NIH_LIST_FOREACH_SAFE (nih_child_watches, iter) {
NihChildWatch *watch = (NihChildWatch *)iter;
if ((watch->pid != pid) && (watch->pid != -1))
continue;
if (! (watch->events & event))
continue;
watch->handler (watch->data, pid, event, status);
if (free_watch && (watch->pid != -1))
nih_free (watch);
}
/* For next waitid call */
memset (&info, 0, sizeof (info));
}
}
|