]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/nfsexport.c
Cleanup xlog logging code to be safe and usable for all
[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         int err;
42
43
44         f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
45         if (f == NULL) return -1;
46         qword_print(f, exp->ex_client);
47         qword_print(f, exp->ex_path);
48         if (export) {
49                 qword_printint(f, 0x7fffffff);
50                 qword_printint(f, exp->ex_flags);
51                 qword_printint(f, exp->ex_anon_uid);
52                 qword_printint(f, exp->ex_anon_gid);
53                 qword_printint(f, exp->ex_dev);
54         } else
55                 qword_printint(f, 1);
56
57         err = qword_eol(f);
58         fclose(f);
59
60         if (stat(exp->ex_path, &stb) != 0)
61                 return -1;
62         f = fopen("/proc/net/rpc/nfsd.fh/channel", "w");
63         if (f==NULL) return -1;
64         if (exp->ex_flags & NFSEXP_FSID) {
65                 qword_print(f,exp->ex_client);
66                 qword_printint(f,1);
67                 fsid = exp->ex_dev;
68                 qword_printhex(f, (char*)&fsid, 4);
69                 if (export) {
70                         qword_printint(f, 0x7fffffff);
71                         qword_print(f, exp->ex_path);
72                 } else
73                         qword_printint(f, 1);
74
75                 err = qword_eol(f) || err;
76         }
77         qword_print(f,exp->ex_client);
78         qword_printint(f,0);
79         dev = htons(major(stb.st_dev)); memcpy(fsidstr, &dev, 2);
80         dev = htons(minor(stb.st_dev)); memcpy(fsidstr+2, &dev, 2);
81         inode = stb.st_ino; memcpy(fsidstr+4, &inode, 4);
82         
83         qword_printhex(f, fsidstr, 8);
84         if (export) {
85                 qword_printint(f, 0x7fffffff);
86                 qword_print(f, exp->ex_path);
87         } else
88                 qword_printint(f, 1);
89         err = qword_eol(f) || err;
90         fclose(f);
91         return err;
92 }
93
94 int
95 nfsexport(struct nfsctl_export *exp)
96 {
97         struct nfsctl_arg       arg;
98         int fd;
99         if ((fd=open("/proc/net/rpc/nfsd.fh/channel", O_WRONLY))>= 0) {
100                 close(fd);
101                 return exp_unexp(exp, 1);
102         }
103         arg.ca_version = NFSCTL_VERSION;
104         memcpy(&arg.ca_export, exp, sizeof(arg.ca_export));
105         return nfsctl(NFSCTL_EXPORT, &arg, NULL);
106 }
107
108 int
109 nfsunexport(struct nfsctl_export *exp)
110 {
111         struct nfsctl_arg       arg;
112
113         int fd;
114         if ((fd=open("/proc/net/rpc/nfsd.fh/channel", O_WRONLY))>= 0) {
115                 close(fd);
116                 return exp_unexp(exp, 0);
117         }
118
119         arg.ca_version = NFSCTL_VERSION;
120         memcpy(&arg.ca_export, exp, sizeof(arg.ca_export));
121         return nfsctl(NFSCTL_UNEXPORT, &arg, NULL);
122 }