added hooks for sort
[oweals/busybox.git] / mount.c
1 /*
2  * Mini mount implementation for busybox
3  *
4  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * 3/21/1999    Charles P. Wright <cpwright@cpwright.com>
21  *              searches through fstab when -a is passed
22  *              will try mounting stuff with all fses when passed -t auto
23  *
24  * 1999-04-17   Dave Cinege...Rewrote -t auto. Fixed ro mtab.
25  *
26  * 1999-10-07   Erik Andersen <andersen@lineo.com>, <andersee@debian.org>.
27  *              Rewrote of a lot of code. Removed mtab usage (I plan on
28  *              putting it back as a compile-time option some time), 
29  *              major adjustments to option parsing, and some serious 
30  *              dieting all around.
31 */
32
33 #include "internal.h"
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <mntent.h>
40 #include <sys/mount.h>
41 #include <ctype.h>
42 #include <fstab.h>
43
44 extern const char mtab_file[]; /* Defined in utility.c */
45
46 static const char mount_usage[] = "\tmount [flags]\n"
47     "\tmount [flags] device directory [-o options,more-options]\n"
48     "\n"
49     "Flags:\n"
50     "\t-a:\tMount all file systems in fstab.\n"
51 #ifdef BB_MTAB
52     "\t-f:\t\"Fake\" mount. Add entry to mount table but don't mount it.\n"
53     "\t-n:\tDon't write a mount table entry.\n"
54 #endif
55     "\t-o option:\tOne of many filesystem options, listed below.\n"
56     "\t-r:\tMount the filesystem read-only.\n"
57     "\t-t filesystem-type:\tSpecify the filesystem type.\n"
58     "\t-w:\tMount for reading and writing (default).\n"
59     "\n"
60     "Options for use with the \"-o\" flag:\n"
61     "\tasync / sync:\tWrites are asynchronous / synchronous.\n"
62     "\tdev / nodev:\tAllow use of special device files / disallow them.\n"
63     "\texec / noexec:\tAllow use of executable files / disallow them.\n"
64     "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n"
65     "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n"
66     "\tro / rw: Mount for read-only / read-write.\n"
67     "\t"
68     "There are EVEN MORE flags that are specific to each filesystem.\n"
69     "You'll have to see the written documentation for those.\n";
70
71
72 struct mount_options {
73     const char *name;
74     unsigned long and;
75     unsigned long or;
76 };
77
78 static const struct mount_options mount_options[] = {
79     {"async", ~MS_SYNCHRONOUS, 0},
80     {"defaults", ~0, 0},
81     {"dev", ~MS_NODEV, 0},
82     {"exec", ~MS_NOEXEC, 0},
83     {"nodev", ~0, MS_NODEV},
84     {"noexec", ~0, MS_NOEXEC},
85     {"nosuid", ~0, MS_NOSUID},
86     {"remount", ~0, MS_REMOUNT},
87     {"ro", ~0, MS_RDONLY},
88     {"rw", ~MS_RDONLY, 0},
89     {"suid", ~MS_NOSUID, 0},
90     {"sync", ~0, MS_SYNCHRONOUS},
91     {0, 0, 0}
92 };
93
94 #if ! defined BB_MTAB
95 #define do_mount(specialfile, dir, filesystemtype, flags, string_flags, useMtab, fakeIt, mtab_opts) \
96         mount(specialfile, dir, filesystemtype, flags, string_flags)
97 #else
98 static int
99 do_mount(char* specialfile, char* dir, char* filesystemtype, 
100         long flags, void* string_flags, int useMtab, int fakeIt, char* mtab_opts)
101 {
102     int status=0;
103
104     if (fakeIt==FALSE)
105         status=mount(specialfile, dir, filesystemtype, flags, string_flags);
106
107     if ( status == 0 ) {
108         if ( useMtab==TRUE )
109             write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
110         return 0;
111     }
112     else 
113         return( status);
114 }
115 #endif
116
117
118 /* Seperate standard mount options from the nonstandard string options */
119 static void
120 parse_mount_options ( char *options, unsigned long *flags, char *strflags)
121 {
122     while (options) {
123         int gotone=FALSE;
124         char *comma = strchr (options, ',');
125         const struct mount_options* f = mount_options;
126         if (comma)
127             *comma = '\0';
128
129         while (f->name != 0) {
130             if (strcasecmp (f->name, options) == 0) {
131
132                 *flags &= f->and;
133                 *flags |= f->or;
134                 gotone=TRUE;
135                 break;
136             }
137             f++;
138         }
139         if (*strflags && strflags!= '\0' && gotone==FALSE) {
140             char *temp=strflags;
141             temp += strlen (strflags);
142             *temp++ = ',';
143             *temp++ = '\0';
144         }
145         if (gotone==FALSE) {
146             strcat (strflags, options);
147             gotone=FALSE;
148         }
149         if (comma) {
150             *comma = ',';
151             options = ++comma;
152         } else {
153             break;
154         }
155     }
156 }
157
158 int
159 mount_one(char *blockDevice, char *directory, char *filesystemType,
160            unsigned long flags, char *string_flags, int useMtab, int fakeIt, char *mtab_opts)
161 {
162     int status = 0;
163
164     char buf[255];
165
166 #if defined BB_FEATURE_USE_PROCFS
167     if (strcmp(filesystemType, "auto") == 0) {
168         FILE *f = fopen ("/proc/filesystems", "r");
169
170         if (f == NULL)
171             return( FALSE);
172
173         while (fgets (buf, sizeof (buf), f) != NULL) {
174             filesystemType = buf;
175             if (*filesystemType == '\t') {      // Not a nodev filesystem
176
177                 // Add NULL termination to each line
178                 while (*filesystemType && *filesystemType != '\n')
179                     filesystemType++;
180                 *filesystemType = '\0';
181
182                 filesystemType = buf;
183                 filesystemType++;       // hop past tab
184
185                 status = do_mount (blockDevice, directory, filesystemType,
186                                 flags | MS_MGC_VAL, string_flags, useMtab, 
187                                 fakeIt, mtab_opts);
188                 if (status == 0)
189                     break;
190             }
191         }
192         fclose (f);
193     } else
194 #endif
195     {
196         status = do_mount (blockDevice, directory, filesystemType,
197                         flags | MS_MGC_VAL, string_flags, useMtab, 
198                         fakeIt, mtab_opts);
199     }
200
201     if (status) {
202         fprintf (stderr, "Mounting %s on %s failed: %s\n",
203                  blockDevice, directory, strerror(errno));
204         return (FALSE);
205     }
206     return (TRUE);
207 }
208
209 extern int mount_main (int argc, char **argv)
210 {
211     char string_flags_buf[1024]="";
212     char *string_flags = string_flags_buf;
213     char *extra_opts = string_flags_buf;
214     unsigned long flags = 0;
215     char *filesystemType = "auto";
216     char *device = NULL;
217     char *directory = NULL;
218     int all = FALSE;
219     int fakeIt = FALSE;
220     int useMtab = TRUE;
221     int i;
222
223     /* Only compiled in if BB_MTAB is not defined */
224     whine_if_fstab_is_missing();
225
226     if (argc == 1) {
227         FILE *mountTable = setmntent (mtab_file, "r");
228         if (mountTable) {
229             struct mntent *m;
230             while ((m = getmntent (mountTable)) != 0) {
231                 struct fstab* fstabItem;
232                 char *blockDevice = m->mnt_fsname;
233                 /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */
234                 if (strcmp (blockDevice, "/dev/root") == 0) {
235                     fstabItem = getfsfile ("/");
236                     if (fstabItem != NULL)
237                         blockDevice = fstabItem->fs_spec;
238                 }
239                 printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
240                         m->mnt_type, m->mnt_opts);
241             }
242             endmntent (mountTable);
243         } else {
244             perror(mtab_file);
245         }
246         exit( TRUE);
247     }
248
249
250     /* Parse options */
251     i = --argc;
252     argv++;
253     while (i > 0 && **argv) {
254         if (**argv == '-') {
255             char *opt = *argv;
256             while (i>0 && *++opt) switch (*opt) {
257             case 'o':
258                 if (--i == 0) {
259                     goto goodbye;
260                 }
261                 parse_mount_options (*(++argv), &flags, string_flags);
262                 break;
263             case 'r':
264                 flags |= MS_RDONLY;
265                 break;
266             case 't':
267                 if (--i == 0) {
268                     goto goodbye;
269                 }
270                 filesystemType = *(++argv);
271                 break;
272             case 'w':
273                 flags &= ~MS_RDONLY;
274                 break;
275             case 'a':
276                 all = TRUE;
277                 break;
278 #ifdef BB_MTAB
279             case 'f':
280                 fakeIt = TRUE;
281                 break;
282             case 'n':
283                 useMtab = FALSE;
284                 break;
285 #endif
286             case 'v':
287             case 'h':
288             case '-':
289                 goto goodbye;
290             }
291         } else {
292             if (device == NULL)
293                 device=*argv;
294             else if (directory == NULL)
295                 directory=*argv;
296             else {
297                 goto goodbye;
298             }
299         }
300         i--;
301         argv++;
302     }
303
304     if (all == TRUE) {
305         struct mntent *m;
306         FILE *f = setmntent ("/etc/fstab", "r");
307
308         if (f == NULL) {
309             perror("/etc/fstab");
310             exit( FALSE); 
311         }
312         while ((m = getmntent (f)) != NULL) {
313             // If the file system isn't noauto, and isn't mounted on /, 
314             // and isn't swap or nfs, then mount it
315             if ((!strstr (m->mnt_opts, "noauto")) &&
316                     (m->mnt_dir[1] != '\0') && 
317                     (!strstr (m->mnt_type, "swap")) && 
318                     (!strstr (m->mnt_type, "nfs"))) 
319             {
320                 flags = 0;
321                 *string_flags = '\0';
322                 parse_mount_options(m->mnt_opts, &flags, string_flags);
323                 mount_one (m->mnt_fsname, m->mnt_dir, m->mnt_type, 
324                         flags, string_flags, useMtab, fakeIt, extra_opts);
325             }
326         }
327         endmntent (f);
328     } else {
329         if (device && directory) {
330 #ifdef BB_NFSMOUNT
331             if (strcmp(filesystemType, "nfs") == 0) {
332                 if (nfsmount(device, directory, &flags, &extra_opts, &string_flags, 1) != 0)
333                 exit(FALSE);
334             }
335 #endif
336             exit (mount_one (device, directory, filesystemType, 
337                         flags, string_flags, useMtab, fakeIt, extra_opts));
338         } else {
339             goto goodbye;
340         }
341     }
342     exit( TRUE);
343
344 goodbye:
345     usage( mount_usage);
346 }