blob: a65778e79aefd99ae7b5725c24e740d3e4d55a0d [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00002 * ismounted.c --- Check to see if the filesystem was mounted
Theodore Ts'o50e1e101997-04-26 13:58:21 +00003 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * 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%
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010 */
11
12#include <stdio.h>
13#if HAVE_UNISTD_H
14#include <unistd.h>
15#endif
Theodore Ts'oc4e749a1998-02-20 05:33:14 +000016#if HAVE_ERRNO_H
17#include <errno.h>
18#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#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
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000035#if EXT2_FLAT_INCLUDES
36#include "ext2_fs.h"
37#else
Theodore Ts'o50e1e101997-04-26 13:58:21 +000038#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000039#endif
40
Theodore Ts'o50e1e101997-04-26 13:58:21 +000041#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
Theodore Ts'o297f47a1997-04-26 14:25:20 +000046 * root filesystem.
Theodore Ts'o50e1e101997-04-26 13:58:21 +000047 */
48static 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
79static 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 */
Theodore Ts'o3cb6c501997-08-11 20:29:22 +0000115#ifdef __TURBOC__
116#pragma argsused
117#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000118errcode_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}