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_get_port(nsm_address *);
80 static void addr_set_port(nsm_address *, int);
81 static int host_lookup(int, const char *, nsm_address *);
82 void nsm_log(int fac, const char *fmt, ...);
84 static struct nsm_host * hosts = NULL;
87 main(int argc, char **argv)
91 while ((c = getopt(argc, argv, "dm:np:v:qP:")) != -1) {
97 opt_max_retry = atoi(optarg) * 60;
100 opt_update_state = 0;
103 opt_srcport = atoi(optarg);
106 opt_srcaddr = optarg;
112 _SM_BASE_PATH = strdup(optarg);
113 _SM_STATE_PATH = malloc(strlen(optarg)+1+sizeof("state"));
114 _SM_DIR_PATH = malloc(strlen(optarg)+1+sizeof("sm"));
115 _SM_BAK_PATH = malloc(strlen(optarg)+1+sizeof("sm.bak"));
116 if (_SM_BASE_PATH == NULL ||
117 _SM_STATE_PATH == NULL ||
118 _SM_DIR_PATH == NULL ||
119 _SM_BAK_PATH == NULL) {
120 nsm_log(LOG_WARNING, "unable to allocate memory");
123 strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
124 strcat(strcpy(_SM_DIR_PATH, _SM_BASE_PATH), "/sm");
125 strcat(strcpy(_SM_BAK_PATH, _SM_BASE_PATH), "/sm.bak");
134 usage: fprintf(stderr, "sm-notify [-d]\n");
139 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
141 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
142 perror("gethostname");
146 backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
147 get_hosts(_SM_BAK_PATH);
151 printf("Backgrounding to notify hosts...\n");
153 openlog("sm-notify", LOG_PID, LOG_DAEMON);
156 if (daemon(0, 0) < 0) {
157 nsm_log(LOG_WARNING, "unable to background: %s",
167 /* Get and update the NSM state. This will call sync() */
168 nsm_state = nsm_get_state(opt_update_state);
175 while ((hp = hosts) != 0) {
178 "Unable to notify %s, giving up",
193 nsm_address local_addr;
197 sock = socket(AF_INET, SOCK_DGRAM, 0);
202 fcntl(sock, F_SETFL, O_NONBLOCK);
204 memset(&local_addr, 0, sizeof(local_addr));
205 local_addr.ss_family = AF_INET; /* Default to IPv4 */
207 /* Bind source IP if provided on command line */
209 if (!host_lookup(AF_INET, opt_srcaddr, &local_addr)) {
211 "Not a valid hostname or address: \"%s\"\n",
215 /* We know it's IPv4 at this point */
218 /* Use source port if provided on the command line,
219 * otherwise use bindresvport */
221 addr_set_port(&local_addr, opt_srcport);
222 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
227 (void) bindresvport(sock, (struct sockaddr_in *) &local_addr);
231 failtime = time(NULL) + opt_max_retry;
235 time_t now = time(NULL);
236 unsigned int sent = 0;
240 if (failtime && now >= failtime)
243 while ((wait = hosts->send_next - now) <= 0) {
244 /* Never send more than 10 packets at once */
248 /* Remove queue head */
252 notify_host(sock, hp);
254 /* Set the timeout for this call, using an
255 exponential timeout strategy */
257 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
258 hp->timeout = NSM_MAX_TIMEOUT;
259 hp->send_next = now + wait;
265 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
274 if (poll(&pfd, 1, wait) != 1)
282 * Send notification to a single host
285 notify_host(int sock, struct nsm_host *host)
287 static unsigned int xid = 0;
289 uint32_t msgbuf[MAXMSGSIZE], *p;
293 xid = getpid() + time(NULL);
297 memset(msgbuf, 0, sizeof(msgbuf));
299 *p++ = htonl(host->xid);
303 /* If we retransmitted 4 times, reset the port to force
304 * a new portmap lookup (in case statd was restarted)
306 if (host->retries >= 4) {
307 addr_set_port(&host->addr, 0);
312 if (addr_get_port(&dest) == 0) {
313 /* Build a PMAP packet */
314 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
316 addr_set_port(&dest, 111);
317 *p++ = htonl(100000);
325 *p++ = htonl(NSM_PROGRAM);
326 *p++ = htonl(NSM_VERSION);
327 *p++ = htonl(IPPROTO_UDP);
330 /* Build an SM_NOTIFY packet */
331 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
333 *p++ = htonl(NSM_PROGRAM);
334 *p++ = htonl(NSM_VERSION);
335 *p++ = htonl(NSM_NOTIFY);
342 len = strlen(nsm_hostname);
344 memcpy(p, nsm_hostname, len);
346 *p++ = htonl(nsm_state);
348 len = (p - msgbuf) << 2;
350 sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest));
354 * Receive reply from remote host
360 uint32_t msgbuf[MAXMSGSIZE], *p, *end;
364 res = recv(sock, msgbuf, sizeof(msgbuf), 0);
368 nsm_log(LOG_DEBUG, "Received packet...");
371 end = p + (res >> 2);
374 if (*p++ != htonl(1) /* must be REPLY */
375 || *p++ != htonl(0) /* must be ACCEPTED */
376 || *p++ != htonl(0) /* must be NULL verifier */
378 || *p++ != htonl(0)) /* must be SUCCESS */
381 /* Before we look at the data, find the host struct for
383 if ((hp = find_host(xid)) == NULL)
386 if (addr_get_port(&hp->addr) == 0) {
387 /* This was a portmap request */
394 hp->send_next = time(NULL);
396 /* No binding for statd. Delay the next
397 * portmap query for max timeout */
398 nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
399 hp->timeout = NSM_MAX_TIMEOUT;
400 hp->send_next += NSM_MAX_TIMEOUT;
402 addr_set_port(&hp->addr, port);
403 if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
404 hp->timeout = NSM_MAX_TIMEOUT / 4;
408 /* Successful NOTIFY call. Server returns void,
409 * so nothing we need to do here (except
410 * check that we didn't read past the end of the
414 nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
423 fail: /* Re-insert the host */
428 * Back up all hosts from the sm directory to sm.bak
431 backup_hosts(const char *dirname, const char *bakname)
436 if (!(dir = opendir(dirname))) {
441 while ((de = readdir(dir)) != NULL) {
442 char src[1024], dst[1024];
444 if (de->d_name[0] == '.')
447 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
448 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
449 if (rename(src, dst) < 0) {
451 "Failed to rename %s -> %s: %m",
459 * Get all entries from sm.bak and convert them to host names
462 get_hosts(const char *dirname)
464 struct nsm_host *host;
468 if (!(dir = opendir(dirname))) {
474 while ((de = readdir(dir)) != NULL) {
478 if (de->d_name[0] == '.')
481 host = calloc(1, sizeof(*host));
483 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
484 if (!host_lookup(AF_UNSPEC, de->d_name, &host->addr)) {
486 "%s doesn't seem to be a valid address, skipped",
492 if (stat(path, &stb) < 0)
494 host->last_used = stb.st_mtime;
495 host->timeout = NSM_TIMEOUT;
496 host->path = strdup(path);
497 host->name = strdup(de->d_name);
509 * Insert host into sorted list
512 insert_host(struct nsm_host *host)
514 struct nsm_host **where, *p;
517 while ((p = *where) != 0) {
518 /* Sort in ascending order of timeout */
519 if (host->send_next < p->send_next)
521 /* If we have the same timeout, put the
522 * most recently used host first.
523 * This makes sure that "recent" hosts
524 * get notified first.
526 if (host->send_next == p->send_next
527 && host->last_used > p->last_used)
537 * Find host given the XID
540 find_host(uint32_t xid)
542 struct nsm_host **where, *p;
545 while ((p = *where) != 0) {
557 * Retrieve the current NSM state
560 nsm_get_state(int update)
562 char newfile[PATH_MAX];
565 if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
567 nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
568 nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
574 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
576 "%s: bad file size, setting state = 1",
589 snprintf(newfile, sizeof(newfile),
590 "%s.new", _SM_STATE_PATH);
591 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
592 nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
595 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
597 "Failed to write state to %s", newfile);
601 if (rename(newfile, _SM_STATE_PATH) < 0) {
603 "Cannot create %s: %m", _SM_STATE_PATH);
613 * Address handling utilities
617 addr_get_port(nsm_address *addr)
619 switch (((struct sockaddr *) addr)->sa_family) {
621 return ntohs(((struct sockaddr_in *) addr)->sin_port);
623 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
629 addr_set_port(nsm_address *addr, int port)
631 switch (((struct sockaddr *) addr)->sa_family) {
633 ((struct sockaddr_in *) addr)->sin_port = htons(port);
636 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
641 host_lookup(int af, const char *name, nsm_address *addr)
643 struct addrinfo hints, *ai;
646 memset(&hints, 0, sizeof(hints));
647 hints.ai_family = af;
649 if (getaddrinfo(name, NULL, &hints, &ai) != 0)
652 if (ai->ai_addrlen < sizeof(*addr)) {
653 memcpy(addr, ai->ai_addr, ai->ai_addrlen);
665 nsm_log(int fac, const char *fmt, ...)
669 if (fac == LOG_DEBUG && !opt_debug)
674 vsyslog(fac, fmt, ap);
676 vfprintf(stderr, fmt, ap);