Skip to content

Instantly share code, notes, and snippets.

@stek29
Created March 15, 2018 23:59
Show Gist options
  • Save stek29/4b388febbc53d61bd33f288ef8053675 to your computer and use it in GitHub Desktop.
Save stek29/4b388febbc53d61bd33f288ef8053675 to your computer and use it in GitHub Desktop.
#include <spawn.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[], char *envp[]) {
if (argc < 2) {
printf("usage: %s program [args...]", argv[0]);
return 1;
}
int ret = 0;
pid_t child = fork();
if (child == 0) {
pid_t pid;
posix_spawnattr_t attr;
ret = posix_spawnattr_init(&attr);
if (ret) { perror("can't init spawnattr"); exit(ret); }
ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED|POSIX_SPAWN_SETEXEC);
if (ret) { perror("can't set flags"); exit(ret); }
ret = setsid();
ret = posix_spawnp(&pid, argv[1], NULL, &attr, &argv[1], envp);
// should not be reached
printf("error: ");
exit(ret);
}
if (child != -1) {
printf("spawned: %u\n", child);
} else {
printf("error: %s\n", strerror(errno));
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment