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>
37 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
40 struct nsm_host * next;
50 static char nsm_hostname[256];
52 static int nsm_family = AF_INET;
53 static int opt_debug = 0;
54 static _Bool opt_update_state = true;
55 static unsigned int opt_max_retry = 15 * 60;
56 static char * opt_srcaddr = 0;
57 static uint16_t opt_srcport = 0;
59 static void notify(const int sock);
60 static int notify_host(int, struct nsm_host *);
61 static void recv_reply(int);
62 static void insert_host(struct nsm_host *);
63 static struct nsm_host *find_host(uint32_t);
64 static int record_pid(void);
66 static struct nsm_host * hosts = NULL;
68 static struct addrinfo *smn_lookup(const char *name)
70 struct addrinfo *ai, hint = {
71 #if HAVE_DECL_AI_ADDRCONFIG
72 .ai_flags = AI_ADDRCONFIG,
73 #endif /* HAVE_DECL_AI_ADDRCONFIG */
75 .ai_protocol = IPPROTO_UDP,
79 error = getaddrinfo(name, NULL, &hint, &ai);
81 xlog(D_GENERAL, "getaddrinfo(3): %s", gai_strerror(error));
89 static struct nsm_host *
90 smn_alloc_host(const char *hostname, const time_t timestamp)
92 struct nsm_host *host;
94 host = calloc(1, sizeof(*host));
98 host->name = strdup(hostname);
99 if (host->name == NULL) {
104 host->last_used = timestamp;
105 host->timeout = NSM_TIMEOUT;
106 host->retries = 100; /* force address retry */
111 xlog_warn("Unable to allocate memory");
115 static void smn_forget_host(struct nsm_host *host)
117 xlog(D_CALL, "Removing %s from notify list", host->name);
119 nsm_delete_notified_host(host->name);
123 freeaddrinfo(host->ai);
129 smn_get_host(const char *hostname,
130 __attribute__ ((unused)) const struct sockaddr *sap,
131 __attribute__ ((unused)) const struct mon *m,
132 const time_t timestamp)
134 struct nsm_host *host;
136 host = smn_alloc_host(hostname, timestamp);
141 xlog(D_GENERAL, "Added host %s to notify list", hostname);
145 #ifdef IPV6_SUPPORTED
146 static int smn_socket(void)
151 * Use an AF_INET socket if IPv6 is disabled on the
154 sock = socket(AF_INET6, SOCK_DGRAM, 0);
156 if (errno != EAFNOSUPPORT) {
157 xlog(L_ERROR, "Failed to create RPC socket: %m");
160 sock = socket(AF_INET, SOCK_DGRAM, 0);
162 xlog(L_ERROR, "Failed to create RPC socket: %m");
166 nsm_family = AF_INET6;
168 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
169 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
174 * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4. However,
175 * since sm-notify open-codes all of its RPC support, it can
176 * use a single socket and let the local network stack provide
177 * the correct mapping between address families automatically.
178 * This is the same thing that is done in the kernel.
180 if (nsm_family == AF_INET6) {
182 socklen_t zerolen = (socklen_t)sizeof(zero);
184 if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
185 (char *)&zero, zerolen) == -1) {
186 xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
197 #else /* !IPV6_SUPPORTED */
198 static int smn_socket(void)
202 sock = socket(AF_INET, SOCK_DGRAM, 0);
204 xlog(L_ERROR, "Failed to create RPC socket: %m");
208 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
209 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
216 #endif /* !IPV6_SUPPORTED */
219 * Prepare a socket for sending RPC requests
221 * Returns a bound datagram socket file descriptor, or -1 if
225 smn_create_socket(const char *srcaddr, const uint16_t srcport)
227 struct sockaddr_storage address;
228 struct sockaddr *local_addr = (struct sockaddr *)&address;
229 int sock, retry_cnt = 0;
236 memset(&address, 0, sizeof(address));
237 local_addr->sa_family = AF_INET; /* Default to IPv4 */
239 /* Bind source IP if provided on command line */
241 struct addrinfo *ai = smn_lookup(srcaddr);
244 "Not a valid hostname or address: \"%s\"",
250 /* We know it's IPv4 at this point */
251 memcpy(local_addr, ai->ai_addr, ai->ai_addrlen);
256 /* Use source port if provided on the command line,
257 * otherwise use bindresvport */
259 nfs_set_port(local_addr, srcport);
260 if (bind(sock, local_addr, sizeof(struct sockaddr_in)) < 0) {
261 xlog(L_ERROR, "Failed to bind RPC socket: %m");
267 struct sockaddr_in *sin = (struct sockaddr_in *)local_addr;
268 (void) bindresvport(sock, sin);
269 /* try to avoid known ports */
270 se = getservbyport(sin->sin_port, "udp");
271 if (se && retry_cnt < 100) {
282 main(int argc, char **argv)
284 int c, sock, force = 0;
287 progname = strrchr(argv[0], '/');
288 if (progname != NULL)
293 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
302 opt_max_retry = atoi(optarg) * 60;
305 opt_update_state = false;
308 opt_srcport = atoi(optarg);
311 opt_srcaddr = optarg;
314 if (!nsm_setup_pathnames(argv[0], optarg))
324 usage: fprintf(stderr,
325 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
326 " [-P /path/to/state/directory] [-v my_host_name]\n",
334 xlog_config(D_ALL, 1);
339 xlog(L_NOTICE, "Version " VERSION " starting");
341 if (nsm_is_default_parentdir()) {
342 if (record_pid() == 0 && force == 0 && opt_update_state) {
343 /* already run, don't try again */
344 xlog(L_NOTICE, "Already notifying clients; Exiting!");
350 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
352 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
353 xlog(L_ERROR, "Failed to obtain name of local host: %m");
357 (void)nsm_retire_monitored_hosts();
358 if (nsm_load_notify_list(smn_get_host) == 0) {
359 xlog(D_GENERAL, "No hosts to notify; exiting");
363 nsm_state = nsm_get_state(opt_update_state);
366 nsm_update_kernel_state(nsm_state);
369 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
371 if (daemon(0, 0) < 0) {
372 xlog(L_ERROR, "unable to background: %m");
381 sock = smn_create_socket(opt_srcaddr, opt_srcport);
385 if (!nsm_drop_privileges(-1))
393 while ((hp = hosts) != 0) {
395 xlog(L_NOTICE, "Unable to notify %s, giving up",
408 notify(const int sock)
413 failtime = time(NULL) + opt_max_retry;
417 time_t now = time(NULL);
418 unsigned int sent = 0;
422 if (failtime && now >= failtime)
425 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
426 /* Never send more than 10 packets at once */
430 /* Remove queue head */
434 if (notify_host(sock, hp))
437 /* Set the timeout for this call, using an
438 exponential timeout strategy */
440 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
441 hp->timeout = NSM_MAX_TIMEOUT;
442 hp->send_next = now + wait;
450 xlog(D_GENERAL, "Host %s due in %ld seconds",
459 if (poll(&pfd, 1, wait) != 1)
467 * Send notification to a single host
470 notify_host(int sock, struct nsm_host *host)
472 struct sockaddr *sap;
475 if (host->ai == NULL) {
476 host->ai = smn_lookup(host->name);
477 if (host->ai == NULL) {
478 xlog_warn("DNS resolution of %s failed; "
479 "retrying later", host->name);
484 /* If we retransmitted 4 times, reset the port to force
485 * a new portmap lookup (in case statd was restarted).
486 * We also rotate through multiple IP addresses at this
489 if (host->retries >= 4) {
490 /* don't rotate if there is only one addrinfo */
491 if (host->ai->ai_next != NULL) {
492 struct addrinfo *first = host->ai;
493 struct addrinfo **next = &host->ai;
495 /* remove the first entry from the list */
496 host->ai = first->ai_next;
497 first->ai_next = NULL;
498 /* find the end of the list */
499 next = &first->ai_next;
501 next = & (*next)->ai_next;
502 /* put first entry at end */
506 nfs_set_port(host->ai->ai_addr, 0);
510 sap = host->ai->ai_addr;
511 salen = host->ai->ai_addrlen;
513 if (nfs_get_port(sap) == 0)
514 host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
516 host->xid = nsm_xmit_notify(sock, sap, salen,
517 SM_PROG, nsm_hostname, nsm_state);
523 * Extract the returned port number and set up the SM_NOTIFY call.
526 recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
528 uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
530 host->send_next = time(NULL);
534 /* No binding for statd... */
535 xlog(D_GENERAL, "No statd on host %s", host->name);
536 host->timeout = NSM_MAX_TIMEOUT;
537 host->send_next += NSM_MAX_TIMEOUT;
539 nfs_set_port(sap, port);
540 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
541 host->timeout = NSM_MAX_TIMEOUT / 4;
548 * Successful NOTIFY call. Server returns void, so nothing
549 * we need to do here.
552 recv_notify_reply(struct nsm_host *host)
554 xlog(D_GENERAL, "Host %s notified successfully", host->name);
556 smn_forget_host(host);
560 * Receive reply from remote host
566 struct sockaddr *sap;
567 char msgbuf[NSM_MAXMSGSIZE];
572 memset(msgbuf, 0 , sizeof(msgbuf));
573 msglen = recv(sock, msgbuf, sizeof(msgbuf), 0);
577 xlog(D_GENERAL, "Received packet...");
579 memset(&xdr, 0, sizeof(xdr));
580 xdrmem_create(&xdr, msgbuf, (unsigned int)msglen, XDR_DECODE);
581 xid = nsm_parse_reply(&xdr);
585 /* Before we look at the data, find the host struct for
587 if ((hp = find_host(xid)) == NULL)
590 sap = hp->ai->ai_addr;
591 if (nfs_get_port(sap) == 0)
592 recv_rpcbind_reply(sap, hp, &xdr);
594 recv_notify_reply(hp);
601 * Insert host into sorted list
604 insert_host(struct nsm_host *host)
606 struct nsm_host **where, *p;
609 while ((p = *where) != 0) {
610 /* Sort in ascending order of timeout */
611 if (host->send_next < p->send_next)
613 /* If we have the same timeout, put the
614 * most recently used host first.
615 * This makes sure that "recent" hosts
616 * get notified first.
618 if (host->send_next == p->send_next
619 && host->last_used > p->last_used)
629 * Find host given the XID
631 static struct nsm_host *
632 find_host(uint32_t xid)
634 struct nsm_host **where, *p;
637 while ((p = *where) != 0) {
648 * Record pid in /var/run/sm-notify.pid
649 * This file should remain until a reboot, even if the
651 * If file already exists, fail.
653 static int record_pid(void)
659 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
660 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
664 len = write(fd, pid, strlen(pid));
665 if ((len < 0) || ((size_t)len != strlen(pid))) {
666 xlog_warn("Writing to pid file failed: errno %d (%m)",