1 /* $NetBSD: stat_proc.c,v 1.1 1997/03/10 06:28:30 scottr Exp $ */ 2 3 /* 4 * Copyright (c) 1995 5 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <errno.h> 40 #include <rpc/rpc.h> 41 #include <syslog.h> 42 #include <netdb.h> 43 44 #include "statd.h" 45 46 /* sm_stat_1 --------------------------------------------------------------- */ 47 /* 48 * Purpose: RPC call to enquire if a host can be monitored 49 * Returns: TRUE for any hostname that can be looked up to give 50 * an address. 51 */ 52 struct sm_stat_res * 53 sm_stat_1_svc(arg, req) 54 sm_name *arg; 55 struct svc_req *req; 56 { 57 static sm_stat_res res; 58 59 if (debug) 60 syslog(LOG_DEBUG, "stat called for host %s", arg->mon_name); 61 62 if (gethostbyname(arg->mon_name)) 63 res.res_stat = stat_succ; 64 else { 65 syslog(LOG_ERR, "invalid hostname to sm_stat: %s", 66 arg->mon_name); 67 res.res_stat = stat_fail; 68 } 69 70 res.state = status_info->ourState; 71 return (&res); 72 } 73 74 /* sm_mon_1 ---------------------------------------------------------------- */ 75 /* 76 * Purpose: RPC procedure to establish a monitor request 77 * Returns: Success, unless lack of resources prevents 78 * the necessary structures from being set up 79 * to record the request, or if the hostname is not 80 * valid (as judged by gethostbyname()) 81 */ 82 struct sm_stat_res * 83 sm_mon_1_svc(arg, req) 84 mon *arg; 85 struct svc_req *req; 86 { 87 static sm_stat_res res; 88 HostInfo *hp; 89 MonList *lp; 90 91 if (debug) { 92 syslog(LOG_DEBUG, "monitor request for host %s", 93 arg->mon_id.mon_name); 94 syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d", 95 arg->mon_id.mon_name, arg->mon_id.my_id.my_name, 96 arg->mon_id.my_id.my_prog, arg->mon_id.my_id.my_vers, 97 arg->mon_id.my_id.my_proc); 98 } 99 res.res_stat = stat_fail; /* Assume fail until set otherwise */ 100 res.state = status_info->ourState; 101 102 /* 103 * Find existing host entry, or create one if not found. If 104 * find_host() fails, it will have logged the error already. 105 */ 106 if (!gethostbyname(arg->mon_id.mon_name)) 107 syslog(LOG_ERR, "Invalid hostname to sm_mon: %s", 108 arg->mon_id.mon_name); 109 else if (hp = find_host(arg->mon_id.mon_name, TRUE)) { 110 lp = (MonList *)malloc(sizeof(MonList)); 111 if (!lp) 112 syslog(LOG_ERR, "Out of memory"); 113 else { 114 strncpy(lp->notifyHost, arg->mon_id.my_id.my_name, 115 SM_MAXSTRLEN); 116 lp->notifyProg = arg->mon_id.my_id.my_prog; 117 lp->notifyVers = arg->mon_id.my_id.my_vers; 118 lp->notifyProc = arg->mon_id.my_id.my_proc; 119 memcpy(lp->notifyData, arg->priv, 120 sizeof(lp->notifyData)); 121 122 lp->next = hp->monList; 123 hp->monList = lp; 124 sync_file(); 125 126 res.res_stat = stat_succ; /* Report success */ 127 } 128 } 129 return (&res); 130 } 131 132 /* do_unmon ---------------------------------------------------------------- */ 133 /* 134 * Purpose: Remove a monitor request from a host 135 * Returns: TRUE if found, FALSE if not found. 136 * Notes: Common code from sm_unmon_1_svc and sm_unmon_all_1_svc 137 * In the unlikely event of more than one identical monitor 138 * request, all are removed. 139 */ 140 static int 141 do_unmon(hp, idp) 142 HostInfo *hp; 143 my_id *idp; 144 { 145 MonList *lp, *next; 146 MonList *last = NULL; 147 int result = FALSE; 148 149 lp = hp->monList; 150 while (lp) { 151 if (!strncasecmp(idp->my_name, lp->notifyHost, SM_MAXSTRLEN) 152 && (idp->my_prog == lp->notifyProg) 153 && (idp->my_proc == lp->notifyProc) 154 && (idp->my_vers == lp->notifyVers)) { 155 /* found one. Unhook from chain and free. */ 156 next = lp->next; 157 if (last) 158 last->next = next; 159 else 160 hp->monList = next; 161 free(lp); 162 lp = next; 163 result = TRUE; 164 } else { 165 last = lp; 166 lp = lp->next; 167 } 168 } 169 return (result); 170 } 171 172 /* sm_unmon_1 -------------------------------------------------------------- */ 173 /* 174 * Purpose: RPC procedure to release a monitor request. 175 * Returns: Local machine's status number 176 * Notes: The supplied mon_id should match the value passed in an 177 * earlier call to sm_mon_1 178 */ 179 struct sm_stat * 180 sm_unmon_1_svc(arg, req) 181 mon_id *arg; 182 struct svc_req *req; 183 { 184 static sm_stat res; 185 HostInfo *hp; 186 187 if (debug) { 188 syslog(LOG_DEBUG, "un-monitor request for host %s", 189 arg->mon_name); 190 syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d", 191 arg->mon_name, arg->my_id.my_name, arg->my_id.my_prog, 192 arg->my_id.my_vers, arg->my_id.my_proc); 193 } 194 if (hp = find_host(arg->mon_name, FALSE)) { 195 if (do_unmon(hp, &arg->my_id)) 196 sync_file(); 197 else 198 syslog(LOG_ERR, 199 "unmon request from %s, no matching monitor", 200 arg->my_id.my_name); 201 } else 202 syslog(LOG_ERR, "unmon request from %s for unknown host %s", 203 arg->my_id.my_name, arg->mon_name); 204 205 res.state = status_info->ourState; 206 207 return (&res); 208 } 209 210 /* sm_unmon_all_1 ---------------------------------------------------------- */ 211 /* 212 * Purpose: RPC procedure to release monitor requests. 213 * Returns: Local machine's status number 214 * Notes: Releases all monitor requests (if any) from the specified 215 * host and program number. 216 */ 217 struct sm_stat * 218 sm_unmon_all_1_svc(arg, req) 219 my_id *arg; 220 struct svc_req *req; 221 { 222 static sm_stat res; 223 HostInfo *hp; 224 MonList *lp; 225 int i; 226 227 if (debug) { 228 syslog(LOG_DEBUG, 229 "unmon_all for host: %s prog: %d ver: %d proc: %d", 230 arg->my_name, arg->my_prog, arg->my_vers, arg->my_proc); 231 } 232 233 for (i = status_info->noOfHosts, hp = status_info->hosts; i; i--, hp++) 234 do_unmon(hp, arg); 235 236 sync_file(); 237 238 res.state = status_info->ourState; 239 240 return (&res); 241 } 242 243 /* sm_simu_crash_1 --------------------------------------------------------- */ 244 /* 245 * Purpose: RPC procedure to simulate a crash 246 * Returns: Nothing 247 * Notes: Standardised mechanism for debug purposes 248 * The specification says that we should drop all of our 249 * status information (apart from the list of monitored hosts 250 * on disc). However, this would confuse the rpc.lockd 251 * which would be unaware that all of its monitor requests 252 * had been silently junked. Hence we in fact retain all 253 * current requests and simply increment the status counter 254 * and inform all hosts on the monitor list. 255 */ 256 void * 257 sm_simu_crash_1_svc(v, req) 258 void *v; 259 struct svc_req *req; 260 { 261 static char dummy; 262 int work_to_do; 263 HostInfo *hp; 264 int i; 265 266 if (debug) 267 syslog(LOG_DEBUG, "simu_crash called!!"); 268 269 /* 270 * Simulate crash by setting notify-required flag on all monitored 271 * hosts, and incrementing our status number. notify_hosts() is 272 * then called to fork a process to do the notifications. 273 */ 274 for (i = status_info->noOfHosts, hp = status_info->hosts; i > 0; 275 i--, hp++) { 276 if (hp->monList) { 277 work_to_do = TRUE; 278 hp->notifyReqd = TRUE; 279 } 280 } 281 status_info->ourState += 2; /* always even numbers if not crashed */ 282 283 if (work_to_do) 284 notify_hosts(); 285 286 return (&dummy); 287 } 288 289 /* sm_notify_1 ------------------------------------------------------------- */ 290 /* 291 * Purpose: RPC procedure notifying local statd of the crash of another 292 * Returns: Nothing 293 * Notes: There is danger of deadlock, since it is quite likely that 294 * the client procedure that we call will in turn call us 295 * to remove or adjust the monitor request. 296 * We therefore fork() a process to do the notifications. 297 * Note that the main HostInfo structure is in a mmap() 298 * region and so will be shared with the child, but the 299 * monList pointed to by the HostInfo is in normal memory. 300 * Hence if we read the monList before forking, we are 301 * protected from the parent servicing other requests 302 * that modify the list. 303 */ 304 void * 305 sm_notify_1_svc(arg, req) 306 stat_chge *arg; 307 struct svc_req *req; 308 { 309 struct timeval timeout = {20, 0}; /* 20 secs timeout */ 310 CLIENT *cli; 311 static char dummy; 312 status tx_arg; /* arg sent to callback procedure */ 313 MonList *lp; 314 HostInfo *hp; 315 pid_t pid; 316 317 if (debug) 318 syslog(LOG_DEBUG, "notify from host %s, new state %d", 319 arg->mon_name, arg->state); 320 321 hp = find_host(arg->mon_name, FALSE); 322 if (!hp) { 323 /* Never heard of this host - why is it notifying us? */ 324 syslog(LOG_ERR, "Unsolicited notification from host %s", 325 arg->mon_name); 326 return; 327 } 328 lp = hp->monList; 329 if (!lp) /* We know this host, but have no outstanding requests. */ 330 return (FALSE); 331 332 pid = fork(); 333 if (pid == -1) { 334 syslog(LOG_ERR, "Unable to fork notify process - %s", 335 strerror(errno)); 336 return; 337 } 338 if (pid) 339 return (&dummy); /* Parent returns */ 340 341 while (lp) { 342 tx_arg.mon_name = arg->mon_name; 343 tx_arg.state = arg->state; 344 memcpy(tx_arg.priv, lp->notifyData, sizeof(tx_arg.priv)); 345 cli = clnt_create(lp->notifyHost, lp->notifyProg, 346 lp->notifyVers, "udp"); 347 if (!cli) 348 syslog(LOG_ERR, "Failed to contact host %s%s", 349 lp->notifyHost, clnt_spcreateerror("")); 350 else { 351 if (clnt_call(cli, lp->notifyProc, xdr_status, &tx_arg, 352 xdr_void, &dummy, timeout) != RPC_SUCCESS) 353 syslog(LOG_ERR, 354 "Failed to call rpc.statd client at host %s", 355 lp->notifyHost); 356 clnt_destroy(cli); 357 } 358 lp = lp->next; 359 } 360 361 exit(0); /* Child quits */ 362 } 363