/* $Id: posix_spawn-execfd.c,v 1.6 2026/01/25 19:12:37 kostik Exp kostik $ */ #include #include #include #include #include #include #include #include #include #include #include extern char **environ; int main(int argc, char *argv[]) { posix_spawnattr_t attr; struct __wrusage wru; siginfo_t si; int execfd, error, procfd, res, status; pid_t pid; argc--; argv++; if (argc == 0) exit(1); execfd = open(argv[0], O_EXEC); if (execfd == -1) err(1, "open %s", argv[0]); posix_spawnattr_init(&attr); posix_spawnattr_setexecfd_np(&attr, execfd); posix_spawnattr_setprocdescp_np(&attr, &procfd, 0); error = posix_spawn(&pid, NULL, NULL, &attr, argv, environ); if (error != 0) errc(1, error, "posix_spawn"); for (;;) { res = pdwait(procfd, &status, WEXITED, &wru, &si); if (res == EINTR) continue; if (res == -1) err(1, "waitpid %d", pid); if (WIFEXITED(status)) { printf("Child %d exited, status %d\n", pid, WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("Child %d terminated by signal %d\n", pid, WTERMSIG(status)); } else { printf("Child %d status %#x\n", pid, status); } break; } }