1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
24 #include <sys/types.h>
35 #include "socket-util.h"
36 #include "sd-daemon.h"
38 #include "bus-internal.h"
39 #include "bus-message.h"
44 #define UNIX_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
45 #define KERNEL_BUS_PATH "kernel:path=/dev/kdbus/0-system/bus"
48 # define DEFAULT_BUS_PATH KERNEL_BUS_PATH ";" UNIX_BUS_PATH
50 # define DEFAULT_BUS_PATH UNIX_BUS_PATH
53 static const char *arg_address = DEFAULT_BUS_PATH;
54 static char *arg_command_line_buffer = NULL;
56 static int help(void) {
58 printf("%s [OPTIONS...]\n\n"
59 "Connect STDIO or a socket to a given bus address.\n\n"
60 " -h --help Show this help\n"
61 " --version Show package version\n"
62 " --address=ADDRESS Connect to the bus specified by ADDRESS\n"
63 " (default: " DEFAULT_BUS_PATH ")\n",
64 program_invocation_short_name);
69 static int parse_argv(int argc, char *argv[]) {
76 static const struct option options[] = {
77 { "help", no_argument, NULL, 'h' },
78 { "version", no_argument, NULL, ARG_VERSION },
79 { "address", required_argument, NULL, ARG_ADDRESS },
88 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
98 puts(SYSTEMD_FEATURES);
102 arg_address = optarg;
109 assert_not_reached("Unhandled option");
113 /* If the first command line argument is only "x" characters
114 * we'll write who we are talking to into it, so that "ps" is
116 arg_command_line_buffer = argv[optind];
117 if (argc > optind + 1 ||
118 (arg_command_line_buffer && arg_command_line_buffer[strspn(arg_command_line_buffer, "x")] != 0)) {
119 log_error("Too many arguments");
126 static int rename_service(sd_bus *a, sd_bus *b) {
127 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
128 _cleanup_free_ char *p = NULL, *name = NULL;
138 r = sd_bus_get_peer_creds(b, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_CMDLINE|SD_BUS_CREDS_COMM, &creds);
142 r = sd_bus_creds_get_uid(creds, &uid);
146 r = sd_bus_creds_get_pid(creds, &pid);
150 r = sd_bus_creds_get_cmdline(creds, &cmdline);
154 r = sd_bus_creds_get_comm(creds, &comm);
158 name = uid_to_name(uid);
162 p = strv_join(cmdline, " ");
166 /* The status string gets the full command line ... */
168 "STATUS=Processing requests from client PID %lu (%s); UID %lu (%s)",
169 (unsigned long) pid, p,
170 (unsigned long) uid, name);
172 /* ... and the argv line only the short comm */
173 if (arg_command_line_buffer) {
176 m = strlen(arg_command_line_buffer);
177 w = snprintf(arg_command_line_buffer, m,
178 "[PID %lu/%s; UID %lu/%s]",
179 (unsigned long) pid, comm,
180 (unsigned long) uid, name);
183 memset(arg_command_line_buffer + w, 0, m - w);
186 log_debug("Running on behalf of PID %lu (%s), UID %lu (%s), %s",
187 (unsigned long) pid, p,
188 (unsigned long) uid, name,
194 static int synthesize_name_acquired(sd_bus *a, sd_bus *b, sd_bus_message *m) {
195 _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
196 const char *name, *old_owner, *new_owner;
203 /* If we get NameOwnerChanged for our own name, we need to
204 * synthesize NameLost/NameAcquired, since socket clients need
205 * that, even though it is obsoleted on kdbus */
210 if (!sd_bus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged") ||
211 !streq_ptr(m->path, "/org/freedesktop/DBus") ||
212 !streq_ptr(m->sender, "org.freedesktop.DBus"))
215 r = sd_bus_message_read(m, "sss", &name, &old_owner, &new_owner);
219 r = sd_bus_message_rewind(m, true);
223 if (streq(old_owner, a->unique_name)) {
225 r = sd_bus_message_new_signal(
227 "/org/freedesktop/DBus",
228 "org.freedesktop.DBus",
232 } else if (streq(new_owner, a->unique_name)) {
234 r = sd_bus_message_new_signal(
236 "/org/freedesktop/DBus",
237 "org.freedesktop.DBus",
246 r = sd_bus_message_append(n, "s", name);
250 r = bus_message_append_sender(n, "org.freedesktop.DBus");
254 r = bus_seal_synthetic_message(b, n);
258 return sd_bus_send(b, n, NULL);
261 static int process_hello(sd_bus *a, sd_bus *b, sd_bus_message *m, bool *got_hello) {
262 _cleanup_bus_message_unref_ sd_bus_message *n = NULL;
271 /* As reaction to hello we need to respond with two messages:
272 * the callback reply and the NameAcquired for the unique
273 * name, since hello is otherwise obsolete on kdbus. */
276 sd_bus_message_is_method_call(m, "org.freedesktop.DBus", "Hello") &&
277 streq_ptr(m->destination, "org.freedesktop.DBus");
284 log_error("First packet isn't hello (it's %s.%s), aborting.", m->interface, m->member);
289 log_error("Got duplicate hello, aborting.");
298 r = sd_bus_message_new_method_return(m, &n);
300 log_error("Failed to generate HELLO reply: %s", strerror(-r));
304 r = sd_bus_message_append(n, "s", a->unique_name);
306 log_error("Failed to append unique name to HELLO reply: %s", strerror(-r));
310 r = bus_message_append_sender(n, "org.freedesktop.DBus");
312 log_error("Failed to append sender to HELLO reply: %s", strerror(-r));
316 r = bus_seal_synthetic_message(b, n);
318 log_error("Failed to seal HELLO reply: %s", strerror(-r));
322 r = sd_bus_send(b, n, NULL);
324 log_error("Failed to send HELLO reply: %s", strerror(-r));
328 n = sd_bus_message_unref(n);
329 r = sd_bus_message_new_signal(
331 "/org/freedesktop/DBus",
332 "org.freedesktop.DBus",
336 log_error("Failed to allocate initial NameAcquired message: %s", strerror(-r));
340 r = sd_bus_message_append(n, "s", a->unique_name);
342 log_error("Failed to append unique name to NameAcquired message: %s", strerror(-r));
346 r = bus_message_append_sender(n, "org.freedesktop.DBus");
348 log_error("Failed to append sender to NameAcquired message: %s", strerror(-r));
352 r = bus_seal_synthetic_message(b, n);
354 log_error("Failed to seal NameAcquired message: %s", strerror(-r));
358 r = sd_bus_send(b, n, NULL);
360 log_error("Failed to send NameAcquired message: %s", strerror(-r));
367 int main(int argc, char *argv[]) {
369 _cleanup_bus_unref_ sd_bus *a = NULL, *b = NULL;
370 sd_id128_t server_id;
371 int r, in_fd, out_fd;
372 bool got_hello = false;
374 struct ucred ucred = {};
375 _cleanup_free_ char *peersec = NULL;
377 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
378 log_parse_environment();
381 r = parse_argv(argc, argv);
385 r = sd_listen_fds(0);
387 in_fd = STDIN_FILENO;
388 out_fd = STDOUT_FILENO;
390 in_fd = SD_LISTEN_FDS_START;
391 out_fd = SD_LISTEN_FDS_START;
393 log_error("Illegal number of file descriptors passed");
398 sd_is_socket(in_fd, AF_UNIX, 0, 0) > 0 &&
399 sd_is_socket(out_fd, AF_UNIX, 0, 0) > 0;
402 getpeercred(in_fd, &ucred);
403 getpeersec(in_fd, &peersec);
408 log_error("Failed to allocate bus: %s", strerror(-r));
412 r = sd_bus_set_address(a, arg_address);
414 log_error("Failed to set address to connect to: %s", strerror(-r));
418 r = sd_bus_negotiate_fds(a, is_unix);
420 log_error("Failed to set FD negotiation: %s", strerror(-r));
425 a->fake_creds.pid = ucred.pid;
426 a->fake_creds.uid = ucred.uid;
427 a->fake_creds.gid = ucred.gid;
428 a->fake_creds_valid = true;
432 a->fake_label = peersec;
436 a->manual_peer_interface = true;
440 log_error("Failed to start bus client: %s", strerror(-r));
444 r = sd_bus_get_server_id(a, &server_id);
446 log_error("Failed to get server ID: %s", strerror(-r));
452 log_error("Failed to allocate bus: %s", strerror(-r));
456 r = sd_bus_set_fd(b, in_fd, out_fd);
458 log_error("Failed to set fds: %s", strerror(-r));
462 r = sd_bus_set_server(b, 1, server_id);
464 log_error("Failed to set server mode: %s", strerror(-r));
468 r = sd_bus_negotiate_fds(b, is_unix);
470 log_error("Failed to set FD negotiation: %s", strerror(-r));
474 r = sd_bus_set_anonymous(b, true);
476 log_error("Failed to set anonymous authentication: %s", strerror(-r));
480 b->manual_peer_interface = true;
484 log_error("Failed to start bus client: %s", strerror(-r));
488 r = rename_service(a, b);
490 log_debug("Failed to rename process: %s", strerror(-r));
493 _cleanup_free_ char *match = NULL;
496 r = sd_bus_get_unique_name(a, &unique);
498 log_error("Failed to get unique name: %s", strerror(-r));
502 match = strjoin("type='signal',"
503 "sender='org.freedesktop.DBus',"
504 "path='/org/freedesktop/DBus',"
505 "interface='org.freedesktop.DBus',"
506 "member='NameOwnerChanged',"
516 r = sd_bus_add_match(a, match, NULL, NULL);
518 log_error("Failed to add match for NameLost: %s", strerror(-r));
523 match = strjoin("type='signal',"
524 "sender='org.freedesktop.DBus',"
525 "path='/org/freedesktop/DBus',"
526 "interface='org.freedesktop.DBus',"
527 "member='NameOwnerChanged',"
537 r = sd_bus_add_match(a, match, NULL, NULL);
539 log_error("Failed to add match for NameAcquired: %s", strerror(-r));
545 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
546 int events_a, events_b, fd;
547 uint64_t timeout_a, timeout_b, t;
548 struct timespec _ts, *ts;
549 struct pollfd *pollfd;
553 r = sd_bus_process(a, &m);
555 /* treat 'connection reset by peer' as clean exit condition */
556 if (r == -ECONNRESET)
559 log_error("Failed to process bus a: %s", strerror(-r));
565 /* We officially got EOF, let's quit */
566 if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) {
571 k = synthesize_name_acquired(a, b, m);
574 log_error("Failed to synthesize message: %s", strerror(-r));
578 k = sd_bus_send(b, m, NULL);
581 log_error("Failed to send message: %s", strerror(-r));
590 r = sd_bus_process(b, &m);
592 /* treat 'connection reset by peer' as clean exit condition */
593 if (r == -ECONNRESET)
596 log_error("Failed to process bus b: %s", strerror(-r));
602 /* We officially got EOF, let's quit */
603 if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected")) {
608 k = process_hello(a, b, m, &got_hello);
617 k = sd_bus_send(a, m, NULL);
620 log_error("Failed to send message: %s", strerror(-r));
629 fd = sd_bus_get_fd(a);
631 log_error("Failed to get fd: %s", strerror(-r));
635 events_a = sd_bus_get_events(a);
637 log_error("Failed to get events mask: %s", strerror(-r));
641 r = sd_bus_get_timeout(a, &timeout_a);
643 log_error("Failed to get timeout: %s", strerror(-r));
647 events_b = sd_bus_get_events(b);
649 log_error("Failed to get events mask: %s", strerror(-r));
653 r = sd_bus_get_timeout(b, &timeout_b);
655 log_error("Failed to get timeout: %s", strerror(-r));
660 if (t == (uint64_t) -1 || (timeout_b != (uint64_t) -1 && timeout_b < timeout_a))
663 if (t == (uint64_t) -1)
668 nw = now(CLOCK_MONOTONIC);
674 ts = timespec_store(&_ts, t);
677 pollfd = (struct pollfd[3]) {
678 {.fd = fd, .events = events_a, },
679 {.fd = in_fd, .events = events_b & POLLIN, },
680 {.fd = out_fd, .events = events_b & POLLOUT, }
683 r = ppoll(pollfd, 3, ts, NULL);
685 log_error("ppoll() failed: %m");
696 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;