source: trunk/minix/commands/simple/mount.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 4.3 KB
Line 
1/* mount - mount a file system Author: Andy Tanenbaum */
2
3#include <errno.h>
4#include <sys/types.h>
5#include <limits.h>
6#include <stdlib.h>
7#include <dirent.h>
8#include <string.h>
9#include <unistd.h>
10#include <fcntl.h>
11#include <minix/config.h>
12#include <minix/const.h>
13#include <minix/minlib.h>
14#include <minix/swap.h>
15#include <sys/svrctl.h>
16#include <stdio.h>
17#include "../../servers/fs/const.h"
18
19_PROTOTYPE(int main, (int argc, char **argv));
20_PROTOTYPE(void list, (void));
21_PROTOTYPE(void usage, (void));
22_PROTOTYPE(void tell, (char *this));
23_PROTOTYPE(void swapon, (char *file));
24
25static u8_t MAGIC[] = { SWAP_MAGIC0, SWAP_MAGIC1, SWAP_MAGIC2, SWAP_MAGIC3 };
26
27int main(argc, argv)
28int argc;
29char *argv[];
30{
31 int i, ro, swap, n, v;
32 char **ap, *vs, *opt, *err;
33 char special[PATH_MAX+1], mounted_on[PATH_MAX+1], version[10], rw_flag[10];
34
35 if (argc == 1) list(); /* just list /etc/mtab */
36 ro = 0;
37 swap = 0;
38 ap = argv+1;
39 for (i = 1; i < argc; i++) {
40 if (argv[i][0] == '-') {
41 opt = argv[i]+1;
42 while (*opt != 0) switch (*opt++) {
43 case 'r': ro = 1; break;
44 case 's': swap = 1; break;
45 default: usage();
46 }
47 } else {
48 *ap++ = argv[i];
49 }
50 }
51 *ap = NULL;
52 argc = (ap - argv);
53
54 if (ro && swap) usage();
55
56 if (swap) {
57 if (argc != 2) usage();
58 swapon(argv[1]);
59 tell(argv[1]);
60 tell(" is swapspace\n");
61 } else {
62 if (argc != 3) usage();
63 if (mount(argv[1], argv[2], ro) < 0) {
64 err = strerror(errno);
65 std_err("mount: Can't mount ");
66 std_err(argv[1]);
67 std_err(" on ");
68 std_err(argv[2]);
69 std_err(": ");
70 std_err(err);
71 std_err("\n");
72 exit(1);
73 }
74 /* The mount has completed successfully. Tell the user. */
75 tell(argv[1]);
76 tell(" is read-");
77 tell(ro ? "only" : "write");
78 tell(" mounted on ");
79 tell(argv[2]);
80 tell("\n");
81 }
82
83 /* Update /etc/mtab. */
84 n = load_mtab("mount");
85 if (n < 0) exit(1); /* something is wrong. */
86
87 /* Loop on all the /etc/mtab entries, copying each one to the output buf. */
88 while (1) {
89 n = get_mtab_entry(special, mounted_on, version, rw_flag);
90 if (n < 0) break;
91 n = put_mtab_entry(special, mounted_on, version, rw_flag);
92 if (n < 0) {
93 std_err("mount: /etc/mtab has grown too large\n");
94 exit(1);
95 }
96 }
97 if (swap) {
98 vs = "swap";
99 } else {
100 v = fsversion(argv[1], "mount");
101 if (v == 1)
102 vs = "1";
103 else if (v == 2)
104 vs = "2";
105 else if (v == 3)
106 vs = "3";
107 else
108 vs = "0";
109 }
110 n = put_mtab_entry(argv[1], swap ? "swap" : argv[2], vs, (ro ? "ro" : "rw") );
111 if (n < 0) {
112 std_err("mount: /etc/mtab has grown too large\n");
113 exit(1);
114 }
115
116 n = rewrite_mtab("mount");
117 return(0);
118}
119
120
121void list()
122{
123 int n;
124 char special[PATH_MAX+1], mounted_on[PATH_MAX+1], version[10], rw_flag[10];
125
126 /* Read and print /etc/mtab. */
127 n = load_mtab("mount");
128 if (n < 0) exit(1);
129
130 while (1) {
131 n = get_mtab_entry(special, mounted_on, version, rw_flag);
132 if (n < 0) break;
133 write(1, special, strlen(special));
134 if (strcmp(version, "swap") == 0) {
135 tell(" is swapspace\n");
136 } else {
137 tell(" is read-");
138 tell(strcmp(rw_flag, "rw") == 0 ? "write" : "only");
139 tell(" mounted on ");
140 tell(mounted_on);
141 tell("\n");
142 }
143 }
144 exit(0);
145}
146
147
148void usage()
149{
150 std_err("Usage: mount [-r] special name\n mount -s special\n");
151 exit(1);
152}
153
154
155void tell(this)
156char *this;
157{
158 write(1, this, strlen(this));
159}
160
161void swapon(file)
162char *file;
163{
164 u32_t super[2][_MAX_BLOCK_SIZE / 2 / sizeof(u32_t)];
165 swap_hdr_t *sp;
166 struct mmswapon mmswapon;
167 int fd, r;
168 char *err;
169
170 if ((fd = open(file, O_RDWR)) < 0
171 || lseek(fd, SUPER_BLOCK_BYTES, SEEK_SET) == -1
172 || (r = read(fd, super, _STATIC_BLOCK_SIZE)) < 0
173 ) {
174 err = strerror(errno);
175 std_err("mount: ");
176 std_err(file);
177 std_err(": ");
178 std_err(err);
179 std_err("\n");
180 exit(1);
181 }
182 sp = (swap_hdr_t *) &super[0];
183 if (memcmp(sp->sh_magic, MAGIC, sizeof(MAGIC)) != 0)
184 sp = (swap_hdr_t *) &super[1];
185 if (r == _STATIC_BLOCK_SIZE && memcmp(sp->sh_magic, MAGIC, sizeof(MAGIC)) != 0
186 || sp->sh_version > SH_VERSION) {
187 std_err("mount: ");
188 std_err(file);
189 std_err(" is not swapspace\n");
190 exit(1);
191 }
192 close(fd);
193 mmswapon.offset = sp->sh_offset;
194 mmswapon.size = sp->sh_swapsize;
195 strncpy(mmswapon.file, file, sizeof(mmswapon.file));
196 mmswapon.file[sizeof(mmswapon.file)-1] = 0;
197 if (svrctl(MMSWAPON, &mmswapon) < 0) {
198 err = strerror(errno);
199 std_err("mount: ");
200 std_err(file);
201 std_err(": ");
202 std_err(err);
203 std_err("\n");
204 exit(1);
205 }
206}
Note: See TracBrowser for help on using the repository browser.