| 1 | #define _GNU_SOURCE |
| 2 | |
| 3 | #include <pthread.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <sys/syscall.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/wait.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | static const char *pid_path; |
| 12 | |
| 13 | static void *spawn_child(void *unused) { |
| 14 | (void)unused; |
| 15 | pid_t child = fork(); |
| 16 | if (child == 0) { |
| 17 | execlp("sleep", "sleep", "120", NULL); |
| 18 | _exit(127); |
| 19 | } |
| 20 | if (child < 0) return NULL; |
| 21 | FILE *file = fopen(pid_path, "w"); |
| 22 | if (file != NULL) { |
| 23 | fprintf(file, "%ld %d\n", syscall(SYS_gettid), child); |
| 24 | fclose(file); |
| 25 | } |
| 26 | waitpid(child, NULL, 0); |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | int main(int argc, char **argv) { |
| 31 | if (argc != 2) return 2; |
| 32 | pid_path = argv[1]; |
| 33 | pthread_t worker; |
| 34 | if (pthread_create(&worker, NULL, spawn_child, NULL) != 0) return 3; |
| 35 | if (pthread_join(worker, NULL) != 0) return 4; |
| 36 | return 0; |
| 37 | } |