3 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed for the FreeBSD project
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #include <sys/types.h>
43 #include <sys/mman.h> /* For mmap() */
50 FileLayout *status_info; /* Pointer to the mmap()ed status file */
51 static int status_fd; /* File descriptor for the open file */
52 static off_t status_file_len; /* Current on-disc length of file */
54 /* sync_file --------------------------------------------------------------- */
56 Purpose: Packaged call of msync() to flush changes to mmap()ed file
57 Returns: Nothing. Errors to syslog.
62 if (msync((void *)status_info, 0, 0) < 0)
64 syslog(LOG_ERR, "msync() failed: %s", strerror(errno));
68 /* find_host -------------------------------------------------------------- */
70 Purpose: Find the entry in the status file for a given host
71 Returns: Pointer to that entry in the mmap() region, or NULL.
72 Notes: Also creates entries if requested.
73 Failure to create also returns NULL.
76 HostInfo *find_host(char *hostname, int create)
79 HostInfo *spare_slot = NULL;
80 HostInfo *result = NULL;
83 for (i = 0, hp = status_info->hosts; i < status_info->noOfHosts; i++, hp++)
85 if (!strncasecmp(hostname, hp->hostname, SM_MAXSTRLEN))
90 if (!spare_slot && !hp->monList && !hp->notifyReqd)
94 /* Return if entry found, or if not asked to create one. */
95 if (result || !create) return (result);
97 /* Now create an entry, using the spare slot if one was found or */
98 /* adding to the end of the list otherwise, extending file if reqd */
102 spare_slot = &status_info->hosts[status_info->noOfHosts];
103 desired_size = ((char*)spare_slot - (char*)status_info) + sizeof(HostInfo);
104 if (desired_size > status_file_len)
106 /* Extend file by writing 1 byte of junk at the desired end pos */
107 lseek(status_fd, desired_size - 1, SEEK_SET);
108 i = write(status_fd, &i, 1);
111 syslog(LOG_ERR, "Unable to extend status file");
114 status_file_len = desired_size;
116 status_info->noOfHosts++;
119 /* Initialise the spare slot that has been found/created */
120 /* Note that we do not msync(), since the caller is presumed to be */
121 /* about to modify the entry further */
122 memset(spare_slot, 0, sizeof(HostInfo));
123 strncpy(spare_slot->hostname, hostname, SM_MAXSTRLEN);
127 /* init_file -------------------------------------------------------------- */
129 Purpose: Open file, create if necessary, initialise it.
130 Returns: Nothing - exits on error
131 Notes: Called before process becomes daemon, hence logs to
132 stderr rather than syslog.
133 Opens the file, then mmap()s it for ease of access.
134 Also performs initial clean-up of the file, zeroing
135 monitor list pointers, setting the notifyReqd flag in
136 all hosts that had a monitor list, and incrementing
137 the state number to the next even value.
140 void init_file(const char *filename)
142 int new_file = FALSE;
143 char buf[HEADER_LEN];
146 /* try to open existing file - if not present, create one */
147 status_fd = open(filename, O_RDWR);
148 if ((status_fd < 0) && (errno == ENOENT))
150 status_fd = open(filename, O_RDWR | O_CREAT, 0644);
154 errx(1, "unable to open status file %s", filename);
156 /* File now open. mmap() it, with a generous size to allow for */
157 /* later growth, where we will extend the file but not re-map it. */
158 status_info = (FileLayout *)
159 mmap(NULL, 0x10000000, PROT_READ | PROT_WRITE, MAP_SHARED, status_fd, 0);
161 if (status_info == (FileLayout *) MAP_FAILED)
162 warn("unable to mmap() status file");
164 status_file_len = lseek(status_fd, 0L, SEEK_END);
166 /* If the file was not newly created, validate the contents, and if */
167 /* defective, re-create from scratch. */
170 if ((status_file_len < HEADER_LEN) || (status_file_len
171 < (HEADER_LEN + sizeof(HostInfo) * status_info->noOfHosts)) )
173 warnx("status file is corrupt");
178 /* Initialisation of a new, empty file. */
181 memset(buf, 0, sizeof(buf));
182 lseek(status_fd, 0L, SEEK_SET);
183 write(status_fd, buf, HEADER_LEN);
184 status_file_len = HEADER_LEN;
188 /* Clean-up of existing file - monitored hosts will have a pointer */
189 /* to a list of clients, which refers to memory in the previous */
190 /* incarnation of the program and so are meaningless now. These */
191 /* pointers are zeroed and the fact that the host was previously */
192 /* monitored is recorded by setting the notifyReqd flag, which will */
193 /* in due course cause a SM_NOTIFY to be sent. */
194 /* Note that if we crash twice in quick succession, some hosts may */
195 /* already have notifyReqd set, where we didn't manage to notify */
196 /* them before the second crash occurred. */
197 for (i = 0; i < status_info->noOfHosts; i++)
199 HostInfo *this_host = &status_info->hosts[i];
201 if (this_host->monList)
203 this_host->notifyReqd = TRUE;
204 this_host->monList = NULL;
207 /* Select the next higher even number for the state counter */
208 status_info->ourState = (status_info->ourState + 2) & 0xfffffffe;
209 /*???????******/ status_info->ourState++;
213 /* notify_one_host --------------------------------------------------------- */
215 Purpose: Perform SM_NOTIFY procedure at specified host
216 Returns: TRUE if success, FALSE if failed.
219 static int notify_one_host(char *hostname)
221 struct timeval timeout = { 20, 0 }; /* 20 secs timeout */
225 char our_hostname[SM_MAXSTRLEN+1];
227 gethostname(our_hostname, sizeof(our_hostname));
228 our_hostname[SM_MAXSTRLEN] = '\0';
229 arg.mon_name = our_hostname;
230 arg.state = status_info->ourState;
232 if (debug) syslog (LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s", hostname, our_hostname);
234 cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp");
237 syslog(LOG_ERR, "Failed to contact host %s%s", hostname,
238 clnt_spcreateerror(""));
242 if (clnt_call(cli, SM_NOTIFY, (xdrproc_t)xdr_stat_chge, &arg,
243 (xdrproc_t)xdr_void, &dummy, timeout)
246 syslog(LOG_ERR, "Failed to contact rpc.statd at host %s", hostname);
255 /* notify_hosts ------------------------------------------------------------ */
257 Purpose: Send SM_NOTIFY to all hosts marked as requiring it
258 Returns: Nothing, immediately - forks a process to do the work.
259 Notes: Does nothing if there are no monitored hosts.
260 Called after all the initialisation has been done -
264 void notify_hosts(void)
268 int work_to_do = FALSE;
272 /* First check if there is in fact any work to do. */
273 for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++)
282 if (!work_to_do) return; /* No work found */
287 syslog(LOG_ERR, "Unable to fork notify process - %s", strerror(errno));
292 /* Here in the child process. We continue until all the hosts marked */
293 /* as requiring notification have been duly notified. */
294 /* If one of the initial attempts fails, we sleep for a while and */
295 /* have another go. This is necessary because when we have crashed, */
296 /* (eg. a power outage) it is quite possible that we won't be able to */
297 /* contact all monitored hosts immediately on restart, either because */
298 /* they crashed too and take longer to come up (in which case the */
299 /* notification isn't really required), or more importantly if some */
300 /* router etc. needed to reach the monitored host has not come back */
301 /* up yet. In this case, we will be a bit late in re-establishing */
302 /* locks (after the grace period) but that is the best we can do. */
303 /* We try 10 times at 5 sec intervals, 10 more times at 1 minute */
304 /* intervals, then 24 more times at hourly intervals, finally */
305 /* giving up altogether if the host hasn't come back to life after */
308 for (attempts = 0; attempts < 44; attempts++)
310 work_to_do = FALSE; /* Unless anything fails */
311 for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++)
315 if (notify_one_host(hp->hostname))
317 hp->notifyReqd = FALSE;
320 else work_to_do = TRUE;
323 if (!work_to_do) break;
324 if (attempts < 10) sleep(5);
325 else if (attempts < 20) sleep(60);