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 exportfs(char *arg, char *options, int verbose);
28 static void unexportfs(char *arg, int verbose);
29 static void exports_update(int verbose);
30 static void dump(int verbose);
31 static void error(nfs_export *exp, int err);
32 static void usage(void);
36 main(int argc, char **argv)
48 xlog_open("exportfs");
52 while ((c = getopt(argc, argv, "aio:ruvf")) != EOF) {
82 if (optind != argc && f_all) {
83 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
86 if (f_ignore && (f_all || ! f_export)) {
87 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
90 if (f_reexport && ! f_export) {
91 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
94 if (optind == argc && ! f_all) {
100 new_cache = check_new_cache();
102 if (f_export && ! f_ignore)
103 export_read(_PATH_EXPORTS);
106 export_all(f_verbose);
108 for (i = optind; i < argc ; i++)
109 exportfs(argv[i], options, f_verbose);
111 /* If we are unexporting everything, then
112 * don't care about what should be exported, as that
113 * may require DNS lookups..
115 if (! ( !f_export && f_all)) {
116 /* note: xtab_*_read does not update entries if they already exist,
117 * so this will not lose new options
122 for (i = optind ; i < argc ; i++)
123 unexportfs(argv[i], f_verbose);
128 exports_update(f_verbose);
132 cache_flush(force_flush);
139 /* we synchronise intention with reality.
140 * entries with m_mayexport get exported
141 * entries with m_exported but not m_mayexport get unexported
142 * looking at m_client->m_type == MCL_FQDN only
145 exports_update(int verbose)
149 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
150 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
152 printf("%sexporting %s:%s to kernel\n",
153 exp->m_exported ?"re":"",
154 exp->m_client->m_hostname,
155 exp->m_export.e_path);
156 if (!export_export(exp))
159 if (exp->m_exported && ! exp->m_mayexport) {
161 printf("unexporting %s:%s from kernel\n",
162 exp->m_client->m_hostname,
163 exp->m_export.e_path);
164 if (!export_unexport(exp))
171 * export_all finds all entries and
172 * marks them xtabent and mayexport so that they get exported
175 export_all(int verbose)
180 for (i = 0; i < MCL_MAXTYPES; i++) {
181 for (exp = exportlist[i]; exp; exp = exp->m_next) {
183 printf("exporting %s:%s\n",
184 exp->m_client->m_hostname,
185 exp->m_export.e_path);
187 exp->m_mayexport = 1;
195 exportfs(char *arg, char *options, int verbose)
197 struct exportent *eep;
199 struct hostent *hp = NULL;
204 if ((path = strchr(arg, ':')) != NULL)
207 if (!path || *path != '/') {
208 fprintf(stderr, "Invalid exporting option: %s\n", arg);
212 if ((htype = client_gettype(hname)) == MCL_FQDN &&
213 (hp = gethostbyname(hname)) != NULL) {
214 struct hostent *hp2 = hostent_dup (hp);
215 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
219 hp = hostent_dup(hp);
222 exp = export_find(hp, path);
225 exp = export_lookup(hname, path, 0);
229 if (!(eep = mkexportent(hname, path, options)) ||
230 !(exp = export_create(eep, 0))) {
234 } else if (!updateexportent(&exp->m_export, options)) {
240 printf("exporting %s:%s\n", exp->m_client->m_hostname,
241 exp->m_export.e_path);
243 exp->m_mayexport = 1;
249 unexportfs(char *arg, int verbose)
252 struct hostent *hp = NULL;
257 if ((path = strchr(arg, ':')) != NULL)
260 if (!path || *path != '/') {
261 fprintf(stderr, "Invalid unexporting option: %s\n",
266 if ((htype = client_gettype(hname)) == MCL_FQDN) {
267 if ((hp = gethostbyname(hname)) != 0) {
268 hp = hostent_dup (hp);
269 hname = (char *) hp->h_name;
273 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
274 if (path && strcmp(path, exp->m_export.e_path))
276 if (htype != exp->m_client->m_type
277 || (htype == MCL_FQDN
278 && !matchhostname(exp->m_export.e_hostname,
284 printf("unexporting %s:%s from kernel\n",
285 exp->m_client->m_hostname,
286 exp->m_export.e_path);
289 printf("unexporting %s:%s\n",
290 exp->m_client->m_hostname,
291 exp->m_export.e_path);
294 if (exp->m_exported && !export_unexport(exp))
298 exp->m_mayexport = 0;
305 dumpopt(char c, char *fmt, ...)
320 struct exportent *ep;
324 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
325 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
328 continue; /* neilb */
329 if (htype == MCL_ANONYMOUS)
332 hname = ep->e_hostname;
333 if (strlen(ep->e_path) > 14)
334 printf("%-14s\n\t\t%s", ep->e_path, hname);
336 printf("%-14s\t%s", ep->e_path, hname);
342 if (ep->e_flags & NFSEXP_READONLY)
343 c = dumpopt(c, "ro");
345 c = dumpopt(c, "rw");
346 if (ep->e_flags & NFSEXP_ASYNC)
347 c = dumpopt(c, "async");
348 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
349 c = dumpopt(c, "wdelay");
350 if (ep->e_flags & NFSEXP_NOHIDE)
351 c = dumpopt(c, "nohide");
352 if (ep->e_flags & NFSEXP_CROSSMNT)
353 c = dumpopt(c, "crossmnt");
354 if (ep->e_flags & NFSEXP_INSECURE_PORT)
355 c = dumpopt(c, "insecure");
356 if (ep->e_flags & NFSEXP_ROOTSQUASH)
357 c = dumpopt(c, "root_squash");
359 c = dumpopt(c, "no_root_squash");
360 if (ep->e_flags & NFSEXP_ALLSQUASH)
361 c = dumpopt(c, "all_squash");
362 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
363 c = dumpopt(c, "no_subtree_check");
364 if (ep->e_flags & NFSEXP_NOAUTHNLM)
365 c = dumpopt(c, "insecure_locks");
366 if (ep->e_flags & NFSEXP_FSID)
367 c = dumpopt(c, "fsid=%d", ep->e_fsid);
368 if (ep->e_maptype == CLE_MAP_UGIDD)
369 c = dumpopt(c, "mapping=ugidd");
370 else if (ep->e_maptype == CLE_MAP_FILE)
371 c = dumpopt(c, "mapping=file");
372 if (ep->e_anonuid != -2)
373 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
374 if (ep->e_anongid != -2)
375 c = dumpopt(c, "anongid=%d", ep->e_anongid);
377 printf("%c\n", (c != '(')? ')' : ' ');
383 error(nfs_export *exp, int err)
385 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
386 exp->m_export.e_path, strerror(err));
392 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");