Browse Source

Replace /proc-based mode by a kill(2) method

develop
Damien Goutte-Gattat 8 years ago
parent
commit
f4b527fe0d
  1. 32
      src/wait4pid.c

32
src/wait4pid.c

@ -180,36 +180,21 @@ wait4all(pid_t *pids, size_t len)
#else /* HAVE_LINUX_NETLINK_H */
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <xmalloc.h>
#include <signal.h>
int
wait4pid(pid_t pid)
{
struct stat st_buf;
char path[20];
snprintf(path, sizeof(path), "/proc/%d", pid);
while ( stat(path, &st_buf) != -1 )
while ( kill(pid, 0) != -1 || errno != ESRCH )
sleep(1);
/* Anything else than ENOENT is an error. */
return errno == ENOENT ? 0 : -1;
return 0;
}
int
wait4all(pid_t *pids, size_t len)
{
int nproc, i;
char *paths;
struct stat st_buf;
paths = xmalloc(20 * len);
for ( i = 0; i < len; i++ )
snprintf(paths + 20 * i, 20, "/proc/%d", pids[i]);
nproc = len;
while ( nproc > 0 ) {
@ -217,12 +202,7 @@ wait4all(pid_t *pids, size_t len)
if ( pids[i] == -1 )
continue;
if ( stat(paths + 20 * i, &st_buf) == -1 ) {
if ( errno != ENOENT ) {
nproc = -1;
break;
}
if ( kill(pids[i], 0) == -1 && errno == ESRCH ) {
pids[i] = -1;
nproc -= 1;
}
@ -231,9 +211,7 @@ wait4all(pid_t *pids, size_t len)
sleep(1);
}
free(paths);
return nproc;
return 0;
}
#endif /* ! HAVE_LINUX_NETLINK_H */
Loading…
Cancel
Save