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 "dlinklist.h"
25 #include "pthreadpool/pthreadpool.h"
29 * This file implements two abstractions: The "unix_dgram" functions implement
30 * queueing for unix domain datagram sockets. You can send to a destination
31 * socket, and if that has no free space available, it will fall back to an
32 * anonymous socket that will poll for writability. "unix_dgram" expects the
33 * data size not to exceed the system limit.
35 * The "unix_msg" functions implement the fragmentation of large messages on
36 * top of "unix_dgram". This is what is exposed to the user of this API.
39 struct unix_dgram_msg {
40 struct unix_dgram_msg *prev, *next;
50 struct unix_dgram_send_queue {
51 struct unix_dgram_send_queue *prev, *next;
52 struct unix_dgram_ctx *ctx;
54 struct unix_dgram_msg *msgs;
58 struct unix_dgram_ctx {
61 const struct poll_funcs *ev_funcs;
64 void (*recv_callback)(struct unix_dgram_ctx *ctx,
65 uint8_t *msg, size_t msg_len,
66 int *fds, size_t num_fds,
70 struct poll_watch *sock_read_watch;
71 struct unix_dgram_send_queue *send_queues;
73 struct pthreadpool *send_pool;
74 struct poll_watch *pool_read_watch;
80 static ssize_t iov_buflen(const struct iovec *iov, int iovlen);
81 static void unix_dgram_recv_handler(struct poll_watch *w, int fd, short events,
84 /* Set socket non blocking. */
85 static int prepare_socket_nonblock(int sock)
89 #define FLAG_TO_SET O_NONBLOCK
92 #define FLAG_TO_SET O_NDELAY
94 #define FLAG_TO_SET FNDELAY
98 flags = fcntl(sock, F_GETFL);
102 flags |= FLAG_TO_SET;
103 if (fcntl(sock, F_SETFL, flags) == -1) {
111 /* Set socket close on exec. */
112 static int prepare_socket_cloexec(int sock)
117 flags = fcntl(sock, F_GETFD, 0);
122 if (fcntl(sock, F_SETFD, flags) == -1) {
129 /* Set socket non blocking and close on exec. */
130 static int prepare_socket(int sock)
132 int ret = prepare_socket_nonblock(sock);
137 return prepare_socket_cloexec(sock);
140 static void extract_fd_array_from_msghdr(struct msghdr *msg, int **fds,
143 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
144 struct cmsghdr *cmsg;
146 for(cmsg = CMSG_FIRSTHDR(msg);
148 cmsg = CMSG_NXTHDR(msg, cmsg))
150 void *data = CMSG_DATA(cmsg);
152 if (cmsg->cmsg_type != SCM_RIGHTS) {
155 if (cmsg->cmsg_level != SOL_SOCKET) {
160 *num_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof (int);
166 static void close_fd_array(int *fds, size_t num_fds)
170 for (i = 0; i < num_fds; i++) {
180 static void close_fd_array_cmsg(struct msghdr *msg)
185 extract_fd_array_from_msghdr(msg, &fds, &num_fds);
188 * TODO: caveat - side-effect - changing msg ???
190 close_fd_array(fds, num_fds);
193 static int unix_dgram_init(const struct sockaddr_un *addr, size_t max_msg,
194 const struct poll_funcs *ev_funcs,
195 void (*recv_callback)(struct unix_dgram_ctx *ctx,
196 uint8_t *msg, size_t msg_len,
197 int *fds, size_t num_fds,
200 struct unix_dgram_ctx **result)
202 struct unix_dgram_ctx *ctx;
207 pathlen = strlen(addr->sun_path)+1;
212 ctx = malloc(offsetof(struct unix_dgram_ctx, path) + pathlen);
217 memcpy(ctx->path, addr->sun_path, pathlen);
222 *ctx = (struct unix_dgram_ctx) {
224 .ev_funcs = ev_funcs,
225 .recv_callback = recv_callback,
226 .private_data = private_data,
227 .created_pid = (pid_t)-1
230 ctx->recv_buf = malloc(max_msg);
231 if (ctx->recv_buf == NULL) {
236 ctx->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
237 if (ctx->sock == -1) {
242 /* Set non-blocking and close-on-exec. */
243 ret = prepare_socket(ctx->sock);
249 ret = bind(ctx->sock,
250 (const struct sockaddr *)(const void *)addr,
257 ctx->created_pid = getpid();
259 ctx->sock_read_watch = ctx->ev_funcs->watch_new(
260 ctx->ev_funcs, ctx->sock, POLLIN,
261 unix_dgram_recv_handler, ctx);
263 if (ctx->sock_read_watch == NULL) {
280 static void unix_dgram_recv_handler(struct poll_watch *w, int fd, short events,
283 struct unix_dgram_ctx *ctx = (struct unix_dgram_ctx *)private_data;
288 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
289 char buf[CMSG_SPACE(sizeof(int)*INT8_MAX)] = { 0, };
290 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
292 size_t i, num_fds = 0;
294 iov = (struct iovec) {
295 .iov_base = (void *)ctx->recv_buf,
296 .iov_len = ctx->max_msg,
299 msg = (struct msghdr) {
302 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
304 .msg_controllen = sizeof(buf),
308 #ifdef MSG_CMSG_CLOEXEC
309 flags |= MSG_CMSG_CLOEXEC;
312 received = recvmsg(fd, &msg, flags);
313 if (received == -1) {
314 if ((errno == EAGAIN) ||
315 (errno == EWOULDBLOCK) ||
316 (errno == EINTR) || (errno == ENOMEM)) {
317 /* Not really an error - just try again. */
320 /* Problem with the socket. Set it unreadable. */
321 ctx->ev_funcs->watch_update(w, 0);
324 if (received > ctx->max_msg) {
325 /* More than we expected, not for us */
329 extract_fd_array_from_msghdr(&msg, &fds, &num_fds);
331 for (i = 0; i < num_fds; i++) {
334 err = prepare_socket_cloexec(fds[i]);
340 ctx->recv_callback(ctx, ctx->recv_buf, received,
341 fds, num_fds, ctx->private_data);
344 * Close those fds that the callback has not set to -1.
346 close_fd_array(fds, num_fds);
351 close_fd_array(fds, num_fds);
353 ctx->recv_callback(ctx, ctx->recv_buf, received,
354 NULL, 0, ctx->private_data);
357 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
360 static int unix_dgram_init_pthreadpool(struct unix_dgram_ctx *ctx)
364 if (ctx->send_pool != NULL) {
368 ret = pthreadpool_init(0, &ctx->send_pool);
373 signalfd = pthreadpool_signal_fd(ctx->send_pool);
375 ctx->pool_read_watch = ctx->ev_funcs->watch_new(
376 ctx->ev_funcs, signalfd, POLLIN,
377 unix_dgram_job_finished, ctx);
378 if (ctx->pool_read_watch == NULL) {
379 pthreadpool_destroy(ctx->send_pool);
380 ctx->send_pool = NULL;
387 static int unix_dgram_send_queue_init(
388 struct unix_dgram_ctx *ctx, const struct sockaddr_un *dst,
389 struct unix_dgram_send_queue **result)
391 struct unix_dgram_send_queue *q;
395 pathlen = strlen(dst->sun_path)+1;
397 q = malloc(offsetof(struct unix_dgram_send_queue, path) + pathlen);
403 memcpy(q->path, dst->sun_path, pathlen);
405 q->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
411 err = prepare_socket_cloexec(q->sock);
417 ret = connect(q->sock,
418 (const struct sockaddr *)(const void *)dst,
420 } while ((ret == -1) && (errno == EINTR));
427 err = unix_dgram_init_pthreadpool(ctx);
432 DLIST_ADD(ctx->send_queues, q);
444 static void unix_dgram_send_queue_free(struct unix_dgram_send_queue *q)
446 struct unix_dgram_ctx *ctx = q->ctx;
448 while (q->msgs != NULL) {
449 struct unix_dgram_msg *msg;
451 DLIST_REMOVE(q->msgs, msg);
452 close_fd_array_cmsg(&msg->msg);
456 DLIST_REMOVE(ctx->send_queues, q);
460 static struct unix_dgram_send_queue *find_send_queue(
461 struct unix_dgram_ctx *ctx, const char *dst_sock)
463 struct unix_dgram_send_queue *s;
465 for (s = ctx->send_queues; s != NULL; s = s->next) {
466 if (strcmp(s->path, dst_sock) == 0) {
473 static int queue_msg(struct unix_dgram_send_queue *q,
474 const struct iovec *iov, int iovlen,
475 const int *fds, size_t num_fds)
477 struct unix_dgram_msg *msg;
484 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
485 size_t fds_size = sizeof(int) * MIN(num_fds, INT8_MAX);
486 int fds_copy[MIN(num_fds, INT8_MAX)];
487 size_t cmsg_len = CMSG_LEN(fds_size);
488 size_t cmsg_space = CMSG_SPACE(fds_size);
490 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
492 if (num_fds > INT8_MAX) {
496 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
502 msglen = sizeof(struct unix_dgram_msg);
505 * Note: No need to check for overflow here,
506 * since cmsg will store <= INT8_MAX fds.
508 msglen += cmsg_space;
510 data_len = iov_buflen(iov, iovlen);
511 if (data_len == -1) {
515 tmp = msglen + data_len;
516 if ((tmp < msglen) || (tmp < data_len)) {
522 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
523 for (i = 0; i < num_fds; i++) {
527 for (i = 0; i < num_fds; i++) {
528 fds_copy[i] = dup(fds[i]);
529 if (fds_copy[i] == -1) {
536 msg = malloc(msglen);
543 msg->num_fds = num_fds;
545 data_buf = (uint8_t *)(msg + 1);
547 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
549 cmsg_buf = (char *)data_buf;
550 memset(cmsg_buf, 0, cmsg_space);
551 data_buf += cmsg_space;
558 msg->iov = (struct iovec) {
559 .iov_base = (void *)data_buf,
563 msg->msg = (struct msghdr) {
564 .msg_iov = &msg->iov,
566 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
567 .msg_control = cmsg_buf,
568 .msg_controllen = cmsg_space,
572 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
574 struct cmsghdr *cmsg;
577 cmsg = CMSG_FIRSTHDR(&msg->msg);
578 cmsg->cmsg_level = SOL_SOCKET;
579 cmsg->cmsg_type = SCM_RIGHTS;
580 cmsg->cmsg_len = cmsg_len;
581 fdptr = CMSG_DATA(cmsg);
582 memcpy(fdptr, fds_copy, fds_size);
583 msg->msg.msg_controllen = cmsg->cmsg_len;
585 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
587 for (i=0; i<iovlen; i++) {
588 memcpy(data_buf, iov[i].iov_base, iov[i].iov_len);
589 data_buf += iov[i].iov_len;
592 DLIST_ADD_END(q->msgs, msg, struct unix_dgram_msg);
596 close_fd_array(fds_copy, num_fds);
600 static void unix_dgram_send_job(void *private_data)
602 struct unix_dgram_msg *dmsg = private_data;
605 dmsg->sent = sendmsg(dmsg->sock, &dmsg->msg, 0);
606 } while ((dmsg->sent == -1) && (errno == EINTR));
608 if (dmsg->sent == -1) {
609 dmsg->sys_errno = errno;
613 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
616 struct unix_dgram_ctx *ctx = private_data;
617 struct unix_dgram_send_queue *q;
618 struct unix_dgram_msg *msg;
621 ret = pthreadpool_finished_jobs(ctx->send_pool, &job, 1);
626 for (q = ctx->send_queues; q != NULL; q = q->next) {
627 if (job == q->sock) {
633 /* Huh? Should not happen */
638 DLIST_REMOVE(q->msgs, msg);
639 close_fd_array_cmsg(&msg->msg);
642 if (q->msgs != NULL) {
643 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
644 unix_dgram_send_job, q->msgs);
650 unix_dgram_send_queue_free(q);
653 static int unix_dgram_send(struct unix_dgram_ctx *ctx,
654 const struct sockaddr_un *dst,
655 const struct iovec *iov, int iovlen,
656 const int *fds, size_t num_fds)
658 struct unix_dgram_send_queue *q;
660 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
661 struct cmsghdr *cmsg;
662 size_t fds_size = sizeof(int) * num_fds;
663 size_t cmsg_len = CMSG_LEN(fds_size);
664 size_t cmsg_space = CMSG_SPACE(fds_size);
665 char cmsg_buf[cmsg_space];
666 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
670 if (num_fds > INT8_MAX) {
674 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
678 #endif /* ! HAVE_STRUCT_MSGHDR_MSG_CONTROL */
680 for (i = 0; i < num_fds; i++) {
682 * Make sure we only allow fd passing
683 * for communication channels,
684 * e.g. sockets, pipes, fifos, ...
686 ret = lseek(fds[i], 0, SEEK_CUR);
687 if (ret == -1 && errno == ESPIPE) {
693 * Reject the message as we may need to call dup(),
694 * if we queue the message.
696 * That might result in unexpected behavior for the caller
697 * for files and broken posix locking.
703 * To preserve message ordering, we have to queue a message when
704 * others are waiting in line already.
706 q = find_send_queue(ctx, dst->sun_path);
708 return queue_msg(q, iov, iovlen, fds, num_fds);
712 * Try a cheap nonblocking send
715 msg = (struct msghdr) {
716 .msg_name = discard_const_p(struct sockaddr_un, dst),
717 .msg_namelen = sizeof(*dst),
718 .msg_iov = discard_const_p(struct iovec, iov),
721 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
725 memset(cmsg_buf, 0, cmsg_space);
727 msg.msg_control = cmsg_buf;
728 msg.msg_controllen = cmsg_space;
729 cmsg = CMSG_FIRSTHDR(&msg);
730 cmsg->cmsg_level = SOL_SOCKET;
731 cmsg->cmsg_type = SCM_RIGHTS;
732 cmsg->cmsg_len = cmsg_len;
733 fdptr = CMSG_DATA(cmsg);
734 memcpy(fdptr, fds, fds_size);
735 msg.msg_controllen = cmsg->cmsg_len;
737 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
739 ret = sendmsg(ctx->sock, &msg, 0);
743 if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EINTR)) {
747 ret = unix_dgram_send_queue_init(ctx, dst, &q);
751 ret = queue_msg(q, iov, iovlen, fds, num_fds);
753 unix_dgram_send_queue_free(q);
756 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
757 unix_dgram_send_job, q->msgs);
759 unix_dgram_send_queue_free(q);
765 static int unix_dgram_sock(struct unix_dgram_ctx *ctx)
770 static int unix_dgram_free(struct unix_dgram_ctx *ctx)
772 if (ctx->send_queues != NULL) {
776 if (ctx->send_pool != NULL) {
777 int ret = pthreadpool_destroy(ctx->send_pool);
781 ctx->ev_funcs->watch_free(ctx->pool_read_watch);
784 ctx->ev_funcs->watch_free(ctx->sock_read_watch);
786 if (getpid() == ctx->created_pid) {
787 /* If we created it, unlink. Otherwise someone else might
788 * still have it open */
799 * Every message starts with a uint64_t cookie.
801 * A value of 0 indicates a single-fragment message which is complete in
802 * itself. The data immediately follows the cookie.
804 * Every multi-fragment message has a cookie != 0 and starts with a cookie
805 * followed by a struct unix_msg_header and then the data. The pid and sock
806 * fields are used to assure uniqueness on the receiver side.
809 struct unix_msg_hdr {
816 struct unix_msg *prev, *next;
825 struct unix_msg_ctx {
826 struct unix_dgram_ctx *dgram;
830 void (*recv_callback)(struct unix_msg_ctx *ctx,
831 uint8_t *msg, size_t msg_len,
832 int *fds, size_t num_fds,
836 struct unix_msg *msgs;
839 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
840 uint8_t *buf, size_t buflen,
841 int *fds, size_t num_fds,
844 int unix_msg_init(const struct sockaddr_un *addr,
845 const struct poll_funcs *ev_funcs,
846 size_t fragment_len, uint64_t cookie,
847 void (*recv_callback)(struct unix_msg_ctx *ctx,
848 uint8_t *msg, size_t msg_len,
849 int *fds, size_t num_fds,
852 struct unix_msg_ctx **result)
854 struct unix_msg_ctx *ctx;
857 ctx = malloc(sizeof(*ctx));
862 *ctx = (struct unix_msg_ctx) {
863 .fragment_len = fragment_len,
865 .recv_callback = recv_callback,
866 .private_data = private_data
869 ret = unix_dgram_init(addr, fragment_len, ev_funcs,
870 unix_msg_recv, ctx, &ctx->dgram);
880 int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
881 const struct iovec *iov, int iovlen,
882 const int *fds, size_t num_fds)
887 struct iovec iov_copy[iovlen+2];
888 struct unix_msg_hdr hdr;
889 struct iovec src_iov;
895 msglen = iov_buflen(iov, iovlen);
900 #ifndef HAVE_STRUCT_MSGHDR_MSG_CONTROL
904 #endif /* ! HAVE_STRUCT_MSGHDR_MSG_CONTROL */
906 if (num_fds > INT8_MAX) {
910 if (msglen <= (ctx->fragment_len - sizeof(uint64_t))) {
913 iov_copy[0].iov_base = &cookie;
914 iov_copy[0].iov_len = sizeof(cookie);
916 memcpy(&iov_copy[1], iov,
917 sizeof(struct iovec) * iovlen);
920 return unix_dgram_send(ctx->dgram, dst, iov_copy, iovlen+1,
924 hdr = (struct unix_msg_hdr) {
927 .sock = unix_dgram_sock(ctx->dgram)
930 iov_copy[0].iov_base = &ctx->cookie;
931 iov_copy[0].iov_len = sizeof(ctx->cookie);
932 iov_copy[1].iov_base = &hdr;
933 iov_copy[1].iov_len = sizeof(hdr);
939 * The following write loop sends the user message in pieces. We have
940 * filled the first two iovecs above with "cookie" and "hdr". In the
941 * following loops we pull message chunks from the user iov array and
942 * fill iov_copy piece by piece, possibly truncating chunks from the
943 * caller's iov array. Ugly, but hopefully efficient.
946 while (sent < msglen) {
948 size_t iov_index = 2;
950 fragment_len = sizeof(ctx->cookie) + sizeof(hdr);
952 while (fragment_len < ctx->fragment_len) {
955 space = ctx->fragment_len - fragment_len;
956 chunk = MIN(space, src_iov.iov_len);
958 iov_copy[iov_index].iov_base = src_iov.iov_base;
959 iov_copy[iov_index].iov_len = chunk;
962 src_iov.iov_base = (char *)src_iov.iov_base + chunk;
963 src_iov.iov_len -= chunk;
964 fragment_len += chunk;
966 if (src_iov.iov_len == 0) {
975 sent += (fragment_len - sizeof(ctx->cookie) - sizeof(hdr));
978 * only the last fragment should pass the fd array.
979 * That simplifies the receiver a lot.
982 ret = unix_dgram_send(ctx->dgram, dst,
986 ret = unix_dgram_send(ctx->dgram, dst,
996 if (ctx->cookie == 0) {
1003 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
1004 uint8_t *buf, size_t buflen,
1005 int *fds, size_t num_fds,
1008 struct unix_msg_ctx *ctx = (struct unix_msg_ctx *)private_data;
1009 struct unix_msg_hdr hdr;
1010 struct unix_msg *msg;
1014 if (buflen < sizeof(cookie)) {
1018 memcpy(&cookie, buf, sizeof(cookie));
1020 buf += sizeof(cookie);
1021 buflen -= sizeof(cookie);
1024 ctx->recv_callback(ctx, buf, buflen, fds, num_fds, ctx->private_data);
1028 if (buflen < sizeof(hdr)) {
1031 memcpy(&hdr, buf, sizeof(hdr));
1034 buflen -= sizeof(hdr);
1036 for (msg = ctx->msgs; msg != NULL; msg = msg->next) {
1037 if ((msg->sender_pid == hdr.pid) &&
1038 (msg->sender_sock == hdr.sock)) {
1043 if ((msg != NULL) && (msg->cookie != cookie)) {
1044 DLIST_REMOVE(ctx->msgs, msg);
1050 msg = malloc(offsetof(struct unix_msg, buf) + hdr.msglen);
1054 *msg = (struct unix_msg) {
1055 .msglen = hdr.msglen,
1056 .sender_pid = hdr.pid,
1057 .sender_sock = hdr.sock,
1060 DLIST_ADD(ctx->msgs, msg);
1063 space = msg->msglen - msg->received;
1064 if (buflen > space) {
1068 memcpy(msg->buf + msg->received, buf, buflen);
1069 msg->received += buflen;
1071 if (msg->received < msg->msglen) {
1075 DLIST_REMOVE(ctx->msgs, msg);
1076 ctx->recv_callback(ctx, msg->buf, msg->msglen, fds, num_fds, ctx->private_data);
1081 close_fd_array(fds, num_fds);
1084 int unix_msg_free(struct unix_msg_ctx *ctx)
1088 ret = unix_dgram_free(ctx->dgram);
1093 while (ctx->msgs != NULL) {
1094 struct unix_msg *msg = ctx->msgs;
1095 DLIST_REMOVE(ctx->msgs, msg);
1103 static ssize_t iov_buflen(const struct iovec *iov, int iovlen)
1108 for (i=0; i<iovlen; i++) {
1109 size_t thislen = iov[i].iov_len;
1110 size_t tmp = buflen + thislen;
1112 if ((tmp < buflen) || (tmp < thislen)) {