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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <sys/wait.h>
#include "triton.h"
#include "spinlock.h"
#include "log.h"
#include "sigchld.h"
static LIST_HEAD(handlers);
static int refs;
static int sleeping = 1;
static pthread_mutex_t handlers_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t refs_lock = PTHREAD_MUTEX_INITIALIZER;
static struct triton_context_t sigchld_ctx;
static void sigchld_handler(void *arg)
{
struct sigchld_handler_t *h, *h0;
pid_t pid;
int status;
while (1) {
pid = waitpid(0, &status, WNOHANG);
pthread_mutex_lock(&handlers_lock);
if (pid == 0 || (pid == -1 && errno == ECHILD)) {
sleeping = 1;
pthread_mutex_unlock(&handlers_lock);
return;
} else if (pid < 0) {
pthread_mutex_unlock(&handlers_lock);
log_error("sigchld: waitpid: %s\n", strerror(errno));
return;
}
h0 = NULL;
list_for_each_entry(h, &handlers, entry) {
if (h->pid == pid) {
h0 = h;
pthread_mutex_lock(&h0->lock);
break;
}
}
pthread_mutex_unlock(&handlers_lock);
if (h0) {
h0->handler(h0, WEXITSTATUS(status));
list_del(&h0->entry);
h0->pid = 0;
pthread_mutex_unlock(&h0->lock);
}
}
}
void __export sigchld_register_handler(struct sigchld_handler_t *h)
{
pthread_mutex_init(&h->lock, NULL);
pthread_mutex_lock(&handlers_lock);
list_add_tail(&h->entry, &handlers);
pthread_mutex_unlock(&handlers_lock);
}
void __export sigchld_unregister_handler(struct sigchld_handler_t *h)
{
pthread_mutex_lock(&handlers_lock);
pthread_mutex_lock(&h->lock);
if (h->pid) {
list_del(&h->entry);
h->pid = 0;
}
pthread_mutex_unlock(&h->lock);
pthread_mutex_unlock(&handlers_lock);
}
void __export sigchld_lock()
{
sigset_t set;
pthread_mutex_lock(&refs_lock);
if (refs == 0) {
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, NULL);
}
++refs;
pthread_mutex_unlock(&refs_lock);
}
void __export sigchld_unlock()
{
sigset_t set;
pthread_mutex_lock(&refs_lock);
if (refs == 1) {
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_UNBLOCK, &set, NULL);
}
--refs;
pthread_mutex_unlock(&refs_lock);
}
static void sigchld(int num)
{
int s;
pthread_mutex_lock(&handlers_lock);
s = sleeping;
sleeping = 0;
pthread_mutex_unlock(&handlers_lock);
if (s)
triton_context_call(&sigchld_ctx, sigchld_handler, NULL);
}
static void __init init(void)
{
struct sigaction sa_sigchld = {
.sa_handler = sigchld,
.sa_flags = SA_NOCLDSTOP,
};
if (sigaction(SIGCHLD, &sa_sigchld, NULL)) {
fprintf(stderr, "sigchld: sigaction: %s\n", strerror(errno));
return;
}
triton_context_register(&sigchld_ctx, NULL);
}
|