23e2344559bb3bec48583c3a15d0187c44c03a3d
[nfs-utils.git] / utils / mountd / cache.c
1
2 /*
3  * Handle communication with knfsd internal cache
4  *
5  * We open /proc/net/rpc/{auth.unix.ip,nfsd.export,nfsd.fh}/channel
6  * and listen for requests (using my_svc_run)
7  * 
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <sys/types.h>
15 #include <sys/select.h>
16 #include <sys/stat.h>
17 #include <sys/vfs.h>
18 #include <time.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <pwd.h>
26 #include <grp.h>
27 #include <mntent.h>
28 #include "misc.h"
29 #include "nfslib.h"
30 #include "exportfs.h"
31 #include "mountd.h"
32 #include "xmalloc.h"
33 #include "fsloc.h"
34 #include "pseudoflavors.h"
35
36 #ifdef USE_BLKID
37 #include "blkid/blkid.h"
38 #endif
39
40
41 enum nfsd_fsid {
42         FSID_DEV = 0,
43         FSID_NUM,
44         FSID_MAJOR_MINOR,
45         FSID_ENCODE_DEV,
46         FSID_UUID4_INUM,
47         FSID_UUID8,
48         FSID_UUID16,
49         FSID_UUID16_INUM,
50 };
51
52 /*
53  * Support routines for text-based upcalls.
54  * Fields are separated by spaces.
55  * Fields are either mangled to quote space tab newline slosh with slosh
56  * or a hexified with a leading \x
57  * Record is terminated with newline.
58  *
59  */
60 int cache_export_ent(char *domain, struct exportent *exp, char *p);
61
62
63 char *lbuf  = NULL;
64 int lbuflen = 0;
65 extern int use_ipaddr;
66
67 void auth_unix_ip(FILE *f)
68 {
69         /* requests are
70          *  class IP-ADDR
71          * Ignore if class != "nfsd"
72          * Otherwise find domainname and write back:
73          *
74          *  "nfsd" IP-ADDR expiry domainname
75          */
76         char *cp;
77         char class[20];
78         char ipaddr[20];
79         char *client = NULL;
80         struct in_addr addr;
81         struct hostent *he = NULL;
82         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
83                 return;
84
85         xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
86
87         cp = lbuf;
88
89         if (qword_get(&cp, class, 20) <= 0 ||
90             strcmp(class, "nfsd") != 0)
91                 return;
92
93         if (qword_get(&cp, ipaddr, 20) <= 0)
94                 return;
95
96         if (inet_aton(ipaddr, &addr)==0)
97                 return;
98
99         auth_reload();
100
101         /* addr is a valid, interesting address, find the domain name... */
102         if (!use_ipaddr) {
103                 he = client_resolve(addr);
104                 client = client_compose(he);
105         }
106         
107         qword_print(f, "nfsd");
108         qword_print(f, ipaddr);
109         qword_printint(f, time(0)+30*60);
110         if (use_ipaddr)
111                 qword_print(f, ipaddr);
112         else if (client)
113                 qword_print(f, *client?client:"DEFAULT");
114         qword_eol(f);
115         xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
116
117         if (client) free(client);
118         free(he);
119 }
120
121 void auth_unix_gid(FILE *f)
122 {
123         /* Request are
124          *  uid
125          * reply is
126          *  uid expiry count list of group ids
127          */
128         int uid;
129         struct passwd *pw;
130         gid_t glist[100], *groups = glist;
131         int ngroups = 100;
132         int rv, i;
133         char *cp;
134
135         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
136                 return;
137
138         cp = lbuf;
139         if (qword_get_int(&cp, &uid) != 0)
140                 return;
141
142         pw = getpwuid(uid);
143         if (!pw)
144                 rv = -1;
145         else {
146                 rv = getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
147                 if (rv == -1 && ngroups >= 100) {
148                         groups = malloc(sizeof(gid_t)*ngroups);
149                         if (!groups)
150                                 rv = -1;
151                         else
152                                 rv = getgrouplist(pw->pw_name, pw->pw_gid,
153                                                   groups, &ngroups);
154                 }
155         }
156         qword_printint(f, uid);
157         qword_printint(f, time(0)+30*60);
158         if (rv >= 0) {
159                 qword_printint(f, ngroups);
160                 for (i=0; i<ngroups; i++)
161                         qword_printint(f, groups[i]);
162         } else
163                 qword_printint(f, 0);
164         qword_eol(f);
165
166         if (groups != glist)
167                 free(groups);
168 }
169
170 #if USE_BLKID
171 static const char *get_uuid_blkdev(char *path)
172 {
173         static blkid_cache cache = NULL;
174         struct stat stb;
175         char *devname;
176         blkid_tag_iterate iter;
177         blkid_dev dev;
178         const char *type;
179         const char *val = NULL;
180
181         if (cache == NULL)
182                 blkid_get_cache(&cache, NULL);
183
184         if (stat(path, &stb) != 0)
185                 return NULL;
186         devname = blkid_devno_to_devname(stb.st_dev);
187         if (!devname)
188                 return NULL;
189         dev = blkid_get_dev(cache, devname, BLKID_DEV_NORMAL);
190         free(devname);
191         if (!dev)
192                 return NULL;
193         iter = blkid_tag_iterate_begin(dev);
194         if (!iter)
195                 return NULL;
196         while (blkid_tag_next(iter, &type, &val) == 0)
197                 if (strcmp(type, "UUID") == 0)
198                         break;
199         blkid_tag_iterate_end(iter);
200         return val;
201 }
202 #else
203 #define get_uuid_blkdev(path) (NULL)
204 #endif
205
206 int get_uuid(char *path, char *uuid, int uuidlen, char *u)
207 {
208         /* extract hex digits from uuidstr and compose a uuid
209          * of the given length (max 16), xoring bytes to make
210          * a smaller uuid.  Then compare with uuid
211          */
212         int i = 0;
213         const char *val = NULL;
214         char fsid_val[17];
215
216         if (path) {
217                 val = get_uuid_blkdev(path);
218                 if (!val)
219                         return 0;
220         } else {
221                 val = uuid;
222         }
223         
224         memset(u, 0, uuidlen);
225         for ( ; *val ; val++) {
226                 char c = *val;
227                 if (!isxdigit(c))
228                         continue;
229                 if (isalpha(c)) {
230                         if (isupper(c))
231                                 c = c - 'A' + 10;
232                         else
233                                 c = c - 'a' + 10;
234                 } else
235                         c = c - '0' + 0;
236                 if ((i&1) == 0)
237                         c <<= 4;
238                 u[i/2] ^= c;
239                 i++;
240                 if (i == uuidlen*2)
241                         i = 0;
242         }
243         return 1;
244 }
245
246 /* Iterate through /etc/mtab, finding mountpoints
247  * at or below a given path
248  */
249 static char *next_mnt(void **v, char *p)
250 {
251         FILE *f;
252         struct mntent *me;
253         int l = strlen(p);
254         if (*v == NULL) {
255                 f = setmntent("/etc/mtab", "r");
256                 *v = f;
257         } else
258                 f = *v;
259         while ((me = getmntent(f)) != NULL &&
260                (strncmp(me->mnt_dir, p, l) != 0 ||
261                 me->mnt_dir[l] != '/'))
262                 ;
263         if (me == NULL) {
264                 endmntent(f);
265                 *v = NULL;
266                 return NULL;
267         }
268         return me->mnt_dir;
269 }
270
271 void nfsd_fh(FILE *f)
272 {
273         /* request are:
274          *  domain fsidtype fsid
275          * interpret fsid, find export point and options, and write:
276          *  domain fsidtype fsid expiry path
277          */
278         char *cp;
279         char *dom;
280         int fsidtype;
281         int fsidlen;
282         unsigned int dev, major=0, minor=0;
283         unsigned int inode=0;
284         unsigned long long inode64;
285         unsigned int fsidnum=0;
286         char fsid[32];
287         struct exportent *found = NULL;
288         struct hostent *he = NULL;
289         struct in_addr addr;
290         char *found_path = NULL;
291         nfs_export *exp;
292         int i;
293         int dev_missing = 0;
294         int uuidlen = 0;
295         char *fhuuid = NULL;
296
297         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
298                 return;
299
300         xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
301
302         cp = lbuf;
303         
304         dom = malloc(strlen(cp));
305         if (dom == NULL)
306                 return;
307         if (qword_get(&cp, dom, strlen(cp)) <= 0)
308                 goto out;
309         if (qword_get_int(&cp, &fsidtype) != 0)
310                 goto out;
311         if (fsidtype < 0 || fsidtype > 7)
312                 goto out; /* unknown type */
313         if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
314                 goto out;
315         switch(fsidtype) {
316         case FSID_DEV: /* 4 bytes: 2 major, 2 minor, 4 inode */
317                 if (fsidlen != 8)
318                         goto out;
319                 memcpy(&dev, fsid, 4);
320                 memcpy(&inode, fsid+4, 4);
321                 major = ntohl(dev)>>16;
322                 minor = ntohl(dev) & 0xFFFF;
323                 break;
324
325         case FSID_NUM: /* 4 bytes - fsid */
326                 if (fsidlen != 4)
327                         goto out;
328                 memcpy(&fsidnum, fsid, 4);
329                 break;
330
331         case FSID_MAJOR_MINOR: /* 12 bytes: 4 major, 4 minor, 4 inode 
332                  * This format is never actually used but was
333                  * an historical accident
334                  */
335                 if (fsidlen != 12)
336                         goto out;
337                 memcpy(&dev, fsid, 4); major = ntohl(dev);
338                 memcpy(&dev, fsid+4, 4); minor = ntohl(dev);
339                 memcpy(&inode, fsid+8, 4);
340                 break;
341
342         case FSID_ENCODE_DEV: /* 8 bytes: 4 byte packed device number, 4 inode */
343                 /* This is *host* endian, not net-byte-order, because
344                  * no-one outside this host has any business interpreting it
345                  */
346                 if (fsidlen != 8)
347                         goto out;
348                 memcpy(&dev, fsid, 4);
349                 memcpy(&inode, fsid+4, 4);
350                 major = (dev & 0xfff00) >> 8;
351                 minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
352                 break;
353
354         case FSID_UUID4_INUM: /* 4 byte inode number and 4 byte uuid */
355                 if (fsidlen != 8)
356                         goto out;
357                 memcpy(&inode, fsid, 4);
358                 uuidlen = 4;
359                 fhuuid = fsid+4;
360                 break;
361         case FSID_UUID8: /* 8 byte uuid */
362                 if (fsidlen != 8)
363                         goto out;
364                 uuidlen = 8;
365                 fhuuid = fsid;
366                 break;
367         case FSID_UUID16: /* 16 byte uuid */
368                 if (fsidlen != 16)
369                         goto out;
370                 uuidlen = 16;
371                 fhuuid = fsid;
372                 break;
373         case FSID_UUID16_INUM: /* 8 byte inode number and 16 byte uuid */
374                 if (fsidlen != 24)
375                         goto out;
376                 memcpy(&inode64, fsid, 8);
377                 inode = inode64;
378                 uuidlen = 16;
379                 fhuuid = fsid+8;
380                 break;
381         }
382
383         auth_reload();
384
385         /* Now determine export point for this fsid/domain */
386         for (i=0 ; i < MCL_MAXTYPES; i++) {
387                 nfs_export *next_exp;
388                 for (exp = exportlist[i]; exp; exp = next_exp) {
389                         struct stat stb;
390                         char u[16];
391                         char *path;
392
393                         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
394                                 static nfs_export *prev = NULL;
395                                 static void *mnt = NULL;
396                                 
397                                 if (prev == exp) {
398                                         /* try a submount */
399                                         path = next_mnt(&mnt, exp->m_export.e_path);
400                                         if (!path) {
401                                                 next_exp = exp->m_next;
402                                                 prev = NULL;
403                                                 continue;
404                                         }
405                                         next_exp = exp;
406                                 } else {
407                                         prev = exp;
408                                         mnt = NULL;
409                                         path = exp->m_export.e_path;
410                                         next_exp = exp;
411                                 }
412                         } else {
413                                 path = exp->m_export.e_path;
414                                 next_exp = exp->m_next;
415                         }
416
417                         if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
418                                 continue;
419                         if (exp->m_export.e_mountpoint &&
420                             !is_mountpoint(exp->m_export.e_mountpoint[0]?
421                                            exp->m_export.e_mountpoint:
422                                            exp->m_export.e_path))
423                                 dev_missing ++;
424                         if (stat(path, &stb) != 0)
425                                 continue;
426                         if (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) {
427                                 continue;
428                         }
429                         switch(fsidtype){
430                         case FSID_DEV:
431                         case FSID_MAJOR_MINOR:
432                         case FSID_ENCODE_DEV:
433                                 if (stb.st_ino != inode)
434                                         continue;
435                                 if (major != major(stb.st_dev) ||
436                                     minor != minor(stb.st_dev))
437                                         continue;
438                                 break;
439                         case FSID_NUM:
440                                 if (((exp->m_export.e_flags & NFSEXP_FSID) == 0 ||
441                                      exp->m_export.e_fsid != fsidnum))
442                                         continue;
443                                 break;
444                         case FSID_UUID4_INUM:
445                         case FSID_UUID16_INUM:
446                                 if (stb.st_ino != inode)
447                                         continue;
448                                 goto check_uuid;
449                         case FSID_UUID8:
450                         case FSID_UUID16:
451                                 if (!is_mountpoint(path))
452                                         continue;
453                         check_uuid:
454                                 if (exp->m_export.e_uuid)
455                                         get_uuid(NULL, exp->m_export.e_uuid,
456                                                  uuidlen, u);
457                                 else if (get_uuid(path, NULL, uuidlen, u) == 0)
458                                         continue;
459
460                                 if (memcmp(u, fhuuid, uuidlen) != 0)
461                                         continue;
462                                 break;
463                         }
464                         if (use_ipaddr) {
465                                 if (he == NULL) {
466                                         if (!inet_aton(dom, &addr))
467                                                 goto out;
468                                         he = client_resolve(addr);
469                                 }
470                                 if (!client_check(exp->m_client, he))
471                                         continue;
472                         }
473                         /* It's a match !! */
474                         if (!found) {
475                                 found = &exp->m_export;
476                                 found_path = strdup(path);
477                                 if (found_path == NULL)
478                                         goto out;
479                         } else if (strcmp(found->e_path, exp->m_export.e_path)!= 0)
480                         {
481                                 xlog(L_WARNING, "%s and %s have same filehandle for %s, using first",
482                                      found_path, path, dom);
483                         }
484                 }
485         }
486         if (found && 
487             found->e_mountpoint &&
488             !is_mountpoint(found->e_mountpoint[0]?
489                            found->e_mountpoint:
490                            found->e_path)) {
491                 /* Cannot export this yet 
492                  * should log a warning, but need to rate limit
493                    xlog(L_WARNING, "%s not exported as %d not a mountpoint",
494                    found->e_path, found->e_mountpoint);
495                  */
496                 /* FIXME we need to make sure we re-visit this later */
497                 goto out;
498         }
499         if (!found && dev_missing) {
500                 /* The missing dev could be what we want, so just be
501                  * quite rather than returning stale yet
502                  */
503                 goto out;
504         }
505
506         if (found)
507                 if (cache_export_ent(dom, found, found_path) < 0)
508                         found = 0;
509
510         qword_print(f, dom);
511         qword_printint(f, fsidtype);
512         qword_printhex(f, fsid, fsidlen);
513         /* The fsid -> path lookup can be quite expensive as it
514          * potentially stats and reads lots of devices, and some of those
515          * might have spun-down.  The Answer is not likely to
516          * change underneath us, and an 'exportfs -f' can always
517          * remove this from the kernel, so use a really log
518          * timeout.  Maybe this should be configurable on the command
519          * line.
520          */
521         qword_printint(f, 0x7fffffff);
522         if (found)
523                 qword_print(f, found_path);
524         qword_eol(f);
525  out:
526         if (found_path)
527                 free(found_path);
528         if (he)
529                 free(he);
530         free(dom);
531         xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
532         return;         
533 }
534
535 static void write_fsloc(FILE *f, struct exportent *ep, char *path)
536 {
537         struct servers *servers;
538
539         if (ep->e_fslocmethod == FSLOC_NONE)
540                 return;
541
542         servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata, path);
543         if (!servers)
544                 return;
545         qword_print(f, "fsloc");
546         qword_printint(f, servers->h_num);
547         if (servers->h_num >= 0) {
548                 int i;
549                 for (i=0; i<servers->h_num; i++) {
550                         qword_print(f, servers->h_mp[i]->h_host);
551                         qword_print(f, servers->h_mp[i]->h_path);
552                 }
553         }
554         qword_printint(f, servers->h_referral);
555         release_replicas(servers);
556 }
557
558 static void write_secinfo(FILE *f, struct exportent *ep)
559 {
560         struct sec_entry *p;
561
562         for (p = ep->e_secinfo; p->flav; p++)
563                 ; /* Do nothing */
564         if (p == ep->e_secinfo) {
565                 /* There was no sec= option */
566                 return;
567         }
568         qword_print(f, "secinfo");
569         qword_printint(f, p - ep->e_secinfo);
570         for (p = ep->e_secinfo; p->flav; p++) {
571                 qword_printint(f, p->flav->fnum);
572                 qword_printint(f, p->flags);
573         }
574
575 }
576
577 static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
578 {
579         qword_print(f, domain);
580         qword_print(f, path);
581         qword_printint(f, time(0)+30*60);
582         if (exp) {
583                 int different_fs = strcmp(path, exp->e_path) != 0;
584                 
585                 if (different_fs)
586                         qword_printint(f, exp->e_flags & ~NFSEXP_FSID);
587                 else
588                         qword_printint(f, exp->e_flags);
589                 qword_printint(f, exp->e_anonuid);
590                 qword_printint(f, exp->e_anongid);
591                 qword_printint(f, exp->e_fsid);
592                 write_fsloc(f, exp, path);
593                 write_secinfo(f, exp);
594                 if (exp->e_uuid == NULL || different_fs) {
595                         char u[16];
596                         if (get_uuid(path, NULL, 16, u)) {
597                                 qword_print(f, "uuid");
598                                 qword_printhex(f, u, 16);
599                         }
600                 } else {
601                         char u[16];
602                         get_uuid(NULL, exp->e_uuid, 16, u);
603                         qword_print(f, "uuid");
604                         qword_printhex(f, u, 16);
605                 }
606         }
607         return qword_eol(f);
608 }
609
610 void nfsd_export(FILE *f)
611 {
612         /* requests are:
613          *  domain path
614          * determine export options and return:
615          *  domain path expiry flags anonuid anongid fsid
616          */
617
618         char *cp;
619         int i;
620         char *dom, *path;
621         nfs_export *exp, *found = NULL;
622         int found_type = 0;
623         struct in_addr addr;
624         struct hostent *he = NULL;
625
626
627         if (readline(fileno(f), &lbuf, &lbuflen) != 1)
628                 return;
629
630         xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
631
632         cp = lbuf;
633         dom = malloc(strlen(cp));
634         path = malloc(strlen(cp));
635
636         if (!dom || !path)
637                 goto out;
638
639         if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
640                 goto out;
641         if (qword_get(&cp, path, strlen(lbuf)) <= 0)
642                 goto out;
643
644         auth_reload();
645
646         /* now find flags for this export point in this domain */
647         for (i=0 ; i < MCL_MAXTYPES; i++) {
648                 for (exp = exportlist[i]; exp; exp = exp->m_next) {
649                         if (!use_ipaddr && !client_member(dom, exp->m_client->m_hostname))
650                                 continue;
651                         if (exp->m_export.e_flags & NFSEXP_CROSSMOUNT) {
652                                 /* if path is a mountpoint below e_path, then OK */
653                                 int l = strlen(exp->m_export.e_path);
654                                 if (strcmp(path, exp->m_export.e_path) == 0 ||
655                                     (strncmp(path, exp->m_export.e_path, l) == 0 &&
656                                      path[l] == '/' &&
657                                      is_mountpoint(path)))
658                                         /* ok */;
659                                 else
660                                         continue;
661                         } else if (strcmp(path, exp->m_export.e_path) != 0)
662                                 continue;
663                         if (use_ipaddr) {
664                                 if (he == NULL) {
665                                         if (!inet_aton(dom, &addr))
666                                                 goto out;
667                                         he = client_resolve(addr);
668                                 }
669                                 if (!client_check(exp->m_client, he))
670                                         continue;
671                         }
672                         if (!found) {
673                                 found = exp;
674                                 found_type = i;
675                                 continue;
676                         }
677                         /* If one is a CROSSMOUNT, then prefer the longest path */
678                         if (((found->m_export.e_flags & NFSEXP_CROSSMOUNT) ||
679                              (exp->m_export.e_flags & NFSEXP_CROSSMOUNT)) &&
680                             strlen(found->m_export.e_path) !=
681                             strlen(exp->m_export.e_path)) {
682
683                                 if (strlen(exp->m_export.e_path) >
684                                     strlen(found->m_export.e_path)) {
685                                         found = exp;
686                                         found_type = i;
687                                 }
688                                 continue;
689
690                         } else if (found_type == i && found->m_warned == 0) {
691                                 xlog(L_WARNING, "%s exported to both %s and %s, "
692                                      "arbitrarily choosing options from first",
693                                      path, found->m_client->m_hostname, exp->m_client->m_hostname,
694                                      dom);
695                                 found->m_warned = 1;
696                         }
697                 }
698         }
699
700         if (found) {
701                 if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
702                         xlog(L_WARNING,
703                              "Cannot export %s, possibly unsupported filesystem"
704                              " or fsid= required", path);
705                         dump_to_cache(f, dom, path, NULL);
706                 }
707         } else {
708                 dump_to_cache(f, dom, path, NULL);
709         }
710  out:
711         xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
712         if (dom) free(dom);
713         if (path) free(path);
714         if (he) free(he);
715 }
716
717
718 struct {
719         char *cache_name;
720         void (*cache_handle)(FILE *f);
721         FILE *f;
722 } cachelist[] = {
723         { "auth.unix.ip", auth_unix_ip},
724         { "auth.unix.gid", auth_unix_gid},
725         { "nfsd.export", nfsd_export},
726         { "nfsd.fh", nfsd_fh},
727         { NULL, NULL }
728 };
729
730 extern int manage_gids;
731 void cache_open(void) 
732 {
733         int i;
734         for (i=0; cachelist[i].cache_name; i++ ) {
735                 char path[100];
736                 if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
737                         continue;
738                 sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
739                 cachelist[i].f = fopen(path, "r+");
740         }
741 }
742
743 void cache_set_fds(fd_set *fdset)
744 {
745         int i;
746         for (i=0; cachelist[i].cache_name; i++) {
747                 if (cachelist[i].f)
748                         FD_SET(fileno(cachelist[i].f), fdset);
749         }
750 }
751
752 int cache_process_req(fd_set *readfds) 
753 {
754         int i;
755         int cnt = 0;
756         for (i=0; cachelist[i].cache_name; i++) {
757                 if (cachelist[i].f != NULL &&
758                     FD_ISSET(fileno(cachelist[i].f), readfds)) {
759                         cnt++;
760                         cachelist[i].cache_handle(cachelist[i].f);
761                         FD_CLR(fileno(cachelist[i].f), readfds);
762                 }
763         }
764         return cnt;
765 }
766
767
768 /*
769  * Give IP->domain and domain+path->options to kernel
770  * % echo nfsd $IP  $[now+30*60] $domain > /proc/net/rpc/auth.unix.ip/channel
771  * % echo $domain $path $[now+30*60] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
772  */
773
774 int cache_export_ent(char *domain, struct exportent *exp, char *path)
775 {
776         int err;
777         FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
778         if (!f)
779                 return -1;
780
781         err = dump_to_cache(f, domain, exp->e_path, exp);
782         if (err) {
783                 xlog(L_WARNING,
784                      "Cannot export %s, possibly unsupported filesystem or"
785                      " fsid= required", exp->e_path);
786         }
787
788         while (err == 0 && (exp->e_flags & NFSEXP_CROSSMOUNT) && path) {
789                 /* really an 'if', but we can break out of
790                  * a 'while' more easily */
791                 /* Look along 'path' for other filesystems
792                  * and export them with the same options
793                  */
794                 struct stat stb;
795                 int l = strlen(exp->e_path);
796                 int dev;
797
798                 if (strlen(path) <= l || path[l] != '/' ||
799                     strncmp(exp->e_path, path, l) != 0)
800                         break;
801                 if (stat(exp->e_path, &stb) != 0)
802                         break;
803                 dev = stb.st_dev;
804                 while(path[l] == '/') {
805                         char c;
806                         /* errors for submount should fail whole filesystem */
807                         int err2;
808
809                         l++;
810                         while (path[l] != '/' && path[l])
811                                 l++;
812                         c = path[l];
813                         path[l] = 0;
814                         err2 = lstat(path, &stb);
815                         path[l] = c;
816                         if (err2 < 0)
817                                 break;
818                         if (stb.st_dev == dev)
819                                 continue;
820                         dev = stb.st_dev;
821                         path[l] = 0;
822                         dump_to_cache(f, domain, path, exp);
823                         path[l] = c;
824                 }
825                 break;
826         }
827
828         fclose(f);
829         return err;
830 }
831
832 int cache_export(nfs_export *exp, char *path)
833 {
834         int err;
835         FILE *f;
836
837         f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
838         if (!f)
839                 return -1;
840
841         qword_print(f, "nfsd");
842         qword_print(f, inet_ntoa(exp->m_client->m_addrlist[0]));
843         qword_printint(f, time(0)+30*60);
844         qword_print(f, exp->m_client->m_hostname);
845         err = qword_eol(f);
846         
847         fclose(f);
848
849         err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
850                 || err;
851         return err;
852 }
853
854 /* Get a filehandle.
855  * { 
856  *   echo $domain $path $length 
857  *   read filehandle <&0
858  * } <> /proc/fs/nfsd/filehandle
859  */
860 struct nfs_fh_len *
861 cache_get_filehandle(nfs_export *exp, int len, char *p)
862 {
863         FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
864         char buf[200];
865         char *bp = buf;
866         int failed;
867         static struct nfs_fh_len fh;
868
869         if (!f)
870                 f = fopen("/proc/fs/nfs/filehandle", "r+");
871         if (!f)
872                 return NULL;
873
874         qword_print(f, exp->m_client->m_hostname);
875         qword_print(f, p);
876         qword_printint(f, len); 
877         failed = qword_eol(f);
878         
879         if (!failed)
880                 failed = (fgets(buf, sizeof(buf), f) == NULL);
881         fclose(f);
882         if (failed)
883                 return NULL;
884         memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
885         fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
886         return &fh;
887 }
888