1/* 2 * Copyright (c) 2002-2010 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23/* 24 * Copyright (c) 1995 25 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 1. Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 3. All advertising materials mentioning features or use of this software 36 * must display the following acknowledgement: 37 * This product includes software developed for the FreeBSD project 38 * 4. Neither the name of the author nor the names of any co-contributors 39 * may be used to endorse or promote products derived from this software 40 * without specific prior written permission. 41 * 42 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND 43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * 54 * $FreeBSD$ 55 */ 56 57 58 59#include "sm_inter.h" 60 61/* ------------------------------------------------------------------------- */ 62/* 63 Data structures for recording monitored hosts 64 65 The information held by the status monitor comprises a list of hosts 66 that we have been asked to monitor, and, associated with each monitored 67 host, one or more clients to be called back if the monitored host crashes. 68 69 The list of monitored hosts must be retained over a crash, so that upon 70 re-boot we can call the SM_NOTIFY procedure in all those hosts so as to 71 cause them to start recovery processing. On the other hand, the client 72 call-backs are not required to be preserved: they are assumed (in the 73 protocol design) to be local processes which will have crashed when 74 we did, and so are discarded on restart. 75 76 We handle this by keeping the list of monitored hosts in a file 77 (/var/db/statd.status) which is mmap()ed and whose format consists 78 of the FileHeader followed by a packed list of HostInfo structures. 79 For each currently-monitored host, a MonitoredHost structure associates 80 the in-file HostInfo entry with a list of client notification callbacks. 81 The MonitoredHost and Notify structures are held in normal memory and so 82 will be lost on restart. 83 84 All values stored in network byte order. 85*/ 86 87typedef struct Notify_s { 88 struct Notify_s *n_next; 89 int32_t n_prog; /* RPC program number to call */ 90 int32_t n_vers; /* version number */ 91 int32_t n_proc; /* procedure number */ 92 uint8_t n_data[16]; /* opaque data from caller */ 93 char n_host[1]; /* host to notify */ 94} Notify; 95 96typedef struct MonitoredHost_s { 97 off_t mh_hostinfo_offset;/* offset of HostInfo struct in file */ 98 char *mh_name; /* alternate in-memory name pointer */ 99 Notify *mh_notify_list; /* list of notifications */ 100} MonitoredHost; 101 102/* file layout: a fixed-size header followed by packed HostInfo entries */ 103 104typedef struct { 105 uint32_t fh_state; /* State number as defined in statd protocol */ 106 uint32_t fh_reccnt; /* # of HostInfo records */ 107 uint32_t fh_version; /* version # of this file layout */ 108 char fh_reserved[244]; /* Reserved for future use */ 109} FileHeader; 110#define STATUS_DB_VERSION_CONVERTED 0xffffffff 111#define STATUS_DB_VERSION 0x4e534d31 /* "NSM1" */ 112 113#define NAMEINCR 64 /* must be power of 2 */ 114 115#define AOK (void *) // assert alignment is OK 116 117typedef struct { 118 uint16_t hi_len; /* length of this HostInfo record */ 119 uint16_t hi_monitored; /* host is being monitored */ 120 uint16_t hi_notify; /* host needs to be notified */ 121 uint16_t hi_namelen; /* length of host name string */ 122 char hi_name[NAMEINCR]; /* name of monitored host */ 123 /* NULL terminated string allocated in NAMEINCR-byte increments */ 124} HostInfo; 125#define RNDUP_NAMELEN(NL) (((NL) + 1 + (NAMEINCR-1)) & (~(NAMEINCR-1))) 126#define HOSTINFO(OFF) ((HostInfo*) AOK ((char*)status_info + (OFF))) 127 128/* Original "version 0" file layout */ 129typedef struct { 130 char hostname[SM_MAXSTRLEN + 1];/* Name of monitored host */ 131 int notifyReqd; /* TRUE if we've crashed and not yet */ 132 /* informed the monitored host */ 133 uint32_t monList; /* List of clients to inform if we */ 134 /* hear that the monitored host has */ 135 /* crashed, NULL if no longer monitored */ 136} HostInfo0; 137typedef struct { 138 uint32_t ourState; /* State number as defined in statd protocol */ 139 uint32_t noOfHosts; /* Number of elements in hosts[] */ 140 uint32_t version; /* version # of this layout */ 141 char reserved[244]; /* Reserved for future use */ 142 HostInfo0 hosts[1]; /* vector of monitored hosts */ 143} FileLayout0; 144 145/* ------------------------------------------------------------------------- */ 146 147#define _PATH_STATD_DATABASE "/var/db/statd.status" 148#define _PATH_STATD_PID "/var/run/statd.pid" 149#define _PATH_STATD_NOTIFY_PID "/var/run/statd.notify.pid" 150#define _PATH_NFS_CONF "/etc/nfs.conf" 151#define _PATH_LAUNCHCTL "/bin/launchctl" 152#define _PATH_STATD_NOTIFY_PLIST "/System/Library/LaunchDaemons/com.apple.statd.notify.plist" 153#define _STATD_NOTIFY_SERVICE_LABEL "com.apple.statd.notify" 154 155/* ------------------------------------------------------------------------- */ 156 157#define LIST_MODE_ONCE 1 /* -l */ 158#define LIST_MODE_WATCH 2 /* -L */ 159 160/* 161 * structure holding statd config values 162 */ 163struct nfs_conf_statd { 164 int port; 165 int send_using_tcp; 166 int simu_crash_allowed; 167 int tcp; 168 int udp; 169 int verbose; 170}; 171 172/* 173 * verbose log levels: 174 * 0 LOG_WARNING and higher 175 * 1 LOG_NOTICE and higher 176 * 2 LOG_INFO and higher 177 * 3 LOG_DEBUG and higher 178 * 4+ more LOG_DEBUG 179 */ 180#define LOG_LEVEL (LOG_WARNING + MIN(config.verbose, 3)) 181#define DEBUG(L, ...) \ 182 do { \ 183 if ((L) <= (config.verbose - 2)) \ 184 SYSLOG(LOG_DEBUG, __VA_ARGS__); \ 185 } while (0) 186#define log SYSLOG 187void SYSLOG(int, const char *,...); 188extern int log_to_stderr; 189 190/* Global variables */ 191 192extern FileHeader *status_info; /* The mmap()ed status file */ 193extern void *mhroot; /* root of the MonitoredHost tree */ 194 195extern int statd_server; /* are we the statd server (not notify, list...) */ 196extern int notify_only; /* just send SM_NOTIFY messages */ 197extern int list_only; /* just list status database entries */ 198extern struct pidfh *pfh; /* pid file */ 199extern struct nfs_conf_statd config; /* config settings */ 200 201/* Function prototypes */ 202 203extern int mhcmp(const void * /* key1 */ , const void * /* key2 */ ); 204extern MonitoredHost *find_host(char * /* hostname */ , int /* create */ ); 205extern int init_file(const char * /* filename */ ); 206extern int notify_hosts(void); 207extern int do_unnotify_host(const char * /* hostname */ ); 208extern int list_hosts(int /* mode */ ); 209extern void sync_file(void); 210extern int getaddresslist(const char * /* hostname */, struct addrinfo ** /* list */); 211extern int sm_check_hostname(struct svc_req * req, char *arg); 212pid_t get_statd_pid(void); 213pid_t get_statd_notify_pid(void); 214int statd_notify_load(void); 215int statd_notify_is_loaded(void); 216int statd_notify_start(void); 217 218#ifndef __unused 219#define __unused 220#endif 221