blob: 12b2cfce4e6fe9b140a24196c48f151dbf141fc2 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include <mntent.h>
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000012
Eric Andersenaad1a882001-03-16 22:47:14 +000013/*
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 Vlasenko09e63bb2009-07-05 04:50:36 +020020struct mntent* FAST_FUNC find_mount_point(const char *name)
Eric Andersenaad1a882001-03-16 22:47:14 +000021{
22 struct stat s;
23 dev_t mountDevice;
24 FILE *mountTable;
25 struct mntent *mountEntry;
26
27 if (stat(name, &s) != 0)
Denys Vlasenko09e63bb2009-07-05 04:50:36 +020028 return NULL;
Eric Andersenaad1a882001-03-16 22:47:14 +000029
Denys Vlasenko09e63bb2009-07-05 04:50:36 +020030 if (S_ISBLK(s.st_mode))
Eric Andersenaad1a882001-03-16 22:47:14 +000031 mountDevice = s.st_rdev;
32 else
33 mountDevice = s.st_dev;
34
35
Denys Vlasenko09e63bb2009-07-05 04:50:36 +020036 mountTable = setmntent(bb_path_mtab_file, "r");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000037 if (!mountTable)
Eric Andersenaad1a882001-03-16 22:47:14 +000038 return 0;
39
Denys Vlasenko09e63bb2009-07-05 04:50:36 +020040 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 Vlasenkoc6ce8732006-11-29 18:15:52 +000047 if (strcmp(name, mountEntry->mnt_dir) == 0
48 || strcmp(name, mountEntry->mnt_fsname) == 0
49 ) { /* String match. */
Eric Andersenaad1a882001-03-16 22:47:14 +000050 break;
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000051 }
Denys Vlasenko09e63bb2009-07-05 04:50:36 +020052 /* Match the device. */
53 if (stat(mountEntry->mnt_fsname, &s) == 0 && s.st_rdev == mountDevice)
Eric Andersenaad1a882001-03-16 22:47:14 +000054 break;
Denys Vlasenko09e63bb2009-07-05 04:50:36 +020055 /* Match the directory's mount point. */
56 if (stat(mountEntry->mnt_dir, &s) == 0 && s.st_dev == mountDevice)
Eric Andersenaad1a882001-03-16 22:47:14 +000057 break;
58 }
59 endmntent(mountTable);
60 return mountEntry;
61}