]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-socket.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "sd-daemon.h"
13 #include "alloc-util.h"
14 #include "bus-internal.h"
15 #include "bus-message.h"
16 #include "bus-socket.h"
18 #include "format-util.h"
20 #include "hexdecoct.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "selinux-util.h"
27 #include "signal-util.h"
28 #include "stdio-util.h"
29 #include "string-util.h"
30 #include "user-util.h"
34 #define SNDBUF_SIZE (8*1024*1024)
36 static void iovec_advance(struct iovec iov
[], unsigned *idx
, size_t size
) {
39 struct iovec
*i
= iov
+ *idx
;
41 if (i
->iov_len
> size
) {
42 i
->iov_base
= (uint8_t*) i
->iov_base
+ size
;
56 static int append_iovec(sd_bus_message
*m
, const void *p
, size_t sz
) {
61 m
->iovec
[m
->n_iovec
].iov_base
= (void*) p
;
62 m
->iovec
[m
->n_iovec
].iov_len
= sz
;
68 static int bus_message_setup_iovec(sd_bus_message
*m
) {
69 struct bus_body_part
*part
;
81 n
= 1 + m
->n_body_parts
;
82 if (n
< ELEMENTSOF(m
->iovec_fixed
))
83 m
->iovec
= m
->iovec_fixed
;
85 m
->iovec
= new(struct iovec
, n
);
92 r
= append_iovec(m
, m
->header
, BUS_MESSAGE_BODY_BEGIN(m
));
96 MESSAGE_FOREACH_PART(part
, i
, m
) {
97 r
= bus_body_part_map(part
);
101 r
= append_iovec(m
, part
->data
, part
->size
);
106 assert(n
== m
->n_iovec
);
115 bool bus_socket_auth_needs_write(sd_bus
*b
) {
119 if (b
->auth_index
>= ELEMENTSOF(b
->auth_iovec
))
122 for (i
= b
->auth_index
; i
< ELEMENTSOF(b
->auth_iovec
); i
++) {
123 struct iovec
*j
= b
->auth_iovec
+ i
;
132 static int bus_socket_write_auth(sd_bus
*b
) {
136 assert(b
->state
== BUS_AUTHENTICATING
);
138 if (!bus_socket_auth_needs_write(b
))
141 if (b
->prefer_writev
)
142 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
147 mh
.msg_iov
= b
->auth_iovec
+ b
->auth_index
;
148 mh
.msg_iovlen
= ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
;
150 k
= sendmsg(b
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
151 if (k
< 0 && errno
== ENOTSOCK
) {
152 b
->prefer_writev
= true;
153 k
= writev(b
->output_fd
, b
->auth_iovec
+ b
->auth_index
, ELEMENTSOF(b
->auth_iovec
) - b
->auth_index
);
158 return errno
== EAGAIN
? 0 : -errno
;
160 iovec_advance(b
->auth_iovec
, &b
->auth_index
, (size_t) k
);
164 static int bus_socket_auth_verify_client(sd_bus
*b
) {
172 /* We expect two response lines: "OK" and possibly
175 e
= memmem_safe(b
->rbuffer
, b
->rbuffer_size
, "\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 OK
193 if (e
- (char*) b
->rbuffer
!= 3 + 32)
196 if (memcmp(b
->rbuffer
, "OK ", 3))
199 b
->auth
= b
->anonymous_auth
? BUS_AUTH_ANONYMOUS
: BUS_AUTH_EXTERNAL
;
201 for (i
= 0; i
< 32; i
+= 2) {
204 x
= unhexchar(((char*) b
->rbuffer
)[3 + i
]);
205 y
= unhexchar(((char*) b
->rbuffer
)[3 + i
+ 1]);
210 peer
.bytes
[i
/2] = ((uint8_t) x
<< 4 | (uint8_t) y
);
213 if (!sd_id128_is_null(b
->server_id
) &&
214 !sd_id128_equal(b
->server_id
, peer
))
219 /* And possibly check the second line, too */
223 (f
- e
== STRLEN("\r\nAGREE_UNIX_FD")) &&
224 memcmp(e
+ 2, "AGREE_UNIX_FD",
225 STRLEN("AGREE_UNIX_FD")) == 0;
227 b
->rbuffer_size
-= (start
- (char*) b
->rbuffer
);
228 memmove(b
->rbuffer
, start
, b
->rbuffer_size
);
230 r
= bus_start_running(b
);
237 static bool line_equals(const char *s
, size_t m
, const char *line
) {
244 return memcmp(s
, line
, l
) == 0;
247 static bool line_begins(const char *s
, size_t m
, const char *word
) {
250 p
= memory_startswith(s
, m
, word
);
254 return IN_SET(*p
, 0, ' ');
257 static int verify_anonymous_token(sd_bus
*b
, const char *p
, size_t l
) {
258 _cleanup_free_
char *token
= NULL
;
262 if (!b
->anonymous_auth
)
274 r
= unhexmem(p
, l
, (void **) &token
, &len
);
278 if (memchr(token
, 0, len
))
281 return !!utf8_is_valid(token
);
284 static int verify_external_token(sd_bus
*b
, const char *p
, size_t l
) {
285 _cleanup_free_
char *token
= NULL
;
290 /* We don't do any real authentication here. Instead, we if
291 * the owner of this bus wanted authentication he should have
292 * checked SO_PEERCRED before even creating the bus object. */
294 if (!b
->anonymous_auth
&& !b
->ucred_valid
)
306 r
= unhexmem(p
, l
, (void**) &token
, &len
);
310 if (memchr(token
, 0, len
))
313 r
= parse_uid(token
, &u
);
317 /* We ignore the passed value if anonymous authentication is
319 if (!b
->anonymous_auth
&& u
!= b
->ucred
.uid
)
325 static int bus_socket_auth_write(sd_bus
*b
, const char *t
) {
332 /* We only make use of the first iovec */
333 assert(IN_SET(b
->auth_index
, 0, 1));
336 p
= malloc(b
->auth_iovec
[0].iov_len
+ l
);
340 memcpy_safe(p
, b
->auth_iovec
[0].iov_base
, b
->auth_iovec
[0].iov_len
);
341 memcpy(p
+ b
->auth_iovec
[0].iov_len
, t
, l
);
343 b
->auth_iovec
[0].iov_base
= p
;
344 b
->auth_iovec
[0].iov_len
+= l
;
346 free(b
->auth_buffer
);
352 static int bus_socket_auth_write_ok(sd_bus
*b
) {
353 char t
[3 + 32 + 2 + 1];
357 xsprintf(t
, "OK " SD_ID128_FORMAT_STR
"\r\n", SD_ID128_FORMAT_VAL(b
->server_id
));
359 return bus_socket_auth_write(b
, t
);
362 static int bus_socket_auth_verify_server(sd_bus
*b
) {
366 bool processed
= false;
371 if (b
->rbuffer_size
< 1)
374 /* First char must be a NUL byte */
375 if (*(char*) b
->rbuffer
!= 0)
378 if (b
->rbuffer_size
< 3)
381 /* Begin with the first line */
382 if (b
->auth_rbegin
<= 0)
386 /* Check if line is complete */
387 line
= (char*) b
->rbuffer
+ b
->auth_rbegin
;
388 e
= memmem(line
, b
->rbuffer_size
- b
->auth_rbegin
, "\r\n", 2);
394 if (line_begins(line
, l
, "AUTH ANONYMOUS")) {
396 r
= verify_anonymous_token(b
, line
+ 14, l
- 14);
400 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
402 b
->auth
= BUS_AUTH_ANONYMOUS
;
403 r
= bus_socket_auth_write_ok(b
);
406 } else if (line_begins(line
, l
, "AUTH EXTERNAL")) {
408 r
= verify_external_token(b
, line
+ 13, l
- 13);
412 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
414 b
->auth
= BUS_AUTH_EXTERNAL
;
415 r
= bus_socket_auth_write_ok(b
);
418 } else if (line_begins(line
, l
, "AUTH"))
419 r
= bus_socket_auth_write(b
, "REJECTED EXTERNAL ANONYMOUS\r\n");
420 else if (line_equals(line
, l
, "CANCEL") ||
421 line_begins(line
, l
, "ERROR")) {
423 b
->auth
= _BUS_AUTH_INVALID
;
424 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
426 } else if (line_equals(line
, l
, "BEGIN")) {
428 if (b
->auth
== _BUS_AUTH_INVALID
)
429 r
= bus_socket_auth_write(b
, "ERROR\r\n");
431 /* We can't leave from the auth phase
432 * before we haven't written
433 * everything queued, so let's check
436 if (bus_socket_auth_needs_write(b
))
439 b
->rbuffer_size
-= (e
+ 2 - (char*) b
->rbuffer
);
440 memmove(b
->rbuffer
, e
+ 2, b
->rbuffer_size
);
441 return bus_start_running(b
);
444 } else if (line_begins(line
, l
, "DATA")) {
446 if (b
->auth
== _BUS_AUTH_INVALID
)
447 r
= bus_socket_auth_write(b
, "ERROR\r\n");
449 if (b
->auth
== BUS_AUTH_ANONYMOUS
)
450 r
= verify_anonymous_token(b
, line
+ 4, l
- 4);
452 r
= verify_external_token(b
, line
+ 4, l
- 4);
457 b
->auth
= _BUS_AUTH_INVALID
;
458 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
460 r
= bus_socket_auth_write_ok(b
);
462 } else if (line_equals(line
, l
, "NEGOTIATE_UNIX_FD")) {
463 if (b
->auth
== _BUS_AUTH_INVALID
|| !b
->accept_fd
)
464 r
= bus_socket_auth_write(b
, "ERROR\r\n");
467 r
= bus_socket_auth_write(b
, "AGREE_UNIX_FD\r\n");
470 r
= bus_socket_auth_write(b
, "ERROR\r\n");
475 b
->auth_rbegin
= e
+ 2 - (char*) b
->rbuffer
;
481 static int bus_socket_auth_verify(sd_bus
*b
) {
485 return bus_socket_auth_verify_server(b
);
487 return bus_socket_auth_verify_client(b
);
490 static int bus_socket_read_auth(sd_bus
*b
) {
492 struct iovec iov
= {};
498 struct cmsghdr cmsghdr
;
499 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
501 bool handle_cmsg
= false;
504 assert(b
->state
== BUS_AUTHENTICATING
);
506 r
= bus_socket_auth_verify(b
);
510 n
= MAX(256u, b
->rbuffer_size
* 2);
512 if (n
> BUS_AUTH_SIZE_MAX
)
513 n
= BUS_AUTH_SIZE_MAX
;
515 if (b
->rbuffer_size
>= n
)
518 p
= realloc(b
->rbuffer
, n
);
524 iov
.iov_base
= (uint8_t*) b
->rbuffer
+ b
->rbuffer_size
;
525 iov
.iov_len
= n
- b
->rbuffer_size
;
528 k
= readv(b
->input_fd
, &iov
, 1);
533 mh
.msg_control
= &control
;
534 mh
.msg_controllen
= sizeof(control
);
536 k
= recvmsg(b
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
|MSG_CMSG_CLOEXEC
);
537 if (k
< 0 && errno
== ENOTSOCK
) {
538 b
->prefer_readv
= true;
539 k
= readv(b
->input_fd
, &iov
, 1);
544 return errno
== EAGAIN
? 0 : -errno
;
548 b
->rbuffer_size
+= k
;
551 struct cmsghdr
*cmsg
;
553 CMSG_FOREACH(cmsg
, &mh
)
554 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
555 cmsg
->cmsg_type
== SCM_RIGHTS
) {
558 /* Whut? We received fds during the auth
559 * protocol? Somebody is playing games with
560 * us. Close them all, and fail */
561 j
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
562 close_many((int*) CMSG_DATA(cmsg
), j
);
565 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
566 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
569 r
= bus_socket_auth_verify(b
);
576 void bus_socket_setup(sd_bus
*b
) {
579 /* Increase the buffers to 8 MB */
580 (void) fd_inc_rcvbuf(b
->input_fd
, SNDBUF_SIZE
);
581 (void) fd_inc_sndbuf(b
->output_fd
, SNDBUF_SIZE
);
583 b
->message_version
= 1;
584 b
->message_endian
= 0;
587 static void bus_get_peercred(sd_bus
*b
) {
591 assert(!b
->ucred_valid
);
593 assert(b
->n_groups
== (size_t) -1);
595 /* Get the peer for socketpair() sockets */
596 b
->ucred_valid
= getpeercred(b
->input_fd
, &b
->ucred
) >= 0;
598 /* Get the SELinux context of the peer */
599 r
= getpeersec(b
->input_fd
, &b
->label
);
600 if (r
< 0 && !IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
601 log_debug_errno(r
, "Failed to determine peer security context: %m");
603 /* Get the list of auxiliary groups of the peer */
604 r
= getpeergroups(b
->input_fd
, &b
->groups
);
606 b
->n_groups
= (size_t) r
;
607 else if (!IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
608 log_debug_errno(r
, "Failed to determine peer's group list: %m");
611 static int bus_socket_start_auth_client(sd_bus
*b
) {
613 const char *auth_suffix
, *auth_prefix
;
617 if (b
->anonymous_auth
) {
618 auth_prefix
= "\0AUTH ANONYMOUS ";
620 /* For ANONYMOUS auth we send some arbitrary "trace" string */
622 b
->auth_buffer
= hexmem("anonymous", l
);
624 char text
[DECIMAL_STR_MAX(uid_t
) + 1];
626 auth_prefix
= "\0AUTH EXTERNAL ";
628 xsprintf(text
, UID_FMT
, geteuid());
631 b
->auth_buffer
= hexmem(text
, l
);
638 auth_suffix
= "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
640 auth_suffix
= "\r\nBEGIN\r\n";
642 b
->auth_iovec
[0].iov_base
= (void*) auth_prefix
;
643 b
->auth_iovec
[0].iov_len
= 1 + strlen(auth_prefix
+ 1);
644 b
->auth_iovec
[1].iov_base
= (void*) b
->auth_buffer
;
645 b
->auth_iovec
[1].iov_len
= l
* 2;
646 b
->auth_iovec
[2].iov_base
= (void*) auth_suffix
;
647 b
->auth_iovec
[2].iov_len
= strlen(auth_suffix
);
649 return bus_socket_write_auth(b
);
652 int bus_socket_start_auth(sd_bus
*b
) {
657 bus_set_state(b
, BUS_AUTHENTICATING
);
658 b
->auth_timeout
= now(CLOCK_MONOTONIC
) + BUS_AUTH_TIMEOUT
;
660 if (sd_is_socket(b
->input_fd
, AF_UNIX
, 0, 0) <= 0)
661 b
->accept_fd
= false;
663 if (b
->output_fd
!= b
->input_fd
)
664 if (sd_is_socket(b
->output_fd
, AF_UNIX
, 0, 0) <= 0)
665 b
->accept_fd
= false;
668 return bus_socket_read_auth(b
);
670 return bus_socket_start_auth_client(b
);
673 static int bus_socket_inotify_setup(sd_bus
*b
) {
674 _cleanup_free_
int *new_watches
= NULL
;
675 _cleanup_free_
char *absolute
= NULL
;
676 size_t n_allocated
= 0, n
= 0, done
= 0, i
;
677 unsigned max_follow
= 32;
682 assert(b
->watch_bind
);
683 assert(b
->sockaddr
.sa
.sa_family
== AF_UNIX
);
684 assert(b
->sockaddr
.un
.sun_path
[0] != 0);
686 /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system socket
687 * appears before connecting to it. The implemented is pretty simplistic: we just subscribe to relevant changes
688 * to all prefix components of the path, and every time we get an event for that we try to reconnect again,
689 * without actually caring what precisely the event we got told us. If we still can't connect we re-subscribe
690 * to all relevant changes of anything in the path, so that our watches include any possibly newly created path
693 if (b
->inotify_fd
< 0) {
694 b
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
695 if (b
->inotify_fd
< 0)
698 b
->inotify_fd
= fd_move_above_stdio(b
->inotify_fd
);
701 /* Make sure the path is NUL terminated */
702 p
= strndupa(b
->sockaddr
.un
.sun_path
, sizeof(b
->sockaddr
.un
.sun_path
));
704 /* Make sure the path is absolute */
705 r
= path_make_absolute_cwd(p
, &absolute
);
709 /* Watch all parent directories, and don't mind any prefix that doesn't exist yet. For the innermost directory
710 * that exists we want to know when files are created or moved into it. For all parents of it we just care if
711 * they are removed or renamed. */
713 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
718 /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a symlink, and
720 wd
= inotify_add_watch(b
->inotify_fd
, "/", IN_CREATE
|IN_MOVED_TO
);
722 r
= log_debug_errno(errno
, "Failed to add inotify watch on /: %m");
725 new_watches
[n
++] = wd
;
728 _cleanup_free_
char *component
= NULL
, *prefix
= NULL
, *destination
= NULL
;
729 size_t n_slashes
, n_component
;
732 n_slashes
= strspn(absolute
+ done
, "/");
733 n_component
= n_slashes
+ strcspn(absolute
+ done
+ n_slashes
, "/");
735 if (n_component
== 0) /* The end */
738 component
= strndup(absolute
+ done
, n_component
);
744 /* A trailing slash? That's a directory, and not a socket then */
745 if (path_equal(component
, "/")) {
750 /* A single dot? Let's eat this up */
751 if (path_equal(component
, "/.")) {
756 prefix
= strndup(absolute
, done
+ n_component
);
762 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
767 wd
= inotify_add_watch(b
->inotify_fd
, prefix
, IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
|IN_DONT_FOLLOW
);
768 log_debug("Added inotify watch for %s on bus %s: %i", prefix
, strna(b
->description
), wd
);
771 if (IN_SET(errno
, ENOENT
, ELOOP
))
772 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
774 r
= log_debug_errno(errno
, "Failed to add inotify watch on %s: %m", empty_to_root(prefix
));
777 new_watches
[n
++] = wd
;
779 /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
780 r
= readlink_malloc(prefix
, &destination
);
781 if (r
== -EINVAL
) { /* not a symlink */
788 if (isempty(destination
)) { /* Empty symlink target? Yuck! */
793 if (max_follow
<= 0) { /* Let's make sure we don't follow symlinks forever */
798 if (path_is_absolute(destination
)) {
799 /* For absolute symlinks we build the new path and start anew */
800 c
= strjoin(destination
, absolute
+ done
+ n_component
);
803 _cleanup_free_
char *t
= NULL
;
805 /* For relative symlinks we replace the last component, and try again */
806 t
= strndup(absolute
, done
);
810 c
= strjoin(t
, "/", destination
, absolute
+ done
+ n_component
);
823 /* And now, let's remove all watches from the previous iteration we don't need anymore */
824 for (i
= 0; i
< b
->n_inotify_watches
; i
++) {
828 for (j
= 0; j
< n
; j
++)
829 if (new_watches
[j
] == b
->inotify_watches
[i
]) {
837 (void) inotify_rm_watch(b
->inotify_fd
, b
->inotify_watches
[i
]);
840 free_and_replace(b
->inotify_watches
, new_watches
);
841 b
->n_inotify_watches
= n
;
846 bus_close_inotify_fd(b
);
850 int bus_socket_connect(sd_bus
*b
) {
851 bool inotify_done
= false;
857 assert(b
->input_fd
< 0);
858 assert(b
->output_fd
< 0);
859 assert(b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
);
861 b
->input_fd
= socket(b
->sockaddr
.sa
.sa_family
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
865 b
->input_fd
= fd_move_above_stdio(b
->input_fd
);
867 b
->output_fd
= b
->input_fd
;
870 if (connect(b
->input_fd
, &b
->sockaddr
.sa
, b
->sockaddr_size
) < 0) {
871 if (errno
== EINPROGRESS
) {
873 /* If we have any inotify watches open, close them now, we don't need them anymore, as
874 * we have successfully initiated a connection */
875 bus_close_inotify_fd(b
);
877 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
878 * we start parsing the address string. The only reason we set the state explicitly
879 * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
880 bus_set_state(b
, BUS_OPENING
);
884 if (IN_SET(errno
, ENOENT
, ECONNREFUSED
) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
886 b
->sockaddr
.sa
.sa_family
== AF_UNIX
&&
887 b
->sockaddr
.un
.sun_path
[0] != 0) {
889 /* This connection attempt failed, let's release the socket for now, and start with a
890 * fresh one when reconnecting. */
894 /* inotify set up already, don't do it again, just return now, and remember
895 * that we are waiting for inotify events now. */
896 bus_set_state(b
, BUS_WATCH_BIND
);
900 /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
901 r
= bus_socket_inotify_setup(b
);
905 /* Let's now try to connect a second time, because in theory there's otherwise a race
906 * here: the socket might have been created in the time between our first connect() and
907 * the time we set up the inotify logic. But let's remember that we set up inotify now,
908 * so that we don't do the connect() more than twice. */
917 /* Yay, established, we don't need no inotify anymore! */
918 bus_close_inotify_fd(b
);
920 return bus_socket_start_auth(b
);
923 int bus_socket_exec(sd_bus
*b
) {
927 assert(b
->input_fd
< 0);
928 assert(b
->output_fd
< 0);
929 assert(b
->exec_path
);
930 assert(b
->busexec_pid
== 0);
932 r
= socketpair(AF_UNIX
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0, s
);
936 r
= safe_fork_full("(sd-busexec)", s
+1, 1, FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
, &b
->busexec_pid
);
944 if (rearrange_stdio(s
[1], s
[1], STDERR_FILENO
) < 0)
948 execvp(b
->exec_path
, b
->exec_argv
);
950 const char *argv
[] = { b
->exec_path
, NULL
};
951 execvp(b
->exec_path
, (char**) argv
);
958 b
->output_fd
= b
->input_fd
= fd_move_above_stdio(s
[0]);
962 return bus_socket_start_auth(b
);
965 int bus_socket_take_fd(sd_bus
*b
) {
970 return bus_socket_start_auth(b
);
973 int bus_socket_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
983 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
985 if (*idx
>= BUS_MESSAGE_SIZE(m
))
988 r
= bus_message_setup_iovec(m
);
992 n
= m
->n_iovec
* sizeof(struct iovec
);
994 memcpy_safe(iov
, m
->iovec
, n
);
997 iovec_advance(iov
, &j
, *idx
);
999 if (bus
->prefer_writev
)
1000 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1002 struct msghdr mh
= {
1004 .msg_iovlen
= m
->n_iovec
,
1007 if (m
->n_fds
> 0 && *idx
== 0) {
1008 struct cmsghdr
*control
;
1010 mh
.msg_control
= control
= alloca(CMSG_SPACE(sizeof(int) * m
->n_fds
));
1011 mh
.msg_controllen
= control
->cmsg_len
= CMSG_LEN(sizeof(int) * m
->n_fds
);
1012 control
->cmsg_level
= SOL_SOCKET
;
1013 control
->cmsg_type
= SCM_RIGHTS
;
1014 memcpy(CMSG_DATA(control
), m
->fds
, sizeof(int) * m
->n_fds
);
1017 k
= sendmsg(bus
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
1018 if (k
< 0 && errno
== ENOTSOCK
) {
1019 bus
->prefer_writev
= true;
1020 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1025 return errno
== EAGAIN
? 0 : -errno
;
1031 static int bus_socket_read_message_need(sd_bus
*bus
, size_t *need
) {
1038 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1040 if (bus
->rbuffer_size
< sizeof(struct bus_header
)) {
1041 *need
= sizeof(struct bus_header
) + 8;
1043 /* Minimum message size:
1047 * Method Call: +2 string headers
1048 * Signal: +3 string headers
1049 * Method Error: +1 string headers
1051 * Method Reply: +1 uint32 headers
1053 * A string header is at least 9 bytes
1054 * A uint32 header is at least 8 bytes
1056 * Hence the minimum message size of a valid message
1057 * is header + 8 bytes */
1062 a
= ((const uint32_t*) bus
->rbuffer
)[1];
1063 b
= ((const uint32_t*) bus
->rbuffer
)[3];
1065 e
= ((const uint8_t*) bus
->rbuffer
)[0];
1066 if (e
== BUS_LITTLE_ENDIAN
) {
1069 } else if (e
== BUS_BIG_ENDIAN
) {
1075 sum
= (uint64_t) sizeof(struct bus_header
) + (uint64_t) ALIGN_TO(b
, 8) + (uint64_t) a
;
1076 if (sum
>= BUS_MESSAGE_SIZE_MAX
)
1079 *need
= (size_t) sum
;
1083 static int bus_socket_make_message(sd_bus
*bus
, size_t size
) {
1089 assert(bus
->rbuffer_size
>= size
);
1090 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1092 r
= bus_rqueue_make_room(bus
);
1096 if (bus
->rbuffer_size
> size
) {
1097 b
= memdup((const uint8_t*) bus
->rbuffer
+ size
,
1098 bus
->rbuffer_size
- size
);
1104 r
= bus_message_from_malloc(bus
,
1106 bus
->fds
, bus
->n_fds
,
1115 bus
->rbuffer_size
-= size
;
1120 bus
->rqueue
[bus
->rqueue_size
++] = t
;
1125 int bus_socket_read_message(sd_bus
*bus
) {
1127 struct iovec iov
= {};
1133 struct cmsghdr cmsghdr
;
1134 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
1136 bool handle_cmsg
= false;
1139 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1141 r
= bus_socket_read_message_need(bus
, &need
);
1145 if (bus
->rbuffer_size
>= need
)
1146 return bus_socket_make_message(bus
, need
);
1148 b
= realloc(bus
->rbuffer
, need
);
1154 iov
.iov_base
= (uint8_t*) bus
->rbuffer
+ bus
->rbuffer_size
;
1155 iov
.iov_len
= need
- bus
->rbuffer_size
;
1157 if (bus
->prefer_readv
)
1158 k
= readv(bus
->input_fd
, &iov
, 1);
1163 mh
.msg_control
= &control
;
1164 mh
.msg_controllen
= sizeof(control
);
1166 k
= recvmsg(bus
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
|MSG_CMSG_CLOEXEC
);
1167 if (k
< 0 && errno
== ENOTSOCK
) {
1168 bus
->prefer_readv
= true;
1169 k
= readv(bus
->input_fd
, &iov
, 1);
1174 return errno
== EAGAIN
? 0 : -errno
;
1178 bus
->rbuffer_size
+= k
;
1181 struct cmsghdr
*cmsg
;
1183 CMSG_FOREACH(cmsg
, &mh
)
1184 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1185 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1188 n
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
1190 if (!bus
->can_fds
) {
1191 /* Whut? We received fds but this
1192 * isn't actually enabled? Close them,
1195 close_many((int*) CMSG_DATA(cmsg
), n
);
1199 f
= reallocarray(bus
->fds
, bus
->n_fds
+ n
, sizeof(int));
1201 close_many((int*) CMSG_DATA(cmsg
), n
);
1205 for (i
= 0; i
< n
; i
++)
1206 f
[bus
->n_fds
++] = fd_move_above_stdio(((int*) CMSG_DATA(cmsg
))[i
]);
1209 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1210 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1213 r
= bus_socket_read_message_need(bus
, &need
);
1217 if (bus
->rbuffer_size
>= need
)
1218 return bus_socket_make_message(bus
, need
);
1223 int bus_socket_process_opening(sd_bus
*b
) {
1225 socklen_t slen
= sizeof(error
);
1232 assert(b
->state
== BUS_OPENING
);
1238 if (!(p
.revents
& (POLLOUT
|POLLERR
|POLLHUP
)))
1241 r
= getsockopt(b
->output_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &slen
);
1243 b
->last_connect_error
= errno
;
1244 else if (error
!= 0)
1245 b
->last_connect_error
= error
;
1246 else if (p
.revents
& (POLLERR
|POLLHUP
))
1247 b
->last_connect_error
= ECONNREFUSED
;
1249 return bus_socket_start_auth(b
);
1251 return bus_next_address(b
);
1254 int bus_socket_process_authenticating(sd_bus
*b
) {
1258 assert(b
->state
== BUS_AUTHENTICATING
);
1260 if (now(CLOCK_MONOTONIC
) >= b
->auth_timeout
)
1263 r
= bus_socket_write_auth(b
);
1267 return bus_socket_read_auth(b
);
1270 int bus_socket_process_watch_bind(sd_bus
*b
) {
1274 assert(b
->state
== BUS_WATCH_BIND
);
1275 assert(b
->inotify_fd
>= 0);
1277 r
= flush_fd(b
->inotify_fd
);
1281 log_debug("Got inotify event on bus %s.", strna(b
->description
));
1283 /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
1286 r
= bus_socket_connect(b
);
1290 q
= bus_attach_io_events(b
);
1294 q
= bus_attach_inotify_event(b
);