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