Whamcloud - gitweb
c73273b84344166cf9a9b98cd0d29a330118543a
[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;
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 && (file_rdev == st_buf.st_rdev))
148                                         break;
149                                 if (check_loop_mounted(mnt->mnt_fsname,
150                                                 st_buf.st_rdev, file_dev,
151                                                 file_ino) == 1)
152                                         break;
153 #endif  /* __GNU__ */
154                         } else {
155                                 if (file_dev && ((file_dev == st_buf.st_dev) &&
156                                                  (file_ino == st_buf.st_ino)))
157                                         break;
158                         }
159                 }
160         }
161
162         if (mnt == 0) {
163 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
164                 /*
165                  * Do an extra check to see if this is the root device.  We
166                  * can't trust /etc/mtab, and /proc/mounts will only list
167                  * /dev/root for the root filesystem.  Argh.  Instead we
168                  * check if the given device has the same major/minor number
169                  * as the device that the root directory is on.
170                  */
171                 if (file_rdev && stat("/", &st_buf) == 0) {
172                         if (st_buf.st_dev == file_rdev) {
173                                 *mount_flags = EXT2_MF_MOUNTED;
174                                 if (mtpt)
175                                         strncpy(mtpt, "/", mtlen);
176                                 goto is_root;
177                         }
178                 }
179 #endif  /* __GNU__ */
180                 goto errout;
181         }
182         *mount_flags = EXT2_MF_MOUNTED;
183
184 #ifdef MNTOPT_RO
185         /* Check to see if the ro option is set */
186         if (hasmntopt(mnt, MNTOPT_RO))
187                 *mount_flags |= EXT2_MF_READONLY;
188 #endif
189
190         if (mtpt)
191                 strncpy(mtpt, mnt->mnt_dir, mtlen);
192         /*
193          * Check to see if we're referring to the root filesystem.
194          * If so, do a manual check to see if we can open /etc/mtab
195          * read/write, since if the root is mounted read/only, the
196          * contents of /etc/mtab may not be accurate.
197          */
198         if (!strcmp(mnt->mnt_dir, "/")) {
199 is_root:
200 #define TEST_FILE "/.ismount-test-file"
201                 *mount_flags |= EXT2_MF_ISROOT;
202                 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
203                 if (fd < 0) {
204                         if (errno == EROFS)
205                                 *mount_flags |= EXT2_MF_READONLY;
206                 } else
207                         close(fd);
208                 (void) unlink(TEST_FILE);
209         }
210
211         if (!strcmp(mnt->mnt_type, "ext4") ||
212             !strcmp(mnt->mnt_type, "ext3") ||
213             !strcmp(mnt->mnt_type, "ext2"))
214                 *mount_flags |= EXT2_MF_EXTFS;
215         retval = 0;
216 errout:
217         endmntent (f);
218         return retval;
219 }
220
221 static errcode_t check_mntent(const char *file, int *mount_flags,
222                               char *mtpt, int mtlen)
223 {
224         errcode_t       retval;
225
226 #ifdef DEBUG
227         retval = check_mntent_file("/tmp/mtab", file, mount_flags,
228                                    mtpt, mtlen);
229         if (retval == 0)
230                 return 0;
231 #endif /* DEBUG */
232 #ifdef __linux__
233         retval = check_mntent_file("/proc/mounts", file, mount_flags,
234                                    mtpt, mtlen);
235         if (retval == 0)
236                 return 0;
237 #endif /* __linux__ */
238 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
239 #ifndef MOUNTED
240 #define MOUNTED _PATH_MOUNTED
241 #endif /* MOUNTED */
242         retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
243         return retval;
244 #else
245         *mount_flags = 0;
246         return 0;
247 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
248 }
249
250 #else
251 #if defined(HAVE_GETMNTINFO)
252
253 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
254                                   char *mtpt, int mtlen)
255 {
256         struct statfs *mp;
257         int    len, n;
258         const  char   *s1;
259         char    *s2;
260
261         n = getmntinfo(&mp, MNT_NOWAIT);
262         if (n == 0)
263                 return errno;
264
265         len = sizeof(_PATH_DEV) - 1;
266         s1 = file;
267         if (strncmp(_PATH_DEV, s1, len) == 0)
268                 s1 += len;
269
270         *mount_flags = 0;
271         while (--n >= 0) {
272                 s2 = mp->f_mntfromname;
273                 if (strncmp(_PATH_DEV, s2, len) == 0) {
274                         s2 += len - 1;
275                         *s2 = 'r';
276                 }
277                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
278                         *mount_flags = EXT2_MF_MOUNTED;
279                         break;
280                 }
281                 ++mp;
282         }
283         if (mtpt)
284                 strncpy(mtpt, mp->f_mntonname, mtlen);
285         return 0;
286 }
287 #endif /* HAVE_GETMNTINFO */
288 #endif /* HAVE_SETMNTENT */
289
290 /*
291  * Check to see if we're dealing with the swap device.
292  */
293 static int is_swap_device(const char *file)
294 {
295         FILE            *f;
296         char            buf[1024], *cp;
297         dev_t           file_dev;
298         struct stat     st_buf;
299         int             ret = 0;
300
301         file_dev = 0;
302 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
303         if ((stat(file, &st_buf) == 0) &&
304             ext2fsP_is_disk_device(st_buf.st_mode))
305                 file_dev = st_buf.st_rdev;
306 #endif  /* __GNU__ */
307
308         if (!(f = fopen("/proc/swaps", "r")))
309                 return 0;
310         /* Skip the first line */
311         if (!fgets(buf, sizeof(buf), f))
312                 goto leave;
313         if (*buf && strncmp(buf, "Filename\t", 9))
314                 /* Linux <=2.6.19 contained a bug in the /proc/swaps
315                  * code where the header would not be displayed
316                  */
317                 goto valid_first_line;
318
319         while (fgets(buf, sizeof(buf), f)) {
320 valid_first_line:
321                 if ((cp = strchr(buf, ' ')) != NULL)
322                         *cp = 0;
323                 if ((cp = strchr(buf, '\t')) != NULL)
324                         *cp = 0;
325                 if (strcmp(buf, file) == 0) {
326                         ret++;
327                         break;
328                 }
329 #ifndef __GNU__
330                 if (file_dev && (stat(buf, &st_buf) == 0) &&
331                     ext2fsP_is_disk_device(st_buf.st_mode) &&
332                     file_dev == st_buf.st_rdev) {
333                         ret++;
334                         break;
335                 }
336 #endif  /* __GNU__ */
337         }
338
339 leave:
340         fclose(f);
341         return ret;
342 }
343
344
345 /*
346  * ext2fs_check_mount_point() fills determines if the device is
347  * mounted or otherwise busy, and fills in mount_flags with one or
348  * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
349  * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY.  If mtpt is
350  * non-NULL, the directory where the device is mounted is copied to
351  * where mtpt is pointing, up to mtlen characters.
352  */
353 #ifdef __TURBOC__
354  #pragma argsused
355 #endif
356 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
357                                   char *mtpt, int mtlen)
358 {
359         errcode_t       retval = 0;
360         int             busy = 0;
361
362         if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
363                 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
364                 if (getenv("EXT2FS_PRETEND_ROOTFS"))
365                         *mount_flags = EXT2_MF_ISROOT;
366                 return 0;
367         }
368         if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
369                 *mount_flags = EXT2_MF_MOUNTED;
370                 if (getenv("EXT2FS_PRETEND_ROOTFS"))
371                         *mount_flags = EXT2_MF_ISROOT;
372                 return 0;
373         }
374
375 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
376         {
377                 struct stat st_buf;
378
379                 if (stat(device, &st_buf) == 0 &&
380                     ext2fsP_is_disk_device(st_buf.st_mode)) {
381                         int fd = open(device, O_RDONLY | O_EXCL);
382
383                         if (fd >= 0) {
384                                 /*
385                                  * The device is not busy so it's
386                                  * definitelly not mounted. No need to
387                                  * to perform any more checks.
388                                  */
389                                 close(fd);
390                                 *mount_flags = 0;
391                                 return 0;
392                         } else if (errno == EBUSY) {
393                                 busy = 1;
394                         }
395                 }
396         }
397 #endif
398
399         if (is_swap_device(device)) {
400                 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
401                 if (mtpt)
402                         strncpy(mtpt, "<swap>", mtlen);
403         } else {
404 #ifdef HAVE_SETMNTENT
405                 retval = check_mntent(device, mount_flags, mtpt, mtlen);
406 #else
407 #ifdef HAVE_GETMNTINFO
408                 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
409 #else
410 #ifdef __GNUC__
411  #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
412 #endif
413                 *mount_flags = 0;
414 #endif /* HAVE_GETMNTINFO */
415 #endif /* HAVE_SETMNTENT */
416         }
417         if (retval)
418                 return retval;
419
420         if (busy)
421                 *mount_flags |= EXT2_MF_BUSY;
422
423         return 0;
424 }
425
426 /*
427  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
428  * EXT2_MF_READONLY, and EXT2_MF_ROOT
429  *
430  */
431 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
432 {
433         return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
434 }
435
436 #ifdef DEBUG
437 int main(int argc, char **argv)
438 {
439         int     retval, mount_flags;
440         char    mntpt[80];
441
442         if (argc < 2) {
443                 fprintf(stderr, "Usage: %s device\n", argv[0]);
444                 exit(1);
445         }
446
447         add_error_table(&et_ext2_error_table);
448         mntpt[0] = 0;
449         retval = ext2fs_check_mount_point(argv[1], &mount_flags,
450                                           mntpt, sizeof(mntpt));
451         if (retval) {
452                 com_err(argv[0], retval,
453                         "while calling ext2fs_check_if_mounted");
454                 exit(1);
455         }
456         printf("Device %s reports flags %02x\n", argv[1], mount_flags);
457         if (mount_flags & EXT2_MF_BUSY)
458                 printf("\t%s is apparently in use.\n", argv[1]);
459         if (mount_flags & EXT2_MF_MOUNTED)
460                 printf("\t%s is mounted.\n", argv[1]);
461         if (mount_flags & EXT2_MF_SWAP)
462                 printf("\t%s is a swap device.\n", argv[1]);
463         if (mount_flags & EXT2_MF_READONLY)
464                 printf("\t%s is read-only.\n", argv[1]);
465         if (mount_flags & EXT2_MF_ISROOT)
466                 printf("\t%s is the root filesystem.\n", argv[1]);
467         if (mntpt[0])
468                 printf("\t%s is mounted on %s.\n", argv[1], mntpt);
469         exit(0);
470 }
471 #endif /* DEBUG */