mount and umount could leak loop device allocations causing the system to
[oweals/busybox.git] / util-linux / 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  *              Rewrite 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  * 1999-11-06   mtab suppport is back - andersee
33  *
34  * 2000-01-12   Ben Collins <bcollins@debian.org>, Borrowed utils-linux's
35  *              mount to add loop support.
36  */
37
38 #include "internal.h"
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <mntent.h>
45 #include <sys/mount.h>
46 #include <ctype.h>
47 #include <fstab.h>
48
49 #if defined BB_FEATURE_MOUNT_LOOP
50 #include <fcntl.h>
51 #include <sys/ioctl.h>
52 #include <linux/loop.h>
53
54 static int set_loop(const char *device, const char *file, int offset, int *loopro);
55 static char *find_unused_loop_device (void);
56
57 static int use_loop = 0;
58 #endif
59
60 extern const char mtab_file[]; /* Defined in utility.c */
61
62 static const char mount_usage[] = "\tmount [flags]\n"
63     "\tmount [flags] device directory [-o options,more-options]\n"
64     "\n"
65     "Flags:\n"
66     "\t-a:\tMount all file systems in fstab.\n"
67 #ifdef BB_MTAB
68     "\t-f:\t\"Fake\" mount. Add entry to mount table but don't mount it.\n"
69     "\t-n:\tDon't write a mount table entry.\n"
70 #endif
71     "\t-o option:\tOne of many filesystem options, listed below.\n"
72     "\t-r:\tMount the filesystem read-only.\n"
73     "\t-t filesystem-type:\tSpecify the filesystem type.\n"
74     "\t-w:\tMount for reading and writing (default).\n"
75     "\n"
76     "Options for use with the \"-o\" flag:\n"
77     "\tasync / sync:\tWrites are asynchronous / synchronous.\n"
78     "\tdev / nodev:\tAllow use of special device files / disallow them.\n"
79     "\texec / noexec:\tAllow use of executable files / disallow them.\n"
80 #if defined BB_FEATURE_MOUNT_LOOP
81     "\tloop: Mounts a file via loop device.\n"
82 #endif
83     "\tsuid / nosuid:\tAllow set-user-id-root programs / disallow them.\n"
84     "\tremount: Re-mount a currently-mounted filesystem, changing its flags.\n"
85     "\tro / rw: Mount for read-only / read-write.\n"
86     "\t"
87     "There are EVEN MORE flags that are specific to each filesystem.\n"
88     "You'll have to see the written documentation for those.\n";
89
90
91 struct mount_options {
92     const char *name;
93     unsigned long and;
94     unsigned long or;
95 };
96
97 static const struct mount_options mount_options[] = {
98     {"async", ~MS_SYNCHRONOUS, 0},
99     {"defaults", ~0, 0},
100     {"dev", ~MS_NODEV, 0},
101     {"exec", ~MS_NOEXEC, 0},
102     {"nodev", ~0, MS_NODEV},
103     {"noexec", ~0, MS_NOEXEC},
104     {"nosuid", ~0, MS_NOSUID},
105     {"remount", ~0, MS_REMOUNT},
106     {"ro", ~0, MS_RDONLY},
107     {"rw", ~MS_RDONLY, 0},
108     {"suid", ~MS_NOSUID, 0},
109     {"sync", ~0, MS_SYNCHRONOUS},
110     {0, 0, 0}
111 };
112
113 static int
114 do_mount(char* specialfile, char* dir, char* filesystemtype, 
115         long flags, void* string_flags, int useMtab, int fakeIt, char* mtab_opts)
116 {
117     int status=0;
118
119 #if defined BB_MTAB
120     if (fakeIt==FALSE)
121 #endif
122     {
123 #if defined BB_FEATURE_MOUNT_LOOP
124         if (use_loop) {
125             int loro = flags & MS_RDONLY;
126             char *lofile = specialfile;
127             specialfile = find_unused_loop_device();
128             if (specialfile == NULL) {
129                 fprintf(stderr, "Could not find a spare loop device\n");
130                 return( FALSE);
131             }
132             if (set_loop (specialfile, lofile, 0, &loro)) {
133                 fprintf(stderr, "Could not setup loop device\n");
134                 return( FALSE);
135             }
136             if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */
137                 fprintf(stderr, "WARNING: loop device is read-only\n");
138                 flags &= ~MS_RDONLY;
139             }
140         }
141 #endif
142         status=mount(specialfile, dir, filesystemtype, flags, string_flags);
143     }
144
145
146     /* If the mount was sucessful, do anything needed, then return TRUE */
147     if (status == 0) {
148
149 #if defined BB_MTAB
150         if (useMtab==TRUE) {
151             write_mtab(specialfile, dir, filesystemtype, flags, mtab_opts);
152         }
153 #endif
154         return( TRUE);
155     }
156
157     /* Bummer.  mount failed.  Clean up */
158 #if defined BB_FEATURE_MOUNT_LOOP
159     if (specialfile != NULL) {
160         del_loop(specialfile);
161     }
162 #endif
163     return( FALSE);
164 }
165
166
167
168 #if defined BB_MTAB
169 #define whine_if_fstab_is_missing() {} 
170 #else
171 extern void whine_if_fstab_is_missing()
172 {
173     struct stat statBuf;
174     if (stat("/etc/fstab", &statBuf) < 0) 
175         fprintf(stderr, "/etc/fstab file missing -- install one to name /dev/root.\n\n");
176 }
177 #endif
178
179
180 #if defined BB_FEATURE_MOUNT_LOOP
181 static int set_loop(const char *device, const char *file, int offset, int *loopro)
182 {
183         struct loop_info loopinfo;
184         int     fd, ffd, mode;
185         
186         mode = *loopro ? O_RDONLY : O_RDWR;
187         if ((ffd = open (file, mode)) < 0 && !*loopro
188             && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) {
189           perror (file);
190           return 1;
191         }
192         if ((fd = open (device, mode)) < 0) {
193           close(ffd);
194           perror (device);
195           return 1;
196         }
197         *loopro = (mode == O_RDONLY);
198
199         memset(&loopinfo, 0, sizeof(loopinfo));
200         strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
201         loopinfo.lo_name[LO_NAME_SIZE-1] = 0;
202
203         loopinfo.lo_offset = offset;
204
205         loopinfo.lo_encrypt_key_size = 0;
206         if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
207                 perror("ioctl: LOOP_SET_FD");
208                 close(fd);
209                 close(ffd);
210                 return 1;
211         }
212         if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
213                 (void) ioctl(fd, LOOP_CLR_FD, 0);
214                 perror("ioctl: LOOP_SET_STATUS");
215                 close(fd);
216                 close(ffd);
217                 return 1;
218         }
219         close(fd);
220         close(ffd);
221         return 0;
222 }
223
224 char *find_unused_loop_device (void)
225 {
226         char dev[20];
227         int i, fd;
228         struct stat statbuf;
229         struct loop_info loopinfo;
230
231         for(i = 0; i <= 7; i++) {
232             sprintf(dev, "/dev/loop%d", i);
233             if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
234                 if ((fd = open (dev, O_RDONLY)) >= 0) {
235                     if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == -1) {
236                         if (errno == ENXIO) { /* probably free */
237                             close (fd);
238                             return strdup(dev);
239                         }
240                     }
241                     close (fd);
242                 }
243             }
244         }
245         return NULL;
246 }
247 #endif /* BB_FEATURE_MOUNT_LOOP */
248
249 /* Seperate standard mount options from the nonstandard string options */
250 static void
251 parse_mount_options ( char *options, unsigned long *flags, char *strflags)
252 {
253     while (options) {
254         int gotone=FALSE;
255         char *comma = strchr (options, ',');
256         const struct mount_options* f = mount_options;
257         if (comma)
258             *comma = '\0';
259
260         while (f->name != 0) {
261             if (strcasecmp (f->name, options) == 0) {
262
263                 *flags &= f->and;
264                 *flags |= f->or;
265                 gotone=TRUE;
266                 break;
267             }
268             f++;
269         }
270 #if defined BB_FEATURE_MOUNT_LOOP
271         if (gotone==FALSE && !strcasecmp ("loop", options)) { /* loop device support */
272             use_loop = 1;
273             gotone=TRUE;
274         }
275 #endif
276         if (*strflags && strflags!= '\0' && gotone==FALSE) {
277             char *temp=strflags;
278             temp += strlen (strflags);
279             *temp++ = ',';
280             *temp++ = '\0';
281         }
282         if (gotone==FALSE)
283             strcat (strflags, options);
284         if (comma) {
285             *comma = ',';
286             options = ++comma;
287         } else {
288             break;
289         }
290     }
291 }
292
293 int
294 mount_one(char *blockDevice, char *directory, char *filesystemType,
295            unsigned long flags, char *string_flags, int useMtab, int fakeIt, char *mtab_opts)
296 {
297     int status = 0;
298
299     char buf[255];
300
301 #if defined BB_FEATURE_USE_PROCFS
302     if (strcmp(filesystemType, "auto") == 0) {
303         FILE *f = fopen ("/proc/filesystems", "r");
304
305         if (f == NULL)
306             return( FALSE);
307
308         while (fgets (buf, sizeof (buf), f) != NULL) {
309             filesystemType = buf;
310             if (*filesystemType == '\t') {      // Not a nodev filesystem
311
312                 // Add NULL termination to each line
313                 while (*filesystemType && *filesystemType != '\n')
314                     filesystemType++;
315                 *filesystemType = '\0';
316
317                 filesystemType = buf;
318                 filesystemType++;       // hop past tab
319
320                 status = do_mount (blockDevice, directory, filesystemType,
321                                 flags | MS_MGC_VAL, string_flags, useMtab, 
322                                 fakeIt, mtab_opts);
323                 if (status == TRUE)
324                     break;
325             }
326         }
327         fclose (f);
328     } else
329 #endif
330     {
331         status = do_mount (blockDevice, directory, filesystemType,
332                         flags | MS_MGC_VAL, string_flags, useMtab, 
333                         fakeIt, mtab_opts);
334     }
335
336     if (status==FALSE) {
337         fprintf (stderr, "Mounting %s on %s failed: %s\n",
338                  blockDevice, directory, strerror(errno));
339         return (FALSE);
340     }
341     return (TRUE);
342 }
343
344 extern int mount_main (int argc, char **argv)
345 {
346     char string_flags_buf[1024]="";
347     char *string_flags = string_flags_buf;
348     char *extra_opts = string_flags_buf;
349     unsigned long flags = 0;
350     char *filesystemType = "auto";
351     char *device = NULL;
352     char *directory = NULL;
353     int all = FALSE;
354     int fakeIt = FALSE;
355     int useMtab = TRUE;
356     int i;
357
358     /* Only compiled in if BB_MTAB is not defined */
359     whine_if_fstab_is_missing();
360
361     if (argc == 1) {
362         FILE *mountTable = setmntent (mtab_file, "r");
363         if (mountTable) {
364             struct mntent *m;
365             while ((m = getmntent (mountTable)) != 0) {
366                 struct fstab* fstabItem;
367                 char *blockDevice = m->mnt_fsname;
368                 /* Note that if /etc/fstab is missing, libc can't fix up /dev/root for us */
369                 if (strcmp (blockDevice, "/dev/root") == 0) {
370                     fstabItem = getfsfile ("/");
371                     if (fstabItem != NULL)
372                         blockDevice = fstabItem->fs_spec;
373                 }
374                 printf ("%s on %s type %s (%s)\n", blockDevice, m->mnt_dir,
375                         m->mnt_type, m->mnt_opts);
376             }
377             endmntent (mountTable);
378         } else {
379             perror(mtab_file);
380         }
381         exit( TRUE);
382     }
383
384
385     /* Parse options */
386     i = --argc;
387     argv++;
388     while (i > 0 && **argv) {
389         if (**argv == '-') {
390             char *opt = *argv;
391             while (i>0 && *++opt) switch (*opt) {
392             case 'o':
393                 if (--i == 0) {
394                     goto goodbye;
395                 }
396                 parse_mount_options (*(++argv), &flags, string_flags);
397                 break;
398             case 'r':
399                 flags |= MS_RDONLY;
400                 break;
401             case 't':
402                 if (--i == 0) {
403                     goto goodbye;
404                 }
405                 filesystemType = *(++argv);
406                 break;
407             case 'w':
408                 flags &= ~MS_RDONLY;
409                 break;
410             case 'a':
411                 all = TRUE;
412                 break;
413 #ifdef BB_MTAB
414             case 'f':
415                 fakeIt = TRUE;
416                 break;
417             case 'n':
418                 useMtab = FALSE;
419                 break;
420 #endif
421             case 'v':
422             case 'h':
423             case '-':
424                 goto goodbye;
425             }
426         } else {
427             if (device == NULL)
428                 device=*argv;
429             else if (directory == NULL)
430                 directory=*argv;
431             else {
432                 goto goodbye;
433             }
434         }
435         i--;
436         argv++;
437     }
438
439     if (all == TRUE) {
440         struct mntent *m;
441         FILE *f = setmntent ("/etc/fstab", "r");
442
443         if (f == NULL) {
444             perror("/etc/fstab");
445             exit( FALSE); 
446         }
447         while ((m = getmntent (f)) != NULL) {
448             // If the file system isn't noauto, and isn't mounted on /, 
449             // and isn't swap or nfs, then mount it
450             if ((!strstr (m->mnt_opts, "noauto")) &&
451                     (m->mnt_dir[1] != '\0') && 
452                     (!strstr (m->mnt_type, "swap")) && 
453                     (!strstr (m->mnt_type, "nfs"))) 
454             {
455                 flags = 0;
456                 *string_flags = '\0';
457                 parse_mount_options(m->mnt_opts, &flags, string_flags);
458                 mount_one (m->mnt_fsname, m->mnt_dir, m->mnt_type, 
459                         flags, string_flags, useMtab, fakeIt, extra_opts);
460             }
461         }
462         endmntent (f);
463     } else {
464         if (device && directory) {
465 #ifdef BB_NFSMOUNT
466             if (strcmp(filesystemType, "nfs") == 0) {
467                 if (nfsmount(device, directory, &flags, &extra_opts, &string_flags, 1) != 0)
468                 exit(FALSE);
469             }
470 #endif
471             exit (mount_one (device, directory, filesystemType, 
472                         flags, string_flags, useMtab, fakeIt, extra_opts));
473         } else {
474             goto goodbye;
475         }
476     }
477     exit( TRUE);
478
479 goodbye:
480     usage( mount_usage);
481 }
482