]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/path/path.c
Merge pull request #25168 from valentindavid/valentindavid/umount-move-recursive...
[thirdparty/systemd.git] / src / path / path.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "sd-path.h"
9
10 #include "alloc-util.h"
11 #include "build.h"
12 #include "log.h"
13 #include "macro.h"
14 #include "main-func.h"
15 #include "pretty-print.h"
16 #include "string-util.h"
17
18 static const char *arg_suffix = NULL;
19
20 static const char* const path_table[_SD_PATH_MAX] = {
21 [SD_PATH_TEMPORARY] = "temporary",
22 [SD_PATH_TEMPORARY_LARGE] = "temporary-large",
23 [SD_PATH_SYSTEM_BINARIES] = "system-binaries",
24 [SD_PATH_SYSTEM_INCLUDE] = "system-include",
25 [SD_PATH_SYSTEM_LIBRARY_PRIVATE] = "system-library-private",
26 [SD_PATH_SYSTEM_LIBRARY_ARCH] = "system-library-arch",
27 [SD_PATH_SYSTEM_SHARED] = "system-shared",
28 [SD_PATH_SYSTEM_CONFIGURATION_FACTORY] = "system-configuration-factory",
29 [SD_PATH_SYSTEM_STATE_FACTORY] = "system-state-factory",
30 [SD_PATH_SYSTEM_CONFIGURATION] = "system-configuration",
31 [SD_PATH_SYSTEM_RUNTIME] = "system-runtime",
32 [SD_PATH_SYSTEM_RUNTIME_LOGS] = "system-runtime-logs",
33 [SD_PATH_SYSTEM_STATE_PRIVATE] = "system-state-private",
34 [SD_PATH_SYSTEM_STATE_LOGS] = "system-state-logs",
35 [SD_PATH_SYSTEM_STATE_CACHE] = "system-state-cache",
36 [SD_PATH_SYSTEM_STATE_SPOOL] = "system-state-spool",
37 [SD_PATH_USER_BINARIES] = "user-binaries",
38 [SD_PATH_USER_LIBRARY_PRIVATE] = "user-library-private",
39 [SD_PATH_USER_LIBRARY_ARCH] = "user-library-arch",
40 [SD_PATH_USER_SHARED] = "user-shared",
41 [SD_PATH_USER_CONFIGURATION] = "user-configuration",
42 [SD_PATH_USER_RUNTIME] = "user-runtime",
43 [SD_PATH_USER_STATE_CACHE] = "user-state-cache",
44 [SD_PATH_USER] = "user",
45 [SD_PATH_USER_DOCUMENTS] = "user-documents",
46 [SD_PATH_USER_MUSIC] = "user-music",
47 [SD_PATH_USER_PICTURES] = "user-pictures",
48 [SD_PATH_USER_VIDEOS] = "user-videos",
49 [SD_PATH_USER_DOWNLOAD] = "user-download",
50 [SD_PATH_USER_PUBLIC] = "user-public",
51 [SD_PATH_USER_TEMPLATES] = "user-templates",
52 [SD_PATH_USER_DESKTOP] = "user-desktop",
53 [SD_PATH_SEARCH_BINARIES] = "search-binaries",
54 [SD_PATH_SEARCH_BINARIES_DEFAULT] = "search-binaries-default",
55 [SD_PATH_SEARCH_LIBRARY_PRIVATE] = "search-library-private",
56 [SD_PATH_SEARCH_LIBRARY_ARCH] = "search-library-arch",
57 [SD_PATH_SEARCH_SHARED] = "search-shared",
58 [SD_PATH_SEARCH_CONFIGURATION_FACTORY] = "search-configuration-factory",
59 [SD_PATH_SEARCH_STATE_FACTORY] = "search-state-factory",
60 [SD_PATH_SEARCH_CONFIGURATION] = "search-configuration",
61
62 [SD_PATH_SYSTEMD_UTIL] = "systemd-util",
63 [SD_PATH_SYSTEMD_SYSTEM_UNIT] = "systemd-system-unit",
64 [SD_PATH_SYSTEMD_SYSTEM_PRESET] = "systemd-system-preset",
65 [SD_PATH_SYSTEMD_SYSTEM_CONF] = "systemd-system-conf",
66 [SD_PATH_SYSTEMD_SEARCH_SYSTEM_UNIT] = "systemd-search-system-unit",
67 [SD_PATH_SYSTEMD_SYSTEM_GENERATOR] = "systemd-system-generator",
68 [SD_PATH_SYSTEMD_SEARCH_SYSTEM_GENERATOR] = "systemd-search-system-generator",
69 [SD_PATH_SYSTEMD_USER_UNIT] = "systemd-user-unit",
70 [SD_PATH_SYSTEMD_USER_PRESET] = "systemd-user-preset",
71 [SD_PATH_SYSTEMD_USER_CONF] = "systemd-user-conf",
72 [SD_PATH_SYSTEMD_SEARCH_USER_UNIT] = "systemd-search-user-unit",
73 [SD_PATH_SYSTEMD_SEARCH_USER_GENERATOR] = "systemd-search-user-generator",
74 [SD_PATH_SYSTEMD_USER_GENERATOR] = "systemd-user-generator",
75 [SD_PATH_SYSTEMD_SLEEP] = "systemd-sleep",
76 [SD_PATH_SYSTEMD_SHUTDOWN] = "systemd-shutdown",
77
78 [SD_PATH_TMPFILES] = "tmpfiles",
79 [SD_PATH_SYSUSERS] = "sysusers",
80 [SD_PATH_SYSCTL] = "sysctl",
81 [SD_PATH_BINFMT] = "binfmt",
82 [SD_PATH_MODULES_LOAD] = "modules-load",
83 [SD_PATH_CATALOG] = "catalog",
84
85 [SD_PATH_SYSTEMD_SEARCH_NETWORK] = "systemd-search-network",
86
87 [SD_PATH_SYSTEMD_SYSTEM_ENVIRONMENT_GENERATOR] = "systemd-system-environment-generator",
88 [SD_PATH_SYSTEMD_USER_ENVIRONMENT_GENERATOR] = "systemd-user-environment-generator",
89 [SD_PATH_SYSTEMD_SEARCH_SYSTEM_ENVIRONMENT_GENERATOR] = "systemd-search-system-environment-generator",
90 [SD_PATH_SYSTEMD_SEARCH_USER_ENVIRONMENT_GENERATOR] = "systemd-search-user-environment-generator",
91 };
92
93 static int list_homes(void) {
94 uint64_t i = 0;
95 int r = 0;
96
97 for (i = 0; i < ELEMENTSOF(path_table); i++) {
98 _cleanup_free_ char *p = NULL;
99 int q;
100
101 q = sd_path_lookup(i, arg_suffix, &p);
102 if (q < 0) {
103 log_full_errno(q == -ENXIO ? LOG_DEBUG : LOG_ERR,
104 q, "Failed to query %s: %m", path_table[i]);
105 if (q != -ENXIO)
106 r = q;
107 continue;
108 }
109
110 printf("%s: %s\n", path_table[i], p);
111 }
112
113 return r;
114 }
115
116 static int print_home(const char *n) {
117 uint64_t i = 0;
118 int r;
119
120 for (i = 0; i < ELEMENTSOF(path_table); i++) {
121 if (streq(path_table[i], n)) {
122 _cleanup_free_ char *p = NULL;
123
124 r = sd_path_lookup(i, arg_suffix, &p);
125 if (r < 0)
126 return log_error_errno(r, "Failed to query %s: %m", n);
127
128 printf("%s\n", p);
129 return 0;
130 }
131 }
132
133 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
134 "Path %s not known.", n);
135 }
136
137 static int help(void) {
138 _cleanup_free_ char *link = NULL;
139 int r;
140
141 r = terminal_urlify_man("systemd-path", "1", &link);
142 if (r < 0)
143 return log_oom();
144
145 printf("%s [OPTIONS...] [NAME...]\n\n"
146 "Show system and user paths.\n\n"
147 " -h --help Show this help\n"
148 " --version Show package version\n"
149 " --suffix=SUFFIX Suffix to append to paths\n"
150 "\nSee the %s for details.\n",
151 program_invocation_short_name,
152 link);
153
154 return 0;
155 }
156
157 static int parse_argv(int argc, char *argv[]) {
158 enum {
159 ARG_VERSION = 0x100,
160 ARG_SUFFIX,
161 };
162
163 static const struct option options[] = {
164 { "help", no_argument, NULL, 'h' },
165 { "version", no_argument, NULL, ARG_VERSION },
166 { "suffix", required_argument, NULL, ARG_SUFFIX },
167 {}
168 };
169
170 int c;
171
172 assert(argc >= 0);
173 assert(argv);
174
175 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
176
177 switch (c) {
178
179 case 'h':
180 return help();
181
182 case ARG_VERSION:
183 return version();
184
185 case ARG_SUFFIX:
186 arg_suffix = optarg;
187 break;
188
189 case '?':
190 return -EINVAL;
191
192 default:
193 assert_not_reached();
194 }
195
196 return 1;
197 }
198
199 static int run(int argc, char* argv[]) {
200 int r;
201
202 log_show_color(true);
203 log_parse_environment();
204 log_open();
205
206 r = parse_argv(argc, argv);
207 if (r <= 0)
208 return r;
209
210 if (argc > optind) {
211 int i, q;
212
213 for (i = optind; i < argc; i++) {
214 q = print_home(argv[i]);
215 if (q < 0)
216 r = q;
217 }
218
219 return r;
220 } else
221 return list_homes();
222 }
223
224 DEFINE_MAIN_FUNCTION(run);