]> git.decadent.org.uk Git - nfs-utils.git/blob - support/export/xtab.c
2001-04-01 Chip Salzenberg <chip@valinux.com>
[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 int
22 xtab_read(char *xtab, int is_export)
23 {
24     /* is_export == 0  => reading /proc/fs/nfs/exports - we know these things are exported to kernel
25      * is_export == 1  => reading /var/lib/nfs/etab - these things are allowed to be exported
26      * is_export == 2  => reading /var/lib/nfs/xtab - these things might be known to kernel
27      */
28         struct exportent        *xp;
29         nfs_export              *exp;
30         int                     lockid;
31
32         if ((lockid = xflock(xtab, "r")) < 0)
33                 return 0;
34         setexportent(xtab, "r");
35         while ((xp = getexportent()) != NULL) {
36                 if (!(exp = export_lookup(xp->e_hostname, xp->e_path)) &&
37                     !(exp = export_create(xp))) {
38                         continue;
39                 }
40                 switch (is_export) {
41                 case 0:
42                         exp->m_exported = 1;
43                         break;
44                 case 1:
45                         exp->m_xtabent = 1;
46                         exp->m_mayexport = 1;
47                         break;
48                 case 2:
49                         exp->m_exported = -1;/* may be exported */
50                         break;
51                 }
52         }
53         endexportent();
54         xfunlock(lockid);
55
56         return 0;
57 }
58
59 int
60 xtab_mount_read(void)
61 {
62         int fd;
63         if ((fd=open(_PATH_PROC_EXPORTS, O_RDONLY))>=0) {
64                 close(fd);
65                 return xtab_read(_PATH_PROC_EXPORTS, 0);
66         } else
67                 return xtab_read(_PATH_XTAB, 2);
68 }
69
70 int
71 xtab_export_read(void)
72 {
73         return xtab_read(_PATH_ETAB, 1);
74 }
75
76 static int
77 xtab_write(char *xtab, char *xtabtmp, int is_export)
78 {
79         struct exportent        xe;
80         nfs_export              *exp;
81         int                     lockid, i;
82
83         if ((lockid = xflock(xtab, "w")) < 0) {
84                 xlog(L_ERROR, "can't lock %s for writing", xtab);
85                 return 0;
86         }
87         setexportent(xtabtmp, "w");
88
89         for (i = 0; i < MCL_MAXTYPES; i++) {
90                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
91                         if (is_export && !exp->m_xtabent)
92                                 continue;
93                         if (!is_export && ! exp->m_exported)
94                                 continue;
95
96                         /* write out the export entry using the FQDN */
97                         xe = exp->m_export;
98                         strncpy(xe.e_hostname,
99                                 exp->m_client->m_hostname,
100                                 sizeof (xe.e_hostname) - 1);
101                         xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
102                         putexportent(&xe);
103                 }
104         }
105         endexportent();
106
107         rename(xtabtmp, xtab);
108
109         xfunlock(lockid);
110
111         return 1;
112 }
113
114 int
115 xtab_export_write()
116 {
117         return xtab_write(_PATH_ETAB, _PATH_ETABTMP, 1);
118 }
119
120 int
121 xtab_mount_write()
122 {
123         return xtab_write(_PATH_XTAB, _PATH_XTABTMP, 0);
124 }
125
126 void
127 xtab_append(nfs_export *exp)
128 {
129         struct exportent xe;
130         int             lockid;
131
132         if ((lockid = xflock(_PATH_XTAB, "w")) < 0)
133                 return;
134         setexportent(_PATH_XTAB, "a");
135         xe = exp->m_export;
136         strncpy(xe.e_hostname, exp->m_client->m_hostname,
137                sizeof (xe.e_hostname) - 1);
138         xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
139         putexportent(&xe);
140         endexportent();
141         xfunlock(lockid);
142         exp->m_xtabent = 1;
143 }
144