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