Browse Source

Add wait4all function (Netlink mode)

develop
Damien Goutte-Gattat 8 years ago
parent
commit
d186fc4857
  1. 42
      src/wait4pid.c
  2. 5
      src/wait4pid.h

42
src/wait4pid.c

@ -129,6 +129,48 @@ wait4pid(pid_t pid)
return ec;
}
int
wait4all(pid_t *pids, size_t len)
{
int ec, nproc;
struct __attribute__ ((__packed__)) payload_t {
struct cn_msg msg;
struct proc_event evt;
} *payload;
char buf[NLMSG_SPACE(sizeof(struct payload_t))];
if ( ! listener_set && wait4pid_init() == -1 )
return -1;
nproc = len;
while ( nproc > 0 ) {
int ret, i;
if ( (ret = recv(sock, buf, sizeof(buf), 0)) <= 0 ) {
if ( ret == -1 && errno == EINTR )
continue;
else
return -1;
}
payload = (struct payload_t *) NLMSG_DATA(buf);
if ( payload->evt.what != PROC_EVENT_EXIT )
continue;
for ( i = 0; i < len; i++ ) {
if ( pids[i] != -1
&& pids[i] == payload->evt.event_data.exit.process_pid ) {
pids[i] = -1;
nproc -= 1;
if ( i == len - 1 )
ec = payload->evt.event_data.exit.exit_code / 256;
}
}
}
return ec;
}
#else /* HAVE_LINUX_NETLINK_H */
#include <string.h>

5
src/wait4pid.h

@ -19,6 +19,8 @@
#ifndef ICP20130319_WAIT4PID_H
#define ICP20130319_WAIT4PID_H
#include <stdlib.h>
#ifdef __cpluscplus
extern "C" {
#endif
@ -26,6 +28,9 @@ extern "C" {
int
wait4pid(pid_t);
int
wait4all(pid_t *, size_t);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save