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