#include #include #include #include #include #include extern char **environ; int main() { pid_t pid; char *argv[] = {"sh", "-c", "echo hi", NULL}; posix_spawnattr_t attr; int status; short flags = POSIX_SPAWN_RESETIDS; if ((status = posix_spawnattr_init(&attr)) != 0) { printf("posix_spawnattr_init: %s\n", strerror(status)); return status; } if ((status = posix_spawnattr_setflags(&attr, flags)) != 0) { printf("posix_spawnattr_setflags: %s\n", strerror(status)); return status; } status = posix_spawn(&pid, "/bin/sh", NULL, &attr, argv, environ); if (status == 0) { printf("Child pid: %i\n", pid); do { if (waitpid(pid, &status, 0) != -1) { printf("Child status %d\n", WEXITSTATUS(status)); } else { perror("waitpid"); return 1; } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); } else { printf("posix_spawn: %s\n", strerror(status)); } return status; }