]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-socket.c
be491c957f6912109cf0a9ceb34fa211fc3ca7f2
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
;
54 static int append_iovec(sd_bus_message
*m
, const void *p
, size_t sz
) {
59 m
->iovec
[m
->n_iovec
].iov_base
= (void*) p
;
60 m
->iovec
[m
->n_iovec
].iov_len
= sz
;
66 static int bus_message_setup_iovec(sd_bus_message
*m
) {
67 struct bus_body_part
*part
;
79 n
= 1 + m
->n_body_parts
;
80 if (n
< ELEMENTSOF(m
->iovec_fixed
))
81 m
->iovec
= m
->iovec_fixed
;
83 m
->iovec
= new(struct iovec
, n
);
90 r
= append_iovec(m
, m
->header
, BUS_MESSAGE_BODY_BEGIN(m
));
94 MESSAGE_FOREACH_PART(part
, i
, m
) {
95 r
= bus_body_part_map(part
);
99 r
= append_iovec(m
, part
->data
, part
->size
);
104 assert(n
== m
->n_iovec
);
113 bool bus_socket_auth_needs_write(sd_bus
*b
) {
117 if (b
->auth_index
>= ELEMENTSOF(b
->auth_iovec
))
120 for (i
= b
->auth_index
; i
< ELEMENTSOF(b
->auth_iovec
); i
++) {
121 struct iovec
*j
= b
->auth_iovec
+ i
;
130 static int bus_socket_write_auth(sd_bus
*b
) {
134 assert(b
->state
== BUS_AUTHENTICATING
);
136 if (!bus_socket_auth_needs_write(b
))
139 if (b
->prefer_writev
)
140 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
145 mh
.msg_iov
= b
->auth_iovec
+ b
->auth_index
;
146 mh
.msg_iovlen
= ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
;
148 k
= sendmsg(b
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
149 if (k
< 0 && errno
== ENOTSOCK
) {
150 b
->prefer_writev
= true;
151 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
156 return errno
== EAGAIN
? 0 : -errno
;
158 iovec_advance(b
->auth_iovec
, &b
->auth_index
, (size_t) k
);
162 static int bus_socket_auth_verify_client(sd_bus
*b
) {
170 /* We expect two response lines: "OK" and possibly
173 e
= memmem_safe(b
->rbuffer
, b
->rbuffer_size
, "\r\n", 2);
178 f
= memmem(e
+ 2, b
->rbuffer_size
- (e
- (char*) b
->rbuffer
) - 2, "\r\n", 2);
188 /* Nice! We got all the lines we need. First check the OK
191 if (e
- (char*) b
->rbuffer
!= 3 + 32)
194 if (memcmp(b
->rbuffer
, "OK ", 3))
197 b
->auth
= b
->anonymous_auth
? BUS_AUTH_ANONYMOUS
: BUS_AUTH_EXTERNAL
;
199 for (i
= 0; i
< 32; i
+= 2) {
202 x
= unhexchar(((char*) b
->rbuffer
)[3 + i
]);
203 y
= unhexchar(((char*) b
->rbuffer
)[3 + i
+ 1]);
208 peer
.bytes
[i
/2] = ((uint8_t) x
<< 4 | (uint8_t) y
);
211 if (!sd_id128_is_null(b
->server_id
) &&
212 !sd_id128_equal(b
->server_id
, peer
))
217 /* And possibly check the second line, too */
221 (f
- e
== STRLEN("\r\nAGREE_UNIX_FD")) &&
222 memcmp(e
+ 2, "AGREE_UNIX_FD",
223 STRLEN("AGREE_UNIX_FD")) == 0;
225 b
->rbuffer_size
-= (start
- (char*) b
->rbuffer
);
226 memmove(b
->rbuffer
, start
, b
->rbuffer_size
);
228 r
= bus_start_running(b
);
235 static bool line_equals(const char *s
, size_t m
, const char *line
) {
242 return memcmp(s
, line
, l
) == 0;
245 static bool line_begins(const char *s
, size_t m
, const char *word
) {
248 p
= memory_startswith(s
, m
, word
);
252 return IN_SET(*p
, 0, ' ');
255 static int verify_anonymous_token(sd_bus
*b
, const char *p
, size_t l
) {
256 _cleanup_free_
char *token
= NULL
;
260 if (!b
->anonymous_auth
)
272 r
= unhexmem(p
, l
, (void **) &token
, &len
);
276 if (memchr(token
, 0, len
))
279 return !!utf8_is_valid(token
);
282 static int verify_external_token(sd_bus
*b
, const char *p
, size_t l
) {
283 _cleanup_free_
char *token
= NULL
;
288 /* We don't do any real authentication here. Instead, we if
289 * the owner of this bus wanted authentication he should have
290 * checked SO_PEERCRED before even creating the bus object. */
292 if (!b
->anonymous_auth
&& !b
->ucred_valid
)
304 r
= unhexmem(p
, l
, (void**) &token
, &len
);
308 if (memchr(token
, 0, len
))
311 r
= parse_uid(token
, &u
);
315 /* We ignore the passed value if anonymous authentication is
317 if (!b
->anonymous_auth
&& u
!= b
->ucred
.uid
)
323 static int bus_socket_auth_write(sd_bus
*b
, const char *t
) {
330 /* We only make use of the first iovec */
331 assert(IN_SET(b
->auth_index
, 0, 1));
334 p
= malloc(b
->auth_iovec
[0].iov_len
+ l
);
338 memcpy_safe(p
, b
->auth_iovec
[0].iov_base
, b
->auth_iovec
[0].iov_len
);
339 memcpy(p
+ b
->auth_iovec
[0].iov_len
, t
, l
);
341 b
->auth_iovec
[0].iov_base
= p
;
342 b
->auth_iovec
[0].iov_len
+= l
;
344 free(b
->auth_buffer
);
350 static int bus_socket_auth_write_ok(sd_bus
*b
) {
351 char t
[3 + 32 + 2 + 1];
355 xsprintf(t
, "OK " SD_ID128_FORMAT_STR
"\r\n", SD_ID128_FORMAT_VAL(b
->server_id
));
357 return bus_socket_auth_write(b
, t
);
360 static int bus_socket_auth_verify_server(sd_bus
*b
) {
364 bool processed
= false;
369 if (b
->rbuffer_size
< 1)
372 /* First char must be a NUL byte */
373 if (*(char*) b
->rbuffer
!= 0)
376 if (b
->rbuffer_size
< 3)
379 /* Begin with the first line */
380 if (b
->auth_rbegin
<= 0)
384 /* Check if line is complete */
385 line
= (char*) b
->rbuffer
+ b
->auth_rbegin
;
386 e
= memmem(line
, b
->rbuffer_size
- b
->auth_rbegin
, "\r\n", 2);
392 if (line_begins(line
, l
, "AUTH ANONYMOUS")) {
394 r
= verify_anonymous_token(b
, line
+ 14, l
- 14);
398 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
400 b
->auth
= BUS_AUTH_ANONYMOUS
;
401 r
= bus_socket_auth_write_ok(b
);
404 } else if (line_begins(line
, l
, "AUTH EXTERNAL")) {
406 r
= verify_external_token(b
, line
+ 13, l
- 13);
410 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
412 b
->auth
= BUS_AUTH_EXTERNAL
;
413 r
= bus_socket_auth_write_ok(b
);
416 } else if (line_begins(line
, l
, "AUTH"))
417 r
= bus_socket_auth_write(b
, "REJECTED EXTERNAL ANONYMOUS\r\n");
418 else if (line_equals(line
, l
, "CANCEL") ||
419 line_begins(line
, l
, "ERROR")) {
421 b
->auth
= _BUS_AUTH_INVALID
;
422 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
424 } else if (line_equals(line
, l
, "BEGIN")) {
426 if (b
->auth
== _BUS_AUTH_INVALID
)
427 r
= bus_socket_auth_write(b
, "ERROR\r\n");
429 /* We can't leave from the auth phase
430 * before we haven't written
431 * everything queued, so let's check
434 if (bus_socket_auth_needs_write(b
))
437 b
->rbuffer_size
-= (e
+ 2 - (char*) b
->rbuffer
);
438 memmove(b
->rbuffer
, e
+ 2, b
->rbuffer_size
);
439 return bus_start_running(b
);
442 } else if (line_begins(line
, l
, "DATA")) {
444 if (b
->auth
== _BUS_AUTH_INVALID
)
445 r
= bus_socket_auth_write(b
, "ERROR\r\n");
447 if (b
->auth
== BUS_AUTH_ANONYMOUS
)
448 r
= verify_anonymous_token(b
, line
+ 4, l
- 4);
450 r
= verify_external_token(b
, line
+ 4, l
- 4);
455 b
->auth
= _BUS_AUTH_INVALID
;
456 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
458 r
= bus_socket_auth_write_ok(b
);
460 } else if (line_equals(line
, l
, "NEGOTIATE_UNIX_FD")) {
461 if (b
->auth
== _BUS_AUTH_INVALID
|| !b
->accept_fd
)
462 r
= bus_socket_auth_write(b
, "ERROR\r\n");
465 r
= bus_socket_auth_write(b
, "AGREE_UNIX_FD\r\n");
468 r
= bus_socket_auth_write(b
, "ERROR\r\n");
473 b
->auth_rbegin
= e
+ 2 - (char*) b
->rbuffer
;
479 static int bus_socket_auth_verify(sd_bus
*b
) {
483 return bus_socket_auth_verify_server(b
);
485 return bus_socket_auth_verify_client(b
);
488 static int bus_socket_read_auth(sd_bus
*b
) {
490 struct iovec iov
= {};
496 struct cmsghdr cmsghdr
;
497 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
499 bool handle_cmsg
= false;
502 assert(b
->state
== BUS_AUTHENTICATING
);
504 r
= bus_socket_auth_verify(b
);
508 n
= MAX(256u, b
->rbuffer_size
* 2);
510 if (n
> BUS_AUTH_SIZE_MAX
)
511 n
= BUS_AUTH_SIZE_MAX
;
513 if (b
->rbuffer_size
>= n
)
516 p
= realloc(b
->rbuffer
, n
);
522 iov
.iov_base
= (uint8_t*) b
->rbuffer
+ b
->rbuffer_size
;
523 iov
.iov_len
= n
- b
->rbuffer_size
;
526 k
= readv(b
->input_fd
, &iov
, 1);
531 mh
.msg_control
= &control
;
532 mh
.msg_controllen
= sizeof(control
);
534 k
= recvmsg(b
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
535 if (k
< 0 && errno
== ENOTSOCK
) {
536 b
->prefer_readv
= true;
537 k
= readv(b
->input_fd
, &iov
, 1);
542 return errno
== EAGAIN
? 0 : -errno
;
546 b
->rbuffer_size
+= k
;
549 struct cmsghdr
*cmsg
;
551 CMSG_FOREACH(cmsg
, &mh
)
552 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
553 cmsg
->cmsg_type
== SCM_RIGHTS
) {
556 /* Whut? We received fds during the auth
557 * protocol? Somebody is playing games with
558 * us. Close them all, and fail */
559 j
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
560 close_many((int*) CMSG_DATA(cmsg
), j
);
563 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
564 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
567 r
= bus_socket_auth_verify(b
);
574 void bus_socket_setup(sd_bus
*b
) {
577 /* Increase the buffers to 8 MB */
578 (void) fd_inc_rcvbuf(b
->input_fd
, SNDBUF_SIZE
);
579 (void) fd_inc_sndbuf(b
->output_fd
, SNDBUF_SIZE
);
581 b
->message_version
= 1;
582 b
->message_endian
= 0;
585 static void bus_get_peercred(sd_bus
*b
) {
589 assert(!b
->ucred_valid
);
591 assert(b
->n_groups
== (size_t) -1);
593 /* Get the peer for socketpair() sockets */
594 b
->ucred_valid
= getpeercred(b
->input_fd
, &b
->ucred
) >= 0;
596 /* Get the SELinux context of the peer */
597 r
= getpeersec(b
->input_fd
, &b
->label
);
598 if (r
< 0 && !IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
599 log_debug_errno(r
, "Failed to determine peer security context: %m");
601 /* Get the list of auxiliary groups of the peer */
602 r
= getpeergroups(b
->input_fd
, &b
->groups
);
604 b
->n_groups
= (size_t) r
;
605 else if (!IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
606 log_debug_errno(r
, "Failed to determine peer's group list: %m");
609 static int bus_socket_start_auth_client(sd_bus
*b
) {
611 const char *auth_suffix
, *auth_prefix
;
615 if (b
->anonymous_auth
) {
616 auth_prefix
= "\0AUTH ANONYMOUS ";
618 /* For ANONYMOUS auth we send some arbitrary "trace" string */
620 b
->auth_buffer
= hexmem("anonymous", l
);
622 char text
[DECIMAL_STR_MAX(uid_t
) + 1];
624 auth_prefix
= "\0AUTH EXTERNAL ";
626 xsprintf(text
, UID_FMT
, geteuid());
629 b
->auth_buffer
= hexmem(text
, l
);
636 auth_suffix
= "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
638 auth_suffix
= "\r\nBEGIN\r\n";
640 b
->auth_iovec
[0].iov_base
= (void*) auth_prefix
;
641 b
->auth_iovec
[0].iov_len
= 1 + strlen(auth_prefix
+ 1);
642 b
->auth_iovec
[1].iov_base
= (void*) b
->auth_buffer
;
643 b
->auth_iovec
[1].iov_len
= l
* 2;
644 b
->auth_iovec
[2].iov_base
= (void*) auth_suffix
;
645 b
->auth_iovec
[2].iov_len
= strlen(auth_suffix
);
647 return bus_socket_write_auth(b
);
650 int bus_socket_start_auth(sd_bus
*b
) {
655 bus_set_state(b
, BUS_AUTHENTICATING
);
656 b
->auth_timeout
= now(CLOCK_MONOTONIC
) + BUS_AUTH_TIMEOUT
;
658 if (sd_is_socket(b
->input_fd
, AF_UNIX
, 0, 0) <= 0)
659 b
->accept_fd
= false;
661 if (b
->output_fd
!= b
->input_fd
)
662 if (sd_is_socket(b
->output_fd
, AF_UNIX
, 0, 0) <= 0)
663 b
->accept_fd
= false;
666 return bus_socket_read_auth(b
);
668 return bus_socket_start_auth_client(b
);
671 static int bus_socket_inotify_setup(sd_bus
*b
) {
672 _cleanup_free_
int *new_watches
= NULL
;
673 _cleanup_free_
char *absolute
= NULL
;
674 size_t n_allocated
= 0, n
= 0, done
= 0, i
;
675 unsigned max_follow
= 32;
680 assert(b
->watch_bind
);
681 assert(b
->sockaddr
.sa
.sa_family
== AF_UNIX
);
682 assert(b
->sockaddr
.un
.sun_path
[0] != 0);
684 /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system socket
685 * appears before connecting to it. The implemented is pretty simplistic: we just subscribe to relevant changes
686 * to all prefix components of the path, and every time we get an event for that we try to reconnect again,
687 * without actually caring what precisely the event we got told us. If we still can't connect we re-subscribe
688 * to all relevant changes of anything in the path, so that our watches include any possibly newly created path
691 if (b
->inotify_fd
< 0) {
692 b
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
693 if (b
->inotify_fd
< 0)
696 b
->inotify_fd
= fd_move_above_stdio(b
->inotify_fd
);
699 /* Make sure the path is NUL terminated */
700 p
= strndupa(b
->sockaddr
.un
.sun_path
, sizeof(b
->sockaddr
.un
.sun_path
));
702 /* Make sure the path is absolute */
703 r
= path_make_absolute_cwd(p
, &absolute
);
707 /* Watch all parent directories, and don't mind any prefix that doesn't exist yet. For the innermost directory
708 * that exists we want to know when files are created or moved into it. For all parents of it we just care if
709 * they are removed or renamed. */
711 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
716 /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a symlink, and
718 wd
= inotify_add_watch(b
->inotify_fd
, "/", IN_CREATE
|IN_MOVED_TO
);
720 r
= log_debug_errno(errno
, "Failed to add inotify watch on /: %m");
723 new_watches
[n
++] = wd
;
726 _cleanup_free_
char *component
= NULL
, *prefix
= NULL
, *destination
= NULL
;
727 size_t n_slashes
, n_component
;
730 n_slashes
= strspn(absolute
+ done
, "/");
731 n_component
= n_slashes
+ strcspn(absolute
+ done
+ n_slashes
, "/");
733 if (n_component
== 0) /* The end */
736 component
= strndup(absolute
+ done
, n_component
);
742 /* A trailing slash? That's a directory, and not a socket then */
743 if (path_equal(component
, "/")) {
748 /* A single dot? Let's eat this up */
749 if (path_equal(component
, "/.")) {
754 prefix
= strndup(absolute
, done
+ n_component
);
760 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
765 wd
= inotify_add_watch(b
->inotify_fd
, prefix
, IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
|IN_DONT_FOLLOW
);
766 log_debug("Added inotify watch for %s on bus %s: %i", prefix
, strna(b
->description
), wd
);
769 if (IN_SET(errno
, ENOENT
, ELOOP
))
770 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
772 r
= log_debug_errno(errno
, "Failed to add inotify watch on %s: %m", empty_to_root(prefix
));
775 new_watches
[n
++] = wd
;
777 /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
778 r
= readlink_malloc(prefix
, &destination
);
779 if (r
== -EINVAL
) { /* not a symlink */
786 if (isempty(destination
)) { /* Empty symlink target? Yuck! */
791 if (max_follow
<= 0) { /* Let's make sure we don't follow symlinks forever */
796 if (path_is_absolute(destination
)) {
797 /* For absolute symlinks we build the new path and start anew */
798 c
= strjoin(destination
, absolute
+ done
+ n_component
);
801 _cleanup_free_
char *t
= NULL
;
803 /* For relative symlinks we replace the last component, and try again */
804 t
= strndup(absolute
, done
);
808 c
= strjoin(t
, "/", destination
, absolute
+ done
+ n_component
);
821 /* And now, let's remove all watches from the previous iteration we don't need anymore */
822 for (i
= 0; i
< b
->n_inotify_watches
; i
++) {
826 for (j
= 0; j
< n
; j
++)
827 if (new_watches
[j
] == b
->inotify_watches
[i
]) {
835 (void) inotify_rm_watch(b
->inotify_fd
, b
->inotify_watches
[i
]);
838 free_and_replace(b
->inotify_watches
, new_watches
);
839 b
->n_inotify_watches
= n
;
844 bus_close_inotify_fd(b
);
848 int bus_socket_connect(sd_bus
*b
) {
849 bool inotify_done
= false;
855 assert(b
->input_fd
< 0);
856 assert(b
->output_fd
< 0);
857 assert(b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
);
859 b
->input_fd
= socket(b
->sockaddr
.sa
.sa_family
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
863 b
->input_fd
= fd_move_above_stdio(b
->input_fd
);
865 b
->output_fd
= b
->input_fd
;
868 if (connect(b
->input_fd
, &b
->sockaddr
.sa
, b
->sockaddr_size
) < 0) {
869 if (errno
== EINPROGRESS
) {
871 /* If we have any inotify watches open, close them now, we don't need them anymore, as
872 * we have successfully initiated a connection */
873 bus_close_inotify_fd(b
);
875 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
876 * we start parsing the address string. The only reason we set the state explicitly
877 * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
878 bus_set_state(b
, BUS_OPENING
);
882 if (IN_SET(errno
, ENOENT
, ECONNREFUSED
) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
884 b
->sockaddr
.sa
.sa_family
== AF_UNIX
&&
885 b
->sockaddr
.un
.sun_path
[0] != 0) {
887 /* This connection attempt failed, let's release the socket for now, and start with a
888 * fresh one when reconnecting. */
892 /* inotify set up already, don't do it again, just return now, and remember
893 * that we are waiting for inotify events now. */
894 bus_set_state(b
, BUS_WATCH_BIND
);
898 /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
899 r
= bus_socket_inotify_setup(b
);
903 /* Let's now try to connect a second time, because in theory there's otherwise a race
904 * here: the socket might have been created in the time between our first connect() and
905 * the time we set up the inotify logic. But let's remember that we set up inotify now,
906 * so that we don't do the connect() more than twice. */
915 /* Yay, established, we don't need no inotify anymore! */
916 bus_close_inotify_fd(b
);
918 return bus_socket_start_auth(b
);
921 int bus_socket_exec(sd_bus
*b
) {
925 assert(b
->input_fd
< 0);
926 assert(b
->output_fd
< 0);
927 assert(b
->exec_path
);
928 assert(b
->busexec_pid
== 0);
930 r
= socketpair(AF_UNIX
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0, s
);
934 r
= safe_fork_full("(sd-busexec)", s
+1, 1, FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
, &b
->busexec_pid
);
942 if (rearrange_stdio(s
[1], s
[1], STDERR_FILENO
) < 0)
946 execvp(b
->exec_path
, b
->exec_argv
);
948 const char *argv
[] = { b
->exec_path
, NULL
};
949 execvp(b
->exec_path
, (char**) argv
);
956 b
->output_fd
= b
->input_fd
= fd_move_above_stdio(s
[0]);
960 return bus_socket_start_auth(b
);
963 int bus_socket_take_fd(sd_bus
*b
) {
968 return bus_socket_start_auth(b
);
971 int bus_socket_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
981 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
983 if (*idx
>= BUS_MESSAGE_SIZE(m
))
986 r
= bus_message_setup_iovec(m
);
990 n
= m
->n_iovec
* sizeof(struct iovec
);
992 memcpy_safe(iov
, m
->iovec
, n
);
995 iovec_advance(iov
, &j
, *idx
);
997 if (bus
->prefer_writev
)
998 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1000 struct msghdr mh
= {
1002 .msg_iovlen
= m
->n_iovec
,
1005 if (m
->n_fds
> 0 && *idx
== 0) {
1006 struct cmsghdr
*control
;
1008 mh
.msg_control
= control
= alloca(CMSG_SPACE(sizeof(int) * m
->n_fds
));
1009 mh
.msg_controllen
= control
->cmsg_len
= CMSG_LEN(sizeof(int) * m
->n_fds
);
1010 control
->cmsg_level
= SOL_SOCKET
;
1011 control
->cmsg_type
= SCM_RIGHTS
;
1012 memcpy(CMSG_DATA(control
), m
->fds
, sizeof(int) * m
->n_fds
);
1015 k
= sendmsg(bus
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
1016 if (k
< 0 && errno
== ENOTSOCK
) {
1017 bus
->prefer_writev
= true;
1018 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1023 return errno
== EAGAIN
? 0 : -errno
;
1029 static int bus_socket_read_message_need(sd_bus
*bus
, size_t *need
) {
1036 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1038 if (bus
->rbuffer_size
< sizeof(struct bus_header
)) {
1039 *need
= sizeof(struct bus_header
) + 8;
1041 /* Minimum message size:
1045 * Method Call: +2 string headers
1046 * Signal: +3 string headers
1047 * Method Error: +1 string headers
1049 * Method Reply: +1 uint32 headers
1051 * A string header is at least 9 bytes
1052 * A uint32 header is at least 8 bytes
1054 * Hence the minimum message size of a valid message
1055 * is header + 8 bytes */
1060 a
= ((const uint32_t*) bus
->rbuffer
)[1];
1061 b
= ((const uint32_t*) bus
->rbuffer
)[3];
1063 e
= ((const uint8_t*) bus
->rbuffer
)[0];
1064 if (e
== BUS_LITTLE_ENDIAN
) {
1067 } else if (e
== BUS_BIG_ENDIAN
) {
1073 sum
= (uint64_t) sizeof(struct bus_header
) + (uint64_t) ALIGN_TO(b
, 8) + (uint64_t) a
;
1074 if (sum
>= BUS_MESSAGE_SIZE_MAX
)
1077 *need
= (size_t) sum
;
1081 static int bus_socket_make_message(sd_bus
*bus
, size_t size
) {
1087 assert(bus
->rbuffer_size
>= size
);
1088 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1090 r
= bus_rqueue_make_room(bus
);
1094 if (bus
->rbuffer_size
> size
) {
1095 b
= memdup((const uint8_t*) bus
->rbuffer
+ size
,
1096 bus
->rbuffer_size
- size
);
1102 r
= bus_message_from_malloc(bus
,
1104 bus
->fds
, bus
->n_fds
,
1113 bus
->rbuffer_size
-= size
;
1118 bus
->rqueue
[bus
->rqueue_size
++] = t
;
1123 int bus_socket_read_message(sd_bus
*bus
) {
1125 struct iovec iov
= {};
1131 struct cmsghdr cmsghdr
;
1132 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
1134 bool handle_cmsg
= false;
1137 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1139 r
= bus_socket_read_message_need(bus
, &need
);
1143 if (bus
->rbuffer_size
>= need
)
1144 return bus_socket_make_message(bus
, need
);
1146 b
= realloc(bus
->rbuffer
, need
);
1152 iov
.iov_base
= (uint8_t*) bus
->rbuffer
+ bus
->rbuffer_size
;
1153 iov
.iov_len
= need
- bus
->rbuffer_size
;
1155 if (bus
->prefer_readv
)
1156 k
= readv(bus
->input_fd
, &iov
, 1);
1161 mh
.msg_control
= &control
;
1162 mh
.msg_controllen
= sizeof(control
);
1164 k
= recvmsg(bus
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
1165 if (k
< 0 && errno
== ENOTSOCK
) {
1166 bus
->prefer_readv
= true;
1167 k
= readv(bus
->input_fd
, &iov
, 1);
1172 return errno
== EAGAIN
? 0 : -errno
;
1176 bus
->rbuffer_size
+= k
;
1179 struct cmsghdr
*cmsg
;
1181 CMSG_FOREACH(cmsg
, &mh
)
1182 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1183 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1186 n
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
1188 if (!bus
->can_fds
) {
1189 /* Whut? We received fds but this
1190 * isn't actually enabled? Close them,
1193 close_many((int*) CMSG_DATA(cmsg
), n
);
1197 f
= reallocarray(bus
->fds
, bus
->n_fds
+ n
, sizeof(int));
1199 close_many((int*) CMSG_DATA(cmsg
), n
);
1203 for (i
= 0; i
< n
; i
++)
1204 f
[bus
->n_fds
++] = fd_move_above_stdio(((int*) CMSG_DATA(cmsg
))[i
]);
1207 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1208 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1211 r
= bus_socket_read_message_need(bus
, &need
);
1215 if (bus
->rbuffer_size
>= need
)
1216 return bus_socket_make_message(bus
, need
);
1221 int bus_socket_process_opening(sd_bus
*b
) {
1223 socklen_t slen
= sizeof(error
);
1230 assert(b
->state
== BUS_OPENING
);
1236 if (!(p
.revents
& (POLLOUT
|POLLERR
|POLLHUP
)))
1239 r
= getsockopt(b
->output_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &slen
);
1241 b
->last_connect_error
= errno
;
1242 else if (error
!= 0)
1243 b
->last_connect_error
= error
;
1244 else if (p
.revents
& (POLLERR
|POLLHUP
))
1245 b
->last_connect_error
= ECONNREFUSED
;
1247 return bus_socket_start_auth(b
);
1249 return bus_next_address(b
);
1252 int bus_socket_process_authenticating(sd_bus
*b
) {
1256 assert(b
->state
== BUS_AUTHENTICATING
);
1258 if (now(CLOCK_MONOTONIC
) >= b
->auth_timeout
)
1261 r
= bus_socket_write_auth(b
);
1265 return bus_socket_read_auth(b
);
1268 int bus_socket_process_watch_bind(sd_bus
*b
) {
1272 assert(b
->state
== BUS_WATCH_BIND
);
1273 assert(b
->inotify_fd
>= 0);
1275 r
= flush_fd(b
->inotify_fd
);
1279 log_debug("Got inotify event on bus %s.", strna(b
->description
));
1281 /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
1284 r
= bus_socket_connect(b
);
1288 q
= bus_attach_io_events(b
);
1292 q
= bus_attach_inotify_event(b
);