Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include <mntent.h> |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 11 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | /* |
| 13 | * Given a block device, find the mount table entry if that block device |
| 14 | * is mounted. |
| 15 | * |
| 16 | * Given any other file (or directory), find the mount table entry for its |
| 17 | * filesystem. |
| 18 | */ |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 19 | struct mntent* FAST_FUNC find_mount_point(const char *name, int subdir_too) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | { |
| 21 | struct stat s; |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 22 | FILE *mtab_fp; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | struct mntent *mountEntry; |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 24 | dev_t devno_of_name; |
| 25 | bool block_dev; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | |
| 27 | if (stat(name, &s) != 0) |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame] | 28 | return NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 30 | devno_of_name = s.st_dev; |
| 31 | block_dev = 0; |
Javier Viguera | ce4f39a | 2012-01-18 01:49:11 +0100 | [diff] [blame] | 32 | /* Why S_ISCHR? - UBI volumes use char devices, not block */ |
| 33 | if (S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode)) { |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 34 | devno_of_name = s.st_rdev; |
| 35 | block_dev = 1; |
| 36 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 38 | mtab_fp = setmntent(bb_path_mtab_file, "r"); |
| 39 | if (!mtab_fp) |
| 40 | return NULL; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 41 | |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 42 | while ((mountEntry = getmntent(mtab_fp)) != NULL) { |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame] | 43 | /* rootfs mount in Linux 2.6 exists always, |
| 44 | * and it makes sense to always ignore it. |
| 45 | * Otherwise people can't reference their "real" root! */ |
Lauri Kasanen | 55ae0e9 | 2011-01-31 06:27:35 +0100 | [diff] [blame] | 46 | if (ENABLE_FEATURE_SKIP_ROOTFS && strcmp(mountEntry->mnt_fsname, "rootfs") == 0) |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame] | 47 | continue; |
| 48 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 49 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 50 | || strcmp(name, mountEntry->mnt_fsname) == 0 |
| 51 | ) { /* String match. */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 52 | break; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 53 | } |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 54 | |
| 55 | if (!(subdir_too || block_dev)) |
| 56 | continue; |
| 57 | |
| 58 | /* Is device's dev_t == name's dev_t? */ |
| 59 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == devno_of_name) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 60 | break; |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame] | 61 | /* Match the directory's mount point. */ |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 62 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == devno_of_name) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 63 | break; |
| 64 | } |
Denys Vlasenko | 6ae6426 | 2009-07-18 16:22:26 +0200 | [diff] [blame] | 65 | endmntent(mtab_fp); |
| 66 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 67 | return mountEntry; |
| 68 | } |