4 * Manage the rmtab file for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <sys/types.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
24 #include "ha-callout.h"
26 #include <limits.h> /* PATH_MAX */
28 extern int reverse_resolve
;
30 /* If new path is a link do not destroy it but place the
31 * file where the link points.
35 slink_safe_rename(const char * oldpath
, const char * newpath
)
39 char slink_path
[PATH_MAX
];
40 const char *real_newpath
= newpath
;
42 if ((lstat(newpath
, &s
) == 0) && S_ISLNK(s
.st_mode
)) {
43 /* New path is a symbolic link, do not destroy but follow */
44 if ((r
= readlink(newpath
, slink_path
, PATH_MAX
- 1)) == -1)
47 real_newpath
= slink_path
;
50 return rename(oldpath
, real_newpath
);
54 mountlist_add(char *host
, const char *path
)
61 if ((lockid
= xflock(_PATH_RMTAB
, "a")) < 0)
64 while ((rep
= getrmtabent(1, &pos
)) != NULL
) {
65 if (strcmp (rep
->r_client
,
67 && strcmp(rep
->r_path
, path
) == 0) {
69 /* PRC: do the HA callout: */
70 ha_callout("mount", rep
->r_client
, rep
->r_path
, rep
->r_count
);
71 putrmtabent(rep
, &pos
);
78 strncpy(xe
.r_client
, host
,
79 sizeof (xe
.r_client
) - 1);
80 xe
.r_client
[sizeof (xe
.r_client
) - 1] = '\0';
81 strncpy(xe
.r_path
, path
, sizeof (xe
.r_path
) - 1);
82 xe
.r_path
[sizeof (xe
.r_path
) - 1] = '\0';
84 if (setrmtabent("a")) {
85 /* PRC: do the HA callout: */
86 ha_callout("mount", xe
.r_client
, xe
.r_path
, xe
.r_count
);
87 putrmtabent(&xe
, NULL
);
94 mountlist_del(char *hname
, const char *path
)
101 if ((lockid
= xflock(_PATH_RMTAB
, "w")) < 0)
103 if (!setrmtabent("r")) {
107 if (!(fp
= fsetrmtabent(_PATH_RMTABTMP
, "w"))) {
112 while ((rep
= getrmtabent(1, NULL
)) != NULL
) {
113 match
= !strcmp (rep
->r_client
, hname
)
114 && !strcmp(rep
->r_path
, path
);
117 /* PRC: do the HA callout: */
118 ha_callout("unmount", rep
->r_client
, rep
->r_path
, rep
->r_count
);
120 if (!match
|| rep
->r_count
)
121 fputrmtabent(fp
, rep
, NULL
);
123 if (slink_safe_rename(_PATH_RMTABTMP
, _PATH_RMTAB
) < 0) {
124 xlog(L_ERROR
, "couldn't rename %s to %s",
125 _PATH_RMTABTMP
, _PATH_RMTAB
);
127 endrmtabent(); /* close & unlink */
133 mountlist_del_all(struct sockaddr_in
*sin
)
135 struct in_addr addr
= sin
->sin_addr
;
137 struct rmtabent
*rep
;
142 if ((lockid
= xflock(_PATH_RMTAB
, "w")) < 0)
144 if (!(hp
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET
))) {
145 xlog(L_ERROR
, "can't get hostname of %s", inet_ntoa(addr
));
150 hp
= hostent_dup (hp
);
152 if (!setrmtabent("r")) {
157 if (!(fp
= fsetrmtabent(_PATH_RMTABTMP
, "w"))) {
163 while ((rep
= getrmtabent(1, NULL
)) != NULL
) {
164 if (strcmp(rep
->r_client
, hp
->h_name
) == 0 &&
165 (exp
= auth_authenticate("umountall", sin
, rep
->r_path
)))
167 fputrmtabent(fp
, rep
, NULL
);
169 if (slink_safe_rename(_PATH_RMTABTMP
, _PATH_RMTAB
) < 0) {
170 xlog(L_ERROR
, "couldn't rename %s to %s",
171 _PATH_RMTABTMP
, _PATH_RMTAB
);
173 endrmtabent(); /* close & unlink */
182 static mountlist mlist
= NULL
;
183 static time_t last_mtime
= 0;
185 struct rmtabent
*rep
;
191 if ((lockid
= xflock(_PATH_RMTAB
, "r")) < 0)
193 if (stat(_PATH_RMTAB
, &stb
) < 0) {
194 xlog(L_ERROR
, "can't stat %s", _PATH_RMTAB
);
197 if (stb
.st_mtime
!= last_mtime
) {
199 mlist
= (m
= mlist
)->ml_next
;
200 xfree(m
->ml_hostname
);
201 xfree(m
->ml_directory
);
204 last_mtime
= stb
.st_mtime
;
207 while ((rep
= getrmtabent(1, NULL
)) != NULL
) {
208 m
= (mountlist
) xmalloc(sizeof(*m
));
210 if (reverse_resolve
&&
211 inet_aton((const char *) rep
->r_client
, &addr
) &&
212 (he
= gethostbyaddr(&addr
, sizeof(addr
), AF_INET
)))
213 m
->ml_hostname
= xstrdup(he
->h_name
);
215 m
->ml_hostname
= xstrdup(rep
->r_client
);
217 m
->ml_directory
= xstrdup(rep
->r_path
);