2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Volker Lendecke 2013
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/select.h"
22 #include "system/time.h"
23 #include "system/network.h"
24 #include "lib/util/dlinklist.h"
25 #include "pthreadpool/pthreadpool_pipe.h"
26 #include "lib/util/iov_buf.h"
27 #include "lib/util/msghdr.h"
31 * This file implements two abstractions: The "unix_dgram" functions implement
32 * queueing for unix domain datagram sockets. You can send to a destination
33 * socket, and if that has no free space available, it will fall back to an
34 * anonymous socket that will poll for writability. "unix_dgram" expects the
35 * data size not to exceed the system limit.
37 * The "unix_msg" functions implement the fragmentation of large messages on
38 * top of "unix_dgram". This is what is exposed to the user of this API.
41 struct unix_dgram_msg {
42 struct unix_dgram_msg *prev, *next;
49 struct unix_dgram_send_queue {
50 struct unix_dgram_send_queue *prev, *next;
51 struct unix_dgram_ctx *ctx;
53 struct unix_dgram_msg *msgs;
57 struct unix_dgram_ctx {
60 const struct poll_funcs *ev_funcs;
63 void (*recv_callback)(struct unix_dgram_ctx *ctx,
64 uint8_t *msg, size_t msg_len,
65 int *fds, size_t num_fds,
69 struct poll_watch *sock_read_watch;
70 struct unix_dgram_send_queue *send_queues;
72 struct pthreadpool_pipe *send_pool;
73 struct poll_watch *pool_read_watch;
79 static void unix_dgram_recv_handler(struct poll_watch *w, int fd, short events,
82 /* Set socket non blocking. */
83 static int prepare_socket_nonblock(int sock, bool nonblock)
87 #define FLAG_TO_SET O_NONBLOCK
90 #define FLAG_TO_SET O_NDELAY
92 #define FLAG_TO_SET FNDELAY
96 flags = fcntl(sock, F_GETFL);
101 flags |= FLAG_TO_SET;
103 flags &= ~FLAG_TO_SET;
105 if (fcntl(sock, F_SETFL, flags) == -1) {
113 /* Set socket close on exec. */
114 static int prepare_socket_cloexec(int sock)
119 flags = fcntl(sock, F_GETFD, 0);
124 if (fcntl(sock, F_SETFD, flags) == -1) {
131 /* Set socket non blocking and close on exec. */
132 static int prepare_socket(int sock)
134 int ret = prepare_socket_nonblock(sock, true);
139 return prepare_socket_cloexec(sock);
142 static size_t unix_dgram_msg_size(void)
144 size_t msgsize = sizeof(struct unix_dgram_msg);
145 msgsize = (msgsize + 15) & ~15; /* align to 16 */
149 static struct msghdr_buf *unix_dgram_msghdr(struct unix_dgram_msg *msg)
152 * Not portable in C99, but "msg" is aligned and so is
153 * unix_dgram_msg_size()
155 return (struct msghdr_buf *)(((char *)msg) + unix_dgram_msg_size());
158 static void close_fd_array(int *fds, size_t num_fds)
162 for (i = 0; i < num_fds; i++) {
172 static void close_fd_array_dgram_msg(struct unix_dgram_msg *dmsg)
174 struct msghdr_buf *hdr = unix_dgram_msghdr(dmsg);
175 struct msghdr *msg = msghdr_buf_msghdr(hdr);
176 size_t num_fds = msghdr_extract_fds(msg, NULL, 0);
179 msghdr_extract_fds(msg, fds, num_fds);
181 close_fd_array(fds, num_fds);
184 static int unix_dgram_init(const struct sockaddr_un *addr, size_t max_msg,
185 const struct poll_funcs *ev_funcs,
186 void (*recv_callback)(struct unix_dgram_ctx *ctx,
187 uint8_t *msg, size_t msg_len,
188 int *fds, size_t num_fds,
191 struct unix_dgram_ctx **result)
193 struct unix_dgram_ctx *ctx;
198 pathlen = strlen(addr->sun_path)+1;
203 ctx = malloc(offsetof(struct unix_dgram_ctx, path) + pathlen);
208 memcpy(ctx->path, addr->sun_path, pathlen);
213 *ctx = (struct unix_dgram_ctx) {
215 .ev_funcs = ev_funcs,
216 .recv_callback = recv_callback,
217 .private_data = private_data,
218 .created_pid = (pid_t)-1
221 ctx->recv_buf = malloc(max_msg);
222 if (ctx->recv_buf == NULL) {
227 ctx->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
228 if (ctx->sock == -1) {
233 /* Set non-blocking and close-on-exec. */
234 ret = prepare_socket(ctx->sock);
240 ret = bind(ctx->sock,
241 (const struct sockaddr *)(const void *)addr,
248 ctx->created_pid = getpid();
250 ctx->sock_read_watch = ctx->ev_funcs->watch_new(
251 ctx->ev_funcs, ctx->sock, POLLIN,
252 unix_dgram_recv_handler, ctx);
254 if (ctx->sock_read_watch == NULL) {
271 static void unix_dgram_recv_handler(struct poll_watch *w, int fd, short events,
274 struct unix_dgram_ctx *ctx = (struct unix_dgram_ctx *)private_data;
279 size_t bufsize = msghdr_prep_recv_fds(NULL, NULL, 0, INT8_MAX);
280 uint8_t buf[bufsize];
282 iov = (struct iovec) {
283 .iov_base = (void *)ctx->recv_buf,
284 .iov_len = ctx->max_msg,
287 msg = (struct msghdr) {
292 msghdr_prep_recv_fds(&msg, buf, bufsize, INT8_MAX);
294 #ifdef MSG_CMSG_CLOEXEC
295 flags |= MSG_CMSG_CLOEXEC;
298 received = recvmsg(fd, &msg, flags);
299 if (received == -1) {
300 if ((errno == EAGAIN) ||
301 (errno == EWOULDBLOCK) ||
302 (errno == EINTR) || (errno == ENOMEM)) {
303 /* Not really an error - just try again. */
306 /* Problem with the socket. Set it unreadable. */
307 ctx->ev_funcs->watch_update(w, 0);
310 if (received > ctx->max_msg) {
311 /* More than we expected, not for us */
316 size_t num_fds = msghdr_extract_fds(&msg, NULL, 0);
320 msghdr_extract_fds(&msg, fds, num_fds);
322 for (i = 0; i < num_fds; i++) {
325 err = prepare_socket_cloexec(fds[i]);
327 close_fd_array(fds, num_fds);
332 ctx->recv_callback(ctx, ctx->recv_buf, received,
333 fds, num_fds, ctx->private_data);
337 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
340 static int unix_dgram_init_pthreadpool(struct unix_dgram_ctx *ctx)
344 if (ctx->send_pool != NULL) {
348 ret = pthreadpool_pipe_init(0, &ctx->send_pool);
353 signalfd = pthreadpool_pipe_signal_fd(ctx->send_pool);
355 ctx->pool_read_watch = ctx->ev_funcs->watch_new(
356 ctx->ev_funcs, signalfd, POLLIN,
357 unix_dgram_job_finished, ctx);
358 if (ctx->pool_read_watch == NULL) {
359 pthreadpool_pipe_destroy(ctx->send_pool);
360 ctx->send_pool = NULL;
367 static int unix_dgram_send_queue_init(
368 struct unix_dgram_ctx *ctx, const struct sockaddr_un *dst,
369 struct unix_dgram_send_queue **result)
371 struct unix_dgram_send_queue *q;
375 pathlen = strlen(dst->sun_path)+1;
377 q = malloc(offsetof(struct unix_dgram_send_queue, path) + pathlen);
383 memcpy(q->path, dst->sun_path, pathlen);
385 q->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
391 err = prepare_socket_cloexec(q->sock);
397 ret = connect(q->sock,
398 (const struct sockaddr *)(const void *)dst,
400 } while ((ret == -1) && (errno == EINTR));
407 err = unix_dgram_init_pthreadpool(ctx);
412 DLIST_ADD(ctx->send_queues, q);
424 static void unix_dgram_send_queue_free(struct unix_dgram_send_queue *q)
426 struct unix_dgram_ctx *ctx = q->ctx;
428 while (q->msgs != NULL) {
429 struct unix_dgram_msg *msg;
431 DLIST_REMOVE(q->msgs, msg);
432 close_fd_array_dgram_msg(msg);
436 DLIST_REMOVE(ctx->send_queues, q);
440 static int find_send_queue(struct unix_dgram_ctx *ctx,
441 const struct sockaddr_un *dst,
442 struct unix_dgram_send_queue **ps)
444 struct unix_dgram_send_queue *s;
446 for (s = ctx->send_queues; s != NULL; s = s->next) {
447 if (strcmp(s->path, dst->sun_path) == 0) {
455 static int queue_msg(struct unix_dgram_send_queue *q,
456 const struct iovec *iov, int iovcnt,
457 const int *fds, size_t num_fds)
459 struct unix_dgram_msg *msg;
460 struct msghdr_buf *hdr;
461 size_t msglen, needed;
463 int fds_copy[MIN(num_fds, INT8_MAX)];
466 for (i=0; i<num_fds; i++) {
470 for (i = 0; i < num_fds; i++) {
471 fds_copy[i] = dup(fds[i]);
472 if (fds_copy[i] == -1) {
478 msglen = unix_dgram_msg_size();
480 msghdrlen = msghdr_copy(NULL, 0, NULL, 0, iov, iovcnt,
482 if (msghdrlen == -1) {
487 needed = msglen + msghdrlen;
488 if (needed < msglen) {
493 msg = malloc(needed);
498 hdr = unix_dgram_msghdr(msg);
501 msghdr_copy(hdr, msghdrlen, NULL, 0, iov, iovcnt,
504 DLIST_ADD_END(q->msgs, msg);
507 close_fd_array(fds_copy, num_fds);
511 static void unix_dgram_send_job(void *private_data)
513 struct unix_dgram_msg *dmsg = private_data;
516 struct msghdr_buf *hdr = unix_dgram_msghdr(dmsg);
517 struct msghdr *msg = msghdr_buf_msghdr(hdr);
518 dmsg->sent = sendmsg(dmsg->sock, msg, 0);
519 } while ((dmsg->sent == -1) && (errno == EINTR));
521 if (dmsg->sent == -1) {
522 dmsg->sys_errno = errno;
526 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
529 struct unix_dgram_ctx *ctx = private_data;
530 struct unix_dgram_send_queue *q;
531 struct unix_dgram_msg *msg;
534 ret = pthreadpool_pipe_finished_jobs(ctx->send_pool, &job, 1);
539 for (q = ctx->send_queues; q != NULL; q = q->next) {
540 if (job == q->sock) {
546 /* Huh? Should not happen */
551 DLIST_REMOVE(q->msgs, msg);
552 close_fd_array_dgram_msg(msg);
555 if (q->msgs != NULL) {
556 ret = pthreadpool_pipe_add_job(ctx->send_pool, q->sock,
557 unix_dgram_send_job, q->msgs);
563 unix_dgram_send_queue_free(q);
566 static int unix_dgram_send(struct unix_dgram_ctx *ctx,
567 const struct sockaddr_un *dst,
568 const struct iovec *iov, int iovlen,
569 const int *fds, size_t num_fds)
571 struct unix_dgram_send_queue *q;
577 if (num_fds > INT8_MAX) {
581 #if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
587 for (i = 0; i < num_fds; i++) {
589 * Make sure we only allow fd passing
590 * for communication channels,
591 * e.g. sockets, pipes, fifos, ...
593 ret = lseek(fds[i], 0, SEEK_CUR);
594 if (ret == -1 && errno == ESPIPE) {
600 * Reject the message as we may need to call dup(),
601 * if we queue the message.
603 * That might result in unexpected behavior for the caller
604 * for files and broken posix locking.
610 * To preserve message ordering, we have to queue a message when
611 * others are waiting in line already.
613 ret = find_send_queue(ctx, dst, &q);
615 return queue_msg(q, iov, iovlen, fds, num_fds);
619 * Try a cheap nonblocking send
622 msg = (struct msghdr) {
623 .msg_name = discard_const_p(struct sockaddr_un, dst),
624 .msg_namelen = sizeof(*dst),
625 .msg_iov = discard_const_p(struct iovec, iov),
629 fdlen = msghdr_prep_fds(&msg, NULL, 0, fds, num_fds);
636 msghdr_prep_fds(&msg, buf, fdlen, fds, num_fds);
638 ret = sendmsg(ctx->sock, &msg, 0);
644 if ((errno != EWOULDBLOCK) &&
647 /* FreeBSD can give this for large messages */
648 (errno != ENOBUFS) &&
654 ret = unix_dgram_send_queue_init(ctx, dst, &q);
658 ret = queue_msg(q, iov, iovlen, fds, num_fds);
660 unix_dgram_send_queue_free(q);
663 ret = pthreadpool_pipe_add_job(ctx->send_pool, q->sock,
664 unix_dgram_send_job, q->msgs);
666 unix_dgram_send_queue_free(q);
672 static int unix_dgram_sock(struct unix_dgram_ctx *ctx)
677 static int unix_dgram_free(struct unix_dgram_ctx *ctx)
679 if (ctx->send_queues != NULL) {
683 if (ctx->send_pool != NULL) {
684 int ret = pthreadpool_pipe_destroy(ctx->send_pool);
688 ctx->ev_funcs->watch_free(ctx->pool_read_watch);
691 ctx->ev_funcs->watch_free(ctx->sock_read_watch);
694 if (getpid() == ctx->created_pid) {
695 /* If we created it, unlink. Otherwise someone else might
696 * still have it open */
706 * Every message starts with a uint64_t cookie.
708 * A value of 0 indicates a single-fragment message which is complete in
709 * itself. The data immediately follows the cookie.
711 * Every multi-fragment message has a cookie != 0 and starts with a cookie
712 * followed by a struct unix_msg_header and then the data. The pid and sock
713 * fields are used to assure uniqueness on the receiver side.
716 struct unix_msg_hdr {
723 struct unix_msg *prev, *next;
732 struct unix_msg_ctx {
733 struct unix_dgram_ctx *dgram;
737 void (*recv_callback)(struct unix_msg_ctx *ctx,
738 uint8_t *msg, size_t msg_len,
739 int *fds, size_t num_fds,
743 struct unix_msg *msgs;
746 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
747 uint8_t *buf, size_t buflen,
748 int *fds, size_t num_fds,
751 int unix_msg_init(const struct sockaddr_un *addr,
752 const struct poll_funcs *ev_funcs,
754 void (*recv_callback)(struct unix_msg_ctx *ctx,
755 uint8_t *msg, size_t msg_len,
756 int *fds, size_t num_fds,
759 struct unix_msg_ctx **result)
761 struct unix_msg_ctx *ctx;
764 ctx = malloc(sizeof(*ctx));
769 *ctx = (struct unix_msg_ctx) {
770 .fragment_len = fragment_len,
772 .recv_callback = recv_callback,
773 .private_data = private_data
776 ret = unix_dgram_init(addr, fragment_len, ev_funcs,
777 unix_msg_recv, ctx, &ctx->dgram);
787 int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
788 const struct iovec *iov, int iovlen,
789 const int *fds, size_t num_fds)
794 struct iovec iov_copy[iovlen+2];
795 struct unix_msg_hdr hdr;
796 struct iovec src_iov;
802 msglen = iov_buflen(iov, iovlen);
807 if (num_fds > INT8_MAX) {
811 if (msglen <= (ctx->fragment_len - sizeof(uint64_t))) {
814 iov_copy[0].iov_base = &cookie;
815 iov_copy[0].iov_len = sizeof(cookie);
817 memcpy(&iov_copy[1], iov,
818 sizeof(struct iovec) * iovlen);
821 return unix_dgram_send(ctx->dgram, dst, iov_copy, iovlen+1,
825 hdr = (struct unix_msg_hdr) {
828 .sock = unix_dgram_sock(ctx->dgram)
831 iov_copy[0].iov_base = &ctx->cookie;
832 iov_copy[0].iov_len = sizeof(ctx->cookie);
833 iov_copy[1].iov_base = &hdr;
834 iov_copy[1].iov_len = sizeof(hdr);
840 * The following write loop sends the user message in pieces. We have
841 * filled the first two iovecs above with "cookie" and "hdr". In the
842 * following loops we pull message chunks from the user iov array and
843 * fill iov_copy piece by piece, possibly truncating chunks from the
844 * caller's iov array. Ugly, but hopefully efficient.
847 while (sent < msglen) {
849 size_t iov_index = 2;
851 fragment_len = sizeof(ctx->cookie) + sizeof(hdr);
853 while (fragment_len < ctx->fragment_len) {
856 space = ctx->fragment_len - fragment_len;
857 chunk = MIN(space, src_iov.iov_len);
859 iov_copy[iov_index].iov_base = src_iov.iov_base;
860 iov_copy[iov_index].iov_len = chunk;
863 src_iov.iov_base = (char *)src_iov.iov_base + chunk;
864 src_iov.iov_len -= chunk;
865 fragment_len += chunk;
867 if (src_iov.iov_len == 0) {
876 sent += (fragment_len - sizeof(ctx->cookie) - sizeof(hdr));
879 * only the last fragment should pass the fd array.
880 * That simplifies the receiver a lot.
883 ret = unix_dgram_send(ctx->dgram, dst,
887 ret = unix_dgram_send(ctx->dgram, dst,
897 if (ctx->cookie == 0) {
904 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
905 uint8_t *buf, size_t buflen,
906 int *fds, size_t num_fds,
909 struct unix_msg_ctx *ctx = (struct unix_msg_ctx *)private_data;
910 struct unix_msg_hdr hdr;
911 struct unix_msg *msg;
915 if (buflen < sizeof(cookie)) {
919 memcpy(&cookie, buf, sizeof(cookie));
921 buf += sizeof(cookie);
922 buflen -= sizeof(cookie);
925 ctx->recv_callback(ctx, buf, buflen, fds, num_fds,
930 if (buflen < sizeof(hdr)) {
933 memcpy(&hdr, buf, sizeof(hdr));
936 buflen -= sizeof(hdr);
938 for (msg = ctx->msgs; msg != NULL; msg = msg->next) {
939 if ((msg->sender_pid == hdr.pid) &&
940 (msg->sender_sock == hdr.sock)) {
945 if ((msg != NULL) && (msg->cookie != cookie)) {
946 DLIST_REMOVE(ctx->msgs, msg);
952 msg = malloc(offsetof(struct unix_msg, buf) + hdr.msglen);
956 *msg = (struct unix_msg) {
957 .msglen = hdr.msglen,
958 .sender_pid = hdr.pid,
959 .sender_sock = hdr.sock,
962 DLIST_ADD(ctx->msgs, msg);
965 space = msg->msglen - msg->received;
966 if (buflen > space) {
970 memcpy(msg->buf + msg->received, buf, buflen);
971 msg->received += buflen;
973 if (msg->received < msg->msglen) {
977 DLIST_REMOVE(ctx->msgs, msg);
978 ctx->recv_callback(ctx, msg->buf, msg->msglen, fds, num_fds,
984 close_fd_array(fds, num_fds);
987 int unix_msg_free(struct unix_msg_ctx *ctx)
991 ret = unix_dgram_free(ctx->dgram);
996 while (ctx->msgs != NULL) {
997 struct unix_msg *msg = ctx->msgs;
998 DLIST_REMOVE(ctx->msgs, msg);