Whamcloud - gitweb
858d08e8de10bc2ef6e3ce1b8e96ee14daceaab5
[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         struct mntent   *mnt;
47         struct stat     st_buf;
48         errcode_t       retval = 0;
49         dev_t           file_dev;
50         FILE            *f;
51         int             fd;
52
53         *mount_flags = 0;
54         if ((f = setmntent (mtab_file, "r")) == NULL)
55                 return errno;
56         file_dev = 0;
57 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
58         if (stat(file, &st_buf) == 0)
59                 file_dev = st_buf.st_rdev;
60 #endif  
61         while ((mnt = getmntent (f)) != NULL) {
62                 if (strcmp(file, mnt->mnt_fsname) == 0)
63                         break;
64 #ifndef __GNU__
65                 if (file_dev && (stat(mnt->mnt_fsname, &st_buf) == 0) &&
66                     file_dev == st_buf.st_rdev)
67                         break;
68 #endif
69         }
70
71         if (mnt == 0) {
72 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
73                 /*
74                  * Do an extra check to see if this is the root device.  We
75                  * can't trust /etc/mtab, and /proc/mounts will only list
76                  * /dev/root for the root filesystem.  Argh.  Instead we
77                  * check if the given device has the same major/minor number
78                  * as the device that the root directory is on.
79                  */
80                 if (file_dev && stat("/", &st_buf) == 0) {
81                         if (st_buf.st_dev == file_dev) {
82                                 *mount_flags = EXT2_MF_MOUNTED;
83                                 if (mtpt)
84                                         strncpy(mtpt, "/", mtlen);
85                                 goto is_root;
86                         }
87                 }
88 #endif
89                 goto exit;
90         }
91 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
92         /* Validate the entry in case /etc/mtab is out of date */
93         /* 
94          * We need to be paranoid, because some broken distributions
95          * (read: Slackware) don't initialize /etc/mtab before checking
96          * all of the non-root filesystems on the disk.
97          */
98         if (stat(mnt->mnt_dir, &st_buf) < 0) {
99                 retval = errno;
100                 if (retval == ENOENT) {
101 #ifdef DEBUG
102                         printf("Bogus entry in %s!  (%s does not exist)\n",
103                                mtab_file, mnt->mnt_dir);
104 #endif
105                         retval = 0;
106                 }
107                 goto exit;
108         }
109         if (file_dev && (st_buf.st_dev != file_dev)) {
110 #ifdef DEBUG
111                 printf("Bogus entry in %s!  (%s not mounted on %s)\n",
112                        mtab_file, file, mnt->mnt_dir);
113 #endif
114                 goto exit;
115         }
116 #endif
117         *mount_flags = EXT2_MF_MOUNTED;
118         
119         /* Check to see if the ro option is set */
120         if (hasmntopt(mnt, MNTOPT_RO))
121                 *mount_flags |= EXT2_MF_READONLY;
122
123         if (mtpt)
124                 strncpy(mtpt, mnt->mnt_dir, mtlen);
125         /*
126          * Check to see if we're referring to the root filesystem.
127          * If so, do a manual check to see if we can open /etc/mtab
128          * read/write, since if the root is mounted read/only, the
129          * contents of /etc/mtab may not be accurate.
130          */
131         if (!strcmp(mnt->mnt_dir, "/")) {
132 is_root:
133 #define TEST_FILE "/.ismount-test-file"         
134                 *mount_flags |= EXT2_MF_ISROOT;
135                 fd = open(TEST_FILE, O_RDWR|O_CREAT);
136                 if (fd < 0) {
137                         if (errno == EROFS)
138                                 *mount_flags |= EXT2_MF_READONLY;
139                 } else
140                         close(fd);
141                 (void) unlink(TEST_FILE);
142         }
143         retval = 0;
144 exit:
145         endmntent (f);
146         return retval;
147 }
148
149 /*
150  * Check to see if we're dealing with the swap device.
151  */
152 static int is_swap_device(const char *file)
153 {
154         FILE            *f;
155         char            buf[1024], *cp;
156         dev_t           file_dev;
157         struct stat     st_buf;
158         int             ret = 0;
159
160         file_dev = 0;
161 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
162         if (stat(file, &st_buf) == 0)
163                 file_dev = st_buf.st_rdev;
164 #endif  
165
166         if (!(f = fopen("/proc/swaps", "r")))
167                 return 0;
168         /* Skip the first line */
169         fgets(buf, sizeof(buf), f);
170         while (!feof(f)) {
171                 if (!fgets(buf, sizeof(buf), f))
172                         break;
173                 if ((cp = strchr(buf, ' ')) != NULL)
174                         *cp = 0;
175                 if ((cp = strchr(buf, '\t')) != NULL)
176                         *cp = 0;
177                 if (strcmp(buf, file) == 0) {
178                         ret++;
179                         break;
180                 }
181 #ifndef __GNU__
182                 if (file_dev && (stat(buf, &st_buf) == 0) &&
183                     file_dev == st_buf.st_rdev) {
184                         ret++;
185                         break;
186                 }
187 #endif
188         }
189         fclose(f);
190         return ret;
191 }
192
193 static errcode_t check_mntent(const char *file, int *mount_flags,
194                               char *mtpt, int mtlen)
195 {
196         errcode_t       retval;
197
198 #ifdef DEBUG
199         retval = check_mntent_file("/tmp/mtab", file, mount_flags,
200                                    mtpt, mtlen);
201         if (retval == 0)
202                 return 0;
203 #endif
204 #ifdef __linux__
205         retval = check_mntent_file("/proc/mounts", file, mount_flags,
206                                    mtpt, mtlen);
207         if (retval == 0)
208                 return 0;
209 #endif
210         retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
211         return retval;
212 }
213
214 #elif defined(HAVE_GETMNTINFO)
215
216 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
217                                   char *mtpt, int mtlen)
218 {
219         struct statfs *mp;
220         int    len, n;
221         const  char   *s1;
222         char    *s2;
223
224         n = getmntinfo(&mp, MNT_NOWAIT);
225         if (n == 0)
226                 return errno;
227
228         len = sizeof(_PATH_DEV) - 1;
229         s1 = file;
230         if (strncmp(_PATH_DEV, s1, len) == 0)
231                 s1 += len;
232  
233         *mount_flags = 0;
234         while (--n >= 0) {
235                 s2 = mp->f_mntfromname;
236                 if (strncmp(_PATH_DEV, s2, len) == 0) {
237                         s2 += len - 1;
238                         *s2 = 'r';
239                 }
240                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
241                         *mount_flags = EXT2_MF_MOUNTED;
242                         break;
243                 }
244                 ++mp;
245         }
246         if (mtpt)
247                 strncpy(mtpt, mp->f_mntonname, mtlen);
248         return 0;
249 }
250 #endif /* HAVE_GETMNTINFO */
251
252 /*
253  * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
254  * otherwise.  If mtpt is non-NULL, the directory where the device is
255  * mounted is copied to where mtpt is pointing, up to mtlen
256  * characters.
257  */
258 #ifdef __TURBOC__
259  #pragma argsused
260 #endif
261 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
262                                   char *mtpt, int mtlen)
263 {
264         if (is_swap_device(device)) {
265                 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
266                 strncpy(mtpt, "<swap>", mtlen);
267                 return 0;
268         }
269 #ifdef HAVE_MNTENT_H
270         return check_mntent(device, mount_flags, mtpt, mtlen);
271 #else 
272 #ifdef HAVE_GETMNTINFO
273         return check_getmntinfo(device, mount_flags, mtpt, mtlen);
274 #else
275 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
276         *mount_flags = 0;
277         return 0;
278 #endif /* HAVE_GETMNTINFO */
279 #endif /* HAVE_MNTENT_H */
280 }
281
282 /*
283  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
284  * EXT2_MF_READONLY, and EXT2_MF_ROOT
285  * 
286  */
287 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
288 {
289         return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
290 }
291
292 #ifdef DEBUG
293 int main(int argc, char **argv)
294 {
295         int     retval, mount_flags;
296         char    mntpt[80];
297         
298         if (argc < 2) {
299                 fprintf(stderr, "Usage: %s device\n", argv[0]);
300                 exit(1);
301         }
302
303         mntpt[0] = 0;
304         retval = ext2fs_check_mount_point(argv[1], &mount_flags,
305                                           mntpt, sizeof(mntpt));
306         if (retval) {
307                 com_err(argv[0], retval,
308                         "while calling ext2fs_check_if_mounted");
309                 exit(1);
310         }
311         printf("Device %s reports flags %02x\n", argv[1], mount_flags);
312         if (mount_flags & EXT2_MF_MOUNTED)
313                 printf("\t%s is mounted.\n", argv[1]);
314
315         if (mount_flags & EXT2_MF_SWAP)
316                 printf("\t%s is a swap device.\n", argv[1]);
317
318         if (mount_flags & EXT2_MF_READONLY)
319                 printf("\t%s is read-only.\n", argv[1]);
320         
321         if (mount_flags & EXT2_MF_ISROOT)
322                 printf("\t%s is the root filesystem.\n", argv[1]);
323         if (mntpt[0])
324                 printf("\t%s is mounted on %s.\n", argv[1], mntpt);
325         
326         exit(0);
327 }
328 #endif