1 /* $OpenBSD: statd.c,v 1.1 2008/06/15 04:43:28 sturm 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 /* main() function for status monitor daemon. Some of the code in this */ 39 /* file was generated by running rpcgen /usr/include/rpcsvc/sm_inter.x */ 40 /* The actual program logic is in the file procs.c */ 41 42 #include <sys/param.h> 43 #include <sys/wait.h> 44 45 #include <err.h> 46 #include <ctype.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <signal.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <syslog.h> 54 #include <unistd.h> 55 #include <db.h> 56 57 #include <rpc/rpc.h> 58 59 #include "statd.h" 60 61 struct sigaction sa; 62 int debug = 0; /* Controls syslog() for debug msgs */ 63 int _rpcsvcdirty = 0; /* XXX ??? */ 64 static DB *db; /* Database file */ 65 66 Header status_info; 67 68 static char undefdata[] = "\0\1\2\3\4\5\6\7"; 69 static DBT undefkey = { 70 undefdata, 71 sizeof(undefdata) 72 }; 73 extern char *__progname; 74 75 /* statd.c */ 76 static int walk_one(int (*fun )(DBT *, HostInfo *, void *), DBT *, DBT *, void *); 77 static int walk_db(int (*fun )(DBT *, HostInfo *, void *), void *); 78 static int reset_host(DBT *, HostInfo *, void *); 79 static int check_work(DBT *, HostInfo *, void *); 80 static int unmon_host(DBT *, HostInfo *, void *); 81 static int notify_one(DBT *, HostInfo *, void *); 82 static void init_file(char *); 83 static int notify_one_host(char *); 84 static void die(int); 85 86 int main(int, char **); 87 88 int 89 main(int argc, char **argv) 90 { 91 SVCXPRT *transp; 92 int ch; 93 struct sigaction nsa; 94 95 while ((ch = getopt(argc, argv, "d")) != (-1)) { 96 switch (ch) { 97 case 'd': 98 debug = 1; 99 break; 100 default: 101 case '?': 102 fprintf(stderr, "usage: %s [-d]\n", __progname); 103 exit(1); 104 /* NOTREACHED */ 105 } 106 } 107 pmap_unset(SM_PROG, SM_VERS); 108 109 transp = svcudp_create(RPC_ANYSOCK); 110 if (transp == NULL) { 111 errx(1, "cannot create udp service."); 112 /* NOTREACHED */ 113 } 114 if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_UDP)) { 115 errx(1, "unable to register (SM_PROG, SM_VERS, udp)."); 116 /* NOTREACHED */ 117 } 118 transp = svctcp_create(RPC_ANYSOCK, 0, 0); 119 if (transp == NULL) { 120 errx(1, "cannot create tcp service."); 121 /* NOTREACHED */ 122 } 123 if (!svc_register(transp, SM_PROG, SM_VERS, sm_prog_1, IPPROTO_TCP)) { 124 errx(1, "unable to register (SM_PROG, SM_VERS, tcp)."); 125 /* NOTREACHED */ 126 } 127 128 init_file("/var/db/statd.status"); 129 130 /* 131 * Note that it is NOT sensible to run this program from inetd - the 132 * protocol assumes that it will run immediately at boot time. 133 */ 134 daemon(0, 0); 135 136 sigemptyset(&nsa.sa_mask); 137 nsa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT; 138 nsa.sa_handler = SIG_IGN; 139 sigaction(SIGCHLD, &nsa, NULL); 140 141 openlog("rpc.statd", 0, LOG_DAEMON); 142 if (debug) 143 syslog(LOG_INFO, "Starting - debug enabled"); 144 else 145 syslog(LOG_INFO, "Starting"); 146 147 sa.sa_handler = die; 148 sa.sa_flags = 0; 149 sigemptyset(&sa.sa_mask); 150 sigaction(SIGTERM, &sa, NULL); 151 sigaction(SIGQUIT, &sa, NULL); 152 sigaction(SIGHUP, &sa, NULL); 153 sigaction(SIGINT, &sa, NULL); 154 155 sa.sa_handler = SIG_IGN; 156 sa.sa_flags = SA_RESTART; 157 sigemptyset(&sa.sa_mask); 158 sigaddset(&sa.sa_mask, SIGALRM); 159 160 /* Initialisation now complete - start operating */ 161 162 /* Notify hosts that need it */ 163 notify_handler(0); 164 165 while (1) 166 svc_run(); /* Should never return */ 167 die(0); 168 } 169 170 /* notify_handler ---------------------------------------------------------- */ 171 /* 172 * Purpose: Catch SIGALRM and collect process status 173 * Returns: Nothing. 174 * Notes: No special action required, other than to collect the 175 * process status and hence allow the child to die: 176 * we only use child processes for asynchronous transmission 177 * of SM_NOTIFY to other systems, so it is normal for the 178 * children to exit when they have done their work. 179 */ 180 void 181 notify_handler(int sig) 182 { 183 time_t now; 184 185 NO_ALARM; 186 sa.sa_handler = SIG_IGN; 187 sigaction(SIGALRM, &sa, NULL); 188 189 now = time(NULL); 190 191 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(char *hostnamep, HostInfo *hp) 241 { 242 DBT key, data; 243 char *ptr; 244 char hostname[MAXHOSTNAMELEN + 1]; 245 HostInfo h; 246 247 strlcpy(hostname, hostnamep, sizeof(hostname)); 248 h = *hp; 249 250 for (ptr = hostname; *ptr; ptr++) 251 if (isupper((unsigned char) *ptr)) 252 *ptr = tolower((unsigned char) *ptr); 253 254 key.data = hostname; 255 key.size = ptr - hostname + 1; 256 data.data = &h; 257 data.size = sizeof(h); 258 259 switch ((*db->put)(db, &key, &data, 0)) { 260 case -1: 261 syslog(LOG_ERR, "database corrupted %m"); 262 die(1); 263 case 0: 264 return; 265 default: 266 abort(); 267 } 268 } 269 270 271 /* find_host -------------------------------------------------------------- */ 272 /* 273 * Purpose: Find the entry in the status file for a given host 274 * Returns: Copy of entry in hd, or NULL 275 * Notes: 276 * 277 */ 278 HostInfo * 279 find_host(char *hostname, HostInfo *hp) 280 { 281 DBT key, data; 282 char *ptr; 283 284 for (ptr = hostname; *ptr; ptr++) 285 if (isupper((unsigned char) *ptr)) 286 *ptr = tolower((unsigned char) *ptr); 287 288 key.data = hostname; 289 key.size = ptr - hostname + 1; 290 switch ((*db->get)(db, &key, &data, 0)) { 291 case 0: 292 if (data.size != sizeof(*hp)) 293 goto bad; 294 return memcpy(hp, data.data, sizeof(*hp)); 295 case 1: 296 return NULL; 297 case -1: 298 goto bad; 299 default: 300 abort(); 301 } 302 303 bad: 304 syslog(LOG_ERR, "Database corrupted %m"); 305 return NULL; 306 } 307 308 /* walk_one ------------------------------------------------------------- */ 309 /* 310 * Purpose: Call the given function if the element is valid 311 * Returns: Nothing - exits on error 312 * Notes: 313 */ 314 static int 315 walk_one(int (*fun)(DBT *, HostInfo *, void *), DBT *key, DBT *data, void *ptr) 316 { 317 HostInfo h; 318 if (key->size == undefkey.size && 319 memcmp(key->data, undefkey.data, key->size) == 0) 320 return 0; 321 if (data->size != sizeof(HostInfo)) { 322 syslog(LOG_ERR, "Bad data in database"); 323 die(1); 324 } 325 memcpy(&h, data->data, sizeof(h)); 326 return (*fun)(key, &h, ptr); 327 } 328 329 /* walk_db -------------------------------------------------------------- */ 330 /* 331 * Purpose: Iterate over all elements calling the given function 332 * Returns: -1 if function failed, 0 on success 333 * Notes: 334 */ 335 static int 336 walk_db(int (*fun)(DBT *, HostInfo *, void *), void *ptr) 337 { 338 DBT key, data; 339 340 switch ((*db->seq)(db, &key, &data, R_FIRST)) { 341 case -1: 342 goto bad; 343 case 1: 344 /* We should have at least the magic entry at this point */ 345 abort(); 346 case 0: 347 if (walk_one(fun, &key, &data, ptr) == -1) 348 return -1; 349 break; 350 default: 351 abort(); 352 } 353 354 for (;;) 355 switch ((*db->seq)(db, &key, &data, R_NEXT)) { 356 case -1: 357 goto bad; 358 case 0: 359 if (walk_one(fun, &key, &data, ptr) == -1) 360 return -1; 361 break; 362 case 1: 363 return 0; 364 default: 365 abort(); 366 } 367 bad: 368 syslog(LOG_ERR, "Corrupted database %m"); 369 die(1); 370 } 371 372 /* reset_host ------------------------------------------------------------ */ 373 /* 374 * Purpose: Clean up existing hosts in file. 375 * Returns: Always success 0. 376 * Notes: Clean-up of existing file - monitored hosts will have a 377 * pointer to a list of clients, which refers to memory in 378 * the previous incarnation of the program and so are 379 * meaningless now. These pointers are zeroed and the fact 380 * that the host was previously monitored is recorded by 381 * setting the notifyReqd flag, which will in due course 382 * cause a SM_NOTIFY to be sent. 383 * 384 * Note that if we crash twice in quick succession, some hosts 385 * may already have notifyReqd set, where we didn't manage to 386 * notify them before the second crash occurred. 387 */ 388 static int 389 reset_host(DBT *key, HostInfo *hi, void *ptr) 390 { 391 if (hi->monList) { 392 hi->notifyReqd = *(time_t *) ptr; 393 hi->attempts = 0; 394 hi->monList = NULL; 395 change_host((char *)key->data, hi); 396 } 397 return 0; 398 } 399 400 /* check_work ------------------------------------------------------------ */ 401 /* 402 * Purpose: Check if there is work to be done. 403 * Returns: 0 if there is no work to be done -1 if there is. 404 * Notes: 405 */ 406 static int 407 check_work(DBT *key, HostInfo *hi, void *ptr) 408 { 409 return hi->notifyReqd ? -1 : 0; 410 } 411 412 /* unmon_host ------------------------------------------------------------ */ 413 /* 414 * Purpose: Unmonitor a host 415 * Returns: 0 416 * Notes: 417 */ 418 static int 419 unmon_host(DBT *key, HostInfo *hi, void *ptr) 420 { 421 char *name = key->data; 422 423 if (do_unmon(name, hi, ptr)) 424 change_host(name, hi); 425 return 0; 426 } 427 428 /* notify_one ------------------------------------------------------------ */ 429 /* 430 * Purpose: Notify one host. 431 * Returns: 0 if success -1 on failure 432 * Notes: 433 */ 434 static int 435 notify_one(DBT *key, HostInfo *hi, void *ptr) 436 { 437 time_t now = *(time_t *) ptr; 438 char *name = key->data; 439 int error; 440 441 if (hi->notifyReqd == 0 || hi->notifyReqd > now) 442 return 0; 443 444 /* 445 * If one of the initial attempts fails, we wait 446 * for a while and have another go. This is necessary 447 * because when we have crashed, (eg. a power outage) 448 * it is quite possible that we won't be able to 449 * contact all monitored hosts immediately on restart, 450 * either because they crashed too and take longer 451 * to come up (in which case the notification isn't 452 * really required), or more importantly if some 453 * router etc. needed to reach the monitored host 454 * has not come back up yet. In this case, we will 455 * be a bit late in re-establishing locks (after the 456 * grace period) but that is the best we can do. We 457 * try 10 times at 5 sec intervals, 10 more times at 458 * 1 minute intervals, then 24 more times at hourly 459 * intervals, finally giving up altogether if the 460 * host hasn't come back to life after 24 hours. 461 */ 462 if (notify_one_host(name) || hi->attempts++ >= 44) { 463 error = 0; 464 hi->notifyReqd = 0; 465 hi->attempts = 0; 466 } else { 467 error = -1; 468 if (hi->attempts < 10) 469 hi->notifyReqd += 5; 470 else if (hi->attempts < 20) 471 hi->notifyReqd += 60; 472 else 473 hi->notifyReqd += 60 * 60; 474 } 475 change_host(name, hi); 476 return error; 477 } 478 479 /* init_file -------------------------------------------------------------- */ 480 /* 481 * Purpose: Open file, create if necessary, initialise it. 482 * Returns: Nothing - exits on error 483 * Notes: Called before process becomes daemon, hence logs to 484 * stderr rather than syslog. 485 * Opens the file, then mmap()s it for ease of access. 486 * Also performs initial clean-up of the file, zeroing 487 * monitor list pointers, setting the notifyReqd flag in 488 * all hosts that had a monitor list, and incrementing 489 * the state number to the next even value. 490 */ 491 static void 492 init_file(char *filename) 493 { 494 DBT data; 495 496 db = dbopen(filename, O_RDWR|O_CREAT|O_NDELAY|O_EXLOCK, 0644, DB_HASH, 497 NULL); 498 if (db == NULL) 499 err(1, "Cannot open `%s'", filename); 500 501 switch ((*db->get)(db, &undefkey, &data, 0)) { 502 case 1: 503 /* New database */ 504 memset(&status_info, 0, sizeof(status_info)); 505 sync_file(); 506 return; 507 case -1: 508 err(1, "error accessing database (%m)"); 509 case 0: 510 /* Existing database */ 511 if (data.size != sizeof(status_info)) 512 errx(1, "database corrupted %lu != %lu", 513 (u_long)data.size, (u_long)sizeof(status_info)); 514 memcpy(&status_info, data.data, data.size); 515 break; 516 default: 517 abort(); 518 } 519 520 reset_database(); 521 return; 522 } 523 524 /* reset_database --------------------------------------------------------- */ 525 /* 526 * Purpose: Clears the statd database 527 * Returns: Nothing 528 * Notes: If this is not called on reset, it will leak memory. 529 */ 530 void 531 reset_database(void) 532 { 533 time_t now = time(NULL); 534 walk_db(reset_host, &now); 535 536 /* Select the next higher even number for the state counter */ 537 status_info.ourState = 538 (status_info.ourState + 2) & 0xfffffffe; 539 status_info.ourState++; /* XXX - ??? */ 540 sync_file(); 541 } 542 543 /* unmon_hosts --------------------------------------------------------- */ 544 /* 545 * Purpose: Unmonitor all the hosts 546 * Returns: Nothing 547 * Notes: 548 */ 549 void 550 unmon_hosts(void) 551 { 552 time_t now = time(NULL); 553 walk_db(unmon_host, &now); 554 sync_file(); 555 } 556 557 static int 558 notify_one_host(char *hostname) 559 { 560 struct timeval timeout = {20, 0}; /* 20 secs timeout */ 561 CLIENT *cli; 562 char dummy; 563 stat_chge arg; 564 char our_hostname[MAXHOSTNAMELEN + 1]; 565 566 gethostname(our_hostname, sizeof(our_hostname)); 567 our_hostname[sizeof(our_hostname) - 1] = '\0'; 568 arg.mon_name = our_hostname; 569 arg.state = status_info.ourState; 570 571 if (debug) 572 syslog(LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s", 573 hostname, our_hostname); 574 575 cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp"); 576 if (!cli) { 577 syslog(LOG_ERR, "Failed to contact host %s%s", hostname, 578 clnt_spcreateerror("")); 579 return (FALSE); 580 } 581 if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void, 582 &dummy, timeout) != RPC_SUCCESS) { 583 syslog(LOG_ERR, "Failed to contact rpc.statd at host %s", 584 hostname); 585 clnt_destroy(cli); 586 return (FALSE); 587 } 588 clnt_destroy(cli); 589 return (TRUE); 590 } 591 592 static void 593 die(int n) 594 { 595 (*db->close)(db); 596 exit(n); 597 } 598