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 */
220 smn_bindresvport(int sock, struct sockaddr *sap)
222 return bindresvport_sa(sock, sap);
225 #else /* !HAVE_LIBTIRPC */
227 smn_bindresvport(int sock, struct sockaddr *sap)
229 if (sap->sa_family != AF_INET) {
230 errno = EAFNOSUPPORT;
234 return bindresvport(sock, (struct sockaddr_in *)(char *)sap);
236 #endif /* !HAVE_LIBTIRPC */
239 * Prepare a socket for sending RPC requests
241 * Returns a bound datagram socket file descriptor, or -1 if
245 smn_create_socket(const char *srcaddr, const uint16_t srcport)
247 struct sockaddr_storage address;
248 struct sockaddr *local_addr = (struct sockaddr *)&address;
249 int sock, retry_cnt = 0;
256 memset(&address, 0, sizeof(address));
257 local_addr->sa_family = AF_INET; /* Default to IPv4 */
259 /* Bind source IP if provided on command line */
261 struct addrinfo *ai = smn_lookup(srcaddr);
264 "Not a valid hostname or address: \"%s\"",
270 /* We know it's IPv4 at this point */
271 memcpy(local_addr, ai->ai_addr, ai->ai_addrlen);
276 /* Use source port if provided on the command line,
277 * otherwise use bindresvport */
279 nfs_set_port(local_addr, srcport);
280 if (bind(sock, local_addr, sizeof(struct sockaddr_in)) < 0) {
281 xlog(L_ERROR, "Failed to bind RPC socket: %m");
287 struct sockaddr_in *sin = (struct sockaddr_in *)local_addr;
289 if (smn_bindresvport(sock, local_addr) == -1) {
291 "bindresvport on RPC socket failed: %m");
296 /* try to avoid known ports */
297 se = getservbyport(sin->sin_port, "udp");
298 if (se && retry_cnt < 100) {
309 main(int argc, char **argv)
311 int c, sock, force = 0;
314 progname = strrchr(argv[0], '/');
315 if (progname != NULL)
320 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
329 opt_max_retry = atoi(optarg) * 60;
332 opt_update_state = false;
335 opt_srcport = atoi(optarg);
338 opt_srcaddr = optarg;
341 if (!nsm_setup_pathnames(argv[0], optarg))
351 usage: fprintf(stderr,
352 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
353 " [-P /path/to/state/directory] [-v my_host_name]\n",
361 xlog_config(D_ALL, 1);
366 xlog(L_NOTICE, "Version " VERSION " starting");
368 if (nsm_is_default_parentdir()) {
369 if (record_pid() == 0 && force == 0 && opt_update_state) {
370 /* already run, don't try again */
371 xlog(L_NOTICE, "Already notifying clients; Exiting!");
377 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
379 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
380 xlog(L_ERROR, "Failed to obtain name of local host: %m");
384 (void)nsm_retire_monitored_hosts();
385 if (nsm_load_notify_list(smn_get_host) == 0) {
386 xlog(D_GENERAL, "No hosts to notify; exiting");
390 nsm_state = nsm_get_state(opt_update_state);
393 nsm_update_kernel_state(nsm_state);
396 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
398 if (daemon(0, 0) < 0) {
399 xlog(L_ERROR, "unable to background: %m");
408 sock = smn_create_socket(opt_srcaddr, opt_srcport);
412 if (!nsm_drop_privileges(-1))
420 while ((hp = hosts) != 0) {
422 xlog(L_NOTICE, "Unable to notify %s, giving up",
435 notify(const int sock)
440 failtime = time(NULL) + opt_max_retry;
444 time_t now = time(NULL);
445 unsigned int sent = 0;
449 if (failtime && now >= failtime)
452 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
453 /* Never send more than 10 packets at once */
457 /* Remove queue head */
461 if (notify_host(sock, hp))
464 /* Set the timeout for this call, using an
465 exponential timeout strategy */
467 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
468 hp->timeout = NSM_MAX_TIMEOUT;
469 hp->send_next = now + wait;
477 xlog(D_GENERAL, "Host %s due in %ld seconds",
486 if (poll(&pfd, 1, wait) != 1)
494 * Send notification to a single host
497 notify_host(int sock, struct nsm_host *host)
499 struct sockaddr *sap;
502 if (host->ai == NULL) {
503 host->ai = smn_lookup(host->name);
504 if (host->ai == NULL) {
505 xlog_warn("DNS resolution of %s failed; "
506 "retrying later", host->name);
511 /* If we retransmitted 4 times, reset the port to force
512 * a new portmap lookup (in case statd was restarted).
513 * We also rotate through multiple IP addresses at this
516 if (host->retries >= 4) {
517 /* don't rotate if there is only one addrinfo */
518 if (host->ai->ai_next != NULL) {
519 struct addrinfo *first = host->ai;
520 struct addrinfo **next = &host->ai;
522 /* remove the first entry from the list */
523 host->ai = first->ai_next;
524 first->ai_next = NULL;
525 /* find the end of the list */
526 next = &first->ai_next;
528 next = & (*next)->ai_next;
529 /* put first entry at end */
533 nfs_set_port(host->ai->ai_addr, 0);
537 sap = host->ai->ai_addr;
538 salen = host->ai->ai_addrlen;
540 if (nfs_get_port(sap) == 0)
541 host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
543 host->xid = nsm_xmit_notify(sock, sap, salen,
544 SM_PROG, nsm_hostname, nsm_state);
550 * Extract the returned port number and set up the SM_NOTIFY call.
553 recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
555 uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
557 host->send_next = time(NULL);
561 /* No binding for statd... */
562 xlog(D_GENERAL, "No statd on host %s", host->name);
563 host->timeout = NSM_MAX_TIMEOUT;
564 host->send_next += NSM_MAX_TIMEOUT;
566 nfs_set_port(sap, port);
567 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
568 host->timeout = NSM_MAX_TIMEOUT / 4;
575 * Successful NOTIFY call. Server returns void, so nothing
576 * we need to do here.
579 recv_notify_reply(struct nsm_host *host)
581 xlog(D_GENERAL, "Host %s notified successfully", host->name);
583 smn_forget_host(host);
587 * Receive reply from remote host
593 struct sockaddr *sap;
594 char msgbuf[NSM_MAXMSGSIZE];
599 memset(msgbuf, 0 , sizeof(msgbuf));
600 msglen = recv(sock, msgbuf, sizeof(msgbuf), 0);
604 xlog(D_GENERAL, "Received packet...");
606 memset(&xdr, 0, sizeof(xdr));
607 xdrmem_create(&xdr, msgbuf, (unsigned int)msglen, XDR_DECODE);
608 xid = nsm_parse_reply(&xdr);
612 /* Before we look at the data, find the host struct for
614 if ((hp = find_host(xid)) == NULL)
617 sap = hp->ai->ai_addr;
618 if (nfs_get_port(sap) == 0)
619 recv_rpcbind_reply(sap, hp, &xdr);
621 recv_notify_reply(hp);
628 * Insert host into sorted list
631 insert_host(struct nsm_host *host)
633 struct nsm_host **where, *p;
636 while ((p = *where) != 0) {
637 /* Sort in ascending order of timeout */
638 if (host->send_next < p->send_next)
640 /* If we have the same timeout, put the
641 * most recently used host first.
642 * This makes sure that "recent" hosts
643 * get notified first.
645 if (host->send_next == p->send_next
646 && host->last_used > p->last_used)
656 * Find host given the XID
658 static struct nsm_host *
659 find_host(uint32_t xid)
661 struct nsm_host **where, *p;
664 while ((p = *where) != 0) {
675 * Record pid in /var/run/sm-notify.pid
676 * This file should remain until a reboot, even if the
678 * If file already exists, fail.
680 static int record_pid(void)
686 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
687 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
691 len = write(fd, pid, strlen(pid));
692 if ((len < 0) || ((size_t)len != strlen(pid))) {
693 xlog_warn("Writing to pid file failed: errno %d (%m)",