1 /* 2 * Copyright (c) 1995 3 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 * 32 */ 33 34 #include <err.h> 35 #include <errno.h> 36 #include <fcntl.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <sys/types.h> 41 #include <sys/mman.h> /* For mmap() */ 42 #include <rpc/rpc.h> 43 #include <syslog.h> 44 45 #include "statd.h" 46 47 FileLayout *status_info; /* Pointer to the mmap()ed status file */ 48 static int status_fd; /* File descriptor for the open file */ 49 static off_t status_file_len; /* Current on-disc length of file */ 50 51 /* sync_file --------------------------------------------------------------- */ 52 /* 53 Purpose: Packaged call of msync() to flush changes to mmap()ed file 54 Returns: Nothing. Errors to syslog. 55 */ 56 57 void sync_file(void) 58 { 59 if (msync((void *)status_info, 0, 0) < 0) 60 { 61 syslog(LOG_ERR, "msync() failed: %s", strerror(errno)); 62 } 63 } 64 65 /* find_host -------------------------------------------------------------- */ 66 /* 67 Purpose: Find the entry in the status file for a given host 68 Returns: Pointer to that entry in the mmap() region, or NULL. 69 Notes: Also creates entries if requested. 70 Failure to create also returns NULL. 71 */ 72 73 HostInfo *find_host(char *hostname, int create) 74 { 75 HostInfo *hp; 76 HostInfo *spare_slot = NULL; 77 HostInfo *result = NULL; 78 int i; 79 80 for (i = 0, hp = status_info->hosts; i < status_info->noOfHosts; i++, hp++) 81 { 82 if (!strncasecmp(hostname, hp->hostname, SM_MAXSTRLEN)) 83 { 84 result = hp; 85 break; 86 } 87 if (!spare_slot && !hp->monList && !hp->notifyReqd) 88 spare_slot = hp; 89 } 90 91 /* Return if entry found, or if not asked to create one. */ 92 if (result || !create) return (result); 93 94 /* Now create an entry, using the spare slot if one was found or */ 95 /* adding to the end of the list otherwise, extending file if reqd */ 96 if (!spare_slot) 97 { 98 off_t desired_size; 99 spare_slot = &status_info->hosts[status_info->noOfHosts]; 100 desired_size = ((char*)spare_slot - (char*)status_info) + sizeof(HostInfo); 101 if (desired_size > status_file_len) 102 { 103 /* Extend file by writing 1 byte of junk at the desired end pos */ 104 lseek(status_fd, desired_size - 1, SEEK_SET); 105 i = write(status_fd, &i, 1); 106 if (i < 1) 107 { 108 syslog(LOG_ERR, "Unable to extend status file"); 109 return (NULL); 110 } 111 status_file_len = desired_size; 112 } 113 status_info->noOfHosts++; 114 } 115 116 /* Initialise the spare slot that has been found/created */ 117 /* Note that we do not msync(), since the caller is presumed to be */ 118 /* about to modify the entry further */ 119 memset(spare_slot, 0, sizeof(HostInfo)); 120 strncpy(spare_slot->hostname, hostname, SM_MAXSTRLEN); 121 return (spare_slot); 122 } 123 124 /* init_file -------------------------------------------------------------- */ 125 /* 126 Purpose: Open file, create if necessary, initialise it. 127 Returns: Nothing - exits on error 128 Notes: Called before process becomes daemon, hence logs to 129 stderr rather than syslog. 130 Opens the file, then mmap()s it for ease of access. 131 Also performs initial clean-up of the file, zeroing 132 monitor list pointers, setting the notifyReqd flag in 133 all hosts that had a monitor list, and incrementing 134 the state number to the next even value. 135 */ 136 137 void init_file(char *filename) 138 { 139 int new_file = FALSE; 140 char buf[HEADER_LEN]; 141 int i; 142 143 /* try to open existing file - if not present, create one */ 144 status_fd = open(filename, O_RDWR); 145 if ((status_fd < 0) && (errno == ENOENT)) 146 { 147 status_fd = open(filename, O_RDWR | O_CREAT, 0644); 148 new_file = TRUE; 149 } 150 if (status_fd < 0) 151 errx(1, "unable to open status file %s", filename); 152 153 /* File now open. mmap() it, with a generous size to allow for */ 154 /* later growth, where we will extend the file but not re-map it. */ 155 status_info = (FileLayout *) 156 mmap(NULL, 0x10000000, PROT_READ | PROT_WRITE, MAP_SHARED, status_fd, 0); 157 158 if (status_info == (FileLayout *) MAP_FAILED) 159 warn("unable to mmap() status file"); 160 161 status_file_len = lseek(status_fd, 0L, SEEK_END); 162 163 /* If the file was not newly created, validate the contents, and if */ 164 /* defective, re-create from scratch. */ 165 if (!new_file) 166 { 167 if ((status_file_len < HEADER_LEN) || (status_file_len 168 < (HEADER_LEN + sizeof(HostInfo) * status_info->noOfHosts)) ) 169 { 170 warnx("status file is corrupt"); 171 new_file = TRUE; 172 } 173 } 174 175 /* Initialisation of a new, empty file. */ 176 if (new_file) 177 { 178 memset(buf, 0, sizeof(buf)); 179 lseek(status_fd, 0L, SEEK_SET); 180 write(status_fd, buf, HEADER_LEN); 181 status_file_len = HEADER_LEN; 182 } 183 else 184 { 185 /* Clean-up of existing file - monitored hosts will have a pointer */ 186 /* to a list of clients, which refers to memory in the previous */ 187 /* incarnation of the program and so are meaningless now. These */ 188 /* pointers are zeroed and the fact that the host was previously */ 189 /* monitored is recorded by setting the notifyReqd flag, which will */ 190 /* in due course cause a SM_NOTIFY to be sent. */ 191 /* Note that if we crash twice in quick succession, some hosts may */ 192 /* already have notifyReqd set, where we didn't manage to notify */ 193 /* them before the second crash occurred. */ 194 for (i = 0; i < status_info->noOfHosts; i++) 195 { 196 HostInfo *this_host = &status_info->hosts[i]; 197 198 if (this_host->monList) 199 { 200 this_host->notifyReqd = TRUE; 201 this_host->monList = NULL; 202 } 203 } 204 /* Select the next higher even number for the state counter */ 205 status_info->ourState = (status_info->ourState + 2) & 0xfffffffe; 206 /*???????******/ status_info->ourState++; 207 } 208 } 209 210 /* xdr_stat_chge ----------------------------------------------------------- */ 211 /* 212 Purpose: XDR-encode structure of type stat_chge 213 Returns: TRUE if successful 214 Notes: This function is missing from librpcsvc, because the 215 sm_inter.x distributed by Sun omits the SM_NOTIFY 216 procedure used between co-operating statd's 217 */ 218 219 bool_t xdr_stat_chge(XDR *xdrs, stat_chge *objp) 220 { 221 if (!xdr_string(xdrs, &objp->mon_name, SM_MAXSTRLEN)) 222 { 223 return (FALSE); 224 } 225 if (!xdr_int(xdrs, &objp->state)) 226 { 227 return (FALSE); 228 } 229 return (TRUE); 230 } 231 232 233 /* notify_one_host --------------------------------------------------------- */ 234 /* 235 Purpose: Perform SM_NOTIFY procedure at specified host 236 Returns: TRUE if success, FALSE if failed. 237 */ 238 239 static int notify_one_host(char *hostname) 240 { 241 struct timeval timeout = { 20, 0 }; /* 20 secs timeout */ 242 CLIENT *cli; 243 char dummy; 244 stat_chge arg; 245 char our_hostname[SM_MAXSTRLEN+1]; 246 247 gethostname(our_hostname, sizeof(our_hostname)); 248 our_hostname[SM_MAXSTRLEN] = '\0'; 249 arg.mon_name = our_hostname; 250 arg.state = status_info->ourState; 251 252 if (debug) syslog (LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s", hostname, our_hostname); 253 254 cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp"); 255 if (!cli) 256 { 257 syslog(LOG_ERR, "Failed to contact host %s%s", hostname, 258 clnt_spcreateerror("")); 259 return (FALSE); 260 } 261 262 if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void, &dummy, timeout) 263 != RPC_SUCCESS) 264 { 265 syslog(LOG_ERR, "Failed to contact rpc.statd at host %s", hostname); 266 clnt_destroy(cli); 267 return (FALSE); 268 } 269 270 clnt_destroy(cli); 271 return (TRUE); 272 } 273 274 /* notify_hosts ------------------------------------------------------------ */ 275 /* 276 Purpose: Send SM_NOTIFY to all hosts marked as requiring it 277 Returns: Nothing, immediately - forks a process to do the work. 278 Notes: Does nothing if there are no monitored hosts. 279 Called after all the initialisation has been done - 280 logs to syslog. 281 */ 282 283 void notify_hosts(void) 284 { 285 int i; 286 int attempts; 287 int work_to_do = FALSE; 288 HostInfo *hp; 289 pid_t pid; 290 291 /* First check if there is in fact any work to do. */ 292 for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) 293 { 294 if (hp->notifyReqd) 295 { 296 work_to_do = TRUE; 297 break; 298 } 299 } 300 301 if (!work_to_do) return; /* No work found */ 302 303 pid = fork(); 304 if (pid == -1) 305 { 306 syslog(LOG_ERR, "Unable to fork notify process - %s", strerror(errno)); 307 return; 308 } 309 if (pid) return; 310 311 /* Here in the child process. We continue until all the hosts marked */ 312 /* as requiring notification have been duly notified. */ 313 /* If one of the initial attempts fails, we sleep for a while and */ 314 /* have another go. This is necessary because when we have crashed, */ 315 /* (eg. a power outage) it is quite possible that we won't be able to */ 316 /* contact all monitored hosts immediately on restart, either because */ 317 /* they crashed too and take longer to come up (in which case the */ 318 /* notification isn't really required), or more importantly if some */ 319 /* router etc. needed to reach the monitored host has not come back */ 320 /* up yet. In this case, we will be a bit late in re-establishing */ 321 /* locks (after the grace period) but that is the best we can do. */ 322 /* We try 10 times at 5 sec intervals, 10 more times at 1 minute */ 323 /* intervals, then 24 more times at hourly intervals, finally */ 324 /* giving up altogether if the host hasn't come back to life after */ 325 /* 24 hours. */ 326 327 for (attempts = 0; attempts < 44; attempts++) 328 { 329 work_to_do = FALSE; /* Unless anything fails */ 330 for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) 331 { 332 if (hp->notifyReqd) 333 { 334 if (notify_one_host(hp->hostname)) 335 { 336 hp->notifyReqd = FALSE; 337 sync_file(); 338 } 339 else work_to_do = TRUE; 340 } 341 } 342 if (!work_to_do) break; 343 if (attempts < 10) sleep(5); 344 else if (attempts < 20) sleep(60); 345 else sleep(60*60); 346 } 347 exit(0); 348 } 349 350 351