~ubuntu-branches/ubuntu/trusty/nilfs-tools/trusty

« back to all changes in this revision

Viewing changes to lib/ismounted.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2011-07-10 17:10:21 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110710171021-xkbtr81jprb97iec
Tags: upstream-2.1.0~rc1
ImportĀ upstreamĀ versionĀ 2.1.0~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* check_mount() checks whether DEVICE is a mounted file system.
 
2
   Returns 0 if the DEVICE is *not* mounted (which we consider a
 
3
   successful outcome), and -1 if DEVICE is mounted or if the mount
 
4
   status cannot be determined.
 
5
 
 
6
   Derived from e2fsprogs/lib/ext2fs/ismounted.c
 
7
   Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o,
 
8
   LGPL v2
 
9
*/
 
10
#ifdef HAVE_CONFIG_H
 
11
#include "config.h"
 
12
#endif  /* HAVE_CONFIG_H */
 
13
 
 
14
#include <stdio.h>
 
15
 
 
16
#if HAVE_STDLIB_H
 
17
#include <stdlib.h>
 
18
#endif  /* HAVE_STDLIB_H */
 
19
 
 
20
#if HAVE_STRING_H
 
21
#include <string.h>
 
22
#endif  /* HAVE_STRING_H */
 
23
 
 
24
#ifdef HAVE_MNTENT_H
 
25
#include <mntent.h>
 
26
#endif
 
27
 
 
28
#include <sys/stat.h>
 
29
 
 
30
#define MOUNTS                  "/etc/mtab"
 
31
#define LINE_BUFFER_SIZE        256  /* Line buffer size for reading mtab */
 
32
 
 
33
int check_mount(const char *device)
 
34
{
 
35
        struct mntent *mnt;
 
36
        struct stat st_buf;
 
37
        FILE *f;
 
38
        dev_t file_dev = 0, file_rdev = 0;
 
39
        ino_t file_ino = 0;
 
40
 
 
41
        f = setmntent(MOUNTS, "r");
 
42
        if (f == NULL) {
 
43
                fprintf(stderr, "Error: cannot open %s!", MOUNTS);
 
44
                return -1;
 
45
        }
 
46
 
 
47
        if (stat(device, &st_buf) == 0) {
 
48
                if (S_ISBLK(st_buf.st_mode)) {
 
49
                        file_rdev = st_buf.st_rdev;
 
50
                } else {
 
51
                        file_dev = st_buf.st_dev;
 
52
                        file_ino = st_buf.st_ino;
 
53
                }
 
54
        }
 
55
 
 
56
        while ((mnt = getmntent(f)) != NULL) {
 
57
                if (mnt->mnt_fsname[0] != '/')
 
58
                        continue;
 
59
                if (strcmp(device, mnt->mnt_fsname) == 0)
 
60
                        break;
 
61
                if (stat(mnt->mnt_fsname, &st_buf) == 0) {
 
62
                        if (S_ISBLK(st_buf.st_mode)) {
 
63
                                if (file_rdev && (file_rdev == st_buf.st_rdev))
 
64
                                        break;
 
65
                        } else {
 
66
                                if (file_dev && ((file_dev == st_buf.st_dev) &&
 
67
                                                 (file_ino == st_buf.st_ino)))
 
68
                                        break;
 
69
                        }
 
70
                }
 
71
        }
 
72
 
 
73
        endmntent(f);
 
74
        return (mnt == NULL) ? 0 : -1;
 
75
}