]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/mount.c
6717941621c9b1e622f7b467c2e273a9d69081d3
[nfs-utils.git] / utils / mount / mount.c
1 /*
2  * mount.c -- Linux NFS mount
3  *
4  * Copyright (C) 2006 Amit Gud <agud@redhat.com>
5  *
6  * - Basic code and wrapper around mount and umount code of NFS.
7  *   Based on util-linux/mount/mount.c.
8  *
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)
12  * any later version.
13  *
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.
18  *
19  */
20
21 #include "config.h"
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/mount.h>
29 #include <getopt.h>
30 #include <mntent.h>
31
32 #include "fstab.h"
33 #include "xcommon.h"
34 #include "mount_constants.h"
35 #include "nfs_paths.h"
36
37 #include "nfs_mount.h"
38 #include "nfs4_mount.h"
39 #include "nfsumount.h"
40 #include "mount.h"
41
42 char *progname;
43 int nomtab;
44 int verbose;
45 int mounttype;
46
47 static struct option longopts[] = {
48   { "fake", 0, 0, 'f' },
49   { "help", 0, 0, 'h' },
50   { "no-mtab", 0, 0, 'n' },
51   { "read-only", 0, 0, 'r' },
52   { "ro", 0, 0, 'r' },
53   { "verbose", 0, 0, 'v' },
54   { "version", 0, 0, 'V' },
55   { "read-write", 0, 0, 'w' },
56   { "rw", 0, 0, 'w' },
57   { "options", 1, 0, 'o' },
58   { "nfsvers", 1, 0, 't' },
59   { "bind", 0, 0, 128 },
60   { "replace", 0, 0, 129 },
61   { "after", 0, 0, 130 },
62   { "before", 0, 0, 131 },
63   { "over", 0, 0, 132 },
64   { "move", 0, 0, 133 },
65   { "rbind", 0, 0, 135 },
66   { NULL, 0, 0, 0 }
67 };
68
69 /* Map from -o and fstab option strings to the flag argument to mount(2).  */
70 struct opt_map {
71   const char *opt;              /* option name */
72   int  skip;                    /* skip in mtab option string */
73   int  inv;                     /* true if flag value should be inverted */
74   int  mask;                    /* flag mask value */
75 };
76
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
94   /* add new options here */
95 #ifdef MS_NOSUB
96   { "sub",      0, 1, MS_NOSUB  },      /* allow submounts */
97   { "nosub",    0, 0, MS_NOSUB  },      /* don't allow submounts */
98 #endif
99 #ifdef MS_SILENT
100   { "quiet",    0, 0, MS_SILENT    },   /* be quiet  */
101   { "loud",     0, 1, MS_SILENT    },   /* print out messages. */
102 #endif
103 #ifdef MS_MANDLOCK
104   { "mand",     0, 0, MS_MANDLOCK },    /* Allow mandatory locks on this FS */
105   { "nomand",   0, 1, MS_MANDLOCK },    /* Forbid mandatory locks on this FS */
106 #endif
107 #ifdef MS_NOATIME
108   { "atime",    0, 1, MS_NOATIME },     /* Update access time */
109   { "noatime",  0, 0, MS_NOATIME },     /* Do not update access time */
110 #endif
111 #ifdef MS_NODIRATIME
112   { "diratime", 0, 1, MS_NODIRATIME },  /* Update dir access times */
113   { "nodiratime", 0, 0, MS_NODIRATIME },/* Do not update dir access times */
114 #endif
115   { NULL,       0, 0, 0         }
116 };
117
118 /* Try to build a canonical options string.  */
119 static char * fix_opts_string (int flags, const char *extra_opts) {
120         const struct opt_map *om;
121         char *new_opts;
122
123         new_opts = xstrdup((flags & MS_RDONLY) ? "ro" : "rw");
124         for (om = opt_map; om->opt != NULL; om++) {
125                 if (om->skip)
126                         continue;
127                 if (om->inv || !om->mask || (flags & om->mask) != om->mask)
128                         continue;
129                 new_opts = xstrconcat3(new_opts, ",", om->opt);
130                 flags &= ~om->mask;
131         }
132         if (extra_opts && *extra_opts) {
133                 new_opts = xstrconcat3(new_opts, ",", extra_opts);
134         }
135         return new_opts;
136 }
137
138
139 int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opts, int freq, int passno)
140 {
141         struct mntent ment;
142         FILE *mtab;
143
144         ment.mnt_fsname = fsname;
145         ment.mnt_dir = mount_point;
146         ment.mnt_type = fstype;
147         ment.mnt_opts = fix_opts_string(flags, opts);
148         ment.mnt_freq = 0;
149         ment.mnt_passno= 0;
150
151         lock_mtab();
152
153         if ((mtab = setmntent(MOUNTED, "a+")) == NULL) {
154                 fprintf(stderr, "Can't open " MOUNTED);
155                 return 1;
156         }
157
158         if (addmntent(mtab, &ment) == 1) {
159                 endmntent(mtab);
160                 unlock_mtab();
161                 fprintf(stderr, "Can't write mount entry");
162                 return 1;
163         }
164
165         if (fchmod(fileno(mtab), 0644) == -1) {
166                 endmntent(mtab);
167                 unlock_mtab();
168                 fprintf(stderr, "Can't set perms on " MOUNTED);
169                 return 1;
170         }
171
172         endmntent(mtab);
173
174         unlock_mtab();
175
176         return 0;
177 }
178
179 int do_mount_syscall(char *spec, char *node, char *type, int flags, void *data)
180 {
181         return mount(spec, node, type, flags, data);
182 }
183
184 void mount_usage()
185 {
186         printf("usage: %s remotetarget dir [-rvVwfnh] [-t version] [-o nfsoptions]\n", progname);
187         printf("options:\n\t-r\t\tMount file system readonly\n");
188         printf("\t-v\t\tVerbose\n");
189         printf("\t-V\t\tPrint version\n");
190         printf("\t-w\t\tMount file system read-write\n");
191         printf("\t-f\t\tFake mount, don't actually mount\n");
192         printf("\t-n\t\tDo not update /etc/mtab\n");
193         printf("\t-h\t\tPrint this help\n");
194         printf("\tversion\t\tnfs4 - NFS version 4, nfs - older NFS version supported\n");
195         printf("\tnfsoptions\tRefer mount.nfs(8) or nfs(5)\n\n");
196 }
197
198 static inline void
199 parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
200         const struct opt_map *om;
201
202         for (om = opt_map; om->opt != NULL; om++) {
203                 if (!strcmp (opt, om->opt)) {
204                         if (om->inv)
205                                 *mask &= ~om->mask;
206                         else
207                                 *mask |= om->mask;
208                         return;
209                 }
210         }
211
212         len -= strlen(extra_opts);
213
214         if (*extra_opts && --len > 0)
215                 strcat(extra_opts, ",");
216
217         if ((len -= strlen(opt)) > 0)
218                 strcat(extra_opts, opt);
219 }
220
221 /* Take -o options list and compute 4th and 5th args to mount(2).  flags
222    gets the standard options (indicated by bits) and extra_opts all the rest */
223 static void parse_opts (const char *options, int *flags, char **extra_opts)
224 {
225         if (options != NULL) {
226                 char *opts = xstrdup(options);
227                 char *opt;
228                 int len = strlen(opts) + 20;
229
230                 *extra_opts = xmalloc(len);
231                 **extra_opts = '\0';
232
233                 for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ","))
234                         parse_opt(opt, flags, *extra_opts, len);
235
236                 free(opts);
237         }
238
239 }
240
241 static void mount_error(char *node)
242 {
243         switch(errno) {
244                 case ENOTDIR:
245                         printf("%s: mount point %s is not a directory\n", progname, node);
246                         break;
247                 case EBUSY:
248                         printf("%s: %s is already mounted or busy\n", progname, node);
249                         break;
250                 case ENOENT:
251                         printf("%s: mount point %s does not exist\n", progname, node);
252                         break;
253                 default:
254                         printf("%s: %s\n", progname, strerror(errno));
255         }
256 }
257
258 static void start_statd()
259 {
260         /* If /var/run/rpc.statd.pid exists and is non-empty,
261          * assume statd already running.
262          * If START_STATD not defined, or defined to a non-existent file,
263          * don't bother,
264          * else run that file (typically a shell script)
265          */
266         struct stat stb;
267         if (stat("/var/run/rpc.statd.pid", &stb) == 0 &&
268             stb.st_size > 0)
269                 return;
270 #ifdef START_STATD
271         if (stat(START_STATD, &stb) ==0 &&
272             S_ISREG(stb.st_mode) &&
273             (stb.st_mode & S_IXUSR))
274                 system(START_STATD);
275 #endif
276 }
277
278 int main(int argc, char *argv[])
279 {
280         int c, flags = 0, nfs_mount_vers = 0, mnt_err = 1, fake = 0;
281         char *spec, *mount_point, *extra_opts = NULL;
282         char *mount_opts = NULL, *p;
283
284         progname = argv[0];
285         if ((p = strrchr(progname, '/')) != NULL)
286                 progname = p+1;
287
288         if (getuid() != 0) {
289                 printf("%s: only root can do that.\n", progname);
290                 exit(1);
291         }
292
293         if(!strncmp(progname, "umount", strlen("umount"))) {
294                 if(argc < 2) {
295                         umount_usage();
296                         exit(1);
297                 }
298                 return(nfsumount(argc, argv));
299         }
300
301         if ((argc < 2)) {
302                 mount_usage();
303                 exit(1);
304         }
305
306         if(argv[1][0] == '-') {
307                 if(argv[1][1] == 'V')
308                         printf("%s ("PACKAGE_STRING")\n", progname);
309                 else
310                         mount_usage();
311                 return 0;
312         }
313
314         while ((c = getopt_long (argc - 2, argv + 2, "rt:vVwfno:h",
315                                 longopts, NULL)) != -1) {
316                 switch (c) {
317                 case 'r':
318                         flags |= MS_RDONLY;
319                         break;
320                 case 't':
321                         nfs_mount_vers = (strncmp(optarg, "nfs4", 4)) ? 0 : 4;
322                         break;
323                 case 'v':
324                         ++verbose;
325                         break;
326                 case 'V':
327                         printf("%s: ("PACKAGE_STRING")\n", progname);
328                         return 0;
329                 case 'w':
330                         flags &= ~MS_RDONLY;
331                         break;
332                 case 'f':
333                         ++fake;
334                         break;
335                 case 'n':
336                         ++nomtab;
337                         break;
338                 case 'o':              /* specify mount options */
339                         if (mount_opts)
340                                 mount_opts = xstrconcat3(mount_opts, ",", optarg);
341                         else
342                                 mount_opts = xstrdup(optarg);
343                         break;
344                 case 128: /* bind */
345                         mounttype = MS_BIND;
346                         break;
347                 case 129: /* replace */
348                         mounttype = MS_REPLACE;
349                         break;
350                 case 130: /* after */
351                         mounttype = MS_AFTER;
352                         break;
353                 case 131: /* before */
354                         mounttype = MS_BEFORE;
355                         break;
356                 case 132: /* over */
357                         mounttype = MS_OVER;
358                         break;
359                 case 133: /* move */
360                         mounttype = MS_MOVE;
361                         break;
362                 case 135: /* rbind */
363                         mounttype = MS_BIND | MS_REC;
364                         break;
365                 case 'h':
366                 default:
367                         mount_usage();
368                         exit(1);
369                 }
370         }
371
372         spec = argv[1];
373         mount_point = canonicalize(argv[2]);
374         
375         parse_opts(mount_opts, &flags, &extra_opts);
376
377         if (!strcmp(progname, "mount.nfs4") || nfs_mount_vers == 4) {
378                 nfs_mount_vers = 4;
379                 mnt_err = nfs4mount(spec, mount_point, &flags, &extra_opts, &mount_opts, 0);
380         }
381         else if (!strcmp(progname, "mount.nfs")) {
382                 int need_statd = 0;
383                 mnt_err = nfsmount(spec, mount_point, &flags,
384                                    &extra_opts, &mount_opts, &nfs_mount_vers,
385                                    0, &need_statd);
386                 if (!mnt_err && !fake && need_statd)
387                         start_statd();
388         }
389
390         if (!mnt_err && !fake) {
391                 mnt_err = do_mount_syscall(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs", flags, mount_opts);
392                 
393                 if(mnt_err) {
394                         mount_error(mount_point);
395                         exit(-1);
396                 }
397
398                 if(!nomtab)
399                         add_mtab(spec, mount_point, nfs_mount_vers == 4 ? "nfs4" : "nfs",
400                                  flags, extra_opts, 0, 0);
401         }
402
403         return 0;
404 }
405