Upload Tizen:Base source
[framework/base/util-linux-ng.git] / lib / ismounted.c
1 /*
2  * ismounted.c --- Check to see if the filesystem was mounted
3  *
4  * Copyright (C) 1995,1996,1997,1998,1999,2000,2008 Theodore Ts'o.
5  *
6  * This file may be redistributed under the terms of the GNU Public
7  * License.
8  */
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <mntent.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <ctype.h>
18
19 #include "pathnames.h"
20 #include "ismounted.h"
21
22 /*
23  * ext2fs_check_if_mounted flags
24  */
25 #define MF_MOUNTED              1
26
27 static char *skip_over_blank(char *cp)
28 {
29         while (*cp && isspace(*cp))
30                 cp++;
31         return cp;
32 }
33
34 static char *skip_over_word(char *cp)
35 {
36         while (*cp && !isspace(*cp))
37                 cp++;
38         return cp;
39 }
40
41 static char *parse_word(char **buf)
42 {
43         char *word, *next;
44
45         word = *buf;
46         if (*word == 0)
47                 return 0;
48
49         word = skip_over_blank(word);
50         next = skip_over_word(word);
51         if (*next)
52                 *next++ = 0;
53         *buf = next;
54         return word;
55 }
56
57 /*
58  * Helper function which checks a file in /etc/mtab format to see if a
59  * filesystem is mounted.  Returns an error if the file doesn't exist
60  * or can't be opened.
61  */
62 static int check_mntent_file(const char *mtab_file, const char *file,
63                                    int *mount_flags)
64 {
65         struct stat     st_buf;
66         int             retval = 0;
67         dev_t           file_dev=0, file_rdev=0;
68         ino_t           file_ino=0;
69         FILE            *f;
70         char            buf[1024], *device = 0, *mnt_dir = 0, *cp;
71
72         *mount_flags = 0;
73         if ((f = setmntent (mtab_file, "r")) == NULL)
74                 return errno;
75         if (stat(file, &st_buf) == 0) {
76                 if (S_ISBLK(st_buf.st_mode)) {
77 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
78                         file_rdev = st_buf.st_rdev;
79 #endif  /* __GNU__ */
80                 } else {
81                         file_dev = st_buf.st_dev;
82                         file_ino = st_buf.st_ino;
83                 }
84         }
85         while (1) {
86                 if (!fgets(buf, sizeof(buf), f)) {
87                         device = mnt_dir = 0;
88                         break;
89                 }
90                 buf[sizeof(buf)-1] = 0;
91
92                 cp = buf;
93                 device = parse_word(&cp);
94                 if (!device || *device == '#')
95                         return 0;       /* Ignore blank lines and comments */
96                 mnt_dir = parse_word(&cp);
97
98                 if (device[0] != '/')
99                         continue;
100
101                 if (strcmp(file, device) == 0)
102                         break;
103                 if (stat(device, &st_buf) == 0) {
104                         if (S_ISBLK(st_buf.st_mode)) {
105 #ifndef __GNU__
106                                 if (file_rdev && (file_rdev == st_buf.st_rdev))
107                                         break;
108 #endif  /* __GNU__ */
109                         } else {
110                                 if (file_dev && ((file_dev == st_buf.st_dev) &&
111                                                  (file_ino == st_buf.st_ino)))
112                                         break;
113                         }
114                 }
115         }
116
117         if (mnt_dir == 0) {
118 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
119                 /*
120                  * Do an extra check to see if this is the root device.  We
121                  * can't trust /etc/mtab, and /proc/mounts will only list
122                  * /dev/root for the root filesystem.  Argh.  Instead we
123                  * check if the given device has the same major/minor number
124                  * as the device that the root directory is on.
125                  */
126                 if (file_rdev && (stat("/", &st_buf) == 0) &&
127                     (st_buf.st_dev == file_rdev))
128                         *mount_flags = MF_MOUNTED;
129 #endif  /* __GNU__ */
130                 goto errout;
131         }
132 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
133         /* Validate the entry in case /etc/mtab is out of date */
134         /*
135          * We need to be paranoid, because some broken distributions
136          * (read: Slackware) don't initialize /etc/mtab before checking
137          * all of the non-root filesystems on the disk.
138          */
139         if (stat(mnt_dir, &st_buf) < 0) {
140                 retval = errno;
141                 if (retval == ENOENT) {
142 #ifdef DEBUG
143                         printf("Bogus entry in %s!  (%s does not exist)\n",
144                                mtab_file, mnt_dir);
145 #endif /* DEBUG */
146                         retval = 0;
147                 }
148                 goto errout;
149         }
150         if (file_rdev && (st_buf.st_dev != file_rdev)) {
151 #ifdef DEBUG
152                 printf("Bogus entry in %s!  (%s not mounted on %s)\n",
153                        mtab_file, file, mnt_dir);
154 #endif /* DEBUG */
155                 goto errout;
156         }
157 #endif /* __GNU__ */
158         *mount_flags = MF_MOUNTED;
159
160         retval = 0;
161 errout:
162         endmntent (f);
163         return retval;
164 }
165
166 int is_mounted(const char *file)
167 {
168         int     retval;
169         int     mount_flags = 0;
170
171 #ifdef __linux__
172         retval = check_mntent_file(_PATH_PROC_MOUNTS, file, &mount_flags);
173         if (retval)
174                 return 0;
175         if (mount_flags)
176                 return 1;
177 #endif /* __linux__ */
178         retval = check_mntent_file(_PATH_MOUNTED, file, &mount_flags);
179         if (retval)
180                 return 0;
181         return mount_flags;
182 }
183
184 #ifdef TEST_PROGRAM
185 int main(int argc, char **argv)
186 {
187         if (argc < 2) {
188                 fprintf(stderr, "Usage: %s device\n", argv[0]);
189                 return EXIT_FAILURE;
190         }
191
192         if (is_mounted(argv[1])) {
193                 printf("mounted\n");
194                 return EXIT_SUCCESS;
195         }
196         printf("not mounted\n");
197         return EXIT_FAILURE;
198 }
199 #endif /* DEBUG */