xref: /freebsd/tests/sys/capsicum/waittest.c (revision 670b568ec1c36464c6d55e400382c290b0391ccf)
1*670b568eSEd Maste #include <errno.h>
2*670b568eSEd Maste #include <string.h>
3*670b568eSEd Maste #include <stdlib.h>
4*670b568eSEd Maste #include <stdio.h>
5*670b568eSEd Maste #include <unistd.h>
6*670b568eSEd Maste #include <sys/types.h>
7*670b568eSEd Maste #include <sys/wait.h>
8*670b568eSEd Maste 
9*670b568eSEd Maste #ifdef __FreeBSD__
10*670b568eSEd Maste #include <sys/procdesc.h>
11*670b568eSEd Maste #endif
12*670b568eSEd Maste 
13*670b568eSEd Maste #ifdef __linux__
14*670b568eSEd Maste #include <sys/syscall.h>
pdfork(int * fd,int flags)15*670b568eSEd Maste int pdfork(int *fd, int flags) {
16*670b568eSEd Maste   return syscall(__NR_pdfork, fd, flags);
17*670b568eSEd Maste }
18*670b568eSEd Maste #endif
19*670b568eSEd Maste 
main()20*670b568eSEd Maste int main() {
21*670b568eSEd Maste   int procfd;
22*670b568eSEd Maste   int rc =  pdfork(&procfd, 0);
23*670b568eSEd Maste   if (rc < 0) {
24*670b568eSEd Maste     fprintf(stderr, "pdfork() failed rc=%d errno=%d %s\n", rc, errno, strerror(errno));
25*670b568eSEd Maste     exit(1);
26*670b568eSEd Maste   }
27*670b568eSEd Maste   if (rc == 0) { // Child process
28*670b568eSEd Maste     sleep(1);
29*670b568eSEd Maste     exit(123);
30*670b568eSEd Maste   }
31*670b568eSEd Maste   fprintf(stderr, "pdfork()ed child pid=%ld procfd=%d\n", (long)rc, procfd);
32*670b568eSEd Maste   sleep(2);  // Allow child to complete
33*670b568eSEd Maste   pid_t child = waitpid(-1, &rc, WNOHANG);
34*670b568eSEd Maste   if (child == 0) {
35*670b568eSEd Maste     fprintf(stderr, "waitpid(): no completed child found\n");
36*670b568eSEd Maste   } else if (child < 0) {
37*670b568eSEd Maste     fprintf(stderr, "waitpid(): failed errno=%d %s\n", errno, strerror(errno));
38*670b568eSEd Maste   } else {
39*670b568eSEd Maste     fprintf(stderr, "waitpid(): found completed child %ld\n", (long)child);
40*670b568eSEd Maste   }
41*670b568eSEd Maste   return 0;
42*670b568eSEd Maste }
43