2 * support/export/xtab.c
4 * Interface to the xtab file.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <sys/fcntl.h>
25 static void cond_rename(char *newfile, char *oldfile);
28 xtab_read(char *xtab, char *lockfn, int is_export)
30 /* is_export == 0 => reading /proc/fs/nfs/exports - we know these things are exported to kernel
31 * is_export == 1 => reading /var/lib/nfs/etab - these things are allowed to be exported
32 * is_export == 2 => reading /var/lib/nfs/xtab - these things might be known to kernel
38 if ((lockid = xflock(lockfn, "r")) < 0)
40 setexportent(xtab, "r");
43 while ((xp = getexportent(is_export==0, 0)) != NULL) {
44 if (!(exp = export_lookup(xp->e_hostname, xp->e_path, is_export != 1)) &&
45 !(exp = export_create(xp, is_export!=1))) {
55 if ((xp->e_flags & NFSEXP_FSID) && xp->e_fsid == 0)
59 exp->m_exported = -1;/* may be exported */
73 if ((fd=open(_PATH_PROC_EXPORTS, O_RDONLY))>=0) {
75 return xtab_read(_PATH_PROC_EXPORTS,
76 _PATH_PROC_EXPORTS, 0);
77 } else if ((fd=open(_PATH_PROC_EXPORTS_ALT, O_RDONLY) >= 0)) {
79 return xtab_read(_PATH_PROC_EXPORTS_ALT,
80 _PATH_PROC_EXPORTS_ALT, 0);
82 return xtab_read(_PATH_XTAB, _PATH_XTABLCK, 2);
86 xtab_export_read(void)
88 return xtab_read(_PATH_ETAB, _PATH_ETABLCK, 1);
92 * mountd now keeps an open fd for the etab at all times to make sure that the
93 * inode number changes when the xtab_export_write is done. If you change the
94 * routine below such that the files are edited in place, then you'll need to
95 * fix the auth_reload logic as well...
98 xtab_write(char *xtab, char *xtabtmp, char *lockfn, int is_export)
104 if ((lockid = xflock(lockfn, "w")) < 0) {
105 xlog(L_ERROR, "can't lock %s for writing", xtab);
108 setexportent(xtabtmp, "w");
110 for (i = 0; i < MCL_MAXTYPES; i++) {
111 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
112 if (is_export && !exp->m_xtabent)
114 if (!is_export && ! exp->m_exported)
117 /* write out the export entry using the FQDN */
119 xe.e_hostname = exp->m_client->m_hostname;
125 cond_rename(xtabtmp, xtab);
135 return xtab_write(_PATH_ETAB, _PATH_ETABTMP, _PATH_ETABLCK, 1);
141 return xtab_write(_PATH_XTAB, _PATH_XTABTMP, _PATH_XTABLCK, 0);
145 xtab_append(nfs_export *exp)
150 if ((lockid = xflock(_PATH_XTABLCK, "w")) < 0)
152 setexportent(_PATH_XTAB, "a");
154 xe.e_hostname = exp->m_client->m_hostname;
162 * rename newfile onto oldfile unless
165 static void cond_rename(char *newfile, char *oldfile)
168 char nbuf[4096], obuf[4096];
171 nfd = open(newfile, 0);
174 ofd = open(oldfile, 0);
177 rename(newfile, oldfile);
182 ncnt = read(nfd, nbuf, sizeof(nbuf));
185 ocnt = read(ofd, obuf, sizeof(obuf));
196 } while (memcmp(obuf, nbuf, ncnt) == 0);
201 rename(newfile, oldfile);