From fa716c83c5be8093e663e260e46e73ea9ad6facf Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Mon, 8 Feb 2010 14:30:46 +1100 Subject: [PATCH] mdmon: insist on creating .pid file at startup. Now that we don't "mdadm --takeover" until /var/run is writable there is no need to continually try to create files in there. So only create these files at startup and fail if they cannot be made. This means that to start an array with externally managed metadata, either /var/run or ALT_RUN (e.g. /lib/init/rw) must be writable. To 'takeover' from a previous mdmon instance, /var/run must be writable. This means we don't need to worry about SIGHUP (which was once used to tell us it was time to create .pid) and SIGALRM. Signed-off-by: NeilBrown --- managemon.c | 26 -------------------------- mdmon.c | 44 +++++++++++++++++++++++++++++--------------- mdmon.h | 3 --- monitor.c | 6 +++++- 4 files changed, 34 insertions(+), 45 deletions(-) diff --git a/managemon.c b/managemon.c index e01d13b9..037406f0 100644 --- a/managemon.c +++ b/managemon.c @@ -680,8 +680,6 @@ void do_manager(struct supertype *container) sigprocmask(SIG_UNBLOCK, NULL, &set); sigdelset(&set, SIGUSR1); - sigdelset(&set, SIGHUP); - sigdelset(&set, SIGALRM); sigdelset(&set, SIGTERM); do { @@ -700,30 +698,6 @@ void do_manager(struct supertype *container) read_sock(container); - if (socket_hup_requested) { - /* Try to create pid file and socket in - * main or alternate RUN directory. - */ - char *dir = VAR_RUN; - if (mkdir(dir, 0600) < 0 && errno != EEXIST) { - char *dir = ALT_RUN; - if (mkdir(dir, 0600) < 0 && errno != EEXIST) - dir = NULL; - } - if (dir && !sigterm && - (container->sock < 0 || - strcmp(dir, pid_dir) != 0)) { - close(container->sock); - remove_pidfile(container->devname); - pid_dir = dir; - container->sock = make_control_sock(container->devname); - make_pidfile(container->devname); - } - socket_hup_requested = 0; - } - if (container->sock < 0) - alarm(30); - free_mdstat(mdstat); } remove_old(); diff --git a/mdmon.c b/mdmon.c index c590fb20..31d45fd5 100644 --- a/mdmon.c +++ b/mdmon.c @@ -113,7 +113,7 @@ static struct superswitch *find_metadata_methods(char *vers) return NULL; } -int make_pidfile(char *devname) +static int make_pidfile(char *devname) { char path[100]; char pid[10]; @@ -192,7 +192,7 @@ void remove_pidfile(char *devname) rmdir(pid_dir); } -int make_control_sock(char *devname) +static int make_control_sock(char *devname) { char path[100]; int sfd; @@ -221,12 +221,6 @@ int make_control_sock(char *devname) return sfd; } -int socket_hup_requested; -static void hup(int sig) -{ - socket_hup_requested = 1; -} - static void term(int sig) { sigterm = 1; @@ -431,24 +425,25 @@ static int mdmon(char *devname, int devnum, int must_fork, int takeover) */ sigemptyset(&set); sigaddset(&set, SIGUSR1); - sigaddset(&set, SIGHUP); - sigaddset(&set, SIGALRM); sigaddset(&set, SIGTERM); sigprocmask(SIG_BLOCK, &set, NULL); act.sa_handler = wake_me; act.sa_flags = 0; sigaction(SIGUSR1, &act, NULL); - sigaction(SIGALRM, &act, NULL); - act.sa_handler = hup; - sigaction(SIGHUP, &act, NULL); act.sa_handler = term; sigaction(SIGTERM, &act, NULL); act.sa_handler = SIG_IGN; sigaction(SIGPIPE, &act, NULL); if (takeover) { + pid_dir = VAR_RUN; victim = mdmon_pid(container->devnum); - victim_sock = connect_monitor(container->devname); + if (victim < 0) { + pid_dir = ALT_RUN; + victim = mdmon_pid(container->devnum); + } + if (victim >= 0) + victim_sock = connect_monitor(container->devname); } ignore = chdir("/"); @@ -470,6 +465,25 @@ static int mdmon(char *devname, int devnum, int must_fork, int takeover) /* Ok, this is close enough. We can say goodbye to our parent now. */ + if (victim > 0) + remove_pidfile(devname); + if (mkdir(VAR_RUN, 0600) >= 0 || errno == EEXIST) + pid_dir = VAR_RUN; + else if (mkdir(ALT_RUN, 0600) >= 0 || errno == EEXIST) + pid_dir = ALT_RUN; + else { + fprintf(stderr, "mdmon: Neither %s nor %s are writable\n" + " cannot create .pid or .sock files. Aborting\n", + VAR_RUN, ALT_RUN); + exit(3); + } + if (make_pidfile(devname) < 0) { + fprintf(stderr, "mdmon: Cannot create pid file in %s - aborting.\n", + pid_dir); + exit(3); + } + container->sock = make_control_sock(devname); + status = 0; if (write(pfd[1], &status, sizeof(status)) < 0) fprintf(stderr, "mdmon: failed to notify our parent: %d\n", @@ -484,7 +498,7 @@ static int mdmon(char *devname, int devnum, int must_fork, int takeover) exit(2); } - if (victim > -1) { + if (victim > 0) { try_kill_monitor(victim, container->devname, victim_sock); close(victim_sock); } diff --git a/mdmon.h b/mdmon.h index a03686f1..20a0a013 100644 --- a/mdmon.h +++ b/mdmon.h @@ -65,9 +65,6 @@ extern struct md_generic_cmd *active_cmd; void remove_pidfile(char *devname); void do_monitor(struct supertype *container); void do_manager(struct supertype *container); -int make_control_sock(char *devname); -int make_pidfile(char *devname); -extern int socket_hup_requested; extern int sigterm; int read_dev_state(int fd); diff --git a/monitor.c b/monitor.c index 81fef496..e43e545c 100644 --- a/monitor.c +++ b/monitor.c @@ -481,7 +481,11 @@ static int wait_and_act(struct supertype *container, int nowait) dprintf("caught sigterm, all clean... exiting\n"); else dprintf("no arrays to monitor... exiting\n"); - remove_pidfile(container->devname); + if (!sigterm) + /* On SIGTERM, someone (the take-over mdmon) will + * clean up + */ + remove_pidfile(container->devname); exit_now = 1; signal_manager(); exit(0); -- 2.39.2