]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/nfsexport.c
Autogen update
[nfs-utils.git] / support / nfs / nfsexport.c
1 /*
2  * support/nfs/export.c
3  *
4  * Add or delete an NFS export in knfsd.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <string.h>
14 #include <sys/types.h>
15 #include <asm/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19
20 #include "nfslib.h"
21
22         /* if /proc/net/rpc/... exists, then 
23          * write to it, as that interface is more stable.
24          * Write:
25          *  client fsidtype fsid path
26          * to /proc/net/rpc/nfsd.fh/channel
27          * and
28          *  client path expiry flags anonuid anongid fsid
29          * to /proc/net/rpc/nfsd.export/channel
30          */
31
32 static int
33 exp_unexp(struct nfsctl_export *exp, int export)
34 {
35         FILE *f;
36         struct stat stb;
37         __u32 fsid;
38         char fsidstr[8];
39         __u16 dev;
40         __u32 inode;
41
42
43         f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
44         if (f == NULL) return -1;
45         qword_print(f, exp->ex_client);
46         qword_print(f, exp->ex_path);
47         if (export) {
48                 qword_printint(f, 0x7fffffff);
49                 qword_printint(f, exp->ex_flags);
50                 qword_printint(f, exp->ex_anon_uid);
51                 qword_printint(f, exp->ex_anon_gid);
52                 qword_printint(f, exp->ex_dev);
53         } else
54                 qword_printint(f, 1);
55
56         qword_eol(f);
57         fclose(f);
58
59         if (stat(exp->ex_path, &stb) != 0)
60                 return -1;
61         f = fopen("/proc/net/rpc/nfsd.fh/channel", "w");
62         if (f==NULL) return -1;
63         if (exp->ex_flags & NFSEXP_FSID) {
64                 qword_print(f,exp->ex_client);
65                 qword_printint(f,1);
66                 fsid = exp->ex_dev;
67                 qword_printhex(f, (char*)&fsid, 4);
68                 if (export) {
69                         qword_printint(f, 0x7fffffff);
70                         qword_print(f, exp->ex_path);
71                 } else
72                         qword_printint(f, 1);
73
74                 qword_eol(f);
75         }
76         qword_print(f,exp->ex_client);
77         qword_printint(f,0);
78         dev = htons(major(stb.st_dev)); memcpy(fsidstr, &dev, 2);
79         dev = htons(minor(stb.st_dev)); memcpy(fsidstr+2, &dev, 2);
80         inode = stb.st_ino; memcpy(fsidstr+4, &inode, 4);
81         
82         qword_printhex(f, fsidstr, 8);
83         if (export) {
84                 qword_printint(f, 0x7fffffff);
85                 qword_print(f, exp->ex_path);
86         } else
87                 qword_printint(f, 1);
88         qword_eol(f);
89         fclose(f);
90         return 0;
91 }
92
93 int
94 nfsexport(struct nfsctl_export *exp)
95 {
96         struct nfsctl_arg       arg;
97         int fd;
98         if ((fd=open("/proc/net/rpc/nfsd.fh/channel", O_WRONLY))>= 0) {
99                 close(fd);
100                 return exp_unexp(exp, 1);
101         }
102         arg.ca_version = NFSCTL_VERSION;
103         memcpy(&arg.ca_export, exp, sizeof(arg.ca_export));
104         return nfsctl(NFSCTL_EXPORT, &arg, NULL);
105 }
106
107 int
108 nfsunexport(struct nfsctl_export *exp)
109 {
110         struct nfsctl_arg       arg;
111
112         int fd;
113         if ((fd=open("/proc/net/rpc/nfsd.fh/channel", O_WRONLY))>= 0) {
114                 close(fd);
115                 return exp_unexp(exp, 0);
116         }
117
118         arg.ca_version = NFSCTL_VERSION;
119         memcpy(&arg.ca_export, exp, sizeof(arg.ca_export));
120         return nfsctl(NFSCTL_UNEXPORT, &arg, NULL);
121 }