ismounted.c revision d5f858dd7f02532ad90e4121537c358e86d0eecf
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					strncpy(mtpt, "/", mtlen);
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#elif defined(HAVE_GETMNTINFO)
121
122static errcode_t check_getmntinfo(const char *file, int *mount_flags,
123				  char *mtpt, int mtlen)
124{
125	struct statfs *mp;
126        int    len, n;
127        const  char   *s1;
128	char	*s2;
129
130        n = getmntinfo(&mp, MNT_NOWAIT);
131        if (n == 0)
132		return errno;
133
134        len = sizeof(_PATH_DEV) - 1;
135        s1 = file;
136        if (strncmp(_PATH_DEV, s1, len) == 0)
137                s1 += len;
138
139	*mount_flags = 0;
140        while (--n >= 0) {
141                s2 = mp->f_mntfromname;
142                if (strncmp(_PATH_DEV, s2, len) == 0) {
143                        s2 += len - 1;
144                        *s2 = 'r';
145                }
146                if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
147			*mount_flags = EXT2_MF_MOUNTED;
148			break;
149		}
150                ++mp;
151	}
152	if (mtpt)
153		strncpy(mtpt, mp->f_mntonname, mtlen);
154	return 0;
155}
156#endif /* HAVE_GETMNTINFO */
157
158/*
159 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
160 * otherwise.  If mtpt is non-NULL, the directory where the device is
161 * mounted is copied to where mtpt is pointing, up to mtlen
162 * characters.
163 */
164#ifdef __TURBOC__
165 #pragma argsused
166#endif
167errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
168				  char *mtpt, int mtlen)
169{
170#ifdef HAVE_MNTENT_H
171	return check_mntent(device, mount_flags, mtpt, mtlen);
172#else
173#ifdef HAVE_GETMNTINFO
174	return check_getmntinfo(device, mount_flags, mtpt, mtlen);
175#else
176#warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
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, and EXT2_MF_ROOT
186 *
187 */
188#ifdef __TURBOC__
189 #pragma argsused
190#endif
191errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
192{
193	return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
194}
195
196#ifdef DEBUG
197int main(int argc, char **argv)
198{
199	int	retval, mount_flags;
200
201	if (argc < 2) {
202		fprintf(stderr, "Usage: %s device\n", argv[0]);
203		exit(1);
204	}
205
206	retval = ext2fs_check_if_mounted(argv[1], &mount_flags);
207	if (retval) {
208		com_err(argv[0], retval,
209			"while calling ext2fs_check_if_mounted");
210		exit(1);
211	}
212	printf("Device %s reports flags %02x\n", argv[1], mount_flags);
213	if (mount_flags & EXT2_MF_MOUNTED)
214		printf("\t%s is mounted.\n", argv[1]);
215
216	if (mount_flags & EXT2_MF_READONLY)
217		printf("\t%s is read-only.\n", argv[1]);
218
219	if (mount_flags & EXT2_MF_ISROOT)
220		printf("\t%s is the root filesystem.\n", argv[1]);
221
222	exit(0);
223}
224#endif
225