Initial revision
[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         struct exportent        *xp;
25         nfs_export              *exp;
26         int                     lockid;
27
28         if ((lockid = xflock(xtab, "r")) < 0)
29                 return 0;
30         setexportent(xtab, "r");
31         while ((xp = getexportent()) != NULL) {
32                 if (!(exp = export_lookup(xp->e_hostname, xp->e_path)) &&
33                     !(exp = export_create(xp))) {
34                         continue;
35                 }
36                 if (is_export) {
37                         exp->m_xtabent = 1;
38                         exp->m_mayexport = 1;
39                 } else
40                         exp->m_exported = 1;
41         }
42         endexportent();
43         xfunlock(lockid);
44
45         return 0;
46 }
47
48 int
49 xtab_mount_read(void)
50 {
51         int fd;
52         if ((fd=open(_PATH_PROC_EXPORTS, O_RDONLY))>=0) {
53                 close(fd);
54                 return xtab_read(_PATH_PROC_EXPORTS, 0);
55         } else
56                 return xtab_read(_PATH_XTAB, 0);
57 }
58
59 int
60 xtab_export_read(void)
61 {
62         return xtab_read(_PATH_ETAB, 1);
63 }
64
65 static int
66 xtab_write(char *xtab, char *xtabtmp, int is_export)
67 {
68         struct exportent        xe;
69         nfs_export              *exp;
70         int                     lockid, i;
71
72         if ((lockid = xflock(xtab, "w")) < 0) {
73                 xlog(L_ERROR, "can't lock %s for writing", xtab);
74                 return 0;
75         }
76         setexportent(xtabtmp, "w");
77
78         for (i = 0; i < MCL_MAXTYPES; i++) {
79                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
80                         if (is_export && !exp->m_xtabent)
81                                 continue;
82                         if (!is_export && ! exp->m_exported)
83                                 continue;
84
85                         /* write out the export entry using the FQDN */
86                         xe = exp->m_export;
87                         strncpy(xe.e_hostname,
88                                 exp->m_client->m_hostname,
89                                 sizeof (xe.e_hostname) - 1);
90                         xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
91                         putexportent(&xe);
92                 }
93         }
94         endexportent();
95
96         rename(xtabtmp, xtab);
97
98         xfunlock(lockid);
99
100         return 1;
101 }
102
103 int
104 xtab_export_write()
105 {
106         return xtab_write(_PATH_ETAB, _PATH_ETABTMP, 1);
107 }
108
109 int
110 xtab_mount_write()
111 {
112         return xtab_write(_PATH_XTAB, _PATH_XTABTMP, 0);
113 }
114
115 void
116 xtab_append(nfs_export *exp)
117 {
118         struct exportent xe;
119         int             lockid;
120
121         if ((lockid = xflock(_PATH_XTAB, "w")) < 0)
122                 return;
123         setexportent(_PATH_XTAB, "a");
124         xe = exp->m_export;
125         strncpy(xe.e_hostname, exp->m_client->m_hostname,
126                sizeof (xe.e_hostname) - 1);
127         xe.e_hostname[sizeof (xe.e_hostname) - 1] = '\0';
128         putexportent(&xe);
129         endexportent();
130         xfunlock(lockid);
131         exp->m_xtabent = 1;
132 }
133