dbus1-generator.c revision f78e6385dc4cee0a1f399c4c89ebf823c108d447
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
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"
static int create_dbus_files(
const char *path,
const char *name,
const char *service,
const char *exec,
const char *user,
const char *type) {
if (!service) {
_cleanup_free_ char *a = NULL;
if (!s)
return log_oom();
if (!a)
return log_oom();
f = fopen(a, "wxe");
if (!f) {
log_error("Failed to create %s: %m", a);
return -errno;
}
fprintf(f,
"# Automatically generated by systemd-dbus1-generator\n\n"
"[Unit]\n"
"SourcePath=%s\n"
"Description=DBUS1: %s\n\n"
"[Service]\n"
"ExecStart=%s\n"
"Type=dbus\n"
"BusName=%s\n",
path,
name,
exec,
name);
if (user)
if (type) {
fprintf(f, "Environment=DBUS_STARTER_ADDRESS=kernel:/dev/kdbus/%lu-user\n", (unsigned long) getuid());
}
fflush(f);
if (ferror(f)) {
log_error("Failed to write %s: %m", a);
return -errno;
}
service = s;
}
if (!b)
return log_oom();
f = fopen(b, "wxe");
if (!f) {
log_error("Failed to create %s: %m", b);
return -errno;
}
fprintf(f,
"# Automatically generated by systemd-dbus1-generator\n\n"
"[Unit]\n"
"SourcePath=%s\n"
"Description=DBUS1: %s\n\n"
"[BusName]\n"
"Name=%s\n"
"Service=%s\n",
path,
name,
name,
service);
fflush(f);
if (ferror(f)) {
log_error("Failed to write %s: %m", b);
return -errno;
}
if (!lnk)
return log_oom();
return -errno;
}
return 0;
}
ConfigTableItem table[] = {
};
_cleanup_free_ char *p = NULL;
int r;
if (!p)
return log_oom();
f = fopen(p, "re");
if (!f) {
return 0;
log_error("Failed to read %s: %m", p);
return -errno;
}
r = config_parse(NULL, p, f, "D-BUS Service\0", config_item_table_lookup, table, true, false, NULL);
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)) {
return 0;
}
log_debug("Skipping %s, identified as systemd.", p);
return 0;
}
if (service) {
return 0;
}
log_warning("Bus names can only activate services, ignoring %s.", p);
return 0;
}
} else {
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;
}
}
}
int r;
if (!d) {
return 0;
log_error("Failed to enumerate D-Bus activated services: %m");
return -errno;
}
r = 0;
int q;
continue;
if (q < 0)
r = q;
}
return r;
fail:
log_error("Failed to read D-Bus services directory: %m");
return -errno;
}
static int link_busnames_target(const char *units) {
const char *f, *t;
mkdir_parents_label(t, 0755);
if (symlink(f, t) < 0) {
log_error("Failed to create symlink %s: %m", t);
return -errno;
}
return 0;
}
static int link_compatibility(const char *units) {
const char *f, *t;
mkdir_parents_label(t, 0755);
if (symlink(f, t) < 0) {
log_error("Failed to create symlink %s: %m", t);
return -errno;
}
log_error("Failed to mask %s: %m", t);
return -errno;
}
return 0;
}
int r, q;
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1) {
}
log_open();
umask(0022);
return 0;
r = cg_pid_get_owner_uid(0, NULL);
if (r >= 0) {
type = "session";
} else if (r == -ENOENT) {
path = "/usr/share/dbus-1/system-services";
type = "system";
} else {
log_error("Failed to determine whether we are running as user or system instance: %s", strerror(-r));
return r;
}
/* FIXME: One day this should just be pulled in statically from basic.target */
q = link_busnames_target(units);
if (q < 0)
r = q;
q = link_compatibility(units);
if (q < 0)
r = q;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}