]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/xtab.c
d9265a24e078627725ea4319e89f43a72f5cb7d9
[nfs-utils.git] / support / export / xtab.c
1 /*
2  * support/export/xtab.c
3  *
4  * Interface to the xtab file.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include "config.h"
10
11 #include <sys/fcntl.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "xmalloc.h"
16 #include "nfslib.h"
17 #include "exportfs.h"
18 #include "xio.h"
19 #include "xlog.h"
20
21 static void cond_rename(char *newfile, char *oldfile);
22
23 static int
24 xtab_read(char *xtab, int is_export)
25 {
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
29      */
30         struct exportent        *xp;
31         nfs_export              *exp;
32         int                     lockid;
33
34         if ((lockid = xflock(xtab, "r")) < 0)
35                 return 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))) {
40                         continue;
41                 }
42                 switch (is_export) {
43                 case 0:
44                         exp->m_exported = 1;
45                         break;
46                 case 1:
47                         exp->m_xtabent = 1;
48                         exp->m_mayexport = 1;
49                         break;
50                 case 2:
51                         exp->m_exported = -1;/* may be exported */
52                         break;
53                 }
54         }
55         endexportent();
56         xfunlock(lockid);
57
58         return 0;
59 }
60
61 int
62 xtab_mount_read(void)
63 {
64         int fd;
65         if ((fd=open(_PATH_PROC_EXPORTS, O_RDONLY))>=0) {
66                 close(fd);
67                 return xtab_read(_PATH_PROC_EXPORTS, 0);
68         } else
69                 return xtab_read(_PATH_XTAB, 2);
70 }
71
72 int
73 xtab_export_read(void)
74 {
75         return xtab_read(_PATH_ETAB, 1);
76 }
77
78 static int
79 xtab_write(char *xtab, char *xtabtmp, int is_export)
80 {
81         struct exportent        xe;
82         nfs_export              *exp;
83         int                     lockid, i;
84
85         if ((lockid = xflock(xtab, "w")) < 0) {
86                 xlog(L_ERROR, "can't lock %s for writing", xtab);
87                 return 0;
88         }
89         setexportent(xtabtmp, "w");
90
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)
94                                 continue;
95                         if (!is_export && ! exp->m_exported)
96                                 continue;
97
98                         /* write out the export entry using the FQDN */
99                         xe = exp->m_export;
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';
104                         putexportent(&xe);
105                 }
106         }
107         endexportent();
108
109         cond_rename(xtabtmp, xtab);
110
111         xfunlock(lockid);
112
113         return 1;
114 }
115
116 int
117 xtab_export_write()
118 {
119         return xtab_write(_PATH_ETAB, _PATH_ETABTMP, 1);
120 }
121
122 int
123 xtab_mount_write()
124 {
125         return xtab_write(_PATH_XTAB, _PATH_XTABTMP, 0);
126 }
127
128 void
129 xtab_append(nfs_export *exp)
130 {
131         struct exportent xe;
132         int             lockid;
133
134         if ((lockid = xflock(_PATH_XTAB, "w")) < 0)
135                 return;
136         setexportent(_PATH_XTAB, "a");
137         xe = exp->m_export;
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';
141         putexportent(&xe);
142         endexportent();
143         xfunlock(lockid);
144         exp->m_xtabent = 1;
145 }
146
147 /*
148  * rename newfile onto oldfile unless
149  * they are identical 
150  */
151 static void cond_rename(char *newfile, char *oldfile)
152 {
153         int nfd, ofd;
154         char nbuf[4096], obuf[4096];
155         int ncnt, ocnt;
156
157         nfd = open(newfile, 0);
158         if (nfd < 0)
159                 return;
160         ofd = open(oldfile, 0);
161         if (ofd < 0) {
162                 close(nfd);
163                 rename(newfile, oldfile);
164                 return;
165         }
166
167         do {
168                 ncnt = read(nfd, nbuf, sizeof(nbuf));
169                 if (ncnt < 0)
170                         break;
171                 ocnt = read(ofd, obuf, sizeof(obuf));
172                 if (ocnt < 0)
173                         break;
174                 if (ncnt != ocnt)
175                         break;
176                 if (ncnt == 0) {
177                         close(nfd);
178                         close(ofd);
179                         unlink(newfile);
180                         return;
181                 }
182         } while (memcmp(obuf, nbuf, ncnt) == 0);
183
184         /* some mis-match */
185         close(nfd);
186         close(ofd);
187         rename(newfile, oldfile);
188         return;
189 }