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