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)
103 new_cache = check_new_cache();
105 if (f_export && ! f_ignore)
106 export_read(_PATH_EXPORTS);
109 export_all(f_verbose);
111 for (i = optind; i < argc ; i++)
112 exportfs(argv[i], options, f_verbose);
114 /* If we are unexporting everything, then
115 * don't care about what should be exported, as that
116 * may require DNS lookups..
118 if (! ( !f_export && f_all)) {
119 /* note: xtab_*_read does not update entries if they already exist,
120 * so this will not lose new options
125 for (i = optind ; i < argc ; i++)
126 unexportfs(argv[i], f_verbose);
131 exports_update(f_verbose);
135 cache_flush(force_flush);
142 /* we synchronise intention with reality.
143 * entries with m_mayexport get exported
144 * entries with m_exported but not m_mayexport get unexported
145 * looking at m_client->m_type == MCL_FQDN only
148 exports_update(int verbose)
152 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
153 /* check mountpoint option */
154 if (exp->m_mayexport &&
155 exp->m_export.e_mountpoint &&
156 !is_mountpoint(exp->m_export.e_mountpoint[0]?
157 exp->m_export.e_mountpoint:
158 exp->m_export.e_path)) {
159 printf("%s not exported as %s not a mountpoint.\n",
160 exp->m_export.e_path, exp->m_export.e_mountpoint);
161 exp->m_mayexport = 0;
163 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
165 printf("%sexporting %s:%s to kernel\n",
166 exp->m_exported ?"re":"",
167 exp->m_client->m_hostname,
168 exp->m_export.e_path);
169 if (!export_export(exp))
172 if (exp->m_exported && ! exp->m_mayexport) {
174 printf("unexporting %s:%s from kernel\n",
175 exp->m_client->m_hostname,
176 exp->m_export.e_path);
177 if (!export_unexport(exp))
184 * export_all finds all entries and
185 * marks them xtabent and mayexport so that they get exported
188 export_all(int verbose)
193 for (i = 0; i < MCL_MAXTYPES; i++) {
194 for (exp = exportlist[i]; exp; exp = exp->m_next) {
196 printf("exporting %s:%s\n",
197 exp->m_client->m_hostname,
198 exp->m_export.e_path);
200 exp->m_mayexport = 1;
208 exportfs(char *arg, char *options, int verbose)
210 struct exportent *eep;
212 struct hostent *hp = NULL;
217 if ((path = strchr(arg, ':')) != NULL)
220 if (!path || *path != '/') {
221 fprintf(stderr, "Invalid exporting option: %s\n", arg);
225 if ((htype = client_gettype(hname)) == MCL_FQDN &&
226 (hp = gethostbyname(hname)) != NULL) {
227 struct hostent *hp2 = hostent_dup (hp);
228 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
232 hp = hostent_dup(hp);
235 exp = export_find(hp, path);
238 exp = export_lookup(hname, path, 0);
242 if (!(eep = mkexportent(hname, path, options)) ||
243 !(exp = export_create(eep, 0))) {
247 } else if (!updateexportent(&exp->m_export, options)) {
253 printf("exporting %s:%s\n", exp->m_client->m_hostname,
254 exp->m_export.e_path);
256 exp->m_mayexport = 1;
262 unexportfs(char *arg, int verbose)
265 struct hostent *hp = NULL;
270 if ((path = strchr(arg, ':')) != NULL)
273 if (!path || *path != '/') {
274 fprintf(stderr, "Invalid unexporting option: %s\n",
279 if ((htype = client_gettype(hname)) == MCL_FQDN) {
280 if ((hp = gethostbyname(hname)) != 0) {
281 hp = hostent_dup (hp);
282 hname = (char *) hp->h_name;
286 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
287 if (path && strcmp(path, exp->m_export.e_path))
289 if (htype != exp->m_client->m_type)
291 if (htype == MCL_FQDN
292 && !matchhostname(exp->m_export.e_hostname,
295 if (htype != MCL_FQDN
296 && strcasecmp(exp->m_export.e_hostname, hname))
300 if (exp->m_exported) {
301 printf("unexporting %s:%s from kernel\n",
302 exp->m_client->m_hostname,
303 exp->m_export.e_path);
307 printf("unexporting %s:%s\n",
308 exp->m_client->m_hostname,
309 exp->m_export.e_path);
312 if (exp->m_exported && !export_unexport(exp))
316 exp->m_mayexport = 0;
323 dumpopt(char c, char *fmt, ...)
338 struct exportent *ep;
342 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
343 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
346 continue; /* neilb */
347 if (htype == MCL_ANONYMOUS)
350 hname = ep->e_hostname;
351 if (strlen(ep->e_path) > 14)
352 printf("%-14s\n\t\t%s", ep->e_path, hname);
354 printf("%-14s\t%s", ep->e_path, hname);
360 if (ep->e_flags & NFSEXP_READONLY)
361 c = dumpopt(c, "ro");
363 c = dumpopt(c, "rw");
364 if (ep->e_flags & NFSEXP_ASYNC)
365 c = dumpopt(c, "async");
366 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
367 c = dumpopt(c, "wdelay");
368 if (ep->e_flags & NFSEXP_NOHIDE)
369 c = dumpopt(c, "nohide");
370 if (ep->e_flags & NFSEXP_CROSSMNT)
371 c = dumpopt(c, "crossmnt");
372 if (ep->e_flags & NFSEXP_INSECURE_PORT)
373 c = dumpopt(c, "insecure");
374 if (ep->e_flags & NFSEXP_ROOTSQUASH)
375 c = dumpopt(c, "root_squash");
377 c = dumpopt(c, "no_root_squash");
378 if (ep->e_flags & NFSEXP_ALLSQUASH)
379 c = dumpopt(c, "all_squash");
380 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
381 c = dumpopt(c, "no_subtree_check");
382 if (ep->e_flags & NFSEXP_NOAUTHNLM)
383 c = dumpopt(c, "insecure_locks");
384 if (ep->e_flags & NFSEXP_FSID)
385 c = dumpopt(c, "fsid=%d", ep->e_fsid);
386 if (ep->e_mountpoint)
387 c = dumpopt(c, "mountpoint%s%s",
388 ep->e_mountpoint[0]?"=":"",
390 if (ep->e_maptype == CLE_MAP_UGIDD)
391 c = dumpopt(c, "mapping=ugidd");
392 else if (ep->e_maptype == CLE_MAP_FILE)
393 c = dumpopt(c, "mapping=file");
394 if (ep->e_anonuid != -2)
395 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
396 if (ep->e_anongid != -2)
397 c = dumpopt(c, "anongid=%d", ep->e_anongid);
399 printf("%c\n", (c != '(')? ')' : ' ');
405 error(nfs_export *exp, int err)
407 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
408 exp->m_export.e_path, strerror(err));
414 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");