file.c revision 14125
114125Speter/* 214125Speter * Copyright (c) 1995 314125Speter * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 414125Speter * 514125Speter * Redistribution and use in source and binary forms, with or without 614125Speter * modification, are permitted provided that the following conditions 714125Speter * are met: 814125Speter * 1. Redistributions of source code must retain the above copyright 914125Speter * notice, this list of conditions and the following disclaimer. 1014125Speter * 2. Redistributions in binary form must reproduce the above copyright 1114125Speter * notice, this list of conditions and the following disclaimer in the 1214125Speter * documentation and/or other materials provided with the distribution. 1314125Speter * 3. All advertising materials mentioning features or use of this software 1414125Speter * must display the following acknowledgement: 1514125Speter * This product includes software developed for the FreeBSD project 1614125Speter * 4. Neither the name of the author nor the names of any co-contributors 1714125Speter * may be used to endorse or promote products derived from this software 1814125Speter * without specific prior written permission. 1914125Speter * 2014125Speter * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND 2114125Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2214125Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2314125Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2414125Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2514125Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2614125Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2714125Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2814125Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2914125Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3014125Speter * SUCH DAMAGE. 3114125Speter * 3214125Speter */ 3314125Speter 3414125Speter#include <stdio.h> 3514125Speter#include <unistd.h> 3614125Speter#include <fcntl.h> 3714125Speter#include <errno.h> 3814125Speter#include <string.h> 3914125Speter#include <sys/types.h> 4014125Speter#include <sys/mman.h> /* For mmap() */ 4114125Speter#include <rpc/rpc.h> 4214125Speter#include <syslog.h> 4314125Speter 4414125Speter#include "statd.h" 4514125Speter 4614125SpeterFileLayout *status_info; /* Pointer to the mmap()ed status file */ 4714125Speterstatic int status_fd; /* File descriptor for the open file */ 4814125Speterstatic off_t status_file_len; /* Current on-disc length of file */ 4914125Speter 5014125Speter/* sync_file --------------------------------------------------------------- */ 5114125Speter/* 5214125Speter Purpose: Packaged call of msync() to flush changes to mmap()ed file 5314125Speter Returns: Nothing. Errors to syslog. 5414125Speter*/ 5514125Speter 5614125Spetervoid sync_file(void) 5714125Speter{ 5814125Speter if (msync((void *)status_info, 0, 0) < 0) 5914125Speter { 6014125Speter syslog(LOG_ERR, "msync() failed: %s", strerror(errno)); 6114125Speter } 6214125Speter} 6314125Speter 6414125Speter/* find_host -------------------------------------------------------------- */ 6514125Speter/* 6614125Speter Purpose: Find the entry in the status file for a given host 6714125Speter Returns: Pointer to that entry in the mmap() region, or NULL. 6814125Speter Notes: Also creates entries if requested. 6914125Speter Failure to create also returns NULL. 7014125Speter*/ 7114125Speter 7214125SpeterHostInfo *find_host(char *hostname, int create) 7314125Speter{ 7414125Speter HostInfo *hp; 7514125Speter HostInfo *spare_slot = NULL; 7614125Speter HostInfo *result = NULL; 7714125Speter int i; 7814125Speter 7914125Speter for (i = 0, hp = status_info->hosts; i < status_info->noOfHosts; i++, hp++) 8014125Speter { 8114125Speter if (!strncasecmp(hostname, hp->hostname, SM_MAXSTRLEN)) 8214125Speter { 8314125Speter result = hp; 8414125Speter break; 8514125Speter } 8614125Speter if (!spare_slot && !hp->monList && !hp->notifyReqd) 8714125Speter spare_slot = hp; 8814125Speter } 8914125Speter 9014125Speter /* Return if entry found, or if not asked to create one. */ 9114125Speter if (result || !create) return (result); 9214125Speter 9314125Speter /* Now create an entry, using the spare slot if one was found or */ 9414125Speter /* adding to the end of the list otherwise, extending file if reqd */ 9514125Speter if (!spare_slot) 9614125Speter { 9714125Speter off_t desired_size; 9814125Speter spare_slot = &status_info->hosts[status_info->noOfHosts]; 9914125Speter desired_size = ((char*)spare_slot - (char*)status_info) + sizeof(HostInfo); 10014125Speter if (desired_size > status_file_len) 10114125Speter { 10214125Speter /* Extend file by writing 1 byte of junk at the desired end pos */ 10314125Speter lseek(status_fd, desired_size - 1, SEEK_SET); 10414125Speter i = write(status_fd, &i, 1); 10514125Speter if (i < 1) 10614125Speter { 10714125Speter syslog(LOG_ERR, "Unable to extend status file"); 10814125Speter return (NULL); 10914125Speter } 11014125Speter status_file_len = desired_size; 11114125Speter } 11214125Speter status_info->noOfHosts++; 11314125Speter } 11414125Speter 11514125Speter /* Initialise the spare slot that has been found/created */ 11614125Speter /* Note that we do not msync(), since the caller is presumed to be */ 11714125Speter /* about to modify the entry further */ 11814125Speter memset(spare_slot, 0, sizeof(HostInfo)); 11914125Speter strncpy(spare_slot->hostname, hostname, SM_MAXSTRLEN); 12014125Speter return (spare_slot); 12114125Speter} 12214125Speter 12314125Speter/* init_file -------------------------------------------------------------- */ 12414125Speter/* 12514125Speter Purpose: Open file, create if necessary, initialise it. 12614125Speter Returns: Nothing - exits on error 12714125Speter Notes: Called before process becomes daemon, hence logs to 12814125Speter stderr rather than syslog. 12914125Speter Opens the file, then mmap()s it for ease of access. 13014125Speter Also performs initial clean-up of the file, zeroing 13114125Speter monitor list pointers, setting the notifyReqd flag in 13214125Speter all hosts that had a monitor list, and incrementing 13314125Speter the state number to the next even value. 13414125Speter*/ 13514125Speter 13614125Spetervoid init_file(char *filename) 13714125Speter{ 13814125Speter int new_file = FALSE; 13914125Speter char buf[HEADER_LEN]; 14014125Speter int i; 14114125Speter 14214125Speter /* try to open existing file - if not present, create one */ 14314125Speter status_fd = open(filename, O_RDWR); 14414125Speter if ((status_fd < 0) && (errno == ENOENT)) 14514125Speter { 14614125Speter status_fd = open(filename, O_RDWR | O_CREAT, 0644); 14714125Speter new_file = TRUE; 14814125Speter } 14914125Speter if (status_fd < 0) 15014125Speter { 15114125Speter perror("rpc.statd"); 15214125Speter fprintf(stderr, "Unable to open status file %s\n", filename); 15314125Speter exit(1); 15414125Speter } 15514125Speter 15614125Speter /* File now open. mmap() it, with a generous size to allow for */ 15714125Speter /* later growth, where we will extend the file but not re-map it. */ 15814125Speter status_info = (FileLayout *) 15914125Speter mmap(NULL, 0x10000000, PROT_READ | PROT_WRITE, MAP_SHARED, status_fd, 0); 16014125Speter 16114125Speter if (status_info == (FileLayout *) -1) 16214125Speter { 16314125Speter perror("rpc.statd"); 16414125Speter fprintf(stderr, "Unable to mmap() status file\n"); 16514125Speter } 16614125Speter 16714125Speter status_file_len = lseek(status_fd, 0L, SEEK_END); 16814125Speter 16914125Speter /* If the file was not newly created, validate the contents, and if */ 17014125Speter /* defective, re-create from scratch. */ 17114125Speter if (!new_file) 17214125Speter { 17314125Speter if ((status_file_len < HEADER_LEN) || (status_file_len 17414125Speter < (HEADER_LEN + sizeof(HostInfo) * status_info->noOfHosts)) ) 17514125Speter { 17614125Speter fprintf(stderr, "rpc.statd: status file is corrupt\n"); 17714125Speter new_file = TRUE; 17814125Speter } 17914125Speter } 18014125Speter 18114125Speter /* Initialisation of a new, empty file. */ 18214125Speter if (new_file) 18314125Speter { 18414125Speter memset(buf, 0, sizeof(buf)); 18514125Speter lseek(status_fd, 0L, SEEK_SET); 18614125Speter write(status_fd, buf, HEADER_LEN); 18714125Speter status_file_len = HEADER_LEN; 18814125Speter } 18914125Speter else 19014125Speter { 19114125Speter /* Clean-up of existing file - monitored hosts will have a pointer */ 19214125Speter /* to a list of clients, which refers to memory in the previous */ 19314125Speter /* incarnation of the program and so are meaningless now. These */ 19414125Speter /* pointers are zeroed and the fact that the host was previously */ 19514125Speter /* monitored is recorded by setting the notifyReqd flag, which will */ 19614125Speter /* in due course cause a SM_NOTIFY to be sent. */ 19714125Speter /* Note that if we crash twice in quick succession, some hosts may */ 19814125Speter /* already have notifyReqd set, where we didn't manage to notify */ 19914125Speter /* them before the second crash occurred. */ 20014125Speter for (i = 0; i < status_info->noOfHosts; i++) 20114125Speter { 20214125Speter HostInfo *this_host = &status_info->hosts[i]; 20314125Speter 20414125Speter if (this_host->monList) 20514125Speter { 20614125Speter this_host->notifyReqd = TRUE; 20714125Speter this_host->monList = NULL; 20814125Speter } 20914125Speter } 21014125Speter /* Select the next higher even number for the state counter */ 21114125Speter status_info->ourState = (status_info->ourState + 2) & 0xfffffffe; 21214125Speter/*???????******/ status_info->ourState++; 21314125Speter } 21414125Speter} 21514125Speter 21614125Speter/* xdr_stat_chge ----------------------------------------------------------- */ 21714125Speter/* 21814125Speter Purpose: XDR-encode structure of type stat_chge 21914125Speter Returns: TRUE if successful 22014125Speter Notes: This function is missing from librpcsvc, because the 22114125Speter sm_inter.x distributed by Sun omits the SM_NOTIFY 22214125Speter procedure used between co-operating statd's 22314125Speter*/ 22414125Speter 22514125Speterbool_t xdr_stat_chge(XDR *xdrs, stat_chge *objp) 22614125Speter{ 22714125Speter if (!xdr_string(xdrs, &objp->mon_name, SM_MAXSTRLEN)) 22814125Speter { 22914125Speter return (FALSE); 23014125Speter } 23114125Speter if (!xdr_int(xdrs, &objp->state)) 23214125Speter { 23314125Speter return (FALSE); 23414125Speter } 23514125Speter return (TRUE); 23614125Speter} 23714125Speter 23814125Speter 23914125Speter/* notify_one_host --------------------------------------------------------- */ 24014125Speter/* 24114125Speter Purpose: Perform SM_NOTIFY procedure at specified host 24214125Speter Returns: TRUE if success, FALSE if failed. 24314125Speter*/ 24414125Speter 24514125Speterstatic int notify_one_host(char *hostname) 24614125Speter{ 24714125Speter struct timeval timeout = { 20, 0 }; /* 20 secs timeout */ 24814125Speter CLIENT *cli; 24914125Speter char dummy; 25014125Speter stat_chge arg; 25114125Speter char our_hostname[SM_MAXSTRLEN+1]; 25214125Speter 25314125Speter gethostname(our_hostname, sizeof(our_hostname)); 25414125Speter our_hostname[SM_MAXSTRLEN] = '\0'; 25514125Speter arg.mon_name = our_hostname; 25614125Speter arg.state = status_info->ourState; 25714125Speter 25814125Speter if (debug) syslog (LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s", hostname, our_hostname); 25914125Speter 26014125Speter cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp"); 26114125Speter if (!cli) 26214125Speter { 26314125Speter syslog(LOG_ERR, "Failed to contact host %s%s", hostname, 26414125Speter clnt_spcreateerror("")); 26514125Speter return (FALSE); 26614125Speter } 26714125Speter 26814125Speter if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void, &dummy, timeout) 26914125Speter != RPC_SUCCESS) 27014125Speter { 27114125Speter syslog(LOG_ERR, "Failed to contact rpc.statd at host %s", hostname); 27214125Speter clnt_destroy(cli); 27314125Speter return (FALSE); 27414125Speter } 27514125Speter 27614125Speter clnt_destroy(cli); 27714125Speter return (TRUE); 27814125Speter} 27914125Speter 28014125Speter/* notify_hosts ------------------------------------------------------------ */ 28114125Speter/* 28214125Speter Purpose: Send SM_NOTIFY to all hosts marked as requiring it 28314125Speter Returns: Nothing, immediately - forks a process to do the work. 28414125Speter Notes: Does nothing if there are no monitored hosts. 28514125Speter Called after all the initialisation has been done - 28614125Speter logs to syslog. 28714125Speter*/ 28814125Speter 28914125Spetervoid notify_hosts(void) 29014125Speter{ 29114125Speter int i; 29214125Speter int attempts; 29314125Speter int work_to_do = FALSE; 29414125Speter HostInfo *hp; 29514125Speter pid_t pid; 29614125Speter 29714125Speter /* First check if there is in fact any work to do. */ 29814125Speter for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) 29914125Speter { 30014125Speter if (hp->notifyReqd) 30114125Speter { 30214125Speter work_to_do = TRUE; 30314125Speter break; 30414125Speter } 30514125Speter } 30614125Speter 30714125Speter if (!work_to_do) return; /* No work found */ 30814125Speter 30914125Speter pid = fork(); 31014125Speter if (pid == -1) 31114125Speter { 31214125Speter syslog(LOG_ERR, "Unable to fork notify process - %s", strerror(errno)); 31314125Speter return; 31414125Speter } 31514125Speter if (pid) return; 31614125Speter 31714125Speter /* Here in the child process. We continue until all the hosts marked */ 31814125Speter /* as requiring notification have been duly notified. */ 31914125Speter /* If one of the initial attempts fails, we sleep for a while and */ 32014125Speter /* have another go. This is necessary because when we have crashed, */ 32114125Speter /* (eg. a power outage) it is quite possible that we won't be able to */ 32214125Speter /* contact all monitored hosts immediately on restart, either because */ 32314125Speter /* they crashed too and take longer to come up (in which case the */ 32414125Speter /* notification isn't really required), or more importantly if some */ 32514125Speter /* router etc. needed to reach the monitored host has not come back */ 32614125Speter /* up yet. In this case, we will be a bit late in re-establishing */ 32714125Speter /* locks (after the grace period) but that is the best we can do. */ 32814125Speter /* We try 10 times at 5 sec intervals, 10 more times at 1 minute */ 32914125Speter /* intervals, then 24 more times at hourly intervals, finally */ 33014125Speter /* giving up altogether if the host hasn't come back to life after */ 33114125Speter /* 24 hours. */ 33214125Speter 33314125Speter for (attempts = 0; attempts < 44; attempts++) 33414125Speter { 33514125Speter work_to_do = FALSE; /* Unless anything fails */ 33614125Speter for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) 33714125Speter { 33814125Speter if (hp->notifyReqd) 33914125Speter { 34014125Speter if (notify_one_host(hp->hostname)) 34114125Speter { 34214125Speter hp->notifyReqd = FALSE; 34314125Speter sync_file(); 34414125Speter } 34514125Speter else work_to_do = TRUE; 34614125Speter } 34714125Speter } 34814125Speter if (!work_to_do) break; 34914125Speter if (attempts < 10) sleep(5); 35014125Speter else if (attempts < 20) sleep(60); 35114125Speter else sleep(60*60); 35214125Speter } 35314125Speter exit(0); 35414125Speter} 35514125Speter 35614125Speter 357