b69f4cca886cd19086ee14496acc2c0f083a48e3
[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
245                 /* We know it's IPv4 at this point */
246                 memcpy(&local_addr, ai->ai_addr, ai->ai_addrlen);
247
248                 freeaddrinfo(ai);
249         }
250
251         /* Use source port if provided on the command line,
252          * otherwise use bindresvport */
253         if (opt_srcport) {
254                 addr_set_port(&local_addr, opt_srcport);
255                 if (bind(sock, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0) {
256                         perror("bind");
257                         exit(1);
258                 }
259         } else {
260                 struct servent *se;
261                 struct sockaddr_in *sin = (struct sockaddr_in *)&local_addr;
262                 (void) bindresvport(sock, sin);
263                 /* try to avoid known ports */
264                 se = getservbyport(sin->sin_port, "udp");
265                 if (se && retry_cnt < 100) {
266                         retry_cnt++;
267                         close(sock);
268                         goto retry;
269                 }
270         }
271
272         if (opt_max_retry)
273                 failtime = time(NULL) + opt_max_retry;
274
275         drop_privs();
276
277         while (hosts) {
278                 struct pollfd   pfd;
279                 time_t          now = time(NULL);
280                 unsigned int    sent = 0;
281                 struct nsm_host *hp;
282                 long            wait;
283
284                 if (failtime && now >= failtime)
285                         break;
286
287                 while (hosts && ((wait = hosts->send_next - now) <= 0)) {
288                         /* Never send more than 10 packets at once */
289                         if (sent++ >= 10)
290                                 break;
291
292                         /* Remove queue head */
293                         hp = hosts;
294                         hosts = hp->next;
295
296                         if (notify_host(sock, hp)){
297                                 unlink(hp->path);
298                                 free(hp->name);
299                                 free(hp->path);
300                                 free(hp);
301                                 continue;
302                         }
303
304                         /* Set the timeout for this call, using an
305                            exponential timeout strategy */
306                         wait = hp->timeout;
307                         if ((hp->timeout <<= 1) > NSM_MAX_TIMEOUT)
308                                 hp->timeout = NSM_MAX_TIMEOUT;
309                         hp->send_next = now + wait;
310                         hp->retries++;
311
312                         insert_host(hp);
313                 }
314                 if (hosts == NULL)
315                         return;
316
317                 nsm_log(LOG_DEBUG, "Host %s due in %ld seconds",
318                                 hosts->name, wait);
319
320                 pfd.fd = sock;
321                 pfd.events = POLLIN;
322
323                 wait *= 1000;
324                 if (wait < 100)
325                         wait = 100;
326                 if (poll(&pfd, 1, wait) != 1)
327                         continue;
328
329                 recv_reply(sock);
330         }
331 }
332
333 /*
334  * Send notification to a single host
335  */
336 int
337 notify_host(int sock, struct nsm_host *host)
338 {
339         static unsigned int     xid = 0;
340         nsm_address             dest;
341         uint32_t                msgbuf[MAXMSGSIZE], *p;
342         unsigned int            len;
343
344         if (!xid)
345                 xid = getpid() + time(NULL);
346         if (!host->xid)
347                 host->xid = xid++;
348
349         if (host->ai == NULL) {
350                 host->ai = host_lookup(AF_UNSPEC, host->name);
351                 if (host->ai == NULL) {
352                         nsm_log(LOG_WARNING,
353                                 "%s doesn't seem to be a valid address,"
354                                 " skipped", host->name);
355                         return 1;
356                 }
357         }
358
359         memset(msgbuf, 0, sizeof(msgbuf));
360         p = msgbuf;
361         *p++ = htonl(host->xid);
362         *p++ = 0;
363         *p++ = htonl(2);
364
365         /* If we retransmitted 4 times, reset the port to force
366          * a new portmap lookup (in case statd was restarted).
367          * We also rotate through multiple IP addresses at this
368          * point.
369          */
370         if (host->retries >= 4) {
371                 struct addrinfo *first = host->ai;
372                 struct addrinfo **next = &host->ai;
373
374                 /* remove the first entry from the list */
375                 host->ai = first->ai_next;
376                 first->ai_next = NULL;
377                 /* find the end of the list */
378                 next = &first->ai_next;
379                 while ( *next )
380                         next = & (*next)->ai_next;
381                 /* put first entry at end */
382                 *next = first;
383                 memcpy(&host->addr, first->ai_addr, first->ai_addrlen);
384                 addr_set_port(&host->addr, 0);
385                 host->retries = 0;
386         }
387
388         dest = host->addr;
389         if (addr_get_port(&dest) == 0) {
390                 /* Build a PMAP packet */
391                 nsm_log(LOG_DEBUG, "Sending portmap query to %s", host->name);
392
393                 addr_set_port(&dest, 111);
394                 *p++ = htonl(100000);
395                 *p++ = htonl(2);
396                 *p++ = htonl(3);
397
398                 /* Auth and verf */
399                 *p++ = 0; *p++ = 0;
400                 *p++ = 0; *p++ = 0;
401
402                 *p++ = htonl(NSM_PROGRAM);
403                 *p++ = htonl(NSM_VERSION);
404                 *p++ = htonl(IPPROTO_UDP);
405                 *p++ = 0;
406         } else {
407                 /* Build an SM_NOTIFY packet */
408                 nsm_log(LOG_DEBUG, "Sending SM_NOTIFY to %s", host->name);
409
410                 *p++ = htonl(NSM_PROGRAM);
411                 *p++ = htonl(NSM_VERSION);
412                 *p++ = htonl(NSM_NOTIFY);
413
414                 /* Auth and verf */
415                 *p++ = 0; *p++ = 0;
416                 *p++ = 0; *p++ = 0;
417
418                 /* state change */
419                 len = strlen(nsm_hostname);
420                 *p++ = htonl(len);
421                 memcpy(p, nsm_hostname, len);
422                 p += (len + 3) >> 2;
423                 *p++ = htonl(nsm_state);
424         }
425         len = (p - msgbuf) << 2;
426
427         if (sendto(sock, msgbuf, len, 0, (struct sockaddr *) &dest, sizeof(dest)) < 0)
428                 nsm_log(LOG_WARNING, "Sending Reboot Notification to "
429                         "'%s' failed: errno %d (%s)", host->name, errno, strerror(errno));
430         
431         return 0;
432 }
433
434 /*
435  * Receive reply from remote host
436  */
437 void
438 recv_reply(int sock)
439 {
440         struct nsm_host *hp;
441         uint32_t        msgbuf[MAXMSGSIZE], *p, *end;
442         uint32_t        xid;
443         int             res;
444
445         res = recv(sock, msgbuf, sizeof(msgbuf), 0);
446         if (res < 0)
447                 return;
448
449         nsm_log(LOG_DEBUG, "Received packet...");
450
451         p = msgbuf;
452         end = p + (res >> 2);
453
454         xid = ntohl(*p++);
455         if (*p++ != htonl(1)    /* must be REPLY */
456          || *p++ != htonl(0)    /* must be ACCEPTED */
457          || *p++ != htonl(0)    /* must be NULL verifier */
458          || *p++ != htonl(0)
459          || *p++ != htonl(0))   /* must be SUCCESS */
460                 return;
461
462         /* Before we look at the data, find the host struct for
463            this reply */
464         if ((hp = find_host(xid)) == NULL)
465                 return;
466
467         if (addr_get_port(&hp->addr) == 0) {
468                 /* This was a portmap request */
469                 unsigned int    port;
470
471                 port = ntohl(*p++);
472                 if (p > end)
473                         goto fail;
474
475                 hp->send_next = time(NULL);
476                 if (port == 0) {
477                         /* No binding for statd. Delay the next
478                          * portmap query for max timeout */
479                         nsm_log(LOG_DEBUG, "No statd on %s", hp->name);
480                         hp->timeout = NSM_MAX_TIMEOUT;
481                         hp->send_next += NSM_MAX_TIMEOUT;
482                 } else {
483                         addr_set_port(&hp->addr, port);
484                         if (hp->timeout >= NSM_MAX_TIMEOUT / 4)
485                                 hp->timeout = NSM_MAX_TIMEOUT / 4;
486                 }
487                 hp->xid = 0;
488         } else {
489                 /* Successful NOTIFY call. Server returns void,
490                  * so nothing we need to do here (except
491                  * check that we didn't read past the end of the
492                  * packet)
493                  */
494                 if (p <= end) {
495                         nsm_log(LOG_DEBUG, "Host %s notified successfully", hp->name);
496                         unlink(hp->path);
497                         free(hp->name);
498                         free(hp->path);
499                         free(hp);
500                         freeaddrinfo(hp->ai);
501                         return;
502                 }
503         }
504
505 fail:   /* Re-insert the host */
506         insert_host(hp);
507 }
508
509 /*
510  * Back up all hosts from the sm directory to sm.bak
511  */
512 static void
513 backup_hosts(const char *dirname, const char *bakname)
514 {
515         struct dirent   *de;
516         DIR             *dir;
517
518         if (!(dir = opendir(dirname))) {
519                 perror(dirname);
520                 return;
521         }
522
523         while ((de = readdir(dir)) != NULL) {
524                 char    src[1024], dst[1024];
525
526                 if (de->d_name[0] == '.')
527                         continue;
528
529                 snprintf(src, sizeof(src), "%s/%s", dirname, de->d_name);
530                 snprintf(dst, sizeof(dst), "%s/%s", bakname, de->d_name);
531                 if (rename(src, dst) < 0) {
532                         nsm_log(LOG_WARNING,
533                                 "Failed to rename %s -> %s: %m",
534                                 src, dst);
535                 }
536         }
537         closedir(dir);
538 }
539
540 /*
541  * Get all entries from sm.bak and convert them to host entries
542  */
543 static void
544 get_hosts(const char *dirname)
545 {
546         struct nsm_host *host;
547         struct dirent   *de;
548         DIR             *dir;
549
550         if (!(dir = opendir(dirname))) {
551                 perror(dirname);
552                 return;
553         }
554
555         host = NULL;
556         while ((de = readdir(dir)) != NULL) {
557                 struct stat     stb;
558                 char            path[1024];
559
560                 if (de->d_name[0] == '.')
561                         continue;
562                 if (host == NULL)
563                         host = calloc(1, sizeof(*host));
564
565                 snprintf(path, sizeof(path), "%s/%s", dirname, de->d_name);
566                 if (stat(path, &stb) < 0)
567                         continue;
568
569                 host->last_used = stb.st_mtime;
570                 host->timeout = NSM_TIMEOUT;
571                 host->path = strdup(path);
572                 host->name = strdup(de->d_name);
573                 host->retries = 100; /* force address retry */
574
575                 insert_host(host);
576                 host = NULL;
577         }
578         closedir(dir);
579
580         if (host)
581                 free(host);
582 }
583
584 /*
585  * Insert host into sorted list
586  */
587 void
588 insert_host(struct nsm_host *host)
589 {
590         struct nsm_host **where, *p;
591
592         where = &hosts;
593         while ((p = *where) != 0) {
594                 /* Sort in ascending order of timeout */
595                 if (host->send_next < p->send_next)
596                         break;
597                 /* If we have the same timeout, put the
598                  * most recently used host first.
599                  * This makes sure that "recent" hosts
600                  * get notified first.
601                  */
602                 if (host->send_next == p->send_next
603                  && host->last_used > p->last_used)
604                         break;
605                 where = &p->next;
606         }
607
608         host->next = *where;
609         *where = host;
610 }
611
612 /*
613  * Find host given the XID
614  */
615 struct nsm_host *
616 find_host(uint32_t xid)
617 {
618         struct nsm_host **where, *p;
619
620         where = &hosts;
621         while ((p = *where) != 0) {
622                 if (p->xid == xid) {
623                         *where = p->next;
624                         return p;
625                 }
626                 where = &p->next;
627         }
628         return NULL;
629 }
630
631
632 /*
633  * Retrieve the current NSM state
634  */
635 unsigned int
636 nsm_get_state(int update)
637 {
638         char            newfile[PATH_MAX];
639         int             fd, state;
640
641         if ((fd = open(_SM_STATE_PATH, O_RDONLY)) < 0) {
642                 if (!opt_quiet) {
643                         nsm_log(LOG_WARNING, "%s: %m", _SM_STATE_PATH);
644                         nsm_log(LOG_WARNING, "Creating %s, set initial state 1",
645                                 _SM_STATE_PATH);
646                 }
647                 state = 1;
648                 update = 1;
649         } else {
650                 if (read(fd, &state, sizeof(state)) != sizeof(state)) {
651                         nsm_log(LOG_WARNING,
652                                 "%s: bad file size, setting state = 1",
653                                 _SM_STATE_PATH);
654                         state = 1;
655                         update = 1;
656                 } else {
657                         if (!(state & 1))
658                                 state += 1;
659                 }
660                 close(fd);
661         }
662
663         if (update) {
664                 state += 2;
665                 snprintf(newfile, sizeof(newfile),
666                                 "%s.new", _SM_STATE_PATH);
667                 if ((fd = open(newfile, O_CREAT|O_WRONLY, 0644)) < 0) {
668                         nsm_log(LOG_WARNING, "Cannot create %s: %m", newfile);
669                         exit(1);
670                 }
671                 if (write(fd, &state, sizeof(state)) != sizeof(state)) {
672                         nsm_log(LOG_WARNING,
673                                 "Failed to write state to %s", newfile);
674                         exit(1);
675                 }
676                 close(fd);
677                 if (rename(newfile, _SM_STATE_PATH) < 0) {
678                         nsm_log(LOG_WARNING,
679                                 "Cannot create %s: %m", _SM_STATE_PATH);
680                         exit(1);
681                 }
682                 sync();
683         }
684
685         return state;
686 }
687
688 /*
689  * Address handling utilities
690  */
691
692 int
693 addr_get_port(nsm_address *addr)
694 {
695         switch (((struct sockaddr *) addr)->sa_family) {
696         case AF_INET:
697                 return ntohs(((struct sockaddr_in *) addr)->sin_port);
698         case AF_INET6:
699                 return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
700         }
701         return 0;
702 }
703
704 static void
705 addr_set_port(nsm_address *addr, int port)
706 {
707         switch (((struct sockaddr *) addr)->sa_family) {
708         case AF_INET:
709                 ((struct sockaddr_in *) addr)->sin_port = htons(port);
710                 break;
711         case AF_INET6:
712                 ((struct sockaddr_in6 *) addr)->sin6_port = htons(port);
713         }
714 }
715
716 static struct addrinfo *
717 host_lookup(int af, const char *name)
718 {
719         struct addrinfo hints, *ai;
720
721         memset(&hints, 0, sizeof(hints));
722         hints.ai_family = af;
723         hints.ai_protocol = IPPROTO_UDP;
724
725         if (getaddrinfo(name, NULL, &hints, &ai) != 0)
726                 return NULL;
727
728         return ai;
729 }
730
731 /*
732  * Log a message
733  */
734 void
735 nsm_log(int fac, const char *fmt, ...)
736 {
737         va_list ap;
738
739         if (fac == LOG_DEBUG && !opt_debug)
740                 return;
741
742         va_start(ap, fmt);
743         if (log_syslog)
744                 vsyslog(fac, fmt, ap);
745         else {
746                 vfprintf(stderr, fmt, ap);
747                 fputs("\n", stderr);
748         }
749         va_end(ap);
750 }
751
752 /*
753  * Record pid in /var/run/sm-notify.pid
754  * This file should remain until a reboot, even if the
755  * program exits.
756  * If file already exists, fail.
757  */
758 static int record_pid(void)
759 {
760         char pid[20];
761         int fd;
762
763         snprintf(pid, 20, "%d\n", getpid());
764         fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600);
765         if (fd < 0)
766                 return 0;
767         write(fd, pid, strlen(pid));
768         close(fd);
769         return 1;
770 }
771
772 /* Drop privileges to match owner of state-directory
773  * (in case a reply triggers some unknown bug).
774  */
775 static void drop_privs(void)
776 {
777         struct stat st;
778
779         if (stat(_SM_DIR_PATH, &st) == -1 &&
780             stat(_SM_BASE_PATH, &st) == -1) {
781                 st.st_uid = 0;
782                 st.st_gid = 0;
783         }
784
785         if (st.st_uid == 0) {
786                 nsm_log(LOG_WARNING,
787                         "sm-notify running as root. chown %s to choose different user\n",
788                     _SM_DIR_PATH);
789                 return;
790         }
791
792         setgroups(0, NULL);
793         if (setgid(st.st_gid) == -1
794             || setuid(st.st_uid) == -1) {
795                 nsm_log(LOG_ERR, "Fail to drop privileges");
796                 exit(1);
797         }
798 }
799
800 static void set_kernel_nsm_state(int state)
801 {
802         int fd;
803
804         fd = open("/proc/sys/fs/nfs/nsm_local_state",O_WRONLY);
805         if (fd >= 0) {
806                 char buf[20];
807                 snprintf(buf, sizeof(buf), "%d", state);
808                 write(fd, buf, strlen(buf));
809                 close(fd);
810         }
811 }