Whamcloud - gitweb
Merge branch 'maint' into next
[tools/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 #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  */
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  */
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
217         if (mnt && mnt->mnt_type &&
218             (!strcmp(mnt->mnt_type, "ext4") ||
219              !strcmp(mnt->mnt_type, "ext3") ||
220              !strcmp(mnt->mnt_type, "ext2")))
221                 *mount_flags |= EXT2_MF_EXTFS;
222         retval = 0;
223 errout:
224         endmntent (f);
225         return retval;
226 }
227
228 static errcode_t check_mntent(const char *file, int *mount_flags,
229                               char *mtpt, int mtlen)
230 {
231         errcode_t       retval;
232
233 #ifdef DEBUG
234         retval = check_mntent_file("/tmp/mtab", file, mount_flags,
235                                    mtpt, mtlen);
236         if (retval == 0)
237                 return 0;
238 #endif /* DEBUG */
239 #ifdef __linux__
240         retval = check_mntent_file("/proc/mounts", file, mount_flags,
241                                    mtpt, mtlen);
242         if (retval == 0)
243                 return 0;
244 #endif /* __linux__ */
245 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
246 #ifndef MOUNTED
247 #define MOUNTED _PATH_MOUNTED
248 #endif /* MOUNTED */
249         retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
250         return retval;
251 #else
252         *mount_flags = 0;
253         return 0;
254 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
255 }
256
257 #else
258 #if defined(HAVE_GETMNTINFO)
259
260 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
261                                   char *mtpt, int mtlen)
262 {
263         struct statfs *mp;
264         int    len, n;
265         const  char   *s1;
266         char    *s2;
267
268         n = getmntinfo(&mp, MNT_NOWAIT);
269         if (n == 0)
270                 return errno;
271
272         len = sizeof(_PATH_DEV) - 1;
273         s1 = file;
274         if (strncmp(_PATH_DEV, s1, len) == 0)
275                 s1 += len;
276
277         *mount_flags = 0;
278         while (--n >= 0) {
279                 s2 = mp->f_mntfromname;
280                 if (strncmp(_PATH_DEV, s2, len) == 0) {
281                         s2 += len - 1;
282                         *s2 = 'r';
283                 }
284                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
285                         *mount_flags = EXT2_MF_MOUNTED;
286                         break;
287                 }
288                 ++mp;
289         }
290         if (mtpt)
291                 strncpy(mtpt, mp->f_mntonname, mtlen);
292         return 0;
293 }
294 #endif /* HAVE_GETMNTINFO */
295 #endif /* HAVE_SETMNTENT */
296
297 /*
298  * Check to see if we're dealing with the swap device.
299  */
300 static int is_swap_device(const char *file)
301 {
302         FILE            *f;
303         char            buf[1024], *cp;
304         dev_t           file_dev;
305         struct stat     st_buf;
306         int             ret = 0;
307
308         file_dev = 0;
309 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
310         if ((stat(file, &st_buf) == 0) &&
311             ext2fsP_is_disk_device(st_buf.st_mode))
312                 file_dev = st_buf.st_rdev;
313 #endif  /* __GNU__ */
314
315         if (!(f = fopen("/proc/swaps", "r")))
316                 return 0;
317         /* Skip the first line */
318         if (!fgets(buf, sizeof(buf), f))
319                 goto leave;
320         if (*buf && strncmp(buf, "Filename\t", 9))
321                 /* Linux <=2.6.19 contained a bug in the /proc/swaps
322                  * code where the header would not be displayed
323                  */
324                 goto valid_first_line;
325
326         while (fgets(buf, sizeof(buf), f)) {
327 valid_first_line:
328                 if ((cp = strchr(buf, ' ')) != NULL)
329                         *cp = 0;
330                 if ((cp = strchr(buf, '\t')) != NULL)
331                         *cp = 0;
332                 if (strcmp(buf, file) == 0) {
333                         ret++;
334                         break;
335                 }
336 #ifndef __GNU__
337                 if (file_dev && (stat(buf, &st_buf) == 0) &&
338                     ext2fsP_is_disk_device(st_buf.st_mode) &&
339                     file_dev == st_buf.st_rdev) {
340                         ret++;
341                         break;
342                 }
343 #endif  /* __GNU__ */
344         }
345
346 leave:
347         fclose(f);
348         return ret;
349 }
350
351
352 /*
353  * ext2fs_check_mount_point() fills determines if the device is
354  * mounted or otherwise busy, and fills in mount_flags with one or
355  * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
356  * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY.  If mtpt is
357  * non-NULL, the directory where the device is mounted is copied to
358  * where mtpt is pointing, up to mtlen characters.
359  */
360 #ifdef __TURBOC__
361  #pragma argsused
362 #endif
363 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
364                                   char *mtpt, int mtlen)
365 {
366         errcode_t       retval = 0;
367         int             busy = 0;
368
369         if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
370                 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
371                 if (getenv("EXT2FS_PRETEND_ROOTFS"))
372                         *mount_flags = EXT2_MF_ISROOT;
373                 return 0;
374         }
375         if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
376                 *mount_flags = EXT2_MF_MOUNTED;
377                 if (getenv("EXT2FS_PRETEND_ROOTFS"))
378                         *mount_flags = EXT2_MF_ISROOT;
379                 return 0;
380         }
381
382 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
383         {
384                 struct stat st_buf;
385
386                 if (stat(device, &st_buf) == 0 &&
387                     ext2fsP_is_disk_device(st_buf.st_mode)) {
388                         int fd = open(device, O_RDONLY | O_EXCL);
389
390                         if (fd >= 0) {
391                                 /*
392                                  * The device is not busy so it's
393                                  * definitelly not mounted. No need to
394                                  * to perform any more checks.
395                                  */
396                                 close(fd);
397                                 *mount_flags = 0;
398                                 return 0;
399                         } else if (errno == EBUSY) {
400                                 busy = 1;
401                         }
402                 }
403         }
404 #endif
405
406         if (is_swap_device(device)) {
407                 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
408                 if (mtpt)
409                         strncpy(mtpt, "<swap>", mtlen);
410         } else {
411 #ifdef HAVE_SETMNTENT
412                 retval = check_mntent(device, mount_flags, mtpt, mtlen);
413 #else
414 #ifdef HAVE_GETMNTINFO
415                 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
416 #else
417 #if defined(__GNUC__) && !defined(_WIN32)
418  #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
419 #endif
420                 *mount_flags = 0;
421 #endif /* HAVE_GETMNTINFO */
422 #endif /* HAVE_SETMNTENT */
423         }
424         if (retval)
425                 return retval;
426
427         if (busy)
428                 *mount_flags |= EXT2_MF_BUSY;
429
430         return 0;
431 }
432
433 /*
434  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
435  * EXT2_MF_READONLY, and EXT2_MF_ROOT
436  *
437  */
438 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
439 {
440         return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
441 }
442
443 #ifdef DEBUG
444 int main(int argc, char **argv)
445 {
446         int     retval, mount_flags;
447         char    mntpt[80];
448
449         if (argc < 2) {
450                 fprintf(stderr, "Usage: %s device\n", argv[0]);
451                 exit(1);
452         }
453
454         add_error_table(&et_ext2_error_table);
455         mntpt[0] = 0;
456         retval = ext2fs_check_mount_point(argv[1], &mount_flags,
457                                           mntpt, sizeof(mntpt));
458         if (retval) {
459                 com_err(argv[0], retval,
460                         "while calling ext2fs_check_if_mounted");
461                 exit(1);
462         }
463         printf("Device %s reports flags %02x\n", argv[1], mount_flags);
464         if (mount_flags & EXT2_MF_BUSY)
465                 printf("\t%s is apparently in use.\n", argv[1]);
466         if (mount_flags & EXT2_MF_MOUNTED)
467                 printf("\t%s is mounted.\n", argv[1]);
468         if (mount_flags & EXT2_MF_SWAP)
469                 printf("\t%s is a swap device.\n", argv[1]);
470         if (mount_flags & EXT2_MF_READONLY)
471                 printf("\t%s is read-only.\n", argv[1]);
472         if (mount_flags & EXT2_MF_ISROOT)
473                 printf("\t%s is the root filesystem.\n", argv[1]);
474         if (mntpt[0])
475                 printf("\t%s is mounted on %s.\n", argv[1], mntpt);
476         exit(0);
477 }
478 #endif /* DEBUG */