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, 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(xtab, "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, 0);
70 } else if ((fd=open(_PATH_PROC_EXPORTS_ALT, O_RDONLY) >= 0)) {
72 return xtab_read(_PATH_PROC_EXPORTS_ALT, 0);
74 return xtab_read(_PATH_XTAB, 2);
78 xtab_export_read(void)
80 return xtab_read(_PATH_ETAB, 1);
84 * mountd now keeps an open fd for the etab at all times to make sure that the
85 * inode number changes when the xtab_export_write is done. If you change the
86 * routine below such that the files are edited in place, then you'll need to
87 * fix the auth_reload logic as well...
90 xtab_write(char *xtab, char *xtabtmp, int is_export)
96 if ((lockid = xflock(xtab, "w")) < 0) {
97 xlog(L_ERROR, "can't lock %s for writing", xtab);
100 setexportent(xtabtmp, "w");
102 for (i = 0; i < MCL_MAXTYPES; i++) {
103 for (exp = exportlist[i]; exp; exp = exp->m_next) {
104 if (is_export && !exp->m_xtabent)
106 if (!is_export && ! exp->m_exported)
109 /* write out the export entry using the FQDN */
111 strncpy(xe.e_hostname,
112 exp->m_client->m_hostname,
113 sizeof (xe.e_hostname) - 1);
114 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
120 cond_rename(xtabtmp, xtab);
130 return xtab_write(_PATH_ETAB, _PATH_ETABTMP, 1);
136 return xtab_write(_PATH_XTAB, _PATH_XTABTMP, 0);
140 xtab_append(nfs_export *exp)
145 if ((lockid = xflock(_PATH_XTAB, "w")) < 0)
147 setexportent(_PATH_XTAB, "a");
149 strncpy(xe.e_hostname, exp->m_client->m_hostname,
150 sizeof (xe.e_hostname) - 1);
151 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
159 * rename newfile onto oldfile unless
162 static void cond_rename(char *newfile, char *oldfile)
165 char nbuf[4096], obuf[4096];
168 nfd = open(newfile, 0);
171 ofd = open(oldfile, 0);
174 rename(newfile, oldfile);
179 ncnt = read(nfd, nbuf, sizeof(nbuf));
182 ocnt = read(ofd, obuf, sizeof(obuf));
193 } while (memcmp(obuf, nbuf, ncnt) == 0);
198 rename(newfile, oldfile);