2 * utils/exportfs/exportfs.c
4 * Export file systems to knfsd
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
8 * Extensive changes, 1999, Neil Brown <neilb@cse.unsw.edu.au>
26 static void export_all(int verbose);
27 static void unexport_all(int verbose);
28 static void exportfs(char *arg, char *options, int verbose);
29 static void unexportfs(char *arg, int verbose);
30 static void exports_update(int verbose);
31 static void dump(int verbose);
32 static void error(nfs_export *exp, int err);
33 static void usage(void);
37 main(int argc, char **argv)
47 xlog_open("exportfs");
49 while ((c = getopt(argc, argv, "aio:ruv")) != EOF) {
76 if (optind != argc && f_all) {
77 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
80 if (f_ignore && (f_all || ! f_export)) {
81 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
84 if (f_reexport && ! f_export) {
85 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
88 if (optind == argc && ! f_all) {
94 if (f_export && ! f_ignore)
95 export_read(_PATH_EXPORTS);
98 export_all(f_verbose);
100 for (i = optind; i < argc ; i++)
101 exportfs(argv[i], options, f_verbose);
103 /* note: xtab_*_read does not update entries if they already exist,
104 * so this will not lose new options
110 unexport_all(f_verbose);
112 for (i = optind ; i < argc ; i++)
113 unexportfs(argv[i], f_verbose);
117 exports_update(f_verbose);
124 /* we synchronise intention with reality.
125 * entries with m_mayexport get exported
126 * entries with m_exported but not m_mayexport get unexported
127 * looking at m_client->m_type == MCL_FQDN only
130 exports_update(int verbose)
134 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
135 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
137 printf("%sexporting %s:%s to kernel\n",
138 exp->m_exported ?"re":"",
139 exp->m_client->m_hostname,
140 exp->m_export.e_path);
141 if (!export_export(exp))
144 if (exp->m_exported && ! exp->m_mayexport) {
146 printf("unexporting %s:%s from kernel\n",
147 exp->m_client->m_hostname,
148 exp->m_export.e_path);
149 if (!export_unexport(exp))
156 * export_all finds all entries and
157 * marks them xtabent and mayexport so that they get exported
160 export_all(int verbose)
165 for (i = 0; i < MCL_MAXTYPES; i++) {
166 for (exp = exportlist[i]; exp; exp = exp->m_next) {
168 printf("exporting %s:%s\n",
169 exp->m_client->m_hostname,
170 exp->m_export.e_path);
172 exp->m_mayexport = 1;
178 * unexport_all finds all entries that are mayexport, and
179 * marks them not xtabent and not mayexport
182 unexport_all(int verbose)
187 for (i = 0; i < MCL_MAXTYPES; i++) {
188 for (exp = exportlist[i]; exp; exp = exp->m_next)
189 if (exp->m_mayexport) {
191 if (exp->m_exported) {
192 printf("unexporting %s:%s from kernel\n",
193 exp->m_client->m_hostname,
194 exp->m_export.e_path);
197 printf("unexporting %s:%s\n",
198 exp->m_client->m_hostname,
199 exp->m_export.e_path);
202 if (exp->m_exported && !export_unexport(exp))
205 exp->m_mayexport = 0;
212 exportfs(char *arg, char *options, int verbose)
214 struct exportent *eep;
216 struct hostent *hp = NULL;
221 if ((path = strchr(arg, ':')) != NULL)
224 if (!path || *path != '/') {
225 fprintf(stderr, "Invalid exporting option: %s\n", arg);
229 if ((htype = client_gettype(hname)) == MCL_FQDN &&
230 (hp = gethostbyname(hname)) != NULL) {
231 struct hostent *hp2 = hostent_dup (hp);
232 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
236 hp = hostent_dup(hp);
239 exp = export_find(hp, path);
241 exp = export_lookup(hname, path);
245 if (!(eep = mkexportent(hname, path, options)) ||
246 !(exp = export_create(eep))) {
250 } else if (!updateexportent(&exp->m_export, options)) {
256 printf("exporting %s:%s\n", exp->m_client->m_hostname,
257 exp->m_export.e_path);
259 exp->m_mayexport = 1;
265 unexportfs(char *arg, int verbose)
268 struct hostent *hp = NULL;
273 if ((path = strchr(arg, ':')) != NULL)
276 if (!path || *path != '/') {
277 fprintf(stderr, "Invalid unexporting option: %s\n",
282 if ((htype = client_gettype(hname)) == MCL_FQDN) {
283 if ((hp = gethostbyname(hname)) != 0) {
284 hp = hostent_dup (hp);
285 hname = (char *) hp->h_name;
289 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
290 if (path && strcmp(path, exp->m_export.e_path))
292 if (htype != exp->m_client->m_type
293 || (htype == MCL_FQDN
294 && !matchhostname(exp->m_export.e_hostname,
298 if (exp->m_exported) {
299 printf("unexporting %s:%s from kernel\n",
300 exp->m_client->m_hostname,
301 exp->m_export.e_path);
304 printf("unexporting %s:%s\n",
305 exp->m_client->m_hostname,
306 exp->m_export.e_path);
309 if (exp->m_exported && !export_unexport(exp))
312 exp->m_mayexport = 0;
319 dumpopt(char c, char *fmt, ...)
334 struct exportent *ep;
338 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
339 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
342 continue; /* neilb */
343 if (htype == MCL_ANONYMOUS)
346 hname = ep->e_hostname;
347 if (strlen(ep->e_path) > 14)
348 printf("%-14s\n\t\t%s", ep->e_path, hname);
350 printf("%-14s\t%s", ep->e_path, hname);
356 if (ep->e_flags & NFSEXP_READONLY)
357 c = dumpopt(c, "ro");
359 c = dumpopt(c, "rw");
360 if (ep->e_flags & NFSEXP_ASYNC)
361 c = dumpopt(c, "async");
362 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
363 c = dumpopt(c, "wdelay");
364 if (ep->e_flags & NFSEXP_CROSSMNT)
365 c = dumpopt(c, "nohide");
366 if (ep->e_flags & NFSEXP_INSECURE_PORT)
367 c = dumpopt(c, "insecure");
368 if (ep->e_flags & NFSEXP_ROOTSQUASH)
369 c = dumpopt(c, "root_squash");
371 c = dumpopt(c, "no_root_squash");
372 if (ep->e_flags & NFSEXP_ALLSQUASH)
373 c = dumpopt(c, "all_squash");
374 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
375 c = dumpopt(c, "no_subtree_check");
376 if (ep->e_flags & NFSEXP_NOAUTHNLM)
377 c = dumpopt(c, "insecure_locks");
378 if (ep->e_maptype == CLE_MAP_UGIDD)
379 c = dumpopt(c, "mapping=ugidd");
380 else if (ep->e_maptype == CLE_MAP_FILE)
381 c = dumpopt(c, "mapping=file");
382 if (ep->e_anonuid != -2)
383 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
384 if (ep->e_anongid != -2)
385 c = dumpopt(c, "anongid=%d", ep->e_anongid);
387 printf("%c\n", (c != '(')? ')' : ' ');
393 error(nfs_export *exp, int err)
395 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
396 exp->m_export.e_path, strerror(err));
402 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");