]>
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
;
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
);
249 return p
&& (p
== (s
+ m
) || *p
== ' ');
252 static int verify_anonymous_token(sd_bus
*b
, const char *p
, size_t l
) {
253 _cleanup_free_
char *token
= NULL
;
257 if (!b
->anonymous_auth
)
269 r
= unhexmem(p
, l
, (void **) &token
, &len
);
273 if (memchr(token
, 0, len
))
276 return !!utf8_is_valid(token
);
279 static int verify_external_token(sd_bus
*b
, const char *p
, size_t l
) {
280 _cleanup_free_
char *token
= NULL
;
285 /* We don't do any real authentication here. Instead, we if
286 * the owner of this bus wanted authentication he should have
287 * checked SO_PEERCRED before even creating the bus object. */
289 if (!b
->anonymous_auth
&& !b
->ucred_valid
)
301 r
= unhexmem(p
, l
, (void**) &token
, &len
);
305 if (memchr(token
, 0, len
))
308 r
= parse_uid(token
, &u
);
312 /* We ignore the passed value if anonymous authentication is
314 if (!b
->anonymous_auth
&& u
!= b
->ucred
.uid
)
320 static int bus_socket_auth_write(sd_bus
*b
, const char *t
) {
327 /* We only make use of the first iovec */
328 assert(IN_SET(b
->auth_index
, 0, 1));
331 p
= malloc(b
->auth_iovec
[0].iov_len
+ l
);
335 memcpy_safe(p
, b
->auth_iovec
[0].iov_base
, b
->auth_iovec
[0].iov_len
);
336 memcpy(p
+ b
->auth_iovec
[0].iov_len
, t
, l
);
338 b
->auth_iovec
[0].iov_base
= p
;
339 b
->auth_iovec
[0].iov_len
+= l
;
341 free(b
->auth_buffer
);
347 static int bus_socket_auth_write_ok(sd_bus
*b
) {
348 char t
[3 + 32 + 2 + 1];
352 xsprintf(t
, "OK " SD_ID128_FORMAT_STR
"\r\n", SD_ID128_FORMAT_VAL(b
->server_id
));
354 return bus_socket_auth_write(b
, t
);
357 static int bus_socket_auth_verify_server(sd_bus
*b
) {
361 bool processed
= false;
366 if (b
->rbuffer_size
< 1)
369 /* First char must be a NUL byte */
370 if (*(char*) b
->rbuffer
!= 0)
373 if (b
->rbuffer_size
< 3)
376 /* Begin with the first line */
377 if (b
->auth_rbegin
<= 0)
381 /* Check if line is complete */
382 line
= (char*) b
->rbuffer
+ b
->auth_rbegin
;
383 e
= memmem(line
, b
->rbuffer_size
- b
->auth_rbegin
, "\r\n", 2);
389 if (line_begins(line
, l
, "AUTH ANONYMOUS")) {
391 r
= verify_anonymous_token(b
, line
+ 14, l
- 14);
395 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
397 b
->auth
= BUS_AUTH_ANONYMOUS
;
398 r
= bus_socket_auth_write_ok(b
);
401 } else if (line_begins(line
, l
, "AUTH EXTERNAL")) {
403 r
= verify_external_token(b
, line
+ 13, l
- 13);
407 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
409 b
->auth
= BUS_AUTH_EXTERNAL
;
410 r
= bus_socket_auth_write_ok(b
);
413 } else if (line_begins(line
, l
, "AUTH"))
414 r
= bus_socket_auth_write(b
, "REJECTED EXTERNAL ANONYMOUS\r\n");
415 else if (line_equals(line
, l
, "CANCEL") ||
416 line_begins(line
, l
, "ERROR")) {
418 b
->auth
= _BUS_AUTH_INVALID
;
419 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
421 } else if (line_equals(line
, l
, "BEGIN")) {
423 if (b
->auth
== _BUS_AUTH_INVALID
)
424 r
= bus_socket_auth_write(b
, "ERROR\r\n");
426 /* We can't leave from the auth phase
427 * before we haven't written
428 * everything queued, so let's check
431 if (bus_socket_auth_needs_write(b
))
434 b
->rbuffer_size
-= (e
+ 2 - (char*) b
->rbuffer
);
435 memmove(b
->rbuffer
, e
+ 2, b
->rbuffer_size
);
436 return bus_start_running(b
);
439 } else if (line_begins(line
, l
, "DATA")) {
441 if (b
->auth
== _BUS_AUTH_INVALID
)
442 r
= bus_socket_auth_write(b
, "ERROR\r\n");
444 if (b
->auth
== BUS_AUTH_ANONYMOUS
)
445 r
= verify_anonymous_token(b
, line
+ 4, l
- 4);
447 r
= verify_external_token(b
, line
+ 4, l
- 4);
452 b
->auth
= _BUS_AUTH_INVALID
;
453 r
= bus_socket_auth_write(b
, "REJECTED\r\n");
455 r
= bus_socket_auth_write_ok(b
);
457 } else if (line_equals(line
, l
, "NEGOTIATE_UNIX_FD")) {
458 if (b
->auth
== _BUS_AUTH_INVALID
|| !b
->accept_fd
)
459 r
= bus_socket_auth_write(b
, "ERROR\r\n");
462 r
= bus_socket_auth_write(b
, "AGREE_UNIX_FD\r\n");
465 r
= bus_socket_auth_write(b
, "ERROR\r\n");
470 b
->auth_rbegin
= e
+ 2 - (char*) b
->rbuffer
;
476 static int bus_socket_auth_verify(sd_bus
*b
) {
480 return bus_socket_auth_verify_server(b
);
482 return bus_socket_auth_verify_client(b
);
485 static int bus_socket_read_auth(sd_bus
*b
) {
487 struct iovec iov
= {};
493 struct cmsghdr cmsghdr
;
494 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
496 bool handle_cmsg
= false;
499 assert(b
->state
== BUS_AUTHENTICATING
);
501 r
= bus_socket_auth_verify(b
);
505 n
= MAX(256u, b
->rbuffer_size
* 2);
507 if (n
> BUS_AUTH_SIZE_MAX
)
508 n
= BUS_AUTH_SIZE_MAX
;
510 if (b
->rbuffer_size
>= n
)
513 p
= realloc(b
->rbuffer
, n
);
519 iov
.iov_base
= (uint8_t*) b
->rbuffer
+ b
->rbuffer_size
;
520 iov
.iov_len
= n
- b
->rbuffer_size
;
523 k
= readv(b
->input_fd
, &iov
, 1);
528 mh
.msg_control
= &control
;
529 mh
.msg_controllen
= sizeof(control
);
531 k
= recvmsg(b
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
532 if (k
< 0 && errno
== ENOTSOCK
) {
533 b
->prefer_readv
= true;
534 k
= readv(b
->input_fd
, &iov
, 1);
539 return errno
== EAGAIN
? 0 : -errno
;
543 b
->rbuffer_size
+= k
;
546 struct cmsghdr
*cmsg
;
548 CMSG_FOREACH(cmsg
, &mh
)
549 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
550 cmsg
->cmsg_type
== SCM_RIGHTS
) {
553 /* Whut? We received fds during the auth
554 * protocol? Somebody is playing games with
555 * us. Close them all, and fail */
556 j
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
557 close_many((int*) CMSG_DATA(cmsg
), j
);
560 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
561 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
564 r
= bus_socket_auth_verify(b
);
571 void bus_socket_setup(sd_bus
*b
) {
574 /* Increase the buffers to 8 MB */
575 (void) fd_inc_rcvbuf(b
->input_fd
, SNDBUF_SIZE
);
576 (void) fd_inc_sndbuf(b
->output_fd
, SNDBUF_SIZE
);
578 b
->message_version
= 1;
579 b
->message_endian
= 0;
582 static void bus_get_peercred(sd_bus
*b
) {
586 assert(!b
->ucred_valid
);
588 assert(b
->n_groups
== (size_t) -1);
590 /* Get the peer for socketpair() sockets */
591 b
->ucred_valid
= getpeercred(b
->input_fd
, &b
->ucred
) >= 0;
593 /* Get the SELinux context of the peer */
594 r
= getpeersec(b
->input_fd
, &b
->label
);
595 if (r
< 0 && !IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
596 log_debug_errno(r
, "Failed to determine peer security context: %m");
598 /* Get the list of auxiliary groups of the peer */
599 r
= getpeergroups(b
->input_fd
, &b
->groups
);
601 b
->n_groups
= (size_t) r
;
602 else if (!IN_SET(r
, -EOPNOTSUPP
, -ENOPROTOOPT
))
603 log_debug_errno(r
, "Failed to determine peer's group list: %m");
606 static int bus_socket_start_auth_client(sd_bus
*b
) {
608 const char *auth_suffix
, *auth_prefix
;
612 if (b
->anonymous_auth
) {
613 auth_prefix
= "\0AUTH ANONYMOUS ";
615 /* For ANONYMOUS auth we send some arbitrary "trace" string */
617 b
->auth_buffer
= hexmem("anonymous", l
);
619 char text
[DECIMAL_STR_MAX(uid_t
) + 1];
621 auth_prefix
= "\0AUTH EXTERNAL ";
623 xsprintf(text
, UID_FMT
, geteuid());
626 b
->auth_buffer
= hexmem(text
, l
);
633 auth_suffix
= "\r\nNEGOTIATE_UNIX_FD\r\nBEGIN\r\n";
635 auth_suffix
= "\r\nBEGIN\r\n";
637 b
->auth_iovec
[0].iov_base
= (void*) auth_prefix
;
638 b
->auth_iovec
[0].iov_len
= 1 + strlen(auth_prefix
+ 1);
639 b
->auth_iovec
[1].iov_base
= (void*) b
->auth_buffer
;
640 b
->auth_iovec
[1].iov_len
= l
* 2;
641 b
->auth_iovec
[2].iov_base
= (void*) auth_suffix
;
642 b
->auth_iovec
[2].iov_len
= strlen(auth_suffix
);
644 return bus_socket_write_auth(b
);
647 int bus_socket_start_auth(sd_bus
*b
) {
652 bus_set_state(b
, BUS_AUTHENTICATING
);
653 b
->auth_timeout
= now(CLOCK_MONOTONIC
) + BUS_AUTH_TIMEOUT
;
655 if (sd_is_socket(b
->input_fd
, AF_UNIX
, 0, 0) <= 0)
656 b
->accept_fd
= false;
658 if (b
->output_fd
!= b
->input_fd
)
659 if (sd_is_socket(b
->output_fd
, AF_UNIX
, 0, 0) <= 0)
660 b
->accept_fd
= false;
663 return bus_socket_read_auth(b
);
665 return bus_socket_start_auth_client(b
);
668 static int bus_socket_inotify_setup(sd_bus
*b
) {
669 _cleanup_free_
int *new_watches
= NULL
;
670 _cleanup_free_
char *absolute
= NULL
;
671 size_t n_allocated
= 0, n
= 0, done
= 0, i
;
672 unsigned max_follow
= 32;
677 assert(b
->watch_bind
);
678 assert(b
->sockaddr
.sa
.sa_family
== AF_UNIX
);
679 assert(b
->sockaddr
.un
.sun_path
[0] != 0);
681 /* Sets up an inotify fd in case watch_bind is enabled: wait until the configured AF_UNIX file system socket
682 * appears before connecting to it. The implemented is pretty simplistic: we just subscribe to relevant changes
683 * to all prefix components of the path, and every time we get an event for that we try to reconnect again,
684 * without actually caring what precisely the event we got told us. If we still can't connect we re-subscribe
685 * to all relevant changes of anything in the path, so that our watches include any possibly newly created path
688 if (b
->inotify_fd
< 0) {
689 b
->inotify_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
690 if (b
->inotify_fd
< 0)
693 b
->inotify_fd
= fd_move_above_stdio(b
->inotify_fd
);
696 /* Make sure the path is NUL terminated */
697 p
= strndupa(b
->sockaddr
.un
.sun_path
, sizeof(b
->sockaddr
.un
.sun_path
));
699 /* Make sure the path is absolute */
700 r
= path_make_absolute_cwd(p
, &absolute
);
704 /* Watch all parent directories, and don't mind any prefix that doesn't exist yet. For the innermost directory
705 * that exists we want to know when files are created or moved into it. For all parents of it we just care if
706 * they are removed or renamed. */
708 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
713 /* Start with the top-level directory, which is a bit simpler than the rest, since it can't be a symlink, and
715 wd
= inotify_add_watch(b
->inotify_fd
, "/", IN_CREATE
|IN_MOVED_TO
);
717 r
= log_debug_errno(errno
, "Failed to add inotify watch on /: %m");
720 new_watches
[n
++] = wd
;
723 _cleanup_free_
char *component
= NULL
, *prefix
= NULL
, *destination
= NULL
;
724 size_t n_slashes
, n_component
;
727 n_slashes
= strspn(absolute
+ done
, "/");
728 n_component
= n_slashes
+ strcspn(absolute
+ done
+ n_slashes
, "/");
730 if (n_component
== 0) /* The end */
733 component
= strndup(absolute
+ done
, n_component
);
739 /* A trailing slash? That's a directory, and not a socket then */
740 if (path_equal(component
, "/")) {
745 /* A single dot? Let's eat this up */
746 if (path_equal(component
, "/.")) {
751 prefix
= strndup(absolute
, done
+ n_component
);
757 if (!GREEDY_REALLOC(new_watches
, n_allocated
, n
+ 1)) {
762 wd
= inotify_add_watch(b
->inotify_fd
, prefix
, IN_DELETE_SELF
|IN_MOVE_SELF
|IN_ATTRIB
|IN_CREATE
|IN_MOVED_TO
|IN_DONT_FOLLOW
);
763 log_debug("Added inotify watch for %s on bus %s: %i", prefix
, strna(b
->description
), wd
);
766 if (IN_SET(errno
, ENOENT
, ELOOP
))
767 break; /* This component doesn't exist yet, or the path contains a cyclic symlink right now */
769 r
= log_debug_errno(errno
, "Failed to add inotify watch on %s: %m", empty_to_root(prefix
));
772 new_watches
[n
++] = wd
;
774 /* Check if this is possibly a symlink. If so, let's follow it and watch it too. */
775 r
= readlink_malloc(prefix
, &destination
);
776 if (r
== -EINVAL
) { /* not a symlink */
783 if (isempty(destination
)) { /* Empty symlink target? Yuck! */
788 if (max_follow
<= 0) { /* Let's make sure we don't follow symlinks forever */
793 if (path_is_absolute(destination
)) {
794 /* For absolute symlinks we build the new path and start anew */
795 c
= strjoin(destination
, absolute
+ done
+ n_component
);
798 _cleanup_free_
char *t
= NULL
;
800 /* For relative symlinks we replace the last component, and try again */
801 t
= strndup(absolute
, done
);
805 c
= strjoin(t
, "/", destination
, absolute
+ done
+ n_component
);
818 /* And now, let's remove all watches from the previous iteration we don't need anymore */
819 for (i
= 0; i
< b
->n_inotify_watches
; i
++) {
823 for (j
= 0; j
< n
; j
++)
824 if (new_watches
[j
] == b
->inotify_watches
[i
]) {
832 (void) inotify_rm_watch(b
->inotify_fd
, b
->inotify_watches
[i
]);
835 free_and_replace(b
->inotify_watches
, new_watches
);
836 b
->n_inotify_watches
= n
;
841 bus_close_inotify_fd(b
);
845 int bus_socket_connect(sd_bus
*b
) {
846 bool inotify_done
= false;
852 assert(b
->input_fd
< 0);
853 assert(b
->output_fd
< 0);
854 assert(b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
);
856 b
->input_fd
= socket(b
->sockaddr
.sa
.sa_family
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
860 b
->input_fd
= fd_move_above_stdio(b
->input_fd
);
862 b
->output_fd
= b
->input_fd
;
865 if (connect(b
->input_fd
, &b
->sockaddr
.sa
, b
->sockaddr_size
) < 0) {
866 if (errno
== EINPROGRESS
) {
868 /* If we have any inotify watches open, close them now, we don't need them anymore, as
869 * we have successfully initiated a connection */
870 bus_close_inotify_fd(b
);
872 /* Note that very likely we are already in BUS_OPENING state here, as we enter it when
873 * we start parsing the address string. The only reason we set the state explicitly
874 * here, is to undo BUS_WATCH_BIND, in case we did the inotify magic. */
875 bus_set_state(b
, BUS_OPENING
);
879 if (IN_SET(errno
, ENOENT
, ECONNREFUSED
) && /* ENOENT → unix socket doesn't exist at all; ECONNREFUSED → unix socket stale */
881 b
->sockaddr
.sa
.sa_family
== AF_UNIX
&&
882 b
->sockaddr
.un
.sun_path
[0] != 0) {
884 /* This connection attempt failed, let's release the socket for now, and start with a
885 * fresh one when reconnecting. */
889 /* inotify set up already, don't do it again, just return now, and remember
890 * that we are waiting for inotify events now. */
891 bus_set_state(b
, BUS_WATCH_BIND
);
895 /* This is a file system socket, and the inotify logic is enabled. Let's create the necessary inotify fd. */
896 r
= bus_socket_inotify_setup(b
);
900 /* Let's now try to connect a second time, because in theory there's otherwise a race
901 * here: the socket might have been created in the time between our first connect() and
902 * the time we set up the inotify logic. But let's remember that we set up inotify now,
903 * so that we don't do the connect() more than twice. */
912 /* Yay, established, we don't need no inotify anymore! */
913 bus_close_inotify_fd(b
);
915 return bus_socket_start_auth(b
);
918 int bus_socket_exec(sd_bus
*b
) {
922 assert(b
->input_fd
< 0);
923 assert(b
->output_fd
< 0);
924 assert(b
->exec_path
);
925 assert(b
->busexec_pid
== 0);
927 r
= socketpair(AF_UNIX
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0, s
);
931 r
= safe_fork_full("(sd-busexec)", s
+1, 1, FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
, &b
->busexec_pid
);
939 if (rearrange_stdio(s
[1], s
[1], STDERR_FILENO
) < 0)
943 execvp(b
->exec_path
, b
->exec_argv
);
945 const char *argv
[] = { b
->exec_path
, NULL
};
946 execvp(b
->exec_path
, (char**) argv
);
953 b
->output_fd
= b
->input_fd
= fd_move_above_stdio(s
[0]);
957 return bus_socket_start_auth(b
);
960 int bus_socket_take_fd(sd_bus
*b
) {
965 return bus_socket_start_auth(b
);
968 int bus_socket_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
978 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
980 if (*idx
>= BUS_MESSAGE_SIZE(m
))
983 r
= bus_message_setup_iovec(m
);
987 n
= m
->n_iovec
* sizeof(struct iovec
);
989 memcpy_safe(iov
, m
->iovec
, n
);
992 iovec_advance(iov
, &j
, *idx
);
994 if (bus
->prefer_writev
)
995 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
999 .msg_iovlen
= m
->n_iovec
,
1002 if (m
->n_fds
> 0 && *idx
== 0) {
1003 struct cmsghdr
*control
;
1005 mh
.msg_control
= control
= alloca(CMSG_SPACE(sizeof(int) * m
->n_fds
));
1006 mh
.msg_controllen
= control
->cmsg_len
= CMSG_LEN(sizeof(int) * m
->n_fds
);
1007 control
->cmsg_level
= SOL_SOCKET
;
1008 control
->cmsg_type
= SCM_RIGHTS
;
1009 memcpy(CMSG_DATA(control
), m
->fds
, sizeof(int) * m
->n_fds
);
1012 k
= sendmsg(bus
->output_fd
, &mh
, MSG_DONTWAIT
|MSG_NOSIGNAL
);
1013 if (k
< 0 && errno
== ENOTSOCK
) {
1014 bus
->prefer_writev
= true;
1015 k
= writev(bus
->output_fd
, iov
, m
->n_iovec
);
1020 return errno
== EAGAIN
? 0 : -errno
;
1026 static int bus_socket_read_message_need(sd_bus
*bus
, size_t *need
) {
1033 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1035 if (bus
->rbuffer_size
< sizeof(struct bus_header
)) {
1036 *need
= sizeof(struct bus_header
) + 8;
1038 /* Minimum message size:
1042 * Method Call: +2 string headers
1043 * Signal: +3 string headers
1044 * Method Error: +1 string headers
1046 * Method Reply: +1 uint32 headers
1048 * A string header is at least 9 bytes
1049 * A uint32 header is at least 8 bytes
1051 * Hence the minimum message size of a valid message
1052 * is header + 8 bytes */
1057 a
= ((const uint32_t*) bus
->rbuffer
)[1];
1058 b
= ((const uint32_t*) bus
->rbuffer
)[3];
1060 e
= ((const uint8_t*) bus
->rbuffer
)[0];
1061 if (e
== BUS_LITTLE_ENDIAN
) {
1064 } else if (e
== BUS_BIG_ENDIAN
) {
1070 sum
= (uint64_t) sizeof(struct bus_header
) + (uint64_t) ALIGN_TO(b
, 8) + (uint64_t) a
;
1071 if (sum
>= BUS_MESSAGE_SIZE_MAX
)
1074 *need
= (size_t) sum
;
1078 static int bus_socket_make_message(sd_bus
*bus
, size_t size
) {
1084 assert(bus
->rbuffer_size
>= size
);
1085 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1087 r
= bus_rqueue_make_room(bus
);
1091 if (bus
->rbuffer_size
> size
) {
1092 b
= memdup((const uint8_t*) bus
->rbuffer
+ size
,
1093 bus
->rbuffer_size
- size
);
1099 r
= bus_message_from_malloc(bus
,
1101 bus
->fds
, bus
->n_fds
,
1110 bus
->rbuffer_size
-= size
;
1115 bus
->rqueue
[bus
->rqueue_size
++] = t
;
1120 int bus_socket_read_message(sd_bus
*bus
) {
1122 struct iovec iov
= {};
1128 struct cmsghdr cmsghdr
;
1129 uint8_t buf
[CMSG_SPACE(sizeof(int) * BUS_FDS_MAX
)];
1131 bool handle_cmsg
= false;
1134 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1136 r
= bus_socket_read_message_need(bus
, &need
);
1140 if (bus
->rbuffer_size
>= need
)
1141 return bus_socket_make_message(bus
, need
);
1143 b
= realloc(bus
->rbuffer
, need
);
1149 iov
.iov_base
= (uint8_t*) bus
->rbuffer
+ bus
->rbuffer_size
;
1150 iov
.iov_len
= need
- bus
->rbuffer_size
;
1152 if (bus
->prefer_readv
)
1153 k
= readv(bus
->input_fd
, &iov
, 1);
1158 mh
.msg_control
= &control
;
1159 mh
.msg_controllen
= sizeof(control
);
1161 k
= recvmsg(bus
->input_fd
, &mh
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
1162 if (k
< 0 && errno
== ENOTSOCK
) {
1163 bus
->prefer_readv
= true;
1164 k
= readv(bus
->input_fd
, &iov
, 1);
1169 return errno
== EAGAIN
? 0 : -errno
;
1173 bus
->rbuffer_size
+= k
;
1176 struct cmsghdr
*cmsg
;
1178 CMSG_FOREACH(cmsg
, &mh
)
1179 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
1180 cmsg
->cmsg_type
== SCM_RIGHTS
) {
1183 n
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
1185 if (!bus
->can_fds
) {
1186 /* Whut? We received fds but this
1187 * isn't actually enabled? Close them,
1190 close_many((int*) CMSG_DATA(cmsg
), n
);
1194 f
= reallocarray(bus
->fds
, bus
->n_fds
+ n
, sizeof(int));
1196 close_many((int*) CMSG_DATA(cmsg
), n
);
1200 for (i
= 0; i
< n
; i
++)
1201 f
[bus
->n_fds
++] = fd_move_above_stdio(((int*) CMSG_DATA(cmsg
))[i
]);
1204 log_debug("Got unexpected auxiliary data with level=%d and type=%d",
1205 cmsg
->cmsg_level
, cmsg
->cmsg_type
);
1208 r
= bus_socket_read_message_need(bus
, &need
);
1212 if (bus
->rbuffer_size
>= need
)
1213 return bus_socket_make_message(bus
, need
);
1218 int bus_socket_process_opening(sd_bus
*b
) {
1220 socklen_t slen
= sizeof(error
);
1227 assert(b
->state
== BUS_OPENING
);
1233 if (!(p
.revents
& (POLLOUT
|POLLERR
|POLLHUP
)))
1236 r
= getsockopt(b
->output_fd
, SOL_SOCKET
, SO_ERROR
, &error
, &slen
);
1238 b
->last_connect_error
= errno
;
1239 else if (error
!= 0)
1240 b
->last_connect_error
= error
;
1241 else if (p
.revents
& (POLLERR
|POLLHUP
))
1242 b
->last_connect_error
= ECONNREFUSED
;
1244 return bus_socket_start_auth(b
);
1246 return bus_next_address(b
);
1249 int bus_socket_process_authenticating(sd_bus
*b
) {
1253 assert(b
->state
== BUS_AUTHENTICATING
);
1255 if (now(CLOCK_MONOTONIC
) >= b
->auth_timeout
)
1258 r
= bus_socket_write_auth(b
);
1262 return bus_socket_read_auth(b
);
1265 int bus_socket_process_watch_bind(sd_bus
*b
) {
1269 assert(b
->state
== BUS_WATCH_BIND
);
1270 assert(b
->inotify_fd
>= 0);
1272 r
= flush_fd(b
->inotify_fd
);
1276 log_debug("Got inotify event on bus %s.", strna(b
->description
));
1278 /* We flushed events out of the inotify fd. In that case, maybe the socket is valid now? Let's try to connect
1281 r
= bus_socket_connect(b
);
1285 q
= bus_attach_io_events(b
);
1289 q
= bus_attach_inotify_event(b
);