rfelker writes in Bug 736: stty.c includes unneeded, obsolete header memory.h
[oweals/busybox.git] / util-linux / mount.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini mount implementation for busybox
4  *
5  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7  * Copyright (C) 2005 by Rob Landley <rob@landley.net>
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include <limits.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <mntent.h>
19 #include <ctype.h>
20 #include <sys/mount.h>
21 #include <fcntl.h>              // for CONFIG_FEATURE_MOUNT_LOOP
22 #include <sys/ioctl.h>  // for CONFIG_FEATURE_MOUNT_LOOP
23 #include "busybox.h"
24
25 // These two aren't always defined in old headers
26 #ifndef MS_BIND
27 #define MS_BIND         4096
28 #endif
29 #ifndef MS_MOVE
30 #define MS_MOVE         8192
31 #endif
32
33 /* Consume standard mount options (from -o options or --options).
34  * Set appropriate flags and collect unrecognized ones as a comma separated
35  * string to pass to kernel */
36
37 struct {
38         const char *name;
39         long flags;
40 } static const  mount_options[] = {
41         {"loop", 0},
42         {"defaults", 0},
43         {"noauto", 0},
44         {"ro", MS_RDONLY},
45         {"rw", ~MS_RDONLY},
46         {"nosuid", MS_NOSUID},
47         {"suid", ~MS_NOSUID},
48         {"dev", ~MS_NODEV},
49         {"nodev", MS_NODEV},
50         {"exec", ~MS_NOEXEC},
51         {"noexec", MS_NOEXEC},
52         {"sync", MS_SYNCHRONOUS},
53         {"async", ~MS_SYNCHRONOUS},
54         {"remount", MS_REMOUNT},
55         {"atime", ~MS_NOATIME},
56         {"noatime", MS_NOATIME},
57         {"diratime", ~MS_NODIRATIME},
58         {"nodiratime", MS_NODIRATIME},
59         {"bind", MS_BIND},
60         {"move", MS_MOVE}
61 };
62
63 /* Uses the mount_options list above */
64 static void parse_mount_options(char *options, int *flags, char **strflags)
65 {
66         // Loop through options
67         for(;;) {
68                 int i;
69                 char *comma = strchr(options, ',');
70
71                 if(comma) *comma = 0;
72
73                 // Find this option in mount_options
74                 for(i = 0; i < (sizeof(mount_options) / sizeof(*mount_options)); i++) {
75                         if(!strcasecmp(mount_options[i].name, options)) {
76                                 long fl = mount_options[i].flags;
77                                 if(fl < 0) *flags &= fl;
78                                 else *flags |= fl;
79                                 break;
80                         }
81                 }
82                 // Unrecognized mount option?
83                 if(i == (sizeof(mount_options) / sizeof(*mount_options))) {
84                         // Add it to strflags, to pass on to kernel
85                         i = *strflags ? strlen(*strflags) : 0;
86                         *strflags = xrealloc(*strflags, i+strlen(options)+2);
87                         // Comma separated if it's not the first one
88                         if(i) (*strflags)[i++] = ',';
89                         strcpy((*strflags)+i, options);
90                 }
91                 // Advance to next option, or finish
92                 if(comma) {
93                         *comma = ',';
94                         options = ++comma;
95                 } else break;
96         }
97 }
98
99 /* This does the work */
100
101 extern int mount_main(int argc, char **argv)
102 {
103         char *string_flags = 0, *fsType = 0, *blockDevice = 0, *directory = 0,
104                  *loopFile = 0, *buf = 0,
105                  *files[] = {"/etc/filesystems", "/proc/filesystems", 0};
106         int i, opt, all = FALSE, fakeIt = FALSE, allowWrite = FALSE,
107                 rc = 1, useMtab = ENABLE_FEATURE_MTAB_SUPPORT;
108         int flags=0xc0ed0000;   // Needed for linux 2.2, ignored by 2.4 and 2.6.
109         FILE *file = 0,*f = 0;
110         char path[PATH_MAX*2];
111         struct mntent m;
112         struct stat statbuf;
113
114         /* parse long options, like --bind and --move.  Note that -o option
115          * and --option are synonymous.  Yes, this means --remount,rw works. */
116
117         for(i = opt = 0; i < argc; i++) {
118                 if(argv[i][0] == '-' && argv[i][1] == '-')
119                         parse_mount_options(argv[i]+2, &flags, &string_flags);
120                 else argv[opt++] = argv[i];
121         }
122         argc = opt;
123
124         // Parse remaining options
125
126         while((opt = getopt(argc, argv, "o:t:rwafnv")) > 0) {
127                 switch (opt) {
128                 case 'o':
129                         parse_mount_options(optarg, &flags, &string_flags);
130                         break;
131                 case 't':
132                         fsType = optarg;
133                         break;
134                 case 'r':
135                         flags |= MS_RDONLY;
136                         break;
137                 case 'w':
138                         allowWrite=TRUE;
139                         break;
140                 case 'a':
141                         all = TRUE;
142                         break;
143                 case 'f':
144                         fakeIt = TRUE;
145                         break;
146                 case 'n':
147                         useMtab = FALSE;
148                         break;
149                 case 'v':
150                         break;          // ignore -v
151                 default:
152                         bb_show_usage();
153                 }
154         }
155
156         // If we have no arguments, show currently mounted filesystems
157
158         if(!all && (optind == argc)) {
159                 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
160
161                 if(!mountTable) bb_perror_msg_and_die(bb_path_mtab_file);
162
163                 while (getmntent_r(mountTable,&m,path,sizeof(path))) {
164                         blockDevice = m.mnt_fsname;
165
166                         // Clean up display a little bit regarding root device
167                         if(!strcmp(blockDevice, "rootfs")) continue;
168                         if(!strcmp(blockDevice, "/dev/root"))
169                                 blockDevice = find_block_device("/");
170
171                         if(!fsType || !strcmp(m.mnt_type, fsType))
172                                 printf("%s on %s type %s (%s)\n", blockDevice, m.mnt_dir,
173                                            m.mnt_type, m.mnt_opts);
174                         if(ENABLE_FEATURE_CLEAN_UP && blockDevice != m.mnt_fsname)
175                                 free(blockDevice);
176                 }
177                 endmntent(mountTable);
178                 return EXIT_SUCCESS;
179         }
180
181         /* The next argument is what to mount.  if there's an argument after that
182          * it's where to mount it.  If we're not mounting all, and we have both
183          * of these arguments, jump straight to the actual mount. */
184
185         statbuf.st_mode=0;
186         if(optind < argc)
187                 blockDevice = !stat(argv[optind], &statbuf) ?
188                                 bb_simplify_path(argv[optind]) :
189                                 (ENABLE_FEATURE_CLEAN_UP ? strdup(argv[optind]) : argv[optind]);
190         if(optind+1 < argc) directory = bb_simplify_path(argv[optind+1]);
191
192     // If we don't have to loop through fstab, skip ahead a bit.
193
194         if(!all && optind+1!=argc) goto singlemount;
195
196         // Loop through /etc/fstab entries to look up this entry.
197
198         if(!(file=setmntent("/etc/fstab","r")))
199                 bb_perror_msg_and_die("\nCannot read /etc/fstab");
200         for(;;) {
201
202                 // Get next fstab entry
203
204                 if(!getmntent_r(file,&m,path,sizeof(path))) {
205                         if(!all)
206                                 bb_perror_msg("Can't find %s in /etc/fstab\n", blockDevice);
207                         break;
208                 }
209
210                 // If we're mounting all and all doesn't mount this one, skip it.
211
212                 if(all) {
213                         if(strstr(m.mnt_opts,"noauto") || strstr(m.mnt_type,"swap"))
214                                 continue;
215                         flags=0;
216
217                 /* If we're mounting something specific and this isn't it, skip it.
218                  * Note we must match both the exact text in fstab (ala "proc") or
219                  * a full path from root */
220
221                 } else if(strcmp(blockDevice,m.mnt_fsname) &&
222                                   strcmp(argv[optind],m.mnt_fsname) &&
223                                   strcmp(blockDevice,m.mnt_dir) &&
224                                   strcmp(argv[optind],m.mnt_dir)) continue;
225
226                 /* Parse flags from /etc/fstab (unless this is a single mount
227                  * overriding fstab -- note the "all" test above zeroed the flags,
228                  * to prevent flags from previous entries affecting this one, so
229                  * the only way we could get here with nonzero flags is a single
230                  * mount). */
231
232                 if(!flags) {
233                         if(ENABLE_FEATURE_CLEAN_UP) free(string_flags);
234                         string_flags=NULL;
235                         parse_mount_options(m.mnt_opts, &flags, &string_flags);
236                 }
237
238                 /* Fill out remaining fields with info from mtab */
239
240                 if(ENABLE_FEATURE_CLEAN_UP) {
241                         free(blockDevice);
242                         blockDevice=strdup(m.mnt_fsname);
243                         free(directory);
244                         directory=strdup(m.mnt_dir);
245                 } else {
246                         blockDevice=m.mnt_fsname;
247                         directory=m.mnt_dir;
248                 }
249                 fsType=m.mnt_type;
250
251                 /* Ok, we're ready to actually mount a specific source on a specific
252                  * directory now. */
253
254 singlemount:
255
256                 // If they said -w, override fstab
257
258                 if(allowWrite) flags&=~MS_RDONLY;
259
260                 // Might this be an NFS filesystem?
261
262                 if(ENABLE_FEATURE_MOUNT_NFS && (!fsType || !strcmp(fsType,"nfs")) &&
263                         strchr(blockDevice, ':') != NULL)
264                 {
265                         if(nfsmount(blockDevice, directory, &flags, &string_flags, 1))
266                                 bb_perror_msg("nfsmount failed");
267                         else {
268                                 rc = 0;
269                                 fsType="nfs";
270                                 // Strangely enough, nfsmount() doesn't actually mount()
271                                 goto mount_it_now;
272                         }
273                 } else {
274
275                         // Do we need to allocate a loopback device?
276
277                         if(ENABLE_FEATURE_MOUNT_LOOP && !fakeIt && S_ISREG(statbuf.st_mode))
278                         {
279                                 loopFile = blockDevice;
280                                 blockDevice = 0;
281                                 switch(set_loop(&blockDevice, loopFile, 0)) {
282                                         case 0:
283                                         case 1:
284                                                 break;
285                                         default:
286                                                 bb_error_msg_and_die(
287                                                         errno == EPERM || errno == EACCES ?
288                                                         bb_msg_perm_denied_are_you_root :
289                                                         "Couldn't setup loop device");
290                                                 break;
291                                 }
292                         }
293
294                         /* If we know the fstype (or don't need to), jump straight
295                          * to the actual mount. */
296
297                         if(fsType || (flags & (MS_REMOUNT | MS_BIND | MS_MOVE)))
298                                 goto mount_it_now;
299                 }
300
301                 // Loop through filesystem types until mount succeeds or we run out
302
303                 for(i = 0; files[i] && rc; i++) {
304                         f = fopen(files[i], "r");
305                         if(!f) continue;
306                         // Get next block device backed filesystem
307                         for(buf = 0; (buf = fsType = bb_get_chomped_line_from_file(f));
308                                         free(buf))
309                         {
310                                 // Skip funky entries in /proc
311                                 if(!strncmp(buf,"nodev",5) && isspace(buf[5])) continue;
312
313                                 while(isspace(*fsType)) fsType++;
314                                 if(*buf=='#' || *buf=='*') continue;
315                                 if(!*fsType) continue;
316 mount_it_now:
317                                 // Okay, try to mount
318
319                                 if (!fakeIt) {
320                                         for(;;) {
321                                                 rc = mount(blockDevice, directory, fsType, flags, string_flags);
322                                                 if(!rc || (flags&MS_RDONLY) || (errno!=EACCES && errno!=EROFS))
323                                                         break;
324                                                 bb_error_msg("%s is write-protected, mounting read-only", blockDevice);
325                                                 flags|=MS_RDONLY;
326                                         }
327                                 }
328                                 if(!rc || !f) break;
329                         }
330                         if(!f) break;
331                         fclose(f);
332                         // goto mount_it_now with -a can jump past the initialization
333                         f=0;
334                         if(!rc) break;
335                 }
336
337                 /* If the mount was successful, and we're maintaining an old-style
338                  * mtab file by hand, add new entry to it now. */
339                 if((!rc || fakeIt) && useMtab) {
340                         FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
341
342                         if(!mountTable) bb_perror_msg(bb_path_mtab_file);
343                         else {
344                                 // Remove trailing / (if any) from directory we mounted on
345                                 int length=strlen(directory);
346                                 if(length>1 && directory[length-1] == '/')
347                                         directory[length-1]=0;
348
349                                 // Fill out structure (should be ok to re-use existing one).
350                                 m.mnt_fsname=blockDevice;
351                                 m.mnt_dir=directory;
352                                 m.mnt_type=fsType ? : "--bind";
353                                 m.mnt_opts=string_flags ? :
354                                         ((flags & MS_RDONLY) ? "ro" : "rw");
355                                 m.mnt_freq = 0;
356                                 m.mnt_passno = 0;
357
358                                 // Write and close
359                                 addmntent(mountTable, &m);
360                                 endmntent(mountTable);
361                         }
362                 } else {
363                         // Mount failed.  Clean up
364                         if(loopFile) {
365                                 del_loop(blockDevice);
366                                 if(ENABLE_FEATURE_CLEAN_UP) free(loopFile);
367                         }
368                         // Don't whine about already mounted fs when mounting all.
369                         if(rc<0 && errno == EBUSY && all) rc = 0;
370                         else if (errno == EPERM)
371                                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
372                 }
373                 // We couldn't free this earlier becase fsType could be in buf.
374                 if(ENABLE_FEATURE_CLEAN_UP) free(buf);
375                 if(!all) break;
376         }
377
378         if(file) endmntent(file);
379         if(rc) bb_perror_msg("Mounting %s on %s failed", blockDevice, directory);
380         if(ENABLE_FEATURE_CLEAN_UP) {
381                 free(blockDevice);
382                 free(directory);
383         }
384
385         return rc;
386 }