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 char *path
, 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
;
147 struct sockaddr_un addr
= { 0, };
152 pathlen
= strlen(path
)+1;
153 if (pathlen
> sizeof(addr
.sun_path
)) {
160 ctx
= malloc(offsetof(struct unix_dgram_ctx
, path
) + pathlen
);
165 memcpy(ctx
->path
, path
, pathlen
);
170 ctx
->recv_buf
= malloc(max_msg
);
171 if (ctx
->recv_buf
== NULL
) {
175 ctx
->max_msg
= max_msg
;
176 ctx
->ev_funcs
= ev_funcs
;
177 ctx
->recv_callback
= recv_callback
;
178 ctx
->private_data
= private_data
;
179 ctx
->sock_read_watch
= NULL
;
180 ctx
->send_pool
= NULL
;
181 ctx
->pool_read_watch
= NULL
;
182 ctx
->send_queues
= NULL
;
183 ctx
->created_pid
= (pid_t
)-1;
185 ctx
->sock
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
186 if (ctx
->sock
== -1) {
191 /* Set non-blocking and close-on-exec. */
192 ret
= prepare_socket(ctx
->sock
);
198 addr
.sun_family
= AF_UNIX
;
199 memcpy(addr
.sun_path
, path
, pathlen
);
201 ret
= bind(ctx
->sock
, (struct sockaddr
*)(void *)&addr
,
208 ctx
->created_pid
= getpid();
210 ctx
->sock_read_watch
= ctx
->ev_funcs
->watch_new(
211 ctx
->ev_funcs
, ctx
->sock
, POLLIN
,
212 unix_dgram_recv_handler
, ctx
);
214 if (ctx
->sock_read_watch
== NULL
) {
231 static void unix_dgram_recv_handler(struct poll_watch
*w
, int fd
, short events
,
234 struct unix_dgram_ctx
*ctx
= (struct unix_dgram_ctx
*)private_data
;
237 received
= recv(fd
, ctx
->recv_buf
, ctx
->max_msg
, 0);
238 if (received
== -1) {
239 if ((errno
== EAGAIN
) ||
241 (errno
== EWOULDBLOCK
) ||
243 (errno
== EINTR
) || (errno
== ENOMEM
)) {
244 /* Not really an error - just try again. */
247 /* Problem with the socket. Set it unreadable. */
248 ctx
->ev_funcs
->watch_update(w
, 0);
251 if (received
> ctx
->max_msg
) {
252 /* More than we expected, not for us */
255 ctx
->recv_callback(ctx
, ctx
->recv_buf
, received
, ctx
->private_data
);
258 static void unix_dgram_job_finished(struct poll_watch
*w
, int fd
, short events
,
261 static int unix_dgram_init_pthreadpool(struct unix_dgram_ctx
*ctx
)
265 if (ctx
->send_pool
!= NULL
) {
269 ret
= pthreadpool_init(0, &ctx
->send_pool
);
274 signalfd
= pthreadpool_signal_fd(ctx
->send_pool
);
276 ctx
->pool_read_watch
= ctx
->ev_funcs
->watch_new(
277 ctx
->ev_funcs
, signalfd
, POLLIN
,
278 unix_dgram_job_finished
, ctx
);
279 if (ctx
->pool_read_watch
== NULL
) {
280 pthreadpool_destroy(ctx
->send_pool
);
281 ctx
->send_pool
= NULL
;
288 static int unix_dgram_send_queue_init(
289 struct unix_dgram_ctx
*ctx
, const char *path
,
290 struct unix_dgram_send_queue
**result
)
292 struct unix_dgram_send_queue
*q
;
293 struct sockaddr_un addr
= { 0, };
297 pathlen
= strlen(path
)+1;
299 if (pathlen
> sizeof(addr
.sun_path
)) {
303 q
= malloc(offsetof(struct unix_dgram_send_queue
, path
) + pathlen
);
309 memcpy(q
->path
, path
, pathlen
);
311 q
->sock
= socket(AF_UNIX
, SOCK_DGRAM
, 0);
317 err
= prepare_socket_cloexec(q
->sock
);
322 addr
.sun_family
= AF_UNIX
;
323 memcpy(addr
.sun_path
, path
, pathlen
+1);
326 ret
= connect(q
->sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
327 } while ((ret
== -1) && (errno
== EINTR
));
334 err
= unix_dgram_init_pthreadpool(ctx
);
339 DLIST_ADD(ctx
->send_queues
, q
);
351 static void unix_dgram_send_queue_free(struct unix_dgram_send_queue
*q
)
353 struct unix_dgram_ctx
*ctx
= q
->ctx
;
355 while (q
->msgs
!= NULL
) {
356 struct unix_dgram_msg
*msg
;
358 DLIST_REMOVE(q
->msgs
, msg
);
362 DLIST_REMOVE(ctx
->send_queues
, q
);
366 static struct unix_dgram_send_queue
*find_send_queue(
367 struct unix_dgram_ctx
*ctx
, const char *dst_sock
)
369 struct unix_dgram_send_queue
*s
;
371 for (s
= ctx
->send_queues
; s
!= NULL
; s
= s
->next
) {
372 if (strcmp(s
->path
, dst_sock
) == 0) {
379 static int queue_msg(struct unix_dgram_send_queue
*q
,
380 const struct iovec
*iov
, int iovlen
)
382 struct unix_dgram_msg
*msg
;
387 buflen
= iov_buflen(iov
, iovlen
);
392 msglen
= offsetof(struct unix_dgram_msg
, buf
) + buflen
;
393 if ((msglen
< buflen
) ||
394 (msglen
< offsetof(struct unix_dgram_msg
, buf
))) {
399 msg
= malloc(msglen
);
403 msg
->buflen
= buflen
;
407 for (i
=0; i
<iovlen
; i
++) {
408 memcpy(&msg
->buf
[buflen
], iov
[i
].iov_base
, iov
[i
].iov_len
);
409 buflen
+= iov
[i
].iov_len
;
412 DLIST_ADD_END(q
->msgs
, msg
, struct unix_dgram_msg
);
416 static void unix_dgram_send_job(void *private_data
)
418 struct unix_dgram_msg
*msg
= private_data
;
421 msg
->sent
= send(msg
->sock
, msg
->buf
, msg
->buflen
, 0);
422 } while ((msg
->sent
== -1) && (errno
== EINTR
));
425 static void unix_dgram_job_finished(struct poll_watch
*w
, int fd
, short events
,
428 struct unix_dgram_ctx
*ctx
= private_data
;
429 struct unix_dgram_send_queue
*q
;
430 struct unix_dgram_msg
*msg
;
433 ret
= pthreadpool_finished_jobs(ctx
->send_pool
, &job
, 1);
438 for (q
= ctx
->send_queues
; q
!= NULL
; q
= q
->next
) {
439 if (job
== q
->sock
) {
445 /* Huh? Should not happen */
450 DLIST_REMOVE(q
->msgs
, msg
);
453 if (q
->msgs
!= NULL
) {
454 ret
= pthreadpool_add_job(ctx
->send_pool
, q
->sock
,
455 unix_dgram_send_job
, q
->msgs
);
461 unix_dgram_send_queue_free(q
);
464 static int unix_dgram_send(struct unix_dgram_ctx
*ctx
, const char *dst_sock
,
465 const struct iovec
*iov
, int iovlen
)
467 struct unix_dgram_send_queue
*q
;
468 struct sockaddr_un addr
= { 0, };
473 dst_len
= strlen(dst_sock
);
474 if (dst_len
>= sizeof(addr
.sun_path
)) {
479 * To preserve message ordering, we have to queue a message when
480 * others are waiting in line already.
482 q
= find_send_queue(ctx
, dst_sock
);
484 return queue_msg(q
, iov
, iovlen
);
488 * Try a cheap nonblocking send
491 addr
.sun_family
= AF_UNIX
;
492 memcpy(addr
.sun_path
, dst_sock
, dst_len
);
494 msg
.msg_name
= &addr
;
495 msg
.msg_namelen
= sizeof(addr
);
496 msg
.msg_iov
= discard_const_p(struct iovec
, iov
);
497 msg
.msg_iovlen
= iovlen
;
498 msg
.msg_control
= NULL
;
499 msg
.msg_controllen
= 0;
502 ret
= sendmsg(ctx
->sock
, &msg
, 0);
507 if ((errno
!= EWOULDBLOCK
) && (errno
!= EAGAIN
) && (errno
!= EINTR
)) {
509 if ((errno
!= EAGAIN
) && (errno
!= EINTR
)) {
514 ret
= unix_dgram_send_queue_init(ctx
, dst_sock
, &q
);
518 ret
= queue_msg(q
, iov
, iovlen
);
520 unix_dgram_send_queue_free(q
);
523 ret
= pthreadpool_add_job(ctx
->send_pool
, q
->sock
,
524 unix_dgram_send_job
, q
->msgs
);
526 unix_dgram_send_queue_free(q
);
532 static int unix_dgram_sock(struct unix_dgram_ctx
*ctx
)
537 static int unix_dgram_free(struct unix_dgram_ctx
*ctx
)
539 if (ctx
->send_queues
!= NULL
) {
543 if (ctx
->send_pool
!= NULL
) {
544 int ret
= pthreadpool_destroy(ctx
->send_pool
);
548 ctx
->ev_funcs
->watch_free(ctx
->pool_read_watch
);
551 ctx
->ev_funcs
->watch_free(ctx
->sock_read_watch
);
553 if (getpid() == ctx
->created_pid
) {
554 /* If we created it, unlink. Otherwise someone else might
555 * still have it open */
566 * Every message starts with a uint64_t cookie.
568 * A value of 0 indicates a single-fragment message which is complete in
569 * itself. The data immediately follows the cookie.
571 * Every multi-fragment message has a cookie != 0 and starts with a cookie
572 * followed by a struct unix_msg_header and then the data. The pid and sock
573 * fields are used to assure uniqueness on the receiver side.
576 struct unix_msg_hdr
{
583 struct unix_msg
*prev
, *next
;
592 struct unix_msg_ctx
{
593 struct unix_dgram_ctx
*dgram
;
597 void (*recv_callback
)(struct unix_msg_ctx
*ctx
,
598 uint8_t *msg
, size_t msg_len
,
602 struct unix_msg
*msgs
;
605 static void unix_msg_recv(struct unix_dgram_ctx
*ctx
,
606 uint8_t *msg
, size_t msg_len
,
609 int unix_msg_init(const char *path
, const struct poll_funcs
*ev_funcs
,
610 size_t fragment_len
, uint64_t cookie
,
611 void (*recv_callback
)(struct unix_msg_ctx
*ctx
,
612 uint8_t *msg
, size_t msg_len
,
615 struct unix_msg_ctx
**result
)
617 struct unix_msg_ctx
*ctx
;
620 ctx
= malloc(sizeof(*ctx
));
625 ret
= unix_dgram_init(path
, fragment_len
, ev_funcs
,
626 unix_msg_recv
, ctx
, &ctx
->dgram
);
632 ctx
->fragment_len
= fragment_len
;
633 ctx
->cookie
= cookie
;
634 ctx
->recv_callback
= recv_callback
;
635 ctx
->private_data
= private_data
;
642 int unix_msg_send(struct unix_msg_ctx
*ctx
, const char *dst_sock
,
643 const struct iovec
*iov
, int iovlen
)
648 struct iovec
*iov_copy
;
649 struct unix_msg_hdr hdr
;
650 struct iovec src_iov
;
656 msglen
= iov_buflen(iov
, iovlen
);
662 (msglen
<= (ctx
->fragment_len
- sizeof(uint64_t)))) {
663 struct iovec tmp_iov
[16];
666 tmp_iov
[0].iov_base
= &cookie
;
667 tmp_iov
[0].iov_len
= sizeof(cookie
);
669 memcpy(&tmp_iov
[1], iov
,
670 sizeof(struct iovec
) * iovlen
);
673 return unix_dgram_send(ctx
->dgram
, dst_sock
, tmp_iov
,
679 hdr
.sock
= unix_dgram_sock(ctx
->dgram
);
681 iov_copy
= malloc(sizeof(struct iovec
) * (iovlen
+ 2));
682 if (iov_copy
== NULL
) {
685 iov_copy
[0].iov_base
= &ctx
->cookie
;
686 iov_copy
[0].iov_len
= sizeof(ctx
->cookie
);
687 iov_copy
[1].iov_base
= &hdr
;
688 iov_copy
[1].iov_len
= sizeof(hdr
);
694 * The following write loop sends the user message in pieces. We have
695 * filled the first two iovecs above with "cookie" and "hdr". In the
696 * following loops we pull message chunks from the user iov array and
697 * fill iov_copy piece by piece, possibly truncating chunks from the
698 * caller's iov array. Ugly, but hopefully efficient.
701 while (sent
< msglen
) {
703 size_t iov_index
= 2;
705 fragment_len
= sizeof(ctx
->cookie
) + sizeof(hdr
);
707 while (fragment_len
< ctx
->fragment_len
) {
710 space
= ctx
->fragment_len
- fragment_len
;
711 chunk
= MIN(space
, src_iov
.iov_len
);
713 iov_copy
[iov_index
].iov_base
= src_iov
.iov_base
;
714 iov_copy
[iov_index
].iov_len
= chunk
;
717 src_iov
.iov_base
= (char *)src_iov
.iov_base
+ chunk
;
718 src_iov
.iov_len
-= chunk
;
719 fragment_len
+= chunk
;
721 if (src_iov
.iov_len
== 0) {
730 sent
+= (fragment_len
- sizeof(ctx
->cookie
) - sizeof(hdr
));
732 ret
= unix_dgram_send(ctx
->dgram
, dst_sock
,
733 iov_copy
, iov_index
);
742 if (ctx
->cookie
== 0) {
749 static void unix_msg_recv(struct unix_dgram_ctx
*dgram_ctx
,
750 uint8_t *buf
, size_t buflen
,
753 struct unix_msg_ctx
*ctx
= (struct unix_msg_ctx
*)private_data
;
754 struct unix_msg_hdr hdr
;
755 struct unix_msg
*msg
;
759 if (buflen
< sizeof(cookie
)) {
762 memcpy(&cookie
, buf
, sizeof(cookie
));
764 buf
+= sizeof(cookie
);
765 buflen
-= sizeof(cookie
);
768 ctx
->recv_callback(ctx
, buf
, buflen
, ctx
->private_data
);
772 if (buflen
< sizeof(hdr
)) {
775 memcpy(&hdr
, buf
, sizeof(hdr
));
778 buflen
-= sizeof(hdr
);
780 for (msg
= ctx
->msgs
; msg
!= NULL
; msg
= msg
->next
) {
781 if ((msg
->sender_pid
== hdr
.pid
) &&
782 (msg
->sender_sock
== hdr
.sock
)) {
787 if ((msg
!= NULL
) && (msg
->cookie
!= cookie
)) {
788 DLIST_REMOVE(ctx
->msgs
, msg
);
794 msg
= malloc(offsetof(struct unix_msg
, buf
) + hdr
.msglen
);
798 msg
->msglen
= hdr
.msglen
;
800 msg
->sender_pid
= hdr
.pid
;
801 msg
->sender_sock
= hdr
.sock
;
802 msg
->cookie
= cookie
;
803 DLIST_ADD(ctx
->msgs
, msg
);
806 space
= msg
->msglen
- msg
->received
;
807 if (buflen
> space
) {
811 memcpy(msg
->buf
+ msg
->received
, buf
, buflen
);
812 msg
->received
+= buflen
;
814 if (msg
->received
< msg
->msglen
) {
818 DLIST_REMOVE(ctx
->msgs
, msg
);
819 ctx
->recv_callback(ctx
, msg
->buf
, msg
->msglen
, ctx
->private_data
);
823 int unix_msg_free(struct unix_msg_ctx
*ctx
)
827 ret
= unix_dgram_free(ctx
->dgram
);
832 while (ctx
->msgs
!= NULL
) {
833 struct unix_msg
*msg
= ctx
->msgs
;
834 DLIST_REMOVE(ctx
->msgs
, msg
);
842 static ssize_t
iov_buflen(const struct iovec
*iov
, int iovlen
)
847 for (i
=0; i
<iovlen
; i
++) {
848 size_t thislen
= iov
[i
].iov_len
;
849 size_t tmp
= buflen
+ thislen
;
851 if ((tmp
< buflen
) || (tmp
< thislen
)) {