2013-12-03 01:13:03 +01:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
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 "util.h"
|
|
|
|
#include "conf-parser.h"
|
|
|
|
#include "special.h"
|
|
|
|
#include "mkdir.h"
|
|
|
|
#include "bus-util.h"
|
|
|
|
#include "bus-internal.h"
|
|
|
|
#include "unit-name.h"
|
|
|
|
#include "cgroup-util.h"
|
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
static const char *arg_dest_late = "/tmp", *arg_dest = "/tmp";
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
static int create_dbus_files(
|
|
|
|
const char *path,
|
|
|
|
const char *name,
|
|
|
|
const char *service,
|
|
|
|
const char *exec,
|
|
|
|
const char *user,
|
|
|
|
const char *type) {
|
|
|
|
|
|
|
|
_cleanup_free_ char *b = NULL, *s = NULL, *lnk = NULL;
|
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2014-08-22 12:44:17 +02:00
|
|
|
int r;
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
assert(path);
|
|
|
|
assert(name);
|
2013-12-03 01:41:02 +01:00
|
|
|
assert(service || exec);
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
_cleanup_free_ char *a = NULL;
|
|
|
|
|
|
|
|
s = strjoin("dbus-", name, ".service", NULL);
|
|
|
|
if (!s)
|
|
|
|
return log_oom();
|
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
a = strjoin(arg_dest_late, "/", s, NULL);
|
2013-12-03 01:13:03 +01:00
|
|
|
if (!a)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
f = fopen(a, "wxe");
|
2014-11-28 19:57:32 +01:00
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to create %s: %m", a);
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by systemd-dbus1-generator\n\n"
|
|
|
|
"[Unit]\n"
|
2013-12-03 01:41:02 +01:00
|
|
|
"SourcePath=%s\n"
|
2014-03-06 18:26:06 +01:00
|
|
|
"Description=DBUS1: %s\n"
|
|
|
|
"Documentation=man:systemd-dbus1-generator(8)\n\n"
|
2013-12-03 01:13:03 +01:00
|
|
|
"[Service]\n"
|
|
|
|
"ExecStart=%s\n"
|
|
|
|
"Type=dbus\n"
|
|
|
|
"BusName=%s\n",
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
exec,
|
|
|
|
name);
|
|
|
|
|
|
|
|
if (user)
|
|
|
|
fprintf(f, "User=%s\n", user);
|
|
|
|
|
|
|
|
|
|
|
|
if (type) {
|
|
|
|
fprintf(f, "Environment=DBUS_STARTER_BUS_TYPE=%s\n", type);
|
|
|
|
|
|
|
|
if (streq(type, "system"))
|
2014-11-28 16:05:43 +01:00
|
|
|
fprintf(f, "Environment=DBUS_STARTER_ADDRESS=" DEFAULT_SYSTEM_BUS_ADDRESS "\n");
|
2014-01-26 17:51:53 +01:00
|
|
|
else if (streq(type, "session")) {
|
|
|
|
char *run;
|
|
|
|
|
|
|
|
run = getenv("XDG_RUNTIME_DIR");
|
|
|
|
if (!run) {
|
|
|
|
log_error("XDG_RUNTIME_DIR not set.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-11-28 16:05:43 +01:00
|
|
|
fprintf(f, "Environment=DBUS_STARTER_ADDRESS="KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT "\n",
|
2014-04-25 13:45:15 +02:00
|
|
|
getuid(), run);
|
2014-01-26 17:51:53 +01:00
|
|
|
}
|
2013-12-03 01:13:03 +01:00
|
|
|
}
|
|
|
|
|
2014-08-22 12:44:17 +02:00
|
|
|
r = fflush_and_check(f);
|
2014-11-28 18:23:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to write %s: %m", a);
|
2013-12-03 01:13:03 +01:00
|
|
|
|
2014-08-22 12:44:17 +02:00
|
|
|
fclose(f);
|
|
|
|
f = NULL;
|
|
|
|
|
2013-12-03 01:13:03 +01:00
|
|
|
service = s;
|
|
|
|
}
|
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
b = strjoin(arg_dest_late, "/", name, ".busname", NULL);
|
2013-12-03 01:13:03 +01:00
|
|
|
if (!b)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
f = fopen(b, "wxe");
|
2014-11-28 19:57:32 +01:00
|
|
|
if (!f)
|
|
|
|
return log_error_errno(errno, "Failed to create %s: %m", b);
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by systemd-dbus1-generator\n\n"
|
|
|
|
"[Unit]\n"
|
2013-12-03 01:41:02 +01:00
|
|
|
"SourcePath=%s\n"
|
2014-03-06 18:26:06 +01:00
|
|
|
"Description=DBUS1: %s\n"
|
|
|
|
"Documentation=man:systemd-dbus1-generator(8)\n\n"
|
2013-12-03 01:13:03 +01:00
|
|
|
"[BusName]\n"
|
|
|
|
"Name=%s\n"
|
2014-03-07 17:56:20 +01:00
|
|
|
"Service=%s\n"
|
|
|
|
"AllowWorld=talk\n",
|
2013-12-03 01:13:03 +01:00
|
|
|
path,
|
|
|
|
name,
|
|
|
|
name,
|
|
|
|
service);
|
|
|
|
|
2014-08-22 12:44:17 +02:00
|
|
|
r = fflush_and_check(f);
|
2014-11-28 18:23:20 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to write %s: %m", b);
|
2013-12-03 01:13:03 +01:00
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
lnk = strjoin(arg_dest_late, "/" SPECIAL_BUSNAMES_TARGET ".wants/", name, ".busname", NULL);
|
2013-12-03 01:13:03 +01:00
|
|
|
if (!lnk)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
mkdir_parents_label(lnk, 0755);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (symlink(b, lnk))
|
|
|
|
return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_dbus(const char *path, const char *fname, const char *type) {
|
|
|
|
_cleanup_free_ char *name = NULL, *exec = NULL, *user = NULL, *service = NULL;
|
|
|
|
|
2014-07-16 03:03:11 +02:00
|
|
|
const ConfigTableItem table[] = {
|
2013-12-03 01:13:03 +01:00
|
|
|
{ "D-BUS Service", "Name", config_parse_string, 0, &name },
|
|
|
|
{ "D-BUS Service", "Exec", config_parse_string, 0, &exec },
|
|
|
|
{ "D-BUS Service", "User", config_parse_string, 0, &user },
|
|
|
|
{ "D-BUS Service", "SystemdService", config_parse_string, 0, &service },
|
|
|
|
};
|
|
|
|
|
2014-07-17 00:27:12 +02:00
|
|
|
char *p;
|
2013-12-03 01:13:03 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(path);
|
|
|
|
assert(fname);
|
|
|
|
|
2014-03-11 15:41:22 +01:00
|
|
|
p = strappenda(path, "/", fname);
|
2014-07-17 00:27:12 +02:00
|
|
|
r = config_parse(NULL, p, NULL,
|
|
|
|
"D-BUS Service\0",
|
|
|
|
config_item_table_lookup, table,
|
|
|
|
true, false, true, NULL);
|
2013-12-03 01:13:03 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
log_warning("Activation file %s lacks name setting, ignoring.", p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!service_name_is_valid(name)) {
|
|
|
|
log_warning("Bus service name %s is not valid, ignoring.", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-03 01:24:39 +01:00
|
|
|
if (streq(name, "org.freedesktop.systemd1")) {
|
|
|
|
log_debug("Skipping %s, identified as systemd.", p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-03 01:13:03 +01:00
|
|
|
if (service) {
|
2013-12-26 21:30:22 +01:00
|
|
|
if (!unit_name_is_valid(service, TEMPLATE_INVALID)) {
|
2013-12-03 01:13:03 +01:00
|
|
|
log_warning("Unit name %s is not valid, ignoring.", service);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!endswith(service, ".service")) {
|
|
|
|
log_warning("Bus names can only activate services, ignoring %s.", p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2013-12-03 01:24:39 +01:00
|
|
|
if (streq(exec, "/bin/false") || !exec) {
|
2013-12-03 01:13:03 +01:00
|
|
|
log_warning("Neither service name nor binary path specified, ignoring %s.", p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exec[0] != '/') {
|
|
|
|
log_warning("Exec= in %s does not start with an absolute path, ignoring.", p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return create_dbus_files(p, name, service, exec, user, type);
|
|
|
|
}
|
|
|
|
|
2013-12-03 01:41:02 +01:00
|
|
|
static int parse_dbus_fragments(const char *path, const char *type) {
|
2013-12-03 01:13:03 +01:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
|
|
|
struct dirent *de;
|
|
|
|
int r;
|
|
|
|
|
2013-12-03 01:41:02 +01:00
|
|
|
assert(path);
|
|
|
|
assert(type);
|
2013-12-03 01:13:03 +01:00
|
|
|
|
2013-12-03 01:41:02 +01:00
|
|
|
d = opendir(path);
|
2013-12-03 01:13:03 +01:00
|
|
|
if (!d) {
|
|
|
|
if (errno == -ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to enumerate D-Bus activated services: %m");
|
2013-12-03 01:13:03 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
FOREACH_DIRENT(de, d, goto fail) {
|
|
|
|
int q;
|
|
|
|
|
|
|
|
if (!endswith(de->d_name, ".service"))
|
|
|
|
continue;
|
|
|
|
|
2013-12-03 01:41:02 +01:00
|
|
|
q = add_dbus(path, de->d_name, type);
|
2013-12-03 01:13:03 +01:00
|
|
|
if (q < 0)
|
|
|
|
r = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
fail:
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to read D-Bus services directory: %m");
|
2013-12-03 01:13:03 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2013-12-03 01:41:02 +01:00
|
|
|
static int link_busnames_target(const char *units) {
|
|
|
|
const char *f, *t;
|
|
|
|
|
|
|
|
f = strappenda(units, "/" SPECIAL_BUSNAMES_TARGET);
|
|
|
|
t = strappenda(arg_dest, "/" SPECIAL_BASIC_TARGET ".wants/" SPECIAL_BUSNAMES_TARGET);
|
|
|
|
|
|
|
|
mkdir_parents_label(t, 0755);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (symlink(f, t) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to create symlink %s: %m", t);
|
2013-12-03 01:41:02 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
static int link_compatibility(const char *units) {
|
|
|
|
const char *f, *t;
|
|
|
|
|
2013-12-16 20:39:44 +01:00
|
|
|
f = strappenda(units, "/systemd-bus-proxyd.socket");
|
2013-12-13 20:29:35 +01:00
|
|
|
t = strappenda(arg_dest, "/" SPECIAL_DBUS_SOCKET);
|
|
|
|
mkdir_parents_label(t, 0755);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (symlink(f, t) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to create symlink %s: %m", t);
|
2013-12-13 20:29:35 +01:00
|
|
|
|
2014-01-07 00:09:08 +01:00
|
|
|
f = strappenda(units, "/systemd-bus-proxyd.socket");
|
|
|
|
t = strappenda(arg_dest, "/" SPECIAL_SOCKETS_TARGET ".wants/systemd-bus-proxyd.socket");
|
|
|
|
mkdir_parents_label(t, 0755);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (symlink(f, t) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to create symlink %s: %m", t);
|
2014-01-07 00:09:08 +01:00
|
|
|
|
2013-12-16 21:06:34 +01:00
|
|
|
t = strappenda(arg_dest, "/" SPECIAL_DBUS_SERVICE);
|
2014-11-28 19:57:32 +01:00
|
|
|
if (symlink("/dev/null", t) < 0)
|
|
|
|
return log_error_errno(errno, "Failed to mask %s: %m", t);
|
2013-12-16 21:06:34 +01:00
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-03 01:13:03 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
2013-12-03 01:41:02 +01:00
|
|
|
const char *path, *type, *units;
|
|
|
|
int r, q;
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
if (argc > 1 && argc != 4) {
|
|
|
|
log_error("This program takes three or no arguments.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
if (argc > 1) {
|
|
|
|
arg_dest = argv[1];
|
|
|
|
arg_dest_late = argv[3];
|
|
|
|
}
|
2013-12-03 01:13:03 +01:00
|
|
|
|
|
|
|
log_set_target(LOG_TARGET_SAFE);
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
umask(0022);
|
|
|
|
|
sd-bus: sync with kdbus upstream (ABI break)
kdbus has seen a larger update than expected lately, most notably with
kdbusfs, a file system to expose the kdbus control files:
* Each time a file system of this type is mounted, a new kdbus
domain is created.
* The layout inside each mount point is the same as before, except
that domains are not hierarchically nested anymore.
* Domains are therefore also unnamed now.
* Unmounting a kdbusfs will automatically also detroy the
associated domain.
* Hence, the action of creating a kdbus domain is now as
privileged as mounting a filesystem.
* This way, we can get around creating dev nodes for everything,
which is last but not least something that is not limited by
20-bit minor numbers.
The kdbus specific bits in nspawn have all been dropped now, as nspawn
can rely on the container OS to set up its own kdbus domain, simply by
mounting a new instance.
A new set of mounts has been added to mount things *after* the kernel
modules have been loaded. For now, only kdbus is in this set, which is
invoked with mount_setup_late().
2014-11-13 20:33:03 +01:00
|
|
|
if (access("/sys/fs/kdbus/control", F_OK) < 0)
|
2013-12-03 01:13:03 +01:00
|
|
|
return 0;
|
|
|
|
|
2013-12-03 01:41:02 +01:00
|
|
|
r = cg_pid_get_owner_uid(0, NULL);
|
|
|
|
if (r >= 0) {
|
|
|
|
path = "/usr/share/dbus-1/services";
|
|
|
|
type = "session";
|
|
|
|
units = USER_DATA_UNIT_PATH;
|
|
|
|
} else if (r == -ENOENT) {
|
|
|
|
path = "/usr/share/dbus-1/system-services";
|
|
|
|
type = "system";
|
|
|
|
units = SYSTEM_DATA_UNIT_PATH;
|
2014-11-28 18:23:20 +01:00
|
|
|
} else
|
|
|
|
return log_error_errno(r, "Failed to determine whether we are running as user or system instance: %m");
|
2013-12-03 01:41:02 +01:00
|
|
|
|
|
|
|
r = parse_dbus_fragments(path, type);
|
|
|
|
|
|
|
|
/* FIXME: One day this should just be pulled in statically from basic.target */
|
|
|
|
q = link_busnames_target(units);
|
|
|
|
if (q < 0)
|
|
|
|
r = q;
|
2013-12-03 01:13:03 +01:00
|
|
|
|
2013-12-13 20:29:35 +01:00
|
|
|
q = link_compatibility(units);
|
|
|
|
if (q < 0)
|
|
|
|
r = q;
|
|
|
|
|
2013-12-03 01:13:03 +01:00
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|