#include <poll.h>
#include <string.h>
#include <errno.h>
+#include <fcntl.h>
#include "mpath_cmd.h"
/*
* connect to a unix domain socket
*/
-int mpath_connect(void)
+int __mpath_connect(int nonblocking)
{
int fd, len;
struct sockaddr_un addr;
+ int flags = 0;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
addr.sun_path[0] = '\0';
+ strncpy(&addr.sun_path[1], DEFAULT_SOCKET, sizeof(addr.sun_path) - 1);
len = strlen(DEFAULT_SOCKET) + 1 + sizeof(sa_family_t);
- strncpy(&addr.sun_path[1], DEFAULT_SOCKET, len);
+ if (len > sizeof(struct sockaddr_un))
+ len = sizeof(struct sockaddr_un);
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd == -1)
return -1;
+ if (nonblocking) {
+ flags = fcntl(fd, F_GETFL, 0);
+ if (flags != -1)
+ (void)fcntl(fd, F_SETFL, flags|O_NONBLOCK);
+ }
+
if (connect(fd, (struct sockaddr *)&addr, len) == -1) {
+ int err = errno;
+
close(fd);
+ errno = err;
return -1;
}
+ if (nonblocking && flags != -1)
+ (void)fcntl(fd, F_SETFL, flags);
+
return fd;
}
+/*
+ * connect to a unix domain socket
+ */
+int mpath_connect(void)
+{
+ return __mpath_connect(0);
+}
+
int mpath_disconnect(int fd)
{
return close(fd);