Skip to content

Instantly share code, notes, and snippets.

@mqudsi
Last active March 5, 2018 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mqudsi/2409301191cec1b717ae673b7f1ea7f1 to your computer and use it in GitHub Desktop.
Save mqudsi/2409301191cec1b717ae673b7f1ea7f1 to your computer and use it in GitHub Desktop.
Test for correct posix_spawn behavior when pgrp = 0
#include <errno.h>
#include <signal.h>
#include <spawn.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define nullptr 0
int main(int argc, const char *argv[]) {
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
uint8_t flags = 0;
flags |= POSIX_SPAWN_SETPGROUP;
posix_spawnattr_setpgroup(&attr, 0);
posix_spawnattr_setflags(&attr, flags);
posix_spawn_file_actions_t actions;
posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_adddup2(&actions, STDIN_FILENO, STDIN_FILENO);
posix_spawn_file_actions_adddup2(&actions, STDOUT_FILENO, STDOUT_FILENO);
posix_spawn_file_actions_adddup2(&actions, STDERR_FILENO, STDERR_FILENO);
const char * new_argv[] = {
"/bin/cat",
nullptr
};
const char * envp[] = {
nullptr
};
pid_t child_pid = 0;
int result = posix_spawn(&child_pid, "/bin/cat", &actions, &attr, (char *const *) new_argv, (char *const *) envp);
if (result != 0) {
fprintf(stderr, "posix_spawn(\"/bin/cat\") failed! errno: %d\n", errno);
return errno;
}
pid_t child_pgrp = getpgid(child_pid);
pid_t own_pid = getpid();
pid_t own_pgrp = getpgrp();
printf("own pid: %d\n", own_pid);
printf("own pgid: %d\n\n", own_pgrp);
printf("child pid: %d\n", child_pid);
printf("child pgid: %d\n\n", child_pgrp);
kill(child_pid, SIGKILL);
if (child_pgrp != child_pid) {
fprintf(stderr, "Test failed: child_pgid != child_pid though `posix_spawnattr_setpgroup(&attr, 0)` was specified!\n");
return -1;
}
printf("Test passed.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment