2 * Send NSM notify calls to all hosts listed in /var/lib/sm
4 * Copyright (C) 2004-2006 Olaf Kirch <okir@suse.de>
8 #include <sys/socket.h>
11 #include <sys/param.h>
12 #include <sys/syslog.h>
13 #include <arpa/inet.h>
27 #define BASEDIR "/var/lib/nfs"
30 #define DEFAULT_SM_STATE_PATH BASEDIR "/state"
31 #define DEFAULT_SM_DIR_PATH BASEDIR "/sm"
32 #define DEFAULT_SM_BAK_PATH DEFAULT_SM_DIR_PATH ".bak"
34 char *_SM_BASE_PATH = BASEDIR;
35 char *_SM_STATE_PATH = DEFAULT_SM_STATE_PATH;
36 char *_SM_DIR_PATH = DEFAULT_SM_DIR_PATH;
37 char *_SM_BAK_PATH = DEFAULT_SM_BAK_PATH;
39 #define NSM_PROG 100024
40 #define NSM_PROGRAM 100024
44 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
45 #define MAXMSGSIZE 256
47 typedef struct sockaddr_storage nsm_address;
50 struct nsm_host * next;
61 static char nsm_hostname[256];
62 static uint32_t nsm_state;
63 static int opt_debug = 0;
64 static int opt_quiet = 0;
65 static int opt_update_state = 1;
66 static unsigned int opt_max_retry = 15 * 60;
67 static char * opt_srcaddr = 0;
68 static uint16_t opt_srcport = 0;
69 static int log_syslog = 0;
71 static unsigned int nsm_get_state(int);
72 static void notify(void);
73 static void notify_host(int, struct nsm_host *);
74 static void recv_reply(int);
75 static void backup_hosts(const char *, const char *);
76 static void get_hosts(const char *);
77 static void insert_host(struct nsm_host *);
78 struct nsm_host * find_host(uint32_t);
79 static int addr_parse(int, const char *, nsm_address *);
80 static int addr_get_port(nsm_address *);
81 static void addr_set_port(nsm_address *, int);
82 static int host_lookup(int, const char *, nsm_address *);
83 void nsm_log(int fac, const char *fmt, ...);
85 static struct nsm_host * hosts = NULL;
88 main(int argc, char **argv)
92 while ((c = getopt(argc, argv, "dm:np:v:qP:")) != -1) {
98 opt_max_retry = atoi(optarg) * 60;
101 opt_update_state = 0;
104 opt_srcport = atoi(optarg);
107 opt_srcaddr = optarg;
113 _SM_BASE_PATH = strdup(optarg);
114 _SM_STATE_PATH = malloc(strlen(optarg)+1+sizeof("state"));
115 _SM_DIR_PATH = malloc(strlen(optarg)+1+sizeof("sm"));
116 _SM_BAK_PATH = malloc(strlen(optarg)+1+sizeof("sm.bak"));
117 if (_SM_BASE_PATH == NULL ||
118 _SM_STATE_PATH == NULL ||
119 _SM_DIR_PATH == NULL ||
120 _SM_BAK_PATH == NULL) {
121 nsm_log(LOG_WARNING, "unable to allocate memory");
124 strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
125 strcat(strcpy(_SM_DIR_PATH, _SM_BASE_PATH), "/sm");
126 strcat(strcpy(_SM_BAK_PATH, _SM_BASE_PATH), "/sm.bak");
135 usage: fprintf(stderr, "sm-notify [-d]\n");
140 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
142 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
143 perror("gethostname");
147 backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
148 get_hosts(_SM_BAK_PATH);
152 printf("Backgrounding to notify hosts...\n");
154 openlog("sm-notify", LOG_PID, LOG_DAEMON);
157 if (daemon(0, 0) < 0) {
158 nsm_log(LOG_WARNING, "unable to background: %s",
168 /* Get and update the NSM state. This will call sync() */
169 nsm_state = nsm_get_state(opt_update_state);
176 while ((hp = hosts) != 0) {
179 "Unable to notify %s, giving up",
194 nsm_address local_addr;
198 sock = socket(AF_INET, SOCK_DGRAM, 0);
203 fcntl(sock, F_SETFL, O_NONBLOCK);
205 memset(&local_addr, 0, sizeof(local_addr));
206 local_addr.ss_family = AF_INET; /* Default to IPv4 */
208 /* Bind source IP if provided on command line */
210 if (!addr_parse(AF_INET, opt_srcaddr, &local_addr)
211 && !host_lookup(AF_INET, opt_srcaddr, &local_addr)) {
213 "Not a valid hostname or address: \"%s\"\n",
217 /* We know it's IPv4 at this point */
220 /* Use source port if provided on the command line,
221 * otherwise use bindresvport */
223 addr_set_port(&local_addr, opt_srcport);
224 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
229 (void) bindresvport(sock, (struct sockaddr_in *) &local_addr);
233 failtime = time(NULL) + opt_max_retry;
237 time_t now = time(NULL);
238 unsigned int sent = 0;
242 if (failtime && now >= failtime)
245 while ((wait = hosts->send_next - now) <= 0) {
246 /* Never send more than 10 packets at once */
250 /* Remove queue head */
254 notify_host(sock, hp);
256 /* Set the timeout for this call, using an
257 exponential timeout strategy */
259 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
260 hp->timeout = NSM_MAX_TIMEOUT;
261 hp->send_next = now + wait;
267 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
276 if (poll(&pfd, 1, wait) != 1)
284 * Send notification to a single host
287 notify_host(int sock, struct nsm_host *host)
289 static unsigned int xid = 0;
291 uint32_t msgbuf[MAXMSGSIZE], *p;
295 xid = getpid() + time(NULL);
299 memset(msgbuf, 0, sizeof(msgbuf));
301 *p++ = htonl(host->xid);
305 /* If we retransmitted 4 times, reset the port to force
306 * a new portmap lookup (in case statd was restarted)
308 if (host->retries >= 4) {
309 addr_set_port(&host->addr, 0);
314 if (addr_get_port(&dest) == 0) {
315 /* Build a PMAP packet */
316 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
318 addr_set_port(&dest, 111);
319 *p++ = htonl(100000);
327 *p++ = htonl(NSM_PROGRAM);
328 *p++ = htonl(NSM_VERSION);
329 *p++ = htonl(IPPROTO_UDP);
332 /* Build an SM_NOTIFY packet */
333 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
335 *p++ = htonl(NSM_PROGRAM);
336 *p++ = htonl(NSM_VERSION);
337 *p++ = htonl(NSM_NOTIFY);
344 len = strlen(nsm_hostname);
346 memcpy(p, nsm_hostname, len);
348 *p++ = htonl(nsm_state);
350 len = (p - msgbuf) << 2;
352 sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest));
356 * Receive reply from remote host
362 uint32_t msgbuf[MAXMSGSIZE], *p, *end;
366 res = recv(sock, msgbuf, sizeof(msgbuf), 0);
370 nsm_log(LOG_DEBUG, "Received packet...");
373 end = p + (res >> 2);
376 if (*p++ != htonl(1) /* must be REPLY */
377 || *p++ != htonl(0) /* must be ACCEPTED */
378 || *p++ != htonl(0) /* must be NULL verifier */
380 || *p++ != htonl(0)) /* must be SUCCESS */
383 /* Before we look at the data, find the host struct for
385 if ((hp = find_host(xid)) == NULL)
388 if (addr_get_port(&hp->addr) == 0) {
389 /* This was a portmap request */
396 hp->send_next = time(NULL);
398 /* No binding for statd. Delay the next
399 * portmap query for max timeout */
400 nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
401 hp->timeout = NSM_MAX_TIMEOUT;
402 hp->send_next += NSM_MAX_TIMEOUT;
404 addr_set_port(&hp->addr, port);
405 if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
406 hp->timeout = NSM_MAX_TIMEOUT / 4;
410 /* Successful NOTIFY call. Server returns void,
411 * so nothing we need to do here (except
412 * check that we didn't read past the end of the
416 nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
425 fail: /* Re-insert the host */
430 * Back up all hosts from the sm directory to sm.bak
433 backup_hosts(const char *dirname, const char *bakname)
438 if (!(dir = opendir(dirname))) {
443 while ((de = readdir(dir)) != NULL) {
444 char src[1024], dst[1024];
446 if (de->d_name[0] == '.')
449 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
450 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
451 if (rename(src, dst) < 0) {
453 "Failed to rename %s -> %s: %m",
461 * Get all entries from sm.bak and convert them to host names
464 get_hosts(const char *dirname)
466 struct nsm_host *host;
470 if (!(dir = opendir(dirname))) {
476 while ((de = readdir(dir)) != NULL) {
480 if (de->d_name[0] == '.')
483 host = calloc(1, sizeof(*host));
485 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
486 if (!addr_parse(AF_INET, de->d_name, &host->addr)
487 && !addr_parse(AF_INET6, de->d_name, &host->addr)
488 && !host_lookup(AF_INET, de->d_name, &host->addr)) {
490 "%s doesn't seem to be a valid address, skipped",
496 if (stat(path, &stb) < 0)
498 host->last_used = stb.st_mtime;
499 host->timeout = NSM_TIMEOUT;
500 host->path = strdup(path);
501 host->name = strdup(de->d_name);
513 * Insert host into sorted list
516 insert_host(struct nsm_host *host)
518 struct nsm_host **where, *p;
521 while ((p = *where) != 0) {
522 /* Sort in ascending order of timeout */
523 if (host->send_next < p->send_next)
525 /* If we have the same timeout, put the
526 * most recently used host first.
527 * This makes sure that "recent" hosts
528 * get notified first.
530 if (host->send_next == p->send_next
531 && host->last_used > p->last_used)
541 * Find host given the XID
544 find_host(uint32_t xid)
546 struct nsm_host **where, *p;
549 while ((p = *where) != 0) {
561 * Retrieve the current NSM state
564 nsm_get_state(int update)
566 char newfile[PATH_MAX];
569 if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
571 nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
572 nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
578 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
580 "%s: bad file size, setting state = 1",
593 snprintf(newfile, sizeof(newfile),
594 "%s.new", _SM_STATE_PATH);
595 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
596 nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
599 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
601 "Failed to write state to %s", newfile);
605 if (rename(newfile, _SM_STATE_PATH) < 0) {
607 "Cannot create %s: %m", _SM_STATE_PATH);
617 * Address handling utilities
620 addr_parse(int af, const char *name, nsm_address *addr)
625 ptr = &((struct sockaddr_in *) addr)->sin_addr;
626 else if (af == AF_INET6)
627 ptr = &((struct sockaddr_in6 *) addr)->sin6_addr;
630 if (inet_pton(af, name, ptr) <= 0)
632 ((struct sockaddr *) addr)->sa_family = af;
637 addr_get_port(nsm_address *addr)
639 switch (((struct sockaddr *) addr)->sa_family) {
641 return ntohs(((struct sockaddr_in *) addr)->sin_port);
643 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
649 addr_set_port(nsm_address *addr, int port)
651 switch (((struct sockaddr *) addr)->sa_family) {
653 ((struct sockaddr_in *) addr)->sin_port = htons(port);
656 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
661 host_lookup(int af, const char *name, nsm_address *addr)
663 struct addrinfo hints, *ai;
666 memset(&hints, 0, sizeof(hints));
667 hints.ai_family = af;
669 if (getaddrinfo(name, NULL, &hints, &ai) != 0)
672 if (ai->ai_addrlen < sizeof(*addr)) {
673 memcpy(addr, ai->ai_addr, ai->ai_addrlen);
685 nsm_log(int fac, const char *fmt, ...)
689 if (fac == LOG_DEBUG && !opt_debug)
694 vsyslog(fac, fmt, ap);
696 vfprintf(stderr, fmt, ap);