OSDN Git Service

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