~ubuntu-branches/debian/jessie/arcboot/jessie

« back to all changes in this revision

Viewing changes to e2fslib/ismounted.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Guenther
  • Date: 2004-03-02 12:01:14 UTC
  • Revision ID: james.westby@ubuntu.com-20040302120114-0pukal9hlpt3k0l7
Tags: 0.3.8.1
correct subarch detection for IP32

Show diffs side-by-side

added added

removed removed

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