1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#undef waitpid
#undef pipe
#undef wait
static int sigpid;
static void
sigenable(int sig, int enabled)
{
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, sig);
sigprocmask(enabled ? SIG_UNBLOCK : SIG_BLOCK, &mask, 0);
}
static void
child(void)
{
int status, pid;
printf("wait %d in %d\n", sigpid, getpid());
pid = waitpid(sigpid, &status, __WALL);
if(pid < 0)
perror("wait");
else if(WIFEXITED(status))
_exit(WEXITSTATUS(status));
printf("pid %d if %d %d\n", pid, WIFEXITED(status), WEXITSTATUS(status));
_exit(97);
}
static void
sigpass(int sig)
{
if(sig == SIGCHLD){
print("sig\n");
child();
}else
kill(sigpid, sig);
}
void
_threadsetupdaemonize(void)
{
int i, n, pid;
int p[2];
char buf[20];
sigpid = 1;
if(pipe(p) < 0)
abort();
signal(SIGCHLD, sigpass);
switch(pid = fork()){
case -1:
abort();
default:
close(p[1]);
break;
case 0:
close(p[0]);
return;
}
sigpid = pid;
read(p[0], buf, sizeof buf-1);
print("pipe\n");
child();
}
void*
sleeper(void *v)
{
pthread_mutex_t m;
pthread_cond_t c;
pthread_mutex_init(&m, 0);
pthread_cond_init(&c, 0);
pthread_cond_wait(&c, &m);
return 0;
}
void
main(int argc, char **argv)
{
pthread_t pid;
_threadsetupdaemonize();
pthread_create(&pid, 0, sleeper, 0);
exit(1);
}
|