]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdmon.c
mdmon: pass symbolic name to mdmon instead of device name.
[thirdparty/mdadm.git] / mdmon.c
diff --git a/mdmon.c b/mdmon.c
index d40adb26706ae732075f591eacd540fc1c99bc3c..73c244af91bcbd92a657f250b8f8ba55b3af7b55 100644 (file)
--- a/mdmon.c
+++ b/mdmon.c
@@ -1,3 +1,22 @@
+/*
+ * mdmon - monitor external metadata arrays
+ *
+ * Copyright (C) 2007-2008 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2007-2008 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,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ */
 
 /*
  * md array manager.
@@ -38,6 +57,7 @@
 #include       <string.h>
 #include       <fcntl.h>
 #include       <signal.h>
+#include       <dirent.h>
 
 #include       <sched.h>
 
@@ -59,13 +79,25 @@ int run_child(void *v)
        return 0;
 }
 
-int clone_monitor(struct supertype *container)
+#ifdef __ia64__
+int __clone2(int (*fn)(void *),
+           void *child_stack_base, size_t stack_size,
+           int flags, void *arg, ...
+        /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );
+#endif
+ int clone_monitor(struct supertype *container)
 {
        static char stack[4096];
 
+#ifdef __ia64__
+       mon_tid = __clone2(run_child, stack, sizeof(stack),
+                  CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
+                  container);
+#else
        mon_tid = clone(run_child, stack+4096-64,
                   CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
                   container);
+#endif
 
        mgr_tid = syscall(SYS_gettid);
 
@@ -138,6 +170,10 @@ static void try_kill_monitor(char *devname)
        close(fd);
        pid = strtoul(buf, NULL, 10);
 
+       /* first rule of survival... don't off yourself */
+       if (pid == getpid())
+               return;
+
        /* kill this process if it is mdmon */
        sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
        fd = open(buf, O_RDONLY);
@@ -158,7 +194,7 @@ 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);
+                       WaitClean(buf, 0);
                }
        free_mdstat(mdstat);
        remove_pidfile(devname);
@@ -226,14 +262,18 @@ static void wake_me(int sig)
 static int do_fork(void)
 {
        #ifdef DEBUG
-       if (env_no_mdmon())
+       if (check_env("MDADM_NO_MDMON"))
                return 0;
        #endif
 
        return 1;
 }
 
-
+void usage(void)
+{
+       fprintf(stderr, "Usage: mdmon [--switch-root dir] /device/name/for/container\n");
+       exit(2);
+}
 
 int main(int argc, char *argv[])
 {
@@ -245,20 +285,43 @@ int main(int argc, char *argv[])
        int pfd[2];
        int status;
        int ignore;
+       char *container_name = NULL;
+       char *switchroot = NULL;
+       int devnum;
+       char *devname;
+
+       switch (argc) {
+       case 2:
+               container_name = argv[1];
+               break;
+       case 4:
+               if (strcmp(argv[1], "--switch-root") != 0) {
+                       fprintf(stderr, "mdmon: unknown argument %s\n", argv[1]);
+                       usage();
+               }
+               switchroot = argv[2];
+               container_name = argv[3];
+               break;
+       default:
+               usage();
+       }
 
-       if (argc != 2) {
-               fprintf(stderr, "Usage: md-manage /device/name/for/container\n");
-               exit(2);
+       devnum = devname2devnum(container_name);
+       devname = devnum2devname(devnum);
+       if (strcmp(container_name, devname) != 0) {
+               fprintf(stderr, "mdmon: %s is not a valid md device name\n",
+                       container_name);
+               exit(1);
        }
-       mdfd = open(argv[1], O_RDWR);
+       mdfd = open_dev(devnum);
        if (mdfd < 0) {
-               fprintf(stderr, "md-manage: %s: %s\n", argv[1],
+               fprintf(stderr, "mdmon: %s: %s\n", container_name,
                        strerror(errno));
                exit(1);
        }
        if (md_get_version(mdfd) < 0) {
-               fprintf(stderr, "md-manage: %s: Not an md device\n",
-                       argv[1]);
+               fprintf(stderr, "mdmon: %s: Not an md device\n",
+                       container_name);
                exit(1);
        }
 
@@ -286,12 +349,52 @@ int main(int argc, char *argv[])
                }
        } else
                pfd[0] = pfd[1] = -1;
-       /* hopefully it is a container - we'll check later */
 
        container = malloc(sizeof(*container));
-       container->devnum = fd2devnum(mdfd);
-       container->devname = devnum2devname(container->devnum);
-       container->device_name = argv[1];
+       container->devnum = devnum;
+       container->devname = devname;
+       container->arrays = NULL;
+
+       if (!container->devname) {
+               fprintf(stderr, "mdmon: failed to allocate container name string\n");
+               exit(3);
+       }
+
+       mdi = sysfs_read(mdfd, container->devnum,
+                        GET_VERSION|GET_LEVEL|GET_DEVS);
+
+       if (!mdi) {
+               fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
+                       container->devname);
+               exit(3);
+       }
+       if (mdi->array.level != UnSet) {
+               fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
+                       container_name);
+               exit(3);
+       }
+       if (mdi->array.major_version != -1 ||
+           mdi->array.minor_version != -2) {
+               fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n",
+                       container_name);
+               exit(3);
+       }
+
+       container->ss = find_metadata_methods(mdi->text_version);
+       if (container->ss == NULL) {
+               fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n",
+                       container_name, mdi->text_version);
+               exit(3);
+       }
+
+       container->devs = NULL;
+       for (di = mdi->devs; di; di = di->next) {
+               struct mdinfo *cd = malloc(sizeof(*cd));
+               *cd = *di;
+               cd->next = container->devs;
+               container->devs = cd;
+       }
+       sysfs_free(mdi);
 
        /* SIGUSR is sent between parent and child.  So both block it
         * and enable it only with pselect.
@@ -313,9 +416,28 @@ int main(int argc, char *argv[])
        act.sa_handler = SIG_IGN;
        sigaction(SIGPIPE, &act, NULL);
 
-       /* If this fails, we hope it already exists */
+       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
+                */
+               try_kill_monitor(container->devname);
+               if (chroot(switchroot) != 0) {
+                       fprintf(stderr, "mdmon: failed to chroot to '%s': %s\n",
+                               switchroot, strerror(errno));
+                       exit(4);
+               }
+       }
+
+       /* 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);
-       /* pid file lives in /var/run/mdadm/mdXX.pid */
+       ignore = chdir("/");
        if (make_pidfile(container->devname, O_EXCL) < 0) {
                if (ping_monitor(container->devname) == 0) {
                        fprintf(stderr, "mdmon: %s already managed\n",
@@ -341,52 +463,14 @@ int main(int argc, char *argv[])
                        }
                }
        }
-
        container->sock = make_control_sock(container->devname);
-       container->arrays = NULL;
-
-       mdi = sysfs_read(mdfd, container->devnum,
-                        GET_VERSION|GET_LEVEL|GET_DEVS);
 
-       if (!mdi) {
-               fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
-                       container->devname);
-               exit(3);
-       }
-       if (mdi->array.level != UnSet) {
-               fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
-                       argv[1]);
-               exit(3);
-       }
-       if (mdi->array.major_version != -1 ||
-           mdi->array.minor_version != -2) {
-               fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n",
-                       argv[1]);
-               exit(3);
-       }
-
-       container->ss = find_metadata_methods(mdi->text_version);
-       if (container->ss == NULL) {
-               fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n",
-                       argv[1], mdi->text_version);
-               exit(3);
-       }
-
-       container->devs = NULL;
-       for (di = mdi->devs; di; di = di->next) {
-               struct mdinfo *cd = malloc(sizeof(*cd));
-               *cd = *di;
-               cd->next = container->devs;
-               container->devs = cd;
-       }
-       sysfs_free(mdi);
-
-
-       if (container->ss->load_super(container, mdfd, argv[1])) {
+       if (container->ss->load_super(container, mdfd, container_name)) {
                fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
-                       argv[1]);
+                       container_name);
                exit(3);
        }
+       close(mdfd);
 
        /* Ok, this is close enough.  We can say goodbye to our parent now.
         */
@@ -396,7 +480,6 @@ int main(int argc, char *argv[])
                        getppid());
        close(pfd[1]);
 
-       ignore = chdir("/");
        setsid();
        close(0);
        open("/dev/null", O_RDWR);