]> git.proxmox.com Git - systemd.git/blame - src/dbus1-generator/dbus1-generator.c
Merge tag 'upstream/229'
[systemd.git] / src / dbus1-generator / dbus1-generator.c
CommitLineData
60f067b4
JS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
db2df898
MP
20#include "alloc-util.h"
21#include "bus-internal.h"
22#include "bus-util.h"
23#include "cgroup-util.h"
60f067b4 24#include "conf-parser.h"
db2df898
MP
25#include "dirent-util.h"
26#include "fd-util.h"
27#include "fileio.h"
60f067b4 28#include "mkdir.h"
db2df898 29#include "special.h"
60f067b4 30#include "unit-name.h"
db2df898 31#include "util.h"
60f067b4
JS
32
33static const char *arg_dest_late = "/tmp", *arg_dest = "/tmp";
34
35static int create_dbus_files(
36 const char *path,
37 const char *name,
38 const char *service,
39 const char *exec,
40 const char *user,
41 const char *type) {
42
43 _cleanup_free_ char *b = NULL, *s = NULL, *lnk = NULL;
44 _cleanup_fclose_ FILE *f = NULL;
5eef597e 45 int r;
60f067b4
JS
46
47 assert(path);
48 assert(name);
49 assert(service || exec);
50
51 if (!service) {
52 _cleanup_free_ char *a = NULL;
53
54 s = strjoin("dbus-", name, ".service", NULL);
55 if (!s)
56 return log_oom();
57
58 a = strjoin(arg_dest_late, "/", s, NULL);
59 if (!a)
60 return log_oom();
61
62 f = fopen(a, "wxe");
f47781d8
MP
63 if (!f)
64 return log_error_errno(errno, "Failed to create %s: %m", a);
60f067b4
JS
65
66 fprintf(f,
67 "# Automatically generated by systemd-dbus1-generator\n\n"
68 "[Unit]\n"
69 "SourcePath=%s\n"
70 "Description=DBUS1: %s\n"
71 "Documentation=man:systemd-dbus1-generator(8)\n\n"
72 "[Service]\n"
73 "ExecStart=%s\n"
74 "Type=dbus\n"
75 "BusName=%s\n",
76 path,
77 name,
78 exec,
79 name);
80
81 if (user)
82 fprintf(f, "User=%s\n", user);
83
84
85 if (type) {
86 fprintf(f, "Environment=DBUS_STARTER_BUS_TYPE=%s\n", type);
87
88 if (streq(type, "system"))
f47781d8 89 fprintf(f, "Environment=DBUS_STARTER_ADDRESS=" DEFAULT_SYSTEM_BUS_ADDRESS "\n");
60f067b4
JS
90 else if (streq(type, "session")) {
91 char *run;
92
93 run = getenv("XDG_RUNTIME_DIR");
94 if (!run) {
95 log_error("XDG_RUNTIME_DIR not set.");
96 return -EINVAL;
97 }
98
f47781d8 99 fprintf(f, "Environment=DBUS_STARTER_ADDRESS="KERNEL_USER_BUS_ADDRESS_FMT ";" UNIX_USER_BUS_ADDRESS_FMT "\n",
60f067b4
JS
100 getuid(), run);
101 }
102 }
103
5eef597e 104 r = fflush_and_check(f);
f47781d8
MP
105 if (r < 0)
106 return log_error_errno(r, "Failed to write %s: %m", a);
60f067b4 107
6300502b 108 f = safe_fclose(f);
5eef597e 109
60f067b4
JS
110 service = s;
111 }
112
113 b = strjoin(arg_dest_late, "/", name, ".busname", NULL);
114 if (!b)
115 return log_oom();
116
117 f = fopen(b, "wxe");
f47781d8
MP
118 if (!f)
119 return log_error_errno(errno, "Failed to create %s: %m", b);
60f067b4
JS
120
121 fprintf(f,
122 "# Automatically generated by systemd-dbus1-generator\n\n"
123 "[Unit]\n"
124 "SourcePath=%s\n"
125 "Description=DBUS1: %s\n"
126 "Documentation=man:systemd-dbus1-generator(8)\n\n"
127 "[BusName]\n"
128 "Name=%s\n"
129 "Service=%s\n"
130 "AllowWorld=talk\n",
131 path,
132 name,
133 name,
134 service);
135
5eef597e 136 r = fflush_and_check(f);
f47781d8
MP
137 if (r < 0)
138 return log_error_errno(r, "Failed to write %s: %m", b);
60f067b4
JS
139
140 lnk = strjoin(arg_dest_late, "/" SPECIAL_BUSNAMES_TARGET ".wants/", name, ".busname", NULL);
141 if (!lnk)
142 return log_oom();
143
144 mkdir_parents_label(lnk, 0755);
f47781d8
MP
145 if (symlink(b, lnk))
146 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
60f067b4
JS
147
148 return 0;
149}
150
151static int add_dbus(const char *path, const char *fname, const char *type) {
152 _cleanup_free_ char *name = NULL, *exec = NULL, *user = NULL, *service = NULL;
153
5eef597e 154 const ConfigTableItem table[] = {
60f067b4
JS
155 { "D-BUS Service", "Name", config_parse_string, 0, &name },
156 { "D-BUS Service", "Exec", config_parse_string, 0, &exec },
157 { "D-BUS Service", "User", config_parse_string, 0, &user },
158 { "D-BUS Service", "SystemdService", config_parse_string, 0, &service },
e735f4d4 159 { },
60f067b4
JS
160 };
161
5eef597e 162 char *p;
60f067b4
JS
163 int r;
164
165 assert(path);
166 assert(fname);
167
e735f4d4 168 p = strjoina(path, "/", fname);
5eef597e
MP
169 r = config_parse(NULL, p, NULL,
170 "D-BUS Service\0",
171 config_item_table_lookup, table,
172 true, false, true, NULL);
60f067b4
JS
173 if (r < 0)
174 return r;
175
176 if (!name) {
177 log_warning("Activation file %s lacks name setting, ignoring.", p);
178 return 0;
179 }
180
181 if (!service_name_is_valid(name)) {
182 log_warning("Bus service name %s is not valid, ignoring.", name);
183 return 0;
184 }
185
186 if (streq(name, "org.freedesktop.systemd1")) {
187 log_debug("Skipping %s, identified as systemd.", p);
188 return 0;
189 }
190
191 if (service) {
e3bff60a 192 if (!unit_name_is_valid(service, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) {
60f067b4
JS
193 log_warning("Unit name %s is not valid, ignoring.", service);
194 return 0;
195 }
196 if (!endswith(service, ".service")) {
197 log_warning("Bus names can only activate services, ignoring %s.", p);
198 return 0;
199 }
200 } else {
201 if (streq(exec, "/bin/false") || !exec) {
202 log_warning("Neither service name nor binary path specified, ignoring %s.", p);
203 return 0;
204 }
205
206 if (exec[0] != '/') {
207 log_warning("Exec= in %s does not start with an absolute path, ignoring.", p);
208 return 0;
209 }
210 }
211
212 return create_dbus_files(p, name, service, exec, user, type);
213}
214
215static int parse_dbus_fragments(const char *path, const char *type) {
216 _cleanup_closedir_ DIR *d = NULL;
217 struct dirent *de;
218 int r;
219
220 assert(path);
221 assert(type);
222
223 d = opendir(path);
224 if (!d) {
225 if (errno == -ENOENT)
226 return 0;
227
db2df898 228 return log_error_errno(errno, "Failed to enumerate D-Bus activated services: %m");
60f067b4
JS
229 }
230
231 r = 0;
232 FOREACH_DIRENT(de, d, goto fail) {
233 int q;
234
235 if (!endswith(de->d_name, ".service"))
236 continue;
237
238 q = add_dbus(path, de->d_name, type);
239 if (q < 0)
240 r = q;
241 }
242
243 return r;
244
245fail:
db2df898 246 return log_error_errno(errno, "Failed to read D-Bus services directory: %m");
60f067b4
JS
247}
248
249static int link_busnames_target(const char *units) {
250 const char *f, *t;
251
e735f4d4
MP
252 f = strjoina(units, "/" SPECIAL_BUSNAMES_TARGET);
253 t = strjoina(arg_dest, "/" SPECIAL_BASIC_TARGET ".wants/" SPECIAL_BUSNAMES_TARGET);
60f067b4
JS
254
255 mkdir_parents_label(t, 0755);
f47781d8
MP
256 if (symlink(f, t) < 0)
257 return log_error_errno(errno, "Failed to create symlink %s: %m", t);
60f067b4
JS
258
259 return 0;
260}
261
262static int link_compatibility(const char *units) {
263 const char *f, *t;
264
e735f4d4
MP
265 f = strjoina(units, "/systemd-bus-proxyd.socket");
266 t = strjoina(arg_dest, "/" SPECIAL_DBUS_SOCKET);
60f067b4 267 mkdir_parents_label(t, 0755);
f47781d8
MP
268 if (symlink(f, t) < 0)
269 return log_error_errno(errno, "Failed to create symlink %s: %m", t);
60f067b4 270
e735f4d4
MP
271 f = strjoina(units, "/systemd-bus-proxyd.socket");
272 t = strjoina(arg_dest, "/" SPECIAL_SOCKETS_TARGET ".wants/systemd-bus-proxyd.socket");
60f067b4 273 mkdir_parents_label(t, 0755);
f47781d8
MP
274 if (symlink(f, t) < 0)
275 return log_error_errno(errno, "Failed to create symlink %s: %m", t);
60f067b4 276
e735f4d4 277 t = strjoina(arg_dest, "/" SPECIAL_DBUS_SERVICE);
f47781d8
MP
278 if (symlink("/dev/null", t) < 0)
279 return log_error_errno(errno, "Failed to mask %s: %m", t);
60f067b4
JS
280
281 return 0;
282}
283
284int main(int argc, char *argv[]) {
285 const char *path, *type, *units;
286 int r, q;
287
288 if (argc > 1 && argc != 4) {
289 log_error("This program takes three or no arguments.");
290 return EXIT_FAILURE;
291 }
292
293 if (argc > 1) {
294 arg_dest = argv[1];
295 arg_dest_late = argv[3];
296 }
297
298 log_set_target(LOG_TARGET_SAFE);
299 log_parse_environment();
300 log_open();
301
302 umask(0022);
303
e3bff60a 304 if (!is_kdbus_available())
60f067b4
JS
305 return 0;
306
307 r = cg_pid_get_owner_uid(0, NULL);
308 if (r >= 0) {
309 path = "/usr/share/dbus-1/services";
310 type = "session";
311 units = USER_DATA_UNIT_PATH;
e3bff60a 312 } else if (r == -ENXIO) {
60f067b4
JS
313 path = "/usr/share/dbus-1/system-services";
314 type = "system";
315 units = SYSTEM_DATA_UNIT_PATH;
f47781d8
MP
316 } else
317 return log_error_errno(r, "Failed to determine whether we are running as user or system instance: %m");
60f067b4
JS
318
319 r = parse_dbus_fragments(path, type);
320
321 /* FIXME: One day this should just be pulled in statically from basic.target */
322 q = link_busnames_target(units);
323 if (q < 0)
324 r = q;
325
326 q = link_compatibility(units);
327 if (q < 0)
328 r = q;
329
330 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
331}