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

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