X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=utils%2Fmount%2Fmount.c;h=442ce0ea034bcefd025b0dad6cd1faac248621a7;hp=487c0a62bc20f5d7edf02a72334423c1ee30702a;hb=46ef88e0f7ec187e4448230efa6baf698643d3f9;hpb=6facb22402a0bd8cd49be2ed1a0856b24fef42f4 diff --git a/utils/mount/mount.c b/utils/mount/mount.c index 487c0a6..442ce0e 100644 --- a/utils/mount/mount.c +++ b/utils/mount/mount.c @@ -99,11 +99,9 @@ static const struct opt_map opt_map[] = { { "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */ { "auto", 0, 0, MS_DUMMY }, /* Can be mounted using -a */ { "noauto", 0, 0, MS_DUMMY }, /* Can only be mounted explicitly */ - { "users", 1, 0, MS_USERS|MS_NOEXEC|MS_NOSUID|MS_NODEV }, - /* Allow ordinary user to mount */ + { "users", 1, 0, MS_USERS }, /* Allow ordinary user to mount */ { "nousers", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */ - { "user", 1, 0, MS_USER|MS_NOEXEC|MS_NOSUID|MS_NODEV }, - /* Allow ordinary user to mount */ + { "user", 1, 0, MS_USER }, /* Allow ordinary user to mount */ { "nouser", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */ { "owner", 0, 0, MS_DUMMY }, /* Let the owner of the device mount */ { "noowner", 0, 0, MS_DUMMY }, /* Device owner has no special privs */ @@ -187,6 +185,7 @@ int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opt lock_mtab(); if ((mtab = setmntent(MOUNTED, "a+")) == NULL) { + unlock_mtab(); fprintf(stderr, "Can't open " MOUNTED); return 1; } @@ -262,7 +261,7 @@ static void parse_opts (const char *options, int *flags, char **extra_opts) if (options != NULL) { char *opts = xstrdup(options); char *opt, *p; - int len = strlen(opts); + int len = strlen(opts) + 1; /* include room for a null */ int open_quote = 0; *extra_opts = xmalloc(len); @@ -287,24 +286,77 @@ static void parse_opts (const char *options, int *flags, char **extra_opts) } } -static void mount_error(char *node) +static void mount_error(char *mntpnt, char *node) { switch(errno) { case ENOTDIR: - fprintf(stderr, "%s: mount point %s is not a directory\n", progname, node); + fprintf(stderr, "%s: mount point %s is not a directory\n", + progname, mntpnt); break; case EBUSY: - fprintf(stderr, "%s: %s is already mounted or busy\n", progname, node); + fprintf(stderr, "%s: %s is already mounted or busy\n", + progname, mntpnt); break; case ENOENT: - fprintf(stderr, "%s: mount point %s does not exist\n", progname, node); + if (node) { + fprintf(stderr, "%s: %s failed, reason given by server: %s\n", + progname, node, strerror(errno)); + } else + fprintf(stderr, "%s: mount point %s does not exist\n", + progname, mntpnt); break; default: fprintf(stderr, "%s: %s\n", progname, strerror(errno)); } } +static int chk_mountpoint(char *mount_point) +{ + struct stat sb; + + if (stat(mount_point, &sb) < 0){ + mount_error(mount_point, NULL); + return 1; + } + if (S_ISDIR(sb.st_mode) == 0){ + errno = ENOTDIR; + mount_error(mount_point, NULL); + return 1; + } + if (access(mount_point, X_OK) < 0) { + mount_error(mount_point, NULL); + return 1; + } + + return 0; +} -static void start_statd() +extern u_short getport( + struct sockaddr_in *saddr, + u_long prog, + u_long vers, + u_int prot); + +static int probe_statd() +{ + struct sockaddr_in addr; + u_short port; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + port = getport(&addr, 100024, 1, IPPROTO_UDP); + + if (port == 0) + return 0; + addr.sin_port = htons(port); + + if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0) + return 0; + + return 1; +} + +static int start_statd() { /* If /var/run/rpc.statd.pid exists and is non-empty, * assume statd already running. @@ -313,15 +365,19 @@ static void start_statd() * else run that file (typically a shell script) */ struct stat stb; - if (stat("/var/run/rpc.statd.pid", &stb) == 0 && - stb.st_size > 0) - return; + + if (probe_statd()) + return 1; #ifdef START_STATD if (stat(START_STATD, &stb) ==0 && S_ISREG(stb.st_mode) && - (stb.st_mode & S_IXUSR)) + (stb.st_mode & S_IXUSR)) { system(START_STATD); + if (probe_statd()) + return 1; + } #endif + return 0; } int main(int argc, char *argv[]) @@ -437,7 +493,7 @@ int main(int argc, char *argv[]) exit(1); } - if (strcmp(progname, "mount.nfsv4") == 0) + if (strcmp(progname, "mount.nfs4") == 0) nfs_mount_vers = 4; if (uid != 0) { @@ -480,6 +536,9 @@ int main(int argc, char *argv[]) } } + if (chk_mountpoint(mount_point)) + exit(EX_FAIL); + if (nfs_mount_vers == 4) mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0); else { @@ -487,8 +546,17 @@ int main(int argc, char *argv[]) mnt_err = nfsmount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0, &need_statd); - if (!mnt_err && !fake && need_statd) - start_statd(); + if (!mnt_err && !fake && need_statd) { + if (!start_statd()) { + fprintf(stderr, + "%s: rpc.statd is not running but is " + "required for remote locking\n" + " Either use \"-o nolocks\" to keep " + "locks local, or start statd.\n", + progname); + exit(1); + } + } } if (mnt_err) @@ -501,7 +569,7 @@ int main(int argc, char *argv[]) mount_opts); if (mnt_err) { - mount_error(mount_point); + mount_error(mount_point, spec); exit(EX_FAIL); } }