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 xtab_write(char *xtab, char *xtabtmp, int is_export)
90 if ((lockid = xflock(xtab, "w")) < 0) {
91 xlog(L_ERROR, "can't lock %s for writing", xtab);
94 setexportent(xtabtmp, "w");
96 for (i = 0; i < MCL_MAXTYPES; i++) {
97 for (exp = exportlist[i]; exp; exp = exp->m_next) {
98 if (is_export && !exp->m_xtabent)
100 if (!is_export && ! exp->m_exported)
103 /* write out the export entry using the FQDN */
105 strncpy(xe.e_hostname,
106 exp->m_client->m_hostname,
107 sizeof (xe.e_hostname) - 1);
108 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
114 cond_rename(xtabtmp, xtab);
124 return xtab_write(_PATH_ETAB, _PATH_ETABTMP, 1);
130 return xtab_write(_PATH_XTAB, _PATH_XTABTMP, 0);
134 xtab_append(nfs_export *exp)
139 if ((lockid = xflock(_PATH_XTAB, "w")) < 0)
141 setexportent(_PATH_XTAB, "a");
143 strncpy(xe.e_hostname, exp->m_client->m_hostname,
144 sizeof (xe.e_hostname) - 1);
145 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
153 * rename newfile onto oldfile unless
156 static void cond_rename(char *newfile, char *oldfile)
159 char nbuf[4096], obuf[4096];
162 nfd = open(newfile, 0);
165 ofd = open(oldfile, 0);
168 rename(newfile, oldfile);
173 ncnt = read(nfd, nbuf, sizeof(nbuf));
176 ocnt = read(ofd, obuf, sizeof(obuf));
187 } while (memcmp(obuf, nbuf, ncnt) == 0);
192 rename(newfile, oldfile);