]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mapfile.c
Create.c: fix uclibc build
[thirdparty/mdadm.git] / mapfile.c
index 149f67b2e4e2417e308b68477354225a43d63866..f1f3ee2cdc77bbe030a269c88d9a83e73b67c425 100644 (file)
--- a/mapfile.c
+++ b/mapfile.c
@@ -46,7 +46,6 @@
 #include       <sys/file.h>
 #include       <ctype.h>
 
-
 #define MAP_READ 0
 #define MAP_NEW 1
 #define MAP_LOCK 2
@@ -105,7 +104,6 @@ int map_write(struct map_ent *mel)
                      mapname[0]) == 0;
 }
 
-
 static FILE *lf = NULL;
 int map_lock(struct map_ent **melp)
 {
@@ -145,6 +143,8 @@ void map_unlock(struct map_ent **melp)
                unlink(mapname[2]);
                fclose(lf);
        }
+       if (*melp)
+               map_free(*melp);
        lf = NULL;
 }
 
@@ -178,7 +178,7 @@ void map_read(struct map_ent **melp)
 {
        FILE *f;
        char buf[8192];
-       char path[200];
+       char path[201];
        int uuid[4];
        char devnm[32];
        char metadata[30];
@@ -215,7 +215,7 @@ void map_free(struct map_ent *map)
 }
 
 int map_update(struct map_ent **mpp, char *devnm, char *metadata,
-              int *uuid, char *path)
+              int uuid[4], char *path)
 {
        struct map_ent *map, *mp;
        int rv;
@@ -268,6 +268,7 @@ void map_remove(struct map_ent **mapp, char *devnm)
        map_delete(mapp, devnm);
        map_write(*mapp);
        map_free(*mapp);
+       *mapp = NULL;
 }
 
 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
@@ -291,6 +292,10 @@ struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
 struct map_ent *map_by_devnm(struct map_ent **map, char *devnm)
 {
        struct map_ent *mp;
+
+       if (!devnm)
+               return NULL;
+
        if (!*map)
                map_read(map);
 
@@ -315,9 +320,9 @@ struct map_ent *map_by_name(struct map_ent **map, char *name)
        for (mp = *map ; mp ; mp = mp->next) {
                if (!mp->path)
                        continue;
-               if (strncmp(mp->path, "/dev/md/", 8) != 0)
+               if (strncmp(mp->path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0)
                        continue;
-               if (strcmp(mp->path+8, name) != 0)
+               if (strcmp(mp->path + DEV_MD_DIR_LEN, name) != 0)
                        continue;
                if (!mddev_busy(mp->devnm)) {
                        mp->bad = 1;
@@ -358,8 +363,7 @@ void RebuildMap(void)
        char *homehost = conf_get_homehost(&require_homehost);
 
        if (homehost == NULL || strcmp(homehost, "<system>")==0) {
-               if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
-                       sys_hostname[sizeof(sys_hostname)-1] = 0;
+               if (s_gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
                        homehost = sys_hostname;
                }
        }
@@ -376,7 +380,7 @@ void RebuildMap(void)
                        char dn[30];
                        int dfd;
                        int ok;
-                       int devid;
+                       dev_t devid;
                        struct supertype *st;
                        char *subarray = NULL;
                        char *path;
@@ -408,7 +412,7 @@ void RebuildMap(void)
                        devid = devnm2devid(md->devnm);
                        path = map_dev(major(devid), minor(devid), 0);
                        if (path == NULL ||
-                           strncmp(path, "/dev/md/", 8) != 0) {
+                           strncmp(path, DEV_MD_DIR, DEV_MD_DIR_LEN) != 0) {
                                /* We would really like a name that provides
                                 * an MD_DEVNAME for udev.
                                 * The name needs to be unique both in /dev/md/
@@ -416,11 +420,11 @@ void RebuildMap(void)
                                 * It needs to match what -I or -As would come
                                 * up with.
                                 * That means:
-                                *   Check if array is in mdadm.conf 
+                                *   Check if array is in mdadm.conf
                                 *        - if so use that.
                                 *   determine trustworthy from homehost etc
                                 *   find a unique name based on metadata name.
-                                *   
+                                *
                                 */
                                struct mddev_ident *match = conf_match(st, info,
                                                                       NULL, 0,
@@ -429,7 +433,7 @@ void RebuildMap(void)
                                if (match && match->devname && match->devname[0] == '/') {
                                        path = match->devname;
                                        if (path[0] != '/') {
-                                               strcpy(namebuf, "/dev/md/");
+                                               strcpy(namebuf, DEV_MD_DIR);
                                                strcat(namebuf, path);
                                                path = namebuf;
                                        }
@@ -441,8 +445,8 @@ void RebuildMap(void)
                                        if ((homehost == NULL ||
                                             st->ss->match_home(st, homehost) != 1) &&
                                            st->ss->match_home(st, "any") != 1 &&
-                                           (require_homehost
-                                            || ! conf_name_is_free(info->name)))
+                                           (require_homehost ||
+                                            !conf_name_is_free(info->name)))
                                                /* require a numeric suffix */
                                                unum = 0;
                                        else
@@ -457,19 +461,26 @@ void RebuildMap(void)
                                                        sep = "";
                                                }
                                        }
-                                       if (strchr(name, ':'))
-                                               /* probably a uniquifying
+                                       if (strchr(name, ':')) {
+                                               /* Probably a uniquifying
                                                 * hostname prefix.  Allow
-                                                * without a suffix
+                                                * without a suffix, and strip
+                                                * hostname if it is us.
                                                 */
+                                               if (homehost && unum == -1 &&
+                                                   strncmp(name, homehost,
+                                                           strlen(homehost)) == 0 &&
+                                                   name[strlen(homehost)] == ':')
+                                                       name += strlen(homehost)+1;
                                                unum = -1;
+                                       }
 
                                        while (conflict) {
                                                if (unum >= 0)
-                                                       sprintf(namebuf, "/dev/md/%s%s%d",
+                                                       sprintf(namebuf, DEV_MD_DIR "%s%s%d",
                                                                name, sep, unum);
                                                else
-                                                       sprintf(namebuf, "/dev/md/%s",
+                                                       sprintf(namebuf, DEV_MD_DIR "%s",
                                                                name);
                                                unum++;
                                                if (lstat(namebuf, &stb) != 0 &&