2 * SPDX-License-Identifier: BSD-4-Clause
5 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the FreeBSD project
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #include <sys/types.h>
46 #include <sys/mman.h> /* For mmap() */
53 FileLayout *status_info; /* Pointer to the mmap()ed status file */
54 static int status_fd; /* File descriptor for the open file */
55 static off_t status_file_len; /* Current on-disc length of file */
57 /* sync_file --------------------------------------------------------------- */
59 Purpose: Packaged call of msync() to flush changes to mmap()ed file
60 Returns: Nothing. Errors to syslog.
65 if (msync((void *)status_info, 0, 0) < 0)
67 syslog(LOG_ERR, "msync() failed: %s", strerror(errno));
71 /* find_host -------------------------------------------------------------- */
73 Purpose: Find the entry in the status file for a given host
74 Returns: Pointer to that entry in the mmap() region, or NULL.
75 Notes: Also creates entries if requested.
76 Failure to create also returns NULL.
79 HostInfo *find_host(char *hostname, int create)
82 HostInfo *spare_slot = NULL;
83 HostInfo *result = NULL;
84 struct addrinfo *ai1, *ai2;
88 if (getaddrinfo(hostname, NULL, NULL, &ai1) != 0)
90 for (i = 0, hp = status_info->hosts; i < status_info->noOfHosts; i++, hp++)
92 if (!strncasecmp(hostname, hp->hostname, SM_MAXSTRLEN))
97 if (hp->hostname[0] != '\0' &&
98 getaddrinfo(hp->hostname, NULL, NULL, &ai2) != 0)
102 struct addrinfo *p1, *p2;
103 for (p1 = ai1; !result && p1; p1 = p1->ai_next)
105 for (p2 = ai2; !result && p2; p2 = p2->ai_next)
107 if (p1->ai_family == p2->ai_family
108 && p1->ai_addrlen == p2->ai_addrlen
109 && !memcmp(p1->ai_addr, p2->ai_addr, p1->ai_addrlen))
123 if (!spare_slot && !hp->monList && !hp->notifyReqd)
129 /* Return if entry found, or if not asked to create one. */
130 if (result || !create) return (result);
132 /* Now create an entry, using the spare slot if one was found or */
133 /* adding to the end of the list otherwise, extending file if reqd */
137 spare_slot = &status_info->hosts[status_info->noOfHosts];
138 desired_size = ((char*)spare_slot - (char*)status_info) + sizeof(HostInfo);
139 if (desired_size > status_file_len)
141 /* Extend file by writing 1 byte of junk at the desired end pos */
142 if (lseek(status_fd, desired_size - 1, SEEK_SET) == -1 ||
143 write(status_fd, "\0", 1) < 0)
145 syslog(LOG_ERR, "Unable to extend status file");
148 status_file_len = desired_size;
150 status_info->noOfHosts++;
153 /* Initialise the spare slot that has been found/created */
154 /* Note that we do not msync(), since the caller is presumed to be */
155 /* about to modify the entry further */
156 memset(spare_slot, 0, sizeof(HostInfo));
157 strncpy(spare_slot->hostname, hostname, SM_MAXSTRLEN);
161 /* init_file -------------------------------------------------------------- */
163 Purpose: Open file, create if necessary, initialise it.
164 Returns: Nothing - exits on error
165 Notes: Called before process becomes daemon, hence logs to
166 stderr rather than syslog.
167 Opens the file, then mmap()s it for ease of access.
168 Also performs initial clean-up of the file, zeroing
169 monitor list pointers, setting the notifyReqd flag in
170 all hosts that had a monitor list, and incrementing
171 the state number to the next even value.
174 void init_file(const char *filename)
176 int new_file = FALSE;
177 char buf[HEADER_LEN];
180 /* try to open existing file - if not present, create one */
181 status_fd = open(filename, O_RDWR);
182 if ((status_fd < 0) && (errno == ENOENT))
184 status_fd = open(filename, O_RDWR | O_CREAT, 0644);
188 errx(1, "unable to open status file %s", filename);
190 /* File now open. mmap() it, with a generous size to allow for */
191 /* later growth, where we will extend the file but not re-map it. */
192 status_info = (FileLayout *)
193 mmap(NULL, 0x10000000, PROT_READ | PROT_WRITE, MAP_SHARED, status_fd, 0);
195 if (status_info == (FileLayout *) MAP_FAILED)
196 err(1, "unable to mmap() status file");
198 status_file_len = lseek(status_fd, 0L, SEEK_END);
200 /* If the file was not newly created, validate the contents, and if */
201 /* defective, re-create from scratch. */
204 if ((status_file_len < (off_t)HEADER_LEN) || (status_file_len
205 < (off_t)(HEADER_LEN + sizeof(HostInfo) * status_info->noOfHosts)) )
207 warnx("status file is corrupt");
212 /* Initialisation of a new, empty file. */
215 memset(buf, 0, sizeof(buf));
216 lseek(status_fd, 0L, SEEK_SET);
217 write(status_fd, buf, HEADER_LEN);
218 status_file_len = HEADER_LEN;
222 /* Clean-up of existing file - monitored hosts will have a pointer */
223 /* to a list of clients, which refers to memory in the previous */
224 /* incarnation of the program and so are meaningless now. These */
225 /* pointers are zeroed and the fact that the host was previously */
226 /* monitored is recorded by setting the notifyReqd flag, which will */
227 /* in due course cause a SM_NOTIFY to be sent. */
228 /* Note that if we crash twice in quick succession, some hosts may */
229 /* already have notifyReqd set, where we didn't manage to notify */
230 /* them before the second crash occurred. */
231 for (i = 0; i < status_info->noOfHosts; i++)
233 HostInfo *this_host = &status_info->hosts[i];
235 if (this_host->monList)
237 this_host->notifyReqd = TRUE;
238 this_host->monList = NULL;
241 /* Select the next higher even number for the state counter */
242 status_info->ourState = (status_info->ourState + 2) & 0xfffffffe;
243 /*???????******/ status_info->ourState++;
247 /* notify_one_host --------------------------------------------------------- */
249 Purpose: Perform SM_NOTIFY procedure at specified host
250 Returns: TRUE if success, FALSE if failed.
251 Notes: Only report failure if verbose is non-zero. Caller will
252 only set verbose to non-zero for the first attempt to
256 static int notify_one_host(char *hostname, int verbose)
258 struct timeval timeout = { 20, 0 }; /* 20 secs timeout */
262 char our_hostname[SM_MAXSTRLEN+1];
264 gethostname(our_hostname, sizeof(our_hostname));
265 our_hostname[SM_MAXSTRLEN] = '\0';
266 arg.mon_name = our_hostname;
267 arg.state = status_info->ourState;
269 if (debug) syslog (LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s", hostname, our_hostname);
271 cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp");
274 syslog(LOG_ERR, "Failed to contact host %s%s", hostname,
275 clnt_spcreateerror(""));
279 if (clnt_call(cli, SM_NOTIFY, (xdrproc_t)xdr_stat_chge, &arg,
280 (xdrproc_t)xdr_void, &dummy, timeout)
284 syslog(LOG_ERR, "Failed to contact rpc.statd at host %s", hostname);
293 /* notify_hosts ------------------------------------------------------------ */
295 Purpose: Send SM_NOTIFY to all hosts marked as requiring it
296 Returns: Nothing, immediately - forks a process to do the work.
297 Notes: Does nothing if there are no monitored hosts.
298 Called after all the initialisation has been done -
302 void notify_hosts(void)
306 int work_to_do = FALSE;
310 /* First check if there is in fact any work to do. */
311 for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++)
320 if (!work_to_do) return; /* No work found */
325 syslog(LOG_ERR, "Unable to fork notify process - %s", strerror(errno));
330 /* Here in the child process. We continue until all the hosts marked */
331 /* as requiring notification have been duly notified. */
332 /* If one of the initial attempts fails, we sleep for a while and */
333 /* have another go. This is necessary because when we have crashed, */
334 /* (eg. a power outage) it is quite possible that we won't be able to */
335 /* contact all monitored hosts immediately on restart, either because */
336 /* they crashed too and take longer to come up (in which case the */
337 /* notification isn't really required), or more importantly if some */
338 /* router etc. needed to reach the monitored host has not come back */
339 /* up yet. In this case, we will be a bit late in re-establishing */
340 /* locks (after the grace period) but that is the best we can do. */
341 /* We try 10 times at 5 sec intervals, 10 more times at 1 minute */
342 /* intervals, then 24 more times at hourly intervals, finally */
343 /* giving up altogether if the host hasn't come back to life after */
346 for (attempts = 0; attempts < 44; attempts++)
348 work_to_do = FALSE; /* Unless anything fails */
349 for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++)
353 if (notify_one_host(hp->hostname, attempts == 0))
355 hp->notifyReqd = FALSE;
358 else work_to_do = TRUE;
361 if (!work_to_do) break;
362 if (attempts < 10) sleep(5);
363 else if (attempts < 20) sleep(60);