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)
46 xlog_open("exportfs");
50 while ((c = getopt(argc, argv, "aio:ruv")) != EOF) {
77 if (optind != argc && f_all) {
78 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
81 if (f_ignore && (f_all || ! f_export)) {
82 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
85 if (f_reexport && ! f_export) {
86 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
89 if (optind == argc && ! f_all) {
95 if (f_export && ! f_ignore)
96 export_read(_PATH_EXPORTS);
99 export_all(f_verbose);
101 for (i = optind; i < argc ; i++)
102 exportfs(argv[i], options, f_verbose);
104 /* If we are unexporting everything, then
105 * don't care about what should be exported, as that
106 * may require DNS lookups..
108 if (! ( !f_export && f_all)) {
109 /* note: xtab_*_read does not update entries if they already exist,
110 * so this will not lose new options
115 for (i = optind ; i < argc ; i++)
116 unexportfs(argv[i], f_verbose);
120 exports_update(f_verbose);
127 /* we synchronise intention with reality.
128 * entries with m_mayexport get exported
129 * entries with m_exported but not m_mayexport get unexported
130 * looking at m_client->m_type == MCL_FQDN only
133 exports_update(int verbose)
137 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
138 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
140 printf("%sexporting %s:%s to kernel\n",
141 exp->m_exported ?"re":"",
142 exp->m_client->m_hostname,
143 exp->m_export.e_path);
144 if (!export_export(exp))
147 if (exp->m_exported && ! exp->m_mayexport) {
149 printf("unexporting %s:%s from kernel\n",
150 exp->m_client->m_hostname,
151 exp->m_export.e_path);
152 if (!export_unexport(exp))
159 * export_all finds all entries and
160 * marks them xtabent and mayexport so that they get exported
163 export_all(int verbose)
168 for (i = 0; i < MCL_MAXTYPES; i++) {
169 for (exp = exportlist[i]; exp; exp = exp->m_next) {
171 printf("exporting %s:%s\n",
172 exp->m_client->m_hostname,
173 exp->m_export.e_path);
175 exp->m_mayexport = 1;
183 exportfs(char *arg, char *options, int verbose)
185 struct exportent *eep;
187 struct hostent *hp = NULL;
192 if ((path = strchr(arg, ':')) != NULL)
195 if (!path || *path != '/') {
196 fprintf(stderr, "Invalid exporting option: %s\n", arg);
200 if ((htype = client_gettype(hname)) == MCL_FQDN &&
201 (hp = gethostbyname(hname)) != NULL) {
202 struct hostent *hp2 = hostent_dup (hp);
203 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
207 hp = hostent_dup(hp);
210 exp = export_find(hp, path);
213 exp = export_lookup(hname, path, 0);
217 if (!(eep = mkexportent(hname, path, options)) ||
218 !(exp = export_create(eep, 0))) {
222 } else if (!updateexportent(&exp->m_export, options)) {
228 printf("exporting %s:%s\n", exp->m_client->m_hostname,
229 exp->m_export.e_path);
231 exp->m_mayexport = 1;
237 unexportfs(char *arg, int verbose)
240 struct hostent *hp = NULL;
245 if ((path = strchr(arg, ':')) != NULL)
248 if (!path || *path != '/') {
249 fprintf(stderr, "Invalid unexporting option: %s\n",
254 if ((htype = client_gettype(hname)) == MCL_FQDN) {
255 if ((hp = gethostbyname(hname)) != 0) {
256 hp = hostent_dup (hp);
257 hname = (char *) hp->h_name;
261 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
262 if (path && strcmp(path, exp->m_export.e_path))
264 if (htype != exp->m_client->m_type
265 || (htype == MCL_FQDN
266 && !matchhostname(exp->m_export.e_hostname,
270 if (exp->m_exported) {
271 printf("unexporting %s:%s from kernel\n",
272 exp->m_client->m_hostname,
273 exp->m_export.e_path);
276 printf("unexporting %s:%s\n",
277 exp->m_client->m_hostname,
278 exp->m_export.e_path);
281 if (exp->m_exported && !export_unexport(exp))
284 exp->m_mayexport = 0;
291 dumpopt(char c, char *fmt, ...)
306 struct exportent *ep;
310 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
311 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
314 continue; /* neilb */
315 if (htype == MCL_ANONYMOUS)
318 hname = ep->e_hostname;
319 if (strlen(ep->e_path) > 14)
320 printf("%-14s\n\t\t%s", ep->e_path, hname);
322 printf("%-14s\t%s", ep->e_path, hname);
328 if (ep->e_flags & NFSEXP_READONLY)
329 c = dumpopt(c, "ro");
331 c = dumpopt(c, "rw");
332 if (ep->e_flags & NFSEXP_ASYNC)
333 c = dumpopt(c, "async");
334 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
335 c = dumpopt(c, "wdelay");
336 if (ep->e_flags & NFSEXP_CROSSMNT)
337 c = dumpopt(c, "nohide");
338 if (ep->e_flags & NFSEXP_INSECURE_PORT)
339 c = dumpopt(c, "insecure");
340 if (ep->e_flags & NFSEXP_ROOTSQUASH)
341 c = dumpopt(c, "root_squash");
343 c = dumpopt(c, "no_root_squash");
344 if (ep->e_flags & NFSEXP_ALLSQUASH)
345 c = dumpopt(c, "all_squash");
346 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
347 c = dumpopt(c, "no_subtree_check");
348 if (ep->e_flags & NFSEXP_NOAUTHNLM)
349 c = dumpopt(c, "insecure_locks");
350 if (ep->e_flags & NFSEXP_FSID)
351 c = dumpopt(c, "fsid=%d", ep->e_fsid);
352 if (ep->e_maptype == CLE_MAP_UGIDD)
353 c = dumpopt(c, "mapping=ugidd");
354 else if (ep->e_maptype == CLE_MAP_FILE)
355 c = dumpopt(c, "mapping=file");
356 if (ep->e_anonuid != -2)
357 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
358 if (ep->e_anongid != -2)
359 c = dumpopt(c, "anongid=%d", ep->e_anongid);
361 printf("%c\n", (c != '(')? ')' : ' ');
367 error(nfs_export *exp, int err)
369 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
370 exp->m_export.e_path, strerror(err));
376 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");