Skip to content

Instantly share code, notes, and snippets.

@metanest
Created December 31, 2016 11:53
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 metanest/66f5ce8bf7ae973108ad0e07c2aa334a to your computer and use it in GitHub Desktop.
Save metanest/66f5ce8bf7ae973108ad0e07c2aa334a to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/procctl.h>
extern int errno;
static void
print_proc_reap_status(pid_t my_pid)
{
struct procctl_reaper_status rst;
int err = procctl(P_PID, my_pid, PROC_REAP_STATUS, &rst);
if (err == 0) {
printf("success\n");
printf("flags: %u\n", rst.rs_flags);
printf(" REAPER_STATUS_OWNED: %s\n",
(rst.rs_flags & REAPER_STATUS_OWNED)
? "true" : "false");
printf(" REAPER_STATUS_REALINIT: %s\n",
(rst.rs_flags & REAPER_STATUS_REALINIT)
? "true" : "false");
printf("children: %u\n", rst.rs_children);
printf("reaper: %d\n", rst.rs_reaper);
printf("pid: %d\n", rst.rs_pid);
} else if (err == -1) {
fprintf(stderr, "error\n");
fprintf(stderr, "%s\n", strerror(errno));
} else {
assert(0);
}
}
int
main(void)
{
pid_t my_pid = getpid();
printf("my pid: %d\n\n", my_pid);
print_proc_reap_status(my_pid);
printf("\n");
/* acquire the reaper status */
int err = procctl(P_PID, my_pid, PROC_REAP_ACQUIRE, 0);
if (err == 0) {
printf("success\n");
} else if (err == -1) {
fprintf(stderr, "error\n");
fprintf(stderr, "%s\n", strerror(errno));
} else {
assert(0);
}
printf("\n");
print_proc_reap_status(my_pid);
printf("\n");
printf("forking...\n");
pid_t pid = fork();
if (pid == -1) {
fprintf(stderr, "fork failed\n");
exit(2);
} else if (pid == 0) {
printf("I am a child, exec-ing xterm\n");
execl("/usr/local/bin/xterm", "/usr/local/bin/xterm", (char *)0);
fprintf(stderr, "exec failed\n");
exit(2);
} else {
printf("I am a my_init, running wait\n");
for (;;) {
int status;
wait(&status);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment