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);
69 return xtab_read(_PATH_XTAB, 2);
73 xtab_export_read(void)
75 return xtab_read(_PATH_ETAB, 1);
79 xtab_write(char *xtab, char *xtabtmp, int is_export)
85 if ((lockid = xflock(xtab, "w")) < 0) {
86 xlog(L_ERROR, "can't lock %s for writing", xtab);
89 setexportent(xtabtmp, "w");
91 for (i = 0; i < MCL_MAXTYPES; i++) {
92 for (exp = exportlist[i]; exp; exp = exp->m_next) {
93 if (is_export && !exp->m_xtabent)
95 if (!is_export && ! exp->m_exported)
98 /* write out the export entry using the FQDN */
100 strncpy(xe.e_hostname,
101 exp->m_client->m_hostname,
102 sizeof (xe.e_hostname) - 1);
103 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
109 cond_rename(xtabtmp, xtab);
119 return xtab_write(_PATH_ETAB, _PATH_ETABTMP, 1);
125 return xtab_write(_PATH_XTAB, _PATH_XTABTMP, 0);
129 xtab_append(nfs_export *exp)
134 if ((lockid = xflock(_PATH_XTAB, "w")) < 0)
136 setexportent(_PATH_XTAB, "a");
138 strncpy(xe.e_hostname, exp->m_client->m_hostname,
139 sizeof (xe.e_hostname) - 1);
140 xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
148 * rename newfile onto oldfile unless
151 static void cond_rename(char *newfile, char *oldfile)
154 char nbuf[4096], obuf[4096];
157 nfd = open(newfile, 0);
160 ofd = open(oldfile, 0);
163 rename(newfile, oldfile);
168 ncnt = read(nfd, nbuf, sizeof(nbuf));
171 ocnt = read(ofd, obuf, sizeof(obuf));
182 } while (memcmp(obuf, nbuf, ncnt) == 0);
187 rename(newfile, oldfile);