[libmultipath] User friendly names enhancements
authorChristophe Varoqui <root@xa-s05.(none)>
Thu, 27 Oct 2005 07:48:41 +0000 (09:48 +0200)
committerChristophe Varoqui <root@xa-s05.(none)>
Thu, 27 Oct 2005 07:48:41 +0000 (09:48 +0200)
- add a '-b' flag to multipath(8) to let user specify the bindings file
  location
- move from "non blocking lock plus retrys" to "blocking lock plus timeout"

Benjamin Marzinski, Redhat

libmultipath/alias.c
libmultipath/alias.h
libmultipath/config.c
libmultipath/config.h
libmultipath/defaults.h
libmultipath/propsel.c
multipath/main.c

index 4e3d3d2..0addeb6 100644 (file)
@@ -7,6 +7,8 @@
 #include <string.h>
 #include <limits.h>
 #include <stdio.h>
+#include <signal.h>
+
 #include "debug.h"
 #include "uxsock.h"
 #include "alias.h"
@@ -67,35 +69,48 @@ ensure_directories_exist(char *str, mode_t dir_mode)
        return 0;
 }
 
+static void
+sigalrm(int sig)
+{
+       /* do nothing */
+}
+
 static int
 lock_bindings_file(int fd)
 {
+       struct sigaction act, oldact;
+       sigset_t set, oldset;
        struct flock lock;
-       int retrys = BINDINGS_FILE_RETRYS;
+       int err;
        
        memset(&lock, 0, sizeof(lock));
        lock.l_type = F_WRLCK;
        lock.l_whence = SEEK_SET;
 
-       while (fcntl(fd, F_SETLK, &lock) < 0) {
-               if (errno != EACCES && errno != EAGAIN) {
-                       condlog(0, "Cannot lock bindings file : %s",
-                               strerror(errno));
-                       return -1;
-               } else {
-                       condlog(0, "Bindings file is currently locked");
-                       if ((retrys--) == 0)
-                               return -1;
-               }
-               /* because I'm paranoid */
-               memset(&lock, 0, sizeof(lock));
-               lock.l_type = F_WRLCK;
-               lock.l_whence = SEEK_SET;
-               
-               condlog(0, "retrying");
-               sleep(1);
+       act.sa_handler = sigalrm;
+       sigemptyset(&act.sa_mask);
+       act.sa_flags = 0;
+       sigemptyset(&set);
+       sigaddset(&set, SIGALRM);
+
+       sigaction(SIGALRM, &act, &oldact);
+       sigprocmask(SIG_UNBLOCK, &set, &oldset);
+
+       alarm(BINDINGS_FILE_TIMEOUT);
+       err = fcntl(fd, F_SETLKW, &lock);
+       alarm(0);
+
+       if (err) {
+               if (errno != EINTR)
+                       condlog(0, "Cannot lock bindings file : %s");
+               else
+                       condlog(0, "Bindings file is locked. Giving up.");
        }
-       return 0;
+
+       sigprocmask(SIG_SETMASK, &oldset, NULL);
+       sigaction(SIGALRM, &oldact, NULL);
+       return err;
+
 }
 
 
@@ -304,7 +319,7 @@ allocate_binding(int fd, char *wwid, int id)
 }              
 
 char *
-get_user_friendly_alias(char *wwid)
+get_user_friendly_alias(char *wwid, char *file)
 {
        char *alias;
        int fd, id;
@@ -314,7 +329,7 @@ get_user_friendly_alias(char *wwid)
                return NULL;
        }
 
-       fd = open_bindings_file(BINDINGS_FILE_NAME);
+       fd = open_bindings_file(file);
        if (fd < 0)
                return NULL;
        id = lookup_binding(fd, wwid, &alias);
@@ -330,7 +345,7 @@ get_user_friendly_alias(char *wwid)
 }
 
 char *
-get_user_friendly_wwid(char *alias)
+get_user_friendly_wwid(char *alias, char *file)
 {
        char *wwid;
        int fd, id;
@@ -340,7 +355,7 @@ get_user_friendly_wwid(char *alias)
                return NULL;
        }
 
-       fd = open_bindings_file(BINDINGS_FILE_NAME);
+       fd = open_bindings_file(file);
        if (fd < 0)
                return NULL;
        id = rlookup_binding(fd, &wwid, alias);
index 749ea7b..3d64b9b 100644 (file)
@@ -1,5 +1,4 @@
-#define BINDINGS_FILE_NAME "/var/lib/multipath/bindings"
-#define BINDINGS_FILE_RETRYS 3
+#define BINDINGS_FILE_TIMEOUT 3
 #define BINDINGS_FILE_HEADER \
 "# Multipath bindings, Version : 1.0\n" \
 "# NOTE: this file is automatically maintained by the multipath program.\n" \
@@ -9,6 +8,5 @@
 "# alias wwid\n" \
 "#\n"
 
-
-char *get_user_friendly_alias(char *wwid);
-char *get_user_friendly_wwid(char *alias);
+char *get_user_friendly_alias(char *wwid, char *file);
+char *get_user_friendly_wwid(char *alias, char *file);
index 99e6b59..51b592f 100644 (file)
@@ -375,6 +375,7 @@ load_config (char * file)
 
        conf->dev_type = DEV_NONE;
        conf->minio = 1000;
+       conf->bindings_file = DEFAULT_BINDINGS_FILE;
 
        /*
         * read the config file
index 0149f68..9908926 100644 (file)
@@ -68,6 +68,7 @@ struct config {
        char * default_getprio;
        char * features;
        char * default_hwhandler;
+       char * bindings_file;
 
        vector mptable;
        vector hwtable;
index 8ec780a..a995794 100644 (file)
@@ -9,5 +9,6 @@
 #define DEFAULT_PIDFILE                "/var/run/multipathd.pid"
 #define DEFAULT_SOCKET         "/var/run/multipathd.sock"
 #define DEFAULT_CONFIGFILE     "/etc/multipath.conf"
+#define DEFAULT_BINDINGS_FILE  "/var/lib/multipath/bindings"
 
 char * set_default (char * str);
index 0253c4b..b955840 100644 (file)
@@ -130,7 +130,8 @@ select_alias (struct multipath * mp)
        else {
                mp->alias = NULL;
                if (conf->user_friendly_names)
-                       mp->alias = get_user_friendly_alias(mp->wwid);
+                       mp->alias = get_user_friendly_alias(mp->wwid,
+                                       conf->bindings_file);
                if (mp->alias == NULL)
                        mp->alias = mp->wwid;
        }
index 0cd3a5a..4be1edf 100644 (file)
@@ -114,7 +114,8 @@ get_refwwid (vector pathvec)
                /*
                 * may be a binding
                 */
-               refwwid = get_user_friendly_wwid(conf->dev);
+               refwwid = get_user_friendly_wwid(conf->dev,
+                                                conf->bindings_file);
 
                if (refwwid)
                        return refwwid;
@@ -822,11 +823,12 @@ usage (char * progname)
                "\t\t\t[-p failover|multibus|group_by_serial|group_by_prio]\n" \
                "\t\t\t[device]\n" \
                "\n" \
-               "\t-v level\tverbosty level\n" \
+               "\t-v level\tverbosity level\n" \
                "\t   0\t\t\tno output\n" \
                "\t   1\t\t\tprint created devmap names only\n" \
                "\t   2\t\t\tdefault verbosity\n" \
                "\t   3\t\t\tprint debug information\n" \
+               "\t-b file\t\tbindings file location\n" \
                "\t-d\t\tdry run, do not create or update devmaps\n" \
                "\t-l\t\tshow multipath topology (sysfs and DM info)\n" \
                "\t-ll\t\tshow multipath topology (maximum info)\n" \
@@ -1061,7 +1063,7 @@ main (int argc, char *argv[])
        if (load_config(DEFAULT_CONFIGFILE))
                exit(1);
 
-       while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:")) != EOF ) {
+       while ((arg = getopt(argc, argv, ":qdl::Ffi:M:v:p:b:")) != EOF ) {
                switch(arg) {
                case 1: printf("optarg : %s\n",optarg);
                        break;
@@ -1072,6 +1074,9 @@ main (int argc, char *argv[])
 
                        conf->verbosity = atoi(optarg);
                        break;
+               case 'b':
+                       conf->bindings_file = optarg;
+                       break;
                case 'd':
                        conf->dry_run = 1;
                        break;