]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdmon.c
Replace all relevant occurrences of -4 with LEVEL_MULTIPATH
[thirdparty/mdadm.git] / mdmon.c
diff --git a/mdmon.c b/mdmon.c
index acb36a2d6f2621ad9ec26daf95522bbe04b3c4aa..0ec42591500de9f6e5ca978fd1d5b20606009e2e 100644 (file)
--- a/mdmon.c
+++ b/mdmon.c
@@ -1,8 +1,8 @@
 /*
  * mdmon - monitor external metadata arrays
  *
- * Copyright (C) 2007-2008 Neil Brown <neilb@suse.de>
- * Copyright (C) 2007-2008 Intel Corporation
+ * Copyright (C) 2007-2009 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2007-2009 Intel Corporation
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU General Public License,
@@ -113,6 +113,14 @@ static struct superswitch *find_metadata_methods(char *vers)
        return NULL;
 }
 
+static int test_pidfile(char *devname)
+{
+       char path[100];
+       struct stat st;
+
+       sprintf(path, "/var/run/mdadm/%s.pid", devname);
+       return stat(path, &st);
+}
 
 int make_pidfile(char *devname, int o_excl)
 {
@@ -149,26 +157,29 @@ int is_container_member(struct mdstat_ent *mdstat, char *container)
        return 1;
 }
 
-void remove_pidfile(char *devname);
-static void try_kill_monitor(char *devname)
+pid_t devname2mdmon(char *devname)
 {
        char buf[100];
+       pid_t pid = -1;
        int fd;
-       pid_t pid;
-       struct mdstat_ent *mdstat;
 
        sprintf(buf, "/var/run/mdadm/%s.pid", devname);
-       fd = open(buf, O_RDONLY);
+       fd = open(buf, O_RDONLY|O_NOATIME);
        if (fd < 0)
-               return;
-
-       if (read(fd, buf, sizeof(buf)) < 0) {
-               close(fd);
-               return;
-       }
+               return -1;
 
+       if (read(fd, buf, sizeof(buf)) > 0)
+               sscanf(buf, "%d\n", &pid);
        close(fd);
-       pid = strtoul(buf, NULL, 10);
+
+       return pid;
+}
+
+static void try_kill_monitor(pid_t pid, char *devname, int sock)
+{
+       char buf[100];
+       int fd;
+       struct mdstat_ent *mdstat;
 
        /* first rule of survival... don't off yourself */
        if (pid == getpid())
@@ -194,10 +205,9 @@ static void try_kill_monitor(char *devname)
        for ( ; mdstat; mdstat = mdstat->next)
                if (is_container_member(mdstat, devname)) {
                        sprintf(buf, "/dev/%s", mdstat->dev);
-                       WaitClean(buf, 0);
+                       WaitClean(buf, sock, 0);
                }
        free_mdstat(mdstat);
-       remove_pidfile(devname);
 }
 
 void remove_pidfile(char *devname)
@@ -306,6 +316,13 @@ int main(int argc, char *argv[])
                        if (strncmp(e->metadata_version, "external:", 9) == 0 &&
                            !is_subarray(&e->metadata_version[9])) {
                                devname = devnum2devname(e->devnum);
+                               /* update cmdline so this mdmon instance can be
+                                * distinguished from others in a call to ps(1)
+                                */
+                               if (strlen(devname) <= strlen(container_name)) {
+                                       memset(container_name, 0, strlen(container_name));
+                                       sprintf(container_name, "%s", devname);
+                               }
                                status |= mdmon(devname, e->devnum, scan,
                                                switchroot);
                        }
@@ -348,9 +365,34 @@ int mdmon(char *devname, int devnum, int scan, char *switchroot)
        int pfd[2];
        int status;
        int ignore;
+       pid_t victim = -1;
+       int victim_sock = -1;
 
        dprintf("starting mdmon for %s in %s\n",
                devname, switchroot ? : "/");
+
+       /* try to spawn mdmon instances from the target file system */
+       if (switchroot && strcmp(switchroot, "/") != 0) {
+               char path[1024];
+               pid_t pid;
+
+               sprintf(path, "%s/sbin/mdmon", switchroot);
+               switch (fork()) {
+               case 0:
+                       execl(path, "mdmon", devname, NULL);
+                       exit(1);
+               case -1:
+                       return 1;
+               default:
+                       pid = wait(&status);
+                       if (pid > -1 && WIFEXITED(status) &&
+                           WEXITSTATUS(status) == 0)
+                               return 0;
+                       else
+                               return 1;
+               }
+       }
+
        mdfd = open_dev(devnum);
        if (mdfd < 0) {
                fprintf(stderr, "mdmon: %s: %s\n", devname,
@@ -388,11 +430,12 @@ int mdmon(char *devname, int devnum, int scan, char *switchroot)
        } else
                pfd[0] = pfd[1] = -1;
 
-       container = malloc(sizeof(*container));
+       container = calloc(1, sizeof(*container));
        container->devnum = devnum;
        container->devname = devname;
        container->arrays = NULL;
        container->subarray[0] = 0;
+       container->sock = -1;
 
        if (!container->devname) {
                fprintf(stderr, "mdmon: failed to allocate container name string\n");
@@ -400,7 +443,7 @@ int mdmon(char *devname, int devnum, int scan, char *switchroot)
        }
 
        mdi = sysfs_read(mdfd, container->devnum,
-                        GET_VERSION|GET_LEVEL|GET_DEVS);
+                        GET_VERSION|GET_LEVEL|GET_DEVS|SKIP_GONE_DEVS);
 
        if (!mdi) {
                fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
@@ -457,12 +500,10 @@ int mdmon(char *devname, int devnum, int scan, char *switchroot)
 
        if (switchroot) {
                /* we assume we assume that /sys /proc /dev are available in
-                * the new root (see nash:setuproot)
-                *
-                * kill any monitors in the current namespace and change
-                * to the new one
+                * the new root
                 */
-               try_kill_monitor(container->devname);
+               victim = devname2mdmon(container->devname);
+               victim_sock = connect_monitor(container->devname);
                if (chroot(switchroot) != 0) {
                        fprintf(stderr, "mdmon: failed to chroot to '%s': %s\n",
                                switchroot, strerror(errno));
@@ -470,40 +511,15 @@ int mdmon(char *devname, int devnum, int scan, char *switchroot)
                }
        }
 
-       /* If this fails, we hope it already exists 
-        * pid file lives in /var/run/mdadm/mdXX.pid
-        */
-       mkdir("/var", 0600);
-       mkdir("/var/run", 0600);
-       mkdir("/var/run/mdadm", 0600);
        ignore = chdir("/");
-       if (make_pidfile(container->devname, O_EXCL) < 0) {
+       if (victim < 0 && test_pidfile(container->devname) == 0) {
                if (ping_monitor(container->devname) == 0) {
                        fprintf(stderr, "mdmon: %s already managed\n",
                                container->devname);
                        exit(3);
-               } else {
-                       int err;
-
-                       /* cleanup the old monitor, this one is taking over */
-                       try_kill_monitor(container->devname);
-                       err = make_pidfile(container->devname, 0);
-                       if (err < 0) {
-                               fprintf(stderr, "mdmon: %s Cannot create pidfile\n",
-                                       container->devname);
-                               if (err == -EROFS) {
-                                       /* FIXME implement a mechanism to
-                                        * prevent duplicate monitor instances
-                                        */
-                                       fprintf(stderr,
-                                               "mdmon: continuing on read-only file system\n");
-                               } else
-                                       exit(3);
-                       }
-               }
+               } else if (victim < 0)
+                       victim = devname2mdmon(container->devname);
        }
-       container->sock = make_control_sock(container->devname);
-
        if (container->ss->load_super(container, mdfd, devname)) {
                fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
                        devname);
@@ -529,7 +545,7 @@ int mdmon(char *devname, int devnum, int scan, char *switchroot)
        ignore = dup(0);
 #endif
 
-       mlockall(MCL_FUTURE);
+       mlockall(MCL_CURRENT | MCL_FUTURE);
 
        if (clone_monitor(container) < 0) {
                fprintf(stderr, "mdmon: failed to start monitor process: %s\n",
@@ -537,6 +553,10 @@ int mdmon(char *devname, int devnum, int scan, char *switchroot)
                exit(2);
        }
 
+       if (victim > -1) {
+               try_kill_monitor(victim, container->devname, victim_sock);
+               close(victim_sock);
+       }
        do_manager(container);
 
        exit(0);