int mount_fs(mntent_t *mnt, int flags, caddr_t mnt_data, int retry, MTYPE_TYPE type, u_long nfs_version, const char *nfs_proto, const char *mnttabname, int on_autofs) { int error = 0; #ifdef MOUNT_TABLE_ON_FILE char *zopts = NULL, *xopts = NULL; size_t l; #endif /* MOUNT_TABLE_ON_FILE */ char *mnt_dir = NULL; #ifdef NEED_AUTOFS_SPACE_HACK char *old_mnt_dir = NULL; /* perform space hack */ if (on_autofs) { old_mnt_dir = mnt->mnt_dir; mnt->mnt_dir = mnt_dir = autofs_strdup_space_hack(old_mnt_dir); } else #endif /* NEED_AUTOFS_SPACE_HACK */ mnt_dir = strdup(mnt->mnt_dir); dlog("'%s' fstype " MTYPE_PRINTF_TYPE " (%s) flags %#x (%s)", mnt_dir, type, mnt->mnt_type, flags, mnt->mnt_opts); again: error = MOUNT_TRAP(type, mnt, flags, mnt_data); if (error < 0) { plog(XLOG_ERROR, "'%s': mount: %m", mnt_dir); /* * The following code handles conditions which shouldn't * occur. They are possible either because amd screws up * in preparing for the mount, or because some human * messed with the mount point. Both have been known to * happen. -- stolcke 2/22/95 */ if (errno == EBUSY) { /* * Also, sometimes unmount isn't called, e.g., because * our mountlist is garbled. This leaves old mount * points around which need to be removed before we * can mount something new in their place. */ errno = umount_fs(mnt_dir, mnttabname, on_autofs); if (errno != 0) plog(XLOG_ERROR, "'%s': umount: %m", mnt_dir); else { plog(XLOG_WARNING, "extra umount required for '%s'", mnt_dir); error = MOUNT_TRAP(type, mnt, flags, mnt_data); } } } if (error < 0 && --retry > 0) { sleep(1); goto again; } #ifdef NEED_AUTOFS_SPACE_HACK /* Undo space hack */ if (on_autofs) mnt->mnt_dir = old_mnt_dir; #endif /* NEED_AUTOFS_SPACE_HACK */ if (error < 0) { error = errno; goto out; } #ifdef MOUNT_TABLE_ON_FILE /* * Allocate memory for options: * dev=..., vers={2,3}, proto={tcp,udp} */ l = strlen(mnt->mnt_opts) + 48; zopts = (char *) xmalloc(l); /* copy standard options */ xopts = mnt->mnt_opts; xstrlcpy(zopts, xopts, l); # ifdef MNTTAB_OPT_DEV { /* add the extra dev= field to the mount table */ struct stat stb; if (lstat(mnt_dir, &stb) == 0) { char optsbuf[48]; if (sizeof(stb.st_dev) == 2) /* e.g. SunOS 4.1 */ xsnprintf(optsbuf, sizeof(optsbuf), "%s=%04lx", MNTTAB_OPT_DEV, (u_long) stb.st_dev & 0xffff); else /* e.g. System Vr4 */ xsnprintf(optsbuf, sizeof(optsbuf), "%s=%08lx", MNTTAB_OPT_DEV, (u_long) stb.st_dev); append_opts(zopts, l, optsbuf); } } # endif /* MNTTAB_OPT_DEV */ # if defined(HAVE_FS_NFS3) && defined(MNTTAB_OPT_VERS) /* * add the extra vers={2,3} field to the mount table, * unless already specified by user */ if (nfs_version == NFS_VERSION3 && hasmntval(mnt, MNTTAB_OPT_VERS) != NFS_VERSION3) { char optsbuf[48]; xsnprintf(optsbuf, sizeof(optsbuf), "%s=%d", MNTTAB_OPT_VERS, NFS_VERSION3); append_opts(zopts, l, optsbuf); } # endif /* defined(HAVE_FS_NFS3) && defined(MNTTAB_OPT_VERS) */ # ifdef MNTTAB_OPT_PROTO /* * add the extra proto={tcp,udp} field to the mount table, * unless already specified by user. */ if (nfs_proto && !amu_hasmntopt(mnt, MNTTAB_OPT_PROTO)) { char optsbuf[48]; xsnprintf(optsbuf, sizeof(optsbuf), "%s=%s", MNTTAB_OPT_PROTO, nfs_proto); append_opts(zopts, l, optsbuf); } # endif /* MNTTAB_OPT_PROTO */ /* finally, store the options into the mount table structure */ mnt->mnt_opts = zopts; /* * Additional fields in mntent_t * are fixed up here */ # ifdef HAVE_MNTENT_T_MNT_CNODE mnt->mnt_cnode = 0; # endif /* HAVE_MNTENT_T_MNT_CNODE */ # ifdef HAVE_MNTENT_T_MNT_RO mnt->mnt_ro = (amu_hasmntopt(mnt, MNTTAB_OPT_RO) != NULL); # endif /* HAVE_MNTENT_T_MNT_RO */ # ifdef HAVE_MNTENT_T_MNT_TIME # ifdef HAVE_MNTENT_T_MNT_TIME_STRING { /* allocate enough space for a long */ size_t l = 13 * sizeof(char); char *str = (char *) xmalloc(l); xsnprintf(str, l, "%ld", time((time_t *) NULL)); mnt->mnt_time = str; } # else /* not HAVE_MNTENT_T_MNT_TIME_STRING */ mnt->mnt_time = time((time_t *) NULL); # endif /* not HAVE_MNTENT_T_MNT_TIME_STRING */ # endif /* HAVE_MNTENT_T_MNT_TIME */ write_mntent(mnt, mnttabname); # ifdef MNTTAB_OPT_DEV if (xopts) { XFREE(mnt->mnt_opts); mnt->mnt_opts = xopts; } # endif /* MNTTAB_OPT_DEV */ #endif /* MOUNT_TABLE_ON_FILE */ out: XFREE(mnt_dir); return error; }
int mount_fs2(mntent_t *mnt, char *real_mntdir, int flags, caddr_t mnt_data, int retry, MTYPE_TYPE type, u_long nfs_version, const char *nfs_proto, const char *mnttabname) { int error = 0; #ifdef MOUNT_TABLE_ON_FILE # ifdef MNTTAB_OPT_DEV struct stat stb; # endif /* MNTTAB_OPT_DEV */ char *zopts = NULL, *xopts = NULL; # if defined(MNTTAB_OPT_DEV) || (defined(HAVE_FS_NFS3) && defined(MNTTAB_OPT_VERS)) || defined(MNTTAB_OPT_PROTO) char optsbuf[48]; # endif /* defined(MNTTAB_OPT_DEV) || (defined(HAVE_FS_NFS3) && defined(MNTTAB_OPT_VERS)) || defined(MNTTAB_OPT_PROTO) */ #endif /* MOUNT_TABLE_ON_FILE */ char *old_mnt_dir; old_mnt_dir = mnt->mnt_dir; mnt->mnt_dir = real_mntdir; amuDebug(D_FULL) { dlog("'%s' fstype " MTYPE_PRINTF_TYPE " (%s) flags %#x (%s)", mnt->mnt_dir, type, mnt->mnt_type, flags, mnt->mnt_opts); } again: clock_valid = 0; error = MOUNT_TRAP(type, mnt, flags, mnt_data); if (error < 0) { plog(XLOG_ERROR, "%s: mount: %m", mnt->mnt_dir); /* * The following code handles conditions which shouldn't * occur. They are possible either because amd screws up * in preparing for the mount, or because some human * messed with the mount point. Both have been known to * happen. -- stolcke 2/22/95 */ if (errno == ENOENT) { /* * Occasionally the mount point vanishes, probably * due to some race condition. Just recreate it * as necessary. */ errno = mkdirs(mnt->mnt_dir, 0555); if (errno != 0 && errno != EEXIST) plog(XLOG_ERROR, "'%s': mkdirs: %m", mnt->mnt_dir); else { plog(XLOG_WARNING, "extra mkdirs required for '%s'", mnt->mnt_dir); error = MOUNT_TRAP(type, mnt, flags, mnt_data); } } else if (errno == EBUSY) { /* * Also, sometimes unmount isn't called, e.g., because * our mountlist is garbled. This leaves old mount * points around which need to be removed before we * can mount something new in their place. */ errno = umount_fs2(old_mnt_dir, mnt->mnt_dir, mnttabname); if (errno != 0) plog(XLOG_ERROR, "'%s': umount: %m", mnt->mnt_dir); else { plog(XLOG_WARNING, "extra umount required for '%s'", mnt->mnt_dir); error = MOUNT_TRAP(type, mnt, flags, mnt_data); } } } if (error < 0 && --retry > 0) { sleep(1); goto again; } if (error < 0) { return errno; } #ifdef MOUNT_TABLE_ON_FILE /* * Allocate memory for options: * dev=..., vers={2,3}, proto={tcp,udp} */ zopts = (char *) xmalloc(strlen(mnt->mnt_opts) + 48); /* copy standard options */ xopts = mnt->mnt_opts; strcpy(zopts, xopts); # ifdef MNTTAB_OPT_DEV /* add the extra dev= field to the mount table */ if (lstat(mnt->mnt_dir, &stb) == 0) { if (sizeof(stb.st_dev) == 2) /* e.g. SunOS 4.1 */ sprintf(optsbuf, "%s=%04lx", MNTTAB_OPT_DEV, (u_long) stb.st_dev & 0xffff); else /* e.g. System Vr4 */ sprintf(optsbuf, "%s=%08lx", MNTTAB_OPT_DEV, (u_long) stb.st_dev); append_opts(zopts, optsbuf); } # endif /* MNTTAB_OPT_DEV */ mnt->mnt_dir = old_mnt_dir; # if defined(HAVE_FS_NFS3) && defined(MNTTAB_OPT_VERS) /* * add the extra vers={2,3} field to the mount table, * unless already specified by user */ if (nfs_version == NFS_VERSION3 && hasmntval(mnt, MNTTAB_OPT_VERS) != NFS_VERSION3) { sprintf(optsbuf, "%s=%d", MNTTAB_OPT_VERS, NFS_VERSION3); append_opts(zopts, optsbuf); } # endif /* defined(HAVE_FS_NFS3) && defined(MNTTAB_OPT_VERS) */ # ifdef MNTTAB_OPT_PROTO /* * add the extra proto={tcp,udp} field to the mount table, * unless already specified by user. */ if (nfs_proto && !hasmntopt(mnt, MNTTAB_OPT_PROTO)) { sprintf(optsbuf, "%s=%s", MNTTAB_OPT_PROTO, nfs_proto); append_opts(zopts, optsbuf); } # endif /* MNTTAB_OPT_PROTO */ /* finally, store the options into the mount table structure */ mnt->mnt_opts = zopts; /* * Additional fields in mntent_t * are fixed up here */ # ifdef HAVE_MNTENT_T_MNT_CNODE mnt->mnt_cnode = 0; # endif /* HAVE_MNTENT_T_MNT_CNODE */ # ifdef HAVE_MNTENT_T_MNT_RO mnt->mnt_ro = (hasmntopt(mnt, MNTTAB_OPT_RO) != NULL); # endif /* HAVE_MNTENT_T_MNT_RO */ # ifdef HAVE_MNTENT_T_MNT_TIME # ifdef HAVE_MNTENT_T_MNT_TIME_STRING { /* allocate enough space for a long */ char *str = (char *) xmalloc(13 * sizeof(char)); sprintf(str, "%ld", time((time_t *) NULL)); mnt->mnt_time = str; } # else /* not HAVE_MNTENT_T_MNT_TIME_STRING */ mnt->mnt_time = time((time_t *) NULL); # endif /* not HAVE_MNTENT_T_MNT_TIME_STRING */ # endif /* HAVE_MNTENT_T_MNT_TIME */ write_mntent(mnt, mnttabname); # ifdef MNTTAB_OPT_DEV if (xopts) { XFREE(mnt->mnt_opts); mnt->mnt_opts = xopts; } # endif /* MNTTAB_OPT_DEV */ #endif /* MOUNT_TABLE_ON_FILE */ return 0; }