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,
68 struct poll_watch *sock_read_watch;
69 struct unix_dgram_send_queue *send_queues;
71 struct pthreadpool *send_pool;
72 struct poll_watch *pool_read_watch;
78 static ssize_t iov_buflen(const struct iovec *iov, int iovlen);
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)
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);
100 flags |= FLAG_TO_SET;
101 if (fcntl(sock, F_SETFL, flags) == -1) {
109 /* Set socket close on exec. */
110 static int prepare_socket_cloexec(int sock)
115 flags = fcntl(sock, F_GETFD, 0);
120 if (fcntl(sock, F_SETFD, flags) == -1) {
127 /* Set socket non blocking and close on exec. */
128 static int prepare_socket(int sock)
130 int ret = prepare_socket_nonblock(sock);
135 return prepare_socket_cloexec(sock);
138 static int unix_dgram_init(const struct sockaddr_un *addr, size_t max_msg,
139 const struct poll_funcs *ev_funcs,
140 void (*recv_callback)(struct unix_dgram_ctx *ctx,
141 uint8_t *msg, size_t msg_len,
144 struct unix_dgram_ctx **result)
146 struct unix_dgram_ctx *ctx;
151 pathlen = strlen(addr->sun_path)+1;
156 ctx = malloc(offsetof(struct unix_dgram_ctx, path) + pathlen);
161 memcpy(ctx->path, addr->sun_path, pathlen);
166 *ctx = (struct unix_dgram_ctx) {
168 .ev_funcs = ev_funcs,
169 .recv_callback = recv_callback,
170 .private_data = private_data,
171 .created_pid = (pid_t)-1
174 ctx->recv_buf = malloc(max_msg);
175 if (ctx->recv_buf == NULL) {
180 ctx->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
181 if (ctx->sock == -1) {
186 /* Set non-blocking and close-on-exec. */
187 ret = prepare_socket(ctx->sock);
193 ret = bind(ctx->sock,
194 (const struct sockaddr *)(const void *)addr,
201 ctx->created_pid = getpid();
203 ctx->sock_read_watch = ctx->ev_funcs->watch_new(
204 ctx->ev_funcs, ctx->sock, POLLIN,
205 unix_dgram_recv_handler, ctx);
207 if (ctx->sock_read_watch == NULL) {
224 static void unix_dgram_recv_handler(struct poll_watch *w, int fd, short events,
227 struct unix_dgram_ctx *ctx = (struct unix_dgram_ctx *)private_data;
232 iov = (struct iovec) {
233 .iov_base = (void *)ctx->recv_buf,
234 .iov_len = ctx->max_msg,
237 msg = (struct msghdr) {
240 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
246 received = recvmsg(fd, &msg, 0);
247 if (received == -1) {
248 if ((errno == EAGAIN) ||
250 (errno == EWOULDBLOCK) ||
252 (errno == EINTR) || (errno == ENOMEM)) {
253 /* Not really an error - just try again. */
256 /* Problem with the socket. Set it unreadable. */
257 ctx->ev_funcs->watch_update(w, 0);
260 if (received > ctx->max_msg) {
261 /* More than we expected, not for us */
264 ctx->recv_callback(ctx, ctx->recv_buf, received, ctx->private_data);
267 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
270 static int unix_dgram_init_pthreadpool(struct unix_dgram_ctx *ctx)
274 if (ctx->send_pool != NULL) {
278 ret = pthreadpool_init(0, &ctx->send_pool);
283 signalfd = pthreadpool_signal_fd(ctx->send_pool);
285 ctx->pool_read_watch = ctx->ev_funcs->watch_new(
286 ctx->ev_funcs, signalfd, POLLIN,
287 unix_dgram_job_finished, ctx);
288 if (ctx->pool_read_watch == NULL) {
289 pthreadpool_destroy(ctx->send_pool);
290 ctx->send_pool = NULL;
297 static int unix_dgram_send_queue_init(
298 struct unix_dgram_ctx *ctx, const struct sockaddr_un *dst,
299 struct unix_dgram_send_queue **result)
301 struct unix_dgram_send_queue *q;
305 pathlen = strlen(dst->sun_path)+1;
307 q = malloc(offsetof(struct unix_dgram_send_queue, path) + pathlen);
313 memcpy(q->path, dst->sun_path, pathlen);
315 q->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
321 err = prepare_socket_cloexec(q->sock);
327 ret = connect(q->sock,
328 (const struct sockaddr *)(const void *)dst,
330 } while ((ret == -1) && (errno == EINTR));
337 err = unix_dgram_init_pthreadpool(ctx);
342 DLIST_ADD(ctx->send_queues, q);
354 static void unix_dgram_send_queue_free(struct unix_dgram_send_queue *q)
356 struct unix_dgram_ctx *ctx = q->ctx;
358 while (q->msgs != NULL) {
359 struct unix_dgram_msg *msg;
361 DLIST_REMOVE(q->msgs, msg);
365 DLIST_REMOVE(ctx->send_queues, q);
369 static struct unix_dgram_send_queue *find_send_queue(
370 struct unix_dgram_ctx *ctx, const char *dst_sock)
372 struct unix_dgram_send_queue *s;
374 for (s = ctx->send_queues; s != NULL; s = s->next) {
375 if (strcmp(s->path, dst_sock) == 0) {
382 static int queue_msg(struct unix_dgram_send_queue *q,
383 const struct iovec *iov, int iovlen)
385 struct unix_dgram_msg *msg;
390 buflen = iov_buflen(iov, iovlen);
395 msglen = offsetof(struct unix_dgram_msg, buf) + buflen;
396 if ((msglen < buflen) ||
397 (msglen < offsetof(struct unix_dgram_msg, buf))) {
402 msg = malloc(msglen);
406 msg->buflen = buflen;
410 for (i=0; i<iovlen; i++) {
411 memcpy(&msg->buf[buflen], iov[i].iov_base, iov[i].iov_len);
412 buflen += iov[i].iov_len;
415 DLIST_ADD_END(q->msgs, msg, struct unix_dgram_msg);
419 static void unix_dgram_send_job(void *private_data)
421 struct unix_dgram_msg *msg = private_data;
424 msg->sent = send(msg->sock, msg->buf, msg->buflen, 0);
425 } while ((msg->sent == -1) && (errno == EINTR));
428 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
431 struct unix_dgram_ctx *ctx = private_data;
432 struct unix_dgram_send_queue *q;
433 struct unix_dgram_msg *msg;
436 ret = pthreadpool_finished_jobs(ctx->send_pool, &job, 1);
441 for (q = ctx->send_queues; q != NULL; q = q->next) {
442 if (job == q->sock) {
448 /* Huh? Should not happen */
453 DLIST_REMOVE(q->msgs, msg);
456 if (q->msgs != NULL) {
457 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
458 unix_dgram_send_job, q->msgs);
464 unix_dgram_send_queue_free(q);
467 static int unix_dgram_send(struct unix_dgram_ctx *ctx,
468 const struct sockaddr_un *dst,
469 const struct iovec *iov, int iovlen)
471 struct unix_dgram_send_queue *q;
476 * To preserve message ordering, we have to queue a message when
477 * others are waiting in line already.
479 q = find_send_queue(ctx, dst->sun_path);
481 return queue_msg(q, iov, iovlen);
485 * Try a cheap nonblocking send
488 msg = (struct msghdr) {
489 .msg_name = discard_const_p(struct sockaddr_un, dst),
490 .msg_namelen = sizeof(*dst),
491 .msg_iov = discard_const_p(struct iovec, iov),
495 ret = sendmsg(ctx->sock, &msg, 0);
500 if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EINTR)) {
502 if ((errno != EAGAIN) && (errno != EINTR)) {
507 ret = unix_dgram_send_queue_init(ctx, dst, &q);
511 ret = queue_msg(q, iov, iovlen);
513 unix_dgram_send_queue_free(q);
516 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
517 unix_dgram_send_job, q->msgs);
519 unix_dgram_send_queue_free(q);
525 static int unix_dgram_sock(struct unix_dgram_ctx *ctx)
530 static int unix_dgram_free(struct unix_dgram_ctx *ctx)
532 if (ctx->send_queues != NULL) {
536 if (ctx->send_pool != NULL) {
537 int ret = pthreadpool_destroy(ctx->send_pool);
541 ctx->ev_funcs->watch_free(ctx->pool_read_watch);
544 ctx->ev_funcs->watch_free(ctx->sock_read_watch);
546 if (getpid() == ctx->created_pid) {
547 /* If we created it, unlink. Otherwise someone else might
548 * still have it open */
559 * Every message starts with a uint64_t cookie.
561 * A value of 0 indicates a single-fragment message which is complete in
562 * itself. The data immediately follows the cookie.
564 * Every multi-fragment message has a cookie != 0 and starts with a cookie
565 * followed by a struct unix_msg_header and then the data. The pid and sock
566 * fields are used to assure uniqueness on the receiver side.
569 struct unix_msg_hdr {
576 struct unix_msg *prev, *next;
585 struct unix_msg_ctx {
586 struct unix_dgram_ctx *dgram;
590 void (*recv_callback)(struct unix_msg_ctx *ctx,
591 uint8_t *msg, size_t msg_len,
595 struct unix_msg *msgs;
598 static void unix_msg_recv(struct unix_dgram_ctx *ctx,
599 uint8_t *msg, size_t msg_len,
602 int unix_msg_init(const struct sockaddr_un *addr,
603 const struct poll_funcs *ev_funcs,
604 size_t fragment_len, uint64_t cookie,
605 void (*recv_callback)(struct unix_msg_ctx *ctx,
606 uint8_t *msg, size_t msg_len,
609 struct unix_msg_ctx **result)
611 struct unix_msg_ctx *ctx;
614 ctx = malloc(sizeof(*ctx));
619 *ctx = (struct unix_msg_ctx) {
620 .fragment_len = fragment_len,
622 .recv_callback = recv_callback,
623 .private_data = private_data
626 ret = unix_dgram_init(addr, fragment_len, ev_funcs,
627 unix_msg_recv, ctx, &ctx->dgram);
637 int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
638 const struct iovec *iov, int iovlen)
643 struct iovec *iov_copy;
644 struct unix_msg_hdr hdr;
645 struct iovec src_iov;
651 msglen = iov_buflen(iov, iovlen);
656 if (msglen <= (ctx->fragment_len - sizeof(uint64_t))) {
657 struct iovec tmp_iov[iovlen+1];
660 tmp_iov[0].iov_base = &cookie;
661 tmp_iov[0].iov_len = sizeof(cookie);
663 memcpy(&tmp_iov[1], iov,
664 sizeof(struct iovec) * iovlen);
667 return unix_dgram_send(ctx->dgram, dst, tmp_iov, iovlen+1);
670 hdr = (struct unix_msg_hdr) {
673 .sock = unix_dgram_sock(ctx->dgram)
676 iov_copy = malloc(sizeof(struct iovec) * (iovlen + 2));
677 if (iov_copy == NULL) {
680 iov_copy[0].iov_base = &ctx->cookie;
681 iov_copy[0].iov_len = sizeof(ctx->cookie);
682 iov_copy[1].iov_base = &hdr;
683 iov_copy[1].iov_len = sizeof(hdr);
689 * The following write loop sends the user message in pieces. We have
690 * filled the first two iovecs above with "cookie" and "hdr". In the
691 * following loops we pull message chunks from the user iov array and
692 * fill iov_copy piece by piece, possibly truncating chunks from the
693 * caller's iov array. Ugly, but hopefully efficient.
696 while (sent < msglen) {
698 size_t iov_index = 2;
700 fragment_len = sizeof(ctx->cookie) + sizeof(hdr);
702 while (fragment_len < ctx->fragment_len) {
705 space = ctx->fragment_len - fragment_len;
706 chunk = MIN(space, src_iov.iov_len);
708 iov_copy[iov_index].iov_base = src_iov.iov_base;
709 iov_copy[iov_index].iov_len = chunk;
712 src_iov.iov_base = (char *)src_iov.iov_base + chunk;
713 src_iov.iov_len -= chunk;
714 fragment_len += chunk;
716 if (src_iov.iov_len == 0) {
725 sent += (fragment_len - sizeof(ctx->cookie) - sizeof(hdr));
727 ret = unix_dgram_send(ctx->dgram, dst, iov_copy, iov_index);
736 if (ctx->cookie == 0) {
743 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
744 uint8_t *buf, size_t buflen,
747 struct unix_msg_ctx *ctx = (struct unix_msg_ctx *)private_data;
748 struct unix_msg_hdr hdr;
749 struct unix_msg *msg;
753 if (buflen < sizeof(cookie)) {
756 memcpy(&cookie, buf, sizeof(cookie));
758 buf += sizeof(cookie);
759 buflen -= sizeof(cookie);
762 ctx->recv_callback(ctx, buf, buflen, ctx->private_data);
766 if (buflen < sizeof(hdr)) {
769 memcpy(&hdr, buf, sizeof(hdr));
772 buflen -= sizeof(hdr);
774 for (msg = ctx->msgs; msg != NULL; msg = msg->next) {
775 if ((msg->sender_pid == hdr.pid) &&
776 (msg->sender_sock == hdr.sock)) {
781 if ((msg != NULL) && (msg->cookie != cookie)) {
782 DLIST_REMOVE(ctx->msgs, msg);
788 msg = malloc(offsetof(struct unix_msg, buf) + hdr.msglen);
792 *msg = (struct unix_msg) {
793 .msglen = hdr.msglen,
794 .sender_pid = hdr.pid,
795 .sender_sock = hdr.sock,
798 DLIST_ADD(ctx->msgs, msg);
801 space = msg->msglen - msg->received;
802 if (buflen > space) {
806 memcpy(msg->buf + msg->received, buf, buflen);
807 msg->received += buflen;
809 if (msg->received < msg->msglen) {
813 DLIST_REMOVE(ctx->msgs, msg);
814 ctx->recv_callback(ctx, msg->buf, msg->msglen, ctx->private_data);
818 int unix_msg_free(struct unix_msg_ctx *ctx)
822 ret = unix_dgram_free(ctx->dgram);
827 while (ctx->msgs != NULL) {
828 struct unix_msg *msg = ctx->msgs;
829 DLIST_REMOVE(ctx->msgs, msg);
837 static ssize_t iov_buflen(const struct iovec *iov, int iovlen)
842 for (i=0; i<iovlen; i++) {
843 size_t thislen = iov[i].iov_len;
844 size_t tmp = buflen + thislen;
846 if ((tmp < buflen) || (tmp < thislen)) {