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,
283 if (exp->m_exported) {
284 printf("unexporting %s:%s from kernel\n",
285 exp->m_client->m_hostname,
286 exp->m_export.e_path);
290 printf("unexporting %s:%s\n",
291 exp->m_client->m_hostname,
292 exp->m_export.e_path);
295 if (exp->m_exported && !export_unexport(exp))
299 exp->m_mayexport = 0;
306 dumpopt(char c, char *fmt, ...)
321 struct exportent *ep;
325 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
326 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
329 continue; /* neilb */
330 if (htype == MCL_ANONYMOUS)
333 hname = ep->e_hostname;
334 if (strlen(ep->e_path) > 14)
335 printf("%-14s\n\t\t%s", ep->e_path, hname);
337 printf("%-14s\t%s", ep->e_path, hname);
343 if (ep->e_flags & NFSEXP_READONLY)
344 c = dumpopt(c, "ro");
346 c = dumpopt(c, "rw");
347 if (ep->e_flags & NFSEXP_ASYNC)
348 c = dumpopt(c, "async");
349 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
350 c = dumpopt(c, "wdelay");
351 if (ep->e_flags & NFSEXP_NOHIDE)
352 c = dumpopt(c, "nohide");
353 if (ep->e_flags & NFSEXP_CROSSMNT)
354 c = dumpopt(c, "crossmnt");
355 if (ep->e_flags & NFSEXP_INSECURE_PORT)
356 c = dumpopt(c, "insecure");
357 if (ep->e_flags & NFSEXP_ROOTSQUASH)
358 c = dumpopt(c, "root_squash");
360 c = dumpopt(c, "no_root_squash");
361 if (ep->e_flags & NFSEXP_ALLSQUASH)
362 c = dumpopt(c, "all_squash");
363 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
364 c = dumpopt(c, "no_subtree_check");
365 if (ep->e_flags & NFSEXP_NOAUTHNLM)
366 c = dumpopt(c, "insecure_locks");
367 if (ep->e_flags & NFSEXP_FSID)
368 c = dumpopt(c, "fsid=%d", ep->e_fsid);
369 if (ep->e_maptype == CLE_MAP_UGIDD)
370 c = dumpopt(c, "mapping=ugidd");
371 else if (ep->e_maptype == CLE_MAP_FILE)
372 c = dumpopt(c, "mapping=file");
373 if (ep->e_anonuid != -2)
374 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
375 if (ep->e_anongid != -2)
376 c = dumpopt(c, "anongid=%d", ep->e_anongid);
378 printf("%c\n", (c != '(')? ')' : ' ');
384 error(nfs_export *exp, int err)
386 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
387 exp->m_export.e_path, strerror(err));
393 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");