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/>.
22 #include "alloc-util.h"
23 #include "bus-internal.h"
25 #include "cgroup-util.h"
26 #include "conf-parser.h"
27 #include "dirent-util.h"
32 #include "unit-name.h"
35 static const char *arg_dest_late
= "/tmp", *arg_dest
= "/tmp";
37 static int create_dbus_files(
45 _cleanup_free_
char *b
= NULL
, *s
= NULL
, *lnk
= NULL
;
46 _cleanup_fclose_
FILE *f
= NULL
;
51 assert(service
|| exec
);
54 _cleanup_free_
char *a
= NULL
;
56 s
= strjoin("dbus-", name
, ".service", NULL
);
60 a
= strjoin(arg_dest_late
, "/", s
, NULL
);
66 return log_error_errno(errno
, "Failed to create %s: %m", a
);
69 "# Automatically generated by systemd-dbus1-generator\n\n"
72 "Description=DBUS1: %s\n"
73 "Documentation=man:systemd-dbus1-generator(8)\n\n"
84 fprintf(f
, "User=%s\n", user
);
88 fprintf(f
, "Environment=DBUS_STARTER_BUS_TYPE=%s\n", type
);
90 if (streq(type
, "system"))
91 fprintf(f
, "Environment=DBUS_STARTER_ADDRESS=" DEFAULT_SYSTEM_BUS_ADDRESS
"\n");
92 else if (streq(type
, "session")) {
95 run
= getenv("XDG_RUNTIME_DIR");
97 log_error("XDG_RUNTIME_DIR not set.");
101 fprintf(f
, "Environment=DBUS_STARTER_ADDRESS="KERNEL_USER_BUS_ADDRESS_FMT
";" UNIX_USER_BUS_ADDRESS_FMT
"\n",
106 r
= fflush_and_check(f
);
108 return log_error_errno(r
, "Failed to write %s: %m", a
);
115 b
= strjoin(arg_dest_late
, "/", name
, ".busname", NULL
);
121 return log_error_errno(errno
, "Failed to create %s: %m", b
);
124 "# Automatically generated by systemd-dbus1-generator\n\n"
127 "Description=DBUS1: %s\n"
128 "Documentation=man:systemd-dbus1-generator(8)\n\n"
138 r
= fflush_and_check(f
);
140 return log_error_errno(r
, "Failed to write %s: %m", b
);
142 lnk
= strjoin(arg_dest_late
, "/" SPECIAL_BUSNAMES_TARGET
".wants/", name
, ".busname", NULL
);
146 mkdir_parents_label(lnk
, 0755);
148 return log_error_errno(errno
, "Failed to create symlink %s: %m", lnk
);
153 static int add_dbus(const char *path
, const char *fname
, const char *type
) {
154 _cleanup_free_
char *name
= NULL
, *exec
= NULL
, *user
= NULL
, *service
= NULL
;
156 const ConfigTableItem table
[] = {
157 { "D-BUS Service", "Name", config_parse_string
, 0, &name
},
158 { "D-BUS Service", "Exec", config_parse_string
, 0, &exec
},
159 { "D-BUS Service", "User", config_parse_string
, 0, &user
},
160 { "D-BUS Service", "SystemdService", config_parse_string
, 0, &service
},
170 p
= strjoina(path
, "/", fname
);
171 r
= config_parse(NULL
, p
, NULL
,
173 config_item_table_lookup
, table
,
174 true, false, true, NULL
);
179 log_warning("Activation file %s lacks name setting, ignoring.", p
);
183 if (!service_name_is_valid(name
)) {
184 log_warning("Bus service name %s is not valid, ignoring.", name
);
188 if (streq(name
, "org.freedesktop.systemd1")) {
189 log_debug("Skipping %s, identified as systemd.", p
);
194 if (!unit_name_is_valid(service
, UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
)) {
195 log_warning("Unit name %s is not valid, ignoring.", service
);
198 if (!endswith(service
, ".service")) {
199 log_warning("Bus names can only activate services, ignoring %s.", p
);
203 if (streq(exec
, "/bin/false") || !exec
) {
204 log_warning("Neither service name nor binary path specified, ignoring %s.", p
);
208 if (exec
[0] != '/') {
209 log_warning("Exec= in %s does not start with an absolute path, ignoring.", p
);
214 return create_dbus_files(p
, name
, service
, exec
, user
, type
);
217 static int parse_dbus_fragments(const char *path
, const char *type
) {
218 _cleanup_closedir_
DIR *d
= NULL
;
227 if (errno
== -ENOENT
)
230 return log_error_errno(errno
, "Failed to enumerate D-Bus activated services: %m");
234 FOREACH_DIRENT(de
, d
, goto fail
) {
237 if (!endswith(de
->d_name
, ".service"))
240 q
= add_dbus(path
, de
->d_name
, type
);
248 return log_error_errno(errno
, "Failed to read D-Bus services directory: %m");
251 static int link_busnames_target(const char *units
) {
254 f
= strjoina(units
, "/" SPECIAL_BUSNAMES_TARGET
);
255 t
= strjoina(arg_dest
, "/" SPECIAL_BASIC_TARGET
".wants/" SPECIAL_BUSNAMES_TARGET
);
257 mkdir_parents_label(t
, 0755);
258 if (symlink(f
, t
) < 0)
259 return log_error_errno(errno
, "Failed to create symlink %s: %m", t
);
264 static int link_compatibility(const char *units
) {
267 f
= strjoina(units
, "/systemd-bus-proxyd.socket");
268 t
= strjoina(arg_dest
, "/" SPECIAL_DBUS_SOCKET
);
269 mkdir_parents_label(t
, 0755);
270 if (symlink(f
, t
) < 0)
271 return log_error_errno(errno
, "Failed to create symlink %s: %m", t
);
273 f
= strjoina(units
, "/systemd-bus-proxyd.socket");
274 t
= strjoina(arg_dest
, "/" SPECIAL_SOCKETS_TARGET
".wants/systemd-bus-proxyd.socket");
275 mkdir_parents_label(t
, 0755);
276 if (symlink(f
, t
) < 0)
277 return log_error_errno(errno
, "Failed to create symlink %s: %m", t
);
279 t
= strjoina(arg_dest
, "/" SPECIAL_DBUS_SERVICE
);
280 if (symlink("/dev/null", t
) < 0)
281 return log_error_errno(errno
, "Failed to mask %s: %m", t
);
286 int main(int argc
, char *argv
[]) {
287 const char *path
, *type
, *units
;
290 if (argc
> 1 && argc
!= 4) {
291 log_error("This program takes three or no arguments.");
297 arg_dest_late
= argv
[3];
300 log_set_target(LOG_TARGET_SAFE
);
301 log_parse_environment();
306 if (!is_kdbus_available())
309 r
= cg_pid_get_owner_uid(0, NULL
);
311 path
= "/usr/share/dbus-1/services";
313 units
= USER_DATA_UNIT_PATH
;
314 } else if (r
== -ENXIO
) {
315 path
= "/usr/share/dbus-1/system-services";
317 units
= SYSTEM_DATA_UNIT_PATH
;
319 return log_error_errno(r
, "Failed to determine whether we are running as user or system instance: %m");
321 r
= parse_dbus_fragments(path
, type
);
323 /* FIXME: One day this should just be pulled in statically from basic.target */
324 q
= link_busnames_target(units
);
328 q
= link_compatibility(units
);
332 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;