]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdmon.c
Improve partition table code.
[thirdparty/mdadm.git] / mdmon.c
diff --git a/mdmon.c b/mdmon.c
index a3da79543b877f2ecb224f6674127572916c85f9..961aa77873e8404944da25c988e6ab17e11b9963 100644 (file)
--- a/mdmon.c
+++ b/mdmon.c
@@ -120,6 +120,9 @@ static int make_pidfile(char *devname)
        int fd;
        int n;
 
+       if (mkdir(pid_dir, 0600) < 0 &&
+           errno != EEXIST)
+               return -errno;
        sprintf(path, "%s/%s.pid", pid_dir, devname);
 
        fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0600);
@@ -442,26 +445,23 @@ static int mdmon(char *devname, int devnum, int must_fork, int takeover)
        act.sa_handler = SIG_IGN;
        sigaction(SIGPIPE, &act, NULL);
 
-       if (takeover) {
-               pid_dir = VAR_RUN;
+       pid_dir = VAR_RUN;
+       victim = mdmon_pid(container->devnum);
+       if (victim < 0) {
+               pid_dir = ALT_RUN;
                victim = mdmon_pid(container->devnum);
-               if (victim < 0) {
-                       pid_dir = ALT_RUN;
-                       victim = mdmon_pid(container->devnum);
-               }
-               if (victim >= 0)
-                       victim_sock = connect_monitor(container->devname);
        }
+       if (victim >= 0)
+               victim_sock = connect_monitor(container->devname);
 
        ignore = chdir("/");
-       if (victim < 0) {
-               if (ping_monitor(container->devname) == 0) {
+       if (!takeover && victim > 0 && victim_sock >= 0) {
+               if (fping_monitor(victim_sock) == 0) {
                        fprintf(stderr, "mdmon: %s already managed\n",
                                container->devname);
                        exit(3);
                }
-               /* if there is a pid file, kill whoever is there just in case */
-               victim = mdmon_pid(container->devnum);
+               close(victim_sock);
        }
        if (container->ss->load_super(container, mdfd, devname)) {
                fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
@@ -474,20 +474,16 @@ static int mdmon(char *devname, int devnum, int must_fork, int takeover)
         */
        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);
-       }
+       pid_dir = VAR_RUN;
        if (make_pidfile(devname) < 0) {
-               fprintf(stderr, "mdmon: Cannot create pid file in %s - aborting.\n",
-                       pid_dir);
-               exit(3);
+               /* Try the alternate */
+               pid_dir = ALT_RUN;
+               if (make_pidfile(devname) < 0) {
+                       fprintf(stderr, "mdmon: Neither %s nor %s are writable\n"
+                               "       cannot create .pid or .sock files.  Aborting\n",
+                               VAR_RUN, ALT_RUN);
+                       exit(3);
+               }
        }
        container->sock = make_control_sock(devname);