Use /proc/mounts instead of /etc/mtab
Subsystems (2)
PATCH 1/1 Use /proc/mounts instead of /etc/mtab
Hi, Ian, list,
Here is a patch to consult /proc/mounts instead of /etc/mtab when deciding if a path has already been mounted. The problem with using /etc/mtab, as you know, is that it sometimes does not get updated. We know that /proc/mounts will always be right, so why not use it?
The patch will determine if /proc/mounts is available at run-time, and so will continue to work in the event that proc isn't available.
Comments welcome.
-Jeff
daemon/automount.c +14 -2
@@ -333,7 +333,7 @@ static int umount_multi(const char *pathdebug ("umount_multi: path=%s incl=%d\n" , path, incl); - mntlist =get_mnt_list (_PATH_MOUNTED, path, incl); + mntlist =get_mnt_list (path_mounted, path, incl);if (!mntlist) {warn ("umount_multi: no mounts found under %s" , path);
@@ -445,7 +445,7 @@ static int mount_autofs(char *path)struct stat st;int len; -if ((ap.state !=ST_INIT ) ||is_mounted (_PATH_MOUNTED, path)) { +if ((ap.state !=ST_INIT ) ||is_mounted (path_mounted, path)) {/* This can happen if an autofs process is already running*/ error ("mount_autofs: already mounted" );return -1 ;
@@ -1844,6 +1844,16 @@ int handle_mounts(char *path)return 0 ; } +void init_path_mounted (void ) +{ +struct stat st; + +if (stat (PROC_MOUNTS , &st) ==0 ) + path_mounted =PROC_MOUNTS ; +else + path_mounted = _PATH_MOUNTED; +} +int main (int argc,char *argv[]) {char *path, *map, *mapfmt;
@@ -1928,6 +1938,8 @@ int main(int argc, char *argv[])if (!dumpmap)become_daemon (); +init_path_mounted (); + path = argv[0 ]; map = argv[1 ]; mapargv = (const char **) &argv[2 ];
daemon/spawn.c +1 -1
@@ -119,7 +119,7 @@ void discard_pending(int sig)*/ int signal_children (int sig) { -struct mnt_list *mnts =get_mnt_list (_PATH_MOUNTED,"/" ,0 ); +struct mnt_list *mnts =get_mnt_list (path_mounted,"/" ,0 );struct mnt_list *next;pid_t pgrp =getpgrp ();int ret =-1 ;
include/automount.h +2 -0
@@ -40,6 +40,7 @@ #endif#define AUTOFS_SUPER_MAGIC 0x00000187L#define DEFAULT_TIMEOUT (5*60)/* 5 minutes */ +#define PROC_MOUNTS "/proc/mounts"#define AUTOFS_LOCK "/var/lock/autofs"/* To serialize access to mount */ #define MOUNTED_LOCK _PATH_MOUNTED "~"/* mounts' lock file */ #define MTAB_NOTUPDATED 0x1000/* mtab succeded but not updated */
@@ -121,6 +122,7 @@ struct autofs_point { };extern struct autofs_point ap; +const char *path_mounted;/* Standard function used by daemon or modules */
lib/cache.c +2 -2
@@ -268,7 +268,7 @@ int cache_delete(const char *root, constif (!path)return CHE_FAIL ; -if (is_mounted (_PATH_MOUNTED, path)) { +if (is_mounted (path_mounted, path)) {free (path);return CHE_FAIL ; }
@@ -439,7 +439,7 @@ int cache_ghost(const char *root, int ghbreak ;case LKP_MOUNT : -if (!is_mounted (_PATH_MOUNTED, gc.direct_base)) { +if (!is_mounted (path_mounted, gc.direct_base)) {debug ("cache_ghost: attempting to mount map, " "key %s" , gc.direct_base);
lib/mounts.c +5 -3
@@ -294,7 +294,7 @@ int contained_in_local_fs(const char *paif (!path || !pathlen || pathlen >PATH_MAX )return 0 ; - mnts =get_mnt_list (_PATH_MOUNTED,"/" ,1 ); + mnts =get_mnt_list (path_mounted,"/" ,1 );if (!mnts)return 0 ;
@@ -309,13 +309,15 @@ int contained_in_local_fs(const char *pa rv =statfs (this->path, &fs);if (rv !=-1 && fs.f_type ==AUTOFS_SUPER_MAGIC ) ret =1 ; +/* What is the below testing? --JEM */ else if (this->fs_name[0 ] =='/' ) {if (strlen (this->fs_name) >1 ) {if (this->fs_name[1 ] !='/' ) ret =1 ; }else ret =1 ; - } + }else if (!strncmp (this->fs_name,"rootfs" ,6 )) + ret =1 ;break ; } }
@@ -357,7 +359,7 @@ int allow_owner_mount(const char *path)struct mntent ent;int ret =0 ; -if (getuid () ||is_mounted (_PATH_MOUNTED, path)) +if (getuid () ||is_mounted (path_mounted, path))return 0 ;if (find_mntent (_PATH_MNTTAB, path, &ent)) {
modules/mount_autofs.c +1 -1
@@ -71,7 +71,7 @@ int mount_mount(const char *root, constdebug (MODPREFIX "fullpath=%s what=%s options=%s" , fullpath, what, options); -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: about to mount over %s, continuing" , fullpath);return 0 ;
modules/mount_bind.c +1 -1
@@ -121,7 +121,7 @@ int mount_mount(const char *root, constif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ;
modules/mount_ext2.c +1 -1
@@ -69,7 +69,7 @@ int mount_mount(const char *root, constif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ;
modules/mount_generic.c +1 -1
@@ -68,7 +68,7 @@ int mount_mount(const char *root, constif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ; }
modules/mount_nfs.c +1 -1
@@ -596,7 +596,7 @@ #endifif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ;
Ian Kent
maintainerQuoted 6 lines · Jeff Moyer
Hi, Ian, list,
Here is a patch to consult
/proc/mountsinstead of/etc/mtabwhen deciding if a path has already been mounted. The problem with using/etc/mtab, as you know, is that it sometimes does not get updated. We know that/proc/mountswill always be right, so why not use it?
Good for v4 but keep in mind that in v5 /proc/mounts can have a lot of entries compared to /etc/mtab. The cost of doing this can be quite high. mount(8) must play well with mtab for v5.
Quoted 216 lines
The patch will determine if
/proc/mountsis available at run-time, and so will continue to work in the event that proc isn't available.Comments welcome.
(quoted diff) +0
-1-Jeffdaemon/automount.c +14
-2@@ -333,7 +333,7 @@ static int umount_multi(const char *pathdebug ("umount_multi: path=%s incl=%d\n" , path, incl); - mntlist =get_mnt_list (_PATH_MOUNTED, path, incl); + mntlist =get_mnt_list (path_mounted, path, incl);if (!mntlist) {warn ("umount_multi: no mounts found under %s" , path);@@ -445,7 +445,7 @@ static int mount_autofs(char *path)struct stat st;int len; -if ((ap.state !=ST_INIT ) ||is_mounted (_PATH_MOUNTED, path)) { +if ((ap.state !=ST_INIT ) ||is_mounted (path_mounted, path)) {/* This can happen if an autofs process is already running*/ error ("mount_autofs: already mounted" );return -1 ;@@ -1844,6 +1844,16 @@ int handle_mounts(char *path)return 0 ; } +void init_path_mounted (void ) +{ +struct stat st; + +if (stat (PROC_MOUNTS , &st) ==0 ) + path_mounted =PROC_MOUNTS ; +else + path_mounted = _PATH_MOUNTED; +} +int main (int argc,char *argv[]) {char *path, *map, *mapfmt;@@ -1928,6 +1938,8 @@ int main(int argc, char *argv[])if (!dumpmap)become_daemon (); +init_path_mounted (); + path = argv[0 ]; map = argv[1 ]; mapargv = (const char **) &argv[2 ];daemon/spawn.c +1
-1@@ -119,7 +119,7 @@ void discard_pending(int sig)*/ int signal_children (int sig) { -struct mnt_list *mnts =get_mnt_list (_PATH_MOUNTED,"/" ,0 ); +struct mnt_list *mnts =get_mnt_list (path_mounted,"/" ,0 );struct mnt_list *next;pid_t pgrp =getpgrp ();int ret =-1 ;include/automount.h +2
-0@@ -40,6 +40,7 @@ #endif#define AUTOFS_SUPER_MAGIC 0x00000187L#define DEFAULT_TIMEOUT (5*60)/* 5 minutes */ +#define PROC_MOUNTS "/proc/mounts"#define AUTOFS_LOCK "/var/lock/autofs"/* To serialize access to mount */ #define MOUNTED_LOCK _PATH_MOUNTED "~"/* mounts' lock file */ #define MTAB_NOTUPDATED 0x1000/* mtab succeded but not updated */ @@ -121,6 +122,7 @@ struct autofs_point { };extern struct autofs_point ap; +const char *path_mounted;/* Standard function used by daemon or modules */ lib/cache.c +2
-2@@ -268,7 +268,7 @@ int cache_delete(const char *root, constif (!path)return CHE_FAIL ; -if (is_mounted (_PATH_MOUNTED, path)) { +if (is_mounted (path_mounted, path)) {free (path);return CHE_FAIL ; }@@ -439,7 +439,7 @@ int cache_ghost(const char *root, int ghbreak ;case LKP_MOUNT : -if (!is_mounted (_PATH_MOUNTED, gc.direct_base)) { +if (!is_mounted (path_mounted, gc.direct_base)) {debug ("cache_ghost: attempting to mount map, " "key %s" , gc.direct_base);lib/mounts.c +5
-3@@ -294,7 +294,7 @@ int contained_in_local_fs(const char *paif (!path || !pathlen || pathlen >PATH_MAX )return 0 ; - mnts =get_mnt_list (_PATH_MOUNTED,"/" ,1 ); + mnts =get_mnt_list (path_mounted,"/" ,1 );if (!mnts)return 0 ;@@ -309,13 +309,15 @@ int contained_in_local_fs(const char *pa rv =statfs (this->path, &fs);if (rv !=-1 && fs.f_type ==AUTOFS_SUPER_MAGIC ) ret =1 ; +/* What is the below testing? --JEM */ else if (this->fs_name[0 ] =='/' ) {if (strlen (this->fs_name) >1 ) {if (this->fs_name[1 ] !='/' ) ret =1 ; }else ret =1 ; - } + }else if (!strncmp (this->fs_name,"rootfs" ,6 )) + ret =1 ;break ; } }@@ -357,7 +359,7 @@ int allow_owner_mount(const char *path)struct mntent ent;int ret =0 ; -if (getuid () ||is_mounted (_PATH_MOUNTED, path)) +if (getuid () ||is_mounted (path_mounted, path))return 0 ;if (find_mntent (_PATH_MNTTAB, path, &ent)) {modules/mount_autofs.c +1
-1@@ -71,7 +71,7 @@ int mount_mount(const char *root, constdebug (MODPREFIX "fullpath=%s what=%s options=%s" , fullpath, what, options); -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: about to mount over %s, continuing" , fullpath);return 0 ;modules/mount_bind.c +1
-1@@ -121,7 +121,7 @@ int mount_mount(const char *root, constif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ;modules/mount_ext2.c +1
-1@@ -69,7 +69,7 @@ int mount_mount(const char *root, constif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ;modules/mount_generic.c +1
-1@@ -68,7 +68,7 @@ int mount_mount(const char *root, constif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ; }modules/mount_nfs.c +1
-1@@ -596,7 +596,7 @@ #endifif (!status) existed =0 ; -if (is_mounted (_PATH_MOUNTED, fullpath)) { +if (is_mounted (path_mounted, fullpath)) {error (MODPREFIX "warning: %s is already mounted" , fullpath);return 0 ;
==> On Thu, 09 Nov 2006 11:17:09 +0800, Ian Kent <[email protected]> said:
Ian> On Wed, 2006-11-08 at 22:03 -0500, Jeff Moyer wrote: > Hi, Ian, Ian> list,
Ian> >
Ian> > Here is a patch to consult /proc/mounts instead of /etc/mtab Ian> when > deciding if a path has already been mounted. The problem Ian> with using > /etc/mtab, as you know, is that it sometimes does Ian> not get updated. We > know that /proc/mounts will always be Ian> right, so why not use it?
Ian> Good for v4 but keep in mind that in v5 /proc/mounts can have a Ian> lot of entries compared to /etc/mtab. The cost of doing this can Ian> be quite high. mount(8) must play well with mtab for v5.
It's not just mount(8), but also the caller's responsibility. You have to be sure to check for the mtab not updated return value, and issue another mount call to try to update it.
-Jeff
Ian Kent
maintainerQuoted 11 lines · Jeff Moyer
==> On Thu, 09 Nov 2006 11:17:09 +0800, Ian Kent <[email protected]> said:
Ian> On Wed, 2006-11-08 at 22:03 -0500, Jeff Moyer wrote: > Hi, Ian, Ian> list,
Ian> >
Ian> > Here is a patch to consult/proc/mountsinstead of/etc/mtabIan> when > deciding if a path has already been mounted. The problem Ian> with using >/etc/mtab, as you know, is that it sometimes does Ian> not get updated. We > know that/proc/mountswill always be Ian> right, so why not use it?
Quoted 7 lines
Ian> Good for v4 but keep in mind that in v5
/proc/mountscan have a Ian> lot of entries compared to/etc/mtab. The cost of doing this can Ian> be quite high. mount(8) must play well with mtab for v5.It's not just mount(8), but also the caller's responsibility. You have to be sure to check for the mtab not updated return value, and issue another mount call to try to update it.
Ya .. been there before.
I'll need to check that.
Ian