]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/sm-notify.c
sm-notify command: include <config.h>
[nfs-utils.git] / utils / statd / sm-notify.c
1 /*
2  * Send NSM notify calls to all hosts listed in /var/lib/sm
3  *
4  * Copyright (C) 2004-2006 Olaf Kirch <okir@suse.de>
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <sys/stat.h>
14 #include <sys/poll.h>
15 #include <sys/param.h>
16 #include <sys/syslog.h>
17 #include <arpa/inet.h>
18 #include <dirent.h>
19 #include <time.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <netdb.h>
28 #include <errno.h>
29 #include <grp.h>
30
31 #ifndef BASEDIR
32 # ifdef NFS_STATEDIR
33 #  define BASEDIR               NFS_STATEDIR
34 # else
35 #  define BASEDIR               "/var/lib/nfs"
36 # endif
37 #endif
38
39 #define DEFAULT_SM_STATE_PATH   BASEDIR "/state"
40 #define DEFAULT_SM_DIR_PATH     BASEDIR "/sm"
41 #define DEFAULT_SM_BAK_PATH     DEFAULT_SM_DIR_PATH ".bak"
42
43 char *_SM_BASE_PATH = BASEDIR;
44 char *_SM_STATE_PATH = DEFAULT_SM_STATE_PATH;
45 char *_SM_DIR_PATH = DEFAULT_SM_DIR_PATH;
46 char *_SM_BAK_PATH = DEFAULT_SM_BAK_PATH;
47
48 #define NSM_PROG        100024
49 #define NSM_PROGRAM     100024
50 #define NSM_VERSION     1
51 #define NSM_TIMEOUT     2
52 #define NSM_NOTIFY      6
53 #define NSM_MAX_TIMEOUT 120     /* don't make this too big */
54 #define MAXMSGSIZE      256
55
56 typedef struct sockaddr_storage nsm_address;
57
58 struct nsm_host {
59         struct nsm_host *       next;
60         char *                  name;
61         char *                  path;
62         nsm_address             addr;
63         struct addrinfo         *ai;
64         time_t                  last_used;
65         time_t                  send_next;
66         unsigned int            timeout;
67         unsigned int            retries;
68         unsigned int            xid;
69 };
70
71 static char             nsm_hostname[256];
72 static uint32_t         nsm_state;
73 static int              opt_debug = 0;
74 static int              opt_quiet = 0;
75 static int              opt_update_state = 1;
76 static unsigned int     opt_max_retry = 15 * 60;
77 static char *           opt_srcaddr = 0;
78 static uint16_t         opt_srcport = 0;
79 static int              log_syslog = 0;
80
81 static unsigned int     nsm_get_state(int);
82 static void             notify(void);
83 static int              notify_host(int, struct nsm_host *);
84 static void             recv_reply(int);
85 static void             backup_hosts(const char *, const char *);
86 static void             get_hosts(const char *);
87 static void             insert_host(struct nsm_host *);
88 struct nsm_host *       find_host(uint32_t);
89 static int              addr_get_port(nsm_address *);
90 static void             addr_set_port(nsm_address *, int);
91 static struct addrinfo  *host_lookup(int, const char *);
92 void                    nsm_log(int fac, const char *fmt, ...);
93 static int              record_pid(void);
94 static void             drop_privs(void);
95 static void set_kernel_nsm_state(int state);
96
97 static struct nsm_host *        hosts = NULL;
98
99 int
100 main(int argc, char **argv)
101 {
102         int     c;
103         int     force = 0;
104
105         while ((c = getopt(argc, argv, "dm:np:v:qP:f")) != -1) {
106                 switch (c) {
107                 case 'f':
108                         force = 1;
109                         break;
110                 case 'd':
111                         opt_debug++;
112                         break;
113                 case 'm':
114                         opt_max_retry = atoi(optarg) * 60;
115                         break;
116                 case 'n':
117                         opt_update_state = 0;
118                         break;
119                 case 'p':
120                         opt_srcport = atoi(optarg);
121                         break;
122                 case 'v':
123                         opt_srcaddr = optarg;
124                         break;
125                 case 'q':
126                         opt_quiet = 1;
127                         break;
128                 case 'P':
129                         _SM_BASE_PATH = strdup(optarg);
130                         _SM_STATE_PATH = malloc(strlen(optarg)+1+sizeof("state"));
131                         _SM_DIR_PATH = malloc(strlen(optarg)+1+sizeof("sm"));
132                         _SM_BAK_PATH = malloc(strlen(optarg)+1+sizeof("sm.bak"));
133                         if (_SM_BASE_PATH == NULL ||
134                             _SM_STATE_PATH == NULL ||
135                             _SM_DIR_PATH == NULL ||
136                             _SM_BAK_PATH == NULL) {
137                                 nsm_log(LOG_WARNING, "unable to allocate memory");
138                                 exit(1);
139                         }
140                         strcat(strcpy(_SM_STATE_PATH, _SM_BASE_PATH), "/state");
141                         strcat(strcpy(_SM_DIR_PATH, _SM_BASE_PATH), "/sm");
142                         strcat(strcpy(_SM_BAK_PATH, _SM_BASE_PATH), "/sm.bak");
143                         break;
144
145                 default:
146                         goto usage;
147                 }
148         }
149
150         if (optind < argc) {
151 usage:          fprintf(stderr,
152                         "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n"
153                         "            [-P /path/to/state/directory] [-v my_host_name]\n");
154                 return 1;
155         }
156
157         if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) {
158                 if (record_pid() == 0 && force == 0 && opt_update_state == 1)
159                         /* already run, don't try again */
160                         exit(0);
161         }
162
163         if (opt_srcaddr) {
164                 strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1);
165         } else
166         if (gethostname(nsm_hostname, sizeof(nsm_hostname)) < 0) {
167                 perror("gethostname");
168                 return 1;
169         }
170
171         backup_hosts(_SM_DIR_PATH, _SM_BAK_PATH);
172         get_hosts(_SM_BAK_PATH);
173
174         /* Get and update the NSM state. This will call sync() */
175         nsm_state = nsm_get_state(opt_update_state);
176         set_kernel_nsm_state(nsm_state);
177
178         if (!opt_debug) {
179                 if (!opt_quiet)
180                         printf("Backgrounding to notify hosts...\n");
181
182                 openlog("sm-notify", LOG_PID, LOG_DAEMON);
183                 log_syslog = 1;
184
185                 if (daemon(0, 0) < 0) {
186                         nsm_log(LOG_WARNING, "unable to background: %s",
187                                         strerror(errno));
188                         return 1;
189                 }
190
191                 close(0);
192                 close(1);
193                 close(2);
194         }
195
196         notify();
197
198         if (hosts) {
199                 struct nsm_host *hp;
200
201                 while ((hp = hosts) != 0) {
202                         hosts = hp->next;
203                         nsm_log(LOG_NOTICE,
204                                 "Unable to notify %s, giving up",
205                                 hp->name);
206                 }
207                 return 1;
208         }
209
210         return 0;
211 }
212
213 /*
214  * Notify hosts
215  */
216 void
217 notify(void)
218 {
219         nsm_address local_addr;
220         time_t  failtime = 0;
221         int     sock = -1;
222         int retry_cnt = 0;
223
224  retry:
225         sock = socket(AF_INET, SOCK_DGRAM, 0);
226         if (sock < 0) {
227                 perror("socket");
228                 exit(1);
229         }
230         fcntl(sock, F_SETFL, O_NONBLOCK);
231
232         memset(&local_addr, 0, sizeof(local_addr));
233         local_addr.ss_family = AF_INET; /* Default to IPv4 */
234
235         /* Bind source IP if provided on command line */
236         if (opt_srcaddr) {
237                 struct addrinfo *ai = host_lookup(AF_INET, opt_srcaddr);
238                 if (!ai) {
239                         nsm_log(LOG_WARNING,
240                                 "Not a valid hostname or address: \"%s\"\n",
241                                 opt_srcaddr);
242                         exit(1);
243                 }
244                 memcpy(&local_addr, ai->ai_addr, ai->ai_addrlen);
245                 /* We know it's IPv4 at this point */
246         }
247
248         /* Use source port if provided on the command line,
249          * otherwise use bindresvport */
250         if (opt_srcport) {
251                 addr_set_port(&local_addr, opt_srcport);
252                 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
253                         perror("bind");
254                         exit(1);
255                 }
256         } else {
257                 struct servent *se;
258                 struct sockaddr_in *sin = (struct sockaddr_in *)&local_addr;
259                 (void) bindresvport(sock, sin);
260                 /* try to avoid known ports */
261                 se = getservbyport(sin->sin_port, "udp");
262                 if (se && retry_cnt < 100) {
263                         retry_cnt++;
264                         close(sock);
265                         goto retry;
266                 }
267         }
268
269         if (opt_max_retry)
270                 failtime = time(NULL) + opt_max_retry;
271
272         drop_privs();
273
274         while (hosts) {
275                 struct pollfd   pfd;
276                 time_t          now = time(NULL);
277                 unsigned int    sent = 0;
278                 struct nsm_host *hp;
279                 long            wait;
280
281                 if (failtime && now >= failtime)
282                         break;
283
284                 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
285                         /* Never send more than 10 packets at once */
286                         if (sent++ >= 10)
287                                 break;
288
289                         /* Remove queue head */
290                         hp = hosts;
291                         hosts = hp->next;
292
293                         if (notify_host(sock, hp)){
294                                 unlink(hp->path);
295                                 free(hp->name);
296                                 free(hp->path);
297                                 free(hp);
298                                 continue;
299                         }
300
301                         /* Set the timeout for this call, using an
302                            exponential timeout strategy */
303                         wait = hp->timeout;
304                         if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
305                                 hp->timeout = NSM_MAX_TIMEOUT;
306                         hp->send_next = now + wait;
307                         hp->retries++;
308
309                         insert_host(hp);
310                 }
311                 if (hosts == NULL)
312                         return;
313
314                 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
315                                 hosts->name, wait);
316
317                 pfd.fd = sock;
318                 pfd.events = POLLIN;
319
320                 wait *= 1000;
321                 if (wait < 100)
322                         wait = 100;
323                 if (poll(&pfd, 1, wait) != 1)
324                         continue;
325
326                 recv_reply(sock);
327         }
328 }
329
330 /*
331  * Send notification to a single host
332  */
333 int
334 notify_host(int sock, struct nsm_host *host)
335 {
336         static unsigned int     xid = 0;
337         nsm_address             dest;
338         uint32_t                msgbuf[MAXMSGSIZE], *p;
339         unsigned int            len;
340
341         if (!xid)
342                 xid = getpid() + time(NULL);
343         if (!host->xid)
344                 host->xid = xid++;
345
346         if (host->ai == NULL) {
347                 host->ai = host_lookup(AF_UNSPEC, host->name);
348                 if (host->ai == NULL) {
349                         nsm_log(LOG_WARNING,
350                                 "%s doesn't seem to be a valid address,"
351                                 " skipped", host->name);
352                         return 1;
353                 }
354         }
355
356         memset(msgbuf, 0, sizeof(msgbuf));
357         p = msgbuf;
358         *p++ = htonl(host->xid);
359         *p++ = 0;
360         *p++ = htonl(2);
361
362         /* If we retransmitted 4 times, reset the port to force
363          * a new portmap lookup (in case statd was restarted).
364          * We also rotate through multiple IP addresses at this
365          * point.
366          */
367         if (host->retries >= 4) {
368                 struct addrinfo *first = host->ai;
369                 struct addrinfo **next = &host->ai;
370
371                 /* remove the first entry from the list */
372                 host->ai = first->ai_next;
373                 first->ai_next = NULL;
374                 /* find the end of the list */
375                 next = &first->ai_next;
376                 while ( *next )
377                         next = & (*next)->ai_next;
378                 /* put first entry at end */
379                 *next = first;
380                 memcpy(&host->addr, first->ai_addr, first->ai_addrlen);
381                 addr_set_port(&host->addr, 0);
382                 host->retries = 0;
383         }
384
385         dest = host->addr;
386         if (addr_get_port(&dest) == 0) {
387                 /* Build a PMAP packet */
388                 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
389
390                 addr_set_port(&dest, 111);
391                 *p++ = htonl(100000);
392                 *p++ = htonl(2);
393                 *p++ = htonl(3);
394
395                 /* Auth and verf */
396                 *p++ = 0; *p++ = 0;
397                 *p++ = 0; *p++ = 0;
398
399                 *p++ = htonl(NSM_PROGRAM);
400                 *p++ = htonl(NSM_VERSION);
401                 *p++ = htonl(IPPROTO_UDP);
402                 *p++ = 0;
403         } else {
404                 /* Build an SM_NOTIFY packet */
405                 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
406
407                 *p++ = htonl(NSM_PROGRAM);
408                 *p++ = htonl(NSM_VERSION);
409                 *p++ = htonl(NSM_NOTIFY);
410
411                 /* Auth and verf */
412                 *p++ = 0; *p++ = 0;
413                 *p++ = 0; *p++ = 0;
414
415                 /* state change */
416                 len = strlen(nsm_hostname);
417                 *p++ = htonl(len);
418                 memcpy(p, nsm_hostname, len);
419                 p += (len + 3) >> 2;
420                 *p++ = htonl(nsm_state);
421         }
422         len = (p - msgbuf) << 2;
423
424         if (sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest)) < 0)
425                 nsm_log(LOG_WARNING, "Sending Reboot Notification to "
426                         "'%s' failed: errno %d (%s)", host->name, errno, strerror(errno));
427         
428         return 0;
429 }
430
431 /*
432  * Receive reply from remote host
433  */
434 void
435 recv_reply(int sock)
436 {
437         struct nsm_host *hp;
438         uint32_t        msgbuf[MAXMSGSIZE], *p, *end;
439         uint32_t        xid;
440         int             res;
441
442         res = recv(sock, msgbuf, sizeof(msgbuf), 0);
443         if (res < 0)
444                 return;
445
446         nsm_log(LOG_DEBUG, "Received packet...");
447
448         p = msgbuf;
449         end = p + (res >> 2);
450
451         xid = ntohl(*p++);
452         if (*p++ != htonl(1)    /* must be REPLY */
453          || *p++ != htonl(0)    /* must be ACCEPTED */
454          || *p++ != htonl(0)    /* must be NULL verifier */
455          || *p++ != htonl(0)
456          || *p++ != htonl(0))   /* must be SUCCESS */
457                 return;
458
459         /* Before we look at the data, find the host struct for
460            this reply */
461         if ((hp = find_host(xid)) == NULL)
462                 return;
463
464         if (addr_get_port(&hp->addr) == 0) {
465                 /* This was a portmap request */
466                 unsigned int    port;
467
468                 port = ntohl(*p++);
469                 if (p > end)
470                         goto fail;
471
472                 hp->send_next = time(NULL);
473                 if (port == 0) {
474                         /* No binding for statd. Delay the next
475                          * portmap query for max timeout */
476                         nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
477                         hp->timeout = NSM_MAX_TIMEOUT;
478                         hp->send_next += NSM_MAX_TIMEOUT;
479                 } else {
480                         addr_set_port(&hp->addr, port);
481                         if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
482                                 hp->timeout = NSM_MAX_TIMEOUT / 4;
483                 }
484                 hp->xid = 0;
485         } else {
486                 /* Successful NOTIFY call. Server returns void,
487                  * so nothing we need to do here (except
488                  * check that we didn't read past the end of the
489                  * packet)
490                  */
491                 if (p <= end) {
492                         nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
493                         unlink(hp->path);
494                         free(hp->name);
495                         free(hp->path);
496                         free(hp);
497                         freeaddrinfo(hp->ai);
498                         return;
499                 }
500         }
501
502 fail:   /* Re-insert the host */
503         insert_host(hp);
504 }
505
506 /*
507  * Back up all hosts from the sm directory to sm.bak
508  */
509 static void
510 backup_hosts(const char *dirname, const char *bakname)
511 {
512         struct dirent   *de;
513         DIR             *dir;
514
515         if (!(dir = opendir(dirname))) {
516                 perror(dirname);
517                 return;
518         }
519
520         while ((de = readdir(dir)) != NULL) {
521                 char    src[1024], dst[1024];
522
523                 if (de->d_name[0] == '.')
524                         continue;
525
526                 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
527                 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
528                 if (rename(src, dst) < 0) {
529                         nsm_log(LOG_WARNING,
530                                 "Failed to rename %s -> %s: %m",
531                                 src, dst);
532                 }
533         }
534         closedir(dir);
535 }
536
537 /*
538  * Get all entries from sm.bak and convert them to host entries
539  */
540 static void
541 get_hosts(const char *dirname)
542 {
543         struct nsm_host *host;
544         struct dirent   *de;
545         DIR             *dir;
546
547         if (!(dir = opendir(dirname))) {
548                 perror(dirname);
549                 return;
550         }
551
552         host = NULL;
553         while ((de = readdir(dir)) != NULL) {
554                 struct stat     stb;
555                 char            path[1024];
556
557                 if (de->d_name[0] == '.')
558                         continue;
559                 if (host == NULL)
560                         host = calloc(1, sizeof(*host));
561
562                 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
563                 if (stat(path, &stb) < 0)
564                         continue;
565
566                 host->last_used = stb.st_mtime;
567                 host->timeout = NSM_TIMEOUT;
568                 host->path = strdup(path);
569                 host->name = strdup(de->d_name);
570                 host->retries = 100; /* force address retry */
571
572                 insert_host(host);
573                 host = NULL;
574         }
575         closedir(dir);
576
577         if (host)
578                 free(host);
579 }
580
581 /*
582  * Insert host into sorted list
583  */
584 void
585 insert_host(struct nsm_host *host)
586 {
587         struct nsm_host **where, *p;
588
589         where = &hosts;
590         while ((p = *where) != 0) {
591                 /* Sort in ascending order of timeout */
592                 if (host->send_next < p->send_next)
593                         break;
594                 /* If we have the same timeout, put the
595                  * most recently used host first.
596                  * This makes sure that "recent" hosts
597                  * get notified first.
598                  */
599                 if (host->send_next == p->send_next
600                  && host->last_used > p->last_used)
601                         break;
602                 where = &p->next;
603         }
604
605         host->next = *where;
606         *where = host;
607 }
608
609 /*
610  * Find host given the XID
611  */
612 struct nsm_host *
613 find_host(uint32_t xid)
614 {
615         struct nsm_host **where, *p;
616
617         where = &hosts;
618         while ((p = *where) != 0) {
619                 if (p->xid == xid) {
620                         *where = p->next;
621                         return p;
622                 }
623                 where = &p->next;
624         }
625         return NULL;
626 }
627
628
629 /*
630  * Retrieve the current NSM state
631  */
632 unsigned int
633 nsm_get_state(int update)
634 {
635         char            newfile[PATH_MAX];
636         int             fd, state;
637
638         if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
639                 if (!opt_quiet) {
640                         nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
641                         nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
642                                 _SM_STATE_PATH);
643                 }
644                 state = 1;
645                 update = 1;
646         } else {
647                 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
648                         nsm_log(LOG_WARNING,
649                                 "%s: bad file size, setting state = 1",
650                                 _SM_STATE_PATH);
651                         state = 1;
652                         update = 1;
653                 } else {
654                         if (!(state & 1))
655                                 state += 1;
656                 }
657                 close(fd);
658         }
659
660         if (update) {
661                 state += 2;
662                 snprintf(newfile, sizeof(newfile),
663                                 "%s.new", _SM_STATE_PATH);
664                 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
665                         nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
666                         exit(1);
667                 }
668                 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
669                         nsm_log(LOG_WARNING,
670                                 "Failed to write state to %s", newfile);
671                         exit(1);
672                 }
673                 close(fd);
674                 if (rename(newfile, _SM_STATE_PATH) < 0) {
675                         nsm_log(LOG_WARNING,
676                                 "Cannot create %s: %m", _SM_STATE_PATH);
677                         exit(1);
678                 }
679                 sync();
680         }
681
682         return state;
683 }
684
685 /*
686  * Address handling utilities
687  */
688
689 int
690 addr_get_port(nsm_address *addr)
691 {
692         switch (((struct sockaddr *) addr)->sa_family) {
693         case AF_INET:
694                 return ntohs(((struct sockaddr_in *) addr)->sin_port);
695         case AF_INET6:
696                 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
697         }
698         return 0;
699 }
700
701 static void
702 addr_set_port(nsm_address *addr, int port)
703 {
704         switch (((struct sockaddr *) addr)->sa_family) {
705         case AF_INET:
706                 ((struct sockaddr_in *) addr)->sin_port = htons(port);
707                 break;
708         case AF_INET6:
709                 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
710         }
711 }
712
713 static struct addrinfo *
714 host_lookup(int af, const char *name)
715 {
716         struct addrinfo hints, *ai;
717
718         memset(&hints, 0, sizeof(hints));
719         hints.ai_family = af;
720         hints.ai_protocol = IPPROTO_UDP;
721
722         if (getaddrinfo(name, NULL, &hints, &ai) != 0)
723                 return NULL;
724
725         return ai;
726 }
727
728 /*
729  * Log a message
730  */
731 void
732 nsm_log(int fac, const char *fmt, ...)
733 {
734         va_list ap;
735
736         if (fac == LOG_DEBUG && !opt_debug)
737                 return;
738
739         va_start(ap, fmt);
740         if (log_syslog)
741                 vsyslog(fac, fmt, ap);
742         else {
743                 vfprintf(stderr, fmt, ap);
744                 fputs("\n", stderr);
745         }
746         va_end(ap);
747 }
748
749 /*
750  * Record pid in /var/run/sm-notify.pid
751  * This file should remain until a reboot, even if the
752  * program exits.
753  * If file already exists, fail.
754  */
755 static int record_pid(void)
756 {
757         char pid[20];
758         int fd;
759
760         snprintf(pid, 20, "%d\n", getpid());
761         fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
762         if (fd < 0)
763                 return 0;
764         write(fd, pid, strlen(pid));
765         close(fd);
766         return 1;
767 }
768
769 /* Drop privileges to match owner of state-directory
770  * (in case a reply triggers some unknown bug).
771  */
772 static void drop_privs(void)
773 {
774         struct stat st;
775
776         if (stat(_SM_DIR_PATH, &st) == -1 &&
777             stat(_SM_BASE_PATH, &st) == -1) {
778                 st.st_uid = 0;
779                 st.st_gid = 0;
780         }
781
782         if (st.st_uid == 0) {
783                 nsm_log(LOG_WARNING,
784                         "sm-notify running as root. chown %s to choose different user\n",
785                     _SM_DIR_PATH);
786                 return;
787         }
788
789         setgroups(0, NULL);
790         if (setgid(st.st_gid) == -1
791             || setuid(st.st_uid) == -1) {
792                 nsm_log(LOG_ERR, "Fail to drop privileges");
793                 exit(1);
794         }
795 }
796
797 static void set_kernel_nsm_state(int state)
798 {
799         int fd;
800
801         fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
802         if (fd >= 0) {
803                 char buf[20];
804                 snprintf(buf, sizeof(buf), "%d", state);
805                 write(fd, buf, strlen(buf));
806                 close(fd);
807         }
808 }