]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
mdmon: insist on creating .pid file at startup.
authorNeilBrown <neilb@suse.de>
Mon, 8 Feb 2010 03:30:46 +0000 (14:30 +1100)
committerNeilBrown <neilb@suse.de>
Mon, 8 Feb 2010 06:26:18 +0000 (17:26 +1100)
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 <neilb@suse.de>
managemon.c
mdmon.c
mdmon.h
monitor.c

index e01d13b9445c1329722e03f762de9c03a4838b61..037406f0df56bcfb447a35f00a5983936b33f444 100644 (file)
@@ -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 c590fb20f8a6ddc52a5236d706932419f0a25c07..31d45fd5662b046ba9e967cf48fb82c276f69443 100644 (file)
--- 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 a03686f15c0d440921bb832f83c0b28a66fa153e..20a0a013c54eadd76325cc95e3504a0075ebc7ae 100644 (file)
--- 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);
index 81fef4964e0ef7279b7b7d8976ec4e72abd51ccd..e43e545ce196f1370ad68b78c9b02dac67ae89f9 100644 (file)
--- 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);