1 /* $NetBSD: statd.c,v 1.12 1998/07/06 06:58:09 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Christos Zoulas. All rights reserved. 5 * Copyright (c) 1995 6 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed for the FreeBSD project 19 * This product includes software developed by Christos Zoulas. 20 * 4. Neither the name of the author nor the names of any co-contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 */ 37 38 #include <sys/cdefs.h> 39 #ifndef lint 40 __RCSID("$NetBSD: statd.c,v 1.12 1998/07/06 06:58:09 mrg Exp $"); 41 #endif 42 43 /* main() function for status monitor daemon. Some of the code in this */ 44 /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x */ 45 /* The actual program logic is in the file procs.c */ 46 47 #include <sys/param.h> 48 49 #include <err.h> 50 #include <ctype.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <signal.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <syslog.h> 58 #include <unistd.h> 59 #include <db.h> 60 61 #include <rpc/rpc.h> 62 63 #include "statd.h" 64 65 struct sigaction sa; 66 int debug = 0; /* Controls syslog() for debug msgs */ 67 int _rpcsvcdirty = 0; /* XXX ??? */ 68 static DB *db; /* Database file */ 69 70 Header status_info; 71 72 static char undefdata[] = "\0\1\2\3\4\5\6\7"; 73 static DBT undefkey = { 74 undefdata, 75 sizeof(undefdata) 76 }; 77 extern char *__progname; 78 79 80 /* statd.c */ 81 static int walk_one __P((int (*fun )__P ((DBT *, DBT *, void *)), DBT *, DBT *, void *)); 82 static int walk_db __P((int (*fun )__P ((DBT *, DBT *, void *)), void *)); 83 static int reset_host __P((DBT *, DBT *, void *)); 84 static int check_work __P((DBT *, DBT *, void *)); 85 static int unmon_host __P((DBT *, DBT *, void *)); 86 static int notify_one __P((DBT *, DBT *, void *)); 87 static void init_file __P((char *)); 88 static int notify_one_host __P((char *)); 89 static void die __P((int)) __attribute__((__noreturn__)); 90 91 int main __P((int, char **)); 92 93 int 94 main(argc, argv) 95 int argc; 96 char **argv; 97 { 98 SVCXPRT *transp; 99 int ch; 100 101 while ((ch = getopt(argc, argv, "d")) != (-1)) { 102 switch (ch) { 103 case 'd': 104 debug = 1; 105 break; 106 default: 107 case '?': 108 (void) fprintf(stderr, "Usage: %s [-d]", __progname); 109 /* NOTREACHED */ 110 } 111 } 112 (void)pmap_unset(SM_PROG, SM_VERS); 113 114 transp = svcudp_create(RPC_ANYSOCK); 115 if (transp == NULL) { 116 errx(1, "cannot create udp service."); 117 /* NOTREACHED */ 118 } 119 if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP)) { 120 errx(1, "unable to register (SM_PROG, SM_VERS, udp)."); 121 /* NOTREACHED */ 122 } 123 transp = svctcp_create(RPC_ANYSOCK, 0, 0); 124 if (transp == NULL) { 125 errx(1, "cannot create tcp service."); 126 /* NOTREACHED */ 127 } 128 if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP)) { 129 errx(1, "unable to register (SM_PROG, SM_VERS, tcp)."); 130 /* NOTREACHED */ 131 } 132 init_file("/var/db/statd.status"); 133 134 /* 135 * Note that it is NOT sensible to run this program from inetd - the 136 * protocol assumes that it will run immediately at boot time. 137 */ 138 if (!debug) 139 daemon(0, 0); 140 openlog("rpc.statd", 0, LOG_DAEMON); 141 if (debug) 142 syslog(LOG_INFO, "Starting - debug enabled"); 143 else 144 syslog(LOG_INFO, "Starting"); 145 146 sa.sa_handler = die; 147 sa.sa_flags = 0; 148 sigemptyset(&sa.sa_mask); 149 (void)sigaction(SIGTERM, &sa, NULL); 150 (void)sigaction(SIGQUIT, &sa, NULL); 151 (void)sigaction(SIGHUP, &sa, NULL); 152 (void)sigaction(SIGINT, &sa, NULL); 153 154 sa.sa_handler = SIG_IGN; 155 sa.sa_flags = SA_RESTART; 156 sigemptyset(&sa.sa_mask); 157 sigaddset(&sa.sa_mask, SIGALRM); 158 159 /* Initialisation now complete - start operating */ 160 161 /* Notify hosts that need it */ 162 notify_handler(0); 163 164 while (1) 165 svc_run(); /* Should never return */ 166 die(0); 167 } 168 169 /* notify_handler ---------------------------------------------------------- */ 170 /* 171 * Purpose: Catch SIGALRM and collect process status 172 * Returns: Nothing. 173 * Notes: No special action required, other than to collect the 174 * process status and hence allow the child to die: 175 * we only use child processes for asynchronous transmission 176 * of SM_NOTIFY to other systems, so it is normal for the 177 * children to exit when they have done their work. 178 */ 179 void 180 notify_handler(sig) 181 int sig; 182 { 183 time_t now; 184 185 NO_ALARM; 186 sa.sa_handler = SIG_IGN; 187 (void)sigaction(SIGALRM, &sa, NULL); 188 189 now = time(NULL); 190 191 (void) walk_db(notify_one, &now); 192 193 if (walk_db(check_work, &now) == 0) { 194 /* 195 * No more work to be done. 196 */ 197 CLR_ALARM; 198 return; 199 } 200 sync_file(); 201 ALARM; 202 alarm(5); 203 } 204 205 /* sync_file --------------------------------------------------------------- */ 206 /* 207 * Purpose: Packaged call of msync() to flush changes to mmap()ed file 208 * Returns: Nothing. Errors to syslog. 209 */ 210 void 211 sync_file() 212 { 213 DBT data; 214 215 data.data = &status_info; 216 data.size = sizeof(status_info); 217 switch ((*db->put)(db, &undefkey, &data, 0)) { 218 case 0: 219 return; 220 case -1: 221 goto bad; 222 default: 223 abort(); 224 } 225 if ((*db->sync)(db, 0) == -1) { 226 bad: 227 syslog(LOG_ERR, "database corrupted %m"); 228 die(1); 229 } 230 } 231 232 /* change_host -------------------------------------------------------------- */ 233 /* 234 * Purpose: Update/Create an entry for host 235 * Returns: Nothing 236 * Notes: 237 * 238 */ 239 void 240 change_host(hostname, hp) 241 char *hostname; 242 HostInfo *hp; 243 { 244 DBT key, data; 245 char *ptr; 246 247 for (ptr = hostname; *ptr; ptr++) 248 if (isupper((unsigned char) *ptr)) 249 *ptr = tolower((unsigned char) *ptr); 250 251 key.data = hostname; 252 key.size = ptr - hostname + 1; 253 data.data = hp; 254 data.size = sizeof(*hp); 255 256 switch ((*db->put)(db, &key, &data, 0)) { 257 case -1: 258 syslog(LOG_ERR, "database corrupted %m"); 259 die(1); 260 case 0: 261 return; 262 default: 263 abort(); 264 } 265 } 266 267 268 /* find_host -------------------------------------------------------------- */ 269 /* 270 * Purpose: Find the entry in the status file for a given host 271 * Returns: Copy of entry in hd, or NULL 272 * Notes: 273 * 274 */ 275 HostInfo * 276 find_host(hostname, hp) 277 char *hostname; 278 HostInfo *hp; 279 { 280 DBT key, data; 281 char *ptr; 282 283 for (ptr = hostname; *ptr; ptr++) 284 if (isupper((unsigned char) *ptr)) 285 *ptr = tolower((unsigned char) *ptr); 286 287 key.data = hostname; 288 key.size = ptr - hostname + 1; 289 switch ((*db->get)(db, &key, &data, 0)) { 290 case 0: 291 if (data.size != sizeof(*hp)) 292 goto bad; 293 return memcpy(hp, data.data, sizeof(*hp)); 294 case 1: 295 return NULL; 296 case -1: 297 goto bad; 298 default: 299 abort(); 300 } 301 302 bad: 303 syslog(LOG_ERR, "Database corrupted %m"); 304 return NULL; 305 } 306 307 /* walk_one ------------------------------------------------------------- */ 308 /* 309 * Purpose: Call the given function if the element is valid 310 * Returns: Nothing - exits on error 311 * Notes: 312 */ 313 static int 314 walk_one(fun, key, data, ptr) 315 int (*fun) __P((DBT *, DBT *, void *)); 316 DBT *key, *data; 317 void *ptr; 318 { 319 if (key->size == undefkey.size && 320 memcmp(key->data, undefkey.data, key->size) == 0) 321 return 0; 322 if (data->size != sizeof(HostInfo)) { 323 syslog(LOG_ERR, "Bad data in database"); 324 die(1); 325 } 326 327 return (*fun)(key, data, ptr); 328 } 329 330 /* walk_db -------------------------------------------------------------- */ 331 /* 332 * Purpose: Iterate over all elements calling the given function 333 * Returns: -1 if function failed, 0 on success 334 * Notes: 335 */ 336 static int 337 walk_db(fun, ptr) 338 int (*fun) __P((DBT *, DBT *, void *)); 339 void *ptr; 340 { 341 DBT key, data; 342 343 switch ((*db->seq)(db, &key, &data, R_FIRST)) { 344 case -1: 345 goto bad; 346 case 1: 347 /* We should have at least the magic entry at this point */ 348 abort(); 349 case 0: 350 if (walk_one(fun, &key, &data, ptr) == -1) 351 return -1; 352 break; 353 default: 354 abort(); 355 } 356 357 358 for (;;) 359 switch ((*db->seq)(db, &key, &data, R_NEXT)) { 360 case -1: 361 goto bad; 362 case 1: 363 if (walk_one(fun, &key, &data, ptr) == -1) 364 return -1; 365 break; 366 case 0: 367 return 0; 368 default: 369 abort(); 370 } 371 bad: 372 syslog(LOG_ERR, "Corrupted database %m"); 373 die(1); 374 } 375 376 /* reset_host ------------------------------------------------------------ */ 377 /* 378 * Purpose: Clean up existing hosts in file. 379 * Returns: Always success 0. 380 * Notes: Clean-up of existing file - monitored hosts will have a 381 * pointer to a list of clients, which refers to memory in 382 * the previous incarnation of the program and so are 383 * meaningless now. These pointers are zeroed and the fact 384 * that the host was previously monitored is recorded by 385 * setting the notifyReqd flag, which will in due course 386 * cause a SM_NOTIFY to be sent. 387 * 388 * Note that if we crash twice in quick succession, some hosts 389 * may already have notifyReqd set, where we didn't manage to 390 * notify them before the second crash occurred. 391 */ 392 static int 393 reset_host(key, data, ptr) 394 DBT *key, *data; 395 void *ptr; 396 { 397 HostInfo *hi = data->data; 398 399 if (hi->monList) { 400 hi->notifyReqd = *(time_t *) data; 401 hi->attempts = 0; 402 hi->monList = NULL; 403 } 404 return 0; 405 } 406 407 /* check_work ------------------------------------------------------------ */ 408 /* 409 * Purpose: Check if there is work to be done. 410 * Returns: 0 if there is no work to be done -1 if there is. 411 * Notes: 412 */ 413 static int 414 check_work(key, data, ptr) 415 DBT *key, *data; 416 void *ptr; 417 { 418 HostInfo *hi = data->data; 419 420 return hi->notifyReqd ? -1 : 0; 421 } 422 423 /* unmon_host ------------------------------------------------------------ */ 424 /* 425 * Purpose: Unmonitor a host 426 * Returns: 0 427 * Notes: 428 */ 429 static int 430 unmon_host(key, data, ptr) 431 DBT *key, *data; 432 void *ptr; 433 { 434 char *name = key->data; 435 HostInfo *hi = data->data; 436 437 if (do_unmon(name, hi, ptr)) 438 change_host(name, hi); 439 return 0; 440 } 441 442 /* notify_one ------------------------------------------------------------ */ 443 /* 444 * Purpose: Notify one host. 445 * Returns: 0 if success -1 on failure 446 * Notes: 447 */ 448 static int 449 notify_one(key, data, ptr) 450 DBT *key, *data; 451 void *ptr; 452 { 453 time_t now = *(time_t *) ptr; 454 char *name = key->data; 455 HostInfo *hi = data->data; 456 457 if (hi->notifyReqd == 0 || hi->notifyReqd > now) 458 return 0; 459 460 if (notify_one_host(name)) { 461 give_up: 462 hi->notifyReqd = 0; 463 hi->attempts = 0; 464 switch ((*db->put)(db, key, data, 0)) { 465 case -1: 466 syslog(LOG_ERR, "Error storing %s (%m)", name); 467 case 0: 468 return 0; 469 470 default: 471 abort(); 472 } 473 } 474 else { 475 /* 476 * If one of the initial attempts fails, we wait 477 * for a while and have another go. This is necessary 478 * because when we have crashed, (eg. a power outage) 479 * it is quite possible that we won't be able to 480 * contact all monitored hosts immediately on restart, 481 * either because they crashed too and take longer 482 * to come up (in which case the notification isn't 483 * really required), or more importantly if some 484 * router etc. needed to reach the monitored host 485 * has not come back up yet. In this case, we will 486 * be a bit late in re-establishing locks (after the 487 * grace period) but that is the best we can do. We 488 * try 10 times at 5 sec intervals, 10 more times at 489 * 1 minute intervals, then 24 more times at hourly 490 * intervals, finally giving up altogether if the 491 * host hasn't come back to life after 24 hours. 492 */ 493 if (hi->attempts++ >= 44) 494 goto give_up; 495 else if (hi->attempts < 10) 496 hi->notifyReqd += 5; 497 else if (hi->attempts < 20) 498 hi->notifyReqd += 60; 499 else 500 hi->notifyReqd += 60 * 60; 501 return -1; 502 } 503 } 504 505 /* init_file -------------------------------------------------------------- */ 506 /* 507 * Purpose: Open file, create if necessary, initialise it. 508 * Returns: Nothing - exits on error 509 * Notes: Called before process becomes daemon, hence logs to 510 * stderr rather than syslog. 511 * Opens the file, then mmap()s it for ease of access. 512 * Also performs initial clean-up of the file, zeroing 513 * monitor list pointers, setting the notifyReqd flag in 514 * all hosts that had a monitor list, and incrementing 515 * the state number to the next even value. 516 */ 517 static void 518 init_file(filename) 519 char *filename; 520 { 521 DBT data; 522 523 db = dbopen(filename, O_RDWR|O_CREAT|O_NDELAY|O_EXLOCK, 0644, DB_HASH, 524 NULL); 525 if (db == NULL) 526 err(1, "Cannot open `%s'", filename); 527 528 switch ((*db->get)(db, &undefkey, &data, 0)) { 529 case 1: 530 /* New database */ 531 (void)memset(&status_info, 0, sizeof(status_info)); 532 sync_file(); 533 return; 534 535 case -1: 536 err(1, "error accessing database (%m)"); 537 case 0: 538 /* Existing database */ 539 if (data.size != sizeof(status_info)) 540 errx(1, "database corrupted %lu != %lu", 541 (u_long)data.size, (u_long)sizeof(status_info)); 542 break; 543 default: 544 abort(); 545 } 546 547 reset_database(); 548 return; 549 } 550 551 /* reset_database --------------------------------------------------------- */ 552 /* 553 * Purpose: Clears the statd database 554 * Returns: Nothing 555 * Notes: If this is not called on reset, it will leak memory. 556 */ 557 void 558 reset_database() 559 { 560 time_t now = time(NULL); 561 walk_db(reset_host, &now); 562 563 /* Select the next higher even number for the state counter */ 564 status_info.ourState = 565 (status_info.ourState + 2) & 0xfffffffe; 566 status_info.ourState++; /* XXX - ??? */ 567 sync_file(); 568 } 569 570 /* unmon_hosts --------------------------------------------------------- */ 571 /* 572 * Purpose: Unmonitor all the hosts 573 * Returns: Nothing 574 * Notes: 575 */ 576 void 577 unmon_hosts() 578 { 579 time_t now = time(NULL); 580 walk_db(unmon_host, &now); 581 sync_file(); 582 } 583 584 static int 585 notify_one_host(hostname) 586 char *hostname; 587 { 588 struct timeval timeout = {20, 0}; /* 20 secs timeout */ 589 CLIENT *cli; 590 char dummy; 591 stat_chge arg; 592 char our_hostname[MAXHOSTNAMELEN + 1]; 593 594 gethostname(our_hostname, sizeof(our_hostname)); 595 our_hostname[sizeof(our_hostname) - 1] = '\0'; 596 our_hostname[SM_MAXSTRLEN] = '\0'; 597 arg.mon_name = our_hostname; 598 arg.state = status_info.ourState; 599 600 if (debug) 601 syslog(LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s", 602 hostname, our_hostname); 603 604 cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp"); 605 if (!cli) { 606 syslog(LOG_ERR, "Failed to contact host %s%s", hostname, 607 clnt_spcreateerror("")); 608 return (FALSE); 609 } 610 if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void, 611 &dummy, timeout) != RPC_SUCCESS) { 612 syslog(LOG_ERR, "Failed to contact rpc.statd at host %s", 613 hostname); 614 clnt_destroy(cli); 615 return (FALSE); 616 } 617 clnt_destroy(cli); 618 return (TRUE); 619 } 620 621 622 static void 623 die(n) 624 int n; 625 { 626 (*db->close)(db); 627 exit(n); 628 } 629