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;
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 *send_pool;
73 struct poll_watch *pool_read_watch;
79 static ssize_t iov_buflen(const struct iovec *iov, int iovlen);
80 static void unix_dgram_recv_handler(struct poll_watch *w, int fd, short events,
83 /* Set socket non blocking. */
84 static int prepare_socket_nonblock(int sock)
88 #define FLAG_TO_SET O_NONBLOCK
91 #define FLAG_TO_SET O_NDELAY
93 #define FLAG_TO_SET FNDELAY
97 flags = fcntl(sock, F_GETFL);
101 flags |= FLAG_TO_SET;
102 if (fcntl(sock, F_SETFL, flags) == -1) {
110 /* Set socket close on exec. */
111 static int prepare_socket_cloexec(int sock)
116 flags = fcntl(sock, F_GETFD, 0);
121 if (fcntl(sock, F_SETFD, flags) == -1) {
128 /* Set socket non blocking and close on exec. */
129 static int prepare_socket(int sock)
131 int ret = prepare_socket_nonblock(sock);
136 return prepare_socket_cloexec(sock);
139 static void close_fd_array(int *fds, size_t num_fds)
143 for (i = 0; i < num_fds; i++) {
153 static int unix_dgram_init(const struct sockaddr_un *addr, size_t max_msg,
154 const struct poll_funcs *ev_funcs,
155 void (*recv_callback)(struct unix_dgram_ctx *ctx,
156 uint8_t *msg, size_t msg_len,
157 int *fds, size_t num_fds,
160 struct unix_dgram_ctx **result)
162 struct unix_dgram_ctx *ctx;
167 pathlen = strlen(addr->sun_path)+1;
172 ctx = malloc(offsetof(struct unix_dgram_ctx, path) + pathlen);
177 memcpy(ctx->path, addr->sun_path, pathlen);
182 *ctx = (struct unix_dgram_ctx) {
184 .ev_funcs = ev_funcs,
185 .recv_callback = recv_callback,
186 .private_data = private_data,
187 .created_pid = (pid_t)-1
190 ctx->recv_buf = malloc(max_msg);
191 if (ctx->recv_buf == NULL) {
196 ctx->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
197 if (ctx->sock == -1) {
202 /* Set non-blocking and close-on-exec. */
203 ret = prepare_socket(ctx->sock);
209 ret = bind(ctx->sock,
210 (const struct sockaddr *)(const void *)addr,
217 ctx->created_pid = getpid();
219 ctx->sock_read_watch = ctx->ev_funcs->watch_new(
220 ctx->ev_funcs, ctx->sock, POLLIN,
221 unix_dgram_recv_handler, ctx);
223 if (ctx->sock_read_watch == NULL) {
240 static void unix_dgram_recv_handler(struct poll_watch *w, int fd, short events,
243 struct unix_dgram_ctx *ctx = (struct unix_dgram_ctx *)private_data;
248 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
249 char buf[CMSG_SPACE(sizeof(int)*INT8_MAX)] = { 0, };
250 struct cmsghdr *cmsg;
251 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
253 size_t i, num_fds = 0;
255 iov = (struct iovec) {
256 .iov_base = (void *)ctx->recv_buf,
257 .iov_len = ctx->max_msg,
260 msg = (struct msghdr) {
263 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
265 .msg_controllen = sizeof(buf),
269 #ifdef MSG_CMSG_CLOEXEC
270 flags |= MSG_CMSG_CLOEXEC;
273 received = recvmsg(fd, &msg, flags);
274 if (received == -1) {
275 if ((errno == EAGAIN) ||
276 (errno == EWOULDBLOCK) ||
277 (errno == EINTR) || (errno == ENOMEM)) {
278 /* Not really an error - just try again. */
281 /* Problem with the socket. Set it unreadable. */
282 ctx->ev_funcs->watch_update(w, 0);
285 if (received > ctx->max_msg) {
286 /* More than we expected, not for us */
290 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
291 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
292 cmsg = CMSG_NXTHDR(&msg, cmsg))
294 void *data = CMSG_DATA(cmsg);
296 if (cmsg->cmsg_type != SCM_RIGHTS) {
299 if (cmsg->cmsg_level != SOL_SOCKET) {
304 num_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof (int);
309 for (i = 0; i < num_fds; i++) {
312 err = prepare_socket_cloexec(fds[i]);
318 ctx->recv_callback(ctx, ctx->recv_buf, received,
319 fds, num_fds, ctx->private_data);
323 close_fd_array(fds, num_fds);
325 ctx->recv_callback(ctx, ctx->recv_buf, received,
326 NULL, 0, ctx->private_data);
329 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
332 static int unix_dgram_init_pthreadpool(struct unix_dgram_ctx *ctx)
336 if (ctx->send_pool != NULL) {
340 ret = pthreadpool_init(0, &ctx->send_pool);
345 signalfd = pthreadpool_signal_fd(ctx->send_pool);
347 ctx->pool_read_watch = ctx->ev_funcs->watch_new(
348 ctx->ev_funcs, signalfd, POLLIN,
349 unix_dgram_job_finished, ctx);
350 if (ctx->pool_read_watch == NULL) {
351 pthreadpool_destroy(ctx->send_pool);
352 ctx->send_pool = NULL;
359 static int unix_dgram_send_queue_init(
360 struct unix_dgram_ctx *ctx, const struct sockaddr_un *dst,
361 struct unix_dgram_send_queue **result)
363 struct unix_dgram_send_queue *q;
367 pathlen = strlen(dst->sun_path)+1;
369 q = malloc(offsetof(struct unix_dgram_send_queue, path) + pathlen);
375 memcpy(q->path, dst->sun_path, pathlen);
377 q->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
383 err = prepare_socket_cloexec(q->sock);
389 ret = connect(q->sock,
390 (const struct sockaddr *)(const void *)dst,
392 } while ((ret == -1) && (errno == EINTR));
399 err = unix_dgram_init_pthreadpool(ctx);
404 DLIST_ADD(ctx->send_queues, q);
416 static void unix_dgram_send_queue_free(struct unix_dgram_send_queue *q)
418 struct unix_dgram_ctx *ctx = q->ctx;
420 while (q->msgs != NULL) {
421 struct unix_dgram_msg *msg;
423 DLIST_REMOVE(q->msgs, msg);
427 DLIST_REMOVE(ctx->send_queues, q);
431 static struct unix_dgram_send_queue *find_send_queue(
432 struct unix_dgram_ctx *ctx, const char *dst_sock)
434 struct unix_dgram_send_queue *s;
436 for (s = ctx->send_queues; s != NULL; s = s->next) {
437 if (strcmp(s->path, dst_sock) == 0) {
444 static int queue_msg(struct unix_dgram_send_queue *q,
445 const struct iovec *iov, int iovlen)
447 struct unix_dgram_msg *msg;
452 buflen = iov_buflen(iov, iovlen);
457 msglen = offsetof(struct unix_dgram_msg, buf) + buflen;
458 if ((msglen < buflen) ||
459 (msglen < offsetof(struct unix_dgram_msg, buf))) {
464 msg = malloc(msglen);
468 msg->buflen = buflen;
472 for (i=0; i<iovlen; i++) {
473 memcpy(&msg->buf[buflen], iov[i].iov_base, iov[i].iov_len);
474 buflen += iov[i].iov_len;
477 DLIST_ADD_END(q->msgs, msg, struct unix_dgram_msg);
481 static void unix_dgram_send_job(void *private_data)
483 struct unix_dgram_msg *dmsg = private_data;
485 .iov_base = (void *)dmsg->buf,
486 .iov_len = dmsg->buflen,
488 struct msghdr msg = {
494 dmsg->sent = sendmsg(dmsg->sock, &msg, 0);
495 } while ((dmsg->sent == -1) && (errno == EINTR));
498 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
501 struct unix_dgram_ctx *ctx = private_data;
502 struct unix_dgram_send_queue *q;
503 struct unix_dgram_msg *msg;
506 ret = pthreadpool_finished_jobs(ctx->send_pool, &job, 1);
511 for (q = ctx->send_queues; q != NULL; q = q->next) {
512 if (job == q->sock) {
518 /* Huh? Should not happen */
523 DLIST_REMOVE(q->msgs, msg);
526 if (q->msgs != NULL) {
527 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
528 unix_dgram_send_job, q->msgs);
534 unix_dgram_send_queue_free(q);
537 static int unix_dgram_send(struct unix_dgram_ctx *ctx,
538 const struct sockaddr_un *dst,
539 const struct iovec *iov, int iovlen)
541 struct unix_dgram_send_queue *q;
546 * To preserve message ordering, we have to queue a message when
547 * others are waiting in line already.
549 q = find_send_queue(ctx, dst->sun_path);
551 return queue_msg(q, iov, iovlen);
555 * Try a cheap nonblocking send
558 msg = (struct msghdr) {
559 .msg_name = discard_const_p(struct sockaddr_un, dst),
560 .msg_namelen = sizeof(*dst),
561 .msg_iov = discard_const_p(struct iovec, iov),
565 ret = sendmsg(ctx->sock, &msg, 0);
569 if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EINTR)) {
573 ret = unix_dgram_send_queue_init(ctx, dst, &q);
577 ret = queue_msg(q, iov, iovlen);
579 unix_dgram_send_queue_free(q);
582 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
583 unix_dgram_send_job, q->msgs);
585 unix_dgram_send_queue_free(q);
591 static int unix_dgram_sock(struct unix_dgram_ctx *ctx)
596 static int unix_dgram_free(struct unix_dgram_ctx *ctx)
598 if (ctx->send_queues != NULL) {
602 if (ctx->send_pool != NULL) {
603 int ret = pthreadpool_destroy(ctx->send_pool);
607 ctx->ev_funcs->watch_free(ctx->pool_read_watch);
610 ctx->ev_funcs->watch_free(ctx->sock_read_watch);
612 if (getpid() == ctx->created_pid) {
613 /* If we created it, unlink. Otherwise someone else might
614 * still have it open */
625 * Every message starts with a uint64_t cookie.
627 * A value of 0 indicates a single-fragment message which is complete in
628 * itself. The data immediately follows the cookie.
630 * Every multi-fragment message has a cookie != 0 and starts with a cookie
631 * followed by a struct unix_msg_header and then the data. The pid and sock
632 * fields are used to assure uniqueness on the receiver side.
635 struct unix_msg_hdr {
642 struct unix_msg *prev, *next;
651 struct unix_msg_ctx {
652 struct unix_dgram_ctx *dgram;
656 void (*recv_callback)(struct unix_msg_ctx *ctx,
657 uint8_t *msg, size_t msg_len,
661 struct unix_msg *msgs;
664 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
665 uint8_t *buf, size_t buflen,
666 int *fds, size_t num_fds,
669 int unix_msg_init(const struct sockaddr_un *addr,
670 const struct poll_funcs *ev_funcs,
671 size_t fragment_len, uint64_t cookie,
672 void (*recv_callback)(struct unix_msg_ctx *ctx,
673 uint8_t *msg, size_t msg_len,
676 struct unix_msg_ctx **result)
678 struct unix_msg_ctx *ctx;
681 ctx = malloc(sizeof(*ctx));
686 *ctx = (struct unix_msg_ctx) {
687 .fragment_len = fragment_len,
689 .recv_callback = recv_callback,
690 .private_data = private_data
693 ret = unix_dgram_init(addr, fragment_len, ev_funcs,
694 unix_msg_recv, ctx, &ctx->dgram);
704 int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
705 const struct iovec *iov, int iovlen)
710 struct iovec iov_copy[iovlen+2];
711 struct unix_msg_hdr hdr;
712 struct iovec src_iov;
718 msglen = iov_buflen(iov, iovlen);
723 if (msglen <= (ctx->fragment_len - sizeof(uint64_t))) {
726 iov_copy[0].iov_base = &cookie;
727 iov_copy[0].iov_len = sizeof(cookie);
729 memcpy(&iov_copy[1], iov,
730 sizeof(struct iovec) * iovlen);
733 return unix_dgram_send(ctx->dgram, dst, iov_copy, iovlen+1);
736 hdr = (struct unix_msg_hdr) {
739 .sock = unix_dgram_sock(ctx->dgram)
742 iov_copy[0].iov_base = &ctx->cookie;
743 iov_copy[0].iov_len = sizeof(ctx->cookie);
744 iov_copy[1].iov_base = &hdr;
745 iov_copy[1].iov_len = sizeof(hdr);
751 * The following write loop sends the user message in pieces. We have
752 * filled the first two iovecs above with "cookie" and "hdr". In the
753 * following loops we pull message chunks from the user iov array and
754 * fill iov_copy piece by piece, possibly truncating chunks from the
755 * caller's iov array. Ugly, but hopefully efficient.
758 while (sent < msglen) {
760 size_t iov_index = 2;
762 fragment_len = sizeof(ctx->cookie) + sizeof(hdr);
764 while (fragment_len < ctx->fragment_len) {
767 space = ctx->fragment_len - fragment_len;
768 chunk = MIN(space, src_iov.iov_len);
770 iov_copy[iov_index].iov_base = src_iov.iov_base;
771 iov_copy[iov_index].iov_len = chunk;
774 src_iov.iov_base = (char *)src_iov.iov_base + chunk;
775 src_iov.iov_len -= chunk;
776 fragment_len += chunk;
778 if (src_iov.iov_len == 0) {
787 sent += (fragment_len - sizeof(ctx->cookie) - sizeof(hdr));
789 ret = unix_dgram_send(ctx->dgram, dst, iov_copy, iov_index);
796 if (ctx->cookie == 0) {
803 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
804 uint8_t *buf, size_t buflen,
805 int *fds, size_t num_fds,
808 struct unix_msg_ctx *ctx = (struct unix_msg_ctx *)private_data;
809 struct unix_msg_hdr hdr;
810 struct unix_msg *msg;
814 /* for now we ignore passed file descriptors */
815 close_fd_array(fds, num_fds);
817 if (buflen < sizeof(cookie)) {
820 memcpy(&cookie, buf, sizeof(cookie));
822 buf += sizeof(cookie);
823 buflen -= sizeof(cookie);
826 ctx->recv_callback(ctx, buf, buflen, ctx->private_data);
830 if (buflen < sizeof(hdr)) {
833 memcpy(&hdr, buf, sizeof(hdr));
836 buflen -= sizeof(hdr);
838 for (msg = ctx->msgs; msg != NULL; msg = msg->next) {
839 if ((msg->sender_pid == hdr.pid) &&
840 (msg->sender_sock == hdr.sock)) {
845 if ((msg != NULL) && (msg->cookie != cookie)) {
846 DLIST_REMOVE(ctx->msgs, msg);
852 msg = malloc(offsetof(struct unix_msg, buf) + hdr.msglen);
856 *msg = (struct unix_msg) {
857 .msglen = hdr.msglen,
858 .sender_pid = hdr.pid,
859 .sender_sock = hdr.sock,
862 DLIST_ADD(ctx->msgs, msg);
865 space = msg->msglen - msg->received;
866 if (buflen > space) {
870 memcpy(msg->buf + msg->received, buf, buflen);
871 msg->received += buflen;
873 if (msg->received < msg->msglen) {
877 DLIST_REMOVE(ctx->msgs, msg);
878 ctx->recv_callback(ctx, msg->buf, msg->msglen, ctx->private_data);
882 int unix_msg_free(struct unix_msg_ctx *ctx)
886 ret = unix_dgram_free(ctx->dgram);
891 while (ctx->msgs != NULL) {
892 struct unix_msg *msg = ctx->msgs;
893 DLIST_REMOVE(ctx->msgs, msg);
901 static ssize_t iov_buflen(const struct iovec *iov, int iovlen)
906 for (i=0; i<iovlen; i++) {
907 size_t thislen = iov[i].iov_len;
908 size_t tmp = buflen + thislen;
910 if ((tmp < buflen) || (tmp < thislen)) {