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 #ifndef HAVE_DECL_AI_ADDRCONFIG
37 #define AI_ADDRCONFIG 0
41 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
44 struct nsm_host * next;
54 static char nsm_hostname[256];
56 static int nsm_family = AF_INET;
57 static int opt_debug = 0;
58 static _Bool opt_update_state = true;
59 static unsigned int opt_max_retry = 15 * 60;
60 static char * opt_srcaddr = NULL;
61 static char * opt_srcport = NULL;
63 static void notify(const int sock);
64 static int notify_host(int, struct nsm_host *);
65 static void recv_reply(int);
66 static void insert_host(struct nsm_host *);
67 static struct nsm_host *find_host(uint32_t);
68 static int record_pid(void);
70 static struct nsm_host * hosts = NULL;
73 static struct addrinfo *
74 smn_lookup(const char *name)
76 struct addrinfo *ai = NULL;
77 struct addrinfo hint = {
78 .ai_flags = AI_ADDRCONFIG,
79 .ai_family = (nsm_family == AF_INET ? AF_INET: AF_UNSPEC),
80 .ai_protocol = (int)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);
150 #ifdef IPV6_SUPPORTED
151 static int smn_socket(void)
156 * Use an AF_INET socket if IPv6 is disabled on the
159 sock = socket(AF_INET6, SOCK_DGRAM, 0);
161 if (errno != EAFNOSUPPORT) {
162 xlog(L_ERROR, "Failed to create RPC socket: %m");
165 sock = socket(AF_INET, SOCK_DGRAM, 0);
167 xlog(L_ERROR, "Failed to create RPC socket: %m");
171 nsm_family = AF_INET6;
173 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
174 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
179 * TI-RPC over IPv6 (udp6/tcp6) does not handle IPv4. However,
180 * since sm-notify open-codes all of its RPC support, it can
181 * use a single socket and let the local network stack provide
182 * the correct mapping between address families automatically.
183 * This is the same thing that is done in the kernel.
185 if (nsm_family == AF_INET6) {
187 socklen_t zerolen = (socklen_t)sizeof(zero);
189 if (setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
190 (char *)&zero, zerolen) == -1) {
191 xlog(L_ERROR, "setsockopt(3) on RPC socket failed: %m");
202 #else /* !IPV6_SUPPORTED */
203 static int smn_socket(void)
207 sock = socket(AF_INET, SOCK_DGRAM, 0);
209 xlog(L_ERROR, "Failed to create RPC socket: %m");
213 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
214 xlog(L_ERROR, "fcntl(3) on RPC socket failed: %m");
221 #endif /* !IPV6_SUPPORTED */
224 * If admin specified a source address or srcport, then convert those
225 * to a sockaddr and return it. Otherwise, return an ANYADDR address.
228 static struct addrinfo *
229 smn_bind_address(const char *srcaddr, const char *srcport)
231 struct addrinfo *ai = NULL;
232 struct addrinfo hint = {
233 .ai_flags = AI_NUMERICSERV,
234 .ai_family = nsm_family,
235 .ai_protocol = (int)IPPROTO_UDP,
240 hint.ai_flags |= AI_PASSIVE;
243 error = getaddrinfo(srcaddr, "", &hint, &ai);
245 error = getaddrinfo(srcaddr, srcport, &hint, &ai);
248 "Invalid bind address or port for RPC socket: %s",
249 gai_strerror(error));
258 smn_bindresvport(int sock, struct sockaddr *sap)
260 return bindresvport_sa(sock, sap);
263 #else /* !HAVE_LIBTIRPC */
265 smn_bindresvport(int sock, struct sockaddr *sap)
267 if (sap->sa_family != AF_INET) {
268 errno = EAFNOSUPPORT;
272 return bindresvport(sock, (struct sockaddr_in *)(char *)sap);
274 #endif /* !HAVE_LIBTIRPC */
277 * Prepare a socket for sending RPC requests
279 * Returns a bound datagram socket file descriptor, or -1 if
283 smn_create_socket(const char *srcaddr, const char *srcport)
285 int sock, retry_cnt = 0;
293 ai = smn_bind_address(srcaddr, srcport);
299 /* Use source port if provided on the command line,
300 * otherwise use bindresvport */
302 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
303 xlog(L_ERROR, "Failed to bind RPC socket: %m");
311 if (smn_bindresvport(sock, ai->ai_addr) == -1) {
313 "bindresvport on RPC socket failed: %m");
319 /* try to avoid known ports */
320 se = getservbyport((int)nfs_get_port(ai->ai_addr), "udp");
321 if (se != NULL && retry_cnt < 100) {
334 main(int argc, char **argv)
336 int c, sock, force = 0;
339 progname = strrchr(argv[0], '/');
340 if (progname != NULL)
345 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
354 opt_max_retry = atoi(optarg) * 60;
357 opt_update_state = false;
360 opt_srcport = optarg;
363 opt_srcaddr = optarg;
366 if (!nsm_setup_pathnames(argv[0], optarg))
376 usage: fprintf(stderr,
377 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
378 " [-P /path/to/state/directory] [-v my_host_name]\n",
386 xlog_config(D_ALL, 1);
391 xlog(L_NOTICE, "Version " VERSION " starting");
393 if (nsm_is_default_parentdir()) {
394 if (record_pid() == 0 && force == 0 && opt_update_state) {
395 /* already run, don't try again */
396 xlog(L_NOTICE, "Already notifying clients; Exiting!");
402 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
404 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
405 xlog(L_ERROR, "Failed to obtain name of local host: %m");
409 (void)nsm_retire_monitored_hosts();
410 if (nsm_load_notify_list(smn_get_host) == 0) {
411 xlog(D_GENERAL, "No hosts to notify; exiting");
415 nsm_state = nsm_get_state(opt_update_state);
418 nsm_update_kernel_state(nsm_state);
421 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
423 if (daemon(0, 0) < 0) {
424 xlog(L_ERROR, "unable to background: %m");
433 sock = smn_create_socket(opt_srcaddr, opt_srcport);
437 if (!nsm_drop_privileges(-1))
445 while ((hp = hosts) != 0) {
447 xlog(L_NOTICE, "Unable to notify %s, giving up",
460 notify(const int sock)
465 failtime = time(NULL) + opt_max_retry;
469 time_t now = time(NULL);
470 unsigned int sent = 0;
474 if (failtime && now >= failtime)
477 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
478 /* Never send more than 10 packets at once */
482 /* Remove queue head */
486 if (notify_host(sock, hp))
489 /* Set the timeout for this call, using an
490 exponential timeout strategy */
492 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
493 hp->timeout = NSM_MAX_TIMEOUT;
494 hp->send_next = now + wait;
502 xlog(D_GENERAL, "Host %s due in %ld seconds",
511 if (poll(&pfd, 1, wait) != 1)
519 * Send notification to a single host
522 notify_host(int sock, struct nsm_host *host)
524 struct sockaddr *sap;
527 if (host->ai == NULL) {
528 host->ai = smn_lookup(host->name);
529 if (host->ai == NULL) {
530 xlog_warn("DNS resolution of %s failed; "
531 "retrying later", host->name);
536 /* If we retransmitted 4 times, reset the port to force
537 * a new portmap lookup (in case statd was restarted).
538 * We also rotate through multiple IP addresses at this
541 if (host->retries >= 4) {
542 /* don't rotate if there is only one addrinfo */
543 if (host->ai->ai_next != NULL) {
544 struct addrinfo *first = host->ai;
545 struct addrinfo **next = &host->ai;
547 /* remove the first entry from the list */
548 host->ai = first->ai_next;
549 first->ai_next = NULL;
550 /* find the end of the list */
551 next = &first->ai_next;
553 next = & (*next)->ai_next;
554 /* put first entry at end */
558 nfs_set_port(host->ai->ai_addr, 0);
562 sap = host->ai->ai_addr;
563 salen = host->ai->ai_addrlen;
565 if (nfs_get_port(sap) == 0)
566 host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
568 host->xid = nsm_xmit_notify(sock, sap, salen,
569 SM_PROG, nsm_hostname, nsm_state);
575 * Extract the returned port number and set up the SM_NOTIFY call.
578 recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
580 uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
582 host->send_next = time(NULL);
586 /* No binding for statd... */
587 xlog(D_GENERAL, "No statd on host %s", host->name);
588 host->timeout = NSM_MAX_TIMEOUT;
589 host->send_next += NSM_MAX_TIMEOUT;
591 nfs_set_port(sap, port);
592 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
593 host->timeout = NSM_MAX_TIMEOUT / 4;
600 * Successful NOTIFY call. Server returns void, so nothing
601 * we need to do here.
604 recv_notify_reply(struct nsm_host *host)
606 xlog(D_GENERAL, "Host %s notified successfully", host->name);
608 smn_forget_host(host);
612 * Receive reply from remote host
618 struct sockaddr *sap;
619 char msgbuf[NSM_MAXMSGSIZE];
624 memset(msgbuf, 0 , sizeof(msgbuf));
625 msglen = recv(sock, msgbuf, sizeof(msgbuf), 0);
629 xlog(D_GENERAL, "Received packet...");
631 memset(&xdr, 0, sizeof(xdr));
632 xdrmem_create(&xdr, msgbuf, (unsigned int)msglen, XDR_DECODE);
633 xid = nsm_parse_reply(&xdr);
637 /* Before we look at the data, find the host struct for
639 if ((hp = find_host(xid)) == NULL)
642 sap = hp->ai->ai_addr;
643 if (nfs_get_port(sap) == 0)
644 recv_rpcbind_reply(sap, hp, &xdr);
646 recv_notify_reply(hp);
653 * Insert host into sorted list
656 insert_host(struct nsm_host *host)
658 struct nsm_host **where, *p;
661 while ((p = *where) != 0) {
662 /* Sort in ascending order of timeout */
663 if (host->send_next < p->send_next)
665 /* If we have the same timeout, put the
666 * most recently used host first.
667 * This makes sure that "recent" hosts
668 * get notified first.
670 if (host->send_next == p->send_next
671 && host->last_used > p->last_used)
681 * Find host given the XID
683 static struct nsm_host *
684 find_host(uint32_t xid)
686 struct nsm_host **where, *p;
689 while ((p = *where) != 0) {
700 * Record pid in /var/run/sm-notify.pid
701 * This file should remain until a reboot, even if the
703 * If file already exists, fail.
705 static int record_pid(void)
711 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
712 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
716 len = write(fd, pid, strlen(pid));
717 if ((len < 0) || ((size_t)len != strlen(pid))) {
718 xlog_warn("Writing to pid file failed: errno %d (%m)",