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>
28 #define BASEDIR "/var/lib/nfs"
31 #define DEFAULT_SM_STATE_PATH BASEDIR "/state"
32 #define DEFAULT_SM_DIR_PATH BASEDIR "/sm"
33 #define DEFAULT_SM_BAK_PATH DEFAULT_SM_DIR_PATH ".bak"
35 char *_SM_BASE_PATH = BASEDIR;
36 char *_SM_STATE_PATH = DEFAULT_SM_STATE_PATH;
37 char *_SM_DIR_PATH = DEFAULT_SM_DIR_PATH;
38 char *_SM_BAK_PATH = DEFAULT_SM_BAK_PATH;
40 #define NSM_PROG 100024
41 #define NSM_PROGRAM 100024
45 #define NSM_MAX_TIMEOUT 120 /* don't make this too big */
46 #define MAXMSGSIZE 256
48 typedef struct sockaddr_storage nsm_address;
51 struct nsm_host * next;
62 static char nsm_hostname[256];
63 static uint32_t nsm_state;
64 static int opt_debug = 0;
65 static int opt_quiet = 0;
66 static int opt_update_state = 1;
67 static unsigned int opt_max_retry = 15 * 60;
68 static char * opt_srcaddr = 0;
69 static uint16_t opt_srcport = 0;
70 static int log_syslog = 0;
72 static unsigned int nsm_get_state(int);
73 static void notify(void);
74 static void notify_host(int, struct nsm_host *);
75 static void recv_reply(int);
76 static void backup_hosts(const char *, const char *);
77 static void get_hosts(const char *);
78 static void insert_host(struct nsm_host *);
79 struct nsm_host * find_host(uint32_t);
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, ...);
84 static int record_pid();
85 static void drop_privs(void);
87 static struct nsm_host * hosts = NULL;
90 main(int argc, char **argv)
95 while ((c = getopt(argc, argv, "dm:np:v:qP:f")) != -1) {
104 opt_max_retry = atoi(optarg) * 60;
107 opt_update_state = 0;
110 opt_srcport = atoi(optarg);
113 opt_srcaddr = optarg;
119 _SM_BASE_PATH = strdup(optarg);
120 _SM_STATE_PATH = malloc(strlen(optarg)+1+sizeof("state"));
121 _SM_DIR_PATH = malloc(strlen(optarg)+1+sizeof("sm"));
122 _SM_BAK_PATH = malloc(strlen(optarg)+1+sizeof("sm.bak"));
123 if (_SM_BASE_PATH == NULL ||
124 _SM_STATE_PATH == NULL ||
125 _SM_DIR_PATH == NULL ||
126 _SM_BAK_PATH == NULL) {
127 nsm_log(LOG_WARNING, "unable to allocate memory");
130 strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
131 strcat(strcpy(_SM_DIR_PATH, _SM_BASE_PATH), "/sm");
132 strcat(strcpy(_SM_BAK_PATH, _SM_BASE_PATH), "/sm.bak");
141 usage: fprintf(stderr,
142 "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
143 " [-P /path/to/state/directory] [-N my_host_name\n");
147 if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) {
148 if (record_pid() == 0 && force == 0 && opt_update_state == 0)
149 /* already run, don't try again */
154 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
156 if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
157 perror("gethostname");
161 backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
162 get_hosts(_SM_BAK_PATH);
166 printf("Backgrounding to notify hosts...\n");
168 openlog("sm-notify", LOG_PID, LOG_DAEMON);
171 if (daemon(0, 0) < 0) {
172 nsm_log(LOG_WARNING, "unable to background: %s",
182 /* Get and update the NSM state. This will call sync() */
183 nsm_state = nsm_get_state(opt_update_state);
190 while ((hp = hosts) != 0) {
193 "Unable to notify %s, giving up",
208 nsm_address local_addr;
212 sock = socket(AF_INET, SOCK_DGRAM, 0);
217 fcntl(sock, F_SETFL, O_NONBLOCK);
219 memset(&local_addr, 0, sizeof(local_addr));
220 local_addr.ss_family = AF_INET; /* Default to IPv4 */
222 /* Bind source IP if provided on command line */
224 if (!host_lookup(AF_INET, opt_srcaddr, &local_addr)) {
226 "Not a valid hostname or address: \"%s\"\n",
230 /* We know it's IPv4 at this point */
233 /* Use source port if provided on the command line,
234 * otherwise use bindresvport */
236 addr_set_port(&local_addr, opt_srcport);
237 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
242 (void) bindresvport(sock, (struct sockaddr_in *) &local_addr);
246 failtime = time(NULL) + opt_max_retry;
252 time_t now = time(NULL);
253 unsigned int sent = 0;
257 if (failtime && now >= failtime)
260 while ((wait = hosts->send_next - now) <= 0) {
261 /* Never send more than 10 packets at once */
265 /* Remove queue head */
269 notify_host(sock, hp);
271 /* Set the timeout for this call, using an
272 exponential timeout strategy */
274 if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
275 hp->timeout = NSM_MAX_TIMEOUT;
276 hp->send_next = now + wait;
282 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
291 if (poll(&pfd, 1, wait) != 1)
299 * Send notification to a single host
302 notify_host(int sock, struct nsm_host *host)
304 static unsigned int xid = 0;
306 uint32_t msgbuf[MAXMSGSIZE], *p;
310 xid = getpid() + time(NULL);
314 memset(msgbuf, 0, sizeof(msgbuf));
316 *p++ = htonl(host->xid);
320 /* If we retransmitted 4 times, reset the port to force
321 * a new portmap lookup (in case statd was restarted)
323 if (host->retries >= 4) {
324 addr_set_port(&host->addr, 0);
329 if (addr_get_port(&dest) == 0) {
330 /* Build a PMAP packet */
331 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
333 addr_set_port(&dest, 111);
334 *p++ = htonl(100000);
342 *p++ = htonl(NSM_PROGRAM);
343 *p++ = htonl(NSM_VERSION);
344 *p++ = htonl(IPPROTO_UDP);
347 /* Build an SM_NOTIFY packet */
348 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
350 *p++ = htonl(NSM_PROGRAM);
351 *p++ = htonl(NSM_VERSION);
352 *p++ = htonl(NSM_NOTIFY);
359 len = strlen(nsm_hostname);
361 memcpy(p, nsm_hostname, len);
363 *p++ = htonl(nsm_state);
365 len = (p - msgbuf) << 2;
367 sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest));
371 * Receive reply from remote host
377 uint32_t msgbuf[MAXMSGSIZE], *p, *end;
381 res = recv(sock, msgbuf, sizeof(msgbuf), 0);
385 nsm_log(LOG_DEBUG, "Received packet...");
388 end = p + (res >> 2);
391 if (*p++ != htonl(1) /* must be REPLY */
392 || *p++ != htonl(0) /* must be ACCEPTED */
393 || *p++ != htonl(0) /* must be NULL verifier */
395 || *p++ != htonl(0)) /* must be SUCCESS */
398 /* Before we look at the data, find the host struct for
400 if ((hp = find_host(xid)) == NULL)
403 if (addr_get_port(&hp->addr) == 0) {
404 /* This was a portmap request */
411 hp->send_next = time(NULL);
413 /* No binding for statd. Delay the next
414 * portmap query for max timeout */
415 nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
416 hp->timeout = NSM_MAX_TIMEOUT;
417 hp->send_next += NSM_MAX_TIMEOUT;
419 addr_set_port(&hp->addr, port);
420 if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
421 hp->timeout = NSM_MAX_TIMEOUT / 4;
425 /* Successful NOTIFY call. Server returns void,
426 * so nothing we need to do here (except
427 * check that we didn't read past the end of the
431 nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
440 fail: /* Re-insert the host */
445 * Back up all hosts from the sm directory to sm.bak
448 backup_hosts(const char *dirname, const char *bakname)
453 if (!(dir = opendir(dirname))) {
458 while ((de = readdir(dir)) != NULL) {
459 char src[1024], dst[1024];
461 if (de->d_name[0] == '.')
464 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
465 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
466 if (rename(src, dst) < 0) {
468 "Failed to rename %s -> %s: %m",
476 * Get all entries from sm.bak and convert them to host names
479 get_hosts(const char *dirname)
481 struct nsm_host *host;
485 if (!(dir = opendir(dirname))) {
491 while ((de = readdir(dir)) != NULL) {
495 if (de->d_name[0] == '.')
498 host = calloc(1, sizeof(*host));
500 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
501 if (!host_lookup(AF_UNSPEC, de->d_name, &host->addr)) {
503 "%s doesn't seem to be a valid address, skipped",
509 if (stat(path, &stb) < 0)
511 host->last_used = stb.st_mtime;
512 host->timeout = NSM_TIMEOUT;
513 host->path = strdup(path);
514 host->name = strdup(de->d_name);
526 * Insert host into sorted list
529 insert_host(struct nsm_host *host)
531 struct nsm_host **where, *p;
534 while ((p = *where) != 0) {
535 /* Sort in ascending order of timeout */
536 if (host->send_next < p->send_next)
538 /* If we have the same timeout, put the
539 * most recently used host first.
540 * This makes sure that "recent" hosts
541 * get notified first.
543 if (host->send_next == p->send_next
544 && host->last_used > p->last_used)
554 * Find host given the XID
557 find_host(uint32_t xid)
559 struct nsm_host **where, *p;
562 while ((p = *where) != 0) {
574 * Retrieve the current NSM state
577 nsm_get_state(int update)
579 char newfile[PATH_MAX];
582 if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
584 nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
585 nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
591 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
593 "%s: bad file size, setting state = 1",
606 snprintf(newfile, sizeof(newfile),
607 "%s.new", _SM_STATE_PATH);
608 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
609 nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
612 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
614 "Failed to write state to %s", newfile);
618 if (rename(newfile, _SM_STATE_PATH) < 0) {
620 "Cannot create %s: %m", _SM_STATE_PATH);
630 * Address handling utilities
634 addr_get_port(nsm_address *addr)
636 switch (((struct sockaddr *) addr)->sa_family) {
638 return ntohs(((struct sockaddr_in *) addr)->sin_port);
640 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
646 addr_set_port(nsm_address *addr, int port)
648 switch (((struct sockaddr *) addr)->sa_family) {
650 ((struct sockaddr_in *) addr)->sin_port = htons(port);
653 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
658 host_lookup(int af, const char *name, nsm_address *addr)
660 struct addrinfo hints, *ai;
663 memset(&hints, 0, sizeof(hints));
664 hints.ai_family = af;
666 if (getaddrinfo(name, NULL, &hints, &ai) != 0)
669 if (ai->ai_addrlen < sizeof(*addr)) {
670 memcpy(addr, ai->ai_addr, ai->ai_addrlen);
682 nsm_log(int fac, const char *fmt, ...)
686 if (fac == LOG_DEBUG && !opt_debug)
691 vsyslog(fac, fmt, ap);
693 vfprintf(stderr, fmt, ap);
700 * Record pid in /var/run/sm-notify.pid
701 * This file should remain until a reboot, even if the
703 * If file already exists, fail.
705 static int record_pid()
710 snprintf(pid, 20, "%d\n", getpid());
711 fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
714 write(fd, pid, strlen(pid));
719 /* Drop privileges to match owner of state-directory
720 * (in case a reply triggers some unknown bug).
722 static void drop_privs(void)
726 if (stat(_SM_DIR_PATH, &st) == -1 &&
727 stat(_SM_BASE_PATH, &st) == -1) {
732 if (st.st_uid == 0) {
734 "sm-notify running as root. chown %s to choose different user\n",
740 if (setgid(st.st_gid) == -1
741 || setuid(st.st_uid) == -1) {
742 nsm_log(LOG_ERR, "Fail to drop privileges");