Page MenuHomeFreeBSD

posix_spawn procdesc test program

Authored By
kib
Sun, Jan 25, 7:18 PM
Size
1 KB
Referenced Files
None
Subscribers
None

posix_spawn procdesc test program

/* $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 <stdio.h>
#include <spawn.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_setpdrfork_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;
}
}

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
28042784
Default Alt Text
posix_spawn procdesc test program (1 KB)

Event Timeline