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 hp = hostent_dup (hp);
232 exp = export_find(hp, path);
234 exp = export_lookup(hname, path);
238 if (!(eep = mkexportent(hname, path, options)) ||
239 !(exp = export_create(eep))) {
243 } else if (!updateexportent(&exp->m_export, options)) {
249 printf("exporting %s:%s\n", exp->m_client->m_hostname,
250 exp->m_export.e_path);
252 exp->m_mayexport = 1;
258 unexportfs(char *arg, int verbose)
261 struct hostent *hp = NULL;
266 if ((path = strchr(arg, ':')) != NULL)
269 if (!path || *path != '/') {
270 fprintf(stderr, "Invalid unexporting option: %s\n",
275 if ((htype = client_gettype(hname)) == MCL_FQDN) {
276 if ((hp = gethostbyname(hname)) != 0) {
277 hp = hostent_dup (hp);
278 hname = (char *) hp->h_name;
282 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
283 if (path && strcmp(path, exp->m_export.e_path))
285 if (htype != exp->m_client->m_type
286 || (htype == MCL_FQDN
287 && !matchhostname(exp->m_export.e_hostname,
291 if (exp->m_exported) {
292 printf("unexporting %s:%s from kernel\n",
293 exp->m_client->m_hostname,
294 exp->m_export.e_path);
297 printf("unexporting %s:%s\n",
298 exp->m_client->m_hostname,
299 exp->m_export.e_path);
302 if (exp->m_exported && !export_unexport(exp))
305 exp->m_mayexport = 0;
312 dumpopt(char c, char *fmt, ...)
327 struct exportent *ep;
331 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
332 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
335 continue; /* neilb */
336 if (htype == MCL_ANONYMOUS)
339 hname = ep->e_hostname;
340 if (strlen(ep->e_path) > 14)
341 printf("%-14s\n\t\t%s", ep->e_path, hname);
343 printf("%-14s\t%s", ep->e_path, hname);
349 if (ep->e_flags & NFSEXP_READONLY)
350 c = dumpopt(c, "ro");
352 c = dumpopt(c, "rw");
353 if (ep->e_flags & NFSEXP_ASYNC)
354 c = dumpopt(c, "async");
355 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
356 c = dumpopt(c, "wdelay");
357 if (ep->e_flags & NFSEXP_CROSSMNT)
358 c = dumpopt(c, "nohide");
359 if (ep->e_flags & NFSEXP_INSECURE_PORT)
360 c = dumpopt(c, "insecure");
361 if (ep->e_flags & NFSEXP_ROOTSQUASH)
362 c = dumpopt(c, "root_squash");
364 c = dumpopt(c, "no_root_squash");
365 if (ep->e_flags & NFSEXP_ALLSQUASH)
366 c = dumpopt(c, "all_squash");
367 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
368 c = dumpopt(c, "no_subtree_check");
369 if (ep->e_flags & NFSEXP_NOAUTHNLM)
370 c = dumpopt(c, "insecure_locks");
371 if (ep->e_maptype == CLE_MAP_UGIDD)
372 c = dumpopt(c, "mapping=ugidd");
373 else if (ep->e_maptype == CLE_MAP_FILE)
374 c = dumpopt(c, "mapping=file");
375 if (ep->e_anonuid != -2)
376 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
377 if (ep->e_anongid != -2)
378 c = dumpopt(c, "anongid=%d", ep->e_anongid);
380 printf("%c\n", (c != '(')? ')' : ' ');
386 error(nfs_export *exp, int err)
388 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
389 exp->m_export.e_path, strerror(err));
395 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");