]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9eb977db | 2 | |
9eb977db | 3 | #include <errno.h> |
11c3a366 | 4 | #include <limits.h> |
07630cea LP |
5 | #include <stdio.h> |
6 | #include <stdlib.h> | |
7 | #include <string.h> | |
11c3a366 | 8 | #include <sys/stat.h> |
07630cea | 9 | #include <unistd.h> |
9eb977db | 10 | |
5f311f8c LP |
11 | /* When we include libgen.h because we need dirname() we immediately |
12 | * undefine basename() since libgen.h defines it as a macro to the | |
13 | * POSIX version which is really broken. We prefer GNU basename(). */ | |
14 | #include <libgen.h> | |
15 | #undef basename | |
16 | ||
b5efdb8a | 17 | #include "alloc-util.h" |
93cc7779 | 18 | #include "extract-word.h" |
f4f15635 | 19 | #include "fs-util.h" |
5a46d55f | 20 | #include "glob-util.h" |
9eb977db | 21 | #include "log.h" |
07630cea LP |
22 | #include "macro.h" |
23 | #include "missing.h" | |
5a46d55f | 24 | #include "parse-util.h" |
3ffd4af2 | 25 | #include "path-util.h" |
8fcde012 | 26 | #include "stat-util.h" |
07630cea | 27 | #include "string-util.h" |
9eb977db | 28 | #include "strv.h" |
93cc7779 | 29 | #include "time-util.h" |
58a53add | 30 | #include "utf8.h" |
9eb977db KS |
31 | |
32 | bool path_is_absolute(const char *p) { | |
33 | return p[0] == '/'; | |
34 | } | |
35 | ||
36 | bool is_path(const char *p) { | |
37 | return !!strchr(p, '/'); | |
38 | } | |
39 | ||
0f474365 | 40 | int path_split_and_make_absolute(const char *p, char ***ret) { |
9eb977db | 41 | char **l; |
0f474365 LP |
42 | int r; |
43 | ||
9eb977db | 44 | assert(p); |
0f474365 | 45 | assert(ret); |
9eb977db | 46 | |
116cc028 ZJS |
47 | l = strv_split(p, ":"); |
48 | if (!l) | |
ce9d6bcf | 49 | return -ENOMEM; |
9eb977db | 50 | |
0f474365 LP |
51 | r = path_strv_make_absolute_cwd(l); |
52 | if (r < 0) { | |
9eb977db | 53 | strv_free(l); |
0f474365 | 54 | return r; |
9eb977db KS |
55 | } |
56 | ||
0f474365 LP |
57 | *ret = l; |
58 | return r; | |
9eb977db KS |
59 | } |
60 | ||
61 | char *path_make_absolute(const char *p, const char *prefix) { | |
62 | assert(p); | |
63 | ||
64 | /* Makes every item in the list an absolute path by prepending | |
65 | * the prefix, if specified and necessary */ | |
66 | ||
81cce8de | 67 | if (path_is_absolute(p) || isempty(prefix)) |
9eb977db KS |
68 | return strdup(p); |
69 | ||
cddd2ce1 LP |
70 | if (endswith(prefix, "/")) |
71 | return strjoin(prefix, p); | |
72 | else | |
73 | return strjoin(prefix, "/", p); | |
9eb977db KS |
74 | } |
75 | ||
a2556d25 LP |
76 | int safe_getcwd(char **ret) { |
77 | char *cwd; | |
78 | ||
79 | cwd = get_current_dir_name(); | |
80 | if (!cwd) | |
81 | return negative_errno(); | |
82 | ||
83 | /* Let's make sure the directory is really absolute, to protect us from the logic behind | |
84 | * CVE-2018-1000001 */ | |
85 | if (cwd[0] != '/') { | |
86 | free(cwd); | |
87 | return -ENOMEDIUM; | |
88 | } | |
89 | ||
90 | *ret = cwd; | |
91 | return 0; | |
92 | } | |
93 | ||
0f474365 LP |
94 | int path_make_absolute_cwd(const char *p, char **ret) { |
95 | char *c; | |
d7249575 | 96 | int r; |
9eb977db KS |
97 | |
98 | assert(p); | |
0f474365 | 99 | assert(ret); |
9eb977db KS |
100 | |
101 | /* Similar to path_make_absolute(), but prefixes with the | |
102 | * current working directory. */ | |
103 | ||
104 | if (path_is_absolute(p)) | |
0f474365 LP |
105 | c = strdup(p); |
106 | else { | |
107 | _cleanup_free_ char *cwd = NULL; | |
9eb977db | 108 | |
d7249575 LP |
109 | r = safe_getcwd(&cwd); |
110 | if (r < 0) | |
111 | return r; | |
0f474365 | 112 | |
7aeeb313 LP |
113 | if (endswith(cwd, "/")) |
114 | c = strjoin(cwd, p); | |
115 | else | |
116 | c = strjoin(cwd, "/", p); | |
0f474365 LP |
117 | } |
118 | if (!c) | |
119 | return -ENOMEM; | |
9eb977db | 120 | |
0f474365 LP |
121 | *ret = c; |
122 | return 0; | |
9eb977db KS |
123 | } |
124 | ||
7cb9c51c | 125 | int path_make_relative(const char *from_dir, const char *to_path, char **_r) { |
48054262 YW |
126 | char *f, *t, *r, *p; |
127 | unsigned n_parents = 0; | |
7cb9c51c TK |
128 | |
129 | assert(from_dir); | |
130 | assert(to_path); | |
131 | assert(_r); | |
132 | ||
133 | /* Strips the common part, and adds ".." elements as necessary. */ | |
134 | ||
48054262 | 135 | if (!path_is_absolute(from_dir) || !path_is_absolute(to_path)) |
7cb9c51c TK |
136 | return -EINVAL; |
137 | ||
48054262 YW |
138 | f = strdupa(from_dir); |
139 | t = strdupa(to_path); | |
140 | ||
141 | path_simplify(f, true); | |
142 | path_simplify(t, true); | |
7cb9c51c TK |
143 | |
144 | /* Skip the common part. */ | |
145 | for (;;) { | |
2a5beb66 | 146 | size_t a, b; |
7cb9c51c | 147 | |
48054262 YW |
148 | f += *f == '/'; |
149 | t += *t == '/'; | |
7cb9c51c | 150 | |
48054262 YW |
151 | if (!*f) { |
152 | if (!*t) | |
7cb9c51c TK |
153 | /* from_dir equals to_path. */ |
154 | r = strdup("."); | |
155 | else | |
156 | /* from_dir is a parent directory of to_path. */ | |
48054262 | 157 | r = strdup(t); |
7cb9c51c TK |
158 | if (!r) |
159 | return -ENOMEM; | |
160 | ||
161 | *_r = r; | |
162 | return 0; | |
163 | } | |
164 | ||
48054262 | 165 | if (!*t) |
7cb9c51c TK |
166 | break; |
167 | ||
48054262 YW |
168 | a = strcspn(f, "/"); |
169 | b = strcspn(t, "/"); | |
7cb9c51c | 170 | |
48054262 | 171 | if (a != b || memcmp(f, t, a) != 0) |
7cb9c51c TK |
172 | break; |
173 | ||
48054262 YW |
174 | f += a; |
175 | t += b; | |
7cb9c51c TK |
176 | } |
177 | ||
178 | /* If we're here, then "from_dir" has one or more elements that need to | |
179 | * be replaced with "..". */ | |
180 | ||
181 | /* Count the number of necessary ".." elements. */ | |
48054262 | 182 | for (; *f;) { |
2a5beb66 LP |
183 | size_t w; |
184 | ||
48054262 | 185 | w = strcspn(f, "/"); |
2a5beb66 LP |
186 | |
187 | /* If this includes ".." we can't do a simple series of "..", refuse */ | |
48054262 | 188 | if (w == 2 && f[0] == '.' && f[1] == '.') |
2a5beb66 LP |
189 | return -EINVAL; |
190 | ||
48054262 YW |
191 | /* Count number of elements */ |
192 | n_parents++; | |
2a5beb66 | 193 | |
48054262 YW |
194 | f += w; |
195 | f += *f == '/'; | |
7cb9c51c TK |
196 | } |
197 | ||
48054262 | 198 | r = new(char, n_parents * 3 + strlen(t) + 1); |
7cb9c51c TK |
199 | if (!r) |
200 | return -ENOMEM; | |
201 | ||
2a5beb66 LP |
202 | for (p = r; n_parents > 0; n_parents--) |
203 | p = mempcpy(p, "../", 3); | |
7cb9c51c | 204 | |
48054262 YW |
205 | if (*t) |
206 | strcpy(p, t); | |
207 | else | |
208 | /* Remove trailing slash */ | |
209 | *(--p) = 0; | |
7cb9c51c TK |
210 | |
211 | *_r = r; | |
212 | return 0; | |
213 | } | |
214 | ||
0f474365 | 215 | int path_strv_make_absolute_cwd(char **l) { |
9eb977db | 216 | char **s; |
0f474365 | 217 | int r; |
9eb977db KS |
218 | |
219 | /* Goes through every item in the string list and makes it | |
220 | * absolute. This works in place and won't rollback any | |
221 | * changes on failure. */ | |
222 | ||
223 | STRV_FOREACH(s, l) { | |
224 | char *t; | |
225 | ||
0f474365 LP |
226 | r = path_make_absolute_cwd(*s, &t); |
227 | if (r < 0) | |
228 | return r; | |
9eb977db | 229 | |
858d36c1 | 230 | path_simplify(t, false); |
32a8f700 | 231 | free_and_replace(*s, t); |
9eb977db KS |
232 | } |
233 | ||
0f474365 | 234 | return 0; |
9eb977db KS |
235 | } |
236 | ||
e1873695 | 237 | char **path_strv_resolve(char **l, const char *root) { |
9eb977db KS |
238 | char **s; |
239 | unsigned k = 0; | |
240 | bool enomem = false; | |
e1873695 | 241 | int r; |
9eb977db KS |
242 | |
243 | if (strv_isempty(l)) | |
244 | return l; | |
245 | ||
246 | /* Goes through every item in the string list and canonicalize | |
247 | * the path. This works in place and won't rollback any | |
248 | * changes on failure. */ | |
249 | ||
250 | STRV_FOREACH(s, l) { | |
12ed81d9 | 251 | _cleanup_free_ char *orig = NULL; |
e1873695 | 252 | char *t, *u; |
9eb977db | 253 | |
12ed81d9 ZJS |
254 | if (!path_is_absolute(*s)) { |
255 | free(*s); | |
9eb977db | 256 | continue; |
12ed81d9 | 257 | } |
112cfb18 | 258 | |
e1873695 | 259 | if (root) { |
12ed81d9 | 260 | orig = *s; |
e1873695 | 261 | t = prefix_root(root, orig); |
112cfb18 MM |
262 | if (!t) { |
263 | enomem = true; | |
264 | continue; | |
265 | } | |
12ed81d9 | 266 | } else |
112cfb18 | 267 | t = *s; |
9eb977db | 268 | |
c4f4fce7 | 269 | r = chase_symlinks(t, root, 0, &u); |
e1873695 LP |
270 | if (r == -ENOENT) { |
271 | if (root) { | |
ae2a15bc | 272 | u = TAKE_PTR(orig); |
874310b7 | 273 | free(t); |
e1873695 LP |
274 | } else |
275 | u = t; | |
276 | } else if (r < 0) { | |
277 | free(t); | |
874310b7 | 278 | |
e1873695 LP |
279 | if (r == -ENOMEM) |
280 | enomem = true; | |
281 | ||
282 | continue; | |
283 | } else if (root) { | |
12ed81d9 ZJS |
284 | char *x; |
285 | ||
286 | free(t); | |
e1873695 | 287 | x = path_startswith(u, root); |
12ed81d9 ZJS |
288 | if (x) { |
289 | /* restore the slash if it was lost */ | |
290 | if (!startswith(x, "/")) | |
291 | *(--x) = '/'; | |
292 | ||
293 | t = strdup(x); | |
294 | free(u); | |
295 | if (!t) { | |
296 | enomem = true; | |
297 | continue; | |
298 | } | |
299 | u = t; | |
300 | } else { | |
301 | /* canonicalized path goes outside of | |
302 | * prefix, keep the original path instead */ | |
3b319885 | 303 | free_and_replace(u, orig); |
12ed81d9 | 304 | } |
91a6489d LP |
305 | } else |
306 | free(t); | |
9eb977db KS |
307 | |
308 | l[k++] = u; | |
309 | } | |
310 | ||
311 | l[k] = NULL; | |
312 | ||
313 | if (enomem) | |
314 | return NULL; | |
315 | ||
316 | return l; | |
317 | } | |
318 | ||
e1873695 | 319 | char **path_strv_resolve_uniq(char **l, const char *root) { |
112cfb18 | 320 | |
fabe5c0e LP |
321 | if (strv_isempty(l)) |
322 | return l; | |
323 | ||
e1873695 | 324 | if (!path_strv_resolve(l, root)) |
fabe5c0e LP |
325 | return NULL; |
326 | ||
327 | return strv_uniq(l); | |
328 | } | |
329 | ||
858d36c1 | 330 | char *path_simplify(char *path, bool kill_dots) { |
9eb977db | 331 | char *f, *t; |
858d36c1 | 332 | bool slash = false, ignore_slash = false, absolute; |
9eb977db | 333 | |
858d36c1 YW |
334 | assert(path); |
335 | ||
336 | /* Removes redundant inner and trailing slashes. Also removes unnecessary dots | |
337 | * if kill_dots is true. Modifies the passed string in-place. | |
9eb977db | 338 | * |
858d36c1 YW |
339 | * ///foo//./bar/. becomes /foo/./bar/. (if kill_dots is false) |
340 | * ///foo//./bar/. becomes /foo/bar (if kill_dots is true) | |
341 | * .//./foo//./bar/. becomes ./foo/bar (if kill_dots is false) | |
342 | * .//./foo//./bar/. becomes foo/bar (if kill_dots is true) | |
9eb977db KS |
343 | */ |
344 | ||
858d36c1 YW |
345 | absolute = path_is_absolute(path); |
346 | ||
347 | f = path; | |
348 | if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/')) { | |
349 | ignore_slash = true; | |
350 | f++; | |
351 | } | |
352 | ||
353 | for (t = path; *f; f++) { | |
9eb977db KS |
354 | |
355 | if (*f == '/') { | |
356 | slash = true; | |
357 | continue; | |
358 | } | |
359 | ||
360 | if (slash) { | |
858d36c1 YW |
361 | if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/')) |
362 | continue; | |
363 | ||
9eb977db | 364 | slash = false; |
858d36c1 YW |
365 | if (ignore_slash) |
366 | ignore_slash = false; | |
367 | else | |
368 | *(t++) = '/'; | |
9eb977db KS |
369 | } |
370 | ||
371 | *(t++) = *f; | |
372 | } | |
373 | ||
858d36c1 YW |
374 | /* Special rule, if we are talking of the root directory, a trailing slash is good */ |
375 | if (absolute && t == path) | |
9eb977db KS |
376 | *(t++) = '/'; |
377 | ||
378 | *t = 0; | |
379 | return path; | |
380 | } | |
381 | ||
424a19f8 | 382 | char* path_startswith(const char *path, const char *prefix) { |
9eb977db KS |
383 | assert(path); |
384 | assert(prefix); | |
385 | ||
0470289b ZJS |
386 | /* Returns a pointer to the start of the first component after the parts matched by |
387 | * the prefix, iff | |
388 | * - both paths are absolute or both paths are relative, | |
389 | * and | |
390 | * - each component in prefix in turn matches a component in path at the same position. | |
391 | * An empty string will be returned when the prefix and path are equivalent. | |
392 | * | |
393 | * Returns NULL otherwise. | |
394 | */ | |
395 | ||
9eb977db | 396 | if ((path[0] == '/') != (prefix[0] == '/')) |
424a19f8 | 397 | return NULL; |
9eb977db KS |
398 | |
399 | for (;;) { | |
400 | size_t a, b; | |
401 | ||
402 | path += strspn(path, "/"); | |
403 | prefix += strspn(prefix, "/"); | |
404 | ||
405 | if (*prefix == 0) | |
424a19f8 | 406 | return (char*) path; |
9eb977db KS |
407 | |
408 | if (*path == 0) | |
424a19f8 | 409 | return NULL; |
9eb977db KS |
410 | |
411 | a = strcspn(path, "/"); | |
412 | b = strcspn(prefix, "/"); | |
413 | ||
414 | if (a != b) | |
424a19f8 | 415 | return NULL; |
9eb977db KS |
416 | |
417 | if (memcmp(path, prefix, a) != 0) | |
424a19f8 | 418 | return NULL; |
9eb977db KS |
419 | |
420 | path += a; | |
421 | prefix += b; | |
422 | } | |
423 | } | |
424 | ||
2230852b MS |
425 | int path_compare(const char *a, const char *b) { |
426 | int d; | |
427 | ||
9eb977db KS |
428 | assert(a); |
429 | assert(b); | |
430 | ||
2230852b MS |
431 | /* A relative path and an abolute path must not compare as equal. |
432 | * Which one is sorted before the other does not really matter. | |
433 | * Here a relative path is ordered before an absolute path. */ | |
434 | d = (a[0] == '/') - (b[0] == '/'); | |
373cd63a | 435 | if (d != 0) |
2230852b | 436 | return d; |
9eb977db KS |
437 | |
438 | for (;;) { | |
439 | size_t j, k; | |
440 | ||
441 | a += strspn(a, "/"); | |
442 | b += strspn(b, "/"); | |
443 | ||
444 | if (*a == 0 && *b == 0) | |
2230852b | 445 | return 0; |
9eb977db | 446 | |
2230852b MS |
447 | /* Order prefixes first: "/foo" before "/foo/bar" */ |
448 | if (*a == 0) | |
449 | return -1; | |
450 | if (*b == 0) | |
451 | return 1; | |
9eb977db KS |
452 | |
453 | j = strcspn(a, "/"); | |
454 | k = strcspn(b, "/"); | |
455 | ||
2230852b MS |
456 | /* Alphabetical sort: "/foo/aaa" before "/foo/b" */ |
457 | d = memcmp(a, b, MIN(j, k)); | |
373cd63a | 458 | if (d != 0) |
2230852b | 459 | return (d > 0) - (d < 0); /* sign of d */ |
9eb977db | 460 | |
2230852b MS |
461 | /* Sort "/foo/a" before "/foo/aaa" */ |
462 | d = (j > k) - (j < k); /* sign of (j - k) */ | |
373cd63a | 463 | if (d != 0) |
2230852b | 464 | return d; |
9eb977db KS |
465 | |
466 | a += j; | |
467 | b += k; | |
468 | } | |
469 | } | |
470 | ||
2230852b MS |
471 | bool path_equal(const char *a, const char *b) { |
472 | return path_compare(a, b) == 0; | |
473 | } | |
474 | ||
e3f791a2 ZJS |
475 | bool path_equal_or_files_same(const char *a, const char *b, int flags) { |
476 | return path_equal(a, b) || files_same(a, b, flags) > 0; | |
c78e47a6 MS |
477 | } |
478 | ||
0c6ea3a4 ZJS |
479 | char* path_join(const char *root, const char *path, const char *rest) { |
480 | assert(path); | |
481 | ||
482 | if (!isempty(root)) | |
bc854dc7 | 483 | return strjoin(root, endswith(root, "/") ? "" : "/", |
0c6ea3a4 | 484 | path[0] == '/' ? path+1 : path, |
bc854dc7 | 485 | rest ? (endswith(path, "/") ? "" : "/") : NULL, |
605405c6 | 486 | rest && rest[0] == '/' ? rest+1 : rest); |
0c6ea3a4 ZJS |
487 | else |
488 | return strjoin(path, | |
bc854dc7 | 489 | rest ? (endswith(path, "/") ? "" : "/") : NULL, |
605405c6 | 490 | rest && rest[0] == '/' ? rest+1 : rest); |
0c6ea3a4 ZJS |
491 | } |
492 | ||
85eca92e LP |
493 | int find_binary(const char *name, char **ret) { |
494 | int last_error, r; | |
495 | const char *p; | |
496 | ||
c9d954b2 | 497 | assert(name); |
4087cb9e | 498 | |
571d0134 | 499 | if (is_path(name)) { |
85eca92e | 500 | if (access(name, X_OK) < 0) |
b972115c ZJS |
501 | return -errno; |
502 | ||
85eca92e | 503 | if (ret) { |
0f474365 LP |
504 | r = path_make_absolute_cwd(name, ret); |
505 | if (r < 0) | |
506 | return r; | |
eb66db55 | 507 | } |
c9d954b2 | 508 | |
c9d954b2 | 509 | return 0; |
85eca92e | 510 | } |
c9d954b2 | 511 | |
85eca92e LP |
512 | /** |
513 | * Plain getenv, not secure_getenv, because we want | |
514 | * to actually allow the user to pick the binary. | |
515 | */ | |
516 | p = getenv("PATH"); | |
517 | if (!p) | |
518 | p = DEFAULT_PATH; | |
519 | ||
520 | last_error = -ENOENT; | |
521 | ||
522 | for (;;) { | |
523 | _cleanup_free_ char *j = NULL, *element = NULL; | |
524 | ||
525 | r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS); | |
526 | if (r < 0) | |
527 | return r; | |
528 | if (r == 0) | |
529 | break; | |
530 | ||
0f474365 LP |
531 | if (!path_is_absolute(element)) |
532 | continue; | |
533 | ||
605405c6 | 534 | j = strjoin(element, "/", name); |
85eca92e LP |
535 | if (!j) |
536 | return -ENOMEM; | |
537 | ||
538 | if (access(j, X_OK) >= 0) { | |
539 | /* Found it! */ | |
c9d954b2 | 540 | |
85eca92e | 541 | if (ret) { |
858d36c1 | 542 | *ret = path_simplify(j, false); |
85eca92e | 543 | j = NULL; |
eb66db55 | 544 | } |
c9d954b2 ZJS |
545 | |
546 | return 0; | |
547 | } | |
548 | ||
85eca92e | 549 | last_error = -errno; |
c9d954b2 | 550 | } |
85eca92e LP |
551 | |
552 | return last_error; | |
c9d954b2 | 553 | } |
8e184852 | 554 | |
2ad8416d | 555 | bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) { |
8e184852 | 556 | bool changed = false; |
2ad8416d | 557 | const char* const* i; |
8e184852 | 558 | |
97f2d76d TG |
559 | assert(timestamp); |
560 | ||
234519ae | 561 | if (!paths) |
4087cb9e | 562 | return false; |
8e184852 | 563 | |
4087cb9e | 564 | STRV_FOREACH(i, paths) { |
8e184852 | 565 | struct stat stats; |
4087cb9e | 566 | usec_t u; |
8e184852 | 567 | |
4087cb9e | 568 | if (stat(*i, &stats) < 0) |
8e184852 TG |
569 | continue; |
570 | ||
4087cb9e LP |
571 | u = timespec_load(&stats.st_mtim); |
572 | ||
97f2d76d | 573 | /* first check */ |
4087cb9e | 574 | if (*timestamp >= u) |
8e184852 TG |
575 | continue; |
576 | ||
9f6445e3 | 577 | log_debug("timestamp of '%s' changed", *i); |
8e184852 TG |
578 | |
579 | /* update timestamp */ | |
4087cb9e LP |
580 | if (update) { |
581 | *timestamp = u; | |
582 | changed = true; | |
583 | } else | |
584 | return true; | |
8e184852 | 585 | } |
4087cb9e | 586 | |
8e184852 TG |
587 | return changed; |
588 | } | |
eb66db55 | 589 | |
5bcd08db | 590 | static int binary_is_good(const char *binary) { |
571d0134 | 591 | _cleanup_free_ char *p = NULL, *d = NULL; |
571d0134 | 592 | int r; |
eb66db55 | 593 | |
85eca92e LP |
594 | r = find_binary(binary, &p); |
595 | if (r == -ENOENT) | |
596 | return 0; | |
571d0134 LP |
597 | if (r < 0) |
598 | return r; | |
599 | ||
061df014 | 600 | /* An fsck that is linked to /bin/true is a non-existent |
571d0134 LP |
601 | * fsck */ |
602 | ||
603 | r = readlink_malloc(p, &d); | |
85eca92e LP |
604 | if (r == -EINVAL) /* not a symlink */ |
605 | return 1; | |
606 | if (r < 0) | |
607 | return r; | |
571d0134 | 608 | |
3ae5990c ZJS |
609 | return !PATH_IN_SET(d, "true" |
610 | "/bin/true", | |
611 | "/usr/bin/true", | |
612 | "/dev/null"); | |
eb66db55 | 613 | } |
1d13f648 | 614 | |
5bcd08db LP |
615 | int fsck_exists(const char *fstype) { |
616 | const char *checker; | |
617 | ||
85eca92e | 618 | assert(fstype); |
5bcd08db | 619 | |
85eca92e LP |
620 | if (streq(fstype, "auto")) |
621 | return -EINVAL; | |
622 | ||
623 | checker = strjoina("fsck.", fstype); | |
5bcd08db LP |
624 | return binary_is_good(checker); |
625 | } | |
626 | ||
627 | int mkfs_exists(const char *fstype) { | |
628 | const char *mkfs; | |
629 | ||
85eca92e | 630 | assert(fstype); |
5bcd08db | 631 | |
85eca92e LP |
632 | if (streq(fstype, "auto")) |
633 | return -EINVAL; | |
634 | ||
635 | mkfs = strjoina("mkfs.", fstype); | |
5bcd08db LP |
636 | return binary_is_good(mkfs); |
637 | } | |
638 | ||
1d13f648 LP |
639 | char *prefix_root(const char *root, const char *path) { |
640 | char *n, *p; | |
641 | size_t l; | |
642 | ||
643 | /* If root is passed, prefixes path with it. Otherwise returns | |
644 | * it as is. */ | |
645 | ||
646 | assert(path); | |
647 | ||
648 | /* First, drop duplicate prefixing slashes from the path */ | |
649 | while (path[0] == '/' && path[1] == '/') | |
650 | path++; | |
651 | ||
57ea45e1 | 652 | if (empty_or_root(root)) |
1d13f648 LP |
653 | return strdup(path); |
654 | ||
655 | l = strlen(root) + 1 + strlen(path) + 1; | |
656 | ||
657 | n = new(char, l); | |
658 | if (!n) | |
659 | return NULL; | |
660 | ||
661 | p = stpcpy(n, root); | |
662 | ||
663 | while (p > n && p[-1] == '/') | |
664 | p--; | |
665 | ||
666 | if (path[0] != '/') | |
667 | *(p++) = '/'; | |
668 | ||
669 | strcpy(p, path); | |
670 | return n; | |
671 | } | |
0f03c2a4 LP |
672 | |
673 | int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) { | |
674 | char *p; | |
675 | int r; | |
676 | ||
677 | /* | |
678 | * This function is intended to be used in command line | |
679 | * parsers, to handle paths that are passed in. It makes the | |
680 | * path absolute, and reduces it to NULL if omitted or | |
681 | * root (the latter optionally). | |
682 | * | |
683 | * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON | |
684 | * SUCCESS! Hence, do not pass in uninitialized pointers. | |
685 | */ | |
686 | ||
687 | if (isempty(path)) { | |
688 | *arg = mfree(*arg); | |
689 | return 0; | |
690 | } | |
691 | ||
692 | r = path_make_absolute_cwd(path, &p); | |
693 | if (r < 0) | |
694 | return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path); | |
695 | ||
858d36c1 | 696 | path_simplify(p, false); |
53e87b5a | 697 | if (suppress_root && empty_or_root(p)) |
0f03c2a4 LP |
698 | p = mfree(p); |
699 | ||
858d36c1 | 700 | free_and_replace(*arg, p); |
53e87b5a | 701 | |
0f03c2a4 LP |
702 | return 0; |
703 | } | |
5f311f8c LP |
704 | |
705 | char* dirname_malloc(const char *path) { | |
706 | char *d, *dir, *dir2; | |
707 | ||
708 | assert(path); | |
709 | ||
710 | d = strdup(path); | |
711 | if (!d) | |
712 | return NULL; | |
713 | ||
714 | dir = dirname(d); | |
715 | assert(dir); | |
716 | ||
717 | if (dir == d) | |
718 | return d; | |
719 | ||
720 | dir2 = strdup(dir); | |
721 | free(d); | |
722 | ||
723 | return dir2; | |
724 | } | |
bb15fafe | 725 | |
b12d25a8 | 726 | const char *last_path_component(const char *path) { |
8460289f LP |
727 | |
728 | /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory. | |
729 | * | |
b12d25a8 ZJS |
730 | * a/b/c → c |
731 | * a/b/c/ → c/ | |
8460289f LP |
732 | * x → x |
733 | * x/ → x/ | |
734 | * /y → y | |
735 | * /y/ → y/ | |
b12d25a8 ZJS |
736 | * / → / |
737 | * // → / | |
738 | * /foo/a → a | |
739 | * /foo/a/ → a/ | |
8460289f LP |
740 | * |
741 | * Also, the empty string is mapped to itself. | |
742 | * | |
743 | * This is different than basename(), which returns "" when a trailing slash is present. | |
b12d25a8 ZJS |
744 | */ |
745 | ||
746 | unsigned l, k; | |
747 | ||
748 | l = k = strlen(path); | |
69f9ccf1 ZJS |
749 | if (l == 0) /* special case — an empty string */ |
750 | return path; | |
751 | ||
b12d25a8 ZJS |
752 | while (k > 0 && path[k-1] == '/') |
753 | k--; | |
754 | ||
755 | if (k == 0) /* the root directory */ | |
756 | return path + l - 1; | |
757 | ||
758 | while (k > 0 && path[k-1] != '/') | |
759 | k--; | |
760 | ||
761 | return path + k; | |
762 | } | |
763 | ||
bb15fafe LP |
764 | bool filename_is_valid(const char *p) { |
765 | const char *e; | |
766 | ||
767 | if (isempty(p)) | |
768 | return false; | |
769 | ||
49bfc877 | 770 | if (dot_or_dot_dot(p)) |
bb15fafe LP |
771 | return false; |
772 | ||
773 | e = strchrnul(p, '/'); | |
774 | if (*e != 0) | |
775 | return false; | |
776 | ||
777 | if (e - p > FILENAME_MAX) | |
778 | return false; | |
779 | ||
780 | return true; | |
781 | } | |
782 | ||
99be45a4 | 783 | bool path_is_normalized(const char *p) { |
bb15fafe LP |
784 | |
785 | if (isempty(p)) | |
786 | return false; | |
787 | ||
49bfc877 LP |
788 | if (dot_or_dot_dot(p)) |
789 | return false; | |
790 | ||
791 | if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../")) | |
bb15fafe LP |
792 | return false; |
793 | ||
794 | if (strlen(p)+1 > PATH_MAX) | |
795 | return false; | |
796 | ||
49bfc877 | 797 | if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) |
bb15fafe LP |
798 | return false; |
799 | ||
800 | if (strstr(p, "//")) | |
801 | return false; | |
802 | ||
803 | return true; | |
804 | } | |
a0956174 LP |
805 | |
806 | char *file_in_same_dir(const char *path, const char *filename) { | |
807 | char *e, *ret; | |
808 | size_t k; | |
809 | ||
810 | assert(path); | |
811 | assert(filename); | |
812 | ||
813 | /* This removes the last component of path and appends | |
814 | * filename, unless the latter is absolute anyway or the | |
815 | * former isn't */ | |
816 | ||
817 | if (path_is_absolute(filename)) | |
818 | return strdup(filename); | |
819 | ||
820 | e = strrchr(path, '/'); | |
821 | if (!e) | |
822 | return strdup(filename); | |
823 | ||
824 | k = strlen(filename); | |
825 | ret = new(char, (e + 1 - path) + k + 1); | |
826 | if (!ret) | |
827 | return NULL; | |
828 | ||
829 | memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1); | |
830 | return ret; | |
831 | } | |
832 | ||
55cdd057 ZJS |
833 | bool hidden_or_backup_file(const char *filename) { |
834 | const char *p; | |
a0956174 | 835 | |
a0956174 LP |
836 | assert(filename); |
837 | ||
55cdd057 ZJS |
838 | if (filename[0] == '.' || |
839 | streq(filename, "lost+found") || | |
840 | streq(filename, "aquota.user") || | |
841 | streq(filename, "aquota.group") || | |
842 | endswith(filename, "~")) | |
a0956174 LP |
843 | return true; |
844 | ||
55cdd057 ZJS |
845 | p = strrchr(filename, '.'); |
846 | if (!p) | |
847 | return false; | |
848 | ||
941060bf LP |
849 | /* Please, let's not add more entries to the list below. If external projects think it's a good idea to come up |
850 | * with always new suffixes and that everybody else should just adjust to that, then it really should be on | |
851 | * them. Hence, in future, let's not add any more entries. Instead, let's ask those packages to instead adopt | |
852 | * one of the generic suffixes/prefixes for hidden files or backups, possibly augmented with an additional | |
853 | * string. Specifically: there's now: | |
854 | * | |
855 | * The generic suffixes "~" and ".bak" for backup files | |
856 | * The generic prefix "." for hidden files | |
857 | * | |
94a0ef6e ZJS |
858 | * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old", ".foopkg-dist" |
859 | * or so registered, let's refuse that and ask them to use ".foopkg.new", ".foopkg.old" or ".foopkg~" instead. | |
941060bf LP |
860 | */ |
861 | ||
55cdd057 ZJS |
862 | return STR_IN_SET(p + 1, |
863 | "rpmnew", | |
864 | "rpmsave", | |
865 | "rpmorig", | |
866 | "dpkg-old", | |
867 | "dpkg-new", | |
868 | "dpkg-tmp", | |
869 | "dpkg-dist", | |
870 | "dpkg-bak", | |
871 | "dpkg-backup", | |
872 | "dpkg-remove", | |
873 | "ucf-new", | |
874 | "ucf-old", | |
875 | "ucf-dist", | |
941060bf | 876 | "swp", |
94a0ef6e ZJS |
877 | "bak", |
878 | "old", | |
879 | "new"); | |
a0956174 LP |
880 | } |
881 | ||
882 | bool is_device_path(const char *path) { | |
883 | ||
bae47ba7 | 884 | /* Returns true on paths that likely refer to a device, either by path in sysfs or to something in /dev */ |
a0956174 | 885 | |
bae47ba7 | 886 | return PATH_STARTSWITH_SET(path, "/dev/", "/sys/"); |
3ccb8862 ZJS |
887 | } |
888 | ||
57e84e75 LP |
889 | bool valid_device_node_path(const char *path) { |
890 | ||
891 | /* Some superficial checks whether the specified path is a valid device node path, all without looking at the | |
892 | * actual device node. */ | |
893 | ||
894 | if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/")) | |
895 | return false; | |
896 | ||
897 | if (endswith(path, "/")) /* can't be a device node if it ends in a slash */ | |
898 | return false; | |
899 | ||
900 | return path_is_normalized(path); | |
901 | } | |
902 | ||
903 | bool valid_device_allow_pattern(const char *path) { | |
904 | assert(path); | |
905 | ||
906 | /* Like valid_device_node_path(), but also allows full-subsystem expressions, like DeviceAllow= and DeviceDeny= | |
907 | * accept it */ | |
908 | ||
909 | if (startswith(path, "block-") || | |
910 | startswith(path, "char-")) | |
911 | return true; | |
912 | ||
913 | return valid_device_node_path(path); | |
a0956174 | 914 | } |
5a46d55f ZJS |
915 | |
916 | int systemd_installation_has_version(const char *root, unsigned minimal_version) { | |
917 | const char *pattern; | |
918 | int r; | |
919 | ||
920 | /* Try to guess if systemd installation is later than the specified version. This | |
921 | * is hacky and likely to yield false negatives, particularly if the installation | |
922 | * is non-standard. False positives should be relatively rare. | |
923 | */ | |
924 | ||
925 | NULSTR_FOREACH(pattern, | |
926 | /* /lib works for systems without usr-merge, and for systems with a sane | |
927 | * usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary | |
928 | * for Gentoo which does a merge without making /lib a symlink. | |
929 | */ | |
930 | "lib/systemd/libsystemd-shared-*.so\0" | |
28faeda4 LP |
931 | "lib64/systemd/libsystemd-shared-*.so\0" |
932 | "usr/lib/systemd/libsystemd-shared-*.so\0" | |
933 | "usr/lib64/systemd/libsystemd-shared-*.so\0") { | |
5a46d55f ZJS |
934 | |
935 | _cleanup_strv_free_ char **names = NULL; | |
936 | _cleanup_free_ char *path = NULL; | |
937 | char *c, **name; | |
938 | ||
939 | path = prefix_root(root, pattern); | |
940 | if (!path) | |
941 | return -ENOMEM; | |
942 | ||
943 | r = glob_extend(&names, path); | |
944 | if (r == -ENOENT) | |
945 | continue; | |
946 | if (r < 0) | |
947 | return r; | |
948 | ||
dcd6361e | 949 | assert_se(c = endswith(path, "*.so")); |
5a46d55f ZJS |
950 | *c = '\0'; /* truncate the glob part */ |
951 | ||
952 | STRV_FOREACH(name, names) { | |
953 | /* This is most likely to run only once, hence let's not optimize anything. */ | |
954 | char *t, *t2; | |
955 | unsigned version; | |
956 | ||
957 | t = startswith(*name, path); | |
958 | if (!t) | |
959 | continue; | |
960 | ||
961 | t2 = endswith(t, ".so"); | |
962 | if (!t2) | |
963 | continue; | |
964 | ||
965 | t2[0] = '\0'; /* truncate the suffix */ | |
966 | ||
967 | r = safe_atou(t, &version); | |
968 | if (r < 0) { | |
969 | log_debug_errno(r, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name); | |
970 | continue; | |
971 | } | |
972 | ||
973 | log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).", | |
974 | *name, version, | |
975 | version >= minimal_version ? "OK" : "too old"); | |
976 | if (version >= minimal_version) | |
977 | return true; | |
978 | } | |
979 | } | |
980 | ||
981 | return false; | |
982 | } | |
49bfc877 LP |
983 | |
984 | bool dot_or_dot_dot(const char *path) { | |
985 | if (!path) | |
986 | return false; | |
987 | if (path[0] != '.') | |
988 | return false; | |
989 | if (path[1] == 0) | |
990 | return true; | |
991 | if (path[1] != '.') | |
992 | return false; | |
993 | ||
994 | return path[2] == 0; | |
995 | } | |
57ea45e1 LP |
996 | |
997 | bool empty_or_root(const char *root) { | |
998 | ||
999 | /* For operations relative to some root directory, returns true if the specified root directory is redundant, | |
1000 | * i.e. either / or NULL or the empty string or any equivalent. */ | |
1001 | ||
1002 | if (!root) | |
1003 | return true; | |
1004 | ||
1005 | return root[strspn(root, "/")] == 0; | |
1006 | } | |
58a53add YW |
1007 | |
1008 | int path_simplify_and_warn( | |
1009 | char *path, | |
1010 | unsigned flag, | |
1011 | const char *unit, | |
1012 | const char *filename, | |
1013 | unsigned line, | |
1014 | const char *lvalue) { | |
1015 | ||
5c270a18 | 1016 | bool absolute, fatal = flag & PATH_CHECK_FATAL; |
58a53add | 1017 | |
5c270a18 | 1018 | assert(!FLAGS_SET(flag, PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)); |
58a53add YW |
1019 | |
1020 | if (!utf8_is_valid(path)) { | |
1021 | log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, path); | |
1022 | return -EINVAL; | |
1023 | } | |
1024 | ||
1025 | if (flag & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) { | |
1026 | absolute = path_is_absolute(path); | |
1027 | ||
1028 | if (!absolute && (flag & PATH_CHECK_ABSOLUTE)) { | |
1029 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
1030 | "%s= path is not absolute%s: %s", | |
86ab333d | 1031 | lvalue, fatal ? "" : ", ignoring", path); |
58a53add YW |
1032 | return -EINVAL; |
1033 | } | |
1034 | ||
1035 | if (absolute && (flag & PATH_CHECK_RELATIVE)) { | |
1036 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
1037 | "%s= path is absolute%s: %s", | |
86ab333d | 1038 | lvalue, fatal ? "" : ", ignoring", path); |
58a53add YW |
1039 | return -EINVAL; |
1040 | } | |
1041 | } | |
1042 | ||
1043 | path_simplify(path, true); | |
1044 | ||
1045 | if (!path_is_normalized(path)) { | |
1046 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
1047 | "%s= path is not normalized%s: %s", | |
86ab333d | 1048 | lvalue, fatal ? "" : ", ignoring", path); |
58a53add YW |
1049 | return -EINVAL; |
1050 | } | |
1051 | ||
1052 | return 0; | |
1053 | } |