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