]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-socket.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "alloc-util.h"
12 #include "bus-internal.h"
13 #include "bus-message.h"
14 #include "bus-socket.h"
16 #include "format-util.h"
18 #include "hexdecoct.h"
21 #include "memory-util.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "rlimit-util.h"
25 #include "signal-util.h"
26 #include "stdio-util.h"
27 #include "string-util.h"
28 #include "user-util.h"
31 #define SNDBUF_SIZE (8*1024*1024)
33 static void iovec_advance(struct iovec iov
[], unsigned *idx
, size_t size
) {
36 struct iovec
*i
= iov
+ *idx
;
38 if (i
->iov_len
> size
) {
39 i
->iov_base
= (uint8_t*) i
->iov_base
+ size
;
46 *i
= IOVEC_MAKE(NULL
, 0);
52 static int append_iovec(sd_bus_message
*m
, const void *p
, size_t sz
) {
57 m
->iovec
[m
->n_iovec
++] = IOVEC_MAKE((void*) p
, sz
);
62 static int bus_message_setup_iovec(sd_bus_message
*m
) {
63 struct bus_body_part
*part
;
75 n
= 1 + m
->n_body_parts
;
76 if (n
< ELEMENTSOF(m
->iovec_fixed
))
77 m
->iovec
= m
->iovec_fixed
;
79 m
->iovec
= new(struct iovec
, n
);
86 r
= append_iovec(m
, m
->header
, BUS_MESSAGE_BODY_BEGIN(m
));
90 MESSAGE_FOREACH_PART(part
, i
, m
) {
91 r
= bus_body_part_map(part
);
95 r
= append_iovec(m
, part
->data
, part
->size
);
100 assert(n
== m
->n_iovec
);
109 bool bus_socket_auth_needs_write(sd_bus
*b
) {
113 if (b
->auth_index
>= ELEMENTSOF(b
->auth_iovec
))
116 for (i
= b
->auth_index
; i
< ELEMENTSOF(b
->auth_iovec
); i
++) {
117 struct iovec
*j
= b
->auth_iovec
+ i
;
126 static int bus_socket_write_auth(sd_bus
*b
) {
130 assert(b
->state
== BUS_AUTHENTICATING
);
132 if (!bus_socket_auth_needs_write(b
))
135 if (b
->prefer_writev
)
136 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
139 .msg_iov
= b
->auth_iovec
+ b
->auth_index
,
140 .msg_iovlen
= ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
,
143 k
= sendmsg(b
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
144 if (k
< 0 && errno
== ENOTSOCK
) {
145 b
->prefer_writev
= true;
146 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
151 return errno
== EAGAIN
? 0 : -errno
;
153 iovec_advance(b
->auth_iovec
, &b
->auth_index
, (size_t) k
);
157 static int bus_socket_auth_verify_client(sd_bus
*b
) {
158 char *d
, *e
, *f
, *start
;
165 * We expect three response lines:
167 * "OK <server-id>\r\n"
168 * "AGREE_UNIX_FD\r\n" (optional)
171 d
= memmem_safe(b
->rbuffer
, b
->rbuffer_size
, "\r\n", 2);
175 e
= memmem(d
+ 2, b
->rbuffer_size
- (d
- (char*) b
->rbuffer
) - 2, "\r\n", 2);
180 f
= memmem(e
+ 2, b
->rbuffer_size
- (e
- (char*) b
->rbuffer
) - 2, "\r\n", 2);
190 /* Nice! We got all the lines we need. First check the DATA line. */
192 if (d
- (char*) b
->rbuffer
== 4) {
193 if (memcmp(b
->rbuffer
, "DATA", 4))
195 } else if (d
- (char*) b
->rbuffer
== 3 + 32) {
197 * Old versions of the server-side implementation of `sd-bus` replied with "OK <id>" to
198 * "AUTH" requests from a client, even if the "AUTH" line did not contain inlined
199 * arguments. Therefore, we also accept "OK <id>" here, even though it is technically the
200 * wrong reply. We ignore the "<id>" parameter, though, since it has no real value.
202 if (memcmp(b
->rbuffer
, "OK ", 3))
207 /* Now check the OK line. */
209 if (e
- d
!= 2 + 3 + 32)
212 if (memcmp(d
+ 2, "OK ", 3))
215 b
->auth
= b
->anonymous_auth
? BUS_AUTH_ANONYMOUS
: BUS_AUTH_EXTERNAL
;
217 for (unsigned i
= 0; i
< 32; i
+= 2) {
220 x
= unhexchar(d
[2 + 3 + i
]);
221 y
= unhexchar(d
[2 + 3 + i
+ 1]);
226 peer
.bytes
[i
/2] = ((uint8_t) x
<< 4 | (uint8_t) y
);
229 if (!sd_id128_is_null(b
->server_id
) &&
230 !sd_id128_equal(b
->server_id
, peer
))
235 /* And possibly check the third line, too */
239 (f
- e
== STRLEN("\r\nAGREE_UNIX_FD")) &&
240 memcmp(e
+ 2, "AGREE_UNIX_FD",
241 STRLEN("AGREE_UNIX_FD")) == 0;
243 b
->rbuffer_size
-= (start
- (char*) b
->rbuffer
);
244 memmove(b
->rbuffer
, start
, b
->rbuffer_size
);
246 r
= bus_start_running(b
);
253 static bool line_equals(const char *s
, size_t m
, const char *line
) {
260 return memcmp(s
, line
, l
) == 0;
263 static bool line_begins(const char *s
, size_t m
, const char *word
) {
266 p
= memory_startswith(s
, m
, word
);
267 return p
&& (p
== (s
+ m
) || *p
== ' ');
270 static int verify_anonymous_token(sd_bus
*b
, const char *p
, size_t l
) {
271 _cleanup_free_
char *token
= NULL
;
275 if (!b
->anonymous_auth
)
287 r
= unhexmem(p
, l
, (void **) &token
, &len
);
291 if (memchr(token
, 0, len
))
294 return !!utf8_is_valid(token
);
297 static int verify_external_token(sd_bus
*b
, const char *p
, size_t l
) {
298 _cleanup_free_
char *token
= NULL
;
303 /* We don't do any real authentication here. Instead, we if
304 * the owner of this bus wanted authentication he should have
305 * checked SO_PEERCRED before even creating the bus object. */
307 if (!b
->anonymous_auth
&& !b
->ucred_valid
)
319 r
= unhexmem(p
, l
, (void**) &token
, &len
);
323 if (memchr(token
, 0, len
))
326 r
= parse_uid(token
, &u
);
330 /* We ignore the passed value if anonymous authentication is
332 if (!b
->anonymous_auth
&& u
!= b
->ucred
.uid
)
338 static int bus_socket_auth_write(sd_bus
*b
, const char *t
) {
345 /* We only make use of the first iovec */
346 assert(IN_SET(b
->auth_index
, 0, 1));
349 p
= malloc(b
->auth_iovec
[0].iov_len
+ l
);
353 memcpy_safe(p
, b
->auth_iovec
[0].iov_base
, b
->auth_iovec
[0].iov_len
);
354 memcpy(p
+ b
->auth_iovec
[0].iov_len
, t
, l
);
356 b
->auth_iovec
[0].iov_base
= p
;
357 b
->auth_iovec
[0].iov_len
+= l
;
359 free(b
->auth_buffer
);
365 static int bus_socket_auth_write_ok(sd_bus
*b
) {
366 char t
[3 + 32 + 2 + 1];
370 xsprintf(t
, "OK " SD_ID128_FORMAT_STR
"\r\n", SD_ID128_FORMAT_VAL(b
->server_id
));
372 return bus_socket_auth_write(b
, t
);
375 static int bus_socket_auth_verify_server(sd_bus
*b
) {
379 bool processed
= false;
384 if (b
->rbuffer_size
< 1)
387 /* First char must be a NUL byte */
388 if (*(char*) b
->rbuffer
!= 0)
391 if (b
->rbuffer_size
< 3)
394 /* Begin with the first line */
395 if (b
->auth_rbegin
<= 0)
399 /* Check if line is complete */
400 line
= (char*) b
->rbuffer
+ b
->auth_rbegin
;
401 e
= memmem(line
, b
->rbuffer_size
- b
->auth_rbegin
, "\r\n", 2);
407 if (line_begins(line
, l
, "AUTH ANONYMOUS")) {
409 r
= verify_anonymous_token(b
,
410 line
+ strlen("AUTH ANONYMOUS"),
411 l
- strlen("AUTH ANONYMOUS"));
415 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
417 b
->auth
= BUS_AUTH_ANONYMOUS
;
418 if (l
<= strlen("AUTH ANONYMOUS"))
419 r
= bus_socket_auth_write(b
, "DATA\r\n");
421 r
= bus_socket_auth_write_ok(b
);
424 } else if (line_begins(line
, l
, "AUTH EXTERNAL")) {
426 r
= verify_external_token(b
,
427 line
+ strlen("AUTH EXTERNAL"),
428 l
- strlen("AUTH EXTERNAL"));
432 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
434 b
->auth
= BUS_AUTH_EXTERNAL
;
435 if (l
<= strlen("AUTH EXTERNAL"))
436 r
= bus_socket_auth_write(b
, "DATA\r\n");
438 r
= bus_socket_auth_write_ok(b
);
441 } else if (line_begins(line
, l
, "AUTH"))
442 r
= bus_socket_auth_write(b
, "REJECTED EXTERNAL ANONYMOUS\r\n");
443 else if (line_equals(line
, l
, "CANCEL") ||
444 line_begins(line
, l
, "ERROR")) {
446 b
->auth
= _BUS_AUTH_INVALID
;
447 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
449 } else if (line_equals(line
, l
, "BEGIN")) {
451 if (b
->auth
== _BUS_AUTH_INVALID
)
452 r
= bus_socket_auth_write(b
, "ERROR\r\n");
454 /* We can't leave from the auth phase
455 * before we haven't written
456 * everything queued, so let's check
459 if (bus_socket_auth_needs_write(b
))
462 b
->rbuffer_size
-= (e
+ 2 - (char*) b
->rbuffer
);
463 memmove(b
->rbuffer
, e
+ 2, b
->rbuffer_size
);
464 return bus_start_running(b
);
467 } else if (line_begins(line
, l
, "DATA")) {
469 if (b
->auth
== _BUS_AUTH_INVALID
)
470 r
= bus_socket_auth_write(b
, "ERROR\r\n");
472 if (b
->auth
== BUS_AUTH_ANONYMOUS
)
473 r
= verify_anonymous_token(b
, line
+ 4, l
- 4);
475 r
= verify_external_token(b
, line
+ 4, l
- 4);
480 b
->auth
= _BUS_AUTH_INVALID
;
481 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
483 r
= bus_socket_auth_write_ok(b
);
485 } else if (line_equals(line
, l
, "NEGOTIATE_UNIX_FD")) {
486 if (b
->auth
== _BUS_AUTH_INVALID
|| !b
->accept_fd
)
487 r
= bus_socket_auth_write(b
, "ERROR\r\n");
490 r
= bus_socket_auth_write(b
, "AGREE_UNIX_FD\r\n");
493 r
= bus_socket_auth_write(b
, "ERROR\r\n");
498 b
->auth_rbegin
= e
+ 2 - (char*) b
->rbuffer
;
504 static int bus_socket_auth_verify(sd_bus
*b
) {
508 return bus_socket_auth_verify_server(b
);
510 return bus_socket_auth_verify_client(b
);
513 static int bus_socket_read_auth(sd_bus
*b
) {
515 struct iovec iov
= {};
520 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)) control
;
521 bool handle_cmsg
= false;
524 assert(b
->state
== BUS_AUTHENTICATING
);
526 r
= bus_socket_auth_verify(b
);
530 n
= MAX(256u, b
->rbuffer_size
* 2);
532 if (n
> BUS_AUTH_SIZE_MAX
)
533 n
= BUS_AUTH_SIZE_MAX
;
535 if (b
->rbuffer_size
>= n
)
538 p
= realloc(b
->rbuffer
, n
);
544 iov
= IOVEC_MAKE((uint8_t *)b
->rbuffer
+ b
->rbuffer_size
, n
- b
->rbuffer_size
);
546 if (b
->prefer_readv
) {
547 k
= readv(b
->input_fd
, &iov
, 1);
551 mh
= (struct msghdr
) {
554 .msg_control
= &control
,
555 .msg_controllen
= sizeof(control
),
558 k
= recvmsg_safe(b
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
559 if (k
== -ENOTSOCK
) {
560 b
->prefer_readv
= true;
561 k
= readv(b
->input_fd
, &iov
, 1);
573 cmsg_close_all(&mh
); /* paranoia, we shouldn't have gotten any fds on EOF */
577 b
->rbuffer_size
+= k
;
580 struct cmsghdr
*cmsg
;
582 CMSG_FOREACH(cmsg
, &mh
)
583 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
584 cmsg
->cmsg_type
== SCM_RIGHTS
) {
587 /* Whut? We received fds during the auth
588 * protocol? Somebody is playing games with
589 * us. Close them all, and fail */
590 j
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
591 close_many((int*) CMSG_DATA(cmsg
), j
);
594 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
595 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
598 r
= bus_socket_auth_verify(b
);
605 void bus_socket_setup(sd_bus
*b
) {
608 /* Increase the buffers to 8 MB */
609 (void) fd_inc_rcvbuf(b
->input_fd
, SNDBUF_SIZE
);
610 (void) fd_inc_sndbuf(b
->output_fd
, SNDBUF_SIZE
);
612 b
->message_version
= 1;
613 b
->message_endian
= 0;
616 static void bus_get_peercred(sd_bus
*b
) {
620 assert(!b
->ucred_valid
);
622 assert(b
->n_groups
== SIZE_MAX
);
624 /* Get the peer for socketpair() sockets */
625 b
->ucred_valid
= getpeercred(b
->input_fd
, &b
->ucred
) >= 0;
627 /* Get the SELinux context of the peer */
628 r
= getpeersec(b
->input_fd
, &b
->label
);
629 if (r
< 0 && !IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
630 log_debug_errno(r
, "Failed to determine peer security context: %m");
632 /* Get the list of auxiliary groups of the peer */
633 r
= getpeergroups(b
->input_fd
, &b
->groups
);
635 b
->n_groups
= (size_t) r
;
636 else if (!IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
637 log_debug_errno(r
, "Failed to determine peer's group list: %m");
640 static int bus_socket_start_auth_client(sd_bus
*b
) {
641 static const char sasl_auth_anonymous
[] = {
643 * We use an arbitrary trace-string for the ANONYMOUS authentication. It can be used by the
644 * message broker to aid debugging of clients. We fully anonymize the connection and use a
647 "\0AUTH ANONYMOUS\r\n"
648 /* HEX a n o n y m o u s */
649 "DATA 616e6f6e796d6f7573\r\n"
651 static const char sasl_auth_external
[] = {
652 "\0AUTH EXTERNAL\r\n"
655 static const char sasl_negotiate_unix_fd
[] = {
656 "NEGOTIATE_UNIX_FD\r\n"
658 static const char sasl_begin
[] = {
665 if (b
->anonymous_auth
)
666 b
->auth_iovec
[i
++] = IOVEC_MAKE((char*) sasl_auth_anonymous
, sizeof(sasl_auth_anonymous
) - 1);
668 b
->auth_iovec
[i
++] = IOVEC_MAKE((char*) sasl_auth_external
, sizeof(sasl_auth_external
) - 1);
671 b
->auth_iovec
[i
++] = IOVEC_MAKE_STRING(sasl_negotiate_unix_fd
);
673 b
->auth_iovec
[i
++] = IOVEC_MAKE_STRING(sasl_begin
);
675 return bus_socket_write_auth(b
);
678 int bus_socket_start_auth(sd_bus
*b
) {
683 bus_set_state(b
, BUS_AUTHENTICATING
);
684 b
->auth_timeout
= now(CLOCK_MONOTONIC
) + BUS_AUTH_TIMEOUT
;
686 if (sd_is_socket(b
->input_fd
, AF_UNIX
, 0, 0) <= 0)
687 b
->accept_fd
= false;
689 if (b
->output_fd
!= b
->input_fd
)
690 if (sd_is_socket(b
->output_fd
, AF_UNIX
, 0, 0) <= 0)
691 b
->accept_fd
= false;
694 return bus_socket_read_auth(b
);
696 return bus_socket_start_auth_client(b
);
699 static int bus_socket_inotify_setup(sd_bus
*b
) {
700 _cleanup_free_
int *new_watches
= NULL
;
701 _cleanup_free_
char *absolute
= NULL
;
702 size_t n
= 0, done
= 0, i
;
703 unsigned max_follow
= 32;
708 assert(b
->watch_bind
);
709 assert(b
->sockaddr
.sa
.sa_family
== AF_UNIX
);
710 assert(b
->sockaddr
.un
.sun_path
[0] != 0);
712 /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system socket
713 * appears before connecting to it. The implemented is pretty simplistic: we just subscribe to relevant changes
714 * to all prefix components of the path, and every time we get an event for that we try to reconnect again,
715 * without actually caring what precisely the event we got told us. If we still can't connect we re-subscribe
716 * to all relevant changes of anything in the path, so that our watches include any possibly newly created path
719 if (b
->inotify_fd
< 0) {
720 b
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
721 if (b
->inotify_fd
< 0)
724 b
->inotify_fd
= fd_move_above_stdio(b
->inotify_fd
);
727 /* Make sure the path is NUL terminated */
728 p
= strndupa(b
->sockaddr
.un
.sun_path
, sizeof(b
->sockaddr
.un
.sun_path
));
730 /* Make sure the path is absolute */
731 r
= path_make_absolute_cwd(p
, &absolute
);
735 /* Watch all parent directories, and don't mind any prefix that doesn't exist yet. For the innermost directory
736 * that exists we want to know when files are created or moved into it. For all parents of it we just care if
737 * they are removed or renamed. */
739 if (!GREEDY_REALLOC(new_watches
, n
+ 1)) {
744 /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a symlink, and
746 wd
= inotify_add_watch(b
->inotify_fd
, "/", IN_CREATE
|IN_MOVED_TO
);
748 r
= log_debug_errno(errno
, "Failed to add inotify watch on /: %m");
751 new_watches
[n
++] = wd
;
754 _cleanup_free_
char *component
= NULL
, *prefix
= NULL
, *destination
= NULL
;
755 size_t n_slashes
, n_component
;
758 n_slashes
= strspn(absolute
+ done
, "/");
759 n_component
= n_slashes
+ strcspn(absolute
+ done
+ n_slashes
, "/");
761 if (n_component
== 0) /* The end */
764 component
= strndup(absolute
+ done
, n_component
);
770 /* A trailing slash? That's a directory, and not a socket then */
771 if (path_equal(component
, "/")) {
776 /* A single dot? Let's eat this up */
777 if (path_equal(component
, "/.")) {
782 prefix
= strndup(absolute
, done
+ n_component
);
788 if (!GREEDY_REALLOC(new_watches
, n
+ 1)) {
793 wd
= inotify_add_watch(b
->inotify_fd
, prefix
, IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
|IN_DONT_FOLLOW
);
794 log_debug("Added inotify watch for %s on bus %s: %i", prefix
, strna(b
->description
), wd
);
797 if (IN_SET(errno
, ENOENT
, ELOOP
))
798 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
800 r
= log_debug_errno(errno
, "Failed to add inotify watch on %s: %m", empty_to_root(prefix
));
803 new_watches
[n
++] = wd
;
805 /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
806 r
= readlink_malloc(prefix
, &destination
);
807 if (r
== -EINVAL
) { /* not a symlink */
814 if (isempty(destination
)) { /* Empty symlink target? Yuck! */
819 if (max_follow
<= 0) { /* Let's make sure we don't follow symlinks forever */
824 if (path_is_absolute(destination
)) {
825 /* For absolute symlinks we build the new path and start anew */
826 c
= strjoin(destination
, absolute
+ done
+ n_component
);
829 _cleanup_free_
char *t
= NULL
;
831 /* For relative symlinks we replace the last component, and try again */
832 t
= strndup(absolute
, done
);
836 c
= strjoin(t
, "/", destination
, absolute
+ done
+ n_component
);
849 /* And now, let's remove all watches from the previous iteration we don't need anymore */
850 for (i
= 0; i
< b
->n_inotify_watches
; i
++) {
854 for (j
= 0; j
< n
; j
++)
855 if (new_watches
[j
] == b
->inotify_watches
[i
]) {
863 (void) inotify_rm_watch(b
->inotify_fd
, b
->inotify_watches
[i
]);
866 free_and_replace(b
->inotify_watches
, new_watches
);
867 b
->n_inotify_watches
= n
;
872 bus_close_inotify_fd(b
);
876 int bus_socket_connect(sd_bus
*b
) {
877 bool inotify_done
= false;
883 assert(b
->input_fd
< 0);
884 assert(b
->output_fd
< 0);
885 assert(b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
);
888 _cleanup_free_
char *pretty
= NULL
;
889 (void) sockaddr_pretty(&b
->sockaddr
.sa
, b
->sockaddr_size
, false, true, &pretty
);
890 log_debug("sd-bus: starting bus%s%s by connecting to %s...",
891 b
->description
? " " : "", strempty(b
->description
), strnull(pretty
));
894 b
->input_fd
= socket(b
->sockaddr
.sa
.sa_family
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
898 b
->input_fd
= fd_move_above_stdio(b
->input_fd
);
900 b
->output_fd
= b
->input_fd
;
903 if (connect(b
->input_fd
, &b
->sockaddr
.sa
, b
->sockaddr_size
) < 0) {
904 if (errno
== EINPROGRESS
) {
906 /* If we have any inotify watches open, close them now, we don't need them anymore, as
907 * we have successfully initiated a connection */
908 bus_close_inotify_fd(b
);
910 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
911 * we start parsing the address string. The only reason we set the state explicitly
912 * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
913 bus_set_state(b
, BUS_OPENING
);
917 if (IN_SET(errno
, ENOENT
, ECONNREFUSED
) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
919 b
->sockaddr
.sa
.sa_family
== AF_UNIX
&&
920 b
->sockaddr
.un
.sun_path
[0] != 0) {
922 /* This connection attempt failed, let's release the socket for now, and start with a
923 * fresh one when reconnecting. */
927 /* inotify set up already, don't do it again, just return now, and remember
928 * that we are waiting for inotify events now. */
929 bus_set_state(b
, BUS_WATCH_BIND
);
933 /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
934 r
= bus_socket_inotify_setup(b
);
938 /* Let's now try to connect a second time, because in theory there's otherwise a race
939 * here: the socket might have been created in the time between our first connect() and
940 * the time we set up the inotify logic. But let's remember that we set up inotify now,
941 * so that we don't do the connect() more than twice. */
950 /* Yay, established, we don't need no inotify anymore! */
951 bus_close_inotify_fd(b
);
953 return bus_socket_start_auth(b
);
956 int bus_socket_exec(sd_bus
*b
) {
960 assert(b
->input_fd
< 0);
961 assert(b
->output_fd
< 0);
962 assert(b
->exec_path
);
963 assert(b
->busexec_pid
== 0);
965 log_debug("sd-bus: starting bus%s%s with %s...",
966 b
->description
? " " : "", strempty(b
->description
), b
->exec_path
);
968 r
= socketpair(AF_UNIX
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0, s
);
972 r
= safe_fork_full("(sd-busexec)", s
+1, 1, FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
, &b
->busexec_pid
);
980 if (rearrange_stdio(s
[1], s
[1], STDERR_FILENO
) < 0)
983 (void) rlimit_nofile_safe();
986 execvp(b
->exec_path
, b
->exec_argv
);
988 const char *argv
[] = { b
->exec_path
, NULL
};
989 execvp(b
->exec_path
, (char**) argv
);
996 b
->output_fd
= b
->input_fd
= fd_move_above_stdio(s
[0]);
1000 return bus_socket_start_auth(b
);
1003 int bus_socket_take_fd(sd_bus
*b
) {
1006 bus_socket_setup(b
);
1008 return bus_socket_start_auth(b
);
1011 int bus_socket_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
1021 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1023 if (*idx
>= BUS_MESSAGE_SIZE(m
))
1026 r
= bus_message_setup_iovec(m
);
1030 n
= m
->n_iovec
* sizeof(struct iovec
);
1031 iov
= newa(struct iovec
, n
);
1032 memcpy_safe(iov
, m
->iovec
, n
);
1035 iovec_advance(iov
, &j
, *idx
);
1037 if (bus
->prefer_writev
)
1038 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1040 struct msghdr mh
= {
1042 .msg_iovlen
= m
->n_iovec
,
1045 if (m
->n_fds
> 0 && *idx
== 0) {
1046 struct cmsghdr
*control
;
1048 mh
.msg_controllen
= CMSG_SPACE(sizeof(int) * m
->n_fds
);
1049 mh
.msg_control
= alloca0(mh
.msg_controllen
);
1050 control
= CMSG_FIRSTHDR(&mh
);
1051 control
->cmsg_len
= CMSG_LEN(sizeof(int) * m
->n_fds
);
1052 control
->cmsg_level
= SOL_SOCKET
;
1053 control
->cmsg_type
= SCM_RIGHTS
;
1054 memcpy(CMSG_DATA(control
), m
->fds
, sizeof(int) * m
->n_fds
);
1057 k
= sendmsg(bus
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
1058 if (k
< 0 && errno
== ENOTSOCK
) {
1059 bus
->prefer_writev
= true;
1060 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1065 return errno
== EAGAIN
? 0 : -errno
;
1071 static int bus_socket_read_message_need(sd_bus
*bus
, size_t *need
) {
1078 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1080 if (bus
->rbuffer_size
< sizeof(struct bus_header
)) {
1081 *need
= sizeof(struct bus_header
) + 8;
1083 /* Minimum message size:
1087 * Method Call: +2 string headers
1088 * Signal: +3 string headers
1089 * Method Error: +1 string headers
1091 * Method Reply: +1 uint32 headers
1093 * A string header is at least 9 bytes
1094 * A uint32 header is at least 8 bytes
1096 * Hence the minimum message size of a valid message
1097 * is header + 8 bytes */
1102 a
= ((const uint32_t*) bus
->rbuffer
)[1];
1103 b
= ((const uint32_t*) bus
->rbuffer
)[3];
1105 e
= ((const uint8_t*) bus
->rbuffer
)[0];
1106 if (e
== BUS_LITTLE_ENDIAN
) {
1109 } else if (e
== BUS_BIG_ENDIAN
) {
1115 sum
= (uint64_t) sizeof(struct bus_header
) + (uint64_t) ALIGN_TO(b
, 8) + (uint64_t) a
;
1116 if (sum
>= BUS_MESSAGE_SIZE_MAX
)
1119 *need
= (size_t) sum
;
1123 static int bus_socket_make_message(sd_bus
*bus
, size_t size
) {
1124 sd_bus_message
*t
= NULL
;
1129 assert(bus
->rbuffer_size
>= size
);
1130 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1132 r
= bus_rqueue_make_room(bus
);
1136 if (bus
->rbuffer_size
> size
) {
1137 b
= memdup((const uint8_t*) bus
->rbuffer
+ size
,
1138 bus
->rbuffer_size
- size
);
1144 r
= bus_message_from_malloc(bus
,
1146 bus
->fds
, bus
->n_fds
,
1149 if (r
== -EBADMSG
) {
1150 log_debug_errno(r
, "Received invalid message from connection %s, dropping.", strna(bus
->description
));
1151 free(bus
->rbuffer
); /* We want to drop current rbuffer and proceed with whatever remains in b */
1157 /* rbuffer ownership was either transferred to t, or we got EBADMSG and dropped it. */
1159 bus
->rbuffer_size
-= size
;
1165 t
->read_counter
= ++bus
->read_counter
;
1166 bus
->rqueue
[bus
->rqueue_size
++] = bus_message_ref_queued(t
, bus
);
1167 sd_bus_message_unref(t
);
1173 int bus_socket_read_message(sd_bus
*bus
) {
1175 struct iovec iov
= {};
1180 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)) control
;
1181 bool handle_cmsg
= false;
1184 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1186 r
= bus_socket_read_message_need(bus
, &need
);
1190 if (bus
->rbuffer_size
>= need
)
1191 return bus_socket_make_message(bus
, need
);
1193 b
= realloc(bus
->rbuffer
, need
);
1199 iov
= IOVEC_MAKE((uint8_t *)bus
->rbuffer
+ bus
->rbuffer_size
, need
- bus
->rbuffer_size
);
1201 if (bus
->prefer_readv
) {
1202 k
= readv(bus
->input_fd
, &iov
, 1);
1206 mh
= (struct msghdr
) {
1209 .msg_control
= &control
,
1210 .msg_controllen
= sizeof(control
),
1213 k
= recvmsg_safe(bus
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
1214 if (k
== -ENOTSOCK
) {
1215 bus
->prefer_readv
= true;
1216 k
= readv(bus
->input_fd
, &iov
, 1);
1228 cmsg_close_all(&mh
); /* On EOF we shouldn't have gotten an fd, but let's make sure */
1232 bus
->rbuffer_size
+= k
;
1235 struct cmsghdr
*cmsg
;
1237 CMSG_FOREACH(cmsg
, &mh
)
1238 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1239 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1242 n
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
1244 if (!bus
->can_fds
) {
1245 /* Whut? We received fds but this
1246 * isn't actually enabled? Close them,
1249 close_many((int*) CMSG_DATA(cmsg
), n
);
1253 f
= reallocarray(bus
->fds
, bus
->n_fds
+ n
, sizeof(int));
1255 close_many((int*) CMSG_DATA(cmsg
), n
);
1259 for (i
= 0; i
< n
; i
++)
1260 f
[bus
->n_fds
++] = fd_move_above_stdio(((int*) CMSG_DATA(cmsg
))[i
]);
1263 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1264 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1267 r
= bus_socket_read_message_need(bus
, &need
);
1271 if (bus
->rbuffer_size
>= need
)
1272 return bus_socket_make_message(bus
, need
);
1277 int bus_socket_process_opening(sd_bus
*b
) {
1278 int error
= 0, events
, r
;
1279 socklen_t slen
= sizeof(error
);
1281 assert(b
->state
== BUS_OPENING
);
1283 events
= fd_wait_for_event(b
->output_fd
, POLLOUT
, 0);
1286 if (!(events
& (POLLOUT
|POLLERR
|POLLHUP
)))
1289 r
= getsockopt(b
->output_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &slen
);
1291 b
->last_connect_error
= errno
;
1292 else if (error
!= 0)
1293 b
->last_connect_error
= error
;
1294 else if (events
& (POLLERR
|POLLHUP
))
1295 b
->last_connect_error
= ECONNREFUSED
;
1297 return bus_socket_start_auth(b
);
1299 return bus_next_address(b
);
1302 int bus_socket_process_authenticating(sd_bus
*b
) {
1306 assert(b
->state
== BUS_AUTHENTICATING
);
1308 if (now(CLOCK_MONOTONIC
) >= b
->auth_timeout
)
1311 r
= bus_socket_write_auth(b
);
1315 return bus_socket_read_auth(b
);
1318 int bus_socket_process_watch_bind(sd_bus
*b
) {
1322 assert(b
->state
== BUS_WATCH_BIND
);
1323 assert(b
->inotify_fd
>= 0);
1325 r
= flush_fd(b
->inotify_fd
);
1329 log_debug("Got inotify event on bus %s.", strna(b
->description
));
1331 /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
1334 r
= bus_socket_connect(b
);
1338 q
= bus_attach_io_events(b
);
1342 q
= bus_attach_inotify_event(b
);