2 * mount.c -- Linux NFS mount
4 * Copyright (C) 2006 Amit Gud <agud@redhat.com>
6 * - Basic code and wrapper around mount and umount code of NFS.
7 * Based on util-linux/mount/mount.c.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
23 #include <sys/types.h>
28 #include <sys/mount.h>
36 #include "mount_constants.h"
37 #include "nfs_paths.h"
39 #include "nfs_mount.h"
40 #include "nfs4_mount.h"
41 #include "nfsumount.h"
50 static struct option longopts[] = {
51 { "fake", 0, 0, 'f' },
52 { "help", 0, 0, 'h' },
53 { "no-mtab", 0, 0, 'n' },
54 { "read-only", 0, 0, 'r' },
56 { "verbose", 0, 0, 'v' },
57 { "version", 0, 0, 'V' },
58 { "read-write", 0, 0, 'w' },
60 { "options", 1, 0, 'o' },
64 /* Map from -o and fstab option strings to the flag argument to mount(2). */
66 const char *opt; /* option name */
67 int skip; /* skip in mtab option string */
68 int inv; /* true if flag value should be inverted */
69 int mask; /* flag mask value */
72 /* Custom mount options for our own purposes. */
73 #define MS_DUMMY 0x00000000
74 #define MS_USERS 0x40000000
75 #define MS_USER 0x80000000
77 static const struct opt_map opt_map[] = {
78 { "defaults", 0, 0, 0 }, /* default options */
79 { "ro", 1, 0, MS_RDONLY }, /* read-only */
80 { "rw", 1, 1, MS_RDONLY }, /* read-write */
81 { "exec", 0, 1, MS_NOEXEC }, /* permit execution of binaries */
82 { "noexec", 0, 0, MS_NOEXEC }, /* don't execute binaries */
83 { "suid", 0, 1, MS_NOSUID }, /* honor suid executables */
84 { "nosuid", 0, 0, MS_NOSUID }, /* don't honor suid executables */
85 { "dev", 0, 1, MS_NODEV }, /* interpret device files */
86 { "nodev", 0, 0, MS_NODEV }, /* don't interpret devices */
87 { "sync", 0, 0, MS_SYNCHRONOUS}, /* synchronous I/O */
88 { "async", 0, 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
89 { "dirsync", 0, 0, MS_DIRSYNC}, /* synchronous directory modifications */
90 { "remount", 0, 0, MS_REMOUNT}, /* Alter flags of mounted FS */
91 { "bind", 0, 0, MS_BIND }, /* Remount part of tree elsewhere */
92 { "rbind", 0, 0, MS_BIND|MS_REC }, /* Idem, plus mounted subtrees */
93 { "auto", 0, 0, MS_DUMMY }, /* Can be mounted using -a */
94 { "noauto", 0, 0, MS_DUMMY }, /* Can only be mounted explicitly */
95 { "users", 1, 0, MS_USERS }, /* Allow ordinary user to mount */
96 { "nousers", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
97 { "user", 1, 0, MS_USER }, /* Allow ordinary user to mount */
98 { "nouser", 0, 1, MS_DUMMY }, /* Forbid ordinary user to mount */
99 { "owner", 0, 0, MS_DUMMY }, /* Let the owner of the device mount */
100 { "noowner", 0, 0, MS_DUMMY }, /* Device owner has no special privs */
101 { "group", 0, 0, MS_DUMMY }, /* Let the group of the device mount */
102 { "nogroup", 0, 0, MS_DUMMY }, /* Device group has no special privs */
103 { "_netdev", 0, 0, MS_DUMMY}, /* Device requires network */
104 { "comment", 0, 0, MS_DUMMY}, /* fstab comment only (kudzu,_netdev)*/
106 /* add new options here */
108 { "sub", 0, 1, MS_NOSUB }, /* allow submounts */
109 { "nosub", 0, 0, MS_NOSUB }, /* don't allow submounts */
112 { "quiet", 0, 0, MS_SILENT }, /* be quiet */
113 { "loud", 0, 1, MS_SILENT }, /* print out messages. */
116 { "mand", 0, 0, MS_MANDLOCK }, /* Allow mandatory locks on this FS */
117 { "nomand", 0, 1, MS_MANDLOCK }, /* Forbid mandatory locks on this FS */
119 { "loop", 1, 0, MS_DUMMY }, /* use a loop device */
121 { "atime", 0, 1, MS_NOATIME }, /* Update access time */
122 { "noatime", 0, 0, MS_NOATIME }, /* Do not update access time */
125 { "diratime", 0, 1, MS_NODIRATIME }, /* Update dir access times */
126 { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
131 /* Try to build a canonical options string. */
132 static char * fix_opts_string (int flags, const char *extra_opts) {
133 const struct opt_map *om;
136 new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
137 if (flags & MS_USER) {
138 /* record who mounted this so they can unmount */
139 struct passwd *pw = getpwuid(getuid());
141 new_opts = xstrconcat3(new_opts, ",user=", pw->pw_name);
143 if (flags & MS_USERS)
144 new_opts = xstrconcat3(new_opts, ",users", "");
146 for (om = opt_map; om->opt != NULL; om++) {
149 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
151 new_opts = xstrconcat3(new_opts, ",", om->opt);
154 if (extra_opts && *extra_opts) {
155 new_opts = xstrconcat3(new_opts, ",", extra_opts);
160 static int add_mtab(char *spec, char *mount_point, char *fstype,
161 int flags, char *opts, int freq, int pass)
165 int result = EX_FILEIO;
167 ment.mnt_fsname = spec;
168 ment.mnt_dir = mount_point;
169 ment.mnt_type = fstype;
170 ment.mnt_opts = fix_opts_string(flags, opts);
171 ment.mnt_freq = freq;
172 ment.mnt_passno = pass;
174 if (flags & MS_REMOUNT) {
175 update_mtab(ment.mnt_dir, &ment);
181 if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
183 nfs_error(_("Can't open mtab: %s"),
188 if (addmntent(mtab, &ment) == 1) {
189 nfs_error(_("Can't write mount entry to mtab: %s"),
194 if (fchmod(fileno(mtab), 0644) == -1) {
195 nfs_error(_("Can't set permissions on mtab: %s"),
210 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
212 return mount(spec, node, type, flags, data);
217 printf("usage: %s remotetarget dir [-rvVwfnh] [-o nfsoptions]\n",
219 printf("options:\n");
220 printf("\t-r\t\tMount file system readonly\n");
221 printf("\t-v\t\tVerbose\n");
222 printf("\t-V\t\tPrint version\n");
223 printf("\t-w\t\tMount file system read-write\n");
224 printf("\t-f\t\tFake mount, do not actually mount\n");
225 printf("\t-n\t\tDo not update /etc/mtab\n");
226 printf("\t-s\t\tTolerate sloppy mount options rather than failing.\n");
227 printf("\t-h\t\tPrint this help\n");
228 printf("\tnfsoptions\tRefer to mount.nfs(8) or nfs(5)\n\n");
232 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
233 const struct opt_map *om;
235 for (om = opt_map; om->opt != NULL; om++) {
236 if (!strcmp (opt, om->opt)) {
245 len -= strlen(extra_opts);
247 if (*extra_opts && --len > 0)
248 strcat(extra_opts, ",");
250 if ((len -= strlen(opt)) > 0)
251 strcat(extra_opts, opt);
254 /* Take -o options list and compute 4th and 5th args to mount(2). flags
255 gets the standard options (indicated by bits) and extra_opts all the rest */
256 static void parse_opts (const char *options, int *flags, char **extra_opts)
258 if (options != NULL) {
259 char *opts = xstrdup(options);
261 int len = strlen(opts) + 1; /* include room for a null */
264 *extra_opts = xmalloc(len);
267 for (p=opts, opt=NULL; p && *p; p++) {
269 opt = p; /* begin of the option item */
271 open_quote ^= 1; /* reverse the status */
273 continue; /* still in a quoted block */
275 *p = '\0'; /* terminate the option item */
276 /* end of option item or last item */
277 if (*p == '\0' || *(p+1) == '\0') {
278 parse_opt(opt, flags, *extra_opts, len);
286 static int chk_mountpoint(char *mount_point)
290 if (stat(mount_point, &sb) < 0){
291 mount_error(NULL, mount_point, errno);
294 if (S_ISDIR(sb.st_mode) == 0){
295 mount_error(NULL, mount_point, ENOTDIR);
298 if (access(mount_point, X_OK) < 0) {
299 mount_error(NULL, mount_point, errno);
306 extern u_short getport(
307 struct sockaddr_in *saddr,
312 static int probe_statd()
314 struct sockaddr_in addr;
317 memset(&addr, 0, sizeof(addr));
318 addr.sin_family = AF_INET;
319 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
320 port = getport(&addr, 100024, 1, IPPROTO_UDP);
324 addr.sin_port = htons(port);
326 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
332 static int start_statd()
334 /* If /var/run/rpc.statd.pid exists and is non-empty,
335 * assume statd already running.
336 * If START_STATD not defined, or defined to a non-existent file,
338 * else run that file (typically a shell script)
345 if (stat(START_STATD, &stb) ==0 &&
346 S_ISREG(stb.st_mode) &&
347 (stb.st_mode & S_IXUSR)) {
356 int main(int argc, char *argv[])
358 int c, flags = 0, mnt_err = 1, fake = 0;
359 char *spec, *mount_point, *fs_type = "nfs";
360 char *extra_opts = NULL, *mount_opts = NULL;
361 uid_t uid = getuid();
363 progname = basename(argv[0]);
365 if(!strncmp(progname, "umount", strlen("umount"))) {
370 exit(nfsumount(argc, argv));
373 if(argv[1] && argv[1][0] == '-') {
374 if(argv[1][1] == 'V')
375 printf("%s ("PACKAGE_STRING")\n", progname);
387 mount_point = argv[2];
389 argv[2] = argv[0]; /* so that getopt error messages are correct */
390 while ((c = getopt_long(argc - 2, argv + 2, "rvVwfno:hs",
391 longopts, NULL)) != -1) {
400 printf("%s: ("PACKAGE_STRING")\n", progname);
411 case 'o': /* specify mount options */
413 mount_opts = xstrconcat3(mount_opts, ",", optarg);
415 mount_opts = xstrdup(optarg);
426 if (optind != argc-2) {
427 /* Extra non-option words at the end... */
432 if (strcmp(progname, "mount.nfs4") == 0)
436 * If a non-root user is attempting to mount, make sure the
437 * user's requested options match the options specified in
438 * /etc/fstab; otherwise, don't allow the mount.
441 struct mntentchn *mc;
443 if ((mc = getfsfile(mount_point)) == NULL ||
444 strcmp(mc->m.mnt_fsname, spec) != 0 ||
445 strcmp(mc->m.mnt_type, fs_type) != 0) {
446 fprintf(stderr, "%s: permission denied: no match for %s "
447 "found in /etc/fstab\n", progname, mount_point);
452 * 'mount' munges the options from fstab before passing them
453 * to us, so it is non-trivial to test that we have the correct
454 * set of options and we don't want to trust what the user
455 * gave us, so just take whatever is in /etc/fstab.
457 mount_opts = strdup(mc->m.mnt_opts);
460 mount_point = canonicalize(mount_point);
461 if (mount_point == NULL ||
462 mount_point[0] != '/') {
463 fprintf(stderr, "%s: unknown mount point %s\n",
464 progname, mount_point ? : "");
468 parse_opts(mount_opts, &flags, &extra_opts);
471 if (! (flags & (MS_USERS | MS_USER))) {
472 fprintf(stderr, "%s: permission denied\n", progname);
477 if (chk_mountpoint(mount_point))
480 if (strcmp(fs_type, "nfs4") == 0)
481 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
484 mnt_err = nfsmount(spec, mount_point, &flags,
485 &extra_opts, &mount_opts,
487 if (!mnt_err && !fake && need_statd) {
488 if (!start_statd()) {
490 "%s: rpc.statd is not running but is "
491 "required for remote locking\n"
492 " Either use \"-o nolocks\" to keep "
493 "locks local, or start statd.\n",
504 mnt_err = do_mount_syscall(spec, mount_point, fs_type,
505 flags & ~(MS_USER|MS_USERS) ,
509 mount_error(spec, mount_point, errno);
515 mnt_err = add_mtab(spec, mount_point, fs_type, flags, extra_opts,
516 0, 0 /* these are always zero for NFS */ );