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>
29 # define BASEDIR NFS_STATEDIR
31 # define BASEDIR "/var/lib/nfs"
35 #define DEFAULT_SM_STATE_PATH BASEDIR "/state"
36 #define DEFAULT_SM_DIR_PATH BASEDIR "/sm"
37 #define DEFAULT_SM_BAK_PATH DEFAULT_SM_DIR_PATH ".bak"
39 char *_SM_BASE_PATH = BASEDIR;
40 char *_SM_STATE_PATH = DEFAULT_SM_STATE_PATH;
41 char *_SM_DIR_PATH = DEFAULT_SM_DIR_PATH;
42 char *_SM_BAK_PATH = DEFAULT_SM_BAK_PATH;
44 #define NSM_PROG 100024
45 #define NSM_PROGRAM 100024
49 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
50 #define MAXMSGSIZE 256
52 typedef struct sockaddr_storage nsm_address;
55 struct nsm_host * next;
67 static char nsm_hostname[256];
68 static uint32_t nsm_state;
69 static int opt_debug = 0;
70 static int opt_quiet = 0;
71 static int opt_update_state = 1;
72 static unsigned int opt_max_retry = 15 * 60;
73 static char * opt_srcaddr = 0;
74 static uint16_t opt_srcport = 0;
75 static int log_syslog = 0;
77 static unsigned int nsm_get_state(int);
78 static void notify(void);
79 static void notify_host(int, struct nsm_host *);
80 static void recv_reply(int);
81 static void backup_hosts(const char *, const char *);
82 static void get_hosts(const char *);
83 static void insert_host(struct nsm_host *);
84 struct nsm_host * find_host(uint32_t);
85 static int addr_get_port(nsm_address *);
86 static void addr_set_port(nsm_address *, int);
87 static struct addrinfo *host_lookup(int, const char *);
88 void nsm_log(int fac, const char *fmt, ...);
89 static int record_pid();
90 static void drop_privs(void);
91 static void set_kernel_nsm_state(int state);
93 static struct nsm_host * hosts = NULL;
96 main(int argc, char **argv)
101 while ((c = getopt(argc, argv, "dm:np:v:qP:f")) != -1) {
110 opt_max_retry = atoi(optarg) * 60;
113 opt_update_state = 0;
116 opt_srcport = atoi(optarg);
119 opt_srcaddr = optarg;
125 _SM_BASE_PATH = strdup(optarg);
126 _SM_STATE_PATH = malloc(strlen(optarg)+1+sizeof("state"));
127 _SM_DIR_PATH = malloc(strlen(optarg)+1+sizeof("sm"));
128 _SM_BAK_PATH = malloc(strlen(optarg)+1+sizeof("sm.bak"));
129 if (_SM_BASE_PATH == NULL ||
130 _SM_STATE_PATH == NULL ||
131 _SM_DIR_PATH == NULL ||
132 _SM_BAK_PATH == NULL) {
133 nsm_log(LOG_WARNING, "unable to allocate memory");
136 strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
137 strcat(strcpy(_SM_DIR_PATH, _SM_BASE_PATH), "/sm");
138 strcat(strcpy(_SM_BAK_PATH, _SM_BASE_PATH), "/sm.bak");
147 usage: fprintf(stderr,
148 "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
149 " [-P /path/to/state/directory] [-v my_host_name]\n");
153 if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) {
154 if (record_pid() == 0 && force == 0 && opt_update_state == 1)
155 /* already run, don't try again */
160 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
162 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
163 perror("gethostname");
167 backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
168 get_hosts(_SM_BAK_PATH);
170 /* Get and update the NSM state. This will call sync() */
171 nsm_state = nsm_get_state(opt_update_state);
172 set_kernel_nsm_state(nsm_state);
176 printf("Backgrounding to notify hosts...\n");
178 openlog("sm-notify", LOG_PID, LOG_DAEMON);
181 if (daemon(0, 0) < 0) {
182 nsm_log(LOG_WARNING, "unable to background: %s",
197 while ((hp = hosts) != 0) {
200 "Unable to notify %s, giving up",
215 nsm_address local_addr;
221 sock = socket(AF_INET, SOCK_DGRAM, 0);
226 fcntl(sock, F_SETFL, O_NONBLOCK);
228 memset(&local_addr, 0, sizeof(local_addr));
229 local_addr.ss_family = AF_INET; /* Default to IPv4 */
231 /* Bind source IP if provided on command line */
233 struct addrinfo *ai = host_lookup(AF_INET, opt_srcaddr);
236 "Not a valid hostname or address: \"%s\"\n",
240 memcpy(&local_addr, ai->ai_addr, ai->ai_addrlen);
241 /* We know it's IPv4 at this point */
244 /* Use source port if provided on the command line,
245 * otherwise use bindresvport */
247 addr_set_port(&local_addr, opt_srcport);
248 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
254 (void) bindresvport(sock, (struct sockaddr_in *) &local_addr);
255 /* try to avoid known ports */
256 se = getservbyport(local_addr.sin_port, "udp");
257 if (se && retry_cnt < 100) {
265 failtime = time(NULL) + opt_max_retry;
271 time_t now = time(NULL);
272 unsigned int sent = 0;
276 if (failtime && now >= failtime)
279 while ((wait = hosts->send_next - now) <= 0) {
280 /* Never send more than 10 packets at once */
284 /* Remove queue head */
288 notify_host(sock, hp);
290 /* Set the timeout for this call, using an
291 exponential timeout strategy */
293 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
294 hp->timeout = NSM_MAX_TIMEOUT;
295 hp->send_next = now + wait;
301 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
310 if (poll(&pfd, 1, wait) != 1)
318 * Send notification to a single host
321 notify_host(int sock, struct nsm_host *host)
323 static unsigned int xid = 0;
325 uint32_t msgbuf[MAXMSGSIZE], *p;
329 xid = getpid() + time(NULL);
333 memset(msgbuf, 0, sizeof(msgbuf));
335 *p++ = htonl(host->xid);
339 /* If we retransmitted 4 times, reset the port to force
340 * a new portmap lookup (in case statd was restarted).
341 * We also rotate through multiple IP addresses at this
344 if (host->retries >= 4) {
345 struct addrinfo *hold = host->ai;
346 struct addrinfo **next = &host->ai;
347 *next = hold->ai_next;
349 next = & (*next)->ai_next;
351 hold->ai_next = NULL;
352 memcpy(&host->addr, hold->ai_addr, hold->ai_addrlen);
353 addr_set_port(&host->addr, 0);
358 if (addr_get_port(&dest) == 0) {
359 /* Build a PMAP packet */
360 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
362 addr_set_port(&dest, 111);
363 *p++ = htonl(100000);
371 *p++ = htonl(NSM_PROGRAM);
372 *p++ = htonl(NSM_VERSION);
373 *p++ = htonl(IPPROTO_UDP);
376 /* Build an SM_NOTIFY packet */
377 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
379 *p++ = htonl(NSM_PROGRAM);
380 *p++ = htonl(NSM_VERSION);
381 *p++ = htonl(NSM_NOTIFY);
388 len = strlen(nsm_hostname);
390 memcpy(p, nsm_hostname, len);
392 *p++ = htonl(nsm_state);
394 len = (p - msgbuf) << 2;
396 sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest));
400 * Receive reply from remote host
406 uint32_t msgbuf[MAXMSGSIZE], *p, *end;
410 res = recv(sock, msgbuf, sizeof(msgbuf), 0);
414 nsm_log(LOG_DEBUG, "Received packet...");
417 end = p + (res >> 2);
420 if (*p++ != htonl(1) /* must be REPLY */
421 || *p++ != htonl(0) /* must be ACCEPTED */
422 || *p++ != htonl(0) /* must be NULL verifier */
424 || *p++ != htonl(0)) /* must be SUCCESS */
427 /* Before we look at the data, find the host struct for
429 if ((hp = find_host(xid)) == NULL)
432 if (addr_get_port(&hp->addr) == 0) {
433 /* This was a portmap request */
440 hp->send_next = time(NULL);
442 /* No binding for statd. Delay the next
443 * portmap query for max timeout */
444 nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
445 hp->timeout = NSM_MAX_TIMEOUT;
446 hp->send_next += NSM_MAX_TIMEOUT;
448 addr_set_port(&hp->addr, port);
449 if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
450 hp->timeout = NSM_MAX_TIMEOUT / 4;
454 /* Successful NOTIFY call. Server returns void,
455 * so nothing we need to do here (except
456 * check that we didn't read past the end of the
460 nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
465 freeaddrinfo(hp->ai);
470 fail: /* Re-insert the host */
475 * Back up all hosts from the sm directory to sm.bak
478 backup_hosts(const char *dirname, const char *bakname)
483 if (!(dir = opendir(dirname))) {
488 while ((de = readdir(dir)) != NULL) {
489 char src[1024], dst[1024];
491 if (de->d_name[0] == '.')
494 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
495 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
496 if (rename(src, dst) < 0) {
498 "Failed to rename %s -> %s: %m",
506 * Get all entries from sm.bak and convert them to host names
509 get_hosts(const char *dirname)
511 struct nsm_host *host;
515 if (!(dir = opendir(dirname))) {
521 while ((de = readdir(dir)) != NULL) {
525 if (de->d_name[0] == '.')
528 host = calloc(1, sizeof(*host));
530 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
531 if (stat(path, &stb) < 0)
534 host->ai = host_lookup(AF_UNSPEC, de->d_name);
537 "%s doesn't seem to be a valid address, skipped",
543 host->last_used = stb.st_mtime;
544 host->timeout = NSM_TIMEOUT;
545 host->path = strdup(path);
546 host->name = strdup(de->d_name);
547 host->retries = 100; /* force address retry */
559 * Insert host into sorted list
562 insert_host(struct nsm_host *host)
564 struct nsm_host **where, *p;
567 while ((p = *where) != 0) {
568 /* Sort in ascending order of timeout */
569 if (host->send_next < p->send_next)
571 /* If we have the same timeout, put the
572 * most recently used host first.
573 * This makes sure that "recent" hosts
574 * get notified first.
576 if (host->send_next == p->send_next
577 && host->last_used > p->last_used)
587 * Find host given the XID
590 find_host(uint32_t xid)
592 struct nsm_host **where, *p;
595 while ((p = *where) != 0) {
607 * Retrieve the current NSM state
610 nsm_get_state(int update)
612 char newfile[PATH_MAX];
615 if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
617 nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
618 nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
624 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
626 "%s: bad file size, setting state = 1",
639 snprintf(newfile, sizeof(newfile),
640 "%s.new", _SM_STATE_PATH);
641 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
642 nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
645 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
647 "Failed to write state to %s", newfile);
651 if (rename(newfile, _SM_STATE_PATH) < 0) {
653 "Cannot create %s: %m", _SM_STATE_PATH);
663 * Address handling utilities
667 addr_get_port(nsm_address *addr)
669 switch (((struct sockaddr *) addr)->sa_family) {
671 return ntohs(((struct sockaddr_in *) addr)->sin_port);
673 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
679 addr_set_port(nsm_address *addr, int port)
681 switch (((struct sockaddr *) addr)->sa_family) {
683 ((struct sockaddr_in *) addr)->sin_port = htons(port);
686 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
690 static struct addrinfo *
691 host_lookup(int af, const char *name)
693 struct addrinfo hints, *ai;
695 memset(&hints, 0, sizeof(hints));
696 hints.ai_family = af;
697 hints.ai_protocol = IPPROTO_UDP;
699 if (getaddrinfo(name, NULL, &hints, &ai) != 0)
709 nsm_log(int fac, const char *fmt, ...)
713 if (fac == LOG_DEBUG && !opt_debug)
718 vsyslog(fac, fmt, ap);
720 vfprintf(stderr, fmt, ap);
727 * Record pid in /var/run/sm-notify.pid
728 * This file should remain until a reboot, even if the
730 * If file already exists, fail.
732 static int record_pid()
737 snprintf(pid, 20, "%d\n", getpid());
738 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
741 write(fd, pid, strlen(pid));
746 /* Drop privileges to match owner of state-directory
747 * (in case a reply triggers some unknown bug).
749 static void drop_privs(void)
753 if (stat(_SM_DIR_PATH, &st) == -1 &&
754 stat(_SM_BASE_PATH, &st) == -1) {
759 if (st.st_uid == 0) {
761 "sm-notify running as root. chown %s to choose different user\n",
767 if (setgid(st.st_gid) == -1
768 || setuid(st.st_uid) == -1) {
769 nsm_log(LOG_ERR, "Fail to drop privileges");
774 static void set_kernel_nsm_state(int state)
778 fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
781 snprintf(buf, sizeof(buf), "%d", state);
782 write(fd, buf, strlen(buf));