Whamcloud - gitweb
ChangeLog, openfs.c:
[tools/e2fsprogs.git] / lib / ext2fs / ismounted.c
1 /*
2  * ismounted.c --- Check to see if the filesystem was mounted
3  * 
4  * Copyright (C) 1995 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #if HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #if HAVE_ERRNO_H
17 #include <errno.h>
18 #endif
19 #include <fcntl.h>
20 #ifdef HAVE_LINUX_FS_H
21 #include <linux/fs.h>
22 #endif
23 #ifdef HAVE_LINUX_FD_H
24 #include <linux/fd.h>
25 #endif
26 #ifdef HAVE_MNTENT_H
27 #include <mntent.h>
28 #endif
29 #ifdef HAVE_GETMNTINFO
30 #include <paths.h>
31 #include <sys/param.h>
32 #include <sys/mount.h>
33 #endif /* HAVE_GETMNTINFO */
34
35 #if EXT2_FLAT_INCLUDES
36 #include "ext2_fs.h"
37 #else
38 #include <linux/ext2_fs.h>
39 #endif
40
41 #include "ext2fs.h"
42
43 #ifdef HAVE_MNTENT_H
44 /*
45  * XXX we only check to see if the mount is readonly when it's the
46  * root filesystem.
47  */
48 static errcode_t check_mntent(const char *file, int *mount_flags)
49 {
50         FILE * f;
51         struct mntent * mnt;
52         int     fd;
53
54         *mount_flags = 0;
55         if ((f = setmntent (MOUNTED, "r")) == NULL)
56                 return errno;
57         while ((mnt = getmntent (f)) != NULL)
58                 if (strcmp(file, mnt->mnt_fsname) == 0)
59                         break;
60         endmntent (f);
61         if (mnt == 0)
62                 return 0;
63         *mount_flags = EXT2_MF_MOUNTED;
64         
65         if (!strcmp(mnt->mnt_dir, "/")) {
66                 *mount_flags |= EXT2_MF_ISROOT;
67                 fd = open(MOUNTED, O_RDWR);
68                 if (fd < 0) {
69                         if (errno == EROFS)
70                                 *mount_flags |= EXT2_MF_READONLY;
71                 } else
72                         close(fd);
73         }
74         return 0;
75 }
76 #endif
77
78 #ifdef HAVE_GETMNTINFO
79 static errcode_t check_getmntinfo(const char *file, int *mount_flags)
80 {
81         struct statfs *mp;
82         int    len, n;
83         const  char   *s1;
84         char    *s2;
85
86         n = getmntinfo(&mp, MNT_NOWAIT);
87         if (n == 0)
88                 return errno;
89
90         len = sizeof(_PATH_DEV) - 1;
91         s1 = file;
92         if (strncmp(_PATH_DEV, s1, len) == 0)
93                 s1 += len;
94  
95         *mount_flags = 0;
96         while (--n >= 0) {
97                 s2 = mp->f_mntfromname;
98                 if (strncmp(_PATH_DEV, s2, len) == 0) {
99                         s2 += len - 1;
100                         *s2 = 'r';
101                 }
102                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
103                         *mount_flags = EXT2_MF_MOUNTED;
104                         break;
105                 }
106                 ++mp;
107         }
108         return 0;
109 }
110 #endif /* HAVE_GETMNTINFO */
111
112 /*
113  * Is_mounted is set to 1 if the device is mounted, 0 otherwise
114  */
115 #ifdef __TURBOC__
116 #pragma argsused
117 #endif
118 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
119 {
120 #ifdef HAVE_MNTENT_H
121         return check_mntent(file, mount_flags);
122 #else 
123 #ifdef HAVE_GETMNTINFO
124         return check_getmntinfo(file, mount_flags);
125 #else
126         *mount_flags = 0;
127         return 0;
128 #endif /* HAVE_GETMNTINFO */
129 #endif /* HAVE_MNTENT_H */
130 }