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) ||
249 (errno == EWOULDBLOCK) ||
250 (errno == EINTR) || (errno == ENOMEM)) {
251 /* Not really an error - just try again. */
254 /* Problem with the socket. Set it unreadable. */
255 ctx->ev_funcs->watch_update(w, 0);
258 if (received > ctx->max_msg) {
259 /* More than we expected, not for us */
262 ctx->recv_callback(ctx, ctx->recv_buf, received, ctx->private_data);
265 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
268 static int unix_dgram_init_pthreadpool(struct unix_dgram_ctx *ctx)
272 if (ctx->send_pool != NULL) {
276 ret = pthreadpool_init(0, &ctx->send_pool);
281 signalfd = pthreadpool_signal_fd(ctx->send_pool);
283 ctx->pool_read_watch = ctx->ev_funcs->watch_new(
284 ctx->ev_funcs, signalfd, POLLIN,
285 unix_dgram_job_finished, ctx);
286 if (ctx->pool_read_watch == NULL) {
287 pthreadpool_destroy(ctx->send_pool);
288 ctx->send_pool = NULL;
295 static int unix_dgram_send_queue_init(
296 struct unix_dgram_ctx *ctx, const struct sockaddr_un *dst,
297 struct unix_dgram_send_queue **result)
299 struct unix_dgram_send_queue *q;
303 pathlen = strlen(dst->sun_path)+1;
305 q = malloc(offsetof(struct unix_dgram_send_queue, path) + pathlen);
311 memcpy(q->path, dst->sun_path, pathlen);
313 q->sock = socket(AF_UNIX, SOCK_DGRAM, 0);
319 err = prepare_socket_cloexec(q->sock);
325 ret = connect(q->sock,
326 (const struct sockaddr *)(const void *)dst,
328 } while ((ret == -1) && (errno == EINTR));
335 err = unix_dgram_init_pthreadpool(ctx);
340 DLIST_ADD(ctx->send_queues, q);
352 static void unix_dgram_send_queue_free(struct unix_dgram_send_queue *q)
354 struct unix_dgram_ctx *ctx = q->ctx;
356 while (q->msgs != NULL) {
357 struct unix_dgram_msg *msg;
359 DLIST_REMOVE(q->msgs, msg);
363 DLIST_REMOVE(ctx->send_queues, q);
367 static struct unix_dgram_send_queue *find_send_queue(
368 struct unix_dgram_ctx *ctx, const char *dst_sock)
370 struct unix_dgram_send_queue *s;
372 for (s = ctx->send_queues; s != NULL; s = s->next) {
373 if (strcmp(s->path, dst_sock) == 0) {
380 static int queue_msg(struct unix_dgram_send_queue *q,
381 const struct iovec *iov, int iovlen)
383 struct unix_dgram_msg *msg;
388 buflen = iov_buflen(iov, iovlen);
393 msglen = offsetof(struct unix_dgram_msg, buf) + buflen;
394 if ((msglen < buflen) ||
395 (msglen < offsetof(struct unix_dgram_msg, buf))) {
400 msg = malloc(msglen);
404 msg->buflen = buflen;
408 for (i=0; i<iovlen; i++) {
409 memcpy(&msg->buf[buflen], iov[i].iov_base, iov[i].iov_len);
410 buflen += iov[i].iov_len;
413 DLIST_ADD_END(q->msgs, msg, struct unix_dgram_msg);
417 static void unix_dgram_send_job(void *private_data)
419 struct unix_dgram_msg *msg = private_data;
422 msg->sent = send(msg->sock, msg->buf, msg->buflen, 0);
423 } while ((msg->sent == -1) && (errno == EINTR));
426 static void unix_dgram_job_finished(struct poll_watch *w, int fd, short events,
429 struct unix_dgram_ctx *ctx = private_data;
430 struct unix_dgram_send_queue *q;
431 struct unix_dgram_msg *msg;
434 ret = pthreadpool_finished_jobs(ctx->send_pool, &job, 1);
439 for (q = ctx->send_queues; q != NULL; q = q->next) {
440 if (job == q->sock) {
446 /* Huh? Should not happen */
451 DLIST_REMOVE(q->msgs, msg);
454 if (q->msgs != NULL) {
455 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
456 unix_dgram_send_job, q->msgs);
462 unix_dgram_send_queue_free(q);
465 static int unix_dgram_send(struct unix_dgram_ctx *ctx,
466 const struct sockaddr_un *dst,
467 const struct iovec *iov, int iovlen)
469 struct unix_dgram_send_queue *q;
474 * To preserve message ordering, we have to queue a message when
475 * others are waiting in line already.
477 q = find_send_queue(ctx, dst->sun_path);
479 return queue_msg(q, iov, iovlen);
483 * Try a cheap nonblocking send
486 msg = (struct msghdr) {
487 .msg_name = discard_const_p(struct sockaddr_un, dst),
488 .msg_namelen = sizeof(*dst),
489 .msg_iov = discard_const_p(struct iovec, iov),
493 ret = sendmsg(ctx->sock, &msg, 0);
497 if ((errno != EWOULDBLOCK) && (errno != EAGAIN) && (errno != EINTR)) {
501 ret = unix_dgram_send_queue_init(ctx, dst, &q);
505 ret = queue_msg(q, iov, iovlen);
507 unix_dgram_send_queue_free(q);
510 ret = pthreadpool_add_job(ctx->send_pool, q->sock,
511 unix_dgram_send_job, q->msgs);
513 unix_dgram_send_queue_free(q);
519 static int unix_dgram_sock(struct unix_dgram_ctx *ctx)
524 static int unix_dgram_free(struct unix_dgram_ctx *ctx)
526 if (ctx->send_queues != NULL) {
530 if (ctx->send_pool != NULL) {
531 int ret = pthreadpool_destroy(ctx->send_pool);
535 ctx->ev_funcs->watch_free(ctx->pool_read_watch);
538 ctx->ev_funcs->watch_free(ctx->sock_read_watch);
540 if (getpid() == ctx->created_pid) {
541 /* If we created it, unlink. Otherwise someone else might
542 * still have it open */
553 * Every message starts with a uint64_t cookie.
555 * A value of 0 indicates a single-fragment message which is complete in
556 * itself. The data immediately follows the cookie.
558 * Every multi-fragment message has a cookie != 0 and starts with a cookie
559 * followed by a struct unix_msg_header and then the data. The pid and sock
560 * fields are used to assure uniqueness on the receiver side.
563 struct unix_msg_hdr {
570 struct unix_msg *prev, *next;
579 struct unix_msg_ctx {
580 struct unix_dgram_ctx *dgram;
584 void (*recv_callback)(struct unix_msg_ctx *ctx,
585 uint8_t *msg, size_t msg_len,
589 struct unix_msg *msgs;
592 static void unix_msg_recv(struct unix_dgram_ctx *ctx,
593 uint8_t *msg, size_t msg_len,
596 int unix_msg_init(const struct sockaddr_un *addr,
597 const struct poll_funcs *ev_funcs,
598 size_t fragment_len, uint64_t cookie,
599 void (*recv_callback)(struct unix_msg_ctx *ctx,
600 uint8_t *msg, size_t msg_len,
603 struct unix_msg_ctx **result)
605 struct unix_msg_ctx *ctx;
608 ctx = malloc(sizeof(*ctx));
613 *ctx = (struct unix_msg_ctx) {
614 .fragment_len = fragment_len,
616 .recv_callback = recv_callback,
617 .private_data = private_data
620 ret = unix_dgram_init(addr, fragment_len, ev_funcs,
621 unix_msg_recv, ctx, &ctx->dgram);
631 int unix_msg_send(struct unix_msg_ctx *ctx, const struct sockaddr_un *dst,
632 const struct iovec *iov, int iovlen)
637 struct iovec iov_copy[iovlen+2];
638 struct unix_msg_hdr hdr;
639 struct iovec src_iov;
645 msglen = iov_buflen(iov, iovlen);
650 if (msglen <= (ctx->fragment_len - sizeof(uint64_t))) {
653 iov_copy[0].iov_base = &cookie;
654 iov_copy[0].iov_len = sizeof(cookie);
656 memcpy(&iov_copy[1], iov,
657 sizeof(struct iovec) * iovlen);
660 return unix_dgram_send(ctx->dgram, dst, iov_copy, iovlen+1);
663 hdr = (struct unix_msg_hdr) {
666 .sock = unix_dgram_sock(ctx->dgram)
669 iov_copy[0].iov_base = &ctx->cookie;
670 iov_copy[0].iov_len = sizeof(ctx->cookie);
671 iov_copy[1].iov_base = &hdr;
672 iov_copy[1].iov_len = sizeof(hdr);
678 * The following write loop sends the user message in pieces. We have
679 * filled the first two iovecs above with "cookie" and "hdr". In the
680 * following loops we pull message chunks from the user iov array and
681 * fill iov_copy piece by piece, possibly truncating chunks from the
682 * caller's iov array. Ugly, but hopefully efficient.
685 while (sent < msglen) {
687 size_t iov_index = 2;
689 fragment_len = sizeof(ctx->cookie) + sizeof(hdr);
691 while (fragment_len < ctx->fragment_len) {
694 space = ctx->fragment_len - fragment_len;
695 chunk = MIN(space, src_iov.iov_len);
697 iov_copy[iov_index].iov_base = src_iov.iov_base;
698 iov_copy[iov_index].iov_len = chunk;
701 src_iov.iov_base = (char *)src_iov.iov_base + chunk;
702 src_iov.iov_len -= chunk;
703 fragment_len += chunk;
705 if (src_iov.iov_len == 0) {
714 sent += (fragment_len - sizeof(ctx->cookie) - sizeof(hdr));
716 ret = unix_dgram_send(ctx->dgram, dst, iov_copy, iov_index);
723 if (ctx->cookie == 0) {
730 static void unix_msg_recv(struct unix_dgram_ctx *dgram_ctx,
731 uint8_t *buf, size_t buflen,
734 struct unix_msg_ctx *ctx = (struct unix_msg_ctx *)private_data;
735 struct unix_msg_hdr hdr;
736 struct unix_msg *msg;
740 if (buflen < sizeof(cookie)) {
743 memcpy(&cookie, buf, sizeof(cookie));
745 buf += sizeof(cookie);
746 buflen -= sizeof(cookie);
749 ctx->recv_callback(ctx, buf, buflen, ctx->private_data);
753 if (buflen < sizeof(hdr)) {
756 memcpy(&hdr, buf, sizeof(hdr));
759 buflen -= sizeof(hdr);
761 for (msg = ctx->msgs; msg != NULL; msg = msg->next) {
762 if ((msg->sender_pid == hdr.pid) &&
763 (msg->sender_sock == hdr.sock)) {
768 if ((msg != NULL) && (msg->cookie != cookie)) {
769 DLIST_REMOVE(ctx->msgs, msg);
775 msg = malloc(offsetof(struct unix_msg, buf) + hdr.msglen);
779 *msg = (struct unix_msg) {
780 .msglen = hdr.msglen,
781 .sender_pid = hdr.pid,
782 .sender_sock = hdr.sock,
785 DLIST_ADD(ctx->msgs, msg);
788 space = msg->msglen - msg->received;
789 if (buflen > space) {
793 memcpy(msg->buf + msg->received, buf, buflen);
794 msg->received += buflen;
796 if (msg->received < msg->msglen) {
800 DLIST_REMOVE(ctx->msgs, msg);
801 ctx->recv_callback(ctx, msg->buf, msg->msglen, ctx->private_data);
805 int unix_msg_free(struct unix_msg_ctx *ctx)
809 ret = unix_dgram_free(ctx->dgram);
814 while (ctx->msgs != NULL) {
815 struct unix_msg *msg = ctx->msgs;
816 DLIST_REMOVE(ctx->msgs, msg);
824 static ssize_t iov_buflen(const struct iovec *iov, int iovlen)
829 for (i=0; i<iovlen; i++) {
830 size_t thislen = iov[i].iov_len;
831 size_t tmp = buflen + thislen;
833 if ((tmp < buflen) || (tmp < thislen)) {