2 * Original author : tridge@samba.org, January 2002
4 * Copyright (c) 2005 Christophe Varoqui
5 * Copyright (c) 2005 Alasdair Kergon, Redhat
12 #include <sys/ioctl.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
20 #include <systemd/sd-daemon.h>
22 #include "mpath_cmd.h"
28 * create a unix domain socket and start listening on it
29 * return a file descriptor open on the socket
31 int ux_socket_listen(const char *name)
37 struct sockaddr_un addr;
40 num = sd_listen_fds(0);
42 condlog(3, "sd_listen_fds returned %d fds", num);
44 } else if (num == 1) {
45 fd = SD_LISTEN_FDS_START + 0;
46 condlog(3, "using fd %d from sd_listen_fds", fd);
50 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
52 condlog(3, "Couldn't create ux_socket, error %d", errno);
56 memset(&addr, 0, sizeof(addr));
57 addr.sun_family = AF_LOCAL;
58 addr.sun_path[0] = '\0';
59 len = strlen(name) + 1 + sizeof(sa_family_t);
60 strncpy(&addr.sun_path[1], name, len);
62 if (bind(fd, (struct sockaddr *)&addr, len) == -1) {
63 condlog(3, "Couldn't bind to ux_socket, error %d", errno);
68 if (listen(fd, 10) == -1) {
69 condlog(3, "Couldn't listen to ux_socket, error %d", errno);
77 * keep writing until it's all sent
79 size_t write_all(int fd, const void *buf, size_t len)
84 ssize_t n = send(fd, buf, len, MSG_NOSIGNAL);
86 if ((errno == EINTR) || (errno == EAGAIN))
92 buf = n + (char *)buf;
100 * keep reading until its all read
102 ssize_t read_all(int fd, void *buf, size_t len, unsigned int timeout)
112 ret = poll(&pfd, 1, timeout);
115 } else if (ret < 0) {
119 } else if (!pfd.revents & POLLIN)
121 n = read(fd, buf, len);
123 if ((errno == EINTR) || (errno == EAGAIN))
129 buf = n + (char *)buf;
137 * send a packet in length prefix format
139 int send_packet(int fd, const char *buf)
141 return mpath_send_cmd(fd, buf);
145 * receive a packet in length prefix format
147 int recv_packet(int fd, char **buf, unsigned int timeout)
153 len = mpath_recv_reply_len(fd, timeout);
156 (*buf) = MALLOC(len);
159 err = mpath_recv_reply_data(fd, *buf, len, timeout);