Codebase list mdadm / 9fe3204
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
2 changed file(s) with 45 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
3131 #include <sys/un.h>
3232 #include <sys/mman.h>
3333 #include <sys/syscall.h>
34 #include <sys/wait.h>
3435 #include <stdio.h>
3536 #include <errno.h>
3637 #include <string.h>
174175 struct supertype *container;
175176 sigset_t set;
176177 struct sigaction act;
178 int pfd[2];
179 int status;
177180
178181 if (argc != 2) {
179182 fprintf(stderr, "Usage: md-manage /device/name/for/container\n");
191194 exit(1);
192195 }
193196
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 }
194215 /* hopefully it is a container - we'll check later */
195216
196217 container = malloc(sizeof(*container));
267288 exit(3);
268289 }
269290
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
270308 mlockall(MCL_FUTURE);
271309
272310 /* SIGUSR is sent between parent and child. So both block it
3030 #include "md_p.h"
3131 #include <sys/socket.h>
3232 #include <sys/utsname.h>
33 #include <sys/wait.h>
3334 #include <sys/un.h>
3435 #include <ctype.h>
3536 #include <dirent.h>
10771078 {
10781079 int i;
10791080 int len;
1081 pid_t pid;
1082 int status;
10801083 char pathbuf[1024];
10811084 char *paths[4] = {
10821085 pathbuf,
11161119 case -1: fprintf(stderr, Name ": cannot run mdmon. "
11171120 "Array remains readonly\n");
11181121 return -1;
1119 default: ; /* parent - good */
1122 default: /* parent - good */
1123 pid = wait(&status);
1124 if (pid < 0 || status != 0)
1125 return -1;
11201126 }
11211127 return 0;
11221128 }