file.c revision 171816
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 * 3274499Salfred * $FreeBSD: head/usr.sbin/rpc.statd/file.c 171816 2007-08-13 15:04:39Z truckman $ 3374499Salfred * 3414125Speter */ 3514125Speter 3630376Scharnier#include <err.h> 3730376Scharnier#include <errno.h> 3830376Scharnier#include <fcntl.h> 3914125Speter#include <stdio.h> 4030376Scharnier#include <string.h> 4114125Speter#include <unistd.h> 4214125Speter#include <sys/types.h> 4314125Speter#include <sys/mman.h> /* For mmap() */ 4414125Speter#include <rpc/rpc.h> 4514125Speter#include <syslog.h> 4699786Salfred#include <stdlib.h> 4714125Speter 4814125Speter#include "statd.h" 4914125Speter 5014125SpeterFileLayout *status_info; /* Pointer to the mmap()ed status file */ 5114125Speterstatic int status_fd; /* File descriptor for the open file */ 5214125Speterstatic off_t status_file_len; /* Current on-disc length of file */ 5314125Speter 5414125Speter/* sync_file --------------------------------------------------------------- */ 5514125Speter/* 5614125Speter Purpose: Packaged call of msync() to flush changes to mmap()ed file 5714125Speter Returns: Nothing. Errors to syslog. 5814125Speter*/ 5914125Speter 6014125Spetervoid sync_file(void) 6114125Speter{ 6214125Speter if (msync((void *)status_info, 0, 0) < 0) 6314125Speter { 6414125Speter syslog(LOG_ERR, "msync() failed: %s", strerror(errno)); 6514125Speter } 6614125Speter} 6714125Speter 6814125Speter/* find_host -------------------------------------------------------------- */ 6914125Speter/* 7014125Speter Purpose: Find the entry in the status file for a given host 7114125Speter Returns: Pointer to that entry in the mmap() region, or NULL. 7214125Speter Notes: Also creates entries if requested. 7314125Speter Failure to create also returns NULL. 7414125Speter*/ 7514125Speter 7614125SpeterHostInfo *find_host(char *hostname, int create) 7714125Speter{ 7814125Speter HostInfo *hp; 7914125Speter HostInfo *spare_slot = NULL; 8014125Speter HostInfo *result = NULL; 8114125Speter int i; 8214125Speter 8314125Speter for (i = 0, hp = status_info->hosts; i < status_info->noOfHosts; i++, hp++) 8414125Speter { 8514125Speter if (!strncasecmp(hostname, hp->hostname, SM_MAXSTRLEN)) 8614125Speter { 8714125Speter result = hp; 8814125Speter break; 8914125Speter } 9014125Speter if (!spare_slot && !hp->monList && !hp->notifyReqd) 9114125Speter spare_slot = hp; 9214125Speter } 9314125Speter 9414125Speter /* Return if entry found, or if not asked to create one. */ 9514125Speter if (result || !create) return (result); 9614125Speter 9714125Speter /* Now create an entry, using the spare slot if one was found or */ 9814125Speter /* adding to the end of the list otherwise, extending file if reqd */ 9914125Speter if (!spare_slot) 10014125Speter { 10114125Speter off_t desired_size; 10214125Speter spare_slot = &status_info->hosts[status_info->noOfHosts]; 10314125Speter desired_size = ((char*)spare_slot - (char*)status_info) + sizeof(HostInfo); 10414125Speter if (desired_size > status_file_len) 10514125Speter { 10614125Speter /* Extend file by writing 1 byte of junk at the desired end pos */ 10714125Speter lseek(status_fd, desired_size - 1, SEEK_SET); 10814125Speter i = write(status_fd, &i, 1); 10914125Speter if (i < 1) 11014125Speter { 11114125Speter syslog(LOG_ERR, "Unable to extend status file"); 11214125Speter return (NULL); 11314125Speter } 11414125Speter status_file_len = desired_size; 11514125Speter } 11614125Speter status_info->noOfHosts++; 11714125Speter } 11814125Speter 11914125Speter /* Initialise the spare slot that has been found/created */ 12014125Speter /* Note that we do not msync(), since the caller is presumed to be */ 12114125Speter /* about to modify the entry further */ 12214125Speter memset(spare_slot, 0, sizeof(HostInfo)); 12314125Speter strncpy(spare_slot->hostname, hostname, SM_MAXSTRLEN); 12414125Speter return (spare_slot); 12514125Speter} 12614125Speter 12714125Speter/* init_file -------------------------------------------------------------- */ 12814125Speter/* 12914125Speter Purpose: Open file, create if necessary, initialise it. 13014125Speter Returns: Nothing - exits on error 13114125Speter Notes: Called before process becomes daemon, hence logs to 13214125Speter stderr rather than syslog. 13314125Speter Opens the file, then mmap()s it for ease of access. 13414125Speter Also performs initial clean-up of the file, zeroing 13514125Speter monitor list pointers, setting the notifyReqd flag in 13614125Speter all hosts that had a monitor list, and incrementing 13714125Speter the state number to the next even value. 13814125Speter*/ 13914125Speter 14099798Salfredvoid init_file(const char *filename) 14114125Speter{ 14214125Speter int new_file = FALSE; 14314125Speter char buf[HEADER_LEN]; 14414125Speter int i; 14514125Speter 14614125Speter /* try to open existing file - if not present, create one */ 14714125Speter status_fd = open(filename, O_RDWR); 14814125Speter if ((status_fd < 0) && (errno == ENOENT)) 14914125Speter { 15014125Speter status_fd = open(filename, O_RDWR | O_CREAT, 0644); 15114125Speter new_file = TRUE; 15214125Speter } 15314125Speter if (status_fd < 0) 15430376Scharnier errx(1, "unable to open status file %s", filename); 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 16121786Salex if (status_info == (FileLayout *) MAP_FAILED) 162171816Struckman err(1, "unable to mmap() status file"); 16314125Speter 16414125Speter status_file_len = lseek(status_fd, 0L, SEEK_END); 16514125Speter 16614125Speter /* If the file was not newly created, validate the contents, and if */ 16714125Speter /* defective, re-create from scratch. */ 16814125Speter if (!new_file) 16914125Speter { 17014125Speter if ((status_file_len < HEADER_LEN) || (status_file_len 17114125Speter < (HEADER_LEN + sizeof(HostInfo) * status_info->noOfHosts)) ) 17214125Speter { 17330376Scharnier warnx("status file is corrupt"); 17414125Speter new_file = TRUE; 17514125Speter } 17614125Speter } 17714125Speter 17814125Speter /* Initialisation of a new, empty file. */ 17914125Speter if (new_file) 18014125Speter { 18114125Speter memset(buf, 0, sizeof(buf)); 18214125Speter lseek(status_fd, 0L, SEEK_SET); 18314125Speter write(status_fd, buf, HEADER_LEN); 18414125Speter status_file_len = HEADER_LEN; 18514125Speter } 18614125Speter else 18714125Speter { 18814125Speter /* Clean-up of existing file - monitored hosts will have a pointer */ 18914125Speter /* to a list of clients, which refers to memory in the previous */ 19014125Speter /* incarnation of the program and so are meaningless now. These */ 19114125Speter /* pointers are zeroed and the fact that the host was previously */ 19214125Speter /* monitored is recorded by setting the notifyReqd flag, which will */ 19314125Speter /* in due course cause a SM_NOTIFY to be sent. */ 19414125Speter /* Note that if we crash twice in quick succession, some hosts may */ 19514125Speter /* already have notifyReqd set, where we didn't manage to notify */ 19614125Speter /* them before the second crash occurred. */ 19714125Speter for (i = 0; i < status_info->noOfHosts; i++) 19814125Speter { 19914125Speter HostInfo *this_host = &status_info->hosts[i]; 20014125Speter 20114125Speter if (this_host->monList) 20214125Speter { 20314125Speter this_host->notifyReqd = TRUE; 20414125Speter this_host->monList = NULL; 20514125Speter } 20614125Speter } 20714125Speter /* Select the next higher even number for the state counter */ 20814125Speter status_info->ourState = (status_info->ourState + 2) & 0xfffffffe; 20914125Speter/*???????******/ status_info->ourState++; 21014125Speter } 21114125Speter} 21214125Speter 21314125Speter/* notify_one_host --------------------------------------------------------- */ 21414125Speter/* 21514125Speter Purpose: Perform SM_NOTIFY procedure at specified host 21614125Speter Returns: TRUE if success, FALSE if failed. 21714125Speter*/ 21814125Speter 21914125Speterstatic int notify_one_host(char *hostname) 22014125Speter{ 22114125Speter struct timeval timeout = { 20, 0 }; /* 20 secs timeout */ 22214125Speter CLIENT *cli; 22314125Speter char dummy; 22414125Speter stat_chge arg; 22514125Speter char our_hostname[SM_MAXSTRLEN+1]; 22614125Speter 22714125Speter gethostname(our_hostname, sizeof(our_hostname)); 22814125Speter our_hostname[SM_MAXSTRLEN] = '\0'; 22914125Speter arg.mon_name = our_hostname; 23014125Speter arg.state = status_info->ourState; 23114125Speter 23214125Speter if (debug) syslog (LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s", hostname, our_hostname); 23314125Speter 23414125Speter cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp"); 23514125Speter if (!cli) 23614125Speter { 23714125Speter syslog(LOG_ERR, "Failed to contact host %s%s", hostname, 23814125Speter clnt_spcreateerror("")); 23914125Speter return (FALSE); 24014125Speter } 24114125Speter 242121560Speter if (clnt_call(cli, SM_NOTIFY, (xdrproc_t)xdr_stat_chge, &arg, 243121560Speter (xdrproc_t)xdr_void, &dummy, timeout) 24414125Speter != RPC_SUCCESS) 24514125Speter { 24614125Speter syslog(LOG_ERR, "Failed to contact rpc.statd at host %s", hostname); 24714125Speter clnt_destroy(cli); 24814125Speter return (FALSE); 24914125Speter } 25014125Speter 25114125Speter clnt_destroy(cli); 25214125Speter return (TRUE); 25314125Speter} 25414125Speter 25514125Speter/* notify_hosts ------------------------------------------------------------ */ 25614125Speter/* 25714125Speter Purpose: Send SM_NOTIFY to all hosts marked as requiring it 25814125Speter Returns: Nothing, immediately - forks a process to do the work. 25914125Speter Notes: Does nothing if there are no monitored hosts. 26014125Speter Called after all the initialisation has been done - 26114125Speter logs to syslog. 26214125Speter*/ 26314125Speter 26414125Spetervoid notify_hosts(void) 26514125Speter{ 26614125Speter int i; 26714125Speter int attempts; 26814125Speter int work_to_do = FALSE; 26914125Speter HostInfo *hp; 27014125Speter pid_t pid; 27114125Speter 27214125Speter /* First check if there is in fact any work to do. */ 27314125Speter for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) 27414125Speter { 27514125Speter if (hp->notifyReqd) 27614125Speter { 27714125Speter work_to_do = TRUE; 27814125Speter break; 27914125Speter } 28014125Speter } 28114125Speter 28214125Speter if (!work_to_do) return; /* No work found */ 28314125Speter 28414125Speter pid = fork(); 28514125Speter if (pid == -1) 28614125Speter { 28714125Speter syslog(LOG_ERR, "Unable to fork notify process - %s", strerror(errno)); 28814125Speter return; 28914125Speter } 29014125Speter if (pid) return; 29114125Speter 29214125Speter /* Here in the child process. We continue until all the hosts marked */ 29314125Speter /* as requiring notification have been duly notified. */ 29414125Speter /* If one of the initial attempts fails, we sleep for a while and */ 29514125Speter /* have another go. This is necessary because when we have crashed, */ 29614125Speter /* (eg. a power outage) it is quite possible that we won't be able to */ 29714125Speter /* contact all monitored hosts immediately on restart, either because */ 29814125Speter /* they crashed too and take longer to come up (in which case the */ 29914125Speter /* notification isn't really required), or more importantly if some */ 30014125Speter /* router etc. needed to reach the monitored host has not come back */ 30114125Speter /* up yet. In this case, we will be a bit late in re-establishing */ 30214125Speter /* locks (after the grace period) but that is the best we can do. */ 30314125Speter /* We try 10 times at 5 sec intervals, 10 more times at 1 minute */ 30414125Speter /* intervals, then 24 more times at hourly intervals, finally */ 30514125Speter /* giving up altogether if the host hasn't come back to life after */ 30614125Speter /* 24 hours. */ 30714125Speter 30814125Speter for (attempts = 0; attempts < 44; attempts++) 30914125Speter { 31014125Speter work_to_do = FALSE; /* Unless anything fails */ 31114125Speter for (i = status_info->noOfHosts, hp = status_info->hosts; i ; i--, hp++) 31214125Speter { 31314125Speter if (hp->notifyReqd) 31414125Speter { 31514125Speter if (notify_one_host(hp->hostname)) 31614125Speter { 31714125Speter hp->notifyReqd = FALSE; 31814125Speter sync_file(); 31914125Speter } 32014125Speter else work_to_do = TRUE; 32114125Speter } 32214125Speter } 32314125Speter if (!work_to_do) break; 32414125Speter if (attempts < 10) sleep(5); 32514125Speter else if (attempts < 20) sleep(60); 32614125Speter else sleep(60*60); 32714125Speter } 32814125Speter exit(0); 32914125Speter} 33014125Speter 33114125Speter 332