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

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