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>
38 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
41 struct nsm_host * next;
53 static char nsm_hostname[SM_MAXSTRLEN + 1];
55 static int nsm_family = AF_INET;
56 static int opt_debug = 0;
57 static _Bool opt_update_state = true;
58 static unsigned int opt_max_retry = 15 * 60;
59 static char * opt_srcaddr = NULL;
60 static char * opt_srcport = NULL;
62 static void notify(const int sock);
63 static int notify_host(int, struct nsm_host *);
64 static void recv_reply(int);
65 static void insert_host(struct nsm_host *);
66 static struct nsm_host *find_host(uint32_t);
67 static int record_pid(void);
69 static struct nsm_host * hosts = NULL;
72 static struct addrinfo *
73 smn_lookup(const char *name)
75 struct addrinfo *ai = NULL;
76 struct addrinfo hint = {
77 .ai_family = (nsm_family == AF_INET ? AF_INET: AF_UNSPEC),
78 .ai_protocol = (int)IPPROTO_UDP,
82 error = getaddrinfo(name, NULL, &hint, &ai);
84 xlog(D_GENERAL, "getaddrinfo(3): %s", gai_strerror(error));
92 static struct nsm_host *
93 smn_alloc_host(const char *hostname, const char *mon_name,
94 const char *my_name, const time_t timestamp)
96 struct nsm_host *host;
98 host = calloc(1, sizeof(*host));
102 host->name = strdup(hostname);
103 host->mon_name = strdup(mon_name);
104 host->my_name = strdup(my_name);
105 if (host->name == NULL ||
106 host->mon_name == NULL ||
107 host->my_name == NULL) {
109 free(host->mon_name);
115 host->last_used = timestamp;
116 host->timeout = NSM_TIMEOUT;
117 host->retries = 100; /* force address retry */
122 xlog_warn("Unable to allocate memory");
126 static void smn_forget_host(struct nsm_host *host)
128 xlog(D_CALL, "Removing %s (%s, %s) from notify list",
129 host->name, host->mon_name, host->my_name);
131 nsm_delete_notified_host(host->name, host->mon_name, host->my_name);
134 free(host->mon_name);
137 freeaddrinfo(host->ai);
143 smn_get_host(const char *hostname,
144 __attribute__ ((unused)) const struct sockaddr *sap,
145 const struct mon *m, const time_t timestamp)
147 struct nsm_host *host;
149 host = smn_alloc_host(hostname,
150 m->mon_id.mon_name, m->mon_id.my_id.my_name, timestamp);
155 xlog(D_GENERAL, "Added host %s to notify list", hostname);
159 #ifdef IPV6_SUPPORTED
160 static int smn_socket(void)
165 * Use an AF_INET socket if IPv6 is disabled on the
168 sock = socket(AF_INET6, SOCK_DGRAM, 0);
170 if (errno != EAFNOSUPPORT) {
171 xlog(L_ERROR, "Failed to create RPC socket: %m");
174 sock = socket(AF_INET, SOCK_DGRAM, 0);
176 xlog(L_ERROR, "Failed to create RPC socket: %m");
180 nsm_family = AF_INET6;
182 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
183 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
188 * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4. However,
189 * since sm-notify open-codes all of its RPC support, it can
190 * use a single socket and let the local network stack provide
191 * the correct mapping between address families automatically.
192 * This is the same thing that is done in the kernel.
194 if (nsm_family == AF_INET6) {
196 socklen_t zerolen = (socklen_t)sizeof(zero);
198 if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
199 (char *)&zero, zerolen) == -1) {
200 xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
211 #else /* !IPV6_SUPPORTED */
212 static int smn_socket(void)
216 sock = socket(AF_INET, SOCK_DGRAM, 0);
218 xlog(L_ERROR, "Failed to create RPC socket: %m");
222 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
223 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
230 #endif /* !IPV6_SUPPORTED */
233 * If admin specified a source address or srcport, then convert those
234 * to a sockaddr and return it. Otherwise, return an ANYADDR address.
237 static struct addrinfo *
238 smn_bind_address(const char *srcaddr, const char *srcport)
240 struct addrinfo *ai = NULL;
241 struct addrinfo hint = {
242 .ai_flags = AI_NUMERICSERV,
243 .ai_family = nsm_family,
244 .ai_protocol = (int)IPPROTO_UDP,
249 hint.ai_flags |= AI_PASSIVE;
252 error = getaddrinfo(srcaddr, "", &hint, &ai);
254 error = getaddrinfo(srcaddr, srcport, &hint, &ai);
257 "Invalid bind address or port for RPC socket: %s",
258 gai_strerror(error));
267 smn_bindresvport(int sock, struct sockaddr *sap)
269 return bindresvport_sa(sock, sap);
272 #else /* !HAVE_LIBTIRPC */
274 smn_bindresvport(int sock, struct sockaddr *sap)
276 if (sap->sa_family != AF_INET) {
277 errno = EAFNOSUPPORT;
281 return bindresvport(sock, (struct sockaddr_in *)(char *)sap);
283 #endif /* !HAVE_LIBTIRPC */
286 * Prepare a socket for sending RPC requests
288 * Returns a bound datagram socket file descriptor, or -1 if
292 smn_create_socket(const char *srcaddr, const char *srcport)
294 int sock, retry_cnt = 0;
302 ai = smn_bind_address(srcaddr, srcport);
308 /* Use source port if provided on the command line,
309 * otherwise use bindresvport */
311 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
312 xlog(L_ERROR, "Failed to bind RPC socket: %m");
320 if (smn_bindresvport(sock, ai->ai_addr) == -1) {
322 "bindresvport on RPC socket failed: %m");
328 /* try to avoid known ports */
329 se = getservbyport((int)nfs_get_port(ai->ai_addr), "udp");
330 if (se != NULL && retry_cnt < 100) {
343 main(int argc, char **argv)
345 int c, sock, force = 0;
348 progname = strrchr(argv[0], '/');
349 if (progname != NULL)
354 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
363 opt_max_retry = atoi(optarg) * 60;
366 opt_update_state = false;
369 opt_srcport = optarg;
372 opt_srcaddr = optarg;
375 if (!nsm_setup_pathnames(argv[0], optarg))
385 usage: fprintf(stderr,
386 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
387 " [-P /path/to/state/directory] [-v my_host_name]\n",
395 xlog_config(D_ALL, 1);
400 xlog(L_NOTICE, "Version " VERSION " starting");
402 if (nsm_is_default_parentdir()) {
403 if (record_pid() == 0 && force == 0 && opt_update_state) {
404 /* already run, don't try again */
405 xlog(L_NOTICE, "Already notifying clients; Exiting!");
410 if (opt_srcaddr != NULL) {
411 struct addrinfo *ai = NULL;
412 struct addrinfo hint = {
413 .ai_family = AF_UNSPEC,
414 .ai_flags = AI_NUMERICHOST,
417 if (getaddrinfo(opt_srcaddr, NULL, &hint, &ai))
418 /* not a presentation address - use it */
419 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname));
421 /* was a presentation address - look it up in
422 * /etc/hosts, so it can be used for my_name */
426 hint.ai_flags = AI_CANONNAME;
427 error = getaddrinfo(opt_srcaddr, NULL, &hint, &ai);
429 xlog(L_ERROR, "Bind address %s is unusable: %s",
430 opt_srcaddr, gai_strerror(error));
433 strncpy(nsm_hostname, ai->ai_canonname,
434 sizeof(nsm_hostname));
439 (void)nsm_retire_monitored_hosts();
440 if (nsm_load_notify_list(smn_get_host) == 0) {
441 xlog(D_GENERAL, "No hosts to notify; exiting");
445 nsm_state = nsm_get_state(opt_update_state);
448 nsm_update_kernel_state(nsm_state);
451 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
453 if (daemon(0, 0) < 0) {
454 xlog(L_ERROR, "unable to background: %m");
463 sock = smn_create_socket(opt_srcaddr, opt_srcport);
467 if (!nsm_drop_privileges(-1))
475 while ((hp = hosts) != 0) {
477 xlog(L_NOTICE, "Unable to notify %s, giving up",
490 notify(const int sock)
495 failtime = time(NULL) + opt_max_retry;
499 time_t now = time(NULL);
500 unsigned int sent = 0;
504 if (failtime && now >= failtime)
507 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
508 /* Never send more than 10 packets at once */
512 /* Remove queue head */
516 if (notify_host(sock, hp))
519 /* Set the timeout for this call, using an
520 exponential timeout strategy */
522 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
523 hp->timeout = NSM_MAX_TIMEOUT;
524 hp->send_next = now + wait;
532 xlog(D_GENERAL, "Host %s due in %ld seconds",
541 if (poll(&pfd, 1, wait) != 1)
549 * Send notification to a single host
552 notify_host(int sock, struct nsm_host *host)
554 const char *my_name = (opt_srcaddr != NULL ?
555 nsm_hostname : host->my_name);
556 struct sockaddr *sap;
559 if (host->ai == NULL) {
560 host->ai = smn_lookup(host->name);
561 if (host->ai == NULL) {
562 xlog_warn("DNS resolution of %s failed; "
563 "retrying later", host->name);
568 /* If we retransmitted 4 times, reset the port to force
569 * a new portmap lookup (in case statd was restarted).
570 * We also rotate through multiple IP addresses at this
573 if (host->retries >= 4) {
574 /* don't rotate if there is only one addrinfo */
575 if (host->ai->ai_next != NULL) {
576 struct addrinfo *first = host->ai;
577 struct addrinfo **next = &host->ai;
579 /* remove the first entry from the list */
580 host->ai = first->ai_next;
581 first->ai_next = NULL;
582 /* find the end of the list */
583 next = &first->ai_next;
585 next = & (*next)->ai_next;
586 /* put first entry at end */
590 nfs_set_port(host->ai->ai_addr, 0);
594 sap = host->ai->ai_addr;
595 salen = host->ai->ai_addrlen;
597 if (nfs_get_port(sap) == 0)
598 host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
600 host->xid = nsm_xmit_notify(sock, sap, salen,
601 SM_PROG, my_name, nsm_state);
607 * Extract the returned port number and set up the SM_NOTIFY call.
610 recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
612 uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
614 host->send_next = time(NULL);
618 /* No binding for statd... */
619 xlog(D_GENERAL, "No statd on host %s", host->name);
620 host->timeout = NSM_MAX_TIMEOUT;
621 host->send_next += NSM_MAX_TIMEOUT;
623 nfs_set_port(sap, port);
624 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
625 host->timeout = NSM_MAX_TIMEOUT / 4;
632 * Successful NOTIFY call. Server returns void.
634 * Try sending another SM_NOTIFY with an unqualified "my_name"
635 * argument. Reuse the port number. If "my_name" is already
636 * unqualified, we're done.
639 recv_notify_reply(struct nsm_host *host)
641 char *dot = strchr(host->my_name, '.');
645 host->send_next = time(NULL);
647 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
648 host->timeout = NSM_MAX_TIMEOUT / 4;
651 xlog(D_GENERAL, "Host %s notified successfully", host->name);
652 smn_forget_host(host);
657 * Receive reply from remote host
663 struct sockaddr *sap;
664 char msgbuf[NSM_MAXMSGSIZE];
669 memset(msgbuf, 0 , sizeof(msgbuf));
670 msglen = recv(sock, msgbuf, sizeof(msgbuf), 0);
674 xlog(D_GENERAL, "Received packet...");
676 memset(&xdr, 0, sizeof(xdr));
677 xdrmem_create(&xdr, msgbuf, (unsigned int)msglen, XDR_DECODE);
678 xid = nsm_parse_reply(&xdr);
682 /* Before we look at the data, find the host struct for
684 if ((hp = find_host(xid)) == NULL)
687 sap = hp->ai->ai_addr;
688 if (nfs_get_port(sap) == 0)
689 recv_rpcbind_reply(sap, hp, &xdr);
691 recv_notify_reply(hp);
698 * Insert host into sorted list
701 insert_host(struct nsm_host *host)
703 struct nsm_host **where, *p;
706 while ((p = *where) != 0) {
707 /* Sort in ascending order of timeout */
708 if (host->send_next < p->send_next)
710 /* If we have the same timeout, put the
711 * most recently used host first.
712 * This makes sure that "recent" hosts
713 * get notified first.
715 if (host->send_next == p->send_next
716 && host->last_used > p->last_used)
726 * Find host given the XID
728 static struct nsm_host *
729 find_host(uint32_t xid)
731 struct nsm_host **where, *p;
734 while ((p = *where) != 0) {
745 * Record pid in /var/run/sm-notify.pid
746 * This file should remain until a reboot, even if the
748 * If file already exists, fail.
750 static int record_pid(void)
756 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
757 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
761 len = write(fd, pid, strlen(pid));
762 if ((len < 0) || ((size_t)len != strlen(pid))) {
763 xlog_warn("Writing to pid file failed: errno %d (%m)",