0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <sys/types.h>
0018 #include <dirent.h>
0019 #include <signal.h>
0020 #include <stdio.h>
0021 #include <stdlib.h>
0022 #include <string.h>
0023 #include <unistd.h>
0024 #include <pthread.h>
0025
0026 static pid_t pid = -1;
0027
0028 static void atexit_hook(void)
0029 {
0030 if (pid > 0) {
0031 kill(pid, SIGKILL);
0032 }
0033 }
0034
0035 static void *f(void *_)
0036 {
0037 return NULL;
0038 }
0039
0040 static void sigalrm(int _)
0041 {
0042 exit(0);
0043 }
0044
0045 int main(void)
0046 {
0047 pid = fork();
0048 if (pid == 0) {
0049
0050 while (1) {
0051 pthread_t pth;
0052 pthread_create(&pth, NULL, f, NULL);
0053 pthread_join(pth, NULL);
0054 }
0055 } else if (pid > 0) {
0056
0057 atexit(atexit_hook);
0058
0059 char buf[64];
0060 snprintf(buf, sizeof(buf), "/proc/%u/task", pid);
0061
0062 signal(SIGALRM, sigalrm);
0063 alarm(1);
0064
0065 while (1) {
0066 DIR *d = opendir(buf);
0067 struct dirent *de;
0068 while ((de = readdir(d))) {
0069 if (strcmp(de->d_name, "0") == 0) {
0070 exit(1);
0071 }
0072 }
0073 closedir(d);
0074 }
0075
0076 return 0;
0077 } else {
0078 perror("fork");
0079 return 1;
0080 }
0081 }