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");
48 while ((c = getopt(argc, argv, "aio:ruv")) != EOF) {
75 if (optind != argc && f_all) {
76 fprintf(stderr,"exportfs: extra arguments are not permitted with -a or -r.\n");
79 if (f_ignore && (f_all || ! f_export)) {
80 fprintf(stderr,"exportfs: -i not meaningful with -a, -r or -u.\n");
83 if (f_reexport && ! f_export) {
84 fprintf(stderr, "exportfs: -r and -u are incompatible.\n");
87 if (optind == argc && ! f_all) {
93 if (f_export && ! f_ignore)
94 export_read(_PATH_EXPORTS);
97 export_all(f_verbose);
99 for (i = optind; i < argc ; i++)
100 exportfs(argv[i], options, f_verbose);
102 /* If we are unexporting everything, then
103 * don't care about what should be exported, as that
104 * may require DNS lookups..
106 if (! ( !f_export && f_all)) {
107 /* note: xtab_*_read does not update entries if they already exist,
108 * so this will not lose new options
113 for (i = optind ; i < argc ; i++)
114 unexportfs(argv[i], f_verbose);
118 exports_update(f_verbose);
125 /* we synchronise intention with reality.
126 * entries with m_mayexport get exported
127 * entries with m_exported but not m_mayexport get unexported
128 * looking at m_client->m_type == MCL_FQDN only
131 exports_update(int verbose)
135 for (exp = exportlist[MCL_FQDN]; exp; exp=exp->m_next) {
136 if (exp->m_mayexport && ((exp->m_exported<1) || exp->m_changed)) {
138 printf("%sexporting %s:%s to kernel\n",
139 exp->m_exported ?"re":"",
140 exp->m_client->m_hostname,
141 exp->m_export.e_path);
142 if (!export_export(exp))
145 if (exp->m_exported && ! exp->m_mayexport) {
147 printf("unexporting %s:%s from kernel\n",
148 exp->m_client->m_hostname,
149 exp->m_export.e_path);
150 if (!export_unexport(exp))
157 * export_all finds all entries and
158 * marks them xtabent and mayexport so that they get exported
161 export_all(int verbose)
166 for (i = 0; i < MCL_MAXTYPES; i++) {
167 for (exp = exportlist[i]; exp; exp = exp->m_next) {
169 printf("exporting %s:%s\n",
170 exp->m_client->m_hostname,
171 exp->m_export.e_path);
173 exp->m_mayexport = 1;
181 exportfs(char *arg, char *options, int verbose)
183 struct exportent *eep;
185 struct hostent *hp = NULL;
190 if ((path = strchr(arg, ':')) != NULL)
193 if (!path || *path != '/') {
194 fprintf(stderr, "Invalid exporting option: %s\n", arg);
198 if ((htype = client_gettype(hname)) == MCL_FQDN &&
199 (hp = gethostbyname(hname)) != NULL) {
200 struct hostent *hp2 = hostent_dup (hp);
201 hp = gethostbyaddr(hp2->h_addr, hp2->h_length,
205 hp = hostent_dup(hp);
208 exp = export_find(hp, path);
211 exp = export_lookup(hname, path, 0);
215 if (!(eep = mkexportent(hname, path, options)) ||
216 !(exp = export_create(eep, 0))) {
220 } else if (!updateexportent(&exp->m_export, options)) {
226 printf("exporting %s:%s\n", exp->m_client->m_hostname,
227 exp->m_export.e_path);
229 exp->m_mayexport = 1;
235 unexportfs(char *arg, int verbose)
238 struct hostent *hp = NULL;
243 if ((path = strchr(arg, ':')) != NULL)
246 if (!path || *path != '/') {
247 fprintf(stderr, "Invalid unexporting option: %s\n",
252 if ((htype = client_gettype(hname)) == MCL_FQDN) {
253 if ((hp = gethostbyname(hname)) != 0) {
254 hp = hostent_dup (hp);
255 hname = (char *) hp->h_name;
259 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
260 if (path && strcmp(path, exp->m_export.e_path))
262 if (htype != exp->m_client->m_type
263 || (htype == MCL_FQDN
264 && !matchhostname(exp->m_export.e_hostname,
268 if (exp->m_exported) {
269 printf("unexporting %s:%s from kernel\n",
270 exp->m_client->m_hostname,
271 exp->m_export.e_path);
274 printf("unexporting %s:%s\n",
275 exp->m_client->m_hostname,
276 exp->m_export.e_path);
279 if (exp->m_exported && !export_unexport(exp))
282 exp->m_mayexport = 0;
289 dumpopt(char c, char *fmt, ...)
304 struct exportent *ep;
308 for (htype = 0; htype < MCL_MAXTYPES; htype++) {
309 for (exp = exportlist[htype]; exp; exp = exp->m_next) {
312 continue; /* neilb */
313 if (htype == MCL_ANONYMOUS)
316 hname = ep->e_hostname;
317 if (strlen(ep->e_path) > 14)
318 printf("%-14s\n\t\t%s", ep->e_path, hname);
320 printf("%-14s\t%s", ep->e_path, hname);
326 if (ep->e_flags & NFSEXP_READONLY)
327 c = dumpopt(c, "ro");
329 c = dumpopt(c, "rw");
330 if (ep->e_flags & NFSEXP_ASYNC)
331 c = dumpopt(c, "async");
332 if (ep->e_flags & NFSEXP_GATHERED_WRITES)
333 c = dumpopt(c, "wdelay");
334 if (ep->e_flags & NFSEXP_CROSSMNT)
335 c = dumpopt(c, "nohide");
336 if (ep->e_flags & NFSEXP_INSECURE_PORT)
337 c = dumpopt(c, "insecure");
338 if (ep->e_flags & NFSEXP_ROOTSQUASH)
339 c = dumpopt(c, "root_squash");
341 c = dumpopt(c, "no_root_squash");
342 if (ep->e_flags & NFSEXP_ALLSQUASH)
343 c = dumpopt(c, "all_squash");
344 if (ep->e_flags & NFSEXP_NOSUBTREECHECK)
345 c = dumpopt(c, "no_subtree_check");
346 if (ep->e_flags & NFSEXP_NOAUTHNLM)
347 c = dumpopt(c, "insecure_locks");
348 if (ep->e_flags & NFSEXP_FSID)
349 c = dumpopt(c, "fsid=%d", ep->e_fsid);
350 if (ep->e_maptype == CLE_MAP_UGIDD)
351 c = dumpopt(c, "mapping=ugidd");
352 else if (ep->e_maptype == CLE_MAP_FILE)
353 c = dumpopt(c, "mapping=file");
354 if (ep->e_anonuid != -2)
355 c = dumpopt(c, "anonuid=%d", ep->e_anonuid);
356 if (ep->e_anongid != -2)
357 c = dumpopt(c, "anongid=%d", ep->e_anongid);
359 printf("%c\n", (c != '(')? ')' : ' ');
365 error(nfs_export *exp, int err)
367 fprintf(stderr, "%s:%s: %s\n", exp->m_client->m_hostname,
368 exp->m_export.e_path, strerror(err));
374 fprintf(stderr, "usage: exportfs [-aruv] [host:/path]\n");