source: MondoRescue/branches/2.2.9/mindi-busybox/e2fsprogs/old_e2fsprogs/ext2fs/ismounted.c@ 2725

Last change on this file since 2725 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * ismounted.c --- Check to see if the filesystem was mounted
4 *
5 * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#if HAVE_ERRNO_H
18#include <errno.h>
19#endif
20#include <fcntl.h>
21#ifdef HAVE_LINUX_FD_H
22#include <linux/fd.h>
23#endif
24#ifdef HAVE_MNTENT_H
25#include <mntent.h>
26#endif
27#ifdef HAVE_GETMNTINFO
28#include <paths.h>
29#include <sys/param.h>
30#include <sys/mount.h>
31#endif /* HAVE_GETMNTINFO */
32#include <string.h>
33#include <sys/stat.h>
34
35#include "ext2_fs.h"
36#include "ext2fs.h"
37
38#ifdef HAVE_MNTENT_H
39/*
40 * Helper function which checks a file in /etc/mtab format to see if a
41 * filesystem is mounted. Returns an error if the file doesn't exist
42 * or can't be opened.
43 */
44static errcode_t check_mntent_file(const char *mtab_file, const char *file,
45 int *mount_flags, char *mtpt, int mtlen)
46{
47 struct mntent *mnt;
48 struct stat st_buf;
49 errcode_t retval = 0;
50 dev_t file_dev=0, file_rdev=0;
51 ino_t file_ino=0;
52 FILE *f;
53 int fd;
54
55 *mount_flags = 0;
56 if ((f = setmntent (mtab_file, "r")) == NULL)
57 return errno;
58 if (stat(file, &st_buf) == 0) {
59 if (S_ISBLK(st_buf.st_mode)) {
60#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
61 file_rdev = st_buf.st_rdev;
62#endif /* __GNU__ */
63 } else {
64 file_dev = st_buf.st_dev;
65 file_ino = st_buf.st_ino;
66 }
67 }
68 while ((mnt = getmntent (f)) != NULL) {
69 if (strcmp(file, mnt->mnt_fsname) == 0)
70 break;
71 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
72 if (S_ISBLK(st_buf.st_mode)) {
73#ifndef __GNU__
74 if (file_rdev && (file_rdev == st_buf.st_rdev))
75 break;
76#endif /* __GNU__ */
77 } else {
78 if (file_dev && ((file_dev == st_buf.st_dev) &&
79 (file_ino == st_buf.st_ino)))
80 break;
81 }
82 }
83 }
84
85 if (mnt == 0) {
86#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
87 /*
88 * Do an extra check to see if this is the root device. We
89 * can't trust /etc/mtab, and /proc/mounts will only list
90 * /dev/root for the root filesystem. Argh. Instead we
91 * check if the given device has the same major/minor number
92 * as the device that the root directory is on.
93 */
94 if (file_rdev && stat("/", &st_buf) == 0) {
95 if (st_buf.st_dev == file_rdev) {
96 *mount_flags = EXT2_MF_MOUNTED;
97 if (mtpt)
98 strncpy(mtpt, "/", mtlen);
99 goto is_root;
100 }
101 }
102#endif /* __GNU__ */
103 goto errout;
104 }
105#ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
106 /* Validate the entry in case /etc/mtab is out of date */
107 /*
108 * We need to be paranoid, because some broken distributions
109 * (read: Slackware) don't initialize /etc/mtab before checking
110 * all of the non-root filesystems on the disk.
111 */
112 if (stat(mnt->mnt_dir, &st_buf) < 0) {
113 retval = errno;
114 if (retval == ENOENT) {
115#ifdef DEBUG
116 printf("Bogus entry in %s! (%s does not exist)\n",
117 mtab_file, mnt->mnt_dir);
118#endif /* DEBUG */
119 retval = 0;
120 }
121 goto errout;
122 }
123 if (file_rdev && (st_buf.st_dev != file_rdev)) {
124#ifdef DEBUG
125 printf("Bogus entry in %s! (%s not mounted on %s)\n",
126 mtab_file, file, mnt->mnt_dir);
127#endif /* DEBUG */
128 goto errout;
129 }
130#endif /* __GNU__ */
131 *mount_flags = EXT2_MF_MOUNTED;
132
133#ifdef MNTOPT_RO
134 /* Check to see if the ro option is set */
135 if (hasmntopt(mnt, MNTOPT_RO))
136 *mount_flags |= EXT2_MF_READONLY;
137#endif
138
139 if (mtpt)
140 strncpy(mtpt, mnt->mnt_dir, mtlen);
141 /*
142 * Check to see if we're referring to the root filesystem.
143 * If so, do a manual check to see if we can open /etc/mtab
144 * read/write, since if the root is mounted read/only, the
145 * contents of /etc/mtab may not be accurate.
146 */
147 if (LONE_CHAR(mnt->mnt_dir, '/')) {
148is_root:
149#define TEST_FILE "/.ismount-test-file"
150 *mount_flags |= EXT2_MF_ISROOT;
151 fd = open(TEST_FILE, O_RDWR|O_CREAT);
152 if (fd < 0) {
153 if (errno == EROFS)
154 *mount_flags |= EXT2_MF_READONLY;
155 } else
156 close(fd);
157 (void) unlink(TEST_FILE);
158 }
159 retval = 0;
160errout:
161 endmntent (f);
162 return retval;
163}
164
165static errcode_t check_mntent(const char *file, int *mount_flags,
166 char *mtpt, int mtlen)
167{
168 errcode_t retval;
169
170#ifdef DEBUG
171 retval = check_mntent_file("/tmp/mtab", file, mount_flags,
172 mtpt, mtlen);
173 if (retval == 0)
174 return 0;
175#endif /* DEBUG */
176#ifdef __linux__
177 retval = check_mntent_file("/proc/mounts", file, mount_flags,
178 mtpt, mtlen);
179 if (retval == 0 && (*mount_flags != 0))
180 return 0;
181#endif /* __linux__ */
182#if defined(MOUNTED) || defined(_PATH_MOUNTED)
183#ifndef MOUNTED
184#define MOUNTED _PATH_MOUNTED
185#endif /* MOUNTED */
186 retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
187 return retval;
188#else
189 *mount_flags = 0;
190 return 0;
191#endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
192}
193
194#else
195#if defined(HAVE_GETMNTINFO)
196
197static errcode_t check_getmntinfo(const char *file, int *mount_flags,
198 char *mtpt, int mtlen)
199{
200 struct statfs *mp;
201 int len, n;
202 const char *s1;
203 char *s2;
204
205 n = getmntinfo(&mp, MNT_NOWAIT);
206 if (n == 0)
207 return errno;
208
209 len = sizeof(_PATH_DEV) - 1;
210 s1 = file;
211 if (strncmp(_PATH_DEV, s1, len) == 0)
212 s1 += len;
213
214 *mount_flags = 0;
215 while (--n >= 0) {
216 s2 = mp->f_mntfromname;
217 if (strncmp(_PATH_DEV, s2, len) == 0) {
218 s2 += len - 1;
219 *s2 = 'r';
220 }
221 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
222 *mount_flags = EXT2_MF_MOUNTED;
223 break;
224 }
225 ++mp;
226 }
227 if (mtpt)
228 strncpy(mtpt, mp->f_mntonname, mtlen);
229 return 0;
230}
231#endif /* HAVE_GETMNTINFO */
232#endif /* HAVE_MNTENT_H */
233
234/*
235 * Check to see if we're dealing with the swap device.
236 */
237static int is_swap_device(const char *file)
238{
239 FILE *f;
240 char buf[1024], *cp;
241 dev_t file_dev;
242 struct stat st_buf;
243 int ret = 0;
244
245 file_dev = 0;
246#ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
247 if ((stat(file, &st_buf) == 0) &&
248 S_ISBLK(st_buf.st_mode))
249 file_dev = st_buf.st_rdev;
250#endif /* __GNU__ */
251
252 if (!(f = fopen_for_read("/proc/swaps")))
253 return 0;
254 /* Skip the first line */
255 fgets(buf, sizeof(buf), f);
256 while (!feof(f)) {
257 if (!fgets(buf, sizeof(buf), f))
258 break;
259 if ((cp = strchr(buf, ' ')) != NULL)
260 *cp = 0;
261 if ((cp = strchr(buf, '\t')) != NULL)
262 *cp = 0;
263 if (strcmp(buf, file) == 0) {
264 ret++;
265 break;
266 }
267#ifndef __GNU__
268 if (file_dev && (stat(buf, &st_buf) == 0) &&
269 S_ISBLK(st_buf.st_mode) &&
270 file_dev == st_buf.st_rdev) {
271 ret++;
272 break;
273 }
274#endif /* __GNU__ */
275 }
276 fclose(f);
277 return ret;
278}
279
280
281/*
282 * ext2fs_check_mount_point() returns 1 if the device is mounted, 0
283 * otherwise. If mtpt is non-NULL, the directory where the device is
284 * mounted is copied to where mtpt is pointing, up to mtlen
285 * characters.
286 */
287#ifdef __TURBOC__
288# pragma argsused
289#endif
290errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
291 char *mtpt, int mtlen)
292{
293 if (is_swap_device(device)) {
294 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
295 strncpy(mtpt, "<swap>", mtlen);
296 return 0;
297 }
298#ifdef HAVE_MNTENT_H
299 return check_mntent(device, mount_flags, mtpt, mtlen);
300#else
301#ifdef HAVE_GETMNTINFO
302 return check_getmntinfo(device, mount_flags, mtpt, mtlen);
303#else
304#ifdef __GNUC__
305 #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
306#endif
307 *mount_flags = 0;
308 return 0;
309#endif /* HAVE_GETMNTINFO */
310#endif /* HAVE_MNTENT_H */
311}
312
313/*
314 * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
315 * EXT2_MF_READONLY, and EXT2_MF_ROOT
316 *
317 */
318errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
319{
320 return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
321}
322
323#ifdef DEBUG
324int main(int argc, char **argv)
325{
326 int retval, mount_flags;
327 char mntpt[80];
328
329 if (argc < 2) {
330 fprintf(stderr, "Usage: %s device\n", argv[0]);
331 exit(1);
332 }
333
334 mntpt[0] = 0;
335 retval = ext2fs_check_mount_point(argv[1], &mount_flags,
336 mntpt, sizeof(mntpt));
337 if (retval) {
338 com_err(argv[0], retval,
339 "while calling ext2fs_check_if_mounted");
340 exit(1);
341 }
342 printf("Device %s reports flags %02x\n", argv[1], mount_flags);
343 if (mount_flags & EXT2_MF_BUSY)
344 printf("\t%s is apparently in use.\n", argv[1]);
345 if (mount_flags & EXT2_MF_MOUNTED)
346 printf("\t%s is mounted.\n", argv[1]);
347 if (mount_flags & EXT2_MF_SWAP)
348 printf("\t%s is a swap device.\n", argv[1]);
349 if (mount_flags & EXT2_MF_READONLY)
350 printf("\t%s is read-only.\n", argv[1]);
351 if (mount_flags & EXT2_MF_ISROOT)
352 printf("\t%s is the root filesystem.\n", argv[1]);
353 if (mntpt[0])
354 printf("\t%s is mounted on %s.\n", argv[1], mntpt);
355 exit(0);
356}
357#endif /* DEBUG */
Note: See TracBrowser for help on using the repository browser.