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 #ifndef HAVE_DECL_AI_ADDRCONFIG
38 #define AI_ADDRCONFIG 0
42 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
45 struct nsm_host * next;
57 static char nsm_hostname[256];
59 static int nsm_family = AF_INET;
60 static int opt_debug = 0;
61 static _Bool opt_update_state = true;
62 static unsigned int opt_max_retry = 15 * 60;
63 static char * opt_srcaddr = NULL;
64 static char * opt_srcport = NULL;
66 static void notify(const int sock);
67 static int notify_host(int, struct nsm_host *);
68 static void recv_reply(int);
69 static void insert_host(struct nsm_host *);
70 static struct nsm_host *find_host(uint32_t);
71 static int record_pid(void);
73 static struct nsm_host * hosts = NULL;
76 static struct addrinfo *
77 smn_lookup(const char *name)
79 struct addrinfo *ai = NULL;
80 struct addrinfo hint = {
81 .ai_flags = AI_ADDRCONFIG,
82 .ai_family = (nsm_family == AF_INET ? AF_INET: AF_UNSPEC),
83 .ai_protocol = (int)IPPROTO_UDP,
87 error = getaddrinfo(name, NULL, &hint, &ai);
89 xlog(D_GENERAL, "getaddrinfo(3): %s", gai_strerror(error));
97 static struct nsm_host *
98 smn_alloc_host(const char *hostname, const char *mon_name,
99 const char *my_name, const time_t timestamp)
101 struct nsm_host *host;
103 host = calloc(1, sizeof(*host));
107 host->name = strdup(hostname);
108 host->mon_name = strdup(mon_name);
109 host->my_name = strdup(my_name);
110 if (host->name == NULL ||
111 host->mon_name == NULL ||
112 host->my_name == NULL) {
114 free(host->mon_name);
120 host->last_used = timestamp;
121 host->timeout = NSM_TIMEOUT;
122 host->retries = 100; /* force address retry */
127 xlog_warn("Unable to allocate memory");
131 static void smn_forget_host(struct nsm_host *host)
133 xlog(D_CALL, "Removing %s from notify list", host->name);
135 nsm_delete_notified_host(host->name);
138 free(host->mon_name);
141 freeaddrinfo(host->ai);
147 smn_get_host(const char *hostname,
148 __attribute__ ((unused)) const struct sockaddr *sap,
149 const struct mon *m, const time_t timestamp)
151 struct nsm_host *host;
153 host = smn_alloc_host(hostname,
154 m->mon_id.mon_name, m->mon_id.my_id.my_name, timestamp);
159 xlog(D_GENERAL, "Added host %s to notify list", hostname);
163 #ifdef IPV6_SUPPORTED
164 static int smn_socket(void)
169 * Use an AF_INET socket if IPv6 is disabled on the
172 sock = socket(AF_INET6, SOCK_DGRAM, 0);
174 if (errno != EAFNOSUPPORT) {
175 xlog(L_ERROR, "Failed to create RPC socket: %m");
178 sock = socket(AF_INET, SOCK_DGRAM, 0);
180 xlog(L_ERROR, "Failed to create RPC socket: %m");
184 nsm_family = AF_INET6;
186 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
187 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
192 * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4. However,
193 * since sm-notify open-codes all of its RPC support, it can
194 * use a single socket and let the local network stack provide
195 * the correct mapping between address families automatically.
196 * This is the same thing that is done in the kernel.
198 if (nsm_family == AF_INET6) {
200 socklen_t zerolen = (socklen_t)sizeof(zero);
202 if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
203 (char *)&zero, zerolen) == -1) {
204 xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
215 #else /* !IPV6_SUPPORTED */
216 static int smn_socket(void)
220 sock = socket(AF_INET, SOCK_DGRAM, 0);
222 xlog(L_ERROR, "Failed to create RPC socket: %m");
226 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
227 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
234 #endif /* !IPV6_SUPPORTED */
237 * If admin specified a source address or srcport, then convert those
238 * to a sockaddr and return it. Otherwise, return an ANYADDR address.
241 static struct addrinfo *
242 smn_bind_address(const char *srcaddr, const char *srcport)
244 struct addrinfo *ai = NULL;
245 struct addrinfo hint = {
246 .ai_flags = AI_NUMERICSERV,
247 .ai_family = nsm_family,
248 .ai_protocol = (int)IPPROTO_UDP,
253 hint.ai_flags |= AI_PASSIVE;
256 error = getaddrinfo(srcaddr, "", &hint, &ai);
258 error = getaddrinfo(srcaddr, srcport, &hint, &ai);
261 "Invalid bind address or port for RPC socket: %s",
262 gai_strerror(error));
271 smn_bindresvport(int sock, struct sockaddr *sap)
273 return bindresvport_sa(sock, sap);
276 #else /* !HAVE_LIBTIRPC */
278 smn_bindresvport(int sock, struct sockaddr *sap)
280 if (sap->sa_family != AF_INET) {
281 errno = EAFNOSUPPORT;
285 return bindresvport(sock, (struct sockaddr_in *)(char *)sap);
287 #endif /* !HAVE_LIBTIRPC */
290 * Prepare a socket for sending RPC requests
292 * Returns a bound datagram socket file descriptor, or -1 if
296 smn_create_socket(const char *srcaddr, const char *srcport)
298 int sock, retry_cnt = 0;
306 ai = smn_bind_address(srcaddr, srcport);
312 /* Use source port if provided on the command line,
313 * otherwise use bindresvport */
315 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
316 xlog(L_ERROR, "Failed to bind RPC socket: %m");
324 if (smn_bindresvport(sock, ai->ai_addr) == -1) {
326 "bindresvport on RPC socket failed: %m");
332 /* try to avoid known ports */
333 se = getservbyport((int)nfs_get_port(ai->ai_addr), "udp");
334 if (se != NULL && retry_cnt < 100) {
347 main(int argc, char **argv)
349 int c, sock, force = 0;
352 progname = strrchr(argv[0], '/');
353 if (progname != NULL)
358 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
367 opt_max_retry = atoi(optarg) * 60;
370 opt_update_state = false;
373 opt_srcport = optarg;
376 opt_srcaddr = optarg;
379 if (!nsm_setup_pathnames(argv[0], optarg))
389 usage: fprintf(stderr,
390 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
391 " [-P /path/to/state/directory] [-v my_host_name]\n",
399 xlog_config(D_ALL, 1);
404 xlog(L_NOTICE, "Version " VERSION " starting");
406 if (nsm_is_default_parentdir()) {
407 if (record_pid() == 0 && force == 0 && opt_update_state) {
408 /* already run, don't try again */
409 xlog(L_NOTICE, "Already notifying clients; Exiting!");
415 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
417 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
418 xlog(L_ERROR, "Failed to obtain name of local host: %m");
422 (void)nsm_retire_monitored_hosts();
423 if (nsm_load_notify_list(smn_get_host) == 0) {
424 xlog(D_GENERAL, "No hosts to notify; exiting");
428 nsm_state = nsm_get_state(opt_update_state);
431 nsm_update_kernel_state(nsm_state);
434 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
436 if (daemon(0, 0) < 0) {
437 xlog(L_ERROR, "unable to background: %m");
446 sock = smn_create_socket(opt_srcaddr, opt_srcport);
450 if (!nsm_drop_privileges(-1))
458 while ((hp = hosts) != 0) {
460 xlog(L_NOTICE, "Unable to notify %s, giving up",
473 notify(const int sock)
478 failtime = time(NULL) + opt_max_retry;
482 time_t now = time(NULL);
483 unsigned int sent = 0;
487 if (failtime && now >= failtime)
490 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
491 /* Never send more than 10 packets at once */
495 /* Remove queue head */
499 if (notify_host(sock, hp))
502 /* Set the timeout for this call, using an
503 exponential timeout strategy */
505 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
506 hp->timeout = NSM_MAX_TIMEOUT;
507 hp->send_next = now + wait;
515 xlog(D_GENERAL, "Host %s due in %ld seconds",
524 if (poll(&pfd, 1, wait) != 1)
532 * Send notification to a single host
535 notify_host(int sock, struct nsm_host *host)
537 struct sockaddr *sap;
540 if (host->ai == NULL) {
541 host->ai = smn_lookup(host->name);
542 if (host->ai == NULL) {
543 xlog_warn("DNS resolution of %s failed; "
544 "retrying later", host->name);
549 /* If we retransmitted 4 times, reset the port to force
550 * a new portmap lookup (in case statd was restarted).
551 * We also rotate through multiple IP addresses at this
554 if (host->retries >= 4) {
555 /* don't rotate if there is only one addrinfo */
556 if (host->ai->ai_next != NULL) {
557 struct addrinfo *first = host->ai;
558 struct addrinfo **next = &host->ai;
560 /* remove the first entry from the list */
561 host->ai = first->ai_next;
562 first->ai_next = NULL;
563 /* find the end of the list */
564 next = &first->ai_next;
566 next = & (*next)->ai_next;
567 /* put first entry at end */
571 nfs_set_port(host->ai->ai_addr, 0);
575 sap = host->ai->ai_addr;
576 salen = host->ai->ai_addrlen;
578 if (nfs_get_port(sap) == 0)
579 host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
581 host->xid = nsm_xmit_notify(sock, sap, salen,
582 SM_PROG, nsm_hostname, nsm_state);
588 * Extract the returned port number and set up the SM_NOTIFY call.
591 recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
593 uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
595 host->send_next = time(NULL);
599 /* No binding for statd... */
600 xlog(D_GENERAL, "No statd on host %s", host->name);
601 host->timeout = NSM_MAX_TIMEOUT;
602 host->send_next += NSM_MAX_TIMEOUT;
604 nfs_set_port(sap, port);
605 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
606 host->timeout = NSM_MAX_TIMEOUT / 4;
613 * Successful NOTIFY call. Server returns void, so nothing
614 * we need to do here.
617 recv_notify_reply(struct nsm_host *host)
619 xlog(D_GENERAL, "Host %s notified successfully", host->name);
621 smn_forget_host(host);
625 * Receive reply from remote host
631 struct sockaddr *sap;
632 char msgbuf[NSM_MAXMSGSIZE];
637 memset(msgbuf, 0 , sizeof(msgbuf));
638 msglen = recv(sock, msgbuf, sizeof(msgbuf), 0);
642 xlog(D_GENERAL, "Received packet...");
644 memset(&xdr, 0, sizeof(xdr));
645 xdrmem_create(&xdr, msgbuf, (unsigned int)msglen, XDR_DECODE);
646 xid = nsm_parse_reply(&xdr);
650 /* Before we look at the data, find the host struct for
652 if ((hp = find_host(xid)) == NULL)
655 sap = hp->ai->ai_addr;
656 if (nfs_get_port(sap) == 0)
657 recv_rpcbind_reply(sap, hp, &xdr);
659 recv_notify_reply(hp);
666 * Insert host into sorted list
669 insert_host(struct nsm_host *host)
671 struct nsm_host **where, *p;
674 while ((p = *where) != 0) {
675 /* Sort in ascending order of timeout */
676 if (host->send_next < p->send_next)
678 /* If we have the same timeout, put the
679 * most recently used host first.
680 * This makes sure that "recent" hosts
681 * get notified first.
683 if (host->send_next == p->send_next
684 && host->last_used > p->last_used)
694 * Find host given the XID
696 static struct nsm_host *
697 find_host(uint32_t xid)
699 struct nsm_host **where, *p;
702 while ((p = *where) != 0) {
713 * Record pid in /var/run/sm-notify.pid
714 * This file should remain until a reboot, even if the
716 * If file already exists, fail.
718 static int record_pid(void)
724 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
725 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
729 len = write(fd, pid, strlen(pid));
730 if ((len < 0) || ((size_t)len != strlen(pid))) {
731 xlog_warn("Writing to pid file failed: errno %d (%m)",