]>
Commit | Line | Data |
---|---|---|
69b7e41e KZ |
1 | /* |
2 | * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com> | |
3 | * | |
4 | * This file may be redistributed under the terms of the | |
5 | * GNU Lesser General Public License. | |
6 | */ | |
7 | ||
192c6aad KZ |
8 | /** |
9 | * SECTION: utils | |
10 | * @title: Utils | |
11 | * @short_description: misc utils. | |
12 | */ | |
69b7e41e | 13 | #include <ctype.h> |
69b7e41e KZ |
14 | #include <fcntl.h> |
15 | #include <pwd.h> | |
a1e8af75 | 16 | #include <grp.h> |
69b7e41e | 17 | |
b49103ed | 18 | #include "strutils.h" |
0532ba1d | 19 | #include "pathnames.h" |
69b7e41e | 20 | #include "mountP.h" |
3c5e4ef8 | 21 | #include "mangle.h" |
0bb44be3 | 22 | #include "canonicalize.h" |
035507c8 | 23 | #include "env.h" |
b483debb | 24 | #include "match.h" |
69b7e41e | 25 | |
b49103ed KZ |
26 | int endswith(const char *s, const char *sx) |
27 | { | |
28 | ssize_t off; | |
29 | ||
30 | assert(s); | |
31 | assert(sx); | |
32 | ||
33 | off = strlen(s); | |
34 | if (!off) | |
35 | return 0; | |
36 | off -= strlen(sx); | |
37 | if (off < 0) | |
38 | return 0; | |
39 | ||
40 | return !strcmp(s + off, sx); | |
41 | } | |
42 | ||
43 | int startswith(const char *s, const char *sx) | |
44 | { | |
45 | size_t off; | |
46 | ||
47 | assert(s); | |
48 | assert(sx); | |
49 | ||
50 | off = strlen(sx); | |
51 | if (!off) | |
52 | return 0; | |
53 | ||
54 | return !strncmp(s, sx, off); | |
55 | } | |
56 | ||
fd1eb7a7 KZ |
57 | int mnt_parse_offset(const char *str, size_t len, uintmax_t *res) |
58 | { | |
59 | char *p; | |
60 | int rc = 0; | |
61 | ||
62 | if (!str && !*str) | |
63 | return -EINVAL; | |
64 | ||
65 | p = strndup(str, len); | |
66 | if (!p) | |
67 | return -errno; | |
68 | ||
69 | if (strtosize(p, res)) | |
70 | rc = -EINVAL; | |
71 | free(p); | |
72 | return rc; | |
73 | } | |
74 | ||
1d0cd73f KZ |
75 | /* returns basename and keeps dirname in the @path, if @path is "/" (root) |
76 | * then returns empty string */ | |
77 | static char *stripoff_last_component(char *path) | |
78 | { | |
79 | char *p = path ? strrchr(path, '/') : NULL; | |
80 | ||
81 | if (!p) | |
82 | return NULL; | |
83 | *p = '\0'; | |
61073773 | 84 | return p + 1; |
1d0cd73f KZ |
85 | } |
86 | ||
61073773 KZ |
87 | /* |
88 | * Note that the @target has to be absolute path (so at least "/"). The | |
89 | * @filename returns allocated buffer with last path component, for example: | |
90 | * | |
91 | * mnt_chdir_to_parent("/mnt/test", &buf) ==> chdir("/mnt"), buf="test" | |
66bb8267 KZ |
92 | */ |
93 | int mnt_chdir_to_parent(const char *target, char **filename) | |
94 | { | |
61073773 | 95 | char *buf, *parent, *last = NULL; |
66bb8267 KZ |
96 | char cwd[PATH_MAX]; |
97 | int rc = -EINVAL; | |
98 | ||
99 | if (!target || *target != '/') | |
100 | return -EINVAL; | |
101 | ||
61073773 KZ |
102 | DBG(UTILS, mnt_debug("moving to %s parent", target)); |
103 | ||
104 | buf = strdup(target); | |
105 | if (!buf) | |
66bb8267 KZ |
106 | return -ENOMEM; |
107 | ||
61073773 KZ |
108 | if (*(buf + 1) != '\0') { |
109 | last = stripoff_last_component(buf); | |
66bb8267 KZ |
110 | if (!last) |
111 | goto err; | |
112 | } | |
66bb8267 | 113 | |
61073773 KZ |
114 | parent = buf && *buf ? buf : "/"; |
115 | ||
116 | if (chdir(parent) == -1) { | |
117 | DBG(UTILS, mnt_debug("failed to chdir to %s: %m", parent)); | |
66bb8267 KZ |
118 | rc = -errno; |
119 | goto err; | |
120 | } | |
121 | if (!getcwd(cwd, sizeof(cwd))) { | |
122 | DBG(UTILS, mnt_debug("failed to obtain current directory: %m")); | |
123 | rc = -errno; | |
124 | goto err; | |
125 | } | |
61073773 KZ |
126 | if (strcmp(cwd, parent) != 0) { |
127 | DBG(UTILS, mnt_debug( | |
128 | "unexpected chdir (expected=%s, cwd=%s)", parent, cwd)); | |
66bb8267 KZ |
129 | goto err; |
130 | } | |
131 | ||
61073773 KZ |
132 | DBG(CXT, mnt_debug( |
133 | "current directory moved to %s [last_component='%s']", | |
134 | parent, last)); | |
66bb8267 | 135 | |
61073773 | 136 | *filename = buf; |
66bb8267 KZ |
137 | |
138 | if (!last || !*last) | |
139 | memcpy(*filename, ".", 2); | |
140 | else | |
141 | memcpy(*filename, last, strlen(last) + 1); | |
142 | return 0; | |
143 | err: | |
61073773 | 144 | free(buf); |
66bb8267 KZ |
145 | return rc; |
146 | } | |
147 | ||
3c5e4ef8 KZ |
148 | /** |
149 | * mnt_mangle: | |
150 | * @str: string | |
151 | * | |
152 | * Encode @str to be compatible with fstab/mtab | |
153 | * | |
154 | * Returns: new allocated string or NULL in case of error. | |
155 | */ | |
156 | char *mnt_mangle(const char *str) | |
157 | { | |
158 | return mangle(str); | |
159 | } | |
160 | ||
161 | /** | |
162 | * mnt_unmangle: | |
163 | * @str: string | |
164 | * | |
165 | * Decode @str from fstab/mtab | |
166 | * | |
167 | * Returns: new allocated string or NULL in case of error. | |
168 | */ | |
169 | char *mnt_unmangle(const char *str) | |
170 | { | |
dd369652 | 171 | return unmangle(str, NULL); |
3c5e4ef8 KZ |
172 | } |
173 | ||
69b7e41e KZ |
174 | /** |
175 | * mnt_fstype_is_pseudofs: | |
176 | * @type: filesystem name | |
177 | * | |
178 | * Returns: 1 for filesystems like proc, sysfs, ... or 0. | |
179 | */ | |
180 | int mnt_fstype_is_pseudofs(const char *type) | |
181 | { | |
182 | if (!type) | |
183 | return 0; | |
184 | if (strcmp(type, "none") == 0 || | |
185 | strcmp(type, "proc") == 0 || | |
186 | strcmp(type, "tmpfs") == 0 || | |
187 | strcmp(type, "sysfs") == 0 || | |
854fa6d9 | 188 | strcmp(type, "autofs") == 0 || |
69b7e41e | 189 | strcmp(type, "devpts") == 0|| |
f5017242 | 190 | strcmp(type, "cgroup") == 0 || |
854fa6d9 | 191 | strcmp(type, "devtmpfs") == 0 || |
69b7e41e KZ |
192 | strcmp(type, "devfs") == 0 || |
193 | strcmp(type, "dlmfs") == 0 || | |
194 | strcmp(type, "cpuset") == 0 || | |
1bbe1bc7 | 195 | strcmp(type, "securityfs") == 0 || |
854fa6d9 | 196 | strcmp(type, "hugetlbfs") == 0 || |
c5a6f7a9 KZ |
197 | strcmp(type, "rpc_pipefs") == 0 || |
198 | strcmp(type, "fusectl") == 0 || | |
854fa6d9 | 199 | strcmp(type, "mqueue") == 0 || |
c5a6f7a9 KZ |
200 | strcmp(type, "binfmt_misc") == 0 || |
201 | strcmp(type, "fuse.gvfs-fuse-daemon") == 0 || | |
1bbe1bc7 | 202 | strcmp(type, "debugfs") == 0 || |
69b7e41e KZ |
203 | strcmp(type, "spufs") == 0) |
204 | return 1; | |
205 | return 0; | |
206 | } | |
207 | ||
208 | /** | |
209 | * mnt_fstype_is_netfs: | |
210 | * @type: filesystem name | |
211 | * | |
212 | * Returns: 1 for filesystems like cifs, nfs, ... or 0. | |
213 | */ | |
214 | int mnt_fstype_is_netfs(const char *type) | |
215 | { | |
216 | if (!type) | |
217 | return 0; | |
c59cf20c | 218 | if (strcmp(type, "cifs") == 0 || |
69b7e41e | 219 | strcmp(type, "smbfs") == 0 || |
c59cf20c KZ |
220 | strncmp(type,"nfs", 3) == 0 || |
221 | strcmp(type, "afs") == 0 || | |
222 | strcmp(type, "ncpfs") == 0 || | |
223 | strncmp(type,"9p", 2) == 0) | |
69b7e41e KZ |
224 | return 1; |
225 | return 0; | |
226 | } | |
227 | ||
abc9c0f7 KZ |
228 | /** |
229 | * mnt_match_fstype: | |
230 | * @type: filesystem type | |
6ad929bb | 231 | * @pattern: filesystem name or comma delimited list of names |
abc9c0f7 KZ |
232 | * |
233 | * The @pattern list of filesystem can be prefixed with a global | |
234 | * "no" prefix to invert matching of the whole list. The "no" could | |
6ad929bb | 235 | * also be used for individual items in the @pattern list. So, |
3d735589 | 236 | * "nofoo,bar" has the same meaning as "nofoo,nobar". |
abc9c0f7 | 237 | * |
3d735589 KZ |
238 | * "bar" : "nofoo,bar" -> False (global "no" prefix) |
239 | * | |
240 | * "bar" : "foo,bar" -> True | |
abc9c0f7 | 241 | * |
abc9c0f7 KZ |
242 | * "bar" : "foo,nobar" -> False |
243 | * | |
244 | * Returns: 1 if type is matching, else 0. This function also returns | |
245 | * 0 if @pattern is NULL and @type is non-NULL. | |
246 | */ | |
247 | int mnt_match_fstype(const char *type, const char *pattern) | |
248 | { | |
12089155 | 249 | return match_fstype(type, pattern); |
abc9c0f7 KZ |
250 | } |
251 | ||
252 | ||
253 | /* Returns 1 if needle found or noneedle not found in haystack | |
254 | * Otherwise returns 0 | |
255 | */ | |
256 | static int check_option(const char *haystack, size_t len, | |
257 | const char *needle, size_t needle_len) | |
258 | { | |
259 | const char *p; | |
260 | int no = 0; | |
261 | ||
262 | if (needle_len >= 2 && !strncmp(needle, "no", 2)) { | |
263 | no = 1; | |
264 | needle += 2; | |
265 | needle_len -= 2; | |
266 | } | |
267 | ||
268 | for (p = haystack; p && p < haystack + len; p++) { | |
269 | char *sep = strchr(p, ','); | |
7fc6d2b8 KZ |
270 | size_t plen = sep ? (size_t) (sep - p) : |
271 | len - (p - haystack); | |
abc9c0f7 KZ |
272 | |
273 | if (plen == needle_len) { | |
274 | if (!strncmp(p, needle, plen)) | |
275 | return !no; /* foo or nofoo was found */ | |
276 | } | |
277 | p += plen; | |
278 | } | |
279 | ||
280 | return no; /* foo or nofoo was not found */ | |
281 | } | |
282 | ||
283 | /** | |
284 | * mnt_match_options: | |
285 | * @optstr: options string | |
6ad929bb | 286 | * @pattern: comma delimited list of options |
abc9c0f7 KZ |
287 | * |
288 | * The "no" could used for individual items in the @options list. The "no" | |
6ad929bb | 289 | * prefix does not have a global meaning. |
abc9c0f7 KZ |
290 | * |
291 | * Unlike fs type matching, nonetdev,user and nonetdev,nouser have | |
292 | * DIFFERENT meanings; each option is matched explicitly as specified. | |
293 | * | |
3d735589 KZ |
294 | * "xxx,yyy,zzz" : "nozzz" -> False |
295 | * | |
296 | * "xxx,yyy,zzz" : "xxx,noeee" -> True | |
abc9c0f7 KZ |
297 | * |
298 | * Returns: 1 if pattern is matching, else 0. This function also returns 0 | |
299 | * if @pattern is NULL and @optstr is non-NULL. | |
300 | */ | |
301 | int mnt_match_options(const char *optstr, const char *pattern) | |
302 | { | |
303 | const char *p; | |
304 | size_t len, optstr_len = 0; | |
305 | ||
306 | if (!pattern && !optstr) | |
307 | return 1; | |
308 | if (!pattern) | |
309 | return 0; | |
310 | ||
311 | len = strlen(pattern); | |
312 | if (optstr) | |
313 | optstr_len = strlen(optstr); | |
314 | ||
315 | for (p = pattern; p < pattern + len; p++) { | |
316 | char *sep = strchr(p, ','); | |
7fc6d2b8 KZ |
317 | size_t plen = sep ? (size_t) (sep - p) : |
318 | len - (p - pattern); | |
abc9c0f7 KZ |
319 | |
320 | if (!plen) | |
321 | continue; /* if two ',' appear in a row */ | |
322 | ||
323 | if (!check_option(optstr, optstr_len, p, plen)) | |
324 | return 0; /* any match failure means failure */ | |
325 | ||
326 | p += plen; | |
327 | } | |
328 | ||
329 | /* no match failures in list means success */ | |
330 | return 1; | |
331 | } | |
332 | ||
97e23b5e KZ |
333 | void mnt_free_filesystems(char **filesystems) |
334 | { | |
335 | char **p; | |
336 | ||
337 | if (!filesystems) | |
338 | return; | |
339 | for (p = filesystems; *p; p++) | |
340 | free(*p); | |
341 | free(filesystems); | |
342 | } | |
343 | ||
344 | static int add_filesystem(char ***filesystems, char *name) | |
345 | { | |
346 | int n = 0; | |
347 | ||
348 | assert(filesystems); | |
349 | assert(name); | |
350 | ||
351 | if (*filesystems) { | |
352 | char **p; | |
353 | for (n = 0, p = *filesystems; *p; p++, n++) { | |
354 | if (strcmp(*p, name) == 0) | |
355 | return 0; | |
356 | } | |
357 | } | |
358 | ||
359 | #define MYCHUNK 16 | |
360 | ||
361 | if (n == 0 || !((n + 1) % MYCHUNK)) { | |
362 | size_t items = ((n + 1 + MYCHUNK) / MYCHUNK) * MYCHUNK; | |
363 | char **x = realloc(*filesystems, items * sizeof(char *)); | |
364 | ||
365 | if (!x) | |
366 | goto err; | |
367 | *filesystems = x; | |
368 | } | |
369 | name = strdup(name); | |
370 | if (!name) | |
371 | goto err; | |
372 | (*filesystems)[n] = name; | |
373 | (*filesystems)[n + 1] = NULL; | |
374 | return 0; | |
375 | err: | |
376 | mnt_free_filesystems(*filesystems); | |
377 | return -ENOMEM; | |
378 | } | |
379 | ||
380 | static int get_filesystems(const char *filename, char ***filesystems, const char *pattern) | |
381 | { | |
382 | FILE *f; | |
383 | char line[128]; | |
384 | ||
385 | f = fopen(filename, "r"); | |
386 | if (!f) | |
387 | return 0; | |
388 | ||
389 | while (fgets(line, sizeof(line), f)) { | |
390 | char name[sizeof(line)]; | |
391 | int rc; | |
392 | ||
393 | if (*line == '#' || strncmp(line, "nodev", 5) == 0) | |
394 | continue; | |
395 | if (sscanf(line, " %128[^\n ]\n", name) != 1) | |
396 | continue; | |
397 | if (pattern && !mnt_match_fstype(name, pattern)) | |
398 | continue; | |
399 | rc = add_filesystem(filesystems, name); | |
400 | if (rc) | |
401 | return rc; | |
402 | } | |
403 | return 0; | |
404 | } | |
405 | ||
406 | int mnt_get_filesystems(char ***filesystems, const char *pattern) | |
407 | { | |
408 | int rc; | |
409 | ||
410 | if (!filesystems) | |
411 | return -EINVAL; | |
412 | *filesystems = NULL; | |
413 | ||
414 | rc = get_filesystems(_PATH_FILESYSTEMS, filesystems, pattern); | |
415 | if (rc) | |
416 | return rc; | |
417 | return get_filesystems(_PATH_PROC_FILESYSTEMS, filesystems, pattern); | |
418 | } | |
419 | ||
69b7e41e KZ |
420 | /* |
421 | * Returns allocated string with username or NULL. | |
422 | */ | |
423 | char *mnt_get_username(const uid_t uid) | |
424 | { | |
425 | struct passwd pwd; | |
426 | struct passwd *res; | |
282a58fd | 427 | #ifdef _SC_GETPW_R_SIZE_MAX |
69b7e41e | 428 | size_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
282a58fd KZ |
429 | #else |
430 | size_t sz = 0; | |
431 | #endif | |
69b7e41e KZ |
432 | char *buf, *username = NULL; |
433 | ||
434 | if (sz <= 0) | |
435 | sz = 16384; /* Should be more than enough */ | |
436 | ||
437 | buf = malloc(sz); | |
438 | if (!buf) | |
439 | return NULL; | |
440 | ||
441 | if (!getpwuid_r(uid, &pwd, buf, sz, &res) && res) | |
442 | username = strdup(pwd.pw_name); | |
443 | ||
444 | free(buf); | |
445 | return username; | |
446 | } | |
abc9c0f7 | 447 | |
a1e8af75 KZ |
448 | int mnt_get_uid(const char *username, uid_t *uid) |
449 | { | |
188dc15a | 450 | int rc = -1; |
a1e8af75 KZ |
451 | struct passwd pwd; |
452 | struct passwd *pw; | |
453 | size_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); | |
454 | char *buf; | |
455 | ||
188dc15a KZ |
456 | if (!username || !uid) |
457 | return -EINVAL; | |
a1e8af75 KZ |
458 | if (sz <= 0) |
459 | sz = 16384; /* Should be more than enough */ | |
460 | ||
461 | buf = malloc(sz); | |
462 | if (!buf) | |
463 | return -ENOMEM; | |
464 | ||
188dc15a | 465 | if (!getpwnam_r(username, &pwd, buf, sz, &pw) && pw) { |
a1e8af75 | 466 | *uid= pw->pw_uid; |
188dc15a KZ |
467 | rc = 0; |
468 | } else { | |
469 | DBG(UTILS, mnt_debug( | |
470 | "cannot convert '%s' username to UID", username)); | |
471 | } | |
a1e8af75 KZ |
472 | |
473 | free(buf); | |
188dc15a | 474 | return rc; |
a1e8af75 KZ |
475 | } |
476 | ||
477 | int mnt_get_gid(const char *groupname, gid_t *gid) | |
478 | { | |
188dc15a | 479 | int rc = -1; |
a1e8af75 KZ |
480 | struct group grp; |
481 | struct group *gr; | |
482 | size_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); | |
483 | char *buf; | |
484 | ||
188dc15a KZ |
485 | if (!groupname || !gid) |
486 | return -EINVAL; | |
a1e8af75 KZ |
487 | if (sz <= 0) |
488 | sz = 16384; /* Should be more than enough */ | |
489 | ||
490 | buf = malloc(sz); | |
491 | if (!buf) | |
492 | return -ENOMEM; | |
493 | ||
188dc15a | 494 | if (!getgrnam_r(groupname, &grp, buf, sz, &gr) && gr) { |
a1e8af75 | 495 | *gid= gr->gr_gid; |
188dc15a KZ |
496 | rc = 0; |
497 | } else { | |
498 | DBG(UTILS, mnt_debug( | |
499 | "cannot convert '%s' groupname to GID", groupname)); | |
500 | } | |
a1e8af75 KZ |
501 | |
502 | free(buf); | |
188dc15a KZ |
503 | return rc; |
504 | } | |
505 | ||
506 | int mnt_in_group(gid_t gid) | |
507 | { | |
508 | int rc = 0, n, i; | |
509 | gid_t *grps = NULL; | |
510 | ||
511 | if (getgid() == gid) | |
512 | return 1; | |
513 | ||
514 | n = getgroups(0, NULL); | |
515 | if (n <= 0) | |
516 | goto done; | |
517 | ||
518 | grps = malloc(n * sizeof(*grps)); | |
519 | if (!grps) | |
520 | goto done; | |
521 | ||
522 | if (getgroups(n, grps) == n) { | |
523 | for (i = 0; i < n; i++) { | |
524 | if (grps[i] == gid) { | |
525 | rc = 1; | |
526 | break; | |
527 | } | |
528 | } | |
529 | } | |
530 | done: | |
531 | free(grps); | |
532 | return rc; | |
a1e8af75 KZ |
533 | } |
534 | ||
1d0cd73f KZ |
535 | static int try_write(const char *filename) |
536 | { | |
537 | int fd; | |
538 | ||
539 | if (!filename) | |
540 | return -EINVAL; | |
541 | ||
b0bb8fb6 KZ |
542 | fd = open(filename, O_RDWR|O_CREAT, S_IWUSR| \ |
543 | S_IRUSR|S_IRGRP|S_IROTH); | |
1d0cd73f KZ |
544 | if (fd >= 0) { |
545 | close(fd); | |
546 | return 0; | |
547 | } | |
548 | return -errno; | |
549 | } | |
550 | ||
551 | /** | |
552 | * mnt_has_regular_mtab: | |
553 | * @mtab: returns path to mtab | |
554 | * @writable: returns 1 if the file is writable | |
555 | * | |
556 | * If the file does not exist and @writable argument is not NULL then it will | |
557 | * try to create the file | |
558 | * | |
6ad929bb | 559 | * Returns: 1 if /etc/mtab is a regular file, and 0 in case of error (check |
1d0cd73f | 560 | * errno for more details). |
0532ba1d | 561 | */ |
1d0cd73f | 562 | int mnt_has_regular_mtab(const char **mtab, int *writable) |
0532ba1d KZ |
563 | { |
564 | struct stat st; | |
70bf97ae | 565 | int rc; |
1d0cd73f | 566 | const char *filename = mtab && *mtab ? *mtab : mnt_get_mtab_path(); |
0532ba1d | 567 | |
7c118af7 KZ |
568 | if (writable) |
569 | *writable = 0; | |
70bf97ae | 570 | if (mtab && !*mtab) |
1d0cd73f KZ |
571 | *mtab = filename; |
572 | ||
573 | DBG(UTILS, mnt_debug("mtab: %s", filename)); | |
70bf97ae | 574 | |
1d0cd73f KZ |
575 | rc = lstat(filename, &st); |
576 | ||
577 | if (rc == 0) { | |
578 | /* file exist */ | |
579 | if (S_ISREG(st.st_mode)) { | |
580 | if (writable) | |
581 | *writable = !try_write(filename); | |
582 | return 1; | |
583 | } | |
7c118af7 | 584 | goto done; |
1d0cd73f KZ |
585 | } |
586 | ||
587 | /* try to create the file */ | |
70bf97ae | 588 | if (writable) { |
1d0cd73f | 589 | *writable = !try_write(filename); |
7c118af7 KZ |
590 | if (*writable) |
591 | return 1; | |
1d0cd73f KZ |
592 | } |
593 | ||
7c118af7 KZ |
594 | done: |
595 | DBG(UTILS, mnt_debug("%s: irregular/non-writable", filename)); | |
1d0cd73f KZ |
596 | return 0; |
597 | } | |
7c118af7 | 598 | |
77417bc0 KZ |
599 | /* |
600 | * Don't export this to libmount API -- utab is private library stuff. | |
1d0cd73f KZ |
601 | * |
602 | * If the file does not exist and @writable argument is not NULL then it will | |
a362ae60 | 603 | * try to create the directory (e.g. /run/mount) and the file. |
1d0cd73f | 604 | * |
a362ae60 | 605 | * Returns: 1 if utab is a regular file, and 0 in case of |
6f5788c5 | 606 | * error (check errno for more details). |
1d0cd73f | 607 | */ |
1d0cd73f KZ |
608 | int mnt_has_regular_utab(const char **utab, int *writable) |
609 | { | |
610 | struct stat st; | |
611 | int rc; | |
612 | const char *filename = utab && *utab ? *utab : mnt_get_utab_path(); | |
613 | ||
7c118af7 KZ |
614 | if (writable) |
615 | *writable = 0; | |
1d0cd73f KZ |
616 | if (utab && !*utab) |
617 | *utab = filename; | |
618 | ||
619 | DBG(UTILS, mnt_debug("utab: %s", filename)); | |
620 | ||
621 | rc = lstat(filename, &st); | |
622 | ||
623 | if (rc == 0) { | |
624 | /* file exist */ | |
625 | if (S_ISREG(st.st_mode)) { | |
626 | if (writable) | |
7c118af7 | 627 | *writable = !try_write(filename); |
1d0cd73f | 628 | return 1; |
70bf97ae | 629 | } |
7c118af7 | 630 | goto done; /* it's not regular file */ |
70bf97ae | 631 | } |
1d0cd73f KZ |
632 | |
633 | if (writable) { | |
634 | char *dirname = strdup(filename); | |
635 | ||
636 | if (!dirname) | |
7c118af7 | 637 | goto done; |
1d0cd73f KZ |
638 | |
639 | stripoff_last_component(dirname); /* remove filename */ | |
640 | ||
b0bb8fb6 KZ |
641 | rc = mkdir(dirname, S_IWUSR| |
642 | S_IRUSR|S_IRGRP|S_IROTH| | |
643 | S_IXUSR|S_IXGRP|S_IXOTH); | |
1d0cd73f KZ |
644 | free(dirname); |
645 | if (rc && errno != EEXIST) | |
7c118af7 | 646 | goto done; /* probably EACCES */ |
1d0cd73f KZ |
647 | |
648 | *writable = !try_write(filename); | |
7c118af7 KZ |
649 | if (*writable) |
650 | return 1; | |
1d0cd73f | 651 | } |
7c118af7 KZ |
652 | done: |
653 | DBG(UTILS, mnt_debug("%s: irregular/non-writable file", filename)); | |
1d0cd73f | 654 | return 0; |
0532ba1d KZ |
655 | } |
656 | ||
3a5b1b1d KZ |
657 | /** |
658 | * mnt_get_fstab_path: | |
659 | * | |
660 | * Returns: path to /etc/fstab or $LIBMOUNT_FSTAB. | |
661 | */ | |
662 | const char *mnt_get_fstab_path(void) | |
663 | { | |
035507c8 | 664 | const char *p = safe_getenv("LIBMOUNT_FSTAB"); |
3a5b1b1d KZ |
665 | return p ? : _PATH_MNTTAB; |
666 | } | |
667 | ||
668 | /** | |
669 | * mnt_get_mtab_path: | |
670 | * | |
b37dd175 | 671 | * This function returns *default* location of the mtab file. The result does |
0f32f1e2 | 672 | * not have to be writable. See also mnt_has_regular_mtab(). |
3a5b1b1d KZ |
673 | * |
674 | * Returns: path to /etc/mtab or $LIBMOUNT_MTAB. | |
675 | */ | |
676 | const char *mnt_get_mtab_path(void) | |
677 | { | |
035507c8 | 678 | const char *p = safe_getenv("LIBMOUNT_MTAB"); |
3a5b1b1d KZ |
679 | return p ? : _PATH_MOUNTED; |
680 | } | |
681 | ||
77417bc0 KZ |
682 | /* |
683 | * Don't export this to libmount API -- utab is private library stuff. | |
be1a5180 | 684 | * |
a362ae60 | 685 | * Returns: path to /run/mount/utab (or /dev/.mount/utab) or $LIBMOUNT_UTAB. |
b37dd175 KZ |
686 | */ |
687 | const char *mnt_get_utab_path(void) | |
688 | { | |
a362ae60 | 689 | struct stat st; |
035507c8 | 690 | const char *p = safe_getenv("LIBMOUNT_UTAB"); |
a362ae60 KZ |
691 | |
692 | if (p) | |
693 | return p; | |
694 | ||
695 | if (stat(MNT_RUNTIME_TOPDIR, &st) == 0) | |
696 | return MNT_PATH_UTAB; | |
697 | ||
698 | return MNT_PATH_UTAB_OLD; | |
b37dd175 KZ |
699 | } |
700 | ||
d1be0c34 | 701 | |
b37dd175 KZ |
702 | /* returns file descriptor or -errno, @name returns uniques filename |
703 | */ | |
4b6cf485 | 704 | int mnt_open_uniq_filename(const char *filename, char **name) |
b37dd175 KZ |
705 | { |
706 | int rc, fd; | |
707 | char *n; | |
708 | ||
709 | assert(filename); | |
710 | ||
711 | if (name) | |
712 | *name = NULL; | |
713 | ||
714 | rc = asprintf(&n, "%s.XXXXXX", filename); | |
715 | if (rc <= 0) | |
716 | return -errno; | |
717 | ||
4b6cf485 | 718 | fd = mkstemp(n); |
b37dd175 KZ |
719 | if (fd >= 0 && name) |
720 | *name = n; | |
721 | else | |
722 | free(n); | |
723 | ||
724 | return fd < 0 ? -errno : fd; | |
725 | } | |
0bb44be3 | 726 | |
0bb44be3 KZ |
727 | char *mnt_get_mountpoint(const char *path) |
728 | { | |
729 | char *mnt = strdup(path); | |
730 | struct stat st; | |
731 | dev_t dir, base; | |
732 | ||
733 | if (!mnt) | |
734 | return NULL; | |
735 | if (*mnt == '/' && *(mnt + 1) == '\0') | |
9758c88a | 736 | goto done; |
0bb44be3 KZ |
737 | |
738 | if (stat(mnt, &st)) | |
739 | goto err; | |
740 | base = st.st_dev; | |
741 | ||
742 | do { | |
743 | char *p = stripoff_last_component(mnt); | |
744 | ||
745 | if (!p) | |
746 | break; | |
747 | if (stat(*mnt ? mnt : "/", &st)) | |
748 | goto err; | |
749 | dir = st.st_dev; | |
750 | if (dir != base) { | |
751 | *(p - 1) = '/'; | |
9758c88a | 752 | goto done; |
0bb44be3 KZ |
753 | } |
754 | base = dir; | |
755 | } while (mnt && *(mnt + 1) != '\0'); | |
756 | ||
757 | memcpy(mnt, "/", 2); | |
9758c88a | 758 | done: |
f84fa6f7 | 759 | DBG(UTILS, mnt_debug("%s mountpoint is %s", path, mnt)); |
9758c88a | 760 | return mnt; |
0bb44be3 KZ |
761 | err: |
762 | free(mnt); | |
763 | return NULL; | |
764 | } | |
765 | ||
9758c88a | 766 | char *mnt_get_fs_root(const char *path, const char *mnt) |
0bb44be3 | 767 | { |
f84fa6f7 | 768 | char *m = (char *) mnt, *res; |
0bb44be3 KZ |
769 | const char *p; |
770 | size_t sz; | |
771 | ||
9758c88a KZ |
772 | if (!m) |
773 | m = mnt_get_mountpoint(path); | |
774 | if (!m) | |
0bb44be3 KZ |
775 | return NULL; |
776 | ||
9758c88a | 777 | sz = strlen(m); |
0bb44be3 KZ |
778 | p = sz > 1 ? path + sz : path; |
779 | ||
9758c88a KZ |
780 | if (m != mnt) |
781 | free(m); | |
0bb44be3 | 782 | |
f84fa6f7 KZ |
783 | res = *p ? strdup(p) : strdup("/"); |
784 | DBG(UTILS, mnt_debug("%s fs-root is %s", path, res)); | |
785 | return res; | |
0bb44be3 KZ |
786 | } |
787 | ||
abc9c0f7 | 788 | #ifdef TEST_PROGRAM |
68164f6c | 789 | int test_match_fstype(struct libmnt_test *ts, int argc, char *argv[]) |
abc9c0f7 KZ |
790 | { |
791 | char *type = argv[1]; | |
792 | char *pattern = argv[2]; | |
793 | ||
794 | printf("%s\n", mnt_match_fstype(type, pattern) ? "MATCH" : "NOT-MATCH"); | |
795 | return 0; | |
796 | } | |
797 | ||
68164f6c | 798 | int test_match_options(struct libmnt_test *ts, int argc, char *argv[]) |
abc9c0f7 KZ |
799 | { |
800 | char *optstr = argv[1]; | |
801 | char *pattern = argv[2]; | |
802 | ||
803 | printf("%s\n", mnt_match_options(optstr, pattern) ? "MATCH" : "NOT-MATCH"); | |
804 | return 0; | |
805 | } | |
806 | ||
68164f6c | 807 | int test_startswith(struct libmnt_test *ts, int argc, char *argv[]) |
b49103ed KZ |
808 | { |
809 | char *optstr = argv[1]; | |
810 | char *pattern = argv[2]; | |
811 | ||
812 | printf("%s\n", startswith(optstr, pattern) ? "YES" : "NOT"); | |
813 | return 0; | |
814 | } | |
815 | ||
68164f6c | 816 | int test_endswith(struct libmnt_test *ts, int argc, char *argv[]) |
b49103ed KZ |
817 | { |
818 | char *optstr = argv[1]; | |
819 | char *pattern = argv[2]; | |
820 | ||
821 | printf("%s\n", endswith(optstr, pattern) ? "YES" : "NOT"); | |
822 | return 0; | |
823 | } | |
824 | ||
68164f6c | 825 | int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[]) |
0bb44be3 KZ |
826 | { |
827 | char *path = canonicalize_path(argv[1]), | |
828 | *mnt = path ? mnt_get_mountpoint(path) : NULL; | |
829 | ||
830 | printf("%s: %s\n", argv[1], mnt ? : "unknown"); | |
831 | free(mnt); | |
832 | free(path); | |
833 | return 0; | |
834 | } | |
835 | ||
68164f6c | 836 | int test_fsroot(struct libmnt_test *ts, int argc, char *argv[]) |
0bb44be3 KZ |
837 | { |
838 | char *path = canonicalize_path(argv[1]), | |
d672fb26 | 839 | *mnt = path ? mnt_get_fs_root(path, NULL) : NULL; |
0bb44be3 KZ |
840 | |
841 | printf("%s: %s\n", argv[1], mnt ? : "unknown"); | |
842 | free(mnt); | |
843 | free(path); | |
844 | return 0; | |
845 | } | |
abc9c0f7 | 846 | |
68164f6c | 847 | int test_filesystems(struct libmnt_test *ts, int argc, char *argv[]) |
97e23b5e KZ |
848 | { |
849 | char **filesystems = NULL; | |
850 | int rc; | |
851 | ||
852 | rc = mnt_get_filesystems(&filesystems, argc ? argv[1] : NULL); | |
853 | if (!rc) { | |
854 | char **p; | |
855 | for (p = filesystems; *p; p++) | |
856 | printf("%s\n", *p); | |
857 | mnt_free_filesystems(filesystems); | |
858 | } | |
859 | return rc; | |
860 | } | |
861 | ||
66bb8267 KZ |
862 | int test_chdir(struct libmnt_test *ts, int argc, char *argv[]) |
863 | { | |
864 | int rc; | |
865 | char *path = canonicalize_path(argv[1]), | |
866 | *last = NULL; | |
867 | ||
868 | if (!path) | |
869 | return -errno; | |
870 | ||
871 | rc = mnt_chdir_to_parent(path, &last); | |
872 | if (!rc) { | |
873 | printf("path='%s', abs='%s', last='%s'\n", | |
874 | argv[1], path, last); | |
875 | } | |
876 | free(path); | |
877 | free(last); | |
878 | return rc; | |
879 | } | |
880 | ||
881 | ||
abc9c0f7 KZ |
882 | int main(int argc, char *argv[]) |
883 | { | |
68164f6c | 884 | struct libmnt_test tss[] = { |
abc9c0f7 KZ |
885 | { "--match-fstype", test_match_fstype, "<type> <pattern> FS types matching" }, |
886 | { "--match-options", test_match_options, "<options> <pattern> options matching" }, | |
97e23b5e | 887 | { "--filesystems", test_filesystems, "[<pattern>] list /{etc,proc}/filesystems" }, |
b49103ed KZ |
888 | { "--starts-with", test_startswith, "<string> <prefix>" }, |
889 | { "--ends-with", test_endswith, "<string> <prefix>" }, | |
0bb44be3 KZ |
890 | { "--mountpoint", test_mountpoint, "<path>" }, |
891 | { "--fs-root", test_fsroot, "<path>" }, | |
66bb8267 | 892 | { "--cd-parent", test_chdir, "<path>" }, |
abc9c0f7 KZ |
893 | { NULL } |
894 | }; | |
895 | ||
896 | return mnt_run_test(tss, argc, argv); | |
897 | } | |
898 | ||
899 | #endif /* TEST_PROGRAM */ |