Whamcloud - gitweb
ChangeLog, ismounted.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,1996,1997,1998,1999,2000 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_FD_H
21 #include <linux/fd.h>
22 #endif
23 #ifdef HAVE_MNTENT_H
24 #include <mntent.h>
25 #endif
26 #ifdef HAVE_GETMNTINFO
27 #include <paths.h>
28 #include <sys/param.h>
29 #include <sys/mount.h>
30 #endif /* HAVE_GETMNTINFO */
31 #include <string.h>
32 #include <sys/stat.h>
33
34 #include "ext2_fs.h"
35 #include "ext2fs.h"
36
37 #ifdef HAVE_MNTENT_H
38 /*
39  * Helper function which checks a file in /etc/mtab format to see if a
40  * filesystem is mounted.  Returns an error if the file doesn't exist
41  * or can't be opened.  
42  */
43 static errcode_t check_mntent_file(const char *mtab_file, const char *file, 
44                                    int *mount_flags, char *mtpt, int mtlen)
45 {
46         FILE * f;
47         struct mntent * mnt;
48         int     fd;
49
50         *mount_flags = 0;
51         if ((f = setmntent (mtab_file, "r")) == NULL)
52                 return errno;
53         while ((mnt = getmntent (f)) != NULL)
54                 if (strcmp(file, mnt->mnt_fsname) == 0)
55                         break;
56
57         if (mnt == 0) {
58 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
59                 struct stat st_root, st_file;
60                 /*
61                  * Do an extra check to see if this is the root device.  We
62                  * can't trust /etc/fstab, and /proc/mounts will only list
63                  * /dev/root for the root filesystem.  Argh.  Instead we
64                  * check if the given device has the same major/minor number
65                  * as the device that the root directory is on.
66                  */
67                 if (stat("/", &st_root) == 0 && stat(file, &st_file) == 0) {
68                         if (st_root.st_dev == st_file.st_rdev) {
69                                 *mount_flags = EXT2_MF_MOUNTED;
70                                 if (mtpt)
71                                         strncpy(mtpt, "/", mtlen);
72                                 goto is_root;
73                         }
74                 }
75 #endif
76                 endmntent (f);
77                 return 0;
78         }
79         *mount_flags = EXT2_MF_MOUNTED;
80         
81         /* Check to see if the ro option is set */
82         if (hasmntopt(mnt, MNTOPT_RO))
83                 *mount_flags |= EXT2_MF_READONLY;
84
85         if (mtpt)
86                 strncpy(mtpt, mnt->mnt_dir, mtlen);
87         /*
88          * Check to see if we're referring to the root filesystem.
89          * If so, do a manual check to see if we can open /etc/mtab
90          * read/write, since if the root is mounted read/only, the
91          * contents of /etc/mtab may not be accurate.
92          */
93         if (!strcmp(mnt->mnt_dir, "/")) {
94 is_root:
95                 *mount_flags |= EXT2_MF_ISROOT;
96                 fd = open(MOUNTED, O_RDWR);
97                 if (fd < 0) {
98                         if (errno == EROFS)
99                                 *mount_flags |= EXT2_MF_READONLY;
100                 } else
101                         close(fd);
102         }
103         endmntent (f);
104         return 0;
105 }
106
107 static errcode_t check_mntent(const char *file, int *mount_flags,
108                               char *mtpt, int mtlen)
109 {
110         errcode_t       retval;
111
112 #ifdef __linux__
113         retval = check_mntent_file("/proc/mounts", file, mount_flags,
114                                    mtpt, mtlen);
115         if (retval == 0)
116                 return 0;
117 #endif
118         retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
119         return retval;
120 }
121
122 #elif defined(HAVE_GETMNTINFO)
123
124 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
125                                   char *mtpt, int mtlen)
126 {
127         struct statfs *mp;
128         int    len, n;
129         const  char   *s1;
130         char    *s2;
131
132         n = getmntinfo(&mp, MNT_NOWAIT);
133         if (n == 0)
134                 return errno;
135
136         len = sizeof(_PATH_DEV) - 1;
137         s1 = file;
138         if (strncmp(_PATH_DEV, s1, len) == 0)
139                 s1 += len;
140  
141         *mount_flags = 0;
142         while (--n >= 0) {
143                 s2 = mp->f_mntfromname;
144                 if (strncmp(_PATH_DEV, s2, len) == 0) {
145                         s2 += len - 1;
146                         *s2 = 'r';
147                 }
148                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
149                         *mount_flags = EXT2_MF_MOUNTED;
150                         break;
151                 }
152                 ++mp;
153         }
154         if (mtpt)
155                 strncpy(mtpt, mp->f_mntonname, mtlen);
156         return 0;
157 }
158 #endif /* HAVE_GETMNTINFO */
159
160 /*
161  * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
162  * otherwise.  If mtpt is non-NULL, the directory where the device is
163  * mounted is copied to where mtpt is pointing, up to mtlen
164  * characters.
165  */
166 #ifdef __TURBOC__
167  #pragma argsused
168 #endif
169 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
170                                   char *mtpt, int mtlen)
171 {
172 #ifdef HAVE_MNTENT_H
173         return check_mntent(device, mount_flags, mtpt, mtlen);
174 #else 
175 #ifdef HAVE_GETMNTINFO
176         return check_getmntinfo(device, mount_flags, mtpt, mtlen);
177 #else
178 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
179         *mount_flags = 0;
180         return 0;
181 #endif /* HAVE_GETMNTINFO */
182 #endif /* HAVE_MNTENT_H */
183 }
184
185 /*
186  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED and
187  * EXT2_MF_READONLY, and EXT2_MF_ROOT
188  * 
189  */
190 #ifdef __TURBOC__
191  #pragma argsused
192 #endif
193 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
194 {
195         return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
196 }
197
198 #ifdef DEBUG
199 int main(int argc, char **argv)
200 {
201         int     retval, mount_flags;
202         
203         if (argc < 2) {
204                 fprintf(stderr, "Usage: %s device\n", argv[0]);
205                 exit(1);
206         }
207
208         retval = ext2fs_check_if_mounted(argv[1], &mount_flags);
209         if (retval) {
210                 com_err(argv[0], retval,
211                         "while calling ext2fs_check_if_mounted");
212                 exit(1);
213         }
214         printf("Device %s reports flags %02x\n", argv[1], mount_flags);
215         if (mount_flags & EXT2_MF_MOUNTED)
216                 printf("\t%s is mounted.\n", argv[1]);
217         
218         if (mount_flags & EXT2_MF_READONLY)
219                 printf("\t%s is read-only.\n", argv[1]);
220         
221         if (mount_flags & EXT2_MF_ISROOT)
222                 printf("\t%s is the root filesystem.\n", argv[1]);
223         
224         exit(0);
225 }
226 #endif