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