autofs-5.1.6 - add helper to construct mount point path From: Ian Kent Add convenience helper to construct mount point path. Signed-off-by: Ian Kent --- CHANGELOG | 1 + include/mounts.h | 3 +++ lib/mounts.c | 23 +++++++++++++++++++++++ modules/mount_bind.c | 19 +++++-------------- modules/mount_changer.c | 19 +++++-------------- modules/mount_ext2.c | 19 +++++-------------- modules/mount_generic.c | 19 +++++-------------- modules/mount_nfs.c | 21 ++++++++------------- 8 files changed, 55 insertions(+), 69 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 58f13c33..e38a1dc5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -69,6 +69,7 @@ xx/xx/2020 autofs-5.1.7 - move submount check into conditional_alarm_add(). - move lib/master.c to daemon/master.c. - use master_list_empty() for list empty check. +- add helper to construct mount point path. 07/10/2019 autofs-5.1.6 - support strictexpire mount option. diff --git a/include/mounts.h b/include/mounts.h index 6e0e96c9..e3022b23 100644 --- a/include/mounts.h +++ b/include/mounts.h @@ -94,6 +94,9 @@ unsigned int linux_version_code(void); int check_nfs_mount_version(struct nfs_mount_vers *, struct nfs_mount_vers *); extern unsigned int nfs_mount_uses_string_options; +int mount_fullpath(char *fullpath, size_t max_len, + const char *root, const char *name); + struct amd_entry; struct substvar *addstdenv(struct substvar *sv, const char *prefix); diff --git a/lib/mounts.c b/lib/mounts.c index e15e6da2..f823a3d6 100644 --- a/lib/mounts.c +++ b/lib/mounts.c @@ -339,6 +339,29 @@ int check_nfs_mount_version(struct nfs_mount_vers *vers, } #endif +int mount_fullpath(char *fullpath, size_t max_len, + const char *root, const char *name) +{ + int last, len; + + last = strlen(root) - 1; + + /* Root offset of multi-mount or direct or offset mount. + * Direct or offset mount, name (or root) is absolute path. + */ + if (root[last] == '/' || *name == '/') + len = snprintf(fullpath, max_len, "%s", root); + else + len = snprintf(fullpath, max_len, "%s/%s", root, name); + + if (len >= max_len) + return 0; + + fullpath[len] = '\0'; + + return len; +} + static char *set_env_name(const char *prefix, const char *name, char *buf) { size_t len; diff --git a/modules/mount_bind.c b/modules/mount_bind.c index 5253501c..c17c6f18 100644 --- a/modules/mount_bind.c +++ b/modules/mount_bind.c @@ -122,21 +122,12 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int } } - /* Root offset of multi-mount */ - len = strlen(root); - if (root[len - 1] == '/') { - len = snprintf(fullpath, len, "%s", root); - } else if (*name == '/') { - /* - * Direct or offset mount, name is absolute path so - * don't use root (but with move mount changes root - * is now the same as name). - */ - len = sprintf(fullpath, "%s", root); - } else { - len = sprintf(fullpath, "%s/%s", root, name); + len = mount_fullpath(fullpath, PATH_MAX, root, name); + if (!len) { + error(ap->logopt, + MODPREFIX "mount point path too long"); + return 1; } - fullpath[len] = '\0'; i = len; while (--i > 0 && fullpath[i] == '/') diff --git a/modules/mount_changer.c b/modules/mount_changer.c index 7d44a720..d02b5f45 100644 --- a/modules/mount_changer.c +++ b/modules/mount_changer.c @@ -59,21 +59,12 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int fstype = "iso9660"; - /* Root offset of multi-mount */ - len = strlen(root); - if (root[len - 1] == '/') { - len = snprintf(fullpath, len, "%s", root); - } else if (*name == '/') { - /* - * Direct or offset mount, name is absolute path so - * don't use root (but with move mount changes root - * is now the same as name). - */ - len = sprintf(fullpath, "%s", root); - } else { - len = sprintf(fullpath, "%s/%s", root, name); + len = mount_fullpath(fullpath, PATH_MAX, root, name); + if (!len) { + error(ap->logopt, + MODPREFIX "mount point path too long"); + return 1; } - fullpath[len] = '\0'; debug(ap->logopt, MODPREFIX "calling umount %s", what); diff --git a/modules/mount_ext2.c b/modules/mount_ext2.c index eb28b06c..53e6ee10 100644 --- a/modules/mount_ext2.c +++ b/modules/mount_ext2.c @@ -55,21 +55,12 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int if (defaults_get_mount_verbose()) mountlog = &log_info; - /* Root offset of multi-mount */ - len = strlen(root); - if (root[len - 1] == '/') { - len = snprintf(fullpath, len, "%s", root); - } else if (*name == '/') { - /* - * Direct or offset mount, name is absolute path so - * don't use root (but with move mount changes root - * is now the same as name). - */ - len = sprintf(fullpath, "%s", root); - } else { - len = sprintf(fullpath, "%s/%s", root, name); + len = mount_fullpath(fullpath, PATH_MAX, root, name); + if (!len) { + error(ap->logopt, + MODPREFIX "mount point path too long"); + return 1; } - fullpath[len] = '\0'; debug(ap->logopt, MODPREFIX "calling mkdir_path %s", fullpath); diff --git a/modules/mount_generic.c b/modules/mount_generic.c index cf531145..c9deb7ae 100644 --- a/modules/mount_generic.c +++ b/modules/mount_generic.c @@ -54,21 +54,12 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int if (defaults_get_mount_verbose()) mountlog = &log_info; - /* Root offset of multi-mount */ - len = strlen(root); - if (root[len - 1] == '/') { - len = snprintf(fullpath, len, "%s", root); - } else if (*name == '/') { - /* - * Direct or offset mount, name is absolute path so - * don't use root (but with move mount changes root - * is now the same as name). - */ - len = sprintf(fullpath, "%s", root); - } else { - len = sprintf(fullpath, "%s/%s", root, name); + len = mount_fullpath(fullpath, PATH_MAX, root, name); + if (!len) { + error(ap->logopt, + MODPREFIX "mount point path too long"); + return 1; } - fullpath[len] = '\0'; debug(ap->logopt, MODPREFIX "calling mkdir_path %s", fullpath); diff --git a/modules/mount_nfs.c b/modules/mount_nfs.c index f1b3fb3a..c70210f4 100644 --- a/modules/mount_nfs.c +++ b/modules/mount_nfs.c @@ -212,6 +212,14 @@ int mount_mount(struct autofs_point *ap, const char *root, const char *name, int nfsoptions, nobind, nosymlink, ro); } + /* Construct mount point directory */ + len = mount_fullpath(fullpath, PATH_MAX, root, name); + if (!len) { + error(ap->logopt, + MODPREFIX "mount point path too long"); + return 1; + } + if (!parse_location(ap->logopt, &hosts, what, flags)) { info(ap->logopt, MODPREFIX "no hosts available"); return 1; @@ -266,19 +274,6 @@ dont_probe: return 1; } - /* Construct and perhaps create mount point directory */ - - /* Root offset of multi-mount */ - len = strlen(root); - if (root[len - 1] == '/') { - len = snprintf(fullpath, len, "%s", root); - } else if (*name == '/') { - len = sprintf(fullpath, "%s", root); - } else { - len = sprintf(fullpath, "%s/%s", root, name); - } - fullpath[len] = '\0'; - debug(ap->logopt, MODPREFIX "calling mkdir_path %s", fullpath); status = mkdir_path(fullpath, mp_mode);