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>
23 static void cond_rename(char *newfile, char *oldfile);
26 xtab_read(char *xtab, char *lockfn, int is_export)
28 /* is_export == 0 => reading /proc/fs/nfs/exports - we know these things are exported to kernel
29 * is_export == 1 => reading /var/lib/nfs/etab - these things are allowed to be exported
30 * is_export == 2 => reading /var/lib/nfs/xtab - these things might be known to kernel
36 if ((lockid = xflock(lockfn, "r")) < 0)
38 setexportent(xtab, "r");
39 while ((xp = getexportent(is_export==0, 0)) != NULL) {
40 if (!(exp = export_lookup(xp->e_hostname, xp->e_path, is_export != 1)) &&
41 !(exp = export_create(xp, is_export!=1))) {
53 exp->m_exported = -1;/* may be exported */
67 if ((fd=open(_PATH_PROC_EXPORTS, O_RDONLY))>=0) {
69 return xtab_read(_PATH_PROC_EXPORTS,
70 _PATH_PROC_EXPORTS, 0);
71 } else if ((fd=open(_PATH_PROC_EXPORTS_ALT, O_RDONLY) >= 0)) {
73 return xtab_read(_PATH_PROC_EXPORTS_ALT,
74 _PATH_PROC_EXPORTS_ALT, 0);
76 return xtab_read(_PATH_XTAB, _PATH_XTABLCK, 2);
80 xtab_export_read(void)
82 return xtab_read(_PATH_ETAB, _PATH_ETABLCK, 1);
86 * mountd now keeps an open fd for the etab at all times to make sure that the
87 * inode number changes when the xtab_export_write is done. If you change the
88 * routine below such that the files are edited in place, then you'll need to
89 * fix the auth_reload logic as well...
92 xtab_write(char *xtab, char *xtabtmp, char *lockfn, int is_export)
98 if ((lockid = xflock(lockfn, "w")) < 0) {
99 xlog(L_ERROR, "can't lock %s for writing", xtab);
102 setexportent(xtabtmp, "w");
104 for (i = 0; i < MCL_MAXTYPES; i++) {
105 for (exp = exportlist[i].p_head; exp; exp = exp->m_next) {
106 if (is_export && !exp->m_xtabent)
108 if (!is_export && ! exp->m_exported)
111 /* write out the export entry using the FQDN */
113 xe.e_hostname = exp->m_client->m_hostname;
119 cond_rename(xtabtmp, xtab);
129 return xtab_write(_PATH_ETAB, _PATH_ETABTMP, _PATH_ETABLCK, 1);
135 return xtab_write(_PATH_XTAB, _PATH_XTABTMP, _PATH_XTABLCK, 0);
139 xtab_append(nfs_export *exp)
144 if ((lockid = xflock(_PATH_XTABLCK, "w")) < 0)
146 setexportent(_PATH_XTAB, "a");
148 xe.e_hostname = exp->m_client->m_hostname;
156 * rename newfile onto oldfile unless
159 static void cond_rename(char *newfile, char *oldfile)
162 char nbuf[4096], obuf[4096];
165 nfd = open(newfile, 0);
168 ofd = open(oldfile, 0);
171 rename(newfile, oldfile);
176 ncnt = read(nfd, nbuf, sizeof(nbuf));
179 ocnt = read(ofd, obuf, sizeof(obuf));
190 } while (memcmp(obuf, nbuf, ncnt) == 0);
195 rename(newfile, oldfile);