]>
Commit | Line | Data |
---|---|---|
9eb977db KS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010-2012 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
9eb977db | 22 | #include <errno.h> |
9eb977db | 23 | #include <fcntl.h> |
07630cea LP |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
9eb977db | 27 | #include <sys/statvfs.h> |
07630cea | 28 | #include <unistd.h> |
9eb977db | 29 | |
3ffd4af2 | 30 | #include "fd-util.h" |
07630cea | 31 | #include "fileio.h" |
9eb977db | 32 | #include "log.h" |
07630cea LP |
33 | #include "macro.h" |
34 | #include "missing.h" | |
6bedfcbb | 35 | #include "parse-util.h" |
3ffd4af2 | 36 | #include "path-util.h" |
07630cea | 37 | #include "string-util.h" |
9eb977db | 38 | #include "strv.h" |
07630cea | 39 | #include "util.h" |
9eb977db KS |
40 | |
41 | bool path_is_absolute(const char *p) { | |
42 | return p[0] == '/'; | |
43 | } | |
44 | ||
45 | bool is_path(const char *p) { | |
46 | return !!strchr(p, '/'); | |
47 | } | |
48 | ||
9eb977db KS |
49 | int path_get_parent(const char *path, char **_r) { |
50 | const char *e, *a = NULL, *b = NULL, *p; | |
51 | char *r; | |
52 | bool slash = false; | |
53 | ||
54 | assert(path); | |
55 | assert(_r); | |
56 | ||
57 | if (!*path) | |
58 | return -EINVAL; | |
59 | ||
60 | for (e = path; *e; e++) { | |
61 | ||
62 | if (!slash && *e == '/') { | |
63 | a = b; | |
64 | b = e; | |
65 | slash = true; | |
66 | } else if (slash && *e != '/') | |
67 | slash = false; | |
68 | } | |
69 | ||
70 | if (*(e-1) == '/') | |
71 | p = a; | |
72 | else | |
73 | p = b; | |
74 | ||
75 | if (!p) | |
76 | return -EINVAL; | |
77 | ||
78 | if (p == path) | |
79 | r = strdup("/"); | |
80 | else | |
81 | r = strndup(path, p-path); | |
82 | ||
83 | if (!r) | |
84 | return -ENOMEM; | |
85 | ||
86 | *_r = r; | |
87 | return 0; | |
88 | } | |
89 | ||
0f474365 | 90 | int path_split_and_make_absolute(const char *p, char ***ret) { |
9eb977db | 91 | char **l; |
0f474365 LP |
92 | int r; |
93 | ||
9eb977db | 94 | assert(p); |
0f474365 | 95 | assert(ret); |
9eb977db | 96 | |
116cc028 ZJS |
97 | l = strv_split(p, ":"); |
98 | if (!l) | |
ce9d6bcf | 99 | return -ENOMEM; |
9eb977db | 100 | |
0f474365 LP |
101 | r = path_strv_make_absolute_cwd(l); |
102 | if (r < 0) { | |
9eb977db | 103 | strv_free(l); |
0f474365 | 104 | return r; |
9eb977db KS |
105 | } |
106 | ||
0f474365 LP |
107 | *ret = l; |
108 | return r; | |
9eb977db KS |
109 | } |
110 | ||
111 | char *path_make_absolute(const char *p, const char *prefix) { | |
112 | assert(p); | |
113 | ||
114 | /* Makes every item in the list an absolute path by prepending | |
115 | * the prefix, if specified and necessary */ | |
116 | ||
117 | if (path_is_absolute(p) || !prefix) | |
118 | return strdup(p); | |
119 | ||
b7def684 | 120 | return strjoin(prefix, "/", p, NULL); |
9eb977db KS |
121 | } |
122 | ||
0f474365 LP |
123 | int path_make_absolute_cwd(const char *p, char **ret) { |
124 | char *c; | |
9eb977db KS |
125 | |
126 | assert(p); | |
0f474365 | 127 | assert(ret); |
9eb977db KS |
128 | |
129 | /* Similar to path_make_absolute(), but prefixes with the | |
130 | * current working directory. */ | |
131 | ||
132 | if (path_is_absolute(p)) | |
0f474365 LP |
133 | c = strdup(p); |
134 | else { | |
135 | _cleanup_free_ char *cwd = NULL; | |
9eb977db | 136 | |
0f474365 LP |
137 | cwd = get_current_dir_name(); |
138 | if (!cwd) | |
139 | return -errno; | |
140 | ||
141 | c = strjoin(cwd, "/", p, NULL); | |
142 | } | |
143 | if (!c) | |
144 | return -ENOMEM; | |
9eb977db | 145 | |
0f474365 LP |
146 | *ret = c; |
147 | return 0; | |
9eb977db KS |
148 | } |
149 | ||
7cb9c51c TK |
150 | int path_make_relative(const char *from_dir, const char *to_path, char **_r) { |
151 | char *r, *p; | |
152 | unsigned n_parents; | |
7cb9c51c TK |
153 | |
154 | assert(from_dir); | |
155 | assert(to_path); | |
156 | assert(_r); | |
157 | ||
158 | /* Strips the common part, and adds ".." elements as necessary. */ | |
159 | ||
160 | if (!path_is_absolute(from_dir)) | |
161 | return -EINVAL; | |
162 | ||
163 | if (!path_is_absolute(to_path)) | |
164 | return -EINVAL; | |
165 | ||
166 | /* Skip the common part. */ | |
167 | for (;;) { | |
168 | size_t a; | |
169 | size_t b; | |
170 | ||
171 | from_dir += strspn(from_dir, "/"); | |
172 | to_path += strspn(to_path, "/"); | |
173 | ||
174 | if (!*from_dir) { | |
175 | if (!*to_path) | |
176 | /* from_dir equals to_path. */ | |
177 | r = strdup("."); | |
178 | else | |
179 | /* from_dir is a parent directory of to_path. */ | |
180 | r = strdup(to_path); | |
181 | ||
182 | if (!r) | |
183 | return -ENOMEM; | |
184 | ||
5216f599 TK |
185 | path_kill_slashes(r); |
186 | ||
7cb9c51c TK |
187 | *_r = r; |
188 | return 0; | |
189 | } | |
190 | ||
191 | if (!*to_path) | |
192 | break; | |
193 | ||
194 | a = strcspn(from_dir, "/"); | |
195 | b = strcspn(to_path, "/"); | |
196 | ||
197 | if (a != b) | |
198 | break; | |
199 | ||
200 | if (memcmp(from_dir, to_path, a) != 0) | |
201 | break; | |
202 | ||
203 | from_dir += a; | |
204 | to_path += b; | |
205 | } | |
206 | ||
207 | /* If we're here, then "from_dir" has one or more elements that need to | |
208 | * be replaced with "..". */ | |
209 | ||
210 | /* Count the number of necessary ".." elements. */ | |
211 | for (n_parents = 0;;) { | |
212 | from_dir += strspn(from_dir, "/"); | |
213 | ||
214 | if (!*from_dir) | |
215 | break; | |
216 | ||
217 | from_dir += strcspn(from_dir, "/"); | |
218 | n_parents++; | |
219 | } | |
220 | ||
5216f599 | 221 | r = malloc(n_parents * 3 + strlen(to_path) + 1); |
7cb9c51c TK |
222 | if (!r) |
223 | return -ENOMEM; | |
224 | ||
225 | for (p = r; n_parents > 0; n_parents--, p += 3) | |
226 | memcpy(p, "../", 3); | |
227 | ||
5216f599 TK |
228 | strcpy(p, to_path); |
229 | path_kill_slashes(r); | |
7cb9c51c TK |
230 | |
231 | *_r = r; | |
232 | return 0; | |
233 | } | |
234 | ||
0f474365 | 235 | int path_strv_make_absolute_cwd(char **l) { |
9eb977db | 236 | char **s; |
0f474365 | 237 | int r; |
9eb977db KS |
238 | |
239 | /* Goes through every item in the string list and makes it | |
240 | * absolute. This works in place and won't rollback any | |
241 | * changes on failure. */ | |
242 | ||
243 | STRV_FOREACH(s, l) { | |
244 | char *t; | |
245 | ||
0f474365 LP |
246 | r = path_make_absolute_cwd(*s, &t); |
247 | if (r < 0) | |
248 | return r; | |
9eb977db KS |
249 | |
250 | free(*s); | |
251 | *s = t; | |
252 | } | |
253 | ||
0f474365 | 254 | return 0; |
9eb977db KS |
255 | } |
256 | ||
7d8da2c9 | 257 | char **path_strv_resolve(char **l, const char *prefix) { |
9eb977db KS |
258 | char **s; |
259 | unsigned k = 0; | |
260 | bool enomem = false; | |
261 | ||
262 | if (strv_isempty(l)) | |
263 | return l; | |
264 | ||
265 | /* Goes through every item in the string list and canonicalize | |
266 | * the path. This works in place and won't rollback any | |
267 | * changes on failure. */ | |
268 | ||
269 | STRV_FOREACH(s, l) { | |
270 | char *t, *u; | |
12ed81d9 | 271 | _cleanup_free_ char *orig = NULL; |
9eb977db | 272 | |
12ed81d9 ZJS |
273 | if (!path_is_absolute(*s)) { |
274 | free(*s); | |
9eb977db | 275 | continue; |
12ed81d9 | 276 | } |
112cfb18 MM |
277 | |
278 | if (prefix) { | |
12ed81d9 ZJS |
279 | orig = *s; |
280 | t = strappend(prefix, orig); | |
112cfb18 MM |
281 | if (!t) { |
282 | enomem = true; | |
283 | continue; | |
284 | } | |
12ed81d9 | 285 | } else |
112cfb18 | 286 | t = *s; |
9eb977db KS |
287 | |
288 | errno = 0; | |
289 | u = canonicalize_file_name(t); | |
9eb977db | 290 | if (!u) { |
12ed81d9 ZJS |
291 | if (errno == ENOENT) { |
292 | if (prefix) { | |
293 | u = orig; | |
294 | orig = NULL; | |
295 | free(t); | |
296 | } else | |
297 | u = t; | |
298 | } else { | |
874310b7 | 299 | free(t); |
112cfb18 | 300 | if (errno == ENOMEM || errno == 0) |
874310b7 ZJS |
301 | enomem = true; |
302 | ||
303 | continue; | |
304 | } | |
12ed81d9 ZJS |
305 | } else if (prefix) { |
306 | char *x; | |
307 | ||
308 | free(t); | |
309 | x = path_startswith(u, prefix); | |
310 | if (x) { | |
311 | /* restore the slash if it was lost */ | |
312 | if (!startswith(x, "/")) | |
313 | *(--x) = '/'; | |
314 | ||
315 | t = strdup(x); | |
316 | free(u); | |
317 | if (!t) { | |
318 | enomem = true; | |
319 | continue; | |
320 | } | |
321 | u = t; | |
322 | } else { | |
323 | /* canonicalized path goes outside of | |
324 | * prefix, keep the original path instead */ | |
3542eac7 | 325 | free(u); |
12ed81d9 ZJS |
326 | u = orig; |
327 | orig = NULL; | |
328 | } | |
91a6489d LP |
329 | } else |
330 | free(t); | |
9eb977db KS |
331 | |
332 | l[k++] = u; | |
333 | } | |
334 | ||
335 | l[k] = NULL; | |
336 | ||
337 | if (enomem) | |
338 | return NULL; | |
339 | ||
340 | return l; | |
341 | } | |
342 | ||
7d8da2c9 | 343 | char **path_strv_resolve_uniq(char **l, const char *prefix) { |
112cfb18 | 344 | |
fabe5c0e LP |
345 | if (strv_isempty(l)) |
346 | return l; | |
347 | ||
7d8da2c9 | 348 | if (!path_strv_resolve(l, prefix)) |
fabe5c0e LP |
349 | return NULL; |
350 | ||
351 | return strv_uniq(l); | |
352 | } | |
353 | ||
9eb977db KS |
354 | char *path_kill_slashes(char *path) { |
355 | char *f, *t; | |
356 | bool slash = false; | |
357 | ||
358 | /* Removes redundant inner and trailing slashes. Modifies the | |
359 | * passed string in-place. | |
360 | * | |
361 | * ///foo///bar/ becomes /foo/bar | |
362 | */ | |
363 | ||
364 | for (f = path, t = path; *f; f++) { | |
365 | ||
366 | if (*f == '/') { | |
367 | slash = true; | |
368 | continue; | |
369 | } | |
370 | ||
371 | if (slash) { | |
372 | slash = false; | |
373 | *(t++) = '/'; | |
374 | } | |
375 | ||
376 | *(t++) = *f; | |
377 | } | |
378 | ||
379 | /* Special rule, if we are talking of the root directory, a | |
380 | trailing slash is good */ | |
381 | ||
382 | if (t == path && slash) | |
383 | *(t++) = '/'; | |
384 | ||
385 | *t = 0; | |
386 | return path; | |
387 | } | |
388 | ||
424a19f8 | 389 | char* path_startswith(const char *path, const char *prefix) { |
9eb977db KS |
390 | assert(path); |
391 | assert(prefix); | |
392 | ||
393 | if ((path[0] == '/') != (prefix[0] == '/')) | |
424a19f8 | 394 | return NULL; |
9eb977db KS |
395 | |
396 | for (;;) { | |
397 | size_t a, b; | |
398 | ||
399 | path += strspn(path, "/"); | |
400 | prefix += strspn(prefix, "/"); | |
401 | ||
402 | if (*prefix == 0) | |
424a19f8 | 403 | return (char*) path; |
9eb977db KS |
404 | |
405 | if (*path == 0) | |
424a19f8 | 406 | return NULL; |
9eb977db KS |
407 | |
408 | a = strcspn(path, "/"); | |
409 | b = strcspn(prefix, "/"); | |
410 | ||
411 | if (a != b) | |
424a19f8 | 412 | return NULL; |
9eb977db KS |
413 | |
414 | if (memcmp(path, prefix, a) != 0) | |
424a19f8 | 415 | return NULL; |
9eb977db KS |
416 | |
417 | path += a; | |
418 | prefix += b; | |
419 | } | |
420 | } | |
421 | ||
2230852b MS |
422 | int path_compare(const char *a, const char *b) { |
423 | int d; | |
424 | ||
9eb977db KS |
425 | assert(a); |
426 | assert(b); | |
427 | ||
2230852b MS |
428 | /* A relative path and an abolute path must not compare as equal. |
429 | * Which one is sorted before the other does not really matter. | |
430 | * Here a relative path is ordered before an absolute path. */ | |
431 | d = (a[0] == '/') - (b[0] == '/'); | |
373cd63a | 432 | if (d != 0) |
2230852b | 433 | return d; |
9eb977db KS |
434 | |
435 | for (;;) { | |
436 | size_t j, k; | |
437 | ||
438 | a += strspn(a, "/"); | |
439 | b += strspn(b, "/"); | |
440 | ||
441 | if (*a == 0 && *b == 0) | |
2230852b | 442 | return 0; |
9eb977db | 443 | |
2230852b MS |
444 | /* Order prefixes first: "/foo" before "/foo/bar" */ |
445 | if (*a == 0) | |
446 | return -1; | |
447 | if (*b == 0) | |
448 | return 1; | |
9eb977db KS |
449 | |
450 | j = strcspn(a, "/"); | |
451 | k = strcspn(b, "/"); | |
452 | ||
2230852b MS |
453 | /* Alphabetical sort: "/foo/aaa" before "/foo/b" */ |
454 | d = memcmp(a, b, MIN(j, k)); | |
373cd63a | 455 | if (d != 0) |
2230852b | 456 | return (d > 0) - (d < 0); /* sign of d */ |
9eb977db | 457 | |
2230852b MS |
458 | /* Sort "/foo/a" before "/foo/aaa" */ |
459 | d = (j > k) - (j < k); /* sign of (j - k) */ | |
373cd63a | 460 | if (d != 0) |
2230852b | 461 | return d; |
9eb977db KS |
462 | |
463 | a += j; | |
464 | b += k; | |
465 | } | |
466 | } | |
467 | ||
2230852b MS |
468 | bool path_equal(const char *a, const char *b) { |
469 | return path_compare(a, b) == 0; | |
470 | } | |
471 | ||
c78e47a6 MS |
472 | bool path_equal_or_files_same(const char *a, const char *b) { |
473 | return path_equal(a, b) || files_same(a, b) > 0; | |
474 | } | |
475 | ||
0c6ea3a4 ZJS |
476 | char* path_join(const char *root, const char *path, const char *rest) { |
477 | assert(path); | |
478 | ||
479 | if (!isempty(root)) | |
bc854dc7 | 480 | return strjoin(root, endswith(root, "/") ? "" : "/", |
0c6ea3a4 | 481 | path[0] == '/' ? path+1 : path, |
bc854dc7 | 482 | rest ? (endswith(path, "/") ? "" : "/") : NULL, |
0c6ea3a4 ZJS |
483 | rest && rest[0] == '/' ? rest+1 : rest, |
484 | NULL); | |
485 | else | |
486 | return strjoin(path, | |
bc854dc7 | 487 | rest ? (endswith(path, "/") ? "" : "/") : NULL, |
0c6ea3a4 ZJS |
488 | rest && rest[0] == '/' ? rest+1 : rest, |
489 | NULL); | |
490 | } | |
491 | ||
3f72b427 LP |
492 | static int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *mnt_id) { |
493 | char path[strlen("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)]; | |
494 | _cleanup_free_ char *fdinfo = NULL; | |
495 | _cleanup_close_ int subfd = -1; | |
496 | char *p; | |
497 | int r; | |
498 | ||
499 | if ((flags & AT_EMPTY_PATH) && isempty(filename)) | |
500 | xsprintf(path, "/proc/self/fdinfo/%i", fd); | |
501 | else { | |
502 | subfd = openat(fd, filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_PATH); | |
503 | if (subfd < 0) | |
504 | return -errno; | |
505 | ||
506 | xsprintf(path, "/proc/self/fdinfo/%i", subfd); | |
507 | } | |
508 | ||
509 | r = read_full_file(path, &fdinfo, NULL); | |
510 | if (r == -ENOENT) /* The fdinfo directory is a relatively new addition */ | |
511 | return -EOPNOTSUPP; | |
512 | if (r < 0) | |
513 | return -errno; | |
514 | ||
515 | p = startswith(fdinfo, "mnt_id:"); | |
516 | if (!p) { | |
517 | p = strstr(fdinfo, "\nmnt_id:"); | |
518 | if (!p) /* The mnt_id field is a relatively new addition */ | |
519 | return -EOPNOTSUPP; | |
520 | ||
521 | p += 8; | |
522 | } | |
523 | ||
524 | p += strspn(p, WHITESPACE); | |
525 | p[strcspn(p, WHITESPACE)] = 0; | |
526 | ||
527 | return safe_atoi(p, mnt_id); | |
528 | } | |
529 | ||
5d409034 | 530 | int fd_is_mount_point(int fd, const char *filename, int flags) { |
05d990ef | 531 | union file_handle_union h = FILE_HANDLE_INIT, h_parent = FILE_HANDLE_INIT; |
e40872fc | 532 | int mount_id = -1, mount_id_parent = -1; |
84317788 | 533 | bool nosupp = false, check_st_dev = true; |
1640a0b6 | 534 | struct stat a, b; |
21749924 | 535 | int r; |
9eb977db | 536 | |
05d990ef | 537 | assert(fd >= 0); |
5d409034 | 538 | assert(filename); |
05d990ef | 539 | |
3f72b427 LP |
540 | /* First we will try the name_to_handle_at() syscall, which |
541 | * tells us the mount id and an opaque file "handle". It is | |
542 | * not supported everywhere though (kernel compile-time | |
543 | * option, not all file systems are hooked up). If it works | |
544 | * the mount id is usually good enough to tell us whether | |
545 | * something is a mount point. | |
546 | * | |
547 | * If that didn't work we will try to read the mount id from | |
548 | * /proc/self/fdinfo/<fd>. This is almost as good as | |
af86c440 | 549 | * name_to_handle_at(), however, does not return the |
3f72b427 LP |
550 | * opaque file handle. The opaque file handle is pretty useful |
551 | * to detect the root directory, which we should always | |
552 | * consider a mount point. Hence we use this only as | |
553 | * fallback. Exporting the mnt_id in fdinfo is a pretty recent | |
554 | * kernel addition. | |
555 | * | |
556 | * As last fallback we do traditional fstat() based st_dev | |
557 | * comparisons. This is how things were traditionally done, | |
558 | * but unionfs breaks breaks this since it exposes file | |
559 | * systems with a variety of st_dev reported. Also, btrfs | |
560 | * subvolumes have different st_dev, even though they aren't | |
561 | * real mounts of their own. */ | |
cde9cb34 | 562 | |
5d409034 | 563 | r = name_to_handle_at(fd, filename, &h.handle, &mount_id, flags); |
9eb977db | 564 | if (r < 0) { |
e40872fc DR |
565 | if (errno == ENOSYS) |
566 | /* This kernel does not support name_to_handle_at() | |
3f72b427 LP |
567 | * fall back to simpler logic. */ |
568 | goto fallback_fdinfo; | |
e40872fc | 569 | else if (errno == EOPNOTSUPP) |
fa125f4e | 570 | /* This kernel or file system does not support |
f25afeb6 LP |
571 | * name_to_handle_at(), hence let's see if the |
572 | * upper fs supports it (in which case it is a | |
573 | * mount point), otherwise fallback to the | |
1640a0b6 | 574 | * traditional stat() logic */ |
6feeeab0 | 575 | nosupp = true; |
6feeeab0 ZJS |
576 | else |
577 | return -errno; | |
9eb977db KS |
578 | } |
579 | ||
5d409034 | 580 | r = name_to_handle_at(fd, "", &h_parent.handle, &mount_id_parent, AT_EMPTY_PATH); |
f25afeb6 LP |
581 | if (r < 0) { |
582 | if (errno == EOPNOTSUPP) { | |
6feeeab0 ZJS |
583 | if (nosupp) |
584 | /* Neither parent nor child do name_to_handle_at()? | |
585 | We have no choice but to fall back. */ | |
3f72b427 | 586 | goto fallback_fdinfo; |
6feeeab0 | 587 | else |
f25afeb6 LP |
588 | /* The parent can't do name_to_handle_at() but the |
589 | * directory we are interested in can? | |
6feeeab0 ZJS |
590 | * If so, it must be a mount point. */ |
591 | return 1; | |
f25afeb6 | 592 | } else |
6feeeab0 | 593 | return -errno; |
3f72b427 LP |
594 | } |
595 | ||
596 | /* The parent can do name_to_handle_at() but the | |
597 | * directory we are interested in can't? If so, it | |
598 | * must be a mount point. */ | |
599 | if (nosupp) | |
8f06b239 | 600 | return 1; |
05d990ef | 601 | |
3f72b427 LP |
602 | /* If the file handle for the directory we are |
603 | * interested in and its parent are identical, we | |
604 | * assume this is the root directory, which is a mount | |
605 | * point. */ | |
606 | ||
607 | if (h.handle.handle_bytes == h_parent.handle.handle_bytes && | |
608 | h.handle.handle_type == h_parent.handle.handle_type && | |
609 | memcmp(h.handle.f_handle, h_parent.handle.f_handle, h.handle.handle_bytes) == 0) | |
610 | return 1; | |
1640a0b6 | 611 | |
3f72b427 LP |
612 | return mount_id != mount_id_parent; |
613 | ||
614 | fallback_fdinfo: | |
5d409034 | 615 | r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id); |
3f72b427 LP |
616 | if (r == -EOPNOTSUPP) |
617 | goto fallback_fstat; | |
e792e890 | 618 | if (r < 0) |
3f72b427 | 619 | return r; |
9eb977db | 620 | |
5d409034 | 621 | r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent); |
1640a0b6 | 622 | if (r < 0) |
3f72b427 LP |
623 | return r; |
624 | ||
625 | if (mount_id != mount_id_parent) | |
626 | return 1; | |
627 | ||
628 | /* Hmm, so, the mount ids are the same. This leaves one | |
629 | * special case though for the root file system. For that, | |
630 | * let's see if the parent directory has the same inode as we | |
631 | * are interested in. Hence, let's also do fstat() checks now, | |
632 | * too, but avoid the st_dev comparisons, since they aren't | |
633 | * that useful on unionfs mounts. */ | |
634 | check_st_dev = false; | |
635 | ||
636 | fallback_fstat: | |
5d409034 MP |
637 | /* yay for fstatat() taking a different set of flags than the other |
638 | * _at() above */ | |
639 | if (flags & AT_SYMLINK_FOLLOW) | |
640 | flags &= ~AT_SYMLINK_FOLLOW; | |
641 | else | |
642 | flags |= AT_SYMLINK_NOFOLLOW; | |
643 | if (fstatat(fd, filename, &a, flags) < 0) | |
3f72b427 LP |
644 | return -errno; |
645 | ||
5d409034 | 646 | if (fstatat(fd, "", &b, AT_EMPTY_PATH) < 0) |
1640a0b6 LP |
647 | return -errno; |
648 | ||
05d990ef LP |
649 | /* A directory with same device and inode as its parent? Must |
650 | * be the root directory */ | |
651 | if (a.st_dev == b.st_dev && | |
652 | a.st_ino == b.st_ino) | |
653 | return 1; | |
654 | ||
3f72b427 | 655 | return check_st_dev && (a.st_dev != b.st_dev); |
9eb977db KS |
656 | } |
657 | ||
e26d6ce5 MP |
658 | /* flags can be AT_SYMLINK_FOLLOW or 0 */ |
659 | int path_is_mount_point(const char *t, int flags) { | |
f25afeb6 | 660 | _cleanup_close_ int fd = -1; |
36908eb8 | 661 | _cleanup_free_ char *canonical = NULL, *parent = NULL; |
5d409034 | 662 | int r; |
e792e890 | 663 | |
f25afeb6 LP |
664 | assert(t); |
665 | ||
666 | if (path_equal(t, "/")) | |
667 | return 1; | |
668 | ||
36908eb8 MP |
669 | /* we need to resolve symlinks manually, we can't just rely on |
670 | * fd_is_mount_point() to do that for us; if we have a structure like | |
671 | * /bin -> /usr/bin/ and /usr is a mount point, then the parent that we | |
672 | * look at needs to be /usr, not /. */ | |
673 | if (flags & AT_SYMLINK_FOLLOW) { | |
674 | canonical = canonicalize_file_name(t); | |
675 | if (!canonical) | |
676 | return -errno; | |
10c03e9e LP |
677 | |
678 | t = canonical; | |
36908eb8 MP |
679 | } |
680 | ||
10c03e9e | 681 | r = path_get_parent(t, &parent); |
5d409034 MP |
682 | if (r < 0) |
683 | return r; | |
684 | ||
685 | fd = openat(AT_FDCWD, parent, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_PATH); | |
e792e890 | 686 | if (fd < 0) |
f25afeb6 | 687 | return -errno; |
f25afeb6 | 688 | |
10c03e9e | 689 | return fd_is_mount_point(fd, basename(t), flags); |
f25afeb6 LP |
690 | } |
691 | ||
9eb977db KS |
692 | int path_is_read_only_fs(const char *path) { |
693 | struct statvfs st; | |
694 | ||
695 | assert(path); | |
696 | ||
697 | if (statvfs(path, &st) < 0) | |
698 | return -errno; | |
699 | ||
70421bdc LP |
700 | if (st.f_flag & ST_RDONLY) |
701 | return true; | |
702 | ||
703 | /* On NFS, statvfs() might not reflect whether we can actually | |
704 | * write to the remote share. Let's try again with | |
705 | * access(W_OK) which is more reliable, at least sometimes. */ | |
706 | if (access(path, W_OK) < 0 && errno == EROFS) | |
707 | return true; | |
708 | ||
709 | return false; | |
9eb977db | 710 | } |
66060897 LP |
711 | |
712 | int path_is_os_tree(const char *path) { | |
713 | char *p; | |
714 | int r; | |
715 | ||
5ae4d543 | 716 | /* We use /usr/lib/os-release as flag file if something is an OS */ |
63c372cb | 717 | p = strjoina(path, "/usr/lib/os-release"); |
5ae4d543 | 718 | r = access(p, F_OK); |
5ae4d543 LP |
719 | if (r >= 0) |
720 | return 1; | |
66060897 | 721 | |
5ae4d543 | 722 | /* Also check for the old location in /etc, just in case. */ |
63c372cb | 723 | p = strjoina(path, "/etc/os-release"); |
66060897 LP |
724 | r = access(p, F_OK); |
725 | ||
5ae4d543 | 726 | return r >= 0; |
66060897 | 727 | } |
c9d954b2 | 728 | |
85eca92e LP |
729 | int find_binary(const char *name, char **ret) { |
730 | int last_error, r; | |
731 | const char *p; | |
732 | ||
c9d954b2 | 733 | assert(name); |
4087cb9e | 734 | |
571d0134 | 735 | if (is_path(name)) { |
85eca92e | 736 | if (access(name, X_OK) < 0) |
b972115c ZJS |
737 | return -errno; |
738 | ||
85eca92e | 739 | if (ret) { |
0f474365 LP |
740 | r = path_make_absolute_cwd(name, ret); |
741 | if (r < 0) | |
742 | return r; | |
eb66db55 | 743 | } |
c9d954b2 | 744 | |
c9d954b2 | 745 | return 0; |
85eca92e | 746 | } |
c9d954b2 | 747 | |
85eca92e LP |
748 | /** |
749 | * Plain getenv, not secure_getenv, because we want | |
750 | * to actually allow the user to pick the binary. | |
751 | */ | |
752 | p = getenv("PATH"); | |
753 | if (!p) | |
754 | p = DEFAULT_PATH; | |
755 | ||
756 | last_error = -ENOENT; | |
757 | ||
758 | for (;;) { | |
759 | _cleanup_free_ char *j = NULL, *element = NULL; | |
760 | ||
761 | r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS); | |
762 | if (r < 0) | |
763 | return r; | |
764 | if (r == 0) | |
765 | break; | |
766 | ||
0f474365 LP |
767 | if (!path_is_absolute(element)) |
768 | continue; | |
769 | ||
85eca92e LP |
770 | j = strjoin(element, "/", name, NULL); |
771 | if (!j) | |
772 | return -ENOMEM; | |
773 | ||
774 | if (access(j, X_OK) >= 0) { | |
775 | /* Found it! */ | |
c9d954b2 | 776 | |
85eca92e LP |
777 | if (ret) { |
778 | *ret = path_kill_slashes(j); | |
779 | j = NULL; | |
eb66db55 | 780 | } |
c9d954b2 ZJS |
781 | |
782 | return 0; | |
783 | } | |
784 | ||
85eca92e | 785 | last_error = -errno; |
c9d954b2 | 786 | } |
85eca92e LP |
787 | |
788 | return last_error; | |
c9d954b2 | 789 | } |
8e184852 | 790 | |
2ad8416d | 791 | bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) { |
8e184852 | 792 | bool changed = false; |
2ad8416d | 793 | const char* const* i; |
8e184852 | 794 | |
97f2d76d TG |
795 | assert(timestamp); |
796 | ||
8e184852 | 797 | if (paths == NULL) |
4087cb9e | 798 | return false; |
8e184852 | 799 | |
4087cb9e | 800 | STRV_FOREACH(i, paths) { |
8e184852 | 801 | struct stat stats; |
4087cb9e | 802 | usec_t u; |
8e184852 | 803 | |
4087cb9e | 804 | if (stat(*i, &stats) < 0) |
8e184852 TG |
805 | continue; |
806 | ||
4087cb9e LP |
807 | u = timespec_load(&stats.st_mtim); |
808 | ||
97f2d76d | 809 | /* first check */ |
4087cb9e | 810 | if (*timestamp >= u) |
8e184852 TG |
811 | continue; |
812 | ||
9f6445e3 | 813 | log_debug("timestamp of '%s' changed", *i); |
8e184852 TG |
814 | |
815 | /* update timestamp */ | |
4087cb9e LP |
816 | if (update) { |
817 | *timestamp = u; | |
818 | changed = true; | |
819 | } else | |
820 | return true; | |
8e184852 | 821 | } |
4087cb9e | 822 | |
8e184852 TG |
823 | return changed; |
824 | } | |
eb66db55 | 825 | |
5bcd08db | 826 | static int binary_is_good(const char *binary) { |
571d0134 | 827 | _cleanup_free_ char *p = NULL, *d = NULL; |
571d0134 | 828 | int r; |
eb66db55 | 829 | |
85eca92e LP |
830 | r = find_binary(binary, &p); |
831 | if (r == -ENOENT) | |
832 | return 0; | |
571d0134 LP |
833 | if (r < 0) |
834 | return r; | |
835 | ||
061df014 | 836 | /* An fsck that is linked to /bin/true is a non-existent |
571d0134 LP |
837 | * fsck */ |
838 | ||
839 | r = readlink_malloc(p, &d); | |
85eca92e LP |
840 | if (r == -EINVAL) /* not a symlink */ |
841 | return 1; | |
842 | if (r < 0) | |
843 | return r; | |
571d0134 | 844 | |
85eca92e LP |
845 | return !path_equal(d, "true") && |
846 | !path_equal(d, "/bin/true") && | |
847 | !path_equal(d, "/usr/bin/true") && | |
848 | !path_equal(d, "/dev/null"); | |
eb66db55 | 849 | } |
1d13f648 | 850 | |
5bcd08db LP |
851 | int fsck_exists(const char *fstype) { |
852 | const char *checker; | |
853 | ||
85eca92e | 854 | assert(fstype); |
5bcd08db | 855 | |
85eca92e LP |
856 | if (streq(fstype, "auto")) |
857 | return -EINVAL; | |
858 | ||
859 | checker = strjoina("fsck.", fstype); | |
5bcd08db LP |
860 | return binary_is_good(checker); |
861 | } | |
862 | ||
863 | int mkfs_exists(const char *fstype) { | |
864 | const char *mkfs; | |
865 | ||
85eca92e | 866 | assert(fstype); |
5bcd08db | 867 | |
85eca92e LP |
868 | if (streq(fstype, "auto")) |
869 | return -EINVAL; | |
870 | ||
871 | mkfs = strjoina("mkfs.", fstype); | |
5bcd08db LP |
872 | return binary_is_good(mkfs); |
873 | } | |
874 | ||
1d13f648 LP |
875 | char *prefix_root(const char *root, const char *path) { |
876 | char *n, *p; | |
877 | size_t l; | |
878 | ||
879 | /* If root is passed, prefixes path with it. Otherwise returns | |
880 | * it as is. */ | |
881 | ||
882 | assert(path); | |
883 | ||
884 | /* First, drop duplicate prefixing slashes from the path */ | |
885 | while (path[0] == '/' && path[1] == '/') | |
886 | path++; | |
887 | ||
888 | if (isempty(root) || path_equal(root, "/")) | |
889 | return strdup(path); | |
890 | ||
891 | l = strlen(root) + 1 + strlen(path) + 1; | |
892 | ||
893 | n = new(char, l); | |
894 | if (!n) | |
895 | return NULL; | |
896 | ||
897 | p = stpcpy(n, root); | |
898 | ||
899 | while (p > n && p[-1] == '/') | |
900 | p--; | |
901 | ||
902 | if (path[0] != '/') | |
903 | *(p++) = '/'; | |
904 | ||
905 | strcpy(p, path); | |
906 | return n; | |
907 | } | |
0f03c2a4 LP |
908 | |
909 | int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) { | |
910 | char *p; | |
911 | int r; | |
912 | ||
913 | /* | |
914 | * This function is intended to be used in command line | |
915 | * parsers, to handle paths that are passed in. It makes the | |
916 | * path absolute, and reduces it to NULL if omitted or | |
917 | * root (the latter optionally). | |
918 | * | |
919 | * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON | |
920 | * SUCCESS! Hence, do not pass in uninitialized pointers. | |
921 | */ | |
922 | ||
923 | if (isempty(path)) { | |
924 | *arg = mfree(*arg); | |
925 | return 0; | |
926 | } | |
927 | ||
928 | r = path_make_absolute_cwd(path, &p); | |
929 | if (r < 0) | |
930 | return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path); | |
931 | ||
932 | path_kill_slashes(p); | |
933 | if (suppress_root && path_equal(p, "/")) | |
934 | p = mfree(p); | |
935 | ||
936 | free(*arg); | |
937 | *arg = p; | |
938 | return 0; | |
939 | } |