mdmon: fork and run as a daemon.
start_mdmon now waits for mdmon to complete initialisation and,
importantly, listen on the socket, before continuing.
Signed-off-by: Neil Brown <neilb@suse.de>
NeilBrown
15 years ago
31 | 31 |
#include <sys/un.h>
|
32 | 32 |
#include <sys/mman.h>
|
33 | 33 |
#include <sys/syscall.h>
|
|
34 |
#include <sys/wait.h>
|
34 | 35 |
#include <stdio.h>
|
35 | 36 |
#include <errno.h>
|
36 | 37 |
#include <string.h>
|
|
174 | 175 |
struct supertype *container;
|
175 | 176 |
sigset_t set;
|
176 | 177 |
struct sigaction act;
|
|
178 |
int pfd[2];
|
|
179 |
int status;
|
177 | 180 |
|
178 | 181 |
if (argc != 2) {
|
179 | 182 |
fprintf(stderr, "Usage: md-manage /device/name/for/container\n");
|
|
191 | 194 |
exit(1);
|
192 | 195 |
}
|
193 | 196 |
|
|
197 |
/* Fork, and have the child tell us when they are ready */
|
|
198 |
pipe(pfd);
|
|
199 |
switch(fork()){
|
|
200 |
case -1:
|
|
201 |
fprintf(stderr, "mdmon: failed to fork: %s\n",
|
|
202 |
strerror(errno));
|
|
203 |
exit(1);
|
|
204 |
case 0: /* child */
|
|
205 |
close(pfd[0]);
|
|
206 |
break;
|
|
207 |
default: /* parent */
|
|
208 |
close(pfd[1]);
|
|
209 |
if (read(pfd[0], &status, sizeof(status)) != sizeof(status)) {
|
|
210 |
wait(&status);
|
|
211 |
status = WEXITSTATUS(status);
|
|
212 |
}
|
|
213 |
exit(status);
|
|
214 |
}
|
194 | 215 |
/* hopefully it is a container - we'll check later */
|
195 | 216 |
|
196 | 217 |
container = malloc(sizeof(*container));
|
|
267 | 288 |
exit(3);
|
268 | 289 |
}
|
269 | 290 |
|
|
291 |
/* Ok, this is close enough. We can say goodbye to our parent now.
|
|
292 |
*/
|
|
293 |
status = 0;
|
|
294 |
write(pfd[1], &status, sizeof(status));
|
|
295 |
close(pfd[1]);
|
|
296 |
|
|
297 |
chdir("/");
|
|
298 |
setsid();
|
|
299 |
close(0);
|
|
300 |
open("/dev/null", O_RDWR);
|
|
301 |
close(1);
|
|
302 |
dup(0);
|
|
303 |
#ifndef DEBUG
|
|
304 |
close(2);
|
|
305 |
dup(0);
|
|
306 |
#endif
|
|
307 |
|
270 | 308 |
mlockall(MCL_FUTURE);
|
271 | 309 |
|
272 | 310 |
/* SIGUSR is sent between parent and child. So both block it
|