]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
8feca247 BB |
2 | |
3 | #include <errno.h> | |
4 | #include <stdio.h> | |
5 | #include <unistd.h> | |
6 | ||
7 | #include "xdg-autostart-service.h" | |
8 | ||
9 | #include "conf-parser.h" | |
10 | #include "escape.h" | |
11 | #include "unit-name.h" | |
12 | #include "path-util.h" | |
13 | #include "fd-util.h" | |
14 | #include "generator.h" | |
15 | #include "log.h" | |
16 | #include "specifier.h" | |
17 | #include "string-util.h" | |
18 | #include "nulstr-util.h" | |
19 | #include "strv.h" | |
20 | ||
21 | XdgAutostartService* xdg_autostart_service_free(XdgAutostartService *s) { | |
22 | if (!s) | |
23 | return NULL; | |
24 | ||
25 | free(s->name); | |
26 | free(s->path); | |
27 | free(s->description); | |
28 | ||
29 | free(s->type); | |
30 | free(s->exec_string); | |
d5273f51 | 31 | free(s->working_directory); |
8feca247 BB |
32 | |
33 | strv_free(s->only_show_in); | |
34 | strv_free(s->not_show_in); | |
35 | ||
36 | free(s->try_exec); | |
37 | free(s->autostart_condition); | |
38 | free(s->kde_autostart_condition); | |
39 | ||
40 | free(s->gnome_autostart_phase); | |
41 | ||
42 | return mfree(s); | |
43 | } | |
44 | ||
45 | char *xdg_autostart_service_translate_name(const char *name) { | |
46 | _cleanup_free_ char *c = NULL, *escaped = NULL; | |
47 | char *res; | |
48 | ||
49 | c = strdup(name); | |
50 | if (!c) | |
51 | return NULL; | |
52 | ||
53 | res = endswith(c, ".desktop"); | |
54 | if (res) | |
55 | *res = '\0'; | |
56 | ||
57 | escaped = unit_name_escape(c); | |
58 | if (!escaped) | |
59 | return NULL; | |
60 | ||
61 | return strjoin("app-", escaped, "-autostart.service"); | |
62 | } | |
63 | ||
64 | static int xdg_config_parse_bool( | |
65 | const char *unit, | |
66 | const char *filename, | |
67 | unsigned line, | |
68 | const char *section, | |
69 | unsigned section_line, | |
70 | const char *lvalue, | |
71 | int ltype, | |
72 | const char *rvalue, | |
73 | void *data, | |
74 | void *userdata) { | |
75 | ||
76 | bool *b = data; | |
8feca247 BB |
77 | |
78 | assert(filename); | |
79 | assert(lvalue); | |
80 | assert(rvalue); | |
81 | assert(data); | |
82 | ||
83 | if (streq(rvalue, "true")) | |
84 | *b = true; | |
85 | else if (streq(rvalue, "false")) | |
86 | *b = false; | |
87 | else | |
6527b019 | 88 | return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Invalid value for boolean: %s", rvalue); |
8feca247 BB |
89 | |
90 | return 0; | |
91 | } | |
92 | ||
93 | /* Unescapes the string in-place, returns non-zero status on error. */ | |
94 | static int xdg_unescape_string( | |
95 | const char *unit, | |
96 | const char *filename, | |
97 | int line, | |
98 | char *str) { | |
99 | ||
100 | char *in; | |
101 | char *out; | |
102 | ||
103 | assert(str); | |
104 | ||
105 | in = out = str; | |
106 | ||
107 | for (; *in; in++, out++) { | |
108 | if (*in == '\\') { | |
109 | /* Move forward, and ensure it is a valid escape. */ | |
110 | in++; | |
111 | ||
112 | switch (*in) { | |
113 | case 's': | |
114 | *out = ' '; | |
115 | break; | |
116 | case 'n': | |
117 | *out = '\n'; | |
118 | break; | |
119 | case 't': | |
120 | *out = '\t'; | |
121 | break; | |
122 | case 'r': | |
123 | *out = '\r'; | |
124 | break; | |
125 | case '\\': | |
126 | *out = '\\'; | |
127 | break; | |
128 | case ';': | |
129 | /* Technically only permitted for strv. */ | |
130 | *out = ';'; | |
131 | break; | |
132 | default: | |
133 | return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Undefined escape sequence \\%c.", *in); | |
134 | } | |
135 | ||
136 | continue; | |
137 | } | |
138 | ||
139 | *out = *in; | |
140 | } | |
141 | *out = '\0'; | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | /* Note: We do not bother with unescaping the strings, hence the _raw postfix. */ | |
147 | static int xdg_config_parse_string( | |
148 | const char *unit, | |
149 | const char *filename, | |
150 | unsigned line, | |
151 | const char *section, | |
152 | unsigned section_line, | |
153 | const char *lvalue, | |
154 | int ltype, | |
155 | const char *rvalue, | |
156 | void *data, | |
157 | void *userdata) { | |
158 | ||
159 | _cleanup_free_ char *res = NULL; | |
160 | char **out = data; | |
161 | int r; | |
162 | ||
163 | assert(filename); | |
164 | assert(lvalue); | |
165 | assert(rvalue); | |
166 | assert(data); | |
167 | ||
168 | /* XDG does not allow duplicate definitions. */ | |
169 | if (*out) { | |
40f04cde | 170 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Key %s was defined multiple times, ignoring.", lvalue); |
8feca247 BB |
171 | return 0; |
172 | } | |
173 | ||
174 | res = strdup(rvalue); | |
175 | if (!res) | |
176 | return log_oom(); | |
177 | ||
178 | r = xdg_unescape_string(unit, filename, line, res); | |
179 | if (r < 0) | |
180 | return r; | |
181 | ||
182 | *out = TAKE_PTR(res); | |
183 | return 0; | |
184 | } | |
185 | ||
d1ca1f7c ZJS |
186 | static int strv_strndup_unescape_and_push( |
187 | const char *unit, | |
188 | const char *filename, | |
189 | unsigned line, | |
190 | char ***sv, | |
191 | size_t *n_allocated, | |
192 | size_t *n, | |
193 | const char *start, | |
194 | const char *end) { | |
195 | ||
dea7f5cc ZJS |
196 | if (end == start) |
197 | return 0; | |
198 | ||
d1ca1f7c ZJS |
199 | _cleanup_free_ char *copy = NULL; |
200 | int r; | |
201 | ||
202 | copy = strndup(start, end - start); | |
203 | if (!copy) | |
204 | return log_oom(); | |
205 | ||
206 | r = xdg_unescape_string(unit, filename, line, copy); | |
207 | if (r < 0) | |
208 | return r; | |
209 | ||
210 | if (!greedy_realloc((void**) sv, n_allocated, *n + 2, sizeof(char*))) /* One extra for NULL */ | |
211 | return log_oom(); | |
212 | ||
213 | (*sv)[*n] = TAKE_PTR(copy); | |
214 | (*sv)[*n + 1] = NULL; | |
215 | (*n)++; | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
8feca247 BB |
220 | static int xdg_config_parse_strv( |
221 | const char *unit, | |
222 | const char *filename, | |
223 | unsigned line, | |
224 | const char *section, | |
225 | unsigned section_line, | |
226 | const char *lvalue, | |
227 | int ltype, | |
228 | const char *rvalue, | |
229 | void *data, | |
230 | void *userdata) { | |
231 | ||
d1ca1f7c | 232 | char ***ret_sv = data; |
8feca247 BB |
233 | int r; |
234 | ||
235 | assert(filename); | |
236 | assert(lvalue); | |
237 | assert(rvalue); | |
238 | assert(data); | |
239 | ||
240 | /* XDG does not allow duplicate definitions. */ | |
d1ca1f7c | 241 | if (*ret_sv) { |
40f04cde | 242 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Key %s was already defined, ignoring.", lvalue); |
8feca247 BB |
243 | return 0; |
244 | } | |
245 | ||
d1ca1f7c ZJS |
246 | size_t n = 0, n_allocated = 0; |
247 | _cleanup_strv_free_ char **sv = NULL; | |
248 | ||
249 | if (!GREEDY_REALLOC0(sv, n_allocated, 1)) | |
8feca247 BB |
250 | return log_oom(); |
251 | ||
252 | /* We cannot use strv_split because it does not handle escaping correctly. */ | |
d1ca1f7c | 253 | const char *start = rvalue, *end; |
8feca247 BB |
254 | |
255 | for (end = start; *end; end++) { | |
256 | if (*end == '\\') { | |
257 | /* Move forward, and ensure it is a valid escape. */ | |
258 | end++; | |
d1ca1f7c | 259 | if (!strchr("sntr\\;", *end)) { |
40f04cde | 260 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Undefined escape sequence \\%c.", *end); |
8feca247 BB |
261 | return 0; |
262 | } | |
263 | continue; | |
264 | } | |
265 | ||
266 | if (*end == ';') { | |
d1ca1f7c ZJS |
267 | r = strv_strndup_unescape_and_push(unit, filename, line, |
268 | &sv, &n_allocated, &n, | |
269 | start, end); | |
8feca247 BB |
270 | if (r < 0) |
271 | return r; | |
8feca247 BB |
272 | |
273 | start = end + 1; | |
274 | } | |
275 | } | |
276 | ||
dea7f5cc ZJS |
277 | /* Handle the trailing entry after the last separator */ |
278 | r = strv_strndup_unescape_and_push(unit, filename, line, | |
279 | &sv, &n_allocated, &n, | |
280 | start, end); | |
281 | if (r < 0) | |
282 | return r; | |
8feca247 | 283 | |
d1ca1f7c | 284 | *ret_sv = TAKE_PTR(sv); |
8feca247 BB |
285 | return 0; |
286 | } | |
287 | ||
288 | static int xdg_config_item_table_lookup( | |
289 | const void *table, | |
290 | const char *section, | |
291 | const char *lvalue, | |
292 | ConfigParserCallback *func, | |
293 | int *ltype, | |
294 | void **data, | |
295 | void *userdata) { | |
296 | ||
297 | assert(lvalue); | |
298 | ||
299 | /* Ignore any keys with [] as those are translations. */ | |
300 | if (strchr(lvalue, '[')) { | |
301 | *func = NULL; | |
302 | *ltype = 0; | |
303 | *data = NULL; | |
304 | return 1; | |
305 | } | |
306 | ||
307 | return config_item_table_lookup(table, section, lvalue, func, ltype, data, userdata); | |
308 | } | |
309 | ||
310 | XdgAutostartService *xdg_autostart_service_parse_desktop(const char *path) { | |
311 | _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL; | |
312 | int r; | |
313 | ||
314 | service = new0(XdgAutostartService, 1); | |
315 | if (!service) | |
316 | return NULL; | |
317 | ||
318 | service->path = strdup(path); | |
319 | if (!service->path) | |
320 | return NULL; | |
321 | ||
322 | const ConfigTableItem items[] = { | |
323 | { "Desktop Entry", "Name", xdg_config_parse_string, 0, &service->description}, | |
324 | { "Desktop Entry", "Exec", xdg_config_parse_string, 0, &service->exec_string}, | |
d5273f51 | 325 | { "Desktop Entry", "Path", xdg_config_parse_string, 0, &service->working_directory}, |
8feca247 BB |
326 | { "Desktop Entry", "TryExec", xdg_config_parse_string, 0, &service->try_exec}, |
327 | { "Desktop Entry", "Type", xdg_config_parse_string, 0, &service->type}, | |
328 | { "Desktop Entry", "OnlyShowIn", xdg_config_parse_strv, 0, &service->only_show_in}, | |
329 | { "Desktop Entry", "NotShowIn", xdg_config_parse_strv, 0, &service->not_show_in}, | |
330 | { "Desktop Entry", "Hidden", xdg_config_parse_bool, 0, &service->hidden}, | |
331 | { "Desktop Entry", "AutostartCondition", xdg_config_parse_string, 0, &service->autostart_condition}, | |
332 | { "Desktop Entry", "X-KDE-autostart-condition", xdg_config_parse_string, 0, &service->kde_autostart_condition}, | |
333 | { "Desktop Entry", "X-GNOME-Autostart-Phase", xdg_config_parse_string, 0, &service->gnome_autostart_phase}, | |
334 | { "Desktop Entry", "X-systemd-skip", xdg_config_parse_bool, 0, &service->systemd_skip}, | |
335 | ||
336 | /* Common entries that we do not use currently. */ | |
337 | { "Desktop Entry", "Categories", NULL, 0, NULL}, | |
338 | { "Desktop Entry", "Comment", NULL, 0, NULL}, | |
339 | { "Desktop Entry", "Encoding", NULL, 0, NULL}, | |
340 | { "Desktop Entry", "GenericName", NULL, 0, NULL}, | |
341 | { "Desktop Entry", "Icon", NULL, 0, NULL}, | |
342 | { "Desktop Entry", "Keywords", NULL, 0, NULL}, | |
51ac77d5 | 343 | { "Desktop Entry", "MimeType", NULL, 0, NULL}, |
8feca247 BB |
344 | { "Desktop Entry", "NoDisplay", NULL, 0, NULL}, |
345 | { "Desktop Entry", "StartupNotify", NULL, 0, NULL}, | |
51ac77d5 | 346 | { "Desktop Entry", "StartupWMClass", NULL, 0, NULL}, |
8feca247 | 347 | { "Desktop Entry", "Terminal", NULL, 0, NULL}, |
51ac77d5 | 348 | { "Desktop Entry", "URL", NULL, 0, NULL}, |
8feca247 BB |
349 | { "Desktop Entry", "Version", NULL, 0, NULL}, |
350 | {} | |
351 | }; | |
352 | ||
353 | r = config_parse(NULL, service->path, NULL, | |
354 | "Desktop Entry\0", | |
355 | xdg_config_item_table_lookup, items, | |
4f9ff96a LP |
356 | CONFIG_PARSE_WARN, service, |
357 | NULL); | |
8feca247 BB |
358 | /* If parsing failed, only hide the file so it will still mask others. */ |
359 | if (r < 0) { | |
360 | log_warning_errno(r, "Failed to parse %s, ignoring it", service->path); | |
361 | service->hidden = true; | |
362 | } | |
363 | ||
364 | return TAKE_PTR(service); | |
365 | } | |
366 | ||
367 | int xdg_autostart_format_exec_start( | |
368 | const char *exec, | |
369 | char **ret_exec_start) { | |
370 | ||
371 | _cleanup_strv_free_ char **exec_split = NULL; | |
372 | char *res; | |
373 | size_t n, i; | |
374 | bool first_arg; | |
375 | int r; | |
376 | ||
377 | /* | |
378 | * Unfortunately, there is a mismatch between systemd's idea of $PATH | |
379 | * and XDGs. i.e. we need to ensure that we have an absolute path to | |
380 | * support cases where $PATH has been modified from the default set. | |
381 | * | |
382 | * Note that this is only needed for development environments though; | |
383 | * so while it is important, this should have no effect in production | |
384 | * environments. | |
385 | * | |
386 | * To be compliant with the XDG specification, we also need to strip | |
387 | * certain parameters and such. Doing so properly makes parsing the | |
388 | * command line unavoidable. | |
389 | * | |
390 | * NOTE: Technically, XDG only specifies " as quotes, while this also | |
391 | * accepts '. | |
392 | */ | |
90e30d76 | 393 | r = strv_split_full(&exec_split, exec, NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX); |
0645b83a ZJS |
394 | if (r < 0) |
395 | return r; | |
8feca247 BB |
396 | |
397 | if (strv_isempty(exec_split)) | |
398 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), "Exec line is empty"); | |
399 | ||
400 | first_arg = true; | |
401 | for (i = n = 0; exec_split[i]; i++) { | |
402 | _cleanup_free_ char *c = NULL, *raw = NULL, *p = NULL, *escaped = NULL, *quoted = NULL; | |
403 | ||
404 | r = cunescape(exec_split[i], 0, &c); | |
405 | if (r < 0) | |
406 | return log_debug_errno(r, "Failed to unescape '%s': %m", exec_split[i]); | |
407 | ||
408 | if (first_arg) { | |
409 | _cleanup_free_ char *executable = NULL; | |
410 | ||
411 | /* This is the executable, find it in $PATH */ | |
412 | first_arg = false; | |
f7bc0c32 | 413 | r = find_executable(c, &executable); |
8feca247 BB |
414 | if (r < 0) |
415 | return log_info_errno(r, "Exec binary '%s' does not exist: %m", c); | |
416 | ||
417 | escaped = cescape(executable); | |
418 | if (!escaped) | |
419 | return log_oom(); | |
420 | ||
421 | free(exec_split[n]); | |
422 | exec_split[n++] = TAKE_PTR(escaped); | |
423 | continue; | |
424 | } | |
425 | ||
426 | /* | |
427 | * Remove any standardised XDG fields; we assume they never appear as | |
428 | * part of another argument as that just does not make any sense as | |
429 | * they can be empty (GLib will e.g. turn "%f" into an empty argument). | |
430 | * Other implementations may handle this differently. | |
431 | */ | |
432 | if (STR_IN_SET(c, | |
433 | "%f", "%F", | |
434 | "%u", "%U", | |
435 | "%d", "%D", | |
436 | "%n", "%N", | |
437 | "%i", /* Location of icon, could be implemented. */ | |
438 | "%c", /* Translated application name, could be implemented. */ | |
439 | "%k", /* Location of desktop file, could be implemented. */ | |
440 | "%v", | |
441 | "%m" | |
442 | )) | |
443 | continue; | |
444 | ||
445 | /* | |
446 | * %% -> % and then % -> %% means that we correctly quote any % | |
447 | * and also quote any left over (and invalid) % specifier from | |
448 | * the desktop file. | |
449 | */ | |
450 | raw = strreplace(c, "%%", "%"); | |
451 | if (!raw) | |
452 | return log_oom(); | |
453 | p = strreplace(raw, "%", "%%"); | |
454 | if (!p) | |
455 | return log_oom(); | |
456 | escaped = cescape(p); | |
457 | if (!escaped) | |
458 | return log_oom(); | |
459 | ||
460 | quoted = strjoin("\"", escaped, "\""); | |
461 | if (!quoted) | |
462 | return log_oom(); | |
463 | ||
464 | free(exec_split[n]); | |
465 | exec_split[n++] = TAKE_PTR(quoted); | |
466 | } | |
467 | for (; exec_split[n]; n++) | |
468 | exec_split[n] = mfree(exec_split[n]); | |
469 | ||
470 | res = strv_join(exec_split, " "); | |
471 | if (!res) | |
472 | return log_oom(); | |
473 | ||
474 | *ret_exec_start = res; | |
475 | return 0; | |
476 | } | |
477 | ||
478 | static int xdg_autostart_generate_desktop_condition( | |
479 | FILE *f, | |
480 | const char *test_binary, | |
481 | const char *condition) { | |
482 | ||
483 | int r; | |
484 | ||
485 | /* Generate an ExecCondition for GNOME autostart condition */ | |
486 | if (!isempty(condition)) { | |
487 | _cleanup_free_ char *gnome_autostart_condition_path = NULL, *e_autostart_condition = NULL; | |
488 | ||
f7bc0c32 | 489 | r = find_executable(test_binary, &gnome_autostart_condition_path); |
8feca247 | 490 | if (r < 0) { |
47c1db67 | 491 | log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, |
8feca247 BB |
492 | "%s not found: %m", test_binary); |
493 | fprintf(f, "# ExecCondition using %s skipped due to missing binary.\n", test_binary); | |
494 | return r; | |
495 | } | |
496 | ||
497 | e_autostart_condition = cescape(condition); | |
498 | if (!e_autostart_condition) | |
499 | return log_oom(); | |
500 | ||
501 | fprintf(f, | |
502 | "ExecCondition=%s --condition \"%s\"\n", | |
503 | gnome_autostart_condition_path, | |
504 | e_autostart_condition); | |
505 | } | |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
510 | int xdg_autostart_service_generate_unit( | |
511 | XdgAutostartService *service, | |
512 | const char *dest) { | |
513 | ||
514 | _cleanup_free_ char *path_escaped = NULL, *exec_start = NULL, *unit = NULL; | |
515 | _cleanup_fclose_ FILE *f = NULL; | |
516 | int r; | |
517 | ||
518 | assert(service); | |
519 | ||
520 | /* Nothing to do for hidden services. */ | |
521 | if (service->hidden) { | |
47c1db67 | 522 | log_debug("Not generating service for XDG autostart %s, it is hidden.", service->name); |
8feca247 BB |
523 | return 0; |
524 | } | |
525 | ||
526 | if (service->systemd_skip) { | |
47c1db67 | 527 | log_debug("Not generating service for XDG autostart %s, should be skipped by generator.", service->name); |
8feca247 BB |
528 | return 0; |
529 | } | |
530 | ||
531 | /* Nothing to do if type is not Application. */ | |
532 | if (!streq_ptr(service->type, "Application")) { | |
47c1db67 | 533 | log_debug("Not generating service for XDG autostart %s, only Type=Application is supported.", service->name); |
8feca247 BB |
534 | return 0; |
535 | } | |
536 | ||
537 | if (!service->exec_string) { | |
538 | log_warning("Not generating service for XDG autostart %s, it is has no Exec= line.", service->name); | |
539 | return 0; | |
540 | } | |
541 | ||
542 | /* | |
543 | * The TryExec key cannot be checked properly from the systemd unit, | |
f7bc0c32 | 544 | * it is trivial to check using find_executable though. |
8feca247 BB |
545 | */ |
546 | if (service->try_exec) { | |
f7bc0c32 | 547 | r = find_executable(service->try_exec, NULL); |
8feca247 | 548 | if (r < 0) { |
47c1db67 | 549 | log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, |
8feca247 BB |
550 | "Not generating service for XDG autostart %s, could not find TryExec= binary %s: %m", |
551 | service->name, service->try_exec); | |
552 | return 0; | |
553 | } | |
554 | } | |
555 | ||
556 | r = xdg_autostart_format_exec_start(service->exec_string, &exec_start); | |
557 | if (r < 0) { | |
558 | log_warning_errno(r, | |
559 | "Not generating service for XDG autostart %s, error parsing Exec= line: %m", | |
560 | service->name); | |
561 | return 0; | |
562 | } | |
563 | ||
9d9a9500 BB |
564 | if (service->gnome_autostart_phase) { |
565 | /* There is no explicit value for the "Application" phase. */ | |
47c1db67 BB |
566 | log_debug("Not generating service for XDG autostart %s, startup phases are not supported.", |
567 | service->name); | |
8feca247 BB |
568 | return 0; |
569 | } | |
570 | ||
571 | path_escaped = specifier_escape(service->path); | |
572 | if (!path_escaped) | |
573 | return log_oom(); | |
574 | ||
575 | unit = path_join(dest, service->name); | |
576 | if (!unit) | |
577 | return log_oom(); | |
578 | ||
579 | f = fopen(unit, "wxe"); | |
580 | if (!f) | |
581 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
582 | ||
583 | fprintf(f, | |
584 | "# Automatically generated by systemd-xdg-autostart-generator\n\n" | |
585 | "[Unit]\n" | |
586 | "Documentation=man:systemd-xdg-autostart-generator(8)\n" | |
587 | "SourcePath=%s\n" | |
588 | "PartOf=graphical-session.target\n\n", | |
589 | path_escaped); | |
590 | ||
591 | if (service->description) { | |
592 | _cleanup_free_ char *t = NULL; | |
593 | ||
594 | t = specifier_escape(service->description); | |
595 | if (!t) | |
596 | return log_oom(); | |
597 | ||
598 | fprintf(f, "Description=%s\n", t); | |
599 | } | |
600 | ||
9d9a9500 | 601 | /* Only start after the session is ready. */ |
8feca247 BB |
602 | fprintf(f, |
603 | "After=graphical-session.target\n"); | |
604 | ||
605 | fprintf(f, | |
606 | "\n[Service]\n" | |
67b2edb2 | 607 | "Type=exec\n" |
8feca247 BB |
608 | "ExecStart=:%s\n" |
609 | "Restart=no\n" | |
610 | "TimeoutSec=5s\n" | |
611 | "Slice=app.slice\n", | |
612 | exec_start); | |
613 | ||
d5273f51 BB |
614 | if (service->working_directory) { |
615 | _cleanup_free_ char *e_working_directory = NULL; | |
616 | ||
617 | e_working_directory = cescape(service->working_directory); | |
618 | if (!e_working_directory) | |
619 | return log_oom(); | |
620 | ||
621 | fprintf(f, "WorkingDirectory=-%s\n", e_working_directory); | |
622 | } | |
623 | ||
8feca247 BB |
624 | /* Generate an ExecCondition to check $XDG_CURRENT_DESKTOP */ |
625 | if (!strv_isempty(service->only_show_in) || !strv_isempty(service->not_show_in)) { | |
626 | _cleanup_free_ char *only_show_in = NULL, *not_show_in = NULL, *e_only_show_in = NULL, *e_not_show_in = NULL; | |
627 | ||
628 | only_show_in = strv_join(service->only_show_in, ":"); | |
629 | not_show_in = strv_join(service->not_show_in, ":"); | |
630 | if (!only_show_in || !not_show_in) | |
631 | return log_oom(); | |
632 | ||
633 | e_only_show_in = cescape(only_show_in); | |
634 | e_not_show_in = cescape(not_show_in); | |
635 | if (!e_only_show_in || !e_not_show_in) | |
636 | return log_oom(); | |
637 | ||
638 | /* Just assume the values are reasonably sane */ | |
639 | fprintf(f, | |
640 | "ExecCondition=" ROOTLIBEXECDIR "/systemd-xdg-autostart-condition \"%s\" \"%s\"\n", | |
641 | e_only_show_in, | |
642 | e_not_show_in); | |
643 | } | |
644 | ||
645 | r = xdg_autostart_generate_desktop_condition(f, | |
646 | "gnome-systemd-autostart-condition", | |
647 | service->autostart_condition); | |
648 | if (r < 0) | |
649 | return r; | |
650 | ||
651 | r = xdg_autostart_generate_desktop_condition(f, | |
652 | "kde-systemd-start-condition", | |
653 | service->kde_autostart_condition); | |
654 | if (r < 0) | |
655 | return r; | |
656 | ||
657 | (void) generator_add_symlink(dest, "xdg-desktop-autostart.target", "wants", service->name); | |
658 | ||
659 | return 0; | |
660 | } |