]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/ismounted.c
2 * ismounted.c --- Check to see if the filesystem was mounted
4 * Copyright (C) 1995,1996,1997,1998,1999,2000,2008 Theodore Ts'o.
6 * This file may be redistributed under the terms of the GNU Public
20 #include "pathnames.h"
21 #include "ismounted.h"
24 * ext2fs_check_if_mounted flags
28 static char *skip_over_blank(char *cp
)
30 while (*cp
&& isspace(*cp
))
35 static char *skip_over_word(char *cp
)
37 while (*cp
&& !isspace(*cp
))
42 static char *parse_word(char **buf
)
50 word
= skip_over_blank(word
);
51 next
= skip_over_word(word
);
59 * Helper function which checks a file in /etc/mtab format to see if a
60 * filesystem is mounted. Returns an error if the file doesn't exist
63 static int check_mntent_file(const char *mtab_file
, const char *file
,
68 dev_t file_dev
=0, file_rdev
=0;
71 char buf
[1024], *device
= 0, *mnt_dir
= 0, *cp
;
74 if ((f
= fopen(mtab_file
, "r")) == NULL
)
77 if ((f
= setmntent (mtab_file
, "r")) == NULL
)
79 if (stat(file
, &st_buf
) == 0) {
80 if (S_ISBLK(st_buf
.st_mode
)) {
81 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
82 file_rdev
= st_buf
.st_rdev
;
85 file_dev
= st_buf
.st_dev
;
86 file_ino
= st_buf
.st_ino
;
90 if (!fgets(buf
, sizeof(buf
), f
)) {
94 buf
[sizeof(buf
)-1] = 0;
97 device
= parse_word(&cp
);
98 if (!device
|| *device
== '#')
99 return 0; /* Ignore blank lines and comments */
100 mnt_dir
= parse_word(&cp
);
102 if (device
[0] != '/')
105 if (strcmp(file
, device
) == 0)
107 if (stat(device
, &st_buf
) == 0) {
108 if (S_ISBLK(st_buf
.st_mode
)) {
110 if (file_rdev
&& (file_rdev
== st_buf
.st_rdev
))
114 if (file_dev
&& ((file_dev
== st_buf
.st_dev
) &&
115 (file_ino
== st_buf
.st_ino
)))
122 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
124 * Do an extra check to see if this is the root device. We
125 * can't trust /etc/mtab, and /proc/mounts will only list
126 * /dev/root for the root filesystem. Argh. Instead we
127 * check if the given device has the same major/minor number
128 * as the device that the root directory is on.
130 if (file_rdev
&& (stat("/", &st_buf
) == 0) &&
131 (st_buf
.st_dev
== file_rdev
))
132 *mount_flags
= MF_MOUNTED
;
136 #ifndef __GNU__ /* The GNU hurd is deficient; what else is new? */
137 /* Validate the entry in case /etc/mtab is out of date */
139 * We need to be paranoid, because some broken distributions
140 * (read: Slackware) don't initialize /etc/mtab before checking
141 * all of the non-root filesystems on the disk.
143 if (stat(mnt_dir
, &st_buf
) < 0) {
145 if (retval
== ENOENT
) {
147 printf("Bogus entry in %s! (%s does not exist)\n",
154 if (file_rdev
&& (st_buf
.st_dev
!= file_rdev
)) {
156 printf("Bogus entry in %s! (%s not mounted on %s)\n",
157 mtab_file
, file
, mnt_dir
);
162 *mount_flags
= MF_MOUNTED
;
170 int is_mounted(const char *file
)
176 retval
= check_mntent_file(_PATH_PROC_MOUNTS
, file
, &mount_flags
);
181 #endif /* __linux__ */
182 retval
= check_mntent_file(_PATH_MOUNTED
, file
, &mount_flags
);
189 int main(int argc
, char **argv
)
192 fprintf(stderr
, "Usage: %s device\n", argv
[0]);
196 if (is_mounted(argv
[1]))
197 printf("\t%s is mounted.\n", argv
[1]);