bus-proxyd.c revision 50e0d56cf37d8ca5b9162ab83906920392998623
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
Copyright 2013 Daniel Mack
Copyright 2014 Kay Sievers
Copyright 2015 David Herrmann
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <getopt.h>
#include <pthread.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#include "sd-daemon.h"
#include "bus-internal.h"
#include "bus-xml-policy.h"
#include "capability.h"
#include "def.h"
#include "formats-util.h"
#include "log.h"
#include "proxy.h"
#include "strv.h"
#include "util.h"
static char *arg_address = NULL;
static char **arg_configuration = NULL;
typedef struct {
int fd;
if (!c)
return NULL;
safe_close(c->fd);
free(c);
return NULL;
}
if (!c)
return -ENOMEM;
c->fd = -1;
*out = c;
c = NULL;
return 0;
}
static void *run_client(void *userdata) {
char comm[16];
int r;
c->fd = -1;
if (r < 0)
goto exit;
/* set comm to "p$PIDu$UID" and suffix with '*' if truncated */
if (r < 0)
goto exit;
r = proxy_hello_policy(p, c->bus_uid);
if (r < 0)
goto exit;
r = proxy_run(p);
exit:
return NULL;
}
int r;
r = pthread_attr_init(&attr);
if (r != 0)
return log_error_errno(r, "Cannot initialize pthread attributes: %m");
if (r != 0) {
r = log_error_errno(r, "Cannot mark pthread attributes as detached: %m");
goto finish;
}
r = shared_policy_new(&sp);
if (r < 0)
goto finish;
for (;;) {
ClientContext *c;
int fd;
if (fd < 0) {
continue;
goto finish;
}
r = client_context_new(&c);
if (r < 0) {
log_oom();
continue;
}
if (r != 0) {
log_warning_errno(r, "Cannot spawn thread, ignoring: %m");
continue;
}
}
return r;
}
static int help(void) {
printf("%s [OPTIONS...]\n\n"
"DBus proxy server.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --configuration=PATH Configuration file or directory\n"
" --machine=MACHINE Connect to specified machine\n"
" --address=ADDRESS Connect to the bus specified by ADDRESS\n"
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{},
};
int c, r;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case ARG_ADDRESS:
if (r < 0)
return log_oom();
break;
case ARG_CONFIGURATION:
if (r < 0)
return log_oom();
break;
case ARG_MACHINE: {
_cleanup_free_ char *e = NULL;
char *a;
e = bus_address_escape(optarg);
if (!e)
return log_oom();
if (!a)
return log_oom();
arg_address = a;
break;
}
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Too many arguments");
return -EINVAL;
}
if (!arg_address) {
if (!arg_address)
return log_oom();
}
return 1;
}
int r, accept_fd;
log_open();
if (geteuid() == 0) {
const char *user = "systemd-bus-proxy";
if (r < 0) {
goto finish;
}
if (r < 0) {
log_error_errno(r, "Cannot drop privileges: %m");
goto finish;
}
}
if (r <= 0)
goto finish;
r = sd_listen_fds(0);
if (r != 1) {
log_error("Illegal number of file descriptors passed");
goto finish;
}
r = fd_nonblock(accept_fd, false);
if (r < 0) {
log_error_errno(r, "Cannot mark accept-fd non-blocking: %m");
goto finish;
}
sd_notify(false,
"STOPPING=1\n"
"STATUS=Shutting down.");
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}