5 #include <sys/socket.h>
11 #include "mpath_cmd.h"
14 * keep reading until its all read
16 static ssize_t read_all(int fd, void *buf, size_t len, unsigned int timeout)
26 ret = poll(&pfd, 1, timeout);
34 } else if (!(pfd.revents & POLLIN))
36 n = recv(fd, buf, len, 0);
38 if ((errno == EINTR) || (errno == EAGAIN))
44 buf = n + (char *)buf;
52 * keep writing until it's all sent
54 static size_t write_all(int fd, const void *buf, size_t len)
59 ssize_t n = send(fd, buf, len, MSG_NOSIGNAL);
61 if ((errno == EINTR) || (errno == EAGAIN))
67 buf = n + (char *)buf;
75 * connect to a unix domain socket
77 int mpath_connect(void)
80 struct sockaddr_un addr;
82 memset(&addr, 0, sizeof(addr));
83 addr.sun_family = AF_LOCAL;
84 addr.sun_path[0] = '\0';
85 len = strlen(DEFAULT_SOCKET) + 1 + sizeof(sa_family_t);
86 strncpy(&addr.sun_path[1], DEFAULT_SOCKET, len);
88 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
92 if (connect(fd, (struct sockaddr *)&addr, len) == -1) {
100 int mpath_disconnect(int fd)
105 ssize_t mpath_recv_reply_len(int fd, unsigned int timeout)
110 ret = read_all(fd, &len, sizeof(len), timeout);
113 if (ret != sizeof(len)) {
120 int mpath_recv_reply_data(int fd, char *reply, size_t len,
121 unsigned int timeout)
125 ret = read_all(fd, reply, len, timeout);
132 reply[len - 1] = '\0';
136 int mpath_recv_reply(int fd, char **reply, unsigned int timeout)
142 len = mpath_recv_reply_len(fd, timeout);
145 if (len > MAX_REPLY_LEN) {
149 *reply = malloc(len);
152 err = mpath_recv_reply_data(fd, *reply, len, timeout);
161 int mpath_send_cmd(int fd, const char *cmd)
166 len = strlen(cmd) + 1;
169 if (write_all(fd, &len, sizeof(len)) != sizeof(len))
171 if (len && write_all(fd, cmd, len) != len)
176 int mpath_process_cmd(int fd, const char *cmd, char **reply,
177 unsigned int timeout)
179 if (mpath_send_cmd(fd, cmd) != 0)
181 return mpath_recv_reply(fd, reply, timeout);