ismounted.c revision cc86017b593ddfbd4d7a12ed8695e62998f30944
1/*
2 * ismounted.c --- Check to see if the filesystem was mounted
3 *
4 * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#if HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16#if HAVE_ERRNO_H
17#include <errno.h>
18#endif
19#include <fcntl.h>
20#ifdef HAVE_LINUX_FD_H
21#include <linux/fd.h>
22#endif
23#ifdef HAVE_MNTENT_H
24#include <mntent.h>
25#endif
26#ifdef HAVE_GETMNTINFO
27#include <paths.h>
28#include <sys/param.h>
29#include <sys/mount.h>
30#endif /* HAVE_GETMNTINFO */
31#include <string.h>
32#include <sys/stat.h>
33
34#include "ext2_fs.h"
35#include "ext2fs.h"
36
37#ifdef HAVE_MNTENT_H
38/*
39 * Helper function which checks a file in /etc/mtab format to see if a
40 * filesystem is mounted.  Returns an error if the file doesn't exist
41 * or can't be opened.
42 */
43static errcode_t check_mntent_file(const char *mtab_file, const char *file,
44				   int *mount_flags, char *mtpt, int mtlen)
45{
46	FILE * f;
47	struct mntent * mnt;
48	int	fd;
49
50	*mount_flags = 0;
51	if ((f = setmntent (mtab_file, "r")) == NULL)
52		return errno;
53	while ((mnt = getmntent (f)) != NULL)
54		if (strcmp(file, mnt->mnt_fsname) == 0)
55			break;
56
57	if (mnt == 0) {
58		struct stat st_root, st_file;
59		/*
60		 * Do an extra check to see if this is the root device.  We
61		 * can't trust /etc/fstab, and /proc/mounts will only list
62		 * /dev/root for the root filesystem.  Argh.  Instead we
63		 * check if the given device has the same major/minor number
64		 * as the device that the root directory is on.
65		 */
66		if (stat("/", &st_root) == 0 && stat(file, &st_file) == 0) {
67			if (st_root.st_dev == st_file.st_rdev) {
68				*mount_flags = EXT2_MF_MOUNTED;
69				if (mtpt)
70					strcpy(mtpt, "/");
71				goto is_root;
72			}
73		}
74		endmntent (f);
75		return 0;
76	}
77	*mount_flags = EXT2_MF_MOUNTED;
78
79	/* Check to see if the ro option is set */
80	if (hasmntopt(mnt, MNTOPT_RO))
81		*mount_flags |= EXT2_MF_READONLY;
82
83	if (mtpt)
84		strncpy(mtpt, mnt->mnt_dir, mtlen);
85	/*
86	 * Check to see if we're referring to the root filesystem.
87	 * If so, do a manual check to see if we can open /etc/mtab
88	 * read/write, since if the root is mounted read/only, the
89	 * contents of /etc/mtab may not be accurate.
90	 */
91	if (!strcmp(mnt->mnt_dir, "/")) {
92is_root:
93		*mount_flags |= EXT2_MF_ISROOT;
94		fd = open(MOUNTED, O_RDWR);
95		if (fd < 0) {
96			if (errno == EROFS)
97				*mount_flags |= EXT2_MF_READONLY;
98		} else
99			close(fd);
100	}
101	endmntent (f);
102	return 0;
103}
104
105static errcode_t check_mntent(const char *file, int *mount_flags,
106			      char *mtpt, int mtlen)
107{
108	errcode_t	retval;
109
110#ifdef __linux__
111	retval = check_mntent_file("/proc/mounts", file, mount_flags,
112				   mtpt, mtlen);
113	if (retval == 0)
114		return 0;
115#endif
116	retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
117	return retval;
118}
119
120#endif
121
122#ifdef HAVE_GETMNTINFO
123static errcode_t check_getmntinfo(const char *file, int *mount_flags,
124				  char *mtpt, int mtlen)
125{
126	struct statfs *mp;
127        int    len, n;
128        const  char   *s1;
129	char	*s2;
130
131        n = getmntinfo(&mp, MNT_NOWAIT);
132        if (n == 0)
133		return errno;
134
135        len = sizeof(_PATH_DEV) - 1;
136        s1 = file;
137        if (strncmp(_PATH_DEV, s1, len) == 0)
138                s1 += len;
139
140	*mount_flags = 0;
141        while (--n >= 0) {
142                s2 = mp->f_mntfromname;
143                if (strncmp(_PATH_DEV, s2, len) == 0) {
144                        s2 += len - 1;
145                        *s2 = 'r';
146                }
147                if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
148			*mount_flags = EXT2_MF_MOUNTED;
149			break;
150		}
151                ++mp;
152	}
153	if (mtpt)
154		strncpy(mtpt, mp->f_mntonname, mtlen);
155	return 0;
156}
157#endif /* HAVE_GETMNTINFO */
158
159/*
160 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
161 * otherwise.  If mtpt is non-NULL, the directory where the device is
162 * mounted is copied to where mtpt is pointing, up to mtlen
163 * characters.
164 */
165#ifdef __TURBOC__
166 #pragma argsused
167#endif
168errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
169				  char *mtpt, int mtlen)
170{
171#ifdef HAVE_MNTENT_H
172	return check_mntent(device, mount_flags, mtpt, mtlen);
173#else
174#ifdef HAVE_GETMNTINFO
175	return check_getmntinfo(device, mount_flags, mtpt, mtlen);
176#else
177	*mount_flags = 0;
178	return 0;
179#endif /* HAVE_GETMNTINFO */
180#endif /* HAVE_MNTENT_H */
181}
182
183/*
184 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED and
185 * EXT2_MF_READONLY
186 *
187 */
188#ifdef __TURBOC__
189 #pragma argsused
190#endif
191errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
192{
193#ifdef HAVE_MNTENT_H
194	return check_mntent(file, mount_flags, NULL, 0);
195#else
196#ifdef HAVE_GETMNTINFO
197	return check_getmntinfo(file, mount_flags, NULL, 0);
198#else
199	*mount_flags = 0;
200	return 0;
201#endif /* HAVE_GETMNTINFO */
202#endif /* HAVE_MNTENT_H */
203}
204
205#ifdef DEBUG
206int main(int argc, char **argv)
207{
208	blk_t	blocks;
209	int	retval, mount_flags;
210
211	if (argc < 2) {
212		fprintf(stderr, "Usage: %s device\n", argv[0]);
213		exit(1);
214	}
215
216	retval = ext2fs_check_if_mounted(argv[1], &mount_flags);
217	if (retval) {
218		com_err(argv[0], retval,
219			"while calling ext2fs_check_if_mounted");
220		exit(1);
221	}
222	printf("Device %s reports flags %02x\n", argv[1], mount_flags);
223	if (mount_flags & EXT2_MF_MOUNTED)
224		printf("\t%s is mounted.\n", argv[1]);
225
226	if (mount_flags & EXT2_MF_READONLY)
227		printf("\t%s is read-only.\n", argv[1]);
228
229	if (mount_flags & EXT2_MF_ISROOT)
230		printf("\t%s is the root filesystem.\n", argv[1]);
231
232	exit(0);
233}
234#endif
235