]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/path/path.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[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 "log.h"
12 #include "macro.h"
13 #include "main-func.h"
14 #include "pretty-print.h"
15 #include "string-util.h"
16 #include "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
88 static int list_homes(void) {
89 uint64_t i = 0;
90 int r = 0;
91
92 for (i = 0; i < ELEMENTSOF(path_table); i++) {
93 _cleanup_free_ char *p = NULL;
94 int q;
95
96 q = sd_path_lookup(i, arg_suffix, &p);
97 if (q < 0) {
98 log_full_errno(q == -ENXIO ? LOG_DEBUG : LOG_ERR,
99 q, "Failed to query %s: %m", path_table[i]);
100 if (q != -ENXIO)
101 r = q;
102 continue;
103 }
104
105 printf("%s: %s\n", path_table[i], p);
106 }
107
108 return r;
109 }
110
111 static int print_home(const char *n) {
112 uint64_t i = 0;
113 int r;
114
115 for (i = 0; i < ELEMENTSOF(path_table); i++) {
116 if (streq(path_table[i], n)) {
117 _cleanup_free_ char *p = NULL;
118
119 r = sd_path_lookup(i, arg_suffix, &p);
120 if (r < 0)
121 return log_error_errno(r, "Failed to query %s: %m", n);
122
123 printf("%s\n", p);
124 return 0;
125 }
126 }
127
128 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
129 "Path %s not known.", n);
130 }
131
132 static int help(void) {
133 _cleanup_free_ char *link = NULL;
134 int r;
135
136 r = terminal_urlify_man("systemd-path", "1", &link);
137 if (r < 0)
138 return log_oom();
139
140 printf("%s [OPTIONS...] [NAME...]\n\n"
141 "Show system and user paths.\n\n"
142 " -h --help Show this help\n"
143 " --version Show package version\n"
144 " --suffix=SUFFIX Suffix to append to paths\n"
145 "\nSee the %s for details.\n"
146 , program_invocation_short_name
147 , link
148 );
149
150 return 0;
151 }
152
153 static int parse_argv(int argc, char *argv[]) {
154 enum {
155 ARG_VERSION = 0x100,
156 ARG_SUFFIX,
157 };
158
159 static const struct option options[] = {
160 { "help", no_argument, NULL, 'h' },
161 { "version", no_argument, NULL, ARG_VERSION },
162 { "suffix", required_argument, NULL, ARG_SUFFIX },
163 {}
164 };
165
166 int c;
167
168 assert(argc >= 0);
169 assert(argv);
170
171 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
172
173 switch (c) {
174
175 case 'h':
176 return help();
177
178 case ARG_VERSION:
179 return version();
180
181 case ARG_SUFFIX:
182 arg_suffix = optarg;
183 break;
184
185 case '?':
186 return -EINVAL;
187
188 default:
189 assert_not_reached("Unhandled option");
190 }
191
192 return 1;
193 }
194
195 static int run(int argc, char* argv[]) {
196 int r;
197
198 log_show_color(true);
199 log_parse_environment();
200 log_open();
201
202 r = parse_argv(argc, argv);
203 if (r <= 0)
204 return r;
205
206 if (argc > optind) {
207 int i, q;
208
209 for (i = optind; i < argc; i++) {
210 q = print_home(argv[i]);
211 if (q < 0)
212 r = q;
213 }
214
215 return r;
216 } else
217 return list_homes();
218 }
219
220 DEFINE_MAIN_FUNCTION(run);