]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-socket.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "selinux-util.h"
25 #include "signal-util.h"
26 #include "stdio-util.h"
27 #include "string-util.h"
28 #include "user-util.h"
32 #define SNDBUF_SIZE (8*1024*1024)
34 static void iovec_advance(struct iovec iov
[], unsigned *idx
, size_t size
) {
37 struct iovec
*i
= iov
+ *idx
;
39 if (i
->iov_len
> size
) {
40 i
->iov_base
= (uint8_t*) i
->iov_base
+ size
;
47 *i
= IOVEC_MAKE(NULL
, 0);
53 static int append_iovec(sd_bus_message
*m
, const void *p
, size_t sz
) {
58 m
->iovec
[m
->n_iovec
++] = IOVEC_MAKE((void*) p
, sz
);
63 static int bus_message_setup_iovec(sd_bus_message
*m
) {
64 struct bus_body_part
*part
;
76 n
= 1 + m
->n_body_parts
;
77 if (n
< ELEMENTSOF(m
->iovec_fixed
))
78 m
->iovec
= m
->iovec_fixed
;
80 m
->iovec
= new(struct iovec
, n
);
87 r
= append_iovec(m
, m
->header
, BUS_MESSAGE_BODY_BEGIN(m
));
91 MESSAGE_FOREACH_PART(part
, i
, m
) {
92 r
= bus_body_part_map(part
);
96 r
= append_iovec(m
, part
->data
, part
->size
);
101 assert(n
== m
->n_iovec
);
110 bool bus_socket_auth_needs_write(sd_bus
*b
) {
114 if (b
->auth_index
>= ELEMENTSOF(b
->auth_iovec
))
117 for (i
= b
->auth_index
; i
< ELEMENTSOF(b
->auth_iovec
); i
++) {
118 struct iovec
*j
= b
->auth_iovec
+ i
;
127 static int bus_socket_write_auth(sd_bus
*b
) {
131 assert(b
->state
== BUS_AUTHENTICATING
);
133 if (!bus_socket_auth_needs_write(b
))
136 if (b
->prefer_writev
)
137 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
142 mh
.msg_iov
= b
->auth_iovec
+ b
->auth_index
;
143 mh
.msg_iovlen
= ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
;
145 k
= sendmsg(b
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
146 if (k
< 0 && errno
== ENOTSOCK
) {
147 b
->prefer_writev
= true;
148 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
153 return errno
== EAGAIN
? 0 : -errno
;
155 iovec_advance(b
->auth_iovec
, &b
->auth_index
, (size_t) k
);
159 static int bus_socket_auth_verify_client(sd_bus
*b
) {
167 /* We expect two response lines: "OK" and possibly
170 e
= memmem_safe(b
->rbuffer
, b
->rbuffer_size
, "\r\n", 2);
175 f
= memmem(e
+ 2, b
->rbuffer_size
- (e
- (char*) b
->rbuffer
) - 2, "\r\n", 2);
185 /* Nice! We got all the lines we need. First check the OK
188 if (e
- (char*) b
->rbuffer
!= 3 + 32)
191 if (memcmp(b
->rbuffer
, "OK ", 3))
194 b
->auth
= b
->anonymous_auth
? BUS_AUTH_ANONYMOUS
: BUS_AUTH_EXTERNAL
;
196 for (i
= 0; i
< 32; i
+= 2) {
199 x
= unhexchar(((char*) b
->rbuffer
)[3 + i
]);
200 y
= unhexchar(((char*) b
->rbuffer
)[3 + i
+ 1]);
205 peer
.bytes
[i
/2] = ((uint8_t) x
<< 4 | (uint8_t) y
);
208 if (!sd_id128_is_null(b
->server_id
) &&
209 !sd_id128_equal(b
->server_id
, peer
))
214 /* And possibly check the second line, too */
218 (f
- e
== STRLEN("\r\nAGREE_UNIX_FD")) &&
219 memcmp(e
+ 2, "AGREE_UNIX_FD",
220 STRLEN("AGREE_UNIX_FD")) == 0;
222 b
->rbuffer_size
-= (start
- (char*) b
->rbuffer
);
223 memmove(b
->rbuffer
, start
, b
->rbuffer_size
);
225 r
= bus_start_running(b
);
232 static bool line_equals(const char *s
, size_t m
, const char *line
) {
239 return memcmp(s
, line
, l
) == 0;
242 static bool line_begins(const char *s
, size_t m
, const char *word
) {
245 p
= memory_startswith(s
, m
, word
);
246 return p
&& (p
== (s
+ m
) || *p
== ' ');
249 static int verify_anonymous_token(sd_bus
*b
, const char *p
, size_t l
) {
250 _cleanup_free_
char *token
= NULL
;
254 if (!b
->anonymous_auth
)
266 r
= unhexmem(p
, l
, (void **) &token
, &len
);
270 if (memchr(token
, 0, len
))
273 return !!utf8_is_valid(token
);
276 static int verify_external_token(sd_bus
*b
, const char *p
, size_t l
) {
277 _cleanup_free_
char *token
= NULL
;
282 /* We don't do any real authentication here. Instead, we if
283 * the owner of this bus wanted authentication he should have
284 * checked SO_PEERCRED before even creating the bus object. */
286 if (!b
->anonymous_auth
&& !b
->ucred_valid
)
298 r
= unhexmem(p
, l
, (void**) &token
, &len
);
302 if (memchr(token
, 0, len
))
305 r
= parse_uid(token
, &u
);
309 /* We ignore the passed value if anonymous authentication is
311 if (!b
->anonymous_auth
&& u
!= b
->ucred
.uid
)
317 static int bus_socket_auth_write(sd_bus
*b
, const char *t
) {
324 /* We only make use of the first iovec */
325 assert(IN_SET(b
->auth_index
, 0, 1));
328 p
= malloc(b
->auth_iovec
[0].iov_len
+ l
);
332 memcpy_safe(p
, b
->auth_iovec
[0].iov_base
, b
->auth_iovec
[0].iov_len
);
333 memcpy(p
+ b
->auth_iovec
[0].iov_len
, t
, l
);
335 b
->auth_iovec
[0].iov_base
= p
;
336 b
->auth_iovec
[0].iov_len
+= l
;
338 free(b
->auth_buffer
);
344 static int bus_socket_auth_write_ok(sd_bus
*b
) {
345 char t
[3 + 32 + 2 + 1];
349 xsprintf(t
, "OK " SD_ID128_FORMAT_STR
"\r\n", SD_ID128_FORMAT_VAL(b
->server_id
));
351 return bus_socket_auth_write(b
, t
);
354 static int bus_socket_auth_verify_server(sd_bus
*b
) {
358 bool processed
= false;
363 if (b
->rbuffer_size
< 1)
366 /* First char must be a NUL byte */
367 if (*(char*) b
->rbuffer
!= 0)
370 if (b
->rbuffer_size
< 3)
373 /* Begin with the first line */
374 if (b
->auth_rbegin
<= 0)
378 /* Check if line is complete */
379 line
= (char*) b
->rbuffer
+ b
->auth_rbegin
;
380 e
= memmem(line
, b
->rbuffer_size
- b
->auth_rbegin
, "\r\n", 2);
386 if (line_begins(line
, l
, "AUTH ANONYMOUS")) {
388 r
= verify_anonymous_token(b
, line
+ 14, l
- 14);
392 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
394 b
->auth
= BUS_AUTH_ANONYMOUS
;
395 r
= bus_socket_auth_write_ok(b
);
398 } else if (line_begins(line
, l
, "AUTH EXTERNAL")) {
400 r
= verify_external_token(b
, line
+ 13, l
- 13);
404 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
406 b
->auth
= BUS_AUTH_EXTERNAL
;
407 r
= bus_socket_auth_write_ok(b
);
410 } else if (line_begins(line
, l
, "AUTH"))
411 r
= bus_socket_auth_write(b
, "REJECTED EXTERNAL ANONYMOUS\r\n");
412 else if (line_equals(line
, l
, "CANCEL") ||
413 line_begins(line
, l
, "ERROR")) {
415 b
->auth
= _BUS_AUTH_INVALID
;
416 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
418 } else if (line_equals(line
, l
, "BEGIN")) {
420 if (b
->auth
== _BUS_AUTH_INVALID
)
421 r
= bus_socket_auth_write(b
, "ERROR\r\n");
423 /* We can't leave from the auth phase
424 * before we haven't written
425 * everything queued, so let's check
428 if (bus_socket_auth_needs_write(b
))
431 b
->rbuffer_size
-= (e
+ 2 - (char*) b
->rbuffer
);
432 memmove(b
->rbuffer
, e
+ 2, b
->rbuffer_size
);
433 return bus_start_running(b
);
436 } else if (line_begins(line
, l
, "DATA")) {
438 if (b
->auth
== _BUS_AUTH_INVALID
)
439 r
= bus_socket_auth_write(b
, "ERROR\r\n");
441 if (b
->auth
== BUS_AUTH_ANONYMOUS
)
442 r
= verify_anonymous_token(b
, line
+ 4, l
- 4);
444 r
= verify_external_token(b
, line
+ 4, l
- 4);
449 b
->auth
= _BUS_AUTH_INVALID
;
450 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
452 r
= bus_socket_auth_write_ok(b
);
454 } else if (line_equals(line
, l
, "NEGOTIATE_UNIX_FD")) {
455 if (b
->auth
== _BUS_AUTH_INVALID
|| !b
->accept_fd
)
456 r
= bus_socket_auth_write(b
, "ERROR\r\n");
459 r
= bus_socket_auth_write(b
, "AGREE_UNIX_FD\r\n");
462 r
= bus_socket_auth_write(b
, "ERROR\r\n");
467 b
->auth_rbegin
= e
+ 2 - (char*) b
->rbuffer
;
473 static int bus_socket_auth_verify(sd_bus
*b
) {
477 return bus_socket_auth_verify_server(b
);
479 return bus_socket_auth_verify_client(b
);
482 static int bus_socket_read_auth(sd_bus
*b
) {
484 struct iovec iov
= {};
490 struct cmsghdr cmsghdr
;
491 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
493 bool handle_cmsg
= false;
496 assert(b
->state
== BUS_AUTHENTICATING
);
498 r
= bus_socket_auth_verify(b
);
502 n
= MAX(256u, b
->rbuffer_size
* 2);
504 if (n
> BUS_AUTH_SIZE_MAX
)
505 n
= BUS_AUTH_SIZE_MAX
;
507 if (b
->rbuffer_size
>= n
)
510 p
= realloc(b
->rbuffer
, n
);
516 iov
= IOVEC_MAKE((uint8_t *)b
->rbuffer
+ b
->rbuffer_size
, n
- b
->rbuffer_size
);
519 k
= readv(b
->input_fd
, &iov
, 1);
524 mh
.msg_control
= &control
;
525 mh
.msg_controllen
= sizeof(control
);
527 k
= recvmsg(b
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
528 if (k
< 0 && errno
== ENOTSOCK
) {
529 b
->prefer_readv
= true;
530 k
= readv(b
->input_fd
, &iov
, 1);
535 return errno
== EAGAIN
? 0 : -errno
;
539 b
->rbuffer_size
+= k
;
542 struct cmsghdr
*cmsg
;
544 CMSG_FOREACH(cmsg
, &mh
)
545 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
546 cmsg
->cmsg_type
== SCM_RIGHTS
) {
549 /* Whut? We received fds during the auth
550 * protocol? Somebody is playing games with
551 * us. Close them all, and fail */
552 j
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
553 close_many((int*) CMSG_DATA(cmsg
), j
);
556 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
557 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
560 r
= bus_socket_auth_verify(b
);
567 void bus_socket_setup(sd_bus
*b
) {
570 /* Increase the buffers to 8 MB */
571 (void) fd_inc_rcvbuf(b
->input_fd
, SNDBUF_SIZE
);
572 (void) fd_inc_sndbuf(b
->output_fd
, SNDBUF_SIZE
);
574 b
->message_version
= 1;
575 b
->message_endian
= 0;
578 static void bus_get_peercred(sd_bus
*b
) {
582 assert(!b
->ucred_valid
);
584 assert(b
->n_groups
== (size_t) -1);
586 /* Get the peer for socketpair() sockets */
587 b
->ucred_valid
= getpeercred(b
->input_fd
, &b
->ucred
) >= 0;
589 /* Get the SELinux context of the peer */
590 r
= getpeersec(b
->input_fd
, &b
->label
);
591 if (r
< 0 && !IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
592 log_debug_errno(r
, "Failed to determine peer security context: %m");
594 /* Get the list of auxiliary groups of the peer */
595 r
= getpeergroups(b
->input_fd
, &b
->groups
);
597 b
->n_groups
= (size_t) r
;
598 else if (!IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
599 log_debug_errno(r
, "Failed to determine peer's group list: %m");
602 static int bus_socket_start_auth_client(sd_bus
*b
) {
604 const char *auth_suffix
, *auth_prefix
;
608 if (b
->anonymous_auth
) {
609 auth_prefix
= "\0AUTH ANONYMOUS ";
611 /* For ANONYMOUS auth we send some arbitrary "trace" string */
613 b
->auth_buffer
= hexmem("anonymous", l
);
615 char text
[DECIMAL_STR_MAX(uid_t
) + 1];
617 auth_prefix
= "\0AUTH EXTERNAL ";
619 xsprintf(text
, UID_FMT
, geteuid());
622 b
->auth_buffer
= hexmem(text
, l
);
629 auth_suffix
= "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
631 auth_suffix
= "\r\nBEGIN\r\n";
633 b
->auth_iovec
[0] = IOVEC_MAKE((void*) auth_prefix
, 1 + strlen(auth_prefix
+ 1));
634 b
->auth_iovec
[1] = IOVEC_MAKE(b
->auth_buffer
, l
* 2);
635 b
->auth_iovec
[2] = IOVEC_MAKE_STRING(auth_suffix
);
637 return bus_socket_write_auth(b
);
640 int bus_socket_start_auth(sd_bus
*b
) {
645 bus_set_state(b
, BUS_AUTHENTICATING
);
646 b
->auth_timeout
= now(CLOCK_MONOTONIC
) + BUS_AUTH_TIMEOUT
;
648 if (sd_is_socket(b
->input_fd
, AF_UNIX
, 0, 0) <= 0)
649 b
->accept_fd
= false;
651 if (b
->output_fd
!= b
->input_fd
)
652 if (sd_is_socket(b
->output_fd
, AF_UNIX
, 0, 0) <= 0)
653 b
->accept_fd
= false;
656 return bus_socket_read_auth(b
);
658 return bus_socket_start_auth_client(b
);
661 static int bus_socket_inotify_setup(sd_bus
*b
) {
662 _cleanup_free_
int *new_watches
= NULL
;
663 _cleanup_free_
char *absolute
= NULL
;
664 size_t n_allocated
= 0, n
= 0, done
= 0, i
;
665 unsigned max_follow
= 32;
670 assert(b
->watch_bind
);
671 assert(b
->sockaddr
.sa
.sa_family
== AF_UNIX
);
672 assert(b
->sockaddr
.un
.sun_path
[0] != 0);
674 /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system socket
675 * appears before connecting to it. The implemented is pretty simplistic: we just subscribe to relevant changes
676 * to all prefix components of the path, and every time we get an event for that we try to reconnect again,
677 * without actually caring what precisely the event we got told us. If we still can't connect we re-subscribe
678 * to all relevant changes of anything in the path, so that our watches include any possibly newly created path
681 if (b
->inotify_fd
< 0) {
682 b
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
683 if (b
->inotify_fd
< 0)
686 b
->inotify_fd
= fd_move_above_stdio(b
->inotify_fd
);
689 /* Make sure the path is NUL terminated */
690 p
= strndupa(b
->sockaddr
.un
.sun_path
, sizeof(b
->sockaddr
.un
.sun_path
));
692 /* Make sure the path is absolute */
693 r
= path_make_absolute_cwd(p
, &absolute
);
697 /* Watch all parent directories, and don't mind any prefix that doesn't exist yet. For the innermost directory
698 * that exists we want to know when files are created or moved into it. For all parents of it we just care if
699 * they are removed or renamed. */
701 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
706 /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a symlink, and
708 wd
= inotify_add_watch(b
->inotify_fd
, "/", IN_CREATE
|IN_MOVED_TO
);
710 r
= log_debug_errno(errno
, "Failed to add inotify watch on /: %m");
713 new_watches
[n
++] = wd
;
716 _cleanup_free_
char *component
= NULL
, *prefix
= NULL
, *destination
= NULL
;
717 size_t n_slashes
, n_component
;
720 n_slashes
= strspn(absolute
+ done
, "/");
721 n_component
= n_slashes
+ strcspn(absolute
+ done
+ n_slashes
, "/");
723 if (n_component
== 0) /* The end */
726 component
= strndup(absolute
+ done
, n_component
);
732 /* A trailing slash? That's a directory, and not a socket then */
733 if (path_equal(component
, "/")) {
738 /* A single dot? Let's eat this up */
739 if (path_equal(component
, "/.")) {
744 prefix
= strndup(absolute
, done
+ n_component
);
750 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
755 wd
= inotify_add_watch(b
->inotify_fd
, prefix
, IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
|IN_DONT_FOLLOW
);
756 log_debug("Added inotify watch for %s on bus %s: %i", prefix
, strna(b
->description
), wd
);
759 if (IN_SET(errno
, ENOENT
, ELOOP
))
760 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
762 r
= log_debug_errno(errno
, "Failed to add inotify watch on %s: %m", empty_to_root(prefix
));
765 new_watches
[n
++] = wd
;
767 /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
768 r
= readlink_malloc(prefix
, &destination
);
769 if (r
== -EINVAL
) { /* not a symlink */
776 if (isempty(destination
)) { /* Empty symlink target? Yuck! */
781 if (max_follow
<= 0) { /* Let's make sure we don't follow symlinks forever */
786 if (path_is_absolute(destination
)) {
787 /* For absolute symlinks we build the new path and start anew */
788 c
= strjoin(destination
, absolute
+ done
+ n_component
);
791 _cleanup_free_
char *t
= NULL
;
793 /* For relative symlinks we replace the last component, and try again */
794 t
= strndup(absolute
, done
);
798 c
= strjoin(t
, "/", destination
, absolute
+ done
+ n_component
);
811 /* And now, let's remove all watches from the previous iteration we don't need anymore */
812 for (i
= 0; i
< b
->n_inotify_watches
; i
++) {
816 for (j
= 0; j
< n
; j
++)
817 if (new_watches
[j
] == b
->inotify_watches
[i
]) {
825 (void) inotify_rm_watch(b
->inotify_fd
, b
->inotify_watches
[i
]);
828 free_and_replace(b
->inotify_watches
, new_watches
);
829 b
->n_inotify_watches
= n
;
834 bus_close_inotify_fd(b
);
838 int bus_socket_connect(sd_bus
*b
) {
839 bool inotify_done
= false;
845 assert(b
->input_fd
< 0);
846 assert(b
->output_fd
< 0);
847 assert(b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
);
849 b
->input_fd
= socket(b
->sockaddr
.sa
.sa_family
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
853 b
->input_fd
= fd_move_above_stdio(b
->input_fd
);
855 b
->output_fd
= b
->input_fd
;
858 if (connect(b
->input_fd
, &b
->sockaddr
.sa
, b
->sockaddr_size
) < 0) {
859 if (errno
== EINPROGRESS
) {
861 /* If we have any inotify watches open, close them now, we don't need them anymore, as
862 * we have successfully initiated a connection */
863 bus_close_inotify_fd(b
);
865 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
866 * we start parsing the address string. The only reason we set the state explicitly
867 * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
868 bus_set_state(b
, BUS_OPENING
);
872 if (IN_SET(errno
, ENOENT
, ECONNREFUSED
) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
874 b
->sockaddr
.sa
.sa_family
== AF_UNIX
&&
875 b
->sockaddr
.un
.sun_path
[0] != 0) {
877 /* This connection attempt failed, let's release the socket for now, and start with a
878 * fresh one when reconnecting. */
882 /* inotify set up already, don't do it again, just return now, and remember
883 * that we are waiting for inotify events now. */
884 bus_set_state(b
, BUS_WATCH_BIND
);
888 /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
889 r
= bus_socket_inotify_setup(b
);
893 /* Let's now try to connect a second time, because in theory there's otherwise a race
894 * here: the socket might have been created in the time between our first connect() and
895 * the time we set up the inotify logic. But let's remember that we set up inotify now,
896 * so that we don't do the connect() more than twice. */
905 /* Yay, established, we don't need no inotify anymore! */
906 bus_close_inotify_fd(b
);
908 return bus_socket_start_auth(b
);
911 int bus_socket_exec(sd_bus
*b
) {
915 assert(b
->input_fd
< 0);
916 assert(b
->output_fd
< 0);
917 assert(b
->exec_path
);
918 assert(b
->busexec_pid
== 0);
920 r
= socketpair(AF_UNIX
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0, s
);
924 r
= safe_fork_full("(sd-busexec)", s
+1, 1, FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
, &b
->busexec_pid
);
932 if (rearrange_stdio(s
[1], s
[1], STDERR_FILENO
) < 0)
936 execvp(b
->exec_path
, b
->exec_argv
);
938 const char *argv
[] = { b
->exec_path
, NULL
};
939 execvp(b
->exec_path
, (char**) argv
);
946 b
->output_fd
= b
->input_fd
= fd_move_above_stdio(s
[0]);
950 return bus_socket_start_auth(b
);
953 int bus_socket_take_fd(sd_bus
*b
) {
958 return bus_socket_start_auth(b
);
961 int bus_socket_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
971 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
973 if (*idx
>= BUS_MESSAGE_SIZE(m
))
976 r
= bus_message_setup_iovec(m
);
980 n
= m
->n_iovec
* sizeof(struct iovec
);
982 memcpy_safe(iov
, m
->iovec
, n
);
985 iovec_advance(iov
, &j
, *idx
);
987 if (bus
->prefer_writev
)
988 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
992 .msg_iovlen
= m
->n_iovec
,
995 if (m
->n_fds
> 0 && *idx
== 0) {
996 struct cmsghdr
*control
;
998 mh
.msg_control
= control
= alloca(CMSG_SPACE(sizeof(int) * m
->n_fds
));
999 mh
.msg_controllen
= control
->cmsg_len
= CMSG_LEN(sizeof(int) * m
->n_fds
);
1000 control
->cmsg_level
= SOL_SOCKET
;
1001 control
->cmsg_type
= SCM_RIGHTS
;
1002 memcpy(CMSG_DATA(control
), m
->fds
, sizeof(int) * m
->n_fds
);
1005 k
= sendmsg(bus
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
1006 if (k
< 0 && errno
== ENOTSOCK
) {
1007 bus
->prefer_writev
= true;
1008 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1013 return errno
== EAGAIN
? 0 : -errno
;
1019 static int bus_socket_read_message_need(sd_bus
*bus
, size_t *need
) {
1026 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1028 if (bus
->rbuffer_size
< sizeof(struct bus_header
)) {
1029 *need
= sizeof(struct bus_header
) + 8;
1031 /* Minimum message size:
1035 * Method Call: +2 string headers
1036 * Signal: +3 string headers
1037 * Method Error: +1 string headers
1039 * Method Reply: +1 uint32 headers
1041 * A string header is at least 9 bytes
1042 * A uint32 header is at least 8 bytes
1044 * Hence the minimum message size of a valid message
1045 * is header + 8 bytes */
1050 a
= ((const uint32_t*) bus
->rbuffer
)[1];
1051 b
= ((const uint32_t*) bus
->rbuffer
)[3];
1053 e
= ((const uint8_t*) bus
->rbuffer
)[0];
1054 if (e
== BUS_LITTLE_ENDIAN
) {
1057 } else if (e
== BUS_BIG_ENDIAN
) {
1063 sum
= (uint64_t) sizeof(struct bus_header
) + (uint64_t) ALIGN_TO(b
, 8) + (uint64_t) a
;
1064 if (sum
>= BUS_MESSAGE_SIZE_MAX
)
1067 *need
= (size_t) sum
;
1071 static int bus_socket_make_message(sd_bus
*bus
, size_t size
) {
1077 assert(bus
->rbuffer_size
>= size
);
1078 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1080 r
= bus_rqueue_make_room(bus
);
1084 if (bus
->rbuffer_size
> size
) {
1085 b
= memdup((const uint8_t*) bus
->rbuffer
+ size
,
1086 bus
->rbuffer_size
- size
);
1092 r
= bus_message_from_malloc(bus
,
1094 bus
->fds
, bus
->n_fds
,
1103 bus
->rbuffer_size
-= size
;
1108 bus
->rqueue
[bus
->rqueue_size
++] = t
;
1113 int bus_socket_read_message(sd_bus
*bus
) {
1115 struct iovec iov
= {};
1121 struct cmsghdr cmsghdr
;
1122 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
1124 bool handle_cmsg
= false;
1127 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1129 r
= bus_socket_read_message_need(bus
, &need
);
1133 if (bus
->rbuffer_size
>= need
)
1134 return bus_socket_make_message(bus
, need
);
1136 b
= realloc(bus
->rbuffer
, need
);
1142 iov
= IOVEC_MAKE((uint8_t *)bus
->rbuffer
+ bus
->rbuffer_size
, need
- bus
->rbuffer_size
);
1144 if (bus
->prefer_readv
)
1145 k
= readv(bus
->input_fd
, &iov
, 1);
1150 mh
.msg_control
= &control
;
1151 mh
.msg_controllen
= sizeof(control
);
1153 k
= recvmsg(bus
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
1154 if (k
< 0 && errno
== ENOTSOCK
) {
1155 bus
->prefer_readv
= true;
1156 k
= readv(bus
->input_fd
, &iov
, 1);
1161 return errno
== EAGAIN
? 0 : -errno
;
1165 bus
->rbuffer_size
+= k
;
1168 struct cmsghdr
*cmsg
;
1170 CMSG_FOREACH(cmsg
, &mh
)
1171 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1172 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1175 n
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
1177 if (!bus
->can_fds
) {
1178 /* Whut? We received fds but this
1179 * isn't actually enabled? Close them,
1182 close_many((int*) CMSG_DATA(cmsg
), n
);
1186 f
= reallocarray(bus
->fds
, bus
->n_fds
+ n
, sizeof(int));
1188 close_many((int*) CMSG_DATA(cmsg
), n
);
1192 for (i
= 0; i
< n
; i
++)
1193 f
[bus
->n_fds
++] = fd_move_above_stdio(((int*) CMSG_DATA(cmsg
))[i
]);
1196 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1197 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1200 r
= bus_socket_read_message_need(bus
, &need
);
1204 if (bus
->rbuffer_size
>= need
)
1205 return bus_socket_make_message(bus
, need
);
1210 int bus_socket_process_opening(sd_bus
*b
) {
1212 socklen_t slen
= sizeof(error
);
1219 assert(b
->state
== BUS_OPENING
);
1225 if (!(p
.revents
& (POLLOUT
|POLLERR
|POLLHUP
)))
1228 r
= getsockopt(b
->output_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &slen
);
1230 b
->last_connect_error
= errno
;
1231 else if (error
!= 0)
1232 b
->last_connect_error
= error
;
1233 else if (p
.revents
& (POLLERR
|POLLHUP
))
1234 b
->last_connect_error
= ECONNREFUSED
;
1236 return bus_socket_start_auth(b
);
1238 return bus_next_address(b
);
1241 int bus_socket_process_authenticating(sd_bus
*b
) {
1245 assert(b
->state
== BUS_AUTHENTICATING
);
1247 if (now(CLOCK_MONOTONIC
) >= b
->auth_timeout
)
1250 r
= bus_socket_write_auth(b
);
1254 return bus_socket_read_auth(b
);
1257 int bus_socket_process_watch_bind(sd_bus
*b
) {
1261 assert(b
->state
== BUS_WATCH_BIND
);
1262 assert(b
->inotify_fd
>= 0);
1264 r
= flush_fd(b
->inotify_fd
);
1268 log_debug("Got inotify event on bus %s.", strna(b
->description
));
1270 /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
1273 r
= bus_socket_connect(b
);
1277 q
= bus_attach_io_events(b
);
1281 q
= bus_attach_inotify_event(b
);