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 = NULL;
57 static char * opt_srcport = NULL;
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 * If admin specified a source address or srcport, then convert those
220 * to a sockaddr and return it. Otherwise, return an ANYADDR address.
223 static struct addrinfo *
224 smn_bind_address(const char *srcaddr, const char *srcport)
226 struct addrinfo *ai = NULL;
227 struct addrinfo hint = {
228 .ai_flags = AI_NUMERICSERV,
229 .ai_family = nsm_family,
230 .ai_protocol = (int)IPPROTO_UDP,
235 hint.ai_flags |= AI_PASSIVE;
238 error = getaddrinfo(srcaddr, "", &hint, &ai);
240 error = getaddrinfo(srcaddr, srcport, &hint, &ai);
243 "Invalid bind address or port for RPC socket: %s",
244 gai_strerror(error));
253 smn_bindresvport(int sock, struct sockaddr *sap)
255 return bindresvport_sa(sock, sap);
258 #else /* !HAVE_LIBTIRPC */
260 smn_bindresvport(int sock, struct sockaddr *sap)
262 if (sap->sa_family != AF_INET) {
263 errno = EAFNOSUPPORT;
267 return bindresvport(sock, (struct sockaddr_in *)(char *)sap);
269 #endif /* !HAVE_LIBTIRPC */
272 * Prepare a socket for sending RPC requests
274 * Returns a bound datagram socket file descriptor, or -1 if
278 smn_create_socket(const char *srcaddr, const char *srcport)
280 int sock, retry_cnt = 0;
288 ai = smn_bind_address(srcaddr, srcport);
294 /* Use source port if provided on the command line,
295 * otherwise use bindresvport */
297 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
298 xlog(L_ERROR, "Failed to bind RPC socket: %m");
306 if (smn_bindresvport(sock, ai->ai_addr) == -1) {
308 "bindresvport on RPC socket failed: %m");
314 /* try to avoid known ports */
315 se = getservbyport((int)nfs_get_port(ai->ai_addr), "udp");
316 if (se != NULL && retry_cnt < 100) {
329 main(int argc, char **argv)
331 int c, sock, force = 0;
334 progname = strrchr(argv[0], '/');
335 if (progname != NULL)
340 while ((c = getopt(argc, argv, "dm:np:v:P:f")) != -1) {
349 opt_max_retry = atoi(optarg) * 60;
352 opt_update_state = false;
355 opt_srcport = optarg;
358 opt_srcaddr = optarg;
361 if (!nsm_setup_pathnames(argv[0], optarg))
371 usage: fprintf(stderr,
372 "Usage: %s -notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
373 " [-P /path/to/state/directory] [-v my_host_name]\n",
381 xlog_config(D_ALL, 1);
386 xlog(L_NOTICE, "Version " VERSION " starting");
388 if (nsm_is_default_parentdir()) {
389 if (record_pid() == 0 && force == 0 && opt_update_state) {
390 /* already run, don't try again */
391 xlog(L_NOTICE, "Already notifying clients; Exiting!");
397 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
399 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
400 xlog(L_ERROR, "Failed to obtain name of local host: %m");
404 (void)nsm_retire_monitored_hosts();
405 if (nsm_load_notify_list(smn_get_host) == 0) {
406 xlog(D_GENERAL, "No hosts to notify; exiting");
410 nsm_state = nsm_get_state(opt_update_state);
413 nsm_update_kernel_state(nsm_state);
416 xlog(L_NOTICE, "Backgrounding to notify hosts...\n");
418 if (daemon(0, 0) < 0) {
419 xlog(L_ERROR, "unable to background: %m");
428 sock = smn_create_socket(opt_srcaddr, opt_srcport);
432 if (!nsm_drop_privileges(-1))
440 while ((hp = hosts) != 0) {
442 xlog(L_NOTICE, "Unable to notify %s, giving up",
455 notify(const int sock)
460 failtime = time(NULL) + opt_max_retry;
464 time_t now = time(NULL);
465 unsigned int sent = 0;
469 if (failtime && now >= failtime)
472 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
473 /* Never send more than 10 packets at once */
477 /* Remove queue head */
481 if (notify_host(sock, hp))
484 /* Set the timeout for this call, using an
485 exponential timeout strategy */
487 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
488 hp->timeout = NSM_MAX_TIMEOUT;
489 hp->send_next = now + wait;
497 xlog(D_GENERAL, "Host %s due in %ld seconds",
506 if (poll(&pfd, 1, wait) != 1)
514 * Send notification to a single host
517 notify_host(int sock, struct nsm_host *host)
519 struct sockaddr *sap;
522 if (host->ai == NULL) {
523 host->ai = smn_lookup(host->name);
524 if (host->ai == NULL) {
525 xlog_warn("DNS resolution of %s failed; "
526 "retrying later", host->name);
531 /* If we retransmitted 4 times, reset the port to force
532 * a new portmap lookup (in case statd was restarted).
533 * We also rotate through multiple IP addresses at this
536 if (host->retries >= 4) {
537 /* don't rotate if there is only one addrinfo */
538 if (host->ai->ai_next != NULL) {
539 struct addrinfo *first = host->ai;
540 struct addrinfo **next = &host->ai;
542 /* remove the first entry from the list */
543 host->ai = first->ai_next;
544 first->ai_next = NULL;
545 /* find the end of the list */
546 next = &first->ai_next;
548 next = & (*next)->ai_next;
549 /* put first entry at end */
553 nfs_set_port(host->ai->ai_addr, 0);
557 sap = host->ai->ai_addr;
558 salen = host->ai->ai_addrlen;
560 if (nfs_get_port(sap) == 0)
561 host->xid = nsm_xmit_rpcbind(sock, sap, SM_PROG, SM_VERS);
563 host->xid = nsm_xmit_notify(sock, sap, salen,
564 SM_PROG, nsm_hostname, nsm_state);
570 * Extract the returned port number and set up the SM_NOTIFY call.
573 recv_rpcbind_reply(struct sockaddr *sap, struct nsm_host *host, XDR *xdr)
575 uint16_t port = nsm_recv_rpcbind(sap->sa_family, xdr);
577 host->send_next = time(NULL);
581 /* No binding for statd... */
582 xlog(D_GENERAL, "No statd on host %s", host->name);
583 host->timeout = NSM_MAX_TIMEOUT;
584 host->send_next += NSM_MAX_TIMEOUT;
586 nfs_set_port(sap, port);
587 if (host->timeout >= NSM_MAX_TIMEOUT / 4)
588 host->timeout = NSM_MAX_TIMEOUT / 4;
595 * Successful NOTIFY call. Server returns void, so nothing
596 * we need to do here.
599 recv_notify_reply(struct nsm_host *host)
601 xlog(D_GENERAL, "Host %s notified successfully", host->name);
603 smn_forget_host(host);
607 * Receive reply from remote host
613 struct sockaddr *sap;
614 char msgbuf[NSM_MAXMSGSIZE];
619 memset(msgbuf, 0 , sizeof(msgbuf));
620 msglen = recv(sock, msgbuf, sizeof(msgbuf), 0);
624 xlog(D_GENERAL, "Received packet...");
626 memset(&xdr, 0, sizeof(xdr));
627 xdrmem_create(&xdr, msgbuf, (unsigned int)msglen, XDR_DECODE);
628 xid = nsm_parse_reply(&xdr);
632 /* Before we look at the data, find the host struct for
634 if ((hp = find_host(xid)) == NULL)
637 sap = hp->ai->ai_addr;
638 if (nfs_get_port(sap) == 0)
639 recv_rpcbind_reply(sap, hp, &xdr);
641 recv_notify_reply(hp);
648 * Insert host into sorted list
651 insert_host(struct nsm_host *host)
653 struct nsm_host **where, *p;
656 while ((p = *where) != 0) {
657 /* Sort in ascending order of timeout */
658 if (host->send_next < p->send_next)
660 /* If we have the same timeout, put the
661 * most recently used host first.
662 * This makes sure that "recent" hosts
663 * get notified first.
665 if (host->send_next == p->send_next
666 && host->last_used > p->last_used)
676 * Find host given the XID
678 static struct nsm_host *
679 find_host(uint32_t xid)
681 struct nsm_host **where, *p;
684 while ((p = *where) != 0) {
695 * Record pid in /var/run/sm-notify.pid
696 * This file should remain until a reboot, even if the
698 * If file already exists, fail.
700 static int record_pid(void)
706 (void)snprintf(pid, sizeof(pid), "%d\n", (int)getpid());
707 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
711 len = write(fd, pid, strlen(pid));
712 if ((len < 0) || ((size_t)len != strlen(pid))) {
713 xlog_warn("Writing to pid file failed: errno %d (%m)",