#include #include #include #include /* * Non-standard compliant platforms may need * #include or something similar * in addition to the include files above. */ extern void handler(int sig, siginfo_t *sip, void *context); extern void dosig(void); pid_t cpid; int main() { siginfo_t si; pid_t pid; int ret; dosig(); if ((pid = fork()) < 0) exit(1); cpid = pid; if (pid == 0) { _exit(1234567890); } ret = waitid(P_PID, pid, &si, WEXITED); printf("waitid(): ret: %d si_pid: %ld si_status: %d si_code: %d\n", ret, (long) si.si_pid, si.si_status, si.si_code); if (pid != si.si_pid) printf("waitid(): si_pid in struct siginfo should be %ld but is %ld\n", (long) pid, (long) si.si_pid); if (si.si_status != 1234567890) printf("waitid(): si_status in struct siginfo should be %d (0x%x) but is %d (0x%x)\n", 1234567890, 1234567890, si.si_status, si.si_status); if (si.si_code != CLD_EXITED) printf("waitid(): si_code in struct siginfo should be %d (0x%x) but is %d (0x%x)\n", CLD_EXITED, CLD_EXITED, si.si_code, si.si_code); if (CLD_EXITED != 1) printf("CLD_EXITED is %d on this platform\n", CLD_EXITED); return (0); } /* * Include it here to allow to verify that #include * makes siginfo_t available */ #include void handler(int sig, siginfo_t *sip, void *context) { printf("received SIGCHLD (%d), si_pid: %ld si_status: %d si_code: %d\n", sig, (long) sip->si_pid, sip->si_status, sip->si_code); if (sip->si_pid != cpid) printf("SIGCHLD: si_pid in struct siginfo should be %ld but is %ld\n", (long) cpid, (long) sip->si_pid); if (sip->si_status != 1234567890) printf("SIGCHLD: si_status in struct siginfo should be %d (0x%x) but is %d (0x%x)\n", 1234567890, 1234567890, sip->si_status, sip->si_status); if (sip->si_code != CLD_EXITED) printf("SIGCHLD: si_code in struct siginfo should be %d (0x%x) but is %d (0x%x)\n", CLD_EXITED, CLD_EXITED, sip->si_code, sip->si_code); } void dosig() { struct sigaction sa; sa.sa_sigaction = handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART|SA_SIGINFO; sigaction(SIGCHLD, &sa, NULL); }