2 * Send NSM notify calls to all hosts listed in /var/lib/sm
4 * Copyright (C) 2004-2006 Olaf Kirch <okir@suse.de>
12 #include <sys/types.h>
13 #include <sys/socket.h>
16 #include <sys/param.h>
17 #include <sys/syslog.h>
18 #include <arpa/inet.h>
36 #define NSM_PROG 100024
37 #define NSM_PROGRAM 100024
41 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
42 #define MAXMSGSIZE 256
45 struct nsm_host * next;
47 struct sockaddr_storage addr;
56 static char nsm_hostname[256];
58 static int opt_debug = 0;
59 static _Bool opt_update_state = true;
60 static unsigned int opt_max_retry = 15 * 60;
61 static char * opt_srcaddr = 0;
62 static uint16_t opt_srcport = 0;
64 static void notify(void);
65 static int notify_host(int, struct nsm_host *);
66 static void recv_reply(int);
67 static void insert_host(struct nsm_host *);
68 static struct nsm_host *find_host(uint32_t);
69 static int record_pid(void);
71 static struct nsm_host * hosts = NULL;
73 static struct addrinfo *smn_lookup(const char *name)
75 struct addrinfo *ai, hint = {
76 #if HAVE_DECL_AI_ADDRCONFIG
77 .ai_flags = AI_ADDRCONFIG,
78 #endif /* HAVE_DECL_AI_ADDRCONFIG */
80 .ai_protocol = IPPROTO_UDP,
84 error = getaddrinfo(name, NULL, &hint, &ai);
86 xlog(D_GENERAL, "getaddrinfo(3): %s", gai_strerror(error));
94 static struct nsm_host *
95 smn_alloc_host(const char *hostname, const time_t timestamp)
97 struct nsm_host *host;
99 host = calloc(1, sizeof(*host));
103 host->name = strdup(hostname);
104 if (host->name == NULL) {
109 host->last_used = timestamp;
110 host->timeout = NSM_TIMEOUT;
111 host->retries = 100; /* force address retry */
116 xlog_warn("Unable to allocate memory");
120 static void smn_forget_host(struct nsm_host *host)
122 xlog(D_CALL, "Removing %s from notify list", host->name);
124 nsm_delete_notified_host(host->name);
128 freeaddrinfo(host->ai);
134 smn_get_host(const char *hostname,
135 __attribute__ ((unused)) const struct sockaddr *sap,
136 __attribute__ ((unused)) const struct mon *m,
137 const time_t timestamp)
139 struct nsm_host *host;
141 host = smn_alloc_host(hostname, timestamp);
146 xlog(D_GENERAL, "Added host %s to notify list", hostname);
151 main(int argc, char **argv)
157 progname = strrchr(argv[0], '/');
158 if (progname != NULL)
163 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
172 opt_max_retry = atoi(optarg) * 60;
175 opt_update_state = false;
178 opt_srcport = atoi(optarg);
181 opt_srcaddr = optarg;
184 if (!nsm_setup_pathnames(argv[0], optarg))
194 usage: fprintf(stderr,
195 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
196 " [-P /path/to/state/directory] [-v my_host_name]\n",
204 xlog_config(D_ALL, 1);
209 xlog(L_NOTICE, "Version " VERSION " starting");
211 if (nsm_is_default_parentdir()) {
212 if (record_pid() == 0 && force == 0 && opt_update_state) {
213 /* already run, don't try again */
214 xlog(L_NOTICE, "Already notifying clients; Exiting!");
220 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
222 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
223 xlog(L_ERROR, "Failed to obtain name of local host: %m");
227 (void)nsm_retire_monitored_hosts();
228 if (nsm_load_notify_list(smn_get_host) == 0) {
229 xlog(D_GENERAL, "No hosts to notify; exiting");
233 nsm_state = nsm_get_state(opt_update_state);
236 nsm_update_kernel_state(nsm_state);
239 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
241 if (daemon(0, 0) < 0) {
242 xlog(L_ERROR, "unable to background: %m");
256 while ((hp = hosts) != 0) {
258 xlog(L_NOTICE, "Unable to notify %s, giving up",
273 struct sockaddr_storage address;
274 struct sockaddr *local_addr = (struct sockaddr *)&address;
280 sock = socket(AF_INET, SOCK_DGRAM, 0);
282 xlog(L_ERROR, "Failed to create RPC socket: %m");
285 fcntl(sock, F_SETFL, O_NONBLOCK);
287 memset(&address, 0, sizeof(address));
288 local_addr->sa_family = AF_INET; /* Default to IPv4 */
290 /* Bind source IP if provided on command line */
292 struct addrinfo *ai = smn_lookup(opt_srcaddr);
295 "Not a valid hostname or address: \"%s\"",
300 /* We know it's IPv4 at this point */
301 memcpy(local_addr, ai->ai_addr, ai->ai_addrlen);
306 /* Use source port if provided on the command line,
307 * otherwise use bindresvport */
309 nfs_set_port(local_addr, opt_srcport);
310 if (bind(sock, local_addr, sizeof(struct sockaddr_in)) < 0) {
311 xlog(L_ERROR, "Failed to bind RPC socket: %m");
316 struct sockaddr_in *sin = (struct sockaddr_in *)local_addr;
317 (void) bindresvport(sock, sin);
318 /* try to avoid known ports */
319 se = getservbyport(sin->sin_port, "udp");
320 if (se && retry_cnt < 100) {
328 failtime = time(NULL) + opt_max_retry;
330 if (!nsm_drop_privileges(-1))
335 time_t now = time(NULL);
336 unsigned int sent = 0;
340 if (failtime && now >= failtime)
343 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
344 /* Never send more than 10 packets at once */
348 /* Remove queue head */
352 if (notify_host(sock, hp))
355 /* Set the timeout for this call, using an
356 exponential timeout strategy */
358 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
359 hp->timeout = NSM_MAX_TIMEOUT;
360 hp->send_next = now + wait;
368 xlog(D_GENERAL, "Host %s due in %ld seconds",
377 if (poll(&pfd, 1, wait) != 1)
385 * Send notification to a single host
388 notify_host(int sock, struct nsm_host *host)
390 struct sockaddr_storage address;
391 struct sockaddr *dest = (struct sockaddr *)&address;
392 socklen_t destlen = sizeof(address);
393 static unsigned int xid = 0;
394 uint32_t msgbuf[MAXMSGSIZE], *p;
398 xid = getpid() + time(NULL);
402 if (host->ai == NULL) {
403 host->ai = smn_lookup(host->name);
404 if (host->ai == NULL) {
405 xlog_warn("DNS resolution of %s failed; "
406 "retrying later", host->name);
411 memset(msgbuf, 0, sizeof(msgbuf));
413 *p++ = htonl(host->xid);
417 /* If we retransmitted 4 times, reset the port to force
418 * a new portmap lookup (in case statd was restarted).
419 * We also rotate through multiple IP addresses at this
422 if (host->retries >= 4) {
423 /* don't rotate if there is only one addrinfo */
424 if (host->ai->ai_next == NULL)
425 memcpy(&host->addr, host->ai->ai_addr,
426 host->ai->ai_addrlen);
428 struct addrinfo *first = host->ai;
429 struct addrinfo **next = &host->ai;
431 /* remove the first entry from the list */
432 host->ai = first->ai_next;
433 first->ai_next = NULL;
434 /* find the end of the list */
435 next = &first->ai_next;
437 next = & (*next)->ai_next;
438 /* put first entry at end */
440 memcpy(&host->addr, first->ai_addr,
444 nfs_set_port((struct sockaddr *)&host->addr, 0);
448 memcpy(dest, &host->addr, destlen);
449 if (nfs_get_port(dest) == 0) {
450 /* Build a PMAP packet */
451 xlog(D_GENERAL, "Sending portmap query to %s", host->name);
453 nfs_set_port(dest, 111);
454 *p++ = htonl(100000);
462 *p++ = htonl(NSM_PROGRAM);
463 *p++ = htonl(NSM_VERSION);
464 *p++ = htonl(IPPROTO_UDP);
467 /* Build an SM_NOTIFY packet */
468 xlog(D_GENERAL, "Sending SM_NOTIFY to %s", host->name);
470 *p++ = htonl(NSM_PROGRAM);
471 *p++ = htonl(NSM_VERSION);
472 *p++ = htonl(NSM_NOTIFY);
479 len = strlen(nsm_hostname);
481 memcpy(p, nsm_hostname, len);
483 *p++ = htonl(nsm_state);
485 len = (p - msgbuf) << 2;
487 if (sendto(sock, msgbuf, len, 0, dest, destlen) < 0)
488 xlog_warn("Sending Reboot Notification to "
489 "'%s' failed: errno %d (%m)", host->name, errno);
495 * Receive reply from remote host
501 struct sockaddr *sap;
502 uint32_t msgbuf[MAXMSGSIZE], *p, *end;
506 res = recv(sock, msgbuf, sizeof(msgbuf), 0);
510 xlog(D_GENERAL, "Received packet...");
513 end = p + (res >> 2);
516 if (*p++ != htonl(1) /* must be REPLY */
517 || *p++ != htonl(0) /* must be ACCEPTED */
518 || *p++ != htonl(0) /* must be NULL verifier */
520 || *p++ != htonl(0)) /* must be SUCCESS */
523 /* Before we look at the data, find the host struct for
525 if ((hp = find_host(xid)) == NULL)
527 sap = (struct sockaddr *)&hp->addr;
529 if (nfs_get_port(sap) == 0) {
530 /* This was a portmap request */
537 hp->send_next = time(NULL);
539 /* No binding for statd. Delay the next
540 * portmap query for max timeout */
541 xlog(D_GENERAL, "No statd on %s", hp->name);
542 hp->timeout = NSM_MAX_TIMEOUT;
543 hp->send_next += NSM_MAX_TIMEOUT;
545 nfs_set_port(sap, port);
546 if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
547 hp->timeout = NSM_MAX_TIMEOUT / 4;
551 /* Successful NOTIFY call. Server returns void,
552 * so nothing we need to do here (except
553 * check that we didn't read past the end of the
557 xlog(D_GENERAL, "Host %s notified successfully",
564 fail: /* Re-insert the host */
569 * Insert host into sorted list
572 insert_host(struct nsm_host *host)
574 struct nsm_host **where, *p;
577 while ((p = *where) != 0) {
578 /* Sort in ascending order of timeout */
579 if (host->send_next < p->send_next)
581 /* If we have the same timeout, put the
582 * most recently used host first.
583 * This makes sure that "recent" hosts
584 * get notified first.
586 if (host->send_next == p->send_next
587 && host->last_used > p->last_used)
597 * Find host given the XID
599 static struct nsm_host *
600 find_host(uint32_t xid)
602 struct nsm_host **where, *p;
605 while ((p = *where) != 0) {
616 * Record pid in /var/run/sm-notify.pid
617 * This file should remain until a reboot, even if the
619 * If file already exists, fail.
621 static int record_pid(void)
627 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
628 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
632 len = write(fd, pid, strlen(pid));
633 if ((len < 0) || ((size_t)len != strlen(pid))) {
634 xlog_warn("Writing to pid file failed: errno %d (%m)",