Initial revision
[nfs-utils.git] / support / export / rmtab.c
1 /*
2  * support/export/rmntab.c
3  *
4  * Interface to the rmnt file.
5  *
6  */
7
8 #include "config.h"
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "xmalloc.h"
14 #include "misc.h"
15 #include "nfslib.h"
16 #include "exportfs.h"
17 #include "xio.h"
18 #include "xlog.h"
19
20 int
21 rmtab_read(void)
22 {
23         struct rmtabent         *rep;
24         nfs_export              *exp;
25
26         setrmtabent("r");
27         while ((rep = getrmtabent(1)) != NULL) {
28                 exp = export_lookup(rep->r_client, rep->r_path);
29                 if (!exp) {
30                         struct exportent        *xp;
31                         struct hostent          *hp;
32                         int                     htype;
33
34                         htype = client_gettype(rep->r_client);
35                         if (htype == MCL_FQDN
36                             && (hp = gethostbyname (rep->r_client), hp)
37                             && (hp = hostent_dup (hp),
38                                    xp = export_allowed (hp, rep->r_path))) {
39                                 strncpy (xp->e_hostname, rep->r_client,
40                                          sizeof (xp->e_hostname) - 1);
41                                 xp->e_hostname[sizeof (xp->e_hostname) -1] = '\0';
42                                 exp = export_create(xp);
43                                 free (hp);
44                         }
45
46                         if (!exp)
47                                 continue;
48                         exp->m_mayexport = 1;
49                 }
50         }
51         if (errno == EINVAL) {
52                 /* Something goes wrong. We need to fix the rmtab
53                    file. */
54                 int     lockid;
55                 FILE    *fp;
56                 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
57                         return -1;
58                 rewindrmtabent();
59                 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
60                         endrmtabent ();
61                         xfunlock(lockid);
62                         return -1;
63                 }
64                 while ((rep = getrmtabent(0)) != NULL) {
65                         fputrmtabent(fp, rep);
66                 }
67                 if (rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
68                         xlog(L_ERROR, "couldn't rename %s to %s",
69                              _PATH_RMTABTMP, _PATH_RMTAB);
70                 }
71                 endrmtabent();
72                 fendrmtabent(fp);
73                 xfunlock(lockid);
74         }
75         else {
76                 endrmtabent();
77         }
78         return 0;
79 }