]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus1-generator/dbus1-generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 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/>.
23 #include "conf-parser.h"
27 #include "bus-internal.h"
28 #include "unit-name.h"
29 #include "cgroup-util.h"
31 static const char *arg_dest
= "/tmp";
33 static int create_dbus_files(
41 _cleanup_free_
char *b
= NULL
, *s
= NULL
, *lnk
= NULL
;
42 _cleanup_fclose_
FILE *f
= NULL
;
48 _cleanup_free_
char *a
= NULL
;
50 s
= strjoin("dbus-", name
, ".service", NULL
);
54 a
= strjoin(arg_dest
, "/", s
, NULL
);
60 log_error("Failed to create %s: %m", a
);
65 "# Automatically generated by systemd-dbus1-generator\n\n"
68 "Description=DBUS1: %s\n\n"
79 fprintf(f
, "User=%s\n", user
);
83 fprintf(f
, "Environment=DBUS_STARTER_BUS_TYPE=%s\n", type
);
85 if (streq(type
, "system"))
86 fprintf(f
, "Environment=DBUS_STARTER_ADDRESS=kernel:/dev/kdbus/0-system\n");
87 else if (streq(type
, "session"))
88 fprintf(f
, "Environment=DBUS_STARTER_ADDRESS=kernel:/dev/kdbus/%lu-user\n", (unsigned long) getuid());
93 log_error("Failed to write %s: %m", a
);
100 b
= strjoin(arg_dest
, "/", name
, ".busname", NULL
);
106 log_error("Failed to create %s: %m", b
);
111 "# Automatically generated by systemd-dbus1-generator\n\n"
114 "Description=DBUS1: %s\n\n"
125 log_error("Failed to write %s: %m", b
);
129 lnk
= strjoin(arg_dest
, "/" SPECIAL_BUSNAMES_TARGET
".wants/", name
, ".busname", NULL
);
133 mkdir_parents_label(lnk
, 0755);
134 if (symlink(b
, lnk
)) {
135 log_error("Failed to create symlinks %s: %m", lnk
);
142 static int add_dbus(const char *path
, const char *fname
, const char *type
) {
143 _cleanup_free_
char *name
= NULL
, *exec
= NULL
, *user
= NULL
, *service
= NULL
;
145 ConfigTableItem table
[] = {
146 { "D-BUS Service", "Name", config_parse_string
, 0, &name
},
147 { "D-BUS Service", "Exec", config_parse_string
, 0, &exec
},
148 { "D-BUS Service", "User", config_parse_string
, 0, &user
},
149 { "D-BUS Service", "SystemdService", config_parse_string
, 0, &service
},
152 _cleanup_fclose_
FILE *f
= NULL
;
153 _cleanup_free_
char *p
= NULL
;
159 p
= strjoin(path
, "/", fname
, NULL
);
165 if (errno
== -ENOENT
)
168 log_error("Failed to read %s: %m", p
);
172 r
= config_parse(NULL
, p
, f
, "D-BUS Service\0", config_item_table_lookup
, table
, true, false, NULL
);
177 log_warning("Activation file %s lacks name setting, ignoring.", p
);
181 if (!service_name_is_valid(name
)) {
182 log_warning("Bus service name %s is not valid, ignoring.", name
);
186 if (streq(name
, "org.freedesktop.systemd1")) {
187 log_debug("Skipping %s, identified as systemd.", p
);
192 if (!unit_name_is_valid(service
, false)) {
193 log_warning("Unit name %s is not valid, ignoring.", service
);
196 if (!endswith(service
, ".service")) {
197 log_warning("Bus names can only activate services, ignoring %s.", p
);
201 if (streq(exec
, "/bin/false") || !exec
) {
202 log_warning("Neither service name nor binary path specified, ignoring %s.", p
);
206 if (exec
[0] != '/') {
207 log_warning("Exec= in %s does not start with an absolute path, ignoring.", p
);
212 return create_dbus_files(p
, name
, service
, exec
, user
, type
);
215 static int parse_dbus_fragments(void) {
216 _cleanup_closedir_
DIR *d
= NULL
;
218 const char *p
, *type
;
221 r
= cg_pid_get_owner_uid(0, NULL
);
223 p
= "/usr/share/dbus-1/services";
225 } else if (r
== -ENOENT
) {
226 p
= "/usr/share/dbus-1/system-services";
229 log_error("Failed to determine whether we are running as user or system instance: %s", strerror(-r
));
235 if (errno
== -ENOENT
)
238 log_error("Failed to enumerate D-Bus activated services: %m");
243 FOREACH_DIRENT(de
, d
, goto fail
) {
246 if (!endswith(de
->d_name
, ".service"))
249 q
= add_dbus(p
, de
->d_name
, type
);
257 log_error("Failed to read D-Bus services directory: %m");
261 int main(int argc
, char *argv
[]) {
264 if (argc
> 1 && argc
!= 4) {
265 log_error("This program takes three or no arguments.");
272 log_set_target(LOG_TARGET_SAFE
);
273 log_parse_environment();
278 if (access("/dev/kdbus/control", F_OK
) < 0)
281 r
= parse_dbus_fragments();
283 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;