summaryrefslogtreecommitdiff
path: root/src/sys/wait/waitpid.c
blob: c3892ff01c0a9f7652eb329d481a0fae37d7f150 (plain)
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
#include "sys/types.h"
#include <sys/wait.h>
#include "signal.h"
#include "unistd.h"
#include "_syscall.h"

#ifndef P_PID
#include "signal/union_sigval.c"
#include "signal/siginfo_t.c"
#include "idtype_t.c"
#include "P_PID.c"
#include "P_PGID.c"
#include "P_ALL.c"
#define getpgid(_pid) __syscall(__syscall_lookup(getpgid), _pid, 0, 0, 0, 0, 0)
#define waitid(_type, _id, _si, _opt) __syscall(__syscall_lookup(waitid), _type, _id, _si, _opt, 0, 0)
#endif

pid_t waitpid(pid_t pid, int *stat_loc, int options)
{
	/* TODO: handle WUNTRACED, is not recognized by waitid() */

	siginfo_t si = { 0 };
	idtype_t idt = P_PID;
	int ret = -1;

	if (pid == (pid_t)-1) {
		idt = P_ALL;
	} else if (pid == 0) {
		idt = P_PGID;
		pid = getpgid(0);
	} else if (pid < (pid_t)-1) {
		idt = P_PGID;
		pid = -pid;
	}

	ret = waitid(idt, pid, &si, options);
	if (stat_loc) {
		*stat_loc = si.si_status;
	}
	return ret;
}

/*
POSIX(1)
*/