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 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 11 | #include <mntent.h> |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 12 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | /* |
| 14 | * Given a block device, find the mount table entry if that block device |
| 15 | * is mounted. |
| 16 | * |
| 17 | * Given any other file (or directory), find the mount table entry for its |
| 18 | * filesystem. |
| 19 | */ |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame^] | 20 | struct mntent* FAST_FUNC find_mount_point(const char *name) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 21 | { |
| 22 | struct stat s; |
| 23 | dev_t mountDevice; |
| 24 | FILE *mountTable; |
| 25 | struct mntent *mountEntry; |
| 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 | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame^] | 30 | if (S_ISBLK(s.st_mode)) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | mountDevice = s.st_rdev; |
| 32 | else |
| 33 | mountDevice = s.st_dev; |
| 34 | |
| 35 | |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame^] | 36 | mountTable = setmntent(bb_path_mtab_file, "r"); |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 37 | if (!mountTable) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 38 | return 0; |
| 39 | |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame^] | 40 | while ((mountEntry = getmntent(mountTable)) != NULL) { |
| 41 | /* rootfs mount in Linux 2.6 exists always, |
| 42 | * and it makes sense to always ignore it. |
| 43 | * Otherwise people can't reference their "real" root! */ |
| 44 | if (strcmp(mountEntry->mnt_fsname, "rootfs") == 0) |
| 45 | continue; |
| 46 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 47 | if (strcmp(name, mountEntry->mnt_dir) == 0 |
| 48 | || strcmp(name, mountEntry->mnt_fsname) == 0 |
| 49 | ) { /* String match. */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 50 | break; |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 51 | } |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame^] | 52 | /* Match the device. */ |
| 53 | if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 54 | break; |
Denys Vlasenko | 09e63bb | 2009-07-05 04:50:36 +0200 | [diff] [blame^] | 55 | /* Match the directory's mount point. */ |
| 56 | if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | endmntent(mountTable); |
| 60 | return mountEntry; |
| 61 | } |