]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdmon.c
Having single function to read mdmon pid file.
[thirdparty/mdadm.git] / mdmon.c
diff --git a/mdmon.c b/mdmon.c
index c66b06bf4aaefbc32f69ba5a3d8241c0e7d03c93..d20bb3e490c01bbde2067bffb70c31ae50dcab04 100644 (file)
--- a/mdmon.c
+++ b/mdmon.c
@@ -113,15 +113,6 @@ 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)
 {
        char path[100];
@@ -157,24 +148,6 @@ int is_container_member(struct mdstat_ent *mdstat, char *container)
        return 1;
 }
 
-pid_t devname2mdmon(char *devname)
-{
-       char buf[100];
-       pid_t pid = -1;
-       int fd;
-
-       sprintf(buf, "/var/run/mdadm/%s.pid", devname);
-       fd = open(buf, O_RDONLY|O_NOATIME);
-       if (fd < 0)
-               return -1;
-
-       if (read(fd, buf, sizeof(buf)) > 0)
-               sscanf(buf, "%d\n", &pid);
-       close(fd);
-
-       return pid;
-}
-
 static void try_kill_monitor(pid_t pid, char *devname, int sock)
 {
        char buf[100];
@@ -368,15 +341,35 @@ static int mdmon(char *devname, int devnum, int must_fork, char *switchroot)
        dprintf("starting mdmon for %s in %s\n",
                devname, switchroot ? : "/");
 
+       /* switchroot is either a path name starting with '/', or a
+        * pid of the original mdmon (we have already done the chroot).
+        * In the latter case, stdin is a socket connected to the original
+        * mdmon.
+        */
+
        /* try to spawn mdmon instances from the target file system */
-       if (switchroot && strcmp(switchroot, "/") != 0) {
-               char path[1024];
+       if (switchroot && switchroot[0] == '/' &&
+           strcmp(switchroot, "/") != 0) {
                pid_t pid;
+               char buf[20];
 
-               sprintf(path, "%s/sbin/mdmon", switchroot);
                switch (fork()) {
                case 0:
-                       execl(path, "mdmon", devname, NULL);
+                       victim = mdmon_pid(devnum);
+                       victim_sock = connect_monitor(devname);
+                       if (chroot(switchroot) != 0) {
+                               fprintf(stderr, "mdmon: failed to chroot to '%s': %s\n",
+                                       switchroot, strerror(errno));
+                               exit(4);
+                       }
+                       ignore = chdir("/");
+                       sprintf(buf, "%d", victim);
+                       if (victim_sock) {
+                               close(0);
+                               dup(victim_sock);
+                               close(victim_sock);
+                       }
+                       execl("/sbin/mdmon", "mdmon", devname, buf, NULL);
                        exit(1);
                case -1:
                        return 1;
@@ -499,23 +492,24 @@ static int mdmon(char *devname, int devnum, int must_fork, char *switchroot)
                /* we assume we assume that /sys /proc /dev are available in
                 * the new root
                 */
-               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));
-                       exit(4);
+               if (switchroot[0] == '/') {
+                       victim = mdmon_pid(container->devnum);
+                       victim_sock = connect_monitor(container->devname);
+               } else {
+                       victim = atoi(switchroot);
+                       victim_sock = 0;
                }
        }
 
        ignore = chdir("/");
-       if (victim < 0 && test_pidfile(container->devname) == 0) {
+       if (victim < 0) {
                if (ping_monitor(container->devname) == 0) {
                        fprintf(stderr, "mdmon: %s already managed\n",
                                container->devname);
                        exit(3);
-               } else if (victim < 0)
-                       victim = devname2mdmon(container->devname);
+               }
+               /* if there is a pid file, kill whoever is there just in case */
+               victim = mdmon_pid(container->devnum);
        }
        if (container->ss->load_super(container, mdfd, devname)) {
                fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
@@ -532,16 +526,6 @@ static int mdmon(char *devname, int devnum, int must_fork, char *switchroot)
                        getppid());
        close(pfd[1]);
 
-       setsid();
-       close(0);
-       open("/dev/null", O_RDWR);
-       close(1);
-       ignore = dup(0);
-#ifndef DEBUG
-       close(2);
-       ignore = dup(0);
-#endif
-
        mlockall(MCL_CURRENT | MCL_FUTURE);
 
        if (clone_monitor(container) < 0) {
@@ -554,6 +538,17 @@ static int mdmon(char *devname, int devnum, int must_fork, char *switchroot)
                try_kill_monitor(victim, container->devname, victim_sock);
                close(victim_sock);
        }
+
+       setsid();
+       close(0);
+       open("/dev/null", O_RDWR);
+       close(1);
+       ignore = dup(0);
+#ifndef DEBUG
+       close(2);
+       ignore = dup(0);
+#endif
+
        do_manager(container);
 
        exit(0);