1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Daniel Mack
8 Copyright 2014 Kay Sievers
9 Copyright 2015 David Herrmann
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/socket.h>
27 #include <sys/types.h>
40 #include "socket-util.h"
41 #include "sd-daemon.h"
43 #include "bus-internal.h"
44 #include "bus-message.h"
49 #include "capability.h"
50 #include "bus-control.h"
51 #include "smack-util.h"
53 #include "bus-xml-policy.h"
56 #include "synthesize.h"
58 static char *arg_address = NULL;
59 static char **arg_configuration = NULL;
65 static ClientContext *client_context_free(ClientContext *c) {
75 DEFINE_TRIVIAL_CLEANUP_FUNC(ClientContext*, client_context_free);
77 static int client_context_new(ClientContext **out, int fd) {
78 _cleanup_(client_context_freep) ClientContext *c = NULL;
80 c = new0(ClientContext, 1);
91 static void *run_client(void *userdata) {
92 _cleanup_(client_context_freep) ClientContext *c = userdata;
93 _cleanup_(proxy_freep) Proxy *p = NULL;
96 r = proxy_new(&p, c->fd, c->fd, arg_address);
100 r = proxy_load_policy(p, arg_configuration);
104 r = proxy_hello_policy(p, getuid());
114 static int loop_clients(int accept_fd) {
118 r = pthread_attr_init(&attr);
120 r = log_error_errno(errno, "Cannot initialize pthread attributes: %m");
124 r = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
126 r = log_error_errno(errno, "Cannot mark pthread attributes as detached: %m");
135 fd = accept4(accept_fd, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
137 if (errno == EAGAIN || errno == EINTR)
140 r = log_error_errno(errno, "accept4() failed: %m");
144 r = client_context_new(&c, fd);
151 r = pthread_create(&tid, &attr, run_client, c);
153 log_error("Cannot spawn thread: %m");
154 client_context_free(c);
160 pthread_attr_destroy(&attr);
165 static int help(void) {
167 printf("%s [OPTIONS...]\n\n"
168 "DBus proxy server.\n\n"
169 " -h --help Show this help\n"
170 " --version Show package version\n"
171 " --configuration=PATH Configuration file or directory\n"
172 " --machine=MACHINE Connect to specified machine\n"
173 " --address=ADDRESS Connect to the bus specified by ADDRESS\n"
174 " (default: " DEFAULT_SYSTEM_BUS_ADDRESS ")\n",
175 program_invocation_short_name);
180 static int parse_argv(int argc, char *argv[]) {
189 static const struct option options[] = {
190 { "help", no_argument, NULL, 'h' },
191 { "version", no_argument, NULL, ARG_VERSION },
192 { "address", required_argument, NULL, ARG_ADDRESS },
193 { "configuration", required_argument, NULL, ARG_CONFIGURATION },
194 { "machine", required_argument, NULL, ARG_MACHINE },
203 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
212 puts(PACKAGE_STRING);
213 puts(SYSTEMD_FEATURES);
228 case ARG_CONFIGURATION:
229 r = strv_extend(&arg_configuration, optarg);
235 _cleanup_free_ char *e = NULL;
238 e = bus_address_escape(optarg);
243 a = strjoin("x-machine-kernel:machine=", e, ";x-machine-unix:machine=", e, NULL);
245 a = strjoin("x-machine-unix:machine=", e, NULL);
260 assert_not_reached("Unhandled option");
264 log_error("Too many arguments");
269 arg_address = strdup(DEFAULT_SYSTEM_BUS_ADDRESS);
277 int main(int argc, char *argv[]) {
280 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
281 log_parse_environment();
284 r = parse_argv(argc, argv);
288 r = sd_listen_fds(0);
290 log_error("Illegal number of file descriptors passed");
294 accept_fd = SD_LISTEN_FDS_START;
295 r = fd_nonblock(accept_fd, false);
297 log_error_errno(r, "Cannot mark accept-fd non-blocking: %m");
301 r = loop_clients(accept_fd);
306 "STATUS=Shutting down.");
308 strv_free(arg_configuration);
311 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;