Lines 1-180
Link Here
|
1 |
--- lib/services/services_linux.c.orig 2016-01-14 21:43:08 UTC |
|
|
2 |
+++ lib/services/services_linux.c |
3 |
@@ -457,13 +457,19 @@ action_launch_child(svc_action_t *op) |
4 |
_exit(op->rc); |
5 |
} |
6 |
|
7 |
+#ifndef HAVE_SYS_SIGNALFD_H |
8 |
+static int sigchld_pipe[2]; |
9 |
+ |
10 |
static void |
11 |
-action_synced_wait(svc_action_t * op, sigset_t mask) |
12 |
+sigchld_handler() |
13 |
{ |
14 |
+ write(sigchld_pipe[1], "", 1); |
15 |
+} |
16 |
+#endif |
17 |
|
18 |
-#ifndef HAVE_SYS_SIGNALFD_H |
19 |
- CRM_ASSERT(FALSE); |
20 |
-#else |
21 |
+static void |
22 |
+action_synced_wait(svc_action_t * op, sigset_t *mask) |
23 |
+{ |
24 |
int status = 0; |
25 |
int timeout = op->timeout; |
26 |
int sfd = -1; |
27 |
@@ -471,10 +477,14 @@ action_synced_wait(svc_action_t * op, si |
28 |
struct pollfd fds[3]; |
29 |
int wait_rc = 0; |
30 |
|
31 |
- sfd = signalfd(-1, &mask, SFD_NONBLOCK); |
32 |
+#ifdef HAVE_SYS_SIGNALFD_H |
33 |
+ sfd = signalfd(-1, mask, SFD_NONBLOCK); |
34 |
if (sfd < 0) { |
35 |
crm_perror(LOG_ERR, "signalfd() failed"); |
36 |
} |
37 |
+#else |
38 |
+ sfd = sigchld_pipe[0]; |
39 |
+#endif |
40 |
|
41 |
fds[0].fd = op->opaque->stdout_fd; |
42 |
fds[0].events = POLLIN; |
43 |
@@ -503,6 +513,7 @@ action_synced_wait(svc_action_t * op, si |
44 |
} |
45 |
|
46 |
if (fds[2].revents & POLLIN) { |
47 |
+#ifdef HAVE_SYS_SIGNALFD_H |
48 |
struct signalfd_siginfo fdsi; |
49 |
ssize_t s; |
50 |
|
51 |
@@ -511,6 +522,12 @@ action_synced_wait(svc_action_t * op, si |
52 |
crm_perror(LOG_ERR, "Read from signal fd %d failed", sfd); |
53 |
|
54 |
} else if (fdsi.ssi_signo == SIGCHLD) { |
55 |
+#else |
56 |
+ if (1) { |
57 |
+ /* Clear out the sigchld pipe. */ |
58 |
+ char ch; |
59 |
+ while (read(sfd, &ch, 1) == 1); |
60 |
+#endif |
61 |
wait_rc = waitpid(op->pid, &status, WNOHANG); |
62 |
|
63 |
if (wait_rc < 0){ |
64 |
@@ -583,10 +600,10 @@ action_synced_wait(svc_action_t * op, si |
65 |
|
66 |
close(op->opaque->stdout_fd); |
67 |
close(op->opaque->stderr_fd); |
68 |
- close(sfd); |
69 |
|
70 |
+#ifdef HAVE_SYS_SIGNALFD_H |
71 |
+ close(sfd); |
72 |
#endif |
73 |
- |
74 |
} |
75 |
|
76 |
/* For an asynchronous 'op', returns FALSE if 'op' should be free'd by the caller */ |
77 |
@@ -596,9 +613,30 @@ services_os_action_execute(svc_action_t |
78 |
{ |
79 |
int stdout_fd[2]; |
80 |
int stderr_fd[2]; |
81 |
+ struct stat st; |
82 |
+ sigset_t *pmask; |
83 |
+ |
84 |
+#ifdef HAVE_SYS_SIGNALFD_H |
85 |
sigset_t mask; |
86 |
sigset_t old_mask; |
87 |
- struct stat st; |
88 |
+#define sigchld_cleanup() { \ |
89 |
+ if (sigismember(&old_mask, SIGCHLD) == 0) { \ |
90 |
+ if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) { \ |
91 |
+ crm_perror(LOG_ERR, "sigprocmask() failed to unblock sigchld"); \ |
92 |
+ } \ |
93 |
+ } \ |
94 |
+} |
95 |
+#else |
96 |
+ struct sigaction sa; |
97 |
+ struct sigaction old_sa; |
98 |
+#define sigchld_cleanup() { \ |
99 |
+ if (sigaction(SIGCHLD, &old_sa, NULL) < 0) { \ |
100 |
+ crm_perror(LOG_ERR, "sigaction() failed to remove sigchld handler"); \ |
101 |
+ } \ |
102 |
+ close(sigchld_pipe[0]); \ |
103 |
+ close(sigchld_pipe[1]); \ |
104 |
+} |
105 |
+#endif |
106 |
|
107 |
if (pipe(stdout_fd) < 0) { |
108 |
crm_err("pipe() failed"); |
109 |
@@ -620,13 +658,33 @@ services_os_action_execute(svc_action_t |
110 |
} |
111 |
|
112 |
if (synchronous) { |
113 |
+#ifdef HAVE_SYS_SIGNALFD_H |
114 |
sigemptyset(&mask); |
115 |
sigaddset(&mask, SIGCHLD); |
116 |
sigemptyset(&old_mask); |
117 |
|
118 |
if (sigprocmask(SIG_BLOCK, &mask, &old_mask) < 0) { |
119 |
- crm_perror(LOG_ERR, "sigprocmask() failed"); |
120 |
+ crm_perror(LOG_ERR, "sigprocmask() failed to block sigchld"); |
121 |
+ } |
122 |
+ |
123 |
+ pmask = &mask; |
124 |
+#else |
125 |
+ if(pipe(sigchld_pipe) == -1) { |
126 |
+ crm_perror(LOG_ERR, "pipe() failed"); |
127 |
+ } |
128 |
+ |
129 |
+ set_fd_opts(sigchld_pipe[0], O_NONBLOCK); |
130 |
+ set_fd_opts(sigchld_pipe[1], O_NONBLOCK); |
131 |
+ |
132 |
+ sa.sa_handler = sigchld_handler; |
133 |
+ sa.sa_flags = 0; |
134 |
+ sigemptyset(&sa.sa_mask); |
135 |
+ if (sigaction(SIGCHLD, &sa, &old_sa) < 0) { |
136 |
+ crm_perror(LOG_ERR, "sigaction() failed to set sigchld handler"); |
137 |
} |
138 |
+ |
139 |
+ pmask = NULL; |
140 |
+#endif |
141 |
} |
142 |
|
143 |
op->pid = fork(); |
144 |
@@ -645,6 +703,8 @@ services_os_action_execute(svc_action_t |
145 |
if (!synchronous) { |
146 |
return operation_finalize(op); |
147 |
} |
148 |
+ |
149 |
+ sigchld_cleanup(); |
150 |
return FALSE; |
151 |
} |
152 |
case 0: /* Child */ |
153 |
@@ -663,6 +723,10 @@ services_os_action_execute(svc_action_t |
154 |
close(stderr_fd[1]); |
155 |
} |
156 |
|
157 |
+ if (synchronous) { |
158 |
+ sigchld_cleanup(); |
159 |
+ } |
160 |
+ |
161 |
action_launch_child(op); |
162 |
} |
163 |
|
164 |
@@ -677,14 +741,8 @@ services_os_action_execute(svc_action_t |
165 |
set_fd_opts(op->opaque->stderr_fd, O_NONBLOCK); |
166 |
|
167 |
if (synchronous) { |
168 |
- action_synced_wait(op, mask); |
169 |
- |
170 |
- if (sigismember(&old_mask, SIGCHLD) == 0) { |
171 |
- if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) { |
172 |
- crm_perror(LOG_ERR, "sigprocmask() to unblocked failed"); |
173 |
- } |
174 |
- } |
175 |
- |
176 |
+ action_synced_wait(op, pmask); |
177 |
+ sigchld_cleanup(); |
178 |
} else { |
179 |
|
180 |
crm_trace("Async waiting for %d - %s", op->pid, op->opaque->exec); |