Page MenuHomeFreeBSD
Paste P686

posix_spawn procdesc test program
ActivePublic

Authored by kib on Sun, Jan 25, 7:18 PM.
Tags
None
Referenced Files
F143085728: posix_spawn procdesc test program
Sun, Jan 25, 7:53 PM
F143085686: posix_spawn procdesc test program
Sun, Jan 25, 7:52 PM
F143083806: posix_spawn procdesc test program
Sun, Jan 25, 7:18 PM
Subscribers
None
/* $Id: posix_spawn-execfd.c,v 1.6 2026/01/25 19:12:37 kostik Exp kostik $ */
#include <sys/types.h>
#include <sys/procdesc.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
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;
}
}

Event Timeline

kib edited the content of this paste. (Show Details)