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, ...);
83 static int record_pid();
85 static struct nsm_host * hosts = NULL;
88 main(int argc, char **argv)
93 while ((c = getopt(argc, argv, "dm:np:v:qP:f")) != -1) {
102 opt_max_retry = atoi(optarg) * 60;
105 opt_update_state = 0;
108 opt_srcport = atoi(optarg);
111 opt_srcaddr = optarg;
117 _SM_BASE_PATH = strdup(optarg);
118 _SM_STATE_PATH = malloc(strlen(optarg)+1+sizeof("state"));
119 _SM_DIR_PATH = malloc(strlen(optarg)+1+sizeof("sm"));
120 _SM_BAK_PATH = malloc(strlen(optarg)+1+sizeof("sm.bak"));
121 if (_SM_BASE_PATH == NULL ||
122 _SM_STATE_PATH == NULL ||
123 _SM_DIR_PATH == NULL ||
124 _SM_BAK_PATH == NULL) {
125 nsm_log(LOG_WARNING, "unable to allocate memory");
128 strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
129 strcat(strcpy(_SM_DIR_PATH, _SM_BASE_PATH), "/sm");
130 strcat(strcpy(_SM_BAK_PATH, _SM_BASE_PATH), "/sm.bak");
139 usage: fprintf(stderr,
140 "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
141 " [-P /path/to/state/directory] [-N my_host_name\n");
145 if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) {
146 if (record_pid() == 0 && force == 0 && opt_update_state == 0)
147 /* already run, don't try again */
152 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
154 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
155 perror("gethostname");
159 backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
160 get_hosts(_SM_BAK_PATH);
164 printf("Backgrounding to notify hosts...\n");
166 openlog("sm-notify", LOG_PID, LOG_DAEMON);
169 if (daemon(0, 0) < 0) {
170 nsm_log(LOG_WARNING, "unable to background: %s",
180 /* Get and update the NSM state. This will call sync() */
181 nsm_state = nsm_get_state(opt_update_state);
188 while ((hp = hosts) != 0) {
191 "Unable to notify %s, giving up",
206 nsm_address local_addr;
210 sock = socket(AF_INET, SOCK_DGRAM, 0);
215 fcntl(sock, F_SETFL, O_NONBLOCK);
217 memset(&local_addr, 0, sizeof(local_addr));
218 local_addr.ss_family = AF_INET; /* Default to IPv4 */
220 /* Bind source IP if provided on command line */
222 if (!host_lookup(AF_INET, opt_srcaddr, &local_addr)) {
224 "Not a valid hostname or address: \"%s\"\n",
228 /* We know it's IPv4 at this point */
231 /* Use source port if provided on the command line,
232 * otherwise use bindresvport */
234 addr_set_port(&local_addr, opt_srcport);
235 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
240 (void) bindresvport(sock, (struct sockaddr_in *) &local_addr);
244 failtime = time(NULL) + opt_max_retry;
248 time_t now = time(NULL);
249 unsigned int sent = 0;
253 if (failtime && now >= failtime)
256 while ((wait = hosts->send_next - now) <= 0) {
257 /* Never send more than 10 packets at once */
261 /* Remove queue head */
265 notify_host(sock, hp);
267 /* Set the timeout for this call, using an
268 exponential timeout strategy */
270 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
271 hp->timeout = NSM_MAX_TIMEOUT;
272 hp->send_next = now + wait;
278 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
287 if (poll(&pfd, 1, wait) != 1)
295 * Send notification to a single host
298 notify_host(int sock, struct nsm_host *host)
300 static unsigned int xid = 0;
302 uint32_t msgbuf[MAXMSGSIZE], *p;
306 xid = getpid() + time(NULL);
310 memset(msgbuf, 0, sizeof(msgbuf));
312 *p++ = htonl(host->xid);
316 /* If we retransmitted 4 times, reset the port to force
317 * a new portmap lookup (in case statd was restarted)
319 if (host->retries >= 4) {
320 addr_set_port(&host->addr, 0);
325 if (addr_get_port(&dest) == 0) {
326 /* Build a PMAP packet */
327 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
329 addr_set_port(&dest, 111);
330 *p++ = htonl(100000);
338 *p++ = htonl(NSM_PROGRAM);
339 *p++ = htonl(NSM_VERSION);
340 *p++ = htonl(IPPROTO_UDP);
343 /* Build an SM_NOTIFY packet */
344 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
346 *p++ = htonl(NSM_PROGRAM);
347 *p++ = htonl(NSM_VERSION);
348 *p++ = htonl(NSM_NOTIFY);
355 len = strlen(nsm_hostname);
357 memcpy(p, nsm_hostname, len);
359 *p++ = htonl(nsm_state);
361 len = (p - msgbuf) << 2;
363 sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest));
367 * Receive reply from remote host
373 uint32_t msgbuf[MAXMSGSIZE], *p, *end;
377 res = recv(sock, msgbuf, sizeof(msgbuf), 0);
381 nsm_log(LOG_DEBUG, "Received packet...");
384 end = p + (res >> 2);
387 if (*p++ != htonl(1) /* must be REPLY */
388 || *p++ != htonl(0) /* must be ACCEPTED */
389 || *p++ != htonl(0) /* must be NULL verifier */
391 || *p++ != htonl(0)) /* must be SUCCESS */
394 /* Before we look at the data, find the host struct for
396 if ((hp = find_host(xid)) == NULL)
399 if (addr_get_port(&hp->addr) == 0) {
400 /* This was a portmap request */
407 hp->send_next = time(NULL);
409 /* No binding for statd. Delay the next
410 * portmap query for max timeout */
411 nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
412 hp->timeout = NSM_MAX_TIMEOUT;
413 hp->send_next += NSM_MAX_TIMEOUT;
415 addr_set_port(&hp->addr, port);
416 if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
417 hp->timeout = NSM_MAX_TIMEOUT / 4;
421 /* Successful NOTIFY call. Server returns void,
422 * so nothing we need to do here (except
423 * check that we didn't read past the end of the
427 nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
436 fail: /* Re-insert the host */
441 * Back up all hosts from the sm directory to sm.bak
444 backup_hosts(const char *dirname, const char *bakname)
449 if (!(dir = opendir(dirname))) {
454 while ((de = readdir(dir)) != NULL) {
455 char src[1024], dst[1024];
457 if (de->d_name[0] == '.')
460 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
461 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
462 if (rename(src, dst) < 0) {
464 "Failed to rename %s -> %s: %m",
472 * Get all entries from sm.bak and convert them to host names
475 get_hosts(const char *dirname)
477 struct nsm_host *host;
481 if (!(dir = opendir(dirname))) {
487 while ((de = readdir(dir)) != NULL) {
491 if (de->d_name[0] == '.')
494 host = calloc(1, sizeof(*host));
496 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
497 if (!host_lookup(AF_UNSPEC, de->d_name, &host->addr)) {
499 "%s doesn't seem to be a valid address, skipped",
505 if (stat(path, &stb) < 0)
507 host->last_used = stb.st_mtime;
508 host->timeout = NSM_TIMEOUT;
509 host->path = strdup(path);
510 host->name = strdup(de->d_name);
522 * Insert host into sorted list
525 insert_host(struct nsm_host *host)
527 struct nsm_host **where, *p;
530 while ((p = *where) != 0) {
531 /* Sort in ascending order of timeout */
532 if (host->send_next < p->send_next)
534 /* If we have the same timeout, put the
535 * most recently used host first.
536 * This makes sure that "recent" hosts
537 * get notified first.
539 if (host->send_next == p->send_next
540 && host->last_used > p->last_used)
550 * Find host given the XID
553 find_host(uint32_t xid)
555 struct nsm_host **where, *p;
558 while ((p = *where) != 0) {
570 * Retrieve the current NSM state
573 nsm_get_state(int update)
575 char newfile[PATH_MAX];
578 if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
580 nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
581 nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
587 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
589 "%s: bad file size, setting state = 1",
602 snprintf(newfile, sizeof(newfile),
603 "%s.new", _SM_STATE_PATH);
604 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
605 nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
608 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
610 "Failed to write state to %s", newfile);
614 if (rename(newfile, _SM_STATE_PATH) < 0) {
616 "Cannot create %s: %m", _SM_STATE_PATH);
626 * Address handling utilities
630 addr_get_port(nsm_address *addr)
632 switch (((struct sockaddr *) addr)->sa_family) {
634 return ntohs(((struct sockaddr_in *) addr)->sin_port);
636 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
642 addr_set_port(nsm_address *addr, int port)
644 switch (((struct sockaddr *) addr)->sa_family) {
646 ((struct sockaddr_in *) addr)->sin_port = htons(port);
649 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
654 host_lookup(int af, const char *name, nsm_address *addr)
656 struct addrinfo hints, *ai;
659 memset(&hints, 0, sizeof(hints));
660 hints.ai_family = af;
662 if (getaddrinfo(name, NULL, &hints, &ai) != 0)
665 if (ai->ai_addrlen < sizeof(*addr)) {
666 memcpy(addr, ai->ai_addr, ai->ai_addrlen);
678 nsm_log(int fac, const char *fmt, ...)
682 if (fac == LOG_DEBUG && !opt_debug)
687 vsyslog(fac, fmt, ap);
689 vfprintf(stderr, fmt, ap);
696 * Record pid in /var/run/sm-notify.pid
697 * This file should remain until a reboot, even if the
699 * If file already exists, fail.
701 static int record_pid()
706 snprintf(pid, 20, "%d\n", getpid());
707 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
710 write(fd, pid, strlen(pid));