Blame SOURCES/0207-UP-add-libmpathcmd.patch

4ae388
From c146b5840bbd7ad89c8a8de6192590ad0595a977 Mon Sep 17 00:00:00 2001
4ae388
From: Benjamin Marzinski <bmarzins@redhat.com>
4ae388
Date: Thu, 7 Apr 2016 18:19:58 -0500
4ae388
Subject: [PATCH] Add libmpathcmd library and use it internally
4ae388
4ae388
Other programs would like to communicate with multipathd to issue
4ae388
command or check status.  Instead of having them exec multipathd,
4ae388
I've pulled the code that sends commands and receives replies from
4ae388
multipathd into its own library.  I've made the multipath tools use
4ae388
this library internally as well.
4ae388
4ae388
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
4ae388
---
4ae388
 Makefile                         |    1 
4ae388
 Makefile.inc                     |    2 
4ae388
 libmpathcmd/Makefile             |   32 +++++++
4ae388
 libmpathcmd/mpath_cmd.c          |  178 +++++++++++++++++++++++++++++++++++++++
4ae388
 libmpathcmd/mpath_cmd.h          |  125 +++++++++++++++++++++++++++
4ae388
 libmpathpersist/Makefile         |    5 -
4ae388
 libmpathpersist/mpath_updatepr.c |   30 +++---
4ae388
 libmultipath/Makefile            |    4 
4ae388
 libmultipath/config.c            |    1 
4ae388
 libmultipath/configure.c         |   10 +-
4ae388
 libmultipath/uxsock.c            |   88 +++----------------
4ae388
 libmultipath/uxsock.h            |    6 -
4ae388
 mpathpersist/Makefile            |    2 
4ae388
 multipath/Makefile               |    5 -
4ae388
 multipathd/Makefile              |    4 
4ae388
 multipathd/uxclnt.c              |   13 +-
4ae388
 multipathd/uxlsnr.c              |    9 -
4ae388
 17 files changed, 401 insertions(+), 114 deletions(-)
4ae388
 create mode 100644 libmpathcmd/Makefile
4ae388
 create mode 100644 libmpathcmd/mpath_cmd.c
4ae388
 create mode 100644 libmpathcmd/mpath_cmd.h
4ae388
4ae388
Index: multipath-tools-130222/Makefile
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/Makefile
4ae388
+++ multipath-tools-130222/Makefile
4ae388
@@ -20,6 +20,7 @@ export KRNLSRC
4ae388
 export KRNLOBJ
4ae388
 
4ae388
 BUILDDIRS = \
4ae388
+	libmpathcmd \
4ae388
 	libmultipath \
4ae388
 	libmultipath/prioritizers \
4ae388
 	libmultipath/checkers \
4ae388
Index: multipath-tools-130222/Makefile.inc
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/Makefile.inc
4ae388
+++ multipath-tools-130222/Makefile.inc
4ae388
@@ -34,6 +34,8 @@ syslibdir   = $(prefix)/usr/$(LIB)
4ae388
 libdir	    = $(prefix)/usr/$(LIB)/multipath
4ae388
 unitdir     = $(prefix)/lib/systemd/system
4ae388
 mpathpersistdir = $(TOPDIR)/libmpathpersist
4ae388
+includedir  = $(prefix)/usr/include
4ae388
+mpathcmddir = $(TOPDIR)/libmpathcmd
4ae388
 
4ae388
 GZIP        = /bin/gzip -9 -c
4ae388
 INSTALL_PROGRAM = install
4ae388
Index: multipath-tools-130222/libmpathcmd/Makefile
4ae388
===================================================================
4ae388
--- /dev/null
4ae388
+++ multipath-tools-130222/libmpathcmd/Makefile
4ae388
@@ -0,0 +1,32 @@
4ae388
+# Makefile
4ae388
+#
4ae388
+include ../Makefile.inc
4ae388
+
4ae388
+SONAME=0
4ae388
+DEVLIB = libmpathcmd.so
4ae388
+LIBS = $(DEVLIB).$(SONAME)
4ae388
+
4ae388
+CFLAGS += -fPIC
4ae388
+
4ae388
+OBJS = mpath_cmd.o
4ae388
+
4ae388
+all: $(LIBS)
4ae388
+
4ae388
+$(LIBS): $(OBJS)
4ae388
+	$(CC) $(LDFLAGS) $(SHARED_FLAGS) -Wl,-soname=$@ $(CFLAGS) -o $@ $(OBJS) $(LIBDEPS)
4ae388
+	ln -sf $@ $(DEVLIB)
4ae388
+
4ae388
+install: $(LIBS)
4ae388
+	$(INSTALL_PROGRAM) -d $(DESTDIR)$(syslibdir)
4ae388
+	$(INSTALL_PROGRAM) -m 755 $(LIBS) $(DESTDIR)$(syslibdir)/$(LIBS)
4ae388
+	ln -sf $(LIBS) $(DESTDIR)$(syslibdir)/$(DEVLIB)
4ae388
+	$(INSTALL_PROGRAM) -d $(DESTDIR)$(includedir)
4ae388
+	$(INSTALL_PROGRAM) -m 644 mpath_cmd.h $(DESTDIR)$(includedir)
4ae388
+
4ae388
+uninstall:
4ae388
+	rm -f $(DESTDIR)$(syslibdir)/$(LIBS)
4ae388
+	rm -f $(DESTDIR)$(syslibdir)/$(DEVLIB)
4ae388
+	rm -f $(DESTDIR)$(includedir)/mpath_cmd.h
4ae388
+
4ae388
+clean:
4ae388
+	rm -f core *.a *.o *.gz *.so *.so.*
4ae388
Index: multipath-tools-130222/libmpathcmd/mpath_cmd.c
4ae388
===================================================================
4ae388
--- /dev/null
4ae388
+++ multipath-tools-130222/libmpathcmd/mpath_cmd.c
4ae388
@@ -0,0 +1,178 @@
4ae388
+#include <stdlib.h>
4ae388
+#include <unistd.h>
4ae388
+#include <stdio.h>
4ae388
+#include <sys/types.h>
4ae388
+#include <sys/socket.h>
4ae388
+#include <sys/un.h>
4ae388
+#include <poll.h>
4ae388
+#include <string.h>
4ae388
+#include <errno.h>
4ae388
+
4ae388
+#include "mpath_cmd.h"
4ae388
+
4ae388
+/*
4ae388
+ * keep reading until its all read
4ae388
+ */
4ae388
+static ssize_t read_all(int fd, void *buf, size_t len, unsigned int timeout)
4ae388
+{
4ae388
+	size_t total = 0;
4ae388
+	ssize_t n;
4ae388
+	int ret;
4ae388
+	struct pollfd pfd;
4ae388
+
4ae388
+	while (len) {
4ae388
+		pfd.fd = fd;
4ae388
+		pfd.events = POLLIN;
4ae388
+		ret = poll(&pfd, 1, timeout);
4ae388
+		if (!ret) {
4ae388
+			errno = ETIMEDOUT;
4ae388
+			return -1;
4ae388
+		} else if (ret < 0) {
4ae388
+			if (errno == EINTR)
4ae388
+				continue;
4ae388
+			return -1;
4ae388
+		} else if (!(pfd.revents & POLLIN))
4ae388
+			continue;
4ae388
+		n = read(fd, buf, len);
4ae388
+		if (n < 0) {
4ae388
+			if ((errno == EINTR) || (errno == EAGAIN))
4ae388
+				continue;
4ae388
+			return -1;
4ae388
+		}
4ae388
+		if (!n)
4ae388
+			return total;
4ae388
+		buf = n + (char *)buf;
4ae388
+		len -= n;
4ae388
+		total += n;
4ae388
+	}
4ae388
+	return total;
4ae388
+}
4ae388
+
4ae388
+/*
4ae388
+ * keep writing until it's all sent
4ae388
+ */
4ae388
+static size_t write_all(int fd, const void *buf, size_t len)
4ae388
+{
4ae388
+	size_t total = 0;
4ae388
+
4ae388
+	while (len) {
4ae388
+		ssize_t n = write(fd, buf, len);
4ae388
+		if (n < 0) {
4ae388
+			if ((errno == EINTR) || (errno == EAGAIN))
4ae388
+				continue;
4ae388
+			return total;
4ae388
+		}
4ae388
+		if (!n)
4ae388
+			return total;
4ae388
+		buf = n + (char *)buf;
4ae388
+		len -= n;
4ae388
+		total += n;
4ae388
+	}
4ae388
+	return total;
4ae388
+}
4ae388
+
4ae388
+/*
4ae388
+ * connect to a unix domain socket
4ae388
+ */
4ae388
+int mpath_connect(void)
4ae388
+{
4ae388
+	int fd, len;
4ae388
+	struct sockaddr_un addr;
4ae388
+
4ae388
+	memset(&addr, 0, sizeof(addr));
4ae388
+	addr.sun_family = AF_LOCAL;
4ae388
+	addr.sun_path[0] = '\0';
4ae388
+	len = strlen(DEFAULT_SOCKET) + 1 + sizeof(sa_family_t);
4ae388
+	strncpy(&addr.sun_path[1], DEFAULT_SOCKET, len);
4ae388
+
4ae388
+	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
4ae388
+	if (fd == -1)
4ae388
+		return -1;
4ae388
+
4ae388
+	if (connect(fd, (struct sockaddr *)&addr, len) == -1) {
4ae388
+		close(fd);
4ae388
+		return -1;
4ae388
+	}
4ae388
+
4ae388
+	return fd;
4ae388
+}
4ae388
+
4ae388
+int mpath_disconnect(int fd)
4ae388
+{
4ae388
+	return close(fd);
4ae388
+}
4ae388
+
4ae388
+ssize_t mpath_recv_reply_len(int fd, unsigned int timeout)
4ae388
+{
4ae388
+	size_t len;
4ae388
+	ssize_t ret;
4ae388
+
4ae388
+	ret = read_all(fd, &len, sizeof(len), timeout);
4ae388
+	if (ret < 0)
4ae388
+		return ret;
4ae388
+	if (ret != sizeof(len)) {
4ae388
+		errno = EIO;
4ae388
+		return -1;
4ae388
+	}
4ae388
+	return len;
4ae388
+}
4ae388
+
4ae388
+int mpath_recv_reply_data(int fd, char *reply, size_t len,
4ae388
+			  unsigned int timeout)
4ae388
+{
4ae388
+	ssize_t ret;
4ae388
+
4ae388
+	ret = read_all(fd, reply, len, timeout);
4ae388
+	if (ret < 0)
4ae388
+		return ret;
4ae388
+	if (ret != len) {
4ae388
+		errno = EIO;
4ae388
+		return -1;
4ae388
+	}
4ae388
+	reply[len - 1] = '\0';
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+int mpath_recv_reply(int fd, char **reply, unsigned int timeout)
4ae388
+{
4ae388
+	int err;
4ae388
+	ssize_t len;
4ae388
+
4ae388
+	*reply = NULL;
4ae388
+	len = mpath_recv_reply_len(fd, timeout);
4ae388
+	if (len <= 0)
4ae388
+		return -1;
4ae388
+	*reply = malloc(len);
4ae388
+	if (!*reply)
4ae388
+		return -1;
4ae388
+	err = mpath_recv_reply_data(fd, *reply, len, timeout);
4ae388
+	if (err) {
4ae388
+		free(*reply);
4ae388
+		*reply = NULL;
4ae388
+		return -1;
4ae388
+	}
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+int mpath_send_cmd(int fd, const char *cmd)
4ae388
+{
4ae388
+	size_t len;
4ae388
+
4ae388
+	if (cmd != NULL)
4ae388
+		len = strlen(cmd) + 1;
4ae388
+	else
4ae388
+		len = 0;
4ae388
+	if (write_all(fd, &len, sizeof(len)) != sizeof(len))
4ae388
+		return -1;
4ae388
+	if (len && write_all(fd, cmd, len) != len)
4ae388
+		return -1;
4ae388
+	return 0;
4ae388
+}
4ae388
+
4ae388
+int mpath_process_cmd(int fd, const char *cmd, char **reply,
4ae388
+		      unsigned int timeout)
4ae388
+{
4ae388
+	if (mpath_send_cmd(fd, cmd) != 0)
4ae388
+		return -1;
4ae388
+	return mpath_recv_reply(fd, reply, timeout);
4ae388
+}
4ae388
Index: multipath-tools-130222/libmpathcmd/mpath_cmd.h
4ae388
===================================================================
4ae388
--- /dev/null
4ae388
+++ multipath-tools-130222/libmpathcmd/mpath_cmd.h
4ae388
@@ -0,0 +1,125 @@
4ae388
+/*
4ae388
+ * Copyright (C) 2015 Red Hat, Inc.
4ae388
+ *
4ae388
+ * This file is part of the device-mapper multipath userspace tools.
4ae388
+ *
4ae388
+ * This program is free software; you can redistribute it and/or
4ae388
+ * modify it under the terms of the GNU Lesser General Public License
4ae388
+ * as published by the Free Software Foundation; either version 2
4ae388
+ * of the License, or (at your option) any later version.
4ae388
+ *
4ae388
+ * This program is distributed in the hope that it will be useful,
4ae388
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4ae388
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4ae388
+ * GNU Lesser General Public License for more details.
4ae388
+ *
4ae388
+ * You should have received a copy of the GNU Lesser General Public
4ae388
+ * License along with this program; if not, write to the Free Software
4ae388
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
4ae388
+ * USA.
4ae388
+ */
4ae388
+
4ae388
+#ifndef LIB_MPATH_CMD_H
4ae388
+#define LIB_MPATH_CMD_H
4ae388
+
4ae388
+#ifdef __cpluscplus
4ae388
+extern "C" {
4ae388
+#endif
4ae388
+
4ae388
+#define DEFAULT_SOCKET		"/org/kernel/linux/storage/multipathd"
4ae388
+#define DEFAULT_REPLY_TIMEOUT	10000
4ae388
+
4ae388
+
4ae388
+/*
4ae388
+ * DESCRIPTION:
4ae388
+ * 	Connect to the running multipathd daemon. On systems with the
4ae388
+ * 	multipathd.socket systemd unit file installed, this command will
4ae388
+ * 	start multipathd if it is not already running. This function
4ae388
+ * 	must be run before any of the others in this library
4ae388
+ *
4ae388
+ * RETURNS:
4ae388
+ * 	A file descriptor on success. -1 on failure (with errno set).
4ae388
+ */
4ae388
+int mpath_connect(void);
4ae388
+
4ae388
+
4ae388
+/*
4ae388
+ * DESCRIPTION:
4ae388
+ * 	Disconnect from the multipathd daemon. This function must be
4ae388
+ * 	run after after processing all the multipath commands.
4ae388
+ *
4ae388
+ * RETURNS:
4ae388
+ * 	0 on success. -1 on failure (with errno set).
4ae388
+ */
4ae388
+int mpath_disconnect(int fd);
4ae388
+
4ae388
+
4ae388
+/*
4ae388
+ * DESCRIPTION
4ae388
+ * 	Send multipathd a command and return the reply. This function
4ae388
+ * 	does the same as calling mpath_send_cmd() and then
4ae388
+ *	mpath_recv_reply()
4ae388
+ *
4ae388
+ * RETURNS:
4ae388
+ * 	0 on successs, and reply will either be NULL (if there was no
4ae388
+ * 	reply data), or point to the reply string, which must be freed by
4ae388
+ * 	the caller. -1 on failure (with errno set).
4ae388
+ */
4ae388
+int mpath_process_cmd(int fd, const char *cmd, char **reply,
4ae388
+		      unsigned int timeout);
4ae388
+
4ae388
+
4ae388
+/*
4ae388
+ * DESCRIPTION:
4ae388
+ * 	Send a command to multipathd
4ae388
+ *
4ae388
+ * RETURNS:
4ae388
+ * 	0 on success. -1 on failure (with errno set)
4ae388
+ */
4ae388
+int mpath_send_cmd(int fd, const char *cmd);
4ae388
+
4ae388
+
4ae388
+/*
4ae388
+ * DESCRIPTION:
4ae388
+ * 	Return a reply from multipathd for a previously sent command.
4ae388
+ * 	This is equivalent to calling mpath_recv_reply_len(), allocating
4ae388
+ * 	a buffer of the appropriate size, and then calling
4ae388
+ *	mpath_recv_reply_data() with that buffer.
4ae388
+ *
4ae388
+ * RETURNS:
4ae388
+ * 	0 on success, and reply will either be NULL (if there was no
4ae388
+ * 	reply data), or point to the reply string, which must be freed by
4ae388
+ * 	the caller, -1 on failure (with errno set).
4ae388
+ */
4ae388
+int mpath_recv_reply(int fd, char **reply, unsigned int timeout);
4ae388
+
4ae388
+
4ae388
+/*
4ae388
+ * DESCRIPTION:
4ae388
+ * 	Return the size of the upcoming reply data from the sent multipath
4ae388
+ * 	command. This must be called before calling mpath_recv_reply_data().
4ae388
+ *
4ae388
+ * RETURNS:
4ae388
+ * 	The required size of the reply data buffer on success. -1 on
4ae388
+ * 	failure (with errno set).
4ae388
+ */
4ae388
+ssize_t mpath_recv_reply_len(int fd, unsigned int timeout);
4ae388
+
4ae388
+
4ae388
+/*
4ae388
+ * DESCRIPTION:
4ae388
+ * 	Return the reply data from the sent multipath command.
4ae388
+ * 	mpath_recv_reply_len must be called first. reply must point to a
4ae388
+ * 	buffer of len size.
4ae388
+ *
4ae388
+ * RETURNS:
4ae388
+ * 	0 on success, and reply will contain the reply data string. -1
4ae388
+ * 	on failure (with errno set).
4ae388
+ */
4ae388
+int mpath_recv_reply_data(int fd, char *reply, size_t len,
4ae388
+			  unsigned int timeout);
4ae388
+
4ae388
+#ifdef __cplusplus
4ae388
+}
4ae388
+#endif
4ae388
+#endif /* LIB_MPATH_CMD_H */
4ae388
Index: multipath-tools-130222/libmpathpersist/Makefile
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmpathpersist/Makefile
4ae388
+++ multipath-tools-130222/libmpathpersist/Makefile
4ae388
@@ -10,8 +10,9 @@ DEVLIB = libmpathpersist.so
4ae388
 LIBS = $(DEVLIB).$(SONAME)
4ae388
 
4ae388
 
4ae388
-CFLAGS += -fPIC -I$(multipathdir) -I$(mpathpersistdir)
4ae388
-LIBDEPS +=  -lpthread -ldevmapper -ldl -L$(multipathdir) -lmultipath
4ae388
+CFLAGS += -fPIC -I$(multipathdir) -I$(mpathpersistdir) -I$(mpathcmddir)
4ae388
+LIBDEPS +=  -lpthread -ldevmapper -ldl -L$(multipathdir) -lmultipath \
4ae388
+	-L$(mpathcmddir) -lmpathcmd
4ae388
 
4ae388
 OBJS = mpath_persist.o mpath_updatepr.o mpath_pr_ioctl.o 
4ae388
 
4ae388
Index: multipath-tools-130222/libmpathpersist/mpath_updatepr.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmpathpersist/mpath_updatepr.c
4ae388
+++ multipath-tools-130222/libmpathpersist/mpath_updatepr.c
4ae388
@@ -12,9 +12,9 @@
4ae388
 #include <sys/poll.h>
4ae388
 #include <errno.h>
4ae388
 #include <debug.h>
4ae388
+#include <mpath_cmd.h>
4ae388
+#include <uxsock.h>
4ae388
 #include "memory.h"
4ae388
-#include "../libmultipath/uxsock.h"
4ae388
-#include "../libmultipath/defaults.h"
4ae388
 
4ae388
 unsigned long mem_allocated;    /* Total memory used in Bytes */
4ae388
 
4ae388
@@ -23,10 +23,9 @@ int update_prflag(char * arg1, char * ar
4ae388
 	int fd;
4ae388
 	char str[64];
4ae388
 	char *reply;
4ae388
-	size_t len;
4ae388
 	int ret = 0;
4ae388
 
4ae388
-	fd = ux_socket_connect(DEFAULT_SOCKET);
4ae388
+	fd = mpath_connect();
4ae388
 	if (fd == -1) {
4ae388
 		condlog (0, "ux socket connect error");
4ae388
 		return 1 ;
4ae388
@@ -34,18 +33,23 @@ int update_prflag(char * arg1, char * ar
4ae388
 
4ae388
 	snprintf(str,sizeof(str),"map %s %s", arg1, arg2);
4ae388
 	condlog (2, "%s: pr flag message=%s", arg1, str);
4ae388
-	send_packet(fd, str, strlen(str) + 1);
4ae388
-	recv_packet(fd, &reply, &len;;
4ae388
-
4ae388
-	condlog (2, "%s: message=%s reply=%s", arg1, str, reply);
4ae388
-	if (!reply || strncmp(reply,"ok", 2) == 0)
4ae388
-		ret = -1;
4ae388
-	else if (strncmp(reply, "fail", 4) == 0)
4ae388
+	send_packet(fd, str);
4ae388
+	ret = recv_packet(fd, &reply);
4ae388
+	if (ret < 0) {
4ae388
+		condlog(2, "%s: message=%s recv error=%d", arg1, str, errno);
4ae388
 		ret = -2;
4ae388
-	else{
4ae388
-		ret = atoi(reply);
4ae388
+	} else {
4ae388
+		condlog (2, "%s: message=%s reply=%s", arg1, str, reply);
4ae388
+		if (!reply || strncmp(reply,"ok", 2) == 0)
4ae388
+			ret = -1;
4ae388
+		else if (strncmp(reply, "fail", 4) == 0)
4ae388
+			ret = -2;
4ae388
+		else{
4ae388
+			ret = atoi(reply);
4ae388
+		}
4ae388
 	}
4ae388
 
4ae388
 	free(reply);
4ae388
+	mpath_disconnect(fd);
4ae388
 	return ret;
4ae388
 }
4ae388
Index: multipath-tools-130222/libmultipath/Makefile
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/Makefile
4ae388
+++ multipath-tools-130222/libmultipath/Makefile
4ae388
@@ -7,8 +7,8 @@ include ../Makefile.inc
4ae388
 SONAME=0
4ae388
 DEVLIB = libmultipath.so
4ae388
 LIBS = $(DEVLIB).$(SONAME)
4ae388
-LIBDEPS = -lpthread -ldl -ldevmapper -ludev
4ae388
-CFLAGS += -fPIC
4ae388
+LIBDEPS = -lpthread -ldl -ldevmapper -ludev -L$(mpathcmddir) -lmpathcmd
4ae388
+CFLAGS += -fPIC -I$(mpathcmddir)
4ae388
 
4ae388
 OBJS = memory.o parser.o vector.o devmapper.o \
4ae388
        hwtable.o blacklist.o util.o dmparser.o config.o \
4ae388
Index: multipath-tools-130222/libmultipath/config.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/config.c
4ae388
+++ multipath-tools-130222/libmultipath/config.c
4ae388
@@ -25,6 +25,7 @@
4ae388
 #include "prio.h"
4ae388
 #include "devmapper.h"
4ae388
 #include "version.h"
4ae388
+#include "mpath_cmd.h"
4ae388
 
4ae388
 static int
4ae388
 hwe_strmatch (struct hwentry *hwe1, struct hwentry *hwe2)
4ae388
Index: multipath-tools-130222/libmultipath/configure.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/configure.c
4ae388
+++ multipath-tools-130222/libmultipath/configure.c
4ae388
@@ -14,6 +14,7 @@
4ae388
 #include <errno.h>
4ae388
 #include <libdevmapper.h>
4ae388
 #include <libudev.h>
4ae388
+#include <mpath_cmd.h>
4ae388
 
4ae388
 #include "checkers.h"
4ae388
 #include "vector.h"
4ae388
@@ -752,16 +753,15 @@ check_daemon(void)
4ae388
 {
4ae388
 	int fd;
4ae388
 	char *reply;
4ae388
-	size_t len;
4ae388
 	int ret = 0;
4ae388
 
4ae388
-	fd = ux_socket_connect(DEFAULT_SOCKET);
4ae388
+	fd = mpath_connect();
4ae388
 	if (fd == -1)
4ae388
 		return 0;
4ae388
 
4ae388
-	if (send_packet(fd, "show daemon", 12) != 0)
4ae388
+	if (send_packet(fd, "show daemon") != 0)
4ae388
 		goto out;
4ae388
-	if (recv_packet(fd, &reply, &len) != 0)
4ae388
+	if (recv_packet(fd, &reply) != 0)
4ae388
 		goto out;
4ae388
 
4ae388
 	if (strstr(reply, "shutdown"))
4ae388
@@ -772,7 +772,7 @@ check_daemon(void)
4ae388
 out_free:
4ae388
 	FREE(reply);
4ae388
 out:
4ae388
-	close(fd);
4ae388
+	mpath_disconnect(fd);
4ae388
 	return ret;
4ae388
 }
4ae388
 
4ae388
Index: multipath-tools-130222/libmultipath/uxsock.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/uxsock.c
4ae388
+++ multipath-tools-130222/libmultipath/uxsock.c
4ae388
@@ -16,37 +16,10 @@
4ae388
 #include <sys/poll.h>
4ae388
 #include <signal.h>
4ae388
 #include <errno.h>
4ae388
+#include <mpath_cmd.h>
4ae388
 
4ae388
 #include "memory.h"
4ae388
 #include "uxsock.h"
4ae388
-
4ae388
-/*
4ae388
- * connect to a unix domain socket
4ae388
- */
4ae388
-int ux_socket_connect(const char *name)
4ae388
-{
4ae388
-	int fd, len;
4ae388
-	struct sockaddr_un addr;
4ae388
-
4ae388
-	memset(&addr, 0, sizeof(addr));
4ae388
-	addr.sun_family = AF_LOCAL;
4ae388
-	addr.sun_path[0] = '\0';
4ae388
-	len = strlen(name) + 1 + sizeof(sa_family_t);
4ae388
-	strncpy(&addr.sun_path[1], name, len);
4ae388
-
4ae388
-	fd = socket(AF_LOCAL, SOCK_STREAM, 0);
4ae388
-	if (fd == -1) {
4ae388
-		return -1;
4ae388
-	}
4ae388
-
4ae388
-	if (connect(fd, (struct sockaddr *)&addr, len) == -1) {
4ae388
-		close(fd);
4ae388
-		return -1;
4ae388
-	}
4ae388
-
4ae388
-	return fd;
4ae388
-}
4ae388
-
4ae388
 /*
4ae388
  * create a unix domain socket and start listening on it
4ae388
  * return a file descriptor open on the socket
4ae388
@@ -102,32 +75,9 @@ size_t write_all(int fd, const void *buf
4ae388
 }
4ae388
 
4ae388
 /*
4ae388
- * keep reading until its all read
4ae388
- */
4ae388
-size_t read_all(int fd, void *buf, size_t len)
4ae388
-{
4ae388
-	size_t total = 0;
4ae388
-
4ae388
-	while (len) {
4ae388
-		ssize_t n = read(fd, buf, len);
4ae388
-		if (n < 0) {
4ae388
-			if ((errno == EINTR) || (errno == EAGAIN))
4ae388
-				continue;
4ae388
-			return total;
4ae388
-		}
4ae388
-		if (!n)
4ae388
-			return total;
4ae388
-		buf = n + (char *)buf;
4ae388
-		len -= n;
4ae388
-		total += n;
4ae388
-	}
4ae388
-	return total;
4ae388
-}
4ae388
-
4ae388
-/*
4ae388
  * send a packet in length prefix format
4ae388
  */
4ae388
-int send_packet(int fd, const char *buf, size_t len)
4ae388
+int send_packet(int fd, const char *buf)
4ae388
 {
4ae388
 	int ret = 0;
4ae388
 	sigset_t set, old;
4ae388
@@ -137,10 +87,7 @@ int send_packet(int fd, const char *buf,
4ae388
 	sigaddset(&set, SIGPIPE);
4ae388
 	pthread_sigmask(SIG_BLOCK, &set, &old;;
4ae388
 
4ae388
-	if (write_all(fd, &len, sizeof(len)) != sizeof(len))
4ae388
-		ret = -1;
4ae388
-	if (!ret && write_all(fd, buf, len) != len)
4ae388
-		ret = -1;
4ae388
+	ret = mpath_send_cmd(fd, buf);
4ae388
 
4ae388
 	/* And unblock it again */
4ae388
 	pthread_sigmask(SIG_SETMASK, &old, NULL);
4ae388
@@ -151,25 +98,24 @@ int send_packet(int fd, const char *buf,
4ae388
 /*
4ae388
  * receive a packet in length prefix format
4ae388
  */
4ae388
-int recv_packet(int fd, char **buf, size_t *len)
4ae388
+int recv_packet(int fd, char **buf)
4ae388
 {
4ae388
-	if (read_all(fd, len, sizeof(*len)) != sizeof(*len)) {
4ae388
-		(*buf) = NULL;
4ae388
-		*len = 0;
4ae388
-		return -1;
4ae388
-	}
4ae388
-	if (len == 0) {
4ae388
-		(*buf) = NULL;
4ae388
-		return 0;
4ae388
-	}
4ae388
-	(*buf) = MALLOC(*len);
4ae388
+	int err;
4ae388
+	ssize_t len;
4ae388
+	unsigned int timeout = DEFAULT_REPLY_TIMEOUT;
4ae388
+
4ae388
+	*buf = NULL;
4ae388
+	len = mpath_recv_reply_len(fd, timeout);
4ae388
+	if (len <= 0)
4ae388
+		return len;
4ae388
+	(*buf) = MALLOC(len);
4ae388
 	if (!*buf)
4ae388
-		return -1;
4ae388
-	if (read_all(fd, *buf, *len) != *len) {
4ae388
+		return -ENOMEM;
4ae388
+	err = mpath_recv_reply_data(fd, *buf, len, timeout);
4ae388
+	if (err) {
4ae388
 		FREE(*buf);
4ae388
 		(*buf) = NULL;
4ae388
-		*len = 0;
4ae388
-		return -1;
4ae388
+		return err;
4ae388
 	}
4ae388
 	return 0;
4ae388
 }
4ae388
Index: multipath-tools-130222/libmultipath/uxsock.h
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/libmultipath/uxsock.h
4ae388
+++ multipath-tools-130222/libmultipath/uxsock.h
4ae388
@@ -1,7 +1,5 @@
4ae388
 /* some prototypes */
4ae388
-int ux_socket_connect(const char *name);
4ae388
 int ux_socket_listen(const char *name);
4ae388
-int send_packet(int fd, const char *buf, size_t len);
4ae388
-int recv_packet(int fd, char **buf, size_t *len);
4ae388
+int send_packet(int fd, const char *buf);
4ae388
+int recv_packet(int fd, char **buf);
4ae388
 size_t write_all(int fd, const void *buf, size_t len);
4ae388
-size_t read_all(int fd, void *buf, size_t len);
4ae388
Index: multipath-tools-130222/mpathpersist/Makefile
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/mpathpersist/Makefile
4ae388
+++ multipath-tools-130222/mpathpersist/Makefile
4ae388
@@ -5,7 +5,7 @@ include ../Makefile.inc
4ae388
 OBJS = main.o 
4ae388
 
4ae388
 CFLAGS += -I$(multipathdir) -I$(mpathpersistdir) 
4ae388
-LDFLAGS += -lpthread -ldevmapper -L$(mpathpersistdir) -lmpathpersist -L$(multipathdir) -lmultipath -ludev
4ae388
+LDFLAGS += -lpthread -ldevmapper -L$(mpathpersistdir) -lmpathpersist -L$(multipathdir) -L$(mpathcmddir) -lmpathcmd -lmultipath -ludev
4ae388
 
4ae388
 EXEC = mpathpersist
4ae388
 
4ae388
Index: multipath-tools-130222/multipath/Makefile
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/multipath/Makefile
4ae388
+++ multipath-tools-130222/multipath/Makefile
4ae388
@@ -6,8 +6,9 @@ include ../Makefile.inc
4ae388
 
4ae388
 OBJS = main.o
4ae388
 
4ae388
-CFLAGS += -fPIC -I$(multipathdir)
4ae388
-LDFLAGS += -lpthread -ldevmapper -ldl -L$(multipathdir) -lmultipath -ludev
4ae388
+CFLAGS += -I$(multipathdir) -I$(mpathcmddir)
4ae388
+LDFLAGS += -lpthread -ldevmapper -ldl -L$(multipathdir) -lmultipath -ludev \
4ae388
+	-L$(mpathcmddir) -lmpathcmd
4ae388
 
4ae388
 EXEC = multipath
4ae388
 
4ae388
Index: multipath-tools-130222/multipathd/Makefile
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/multipathd/Makefile
4ae388
+++ multipath-tools-130222/multipathd/Makefile
4ae388
@@ -5,10 +5,10 @@ include ../Makefile.inc
4ae388
 #
4ae388
 # basic flags setting
4ae388
 #
4ae388
-CFLAGS += -fPIE -DPIE -I$(multipathdir) -I$(mpathpersistdir)
4ae388
+CFLAGS += -fPIE -DPIE -I$(multipathdir) -I$(mpathpersistdir) -I$(mpathcmddir)
4ae388
 LDFLAGS += -lpthread -ldevmapper -lreadline -ludev -ldl \
4ae388
 	   -L$(multipathdir) -lmultipath -L$(mpathpersistdir) -lmpathpersist \
4ae388
-	   -Wl,-z,now -pie
4ae388
+	   -L$(mpathcmddir) -lmpathcmd -Wl,-z,now -pie
4ae388
 
4ae388
 #
4ae388
 # debuging stuff
4ae388
Index: multipath-tools-130222/multipathd/uxclnt.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/multipathd/uxclnt.c
4ae388
+++ multipath-tools-130222/multipathd/uxclnt.c
4ae388
@@ -17,6 +17,7 @@
4ae388
 #include <readline/readline.h>
4ae388
 #include <readline/history.h>
4ae388
 
4ae388
+#include <mpath_cmd.h>
4ae388
 #include <uxsock.h>
4ae388
 #include <memory.h>
4ae388
 #include <defaults.h>
4ae388
@@ -49,7 +50,6 @@ static void process(int fd)
4ae388
 	rl_readline_name = "multipathd";
4ae388
 	rl_completion_entry_function = key_generator;
4ae388
 	while ((line = readline("multipathd> "))) {
4ae388
-		size_t len;
4ae388
 		size_t llen = strlen(line);
4ae388
 
4ae388
 		if (!llen) {
4ae388
@@ -61,8 +61,8 @@ static void process(int fd)
4ae388
 		if (!strncmp(line, "quit", 4) && llen == 4)
4ae388
 			break;
4ae388
 
4ae388
-		if (send_packet(fd, line, llen + 1) != 0) break;
4ae388
-		if (recv_packet(fd, &reply, &len) != 0) break;
4ae388
+		if (send_packet(fd, line) != 0) break;
4ae388
+		if (recv_packet(fd, &reply) != 0) break;
4ae388
 
4ae388
 		print_reply(reply);
4ae388
 
4ae388
@@ -77,13 +77,12 @@ static void process(int fd)
4ae388
 static void process_req(int fd, char * inbuf)
4ae388
 {
4ae388
 	char *reply;
4ae388
-	size_t len;
4ae388
 
4ae388
-	if (send_packet(fd, inbuf, strlen(inbuf) + 1) != 0) {
4ae388
+	if (send_packet(fd, inbuf) != 0) {
4ae388
 		printf("cannot send packet\n");
4ae388
 		return;
4ae388
 	}
4ae388
-	if (recv_packet(fd, &reply, &len) != 0)
4ae388
+	if (recv_packet(fd, &reply) != 0)
4ae388
 		printf("error receiving packet\n");
4ae388
 	else {
4ae388
 		printf("%s", reply);
4ae388
@@ -98,7 +97,7 @@ int uxclnt(char * inbuf)
4ae388
 {
4ae388
 	int fd;
4ae388
 
4ae388
-	fd = ux_socket_connect(DEFAULT_SOCKET);
4ae388
+	fd = mpath_connect();
4ae388
 	if (fd == -1) {
4ae388
 		perror("ux_socket_connect");
4ae388
 		exit(1);
4ae388
Index: multipath-tools-130222/multipathd/uxlsnr.c
4ae388
===================================================================
4ae388
--- multipath-tools-130222.orig/multipathd/uxlsnr.c
4ae388
+++ multipath-tools-130222/multipathd/uxlsnr.c
4ae388
@@ -29,6 +29,7 @@
4ae388
 #include <structs_vec.h>
4ae388
 #include <uxsock.h>
4ae388
 #include <defaults.h>
4ae388
+#include <mpath_cmd.h>
4ae388
 
4ae388
 #include "main.h"
4ae388
 #include "cli.h"
4ae388
@@ -108,7 +109,6 @@ void * uxsock_listen(int (*uxsock_trigge
4ae388
 			void * trigger_data)
4ae388
 {
4ae388
 	int ux_sock;
4ae388
-	size_t len;
4ae388
 	int rlen;
4ae388
 	char *inbuf;
4ae388
 	char *reply;
4ae388
@@ -171,16 +171,15 @@ void * uxsock_listen(int (*uxsock_trigge
4ae388
 			struct client *next = c->next;
4ae388
 
4ae388
 			if (polls[i].revents & POLLIN) {
4ae388
-				if (recv_packet(c->fd, &inbuf, &len) != 0) {
4ae388
+				if (recv_packet(c->fd, &inbuf) != 0) {
4ae388
 					dead_client(c);
4ae388
 				} else {
4ae388
-					inbuf[len - 1] = 0;
4ae388
 					condlog(4, "Got request [%s]", inbuf);
4ae388
 					uxsock_trigger(inbuf, &reply, &rlen,
4ae388
 						       trigger_data);
4ae388
 					if (reply) {
4ae388
-						if (send_packet(c->fd, reply,
4ae388
-								rlen) != 0) {
4ae388
+						if (send_packet(c->fd,
4ae388
+								reply) != 0) {
4ae388
 							dead_client(c);
4ae388
 						}
4ae388
 						condlog(4, "Reply [%d bytes]",