xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/ismounted.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * ismounted.c --- Check to see if the filesystem was mounted
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
8*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker /* define BSD_SOURCE to make sure we get the major() macro */
13*6a54128fSAndroid Build Coastguard Worker #ifndef _BSD_SOURCE
14*6a54128fSAndroid Build Coastguard Worker #define _BSD_SOURCE
15*6a54128fSAndroid Build Coastguard Worker #endif
16*6a54128fSAndroid Build Coastguard Worker #ifndef _DEFAULT_SOURCE
17*6a54128fSAndroid Build Coastguard Worker #define _DEFAULT_SOURCE	/* since glibc 2.20 _SVID_SOURCE is deprecated */
18*6a54128fSAndroid Build Coastguard Worker #endif
19*6a54128fSAndroid Build Coastguard Worker 
20*6a54128fSAndroid Build Coastguard Worker #include "config.h"
21*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
22*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
23*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
24*6a54128fSAndroid Build Coastguard Worker #endif
25*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
26*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
27*6a54128fSAndroid Build Coastguard Worker #endif
28*6a54128fSAndroid Build Coastguard Worker #include <fcntl.h>
29*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_LINUX_FD_H
30*6a54128fSAndroid Build Coastguard Worker #include <linux/fd.h>
31*6a54128fSAndroid Build Coastguard Worker #endif
32*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_LINUX_LOOP_H
33*6a54128fSAndroid Build Coastguard Worker #include <linux/loop.h>
34*6a54128fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
35*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_LINUX_MAJOR_H
36*6a54128fSAndroid Build Coastguard Worker #include <linux/major.h>
37*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_LINUX_MAJOR_H */
38*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_LINUX_LOOP_H */
39*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_MNTENT_H
40*6a54128fSAndroid Build Coastguard Worker #include <mntent.h>
41*6a54128fSAndroid Build Coastguard Worker #endif
42*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETMNTINFO
43*6a54128fSAndroid Build Coastguard Worker #include <paths.h>
44*6a54128fSAndroid Build Coastguard Worker #include <sys/param.h>
45*6a54128fSAndroid Build Coastguard Worker #include <sys/mount.h>
46*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_GETMNTINFO */
47*6a54128fSAndroid Build Coastguard Worker #include <string.h>
48*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
49*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
50*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
51*6a54128fSAndroid Build Coastguard Worker #endif
52*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SYS_SYSMACROS_H
53*6a54128fSAndroid Build Coastguard Worker #include <sys/sysmacros.h>
54*6a54128fSAndroid Build Coastguard Worker #endif
55*6a54128fSAndroid Build Coastguard Worker 
56*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
57*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
58*6a54128fSAndroid Build Coastguard Worker #include "ext2fsP.h"
59*6a54128fSAndroid Build Coastguard Worker 
60*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SETMNTENT
61*6a54128fSAndroid Build Coastguard Worker /*
62*6a54128fSAndroid Build Coastguard Worker  * Check to see if a regular file is mounted.
63*6a54128fSAndroid Build Coastguard Worker  * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
64*6a54128fSAndroid Build Coastguard Worker  * because the name in /proc/mounts is a loopback device not a regular file.
65*6a54128fSAndroid Build Coastguard Worker  */
check_loop_mounted(const char * mnt_fsname,dev_t mnt_rdev,dev_t file_dev,ino_t file_ino)66*6a54128fSAndroid Build Coastguard Worker static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
67*6a54128fSAndroid Build Coastguard Worker 				dev_t file_dev, ino_t file_ino)
68*6a54128fSAndroid Build Coastguard Worker {
69*6a54128fSAndroid Build Coastguard Worker #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
70*6a54128fSAndroid Build Coastguard Worker 	struct loop_info64 loopinfo = {0, };
71*6a54128fSAndroid Build Coastguard Worker 	int loop_fd, ret;
72*6a54128fSAndroid Build Coastguard Worker 
73*6a54128fSAndroid Build Coastguard Worker 	if (major(mnt_rdev) == LOOP_MAJOR) {
74*6a54128fSAndroid Build Coastguard Worker 		loop_fd = open(mnt_fsname, O_RDONLY);
75*6a54128fSAndroid Build Coastguard Worker 		if (loop_fd < 0)
76*6a54128fSAndroid Build Coastguard Worker 			return -1;
77*6a54128fSAndroid Build Coastguard Worker 
78*6a54128fSAndroid Build Coastguard Worker 		ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
79*6a54128fSAndroid Build Coastguard Worker 		close(loop_fd);
80*6a54128fSAndroid Build Coastguard Worker 		if (ret < 0)
81*6a54128fSAndroid Build Coastguard Worker 			return -1;
82*6a54128fSAndroid Build Coastguard Worker 
83*6a54128fSAndroid Build Coastguard Worker 		if (file_dev == loopinfo.lo_device &&
84*6a54128fSAndroid Build Coastguard Worker 				file_ino == loopinfo.lo_inode)
85*6a54128fSAndroid Build Coastguard Worker 			return 1;
86*6a54128fSAndroid Build Coastguard Worker 	}
87*6a54128fSAndroid Build Coastguard Worker #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
88*6a54128fSAndroid Build Coastguard Worker 	return 0;
89*6a54128fSAndroid Build Coastguard Worker }
90*6a54128fSAndroid Build Coastguard Worker 
91*6a54128fSAndroid Build Coastguard Worker /*
92*6a54128fSAndroid Build Coastguard Worker  * Helper function which checks a file in /etc/mtab format to see if a
93*6a54128fSAndroid Build Coastguard Worker  * filesystem is mounted.  Returns an error if the file doesn't exist
94*6a54128fSAndroid Build Coastguard Worker  * or can't be opened.
95*6a54128fSAndroid Build Coastguard Worker  */
check_mntent_file(const char * mtab_file,const char * file,int * mount_flags,char * mtpt,int mtlen)96*6a54128fSAndroid Build Coastguard Worker static errcode_t check_mntent_file(const char *mtab_file, const char *file,
97*6a54128fSAndroid Build Coastguard Worker 				   int *mount_flags, char *mtpt, int mtlen)
98*6a54128fSAndroid Build Coastguard Worker {
99*6a54128fSAndroid Build Coastguard Worker 	struct mntent 	*mnt;
100*6a54128fSAndroid Build Coastguard Worker 	struct stat	st_buf, dir_st_buf;
101*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
102*6a54128fSAndroid Build Coastguard Worker 	dev_t		file_dev=0, file_rdev=0;
103*6a54128fSAndroid Build Coastguard Worker 	ino_t		file_ino=0;
104*6a54128fSAndroid Build Coastguard Worker 	FILE 		*f;
105*6a54128fSAndroid Build Coastguard Worker 	int		fd;
106*6a54128fSAndroid Build Coastguard Worker 
107*6a54128fSAndroid Build Coastguard Worker 	*mount_flags = 0;
108*6a54128fSAndroid Build Coastguard Worker 
109*6a54128fSAndroid Build Coastguard Worker 	if ((f = setmntent (mtab_file, "r")) == NULL) {
110*6a54128fSAndroid Build Coastguard Worker 		if (errno == ENOENT) {
111*6a54128fSAndroid Build Coastguard Worker 			if (getenv("EXT2FS_NO_MTAB_OK"))
112*6a54128fSAndroid Build Coastguard Worker 				return 0;
113*6a54128fSAndroid Build Coastguard Worker 			else
114*6a54128fSAndroid Build Coastguard Worker 				return EXT2_ET_NO_MTAB_FILE;
115*6a54128fSAndroid Build Coastguard Worker 		}
116*6a54128fSAndroid Build Coastguard Worker 		return errno;
117*6a54128fSAndroid Build Coastguard Worker 	}
118*6a54128fSAndroid Build Coastguard Worker 	if (stat(file, &st_buf) == 0) {
119*6a54128fSAndroid Build Coastguard Worker 		if (ext2fsP_is_disk_device(st_buf.st_mode)) {
120*6a54128fSAndroid Build Coastguard Worker #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
121*6a54128fSAndroid Build Coastguard Worker 			file_rdev = st_buf.st_rdev;
122*6a54128fSAndroid Build Coastguard Worker #endif	/* __GNU__ */
123*6a54128fSAndroid Build Coastguard Worker 		} else {
124*6a54128fSAndroid Build Coastguard Worker 			file_dev = st_buf.st_dev;
125*6a54128fSAndroid Build Coastguard Worker 			file_ino = st_buf.st_ino;
126*6a54128fSAndroid Build Coastguard Worker 		}
127*6a54128fSAndroid Build Coastguard Worker 	}
128*6a54128fSAndroid Build Coastguard Worker 	while ((mnt = getmntent (f)) != NULL) {
129*6a54128fSAndroid Build Coastguard Worker 		if (mnt->mnt_fsname[0] != '/')
130*6a54128fSAndroid Build Coastguard Worker 			continue;
131*6a54128fSAndroid Build Coastguard Worker 		if (strcmp(file, mnt->mnt_fsname) == 0) {
132*6a54128fSAndroid Build Coastguard Worker 			if (stat(mnt->mnt_dir, &st_buf) != 0)
133*6a54128fSAndroid Build Coastguard Worker 				continue;
134*6a54128fSAndroid Build Coastguard Worker 			if (file_rdev && (file_rdev != st_buf.st_dev)) {
135*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
136*6a54128fSAndroid Build Coastguard Worker 				printf("Bogus entry in %s!  "
137*6a54128fSAndroid Build Coastguard Worker 				       "(%s is not mounted on %s)\n",
138*6a54128fSAndroid Build Coastguard Worker 				       mtab_file, file, mnt->mnt_dir);
139*6a54128fSAndroid Build Coastguard Worker #endif /* DEBUG */
140*6a54128fSAndroid Build Coastguard Worker 				continue;
141*6a54128fSAndroid Build Coastguard Worker 			}
142*6a54128fSAndroid Build Coastguard Worker 			break;
143*6a54128fSAndroid Build Coastguard Worker 		}
144*6a54128fSAndroid Build Coastguard Worker 		if (stat(mnt->mnt_fsname, &st_buf) == 0) {
145*6a54128fSAndroid Build Coastguard Worker 			if (ext2fsP_is_disk_device(st_buf.st_mode)) {
146*6a54128fSAndroid Build Coastguard Worker #ifndef __GNU__
147*6a54128fSAndroid Build Coastguard Worker 				if (file_rdev &&
148*6a54128fSAndroid Build Coastguard Worker 				    (file_rdev == st_buf.st_rdev)) {
149*6a54128fSAndroid Build Coastguard Worker 					if (stat(mnt->mnt_dir,
150*6a54128fSAndroid Build Coastguard Worker 						 &dir_st_buf) != 0)
151*6a54128fSAndroid Build Coastguard Worker 						continue;
152*6a54128fSAndroid Build Coastguard Worker 					if (file_rdev == dir_st_buf.st_dev)
153*6a54128fSAndroid Build Coastguard Worker 						break;
154*6a54128fSAndroid Build Coastguard Worker 				}
155*6a54128fSAndroid Build Coastguard Worker 				if (check_loop_mounted(mnt->mnt_fsname,
156*6a54128fSAndroid Build Coastguard Worker 						st_buf.st_rdev, file_dev,
157*6a54128fSAndroid Build Coastguard Worker 						file_ino) == 1)
158*6a54128fSAndroid Build Coastguard Worker 					break;
159*6a54128fSAndroid Build Coastguard Worker #endif	/* __GNU__ */
160*6a54128fSAndroid Build Coastguard Worker 			} else {
161*6a54128fSAndroid Build Coastguard Worker 				if (file_dev && ((file_dev == st_buf.st_dev) &&
162*6a54128fSAndroid Build Coastguard Worker 						 (file_ino == st_buf.st_ino)))
163*6a54128fSAndroid Build Coastguard Worker 					break;
164*6a54128fSAndroid Build Coastguard Worker 			}
165*6a54128fSAndroid Build Coastguard Worker 		}
166*6a54128fSAndroid Build Coastguard Worker 	}
167*6a54128fSAndroid Build Coastguard Worker 
168*6a54128fSAndroid Build Coastguard Worker 	if (mnt == 0) {
169*6a54128fSAndroid Build Coastguard Worker #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
170*6a54128fSAndroid Build Coastguard Worker 		/*
171*6a54128fSAndroid Build Coastguard Worker 		 * Do an extra check to see if this is the root device.  We
172*6a54128fSAndroid Build Coastguard Worker 		 * can't trust /etc/mtab, and /proc/mounts will only list
173*6a54128fSAndroid Build Coastguard Worker 		 * /dev/root for the root filesystem.  Argh.  Instead we
174*6a54128fSAndroid Build Coastguard Worker 		 * check if the given device has the same major/minor number
175*6a54128fSAndroid Build Coastguard Worker 		 * as the device that the root directory is on.
176*6a54128fSAndroid Build Coastguard Worker 		 */
177*6a54128fSAndroid Build Coastguard Worker 		if (file_rdev && stat("/", &st_buf) == 0) {
178*6a54128fSAndroid Build Coastguard Worker 			if (st_buf.st_dev == file_rdev) {
179*6a54128fSAndroid Build Coastguard Worker 				*mount_flags = EXT2_MF_MOUNTED;
180*6a54128fSAndroid Build Coastguard Worker 				if (mtpt)
181*6a54128fSAndroid Build Coastguard Worker 					strncpy(mtpt, "/", mtlen);
182*6a54128fSAndroid Build Coastguard Worker 				goto is_root;
183*6a54128fSAndroid Build Coastguard Worker 			}
184*6a54128fSAndroid Build Coastguard Worker 		}
185*6a54128fSAndroid Build Coastguard Worker #endif	/* __GNU__ */
186*6a54128fSAndroid Build Coastguard Worker 		goto errout;
187*6a54128fSAndroid Build Coastguard Worker 	}
188*6a54128fSAndroid Build Coastguard Worker 	*mount_flags = EXT2_MF_MOUNTED;
189*6a54128fSAndroid Build Coastguard Worker 
190*6a54128fSAndroid Build Coastguard Worker #ifdef MNTOPT_RO
191*6a54128fSAndroid Build Coastguard Worker 	/* Check to see if the ro option is set */
192*6a54128fSAndroid Build Coastguard Worker 	if (hasmntopt(mnt, MNTOPT_RO))
193*6a54128fSAndroid Build Coastguard Worker 		*mount_flags |= EXT2_MF_READONLY;
194*6a54128fSAndroid Build Coastguard Worker #endif
195*6a54128fSAndroid Build Coastguard Worker 
196*6a54128fSAndroid Build Coastguard Worker 	if (mtpt)
197*6a54128fSAndroid Build Coastguard Worker 		strncpy(mtpt, mnt->mnt_dir, mtlen);
198*6a54128fSAndroid Build Coastguard Worker 	/*
199*6a54128fSAndroid Build Coastguard Worker 	 * Check to see if we're referring to the root filesystem.
200*6a54128fSAndroid Build Coastguard Worker 	 * If so, do a manual check to see if we can open /etc/mtab
201*6a54128fSAndroid Build Coastguard Worker 	 * read/write, since if the root is mounted read/only, the
202*6a54128fSAndroid Build Coastguard Worker 	 * contents of /etc/mtab may not be accurate.
203*6a54128fSAndroid Build Coastguard Worker 	 */
204*6a54128fSAndroid Build Coastguard Worker 	if (!strcmp(mnt->mnt_dir, "/")) {
205*6a54128fSAndroid Build Coastguard Worker is_root:
206*6a54128fSAndroid Build Coastguard Worker #define TEST_FILE "/.ismount-test-file"
207*6a54128fSAndroid Build Coastguard Worker 		*mount_flags |= EXT2_MF_ISROOT;
208*6a54128fSAndroid Build Coastguard Worker 		fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
209*6a54128fSAndroid Build Coastguard Worker 		if (fd < 0) {
210*6a54128fSAndroid Build Coastguard Worker 			if (errno == EROFS)
211*6a54128fSAndroid Build Coastguard Worker 				*mount_flags |= EXT2_MF_READONLY;
212*6a54128fSAndroid Build Coastguard Worker 		} else
213*6a54128fSAndroid Build Coastguard Worker 			close(fd);
214*6a54128fSAndroid Build Coastguard Worker 		(void) unlink(TEST_FILE);
215*6a54128fSAndroid Build Coastguard Worker 	}
216*6a54128fSAndroid Build Coastguard Worker 	retval = 0;
217*6a54128fSAndroid Build Coastguard Worker errout:
218*6a54128fSAndroid Build Coastguard Worker 	endmntent (f);
219*6a54128fSAndroid Build Coastguard Worker 	return retval;
220*6a54128fSAndroid Build Coastguard Worker }
221*6a54128fSAndroid Build Coastguard Worker 
check_mntent(const char * file,int * mount_flags,char * mtpt,int mtlen)222*6a54128fSAndroid Build Coastguard Worker static errcode_t check_mntent(const char *file, int *mount_flags,
223*6a54128fSAndroid Build Coastguard Worker 			      char *mtpt, int mtlen)
224*6a54128fSAndroid Build Coastguard Worker {
225*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
226*6a54128fSAndroid Build Coastguard Worker 
227*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
228*6a54128fSAndroid Build Coastguard Worker 	retval = check_mntent_file("/tmp/mtab", file, mount_flags,
229*6a54128fSAndroid Build Coastguard Worker 				   mtpt, mtlen);
230*6a54128fSAndroid Build Coastguard Worker 	if (retval == 0)
231*6a54128fSAndroid Build Coastguard Worker 		return 0;
232*6a54128fSAndroid Build Coastguard Worker #endif /* DEBUG */
233*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
234*6a54128fSAndroid Build Coastguard Worker 	retval = check_mntent_file("/proc/mounts", file, mount_flags,
235*6a54128fSAndroid Build Coastguard Worker 				   mtpt, mtlen);
236*6a54128fSAndroid Build Coastguard Worker 	if (retval == 0)
237*6a54128fSAndroid Build Coastguard Worker 		return 0;
238*6a54128fSAndroid Build Coastguard Worker #endif /* __linux__ */
239*6a54128fSAndroid Build Coastguard Worker #if defined(MOUNTED) || defined(_PATH_MOUNTED)
240*6a54128fSAndroid Build Coastguard Worker #ifndef MOUNTED
241*6a54128fSAndroid Build Coastguard Worker #define MOUNTED _PATH_MOUNTED
242*6a54128fSAndroid Build Coastguard Worker #endif /* MOUNTED */
243*6a54128fSAndroid Build Coastguard Worker 	retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
244*6a54128fSAndroid Build Coastguard Worker 	return retval;
245*6a54128fSAndroid Build Coastguard Worker #else
246*6a54128fSAndroid Build Coastguard Worker 	*mount_flags = 0;
247*6a54128fSAndroid Build Coastguard Worker 	return 0;
248*6a54128fSAndroid Build Coastguard Worker #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
249*6a54128fSAndroid Build Coastguard Worker }
250*6a54128fSAndroid Build Coastguard Worker 
251*6a54128fSAndroid Build Coastguard Worker #else
252*6a54128fSAndroid Build Coastguard Worker #if defined(HAVE_GETMNTINFO)
253*6a54128fSAndroid Build Coastguard Worker 
check_getmntinfo(const char * file,int * mount_flags,char * mtpt,int mtlen)254*6a54128fSAndroid Build Coastguard Worker static errcode_t check_getmntinfo(const char *file, int *mount_flags,
255*6a54128fSAndroid Build Coastguard Worker 				  char *mtpt, int mtlen)
256*6a54128fSAndroid Build Coastguard Worker {
257*6a54128fSAndroid Build Coastguard Worker 	struct statfs *mp;
258*6a54128fSAndroid Build Coastguard Worker         int    len, n;
259*6a54128fSAndroid Build Coastguard Worker         const  char   *s1;
260*6a54128fSAndroid Build Coastguard Worker 	char	*s2;
261*6a54128fSAndroid Build Coastguard Worker 
262*6a54128fSAndroid Build Coastguard Worker         n = getmntinfo(&mp, MNT_NOWAIT);
263*6a54128fSAndroid Build Coastguard Worker         if (n == 0)
264*6a54128fSAndroid Build Coastguard Worker 		return errno;
265*6a54128fSAndroid Build Coastguard Worker 
266*6a54128fSAndroid Build Coastguard Worker         len = sizeof(_PATH_DEV) - 1;
267*6a54128fSAndroid Build Coastguard Worker         s1 = file;
268*6a54128fSAndroid Build Coastguard Worker         if (strncmp(_PATH_DEV, s1, len) == 0)
269*6a54128fSAndroid Build Coastguard Worker                 s1 += len;
270*6a54128fSAndroid Build Coastguard Worker 
271*6a54128fSAndroid Build Coastguard Worker 	*mount_flags = 0;
272*6a54128fSAndroid Build Coastguard Worker         while (--n >= 0) {
273*6a54128fSAndroid Build Coastguard Worker                 s2 = mp->f_mntfromname;
274*6a54128fSAndroid Build Coastguard Worker                 if (strncmp(_PATH_DEV, s2, len) == 0) {
275*6a54128fSAndroid Build Coastguard Worker                         s2 += len - 1;
276*6a54128fSAndroid Build Coastguard Worker                         *s2 = 'r';
277*6a54128fSAndroid Build Coastguard Worker                 }
278*6a54128fSAndroid Build Coastguard Worker                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
279*6a54128fSAndroid Build Coastguard Worker 			*mount_flags = EXT2_MF_MOUNTED;
280*6a54128fSAndroid Build Coastguard Worker 			break;
281*6a54128fSAndroid Build Coastguard Worker 		}
282*6a54128fSAndroid Build Coastguard Worker                 ++mp;
283*6a54128fSAndroid Build Coastguard Worker 	}
284*6a54128fSAndroid Build Coastguard Worker 	if (mtpt)
285*6a54128fSAndroid Build Coastguard Worker 		strncpy(mtpt, mp->f_mntonname, mtlen);
286*6a54128fSAndroid Build Coastguard Worker 	return 0;
287*6a54128fSAndroid Build Coastguard Worker }
288*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_GETMNTINFO */
289*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_SETMNTENT */
290*6a54128fSAndroid Build Coastguard Worker 
291*6a54128fSAndroid Build Coastguard Worker /*
292*6a54128fSAndroid Build Coastguard Worker  * Check to see if we're dealing with the swap device.
293*6a54128fSAndroid Build Coastguard Worker  */
is_swap_device(const char * file)294*6a54128fSAndroid Build Coastguard Worker static int is_swap_device(const char *file)
295*6a54128fSAndroid Build Coastguard Worker {
296*6a54128fSAndroid Build Coastguard Worker 	FILE		*f;
297*6a54128fSAndroid Build Coastguard Worker 	char		buf[1024], *cp;
298*6a54128fSAndroid Build Coastguard Worker 	dev_t		file_dev;
299*6a54128fSAndroid Build Coastguard Worker 	struct stat	st_buf;
300*6a54128fSAndroid Build Coastguard Worker 	int		ret = 0;
301*6a54128fSAndroid Build Coastguard Worker 
302*6a54128fSAndroid Build Coastguard Worker 	file_dev = 0;
303*6a54128fSAndroid Build Coastguard Worker #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
304*6a54128fSAndroid Build Coastguard Worker 	if ((stat(file, &st_buf) == 0) &&
305*6a54128fSAndroid Build Coastguard Worker 	    ext2fsP_is_disk_device(st_buf.st_mode))
306*6a54128fSAndroid Build Coastguard Worker 		file_dev = st_buf.st_rdev;
307*6a54128fSAndroid Build Coastguard Worker #endif	/* __GNU__ */
308*6a54128fSAndroid Build Coastguard Worker 
309*6a54128fSAndroid Build Coastguard Worker 	if (!(f = fopen("/proc/swaps", "r")))
310*6a54128fSAndroid Build Coastguard Worker 		return 0;
311*6a54128fSAndroid Build Coastguard Worker 	/* Skip the first line */
312*6a54128fSAndroid Build Coastguard Worker 	if (!fgets(buf, sizeof(buf), f))
313*6a54128fSAndroid Build Coastguard Worker 		goto leave;
314*6a54128fSAndroid Build Coastguard Worker 	if (*buf && strncmp(buf, "Filename\t", 9))
315*6a54128fSAndroid Build Coastguard Worker 		/* Linux <=2.6.19 contained a bug in the /proc/swaps
316*6a54128fSAndroid Build Coastguard Worker 		 * code where the header would not be displayed
317*6a54128fSAndroid Build Coastguard Worker 		 */
318*6a54128fSAndroid Build Coastguard Worker 		goto valid_first_line;
319*6a54128fSAndroid Build Coastguard Worker 
320*6a54128fSAndroid Build Coastguard Worker 	while (fgets(buf, sizeof(buf), f)) {
321*6a54128fSAndroid Build Coastguard Worker valid_first_line:
322*6a54128fSAndroid Build Coastguard Worker 		if ((cp = strchr(buf, ' ')) != NULL)
323*6a54128fSAndroid Build Coastguard Worker 			*cp = 0;
324*6a54128fSAndroid Build Coastguard Worker 		if ((cp = strchr(buf, '\t')) != NULL)
325*6a54128fSAndroid Build Coastguard Worker 			*cp = 0;
326*6a54128fSAndroid Build Coastguard Worker 		if (strcmp(buf, file) == 0) {
327*6a54128fSAndroid Build Coastguard Worker 			ret++;
328*6a54128fSAndroid Build Coastguard Worker 			break;
329*6a54128fSAndroid Build Coastguard Worker 		}
330*6a54128fSAndroid Build Coastguard Worker #ifndef __GNU__
331*6a54128fSAndroid Build Coastguard Worker 		if (file_dev && (stat(buf, &st_buf) == 0) &&
332*6a54128fSAndroid Build Coastguard Worker 		    ext2fsP_is_disk_device(st_buf.st_mode) &&
333*6a54128fSAndroid Build Coastguard Worker 		    file_dev == st_buf.st_rdev) {
334*6a54128fSAndroid Build Coastguard Worker 			ret++;
335*6a54128fSAndroid Build Coastguard Worker 			break;
336*6a54128fSAndroid Build Coastguard Worker 		}
337*6a54128fSAndroid Build Coastguard Worker #endif 	/* __GNU__ */
338*6a54128fSAndroid Build Coastguard Worker 	}
339*6a54128fSAndroid Build Coastguard Worker 
340*6a54128fSAndroid Build Coastguard Worker leave:
341*6a54128fSAndroid Build Coastguard Worker 	fclose(f);
342*6a54128fSAndroid Build Coastguard Worker 	return ret;
343*6a54128fSAndroid Build Coastguard Worker }
344*6a54128fSAndroid Build Coastguard Worker 
345*6a54128fSAndroid Build Coastguard Worker 
346*6a54128fSAndroid Build Coastguard Worker /*
347*6a54128fSAndroid Build Coastguard Worker  * ext2fs_check_mount_point() fills determines if the device is
348*6a54128fSAndroid Build Coastguard Worker  * mounted or otherwise busy, and fills in mount_flags with one or
349*6a54128fSAndroid Build Coastguard Worker  * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
350*6a54128fSAndroid Build Coastguard Worker  * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY.  If mtpt is
351*6a54128fSAndroid Build Coastguard Worker  * non-NULL, the directory where the device is mounted is copied to
352*6a54128fSAndroid Build Coastguard Worker  * where mtpt is pointing, up to mtlen characters.
353*6a54128fSAndroid Build Coastguard Worker  */
354*6a54128fSAndroid Build Coastguard Worker #ifdef __TURBOC__
355*6a54128fSAndroid Build Coastguard Worker  #pragma argsused
356*6a54128fSAndroid Build Coastguard Worker #endif
ext2fs_check_mount_point(const char * device,int * mount_flags,char * mtpt,int mtlen)357*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
358*6a54128fSAndroid Build Coastguard Worker 				  char *mtpt, int mtlen)
359*6a54128fSAndroid Build Coastguard Worker {
360*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval = 0;
361*6a54128fSAndroid Build Coastguard Worker 	int 		busy = 0;
362*6a54128fSAndroid Build Coastguard Worker 
363*6a54128fSAndroid Build Coastguard Worker 	if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
364*6a54128fSAndroid Build Coastguard Worker 		*mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
365*6a54128fSAndroid Build Coastguard Worker 		if (getenv("EXT2FS_PRETEND_ROOTFS"))
366*6a54128fSAndroid Build Coastguard Worker 			*mount_flags = EXT2_MF_ISROOT;
367*6a54128fSAndroid Build Coastguard Worker 		return 0;
368*6a54128fSAndroid Build Coastguard Worker 	}
369*6a54128fSAndroid Build Coastguard Worker 	if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
370*6a54128fSAndroid Build Coastguard Worker 		*mount_flags = EXT2_MF_MOUNTED;
371*6a54128fSAndroid Build Coastguard Worker 		if (getenv("EXT2FS_PRETEND_ROOTFS"))
372*6a54128fSAndroid Build Coastguard Worker 			*mount_flags = EXT2_MF_ISROOT;
373*6a54128fSAndroid Build Coastguard Worker 		return 0;
374*6a54128fSAndroid Build Coastguard Worker 	}
375*6a54128fSAndroid Build Coastguard Worker 
376*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__ /* This only works on Linux 2.6+ systems */
377*6a54128fSAndroid Build Coastguard Worker 	{
378*6a54128fSAndroid Build Coastguard Worker 		struct stat st_buf;
379*6a54128fSAndroid Build Coastguard Worker 
380*6a54128fSAndroid Build Coastguard Worker 		if (stat(device, &st_buf) == 0 &&
381*6a54128fSAndroid Build Coastguard Worker 		    ext2fsP_is_disk_device(st_buf.st_mode)) {
382*6a54128fSAndroid Build Coastguard Worker 			int fd = open(device, O_RDONLY | O_EXCL);
383*6a54128fSAndroid Build Coastguard Worker 
384*6a54128fSAndroid Build Coastguard Worker 			if (fd >= 0) {
385*6a54128fSAndroid Build Coastguard Worker 				/*
386*6a54128fSAndroid Build Coastguard Worker 				 * The device is not busy so it's
387*6a54128fSAndroid Build Coastguard Worker 				 * definitelly not mounted. No need to
388*6a54128fSAndroid Build Coastguard Worker 				 * to perform any more checks.
389*6a54128fSAndroid Build Coastguard Worker 				 */
390*6a54128fSAndroid Build Coastguard Worker 				close(fd);
391*6a54128fSAndroid Build Coastguard Worker 				*mount_flags = 0;
392*6a54128fSAndroid Build Coastguard Worker 				return 0;
393*6a54128fSAndroid Build Coastguard Worker 			} else if (errno == EBUSY) {
394*6a54128fSAndroid Build Coastguard Worker 				busy = 1;
395*6a54128fSAndroid Build Coastguard Worker 			}
396*6a54128fSAndroid Build Coastguard Worker 		}
397*6a54128fSAndroid Build Coastguard Worker 	}
398*6a54128fSAndroid Build Coastguard Worker #endif
399*6a54128fSAndroid Build Coastguard Worker 
400*6a54128fSAndroid Build Coastguard Worker 	if (is_swap_device(device)) {
401*6a54128fSAndroid Build Coastguard Worker 		*mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
402*6a54128fSAndroid Build Coastguard Worker 		if (mtpt)
403*6a54128fSAndroid Build Coastguard Worker 			strncpy(mtpt, "<swap>", mtlen);
404*6a54128fSAndroid Build Coastguard Worker 	} else {
405*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_SETMNTENT
406*6a54128fSAndroid Build Coastguard Worker 		retval = check_mntent(device, mount_flags, mtpt, mtlen);
407*6a54128fSAndroid Build Coastguard Worker #else
408*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_GETMNTINFO
409*6a54128fSAndroid Build Coastguard Worker 		retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
410*6a54128fSAndroid Build Coastguard Worker #else
411*6a54128fSAndroid Build Coastguard Worker #if defined(__GNUC__) && !defined(_WIN32)
412*6a54128fSAndroid Build Coastguard Worker  #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
413*6a54128fSAndroid Build Coastguard Worker #endif
414*6a54128fSAndroid Build Coastguard Worker 		*mount_flags = 0;
415*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_GETMNTINFO */
416*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_SETMNTENT */
417*6a54128fSAndroid Build Coastguard Worker 	}
418*6a54128fSAndroid Build Coastguard Worker 	if (retval)
419*6a54128fSAndroid Build Coastguard Worker 		return retval;
420*6a54128fSAndroid Build Coastguard Worker 
421*6a54128fSAndroid Build Coastguard Worker 	if (busy)
422*6a54128fSAndroid Build Coastguard Worker 		*mount_flags |= EXT2_MF_BUSY;
423*6a54128fSAndroid Build Coastguard Worker 
424*6a54128fSAndroid Build Coastguard Worker 	return 0;
425*6a54128fSAndroid Build Coastguard Worker }
426*6a54128fSAndroid Build Coastguard Worker 
427*6a54128fSAndroid Build Coastguard Worker /*
428*6a54128fSAndroid Build Coastguard Worker  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
429*6a54128fSAndroid Build Coastguard Worker  * EXT2_MF_READONLY, and EXT2_MF_ROOT
430*6a54128fSAndroid Build Coastguard Worker  *
431*6a54128fSAndroid Build Coastguard Worker  */
ext2fs_check_if_mounted(const char * file,int * mount_flags)432*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
433*6a54128fSAndroid Build Coastguard Worker {
434*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
435*6a54128fSAndroid Build Coastguard Worker }
436*6a54128fSAndroid Build Coastguard Worker 
437*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
main(int argc,char ** argv)438*6a54128fSAndroid Build Coastguard Worker int main(int argc, char **argv)
439*6a54128fSAndroid Build Coastguard Worker {
440*6a54128fSAndroid Build Coastguard Worker 	int	retval, mount_flags;
441*6a54128fSAndroid Build Coastguard Worker 	char	mntpt[80];
442*6a54128fSAndroid Build Coastguard Worker 
443*6a54128fSAndroid Build Coastguard Worker 	if (argc < 2) {
444*6a54128fSAndroid Build Coastguard Worker 		fprintf(stderr, "Usage: %s device\n", argv[0]);
445*6a54128fSAndroid Build Coastguard Worker 		exit(1);
446*6a54128fSAndroid Build Coastguard Worker 	}
447*6a54128fSAndroid Build Coastguard Worker 
448*6a54128fSAndroid Build Coastguard Worker 	add_error_table(&et_ext2_error_table);
449*6a54128fSAndroid Build Coastguard Worker 	mntpt[0] = 0;
450*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_check_mount_point(argv[1], &mount_flags,
451*6a54128fSAndroid Build Coastguard Worker 					  mntpt, sizeof(mntpt));
452*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
453*6a54128fSAndroid Build Coastguard Worker 		com_err(argv[0], retval,
454*6a54128fSAndroid Build Coastguard Worker 			"while calling ext2fs_check_if_mounted");
455*6a54128fSAndroid Build Coastguard Worker 		exit(1);
456*6a54128fSAndroid Build Coastguard Worker 	}
457*6a54128fSAndroid Build Coastguard Worker 	printf("Device %s reports flags %02x\n", argv[1], mount_flags);
458*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_BUSY)
459*6a54128fSAndroid Build Coastguard Worker 		printf("\t%s is apparently in use.\n", argv[1]);
460*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_MOUNTED)
461*6a54128fSAndroid Build Coastguard Worker 		printf("\t%s is mounted.\n", argv[1]);
462*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_SWAP)
463*6a54128fSAndroid Build Coastguard Worker 		printf("\t%s is a swap device.\n", argv[1]);
464*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_READONLY)
465*6a54128fSAndroid Build Coastguard Worker 		printf("\t%s is read-only.\n", argv[1]);
466*6a54128fSAndroid Build Coastguard Worker 	if (mount_flags & EXT2_MF_ISROOT)
467*6a54128fSAndroid Build Coastguard Worker 		printf("\t%s is the root filesystem.\n", argv[1]);
468*6a54128fSAndroid Build Coastguard Worker 	if (mntpt[0])
469*6a54128fSAndroid Build Coastguard Worker 		printf("\t%s is mounted on %s.\n", argv[1], mntpt);
470*6a54128fSAndroid Build Coastguard Worker 	exit(0);
471*6a54128fSAndroid Build Coastguard Worker }
472*6a54128fSAndroid Build Coastguard Worker #endif /* DEBUG */
473