]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/xdg-autostart-generator/xdg-autostart-generator.c
Merge pull request #16112 from poettering/nss-systemd-block-fix
[thirdparty/systemd.git] / src / xdg-autostart-generator / xdg-autostart-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "dirent-util.h"
8 #include "fd-util.h"
9 #include "generator.h"
10 #include "hashmap.h"
11 #include "log.h"
12 #include "main-func.h"
13 #include "nulstr-util.h"
14 #include "path-lookup.h"
15 #include "stat-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "xdg-autostart-service.h"
19
20 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(xdgautostartservice_hash_ops, char, string_hash_func, string_compare_func, XdgAutostartService, xdg_autostart_service_free);
21
22 static int enumerate_xdg_autostart(Hashmap *all_services) {
23 _cleanup_strv_free_ char **autostart_dirs = NULL;
24 _cleanup_strv_free_ char **config_dirs = NULL;
25 _unused_ _cleanup_strv_free_ char **data_dirs = NULL;
26 _cleanup_free_ char *user_config_autostart_dir = NULL;
27 char **path;
28 int r;
29
30 r = xdg_user_config_dir(&user_config_autostart_dir, "/autostart");
31 if (r < 0)
32 return r;
33 r = strv_extend(&autostart_dirs, user_config_autostart_dir);
34 if (r < 0)
35 return r;
36
37 r = xdg_user_dirs(&config_dirs, &data_dirs);
38 if (r < 0)
39 return r;
40 r = strv_extend_strv_concat(&autostart_dirs, config_dirs, "/autostart");
41 if (r < 0)
42 return r;
43
44 STRV_FOREACH(path, autostart_dirs) {
45 _cleanup_closedir_ DIR *d = NULL;
46 struct dirent *de;
47
48 d = opendir(*path);
49 if (!d) {
50 if (errno != ENOENT)
51 log_warning_errno(errno, "Opening %s failed, ignoring: %m", *path);
52 continue;
53 }
54
55 FOREACH_DIRENT(de, d, log_warning_errno(errno, "Failed to enumerate directory %s, ignoring: %m", *path)) {
56 _cleanup_free_ char *fpath = NULL, *name = NULL;
57 _cleanup_(xdg_autostart_service_freep) XdgAutostartService *service = NULL;
58 struct stat st;
59
60 if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
61 log_warning_errno(errno, "stat() failed on %s/%s, ignoring: %m", *path, de->d_name);
62 continue;
63 }
64
65 if (!S_ISREG(st.st_mode))
66 continue;
67
68 name = xdg_autostart_service_translate_name(de->d_name);
69 if (!name)
70 return log_oom();
71
72 if (hashmap_contains(all_services, name))
73 continue;
74
75 fpath = path_join(*path, de->d_name);
76 if (!fpath)
77 return log_oom();
78
79 service = xdg_autostart_service_parse_desktop(fpath);
80 if (!service)
81 return log_oom();
82 service->name = TAKE_PTR(name);
83
84 r = hashmap_put(all_services, service->name, service);
85 if (r < 0)
86 return log_oom();
87 TAKE_PTR(service);
88 }
89 }
90
91 return 0;
92 }
93
94 static int run(const char *dest, const char *dest_early, const char *dest_late) {
95 _cleanup_(hashmap_freep) Hashmap *all_services = NULL;
96 XdgAutostartService *service;
97 Iterator j;
98 int r;
99
100 assert_se(dest_late);
101
102 all_services = hashmap_new(&xdgautostartservice_hash_ops);
103 if (!all_services)
104 return log_oom();
105
106 r = enumerate_xdg_autostart(all_services);
107 if (r < 0)
108 return r;
109
110 HASHMAP_FOREACH(service, all_services, j)
111 (void) xdg_autostart_service_generate_unit(service, dest_late);
112
113 return 0;
114 }
115
116 DEFINE_MAIN_GENERATOR_FUNCTION(run);