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