Magellan Linux

Contents of /tags/mkinitrd-6_3_3/klibc/usr/utils/mount_main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1182 - (show annotations) (download)
Wed Dec 15 21:43:57 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 3024 byte(s)
tagged 'mkinitrd-6_3_3'
1 /*
2 * by rmk
3 */
4 #include <sys/mount.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <mntent.h>
11
12 #include "mount_opts.h"
13
14 #define _PATH_MOUNTED "/etc/mtab"
15 #define _PATH_PROC_MOUNTS "/proc/mounts"
16
17 char *progname;
18
19 static struct extra_opts extra;
20 static unsigned long rwflag;
21
22 static __noreturn usage(void)
23 {
24 fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] [-f] [-i] "
25 "[-n] device directory\n", progname);
26 exit(1);
27 }
28
29 static __noreturn print_mount(char *type)
30 {
31 FILE *mfp;
32 struct mntent *mnt;
33
34 mfp = setmntent(_PATH_PROC_MOUNTS, "r");
35 if (!mfp)
36 mfp = setmntent(_PATH_MOUNTED, "r");
37 if (!mfp)
38 perror("setmntent");
39
40 while ((mnt = getmntent(mfp)) != NULL) {
41 if (mnt->mnt_fsname && !strncmp(mnt->mnt_fsname, "no", 2))
42 continue;
43 if (type && mnt->mnt_type && strcmp(type, mnt->mnt_type))
44 continue;
45 printf("%s on %s", mnt->mnt_fsname, mnt->mnt_dir);
46 if (mnt->mnt_type != NULL && mnt->mnt_type != '\0')
47 printf (" type %s", mnt->mnt_type);
48 if (mnt->mnt_opts != NULL && mnt->mnt_opts != '\0')
49 printf (" (%s)", mnt->mnt_opts);
50 printf("\n");
51 }
52 endmntent(mfp);
53 exit(0);
54 }
55
56 static int
57 do_mount(char *dev, char *dir, char *type, unsigned long rwflag, void *data)
58 {
59 char *s;
60 int error = 0;
61
62 while ((s = strsep(&type, ",")) != NULL) {
63 retry:
64 if (mount(dev, dir, s, rwflag, data) == -1) {
65 error = errno;
66 /*
67 * If the filesystem is not found, or the
68 * superblock is invalid, try the next.
69 */
70 if (error == ENODEV || error == EINVAL)
71 continue;
72
73 /*
74 * If we get EACCESS, and we're trying to
75 * mount readwrite and this isn't a remount,
76 * try read only.
77 */
78 if (error == EACCES &&
79 (rwflag & (MS_REMOUNT | MS_RDONLY)) == 0) {
80 rwflag |= MS_RDONLY;
81 goto retry;
82 }
83 } else {
84 error = 0;
85 }
86 break;
87 }
88
89 if (error) {
90 errno = error;
91 perror("mount");
92 return 255;
93 }
94
95 return 0;
96 }
97
98 int main(int argc, char *argv[])
99 {
100 char *type = NULL;
101 int c;
102
103 progname = argv[0];
104 rwflag = MS_VERBOSE;
105
106 do {
107 c = getopt(argc, argv, "fhino:rt:w");
108 if (c == EOF)
109 break;
110 switch (c) {
111 case 'f':
112 /* we can't edit /etc/mtab yet anyway; exit */
113 exit(0);
114 case 'i':
115 /* ignore for now; no support for mount helpers */
116 break;
117 case 'h':
118 usage();
119 case 'n':
120 /* no mtab writing */
121 break;
122 case 'o':
123 rwflag = parse_mount_options(optarg, rwflag, &extra);
124 break;
125 case 'r':
126 rwflag |= MS_RDONLY;
127 break;
128 case 't':
129 type = optarg;
130 break;
131 case 'w':
132 rwflag &= ~MS_RDONLY;
133 break;
134 case '?':
135 fprintf(stderr, "%s: invalid option -%c\n",
136 progname, optopt);
137 exit(1);
138 }
139 } while (1);
140
141 if (optind == argc)
142 print_mount(type);
143
144 /*
145 * If remount, bind or move was specified, then we don't
146 * have a "type" as such. Use the dummy "none" type.
147 */
148 if (rwflag & MS_TYPE)
149 type = "none";
150
151 if (optind + 2 != argc || type == NULL)
152 usage();
153
154 return do_mount(argv[optind], argv[optind + 1], type, rwflag,
155 extra.str);
156 }