4 * Manage the rmtab file for mountd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
11 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
22 #include "ha-callout.h"
24 #include <limits.h> /* PATH_MAX */
26 /* If new path is a link do not destroy it but place the
27 * file where the link points.
31 slink_safe_rename(const char * oldpath, const char * newpath)
35 char slink_path[PATH_MAX];
36 const char *real_newpath = newpath;
38 if ((lstat(newpath, &s) == 0) && S_ISLNK(s.st_mode)) {
39 /* New path is a symbolic link, do not destroy but follow */
40 if ((r = readlink(newpath, slink_path, PATH_MAX - 1)) == -1)
43 real_newpath = slink_path;
46 return rename(oldpath, real_newpath);
50 mountlist_add(char *host, const char *path)
57 if ((lockid = xflock(_PATH_RMTAB, "a")) < 0)
60 while ((rep = getrmtabent(1, &pos)) != NULL) {
61 if (strcmp (rep->r_client,
63 && strcmp(rep->r_path, path) == 0) {
65 /* PRC: do the HA callout: */
66 ha_callout("mount", rep->r_client, rep->r_path, rep->r_count);
67 putrmtabent(rep, &pos);
74 strncpy(xe.r_client, host,
75 sizeof (xe.r_client) - 1);
76 xe.r_client [sizeof (xe.r_client) - 1] = '\0';
77 strncpy(xe.r_path, path, sizeof (xe.r_path) - 1);
78 xe.r_path [sizeof (xe.r_path) - 1] = '\0';
80 if (setrmtabent("a")) {
81 /* PRC: do the HA callout: */
82 ha_callout("mount", xe.r_client, xe.r_path, xe.r_count);
83 putrmtabent(&xe, NULL);
90 mountlist_del(char *hname, const char *path)
97 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
99 if (!setrmtabent("r")) {
103 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
108 while ((rep = getrmtabent(1, NULL)) != NULL) {
109 match = !strcmp (rep->r_client, hname)
110 && !strcmp(rep->r_path, path);
113 /* PRC: do the HA callout: */
114 ha_callout("unmount", rep->r_client, rep->r_path, rep->r_count);
116 if (!match || rep->r_count)
117 fputrmtabent(fp, rep, NULL);
119 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
120 xlog(L_ERROR, "couldn't rename %s to %s",
121 _PATH_RMTABTMP, _PATH_RMTAB);
123 endrmtabent(); /* close & unlink */
129 mountlist_del_all(struct sockaddr_in *sin)
131 struct in_addr addr = sin->sin_addr;
133 struct rmtabent *rep;
138 if ((lockid = xflock(_PATH_RMTAB, "w")) < 0)
140 if (!(hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
141 xlog(L_ERROR, "can't get hostname of %s", inet_ntoa(addr));
146 hp = hostent_dup (hp);
148 if (!setrmtabent("r")) {
153 if (!(fp = fsetrmtabent(_PATH_RMTABTMP, "w"))) {
159 while ((rep = getrmtabent(1, NULL)) != NULL) {
160 if (strcmp(rep->r_client, hp->h_name) == 0 &&
161 (exp = auth_authenticate("umountall", sin, rep->r_path))) {
165 fputrmtabent(fp, rep, NULL);
167 if (slink_safe_rename(_PATH_RMTABTMP, _PATH_RMTAB) < 0) {
168 xlog(L_ERROR, "couldn't rename %s to %s",
169 _PATH_RMTABTMP, _PATH_RMTAB);
171 endrmtabent(); /* close & unlink */
180 static mountlist mlist = NULL;
181 static time_t last_mtime = 0;
183 struct rmtabent *rep;
187 if ((lockid = xflock(_PATH_RMTAB, "r")) < 0)
189 if (stat(_PATH_RMTAB, &stb) < 0) {
190 xlog(L_ERROR, "can't stat %s", _PATH_RMTAB);
193 if (stb.st_mtime != last_mtime) {
195 mlist = (m = mlist)->ml_next;
196 xfree(m->ml_hostname);
197 xfree(m->ml_directory);
200 last_mtime = stb.st_mtime;
203 while ((rep = getrmtabent(1, NULL)) != NULL) {
204 m = (mountlist) xmalloc(sizeof(*m));
205 m->ml_hostname = xstrdup(rep->r_client);
206 m->ml_directory = xstrdup(rep->r_path);