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 /* If new path is a link do not destroy it but place the
29 * file where the link points.
33 slink_safe_rename(const char * oldpath, const char * newpath)
37 char slink_path[PATH_MAX];
38 const char *real_newpath = newpath;
40 if ((lstat(newpath, &s) == 0) && S_ISLNK(s.st_mode)) {
41 /* New path is a symbolic link, do not destroy but follow */
42 if ((r = readlink(newpath, slink_path, PATH_MAX - 1)) == -1)
45 real_newpath = slink_path;
48 return rename(oldpath, real_newpath);
52 mountlist_add(char *host, const char *path)
59 if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
62 while ((rep = getrmtabent(1, &pos)) != NULL) {
63 if (strcmp (rep->r_client,
65 && strcmp(rep->r_path, path) == 0) {
67 /* PRC: do the HA callout: */
68 ha_callout("mount", rep->r_client, rep->r_path, rep->r_count);
69 putrmtabent(rep, &pos);
76 strncpy(xe.r_client, host,
77 sizeof (xe.r_client) - 1);
78 xe.r_client [sizeof (xe.r_client) - 1] = '\0';
79 strncpy(xe.r_path, path, sizeof (xe.r_path) - 1);
80 xe.r_path [sizeof (xe.r_path) - 1] = '\0';
82 if (setrmtabent("a")) {
83 /* PRC: do the HA callout: */
84 ha_callout("mount", xe.r_client, xe.r_path, xe.r_count);
85 putrmtabent(&xe, NULL);
92 mountlist_del(char *hname, const char *path)
99 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
101 if (!setrmtabent("r")) {
105 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
110 while ((rep = getrmtabent(1, NULL)) != NULL) {
111 match = !strcmp (rep->r_client, hname)
112 && !strcmp(rep->r_path, path);
115 /* PRC: do the HA callout: */
116 ha_callout("unmount", rep->r_client, rep->r_path, rep->r_count);
118 if (!match || rep->r_count)
119 fputrmtabent(fp, rep, NULL);
121 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
122 xlog(L_ERROR, "couldn't rename %s to %s",
123 _PATH_RMTABTMP, _PATH_RMTAB);
125 endrmtabent(); /* close & unlink */
131 mountlist_del_all(struct sockaddr_in *sin)
133 struct in_addr addr = sin->sin_addr;
135 struct rmtabent *rep;
140 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
142 if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
143 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
148 hp = hostent_dup (hp);
150 if (!setrmtabent("r")) {
155 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
161 while ((rep = getrmtabent(1, NULL)) != NULL) {
162 if (strcmp(rep->r_client, hp->h_name) == 0 &&
163 (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;
189 if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
191 if (stat(_PATH_RMTAB, &stb) < 0) {
192 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
195 if (stb.st_mtime != last_mtime) {
197 mlist = (m = mlist)->ml_next;
198 xfree(m->ml_hostname);
199 xfree(m->ml_directory);
202 last_mtime = stb.st_mtime;
205 while ((rep = getrmtabent(1, NULL)) != NULL) {
206 m = (mountlist) xmalloc(sizeof(*m));
207 m->ml_hostname = xstrdup(rep->r_client);
208 m->ml_directory = xstrdup(rep->r_path);