2 * support/export/xtab.c
4 * Interface to the xtab file.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
11 #include <sys/fcntl.h>
21 static void cond_rename(char *newfile, char *oldfile);
24 xtab_read(char *xtab, int is_export)
26 /* is_export == 0 => reading /proc/fs/nfs/exports - we know these things are exported to kernel
27 * is_export == 1 => reading /var/lib/nfs/etab - these things are allowed to be exported
28 * is_export == 2 => reading /var/lib/nfs/xtab - these things might be known to kernel
34 if ((lockid = xflock(xtab, "r")) < 0)
36 setexportent(xtab, "r");
37 while ((xp = getexportent(is_export==0, 0)) != NULL) {
38 if (!(exp = export_lookup(xp->e_hostname, xp->e_path, is_export != 1)) &&
39 !(exp = export_create(xp, is_export!=1))) {
51 exp->m_exported = -1;/* may be exported */
65 if ((fd=open(_PATH_PROC_EXPORTS, O_RDONLY))>=0) {
67 return xtab_read(_PATH_PROC_EXPORTS, 0);
68 } else if ((fd=open(_PATH_PROC_EXPORTS_ALT, O_RDONLY) >= 0)) {
70 return xtab_read(_PATH_PROC_EXPORTS_ALT, 0);
72 return xtab_read(_PATH_XTAB, 2);
76 xtab_export_read(void)
78 return xtab_read(_PATH_ETAB, 1);
82 xtab_write(char *xtab, char *xtabtmp, int is_export)
88 if ((lockid = xflock(xtab, "w")) < 0) {
89 xlog(L_ERROR, "can't lock %s for writing", xtab);
92 setexportent(xtabtmp, "w");
94 for (i = 0; i < MCL_MAXTYPES; i++) {
95 for (exp = exportlist[i]; exp; exp = exp->m_next) {
96 if (is_export && !exp->m_xtabent)
98 if (!is_export && ! exp->m_exported)
101 /* write out the export entry using the FQDN */
103 strncpy(xe.e_hostname,
104 exp->m_client->m_hostname,
105 sizeof (xe.e_hostname) - 1);
106 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
112 cond_rename(xtabtmp, xtab);
122 return xtab_write(_PATH_ETAB, _PATH_ETABTMP, 1);
128 return xtab_write(_PATH_XTAB, _PATH_XTABTMP, 0);
132 xtab_append(nfs_export *exp)
137 if ((lockid = xflock(_PATH_XTAB, "w")) < 0)
139 setexportent(_PATH_XTAB, "a");
141 strncpy(xe.e_hostname, exp->m_client->m_hostname,
142 sizeof (xe.e_hostname) - 1);
143 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
151 * rename newfile onto oldfile unless
154 static void cond_rename(char *newfile, char *oldfile)
157 char nbuf[4096], obuf[4096];
160 nfd = open(newfile, 0);
163 ofd = open(oldfile, 0);
166 rename(newfile, oldfile);
171 ncnt = read(nfd, nbuf, sizeof(nbuf));
174 ocnt = read(ofd, obuf, sizeof(obuf));
185 } while (memcmp(obuf, nbuf, ncnt) == 0);
190 rename(newfile, oldfile);