]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/cacheio.c
e641c4535c652c803dc067e4cfe3f4633bc28b12
[nfs-utils.git] / support / nfs / cacheio.c
1 /*
2  * support/nfs/cacheio.c
3  * support IO on the cache channel files in 2.5 and beyond.
4  * These use 'qwords' which are like words, but with a little quoting.
5  *
6  */
7
8
9 /*
10  * Support routines for text-based upcalls.
11  * Fields are separated by spaces.
12  * Fields are either mangled to quote space tab newline slosh with slosh
13  * or a hexified with a leading \x
14  * Record is terminated with newline.
15  *
16  */
17
18 #include <nfslib.h>
19 #include <stdio.h>
20 #include <stdio_ext.h>
21 #include <ctype.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <time.h>
27 #include <errno.h>
28
29 void qword_add(char **bpp, int *lp, char *str)
30 {
31         char *bp = *bpp;
32         int len = *lp;
33         char c;
34
35         if (len < 0) return;
36
37         while ((c=*str++) && len)
38                 switch(c) {
39                 case ' ':
40                 case '\t':
41                 case '\n':
42                 case '\\':
43                         if (len >= 4) {
44                                 *bp++ = '\\';
45                                 *bp++ = '0' + ((c & 0300)>>6);
46                                 *bp++ = '0' + ((c & 0070)>>3);
47                                 *bp++ = '0' + ((c & 0007)>>0);
48                         }
49                         len -= 4;
50                         break;
51                 default:
52                         *bp++ = c;
53                         len--;
54                 }
55         if (c || len <1) len = -1;
56         else {
57                 *bp++ = ' ';
58                 len--;
59         }
60         *bpp = bp;
61         *lp = len;
62 }
63
64 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
65 {
66         char *bp = *bpp;
67         int len = *lp;
68
69         if (len < 0) return;
70
71         if (len > 2) {
72                 *bp++ = '\\';
73                 *bp++ = 'x';
74                 len -= 2;
75                 while (blen && len >= 2) {
76                         unsigned char c = *buf++;
77                         *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
78                         *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
79                         len -= 2;
80                         blen--;
81                 }
82         }
83         if (blen || len<1) len = -1;
84         else {
85                 *bp++ = ' ';
86                 len--;
87         }
88         *bpp = bp;
89         *lp = len;
90 }
91
92 void qword_addint(char **bpp, int *lp, int n)
93 {
94         int len;
95
96         len = snprintf(*bpp, *lp, "%d ", n);
97         if (len > *lp)
98                 len = *lp;
99         *bpp += len;
100         *lp -= len;
101 }
102
103 void qword_adduint(char **bpp, int *lp, unsigned int n)
104 {
105         int len;
106
107         len = snprintf(*bpp, *lp, "%u ", n);
108         if (len > *lp)
109                 len = *lp;
110         *bpp += len;
111         *lp -= len;
112 }
113
114 void qword_addeol(char **bpp, int *lp)
115 {
116         if (*lp <= 0)
117                 return;
118         **bpp = '\n';
119         (*bpp)++;
120         (*lp)--;
121 }
122
123 static char qword_buf[8192];
124 void qword_print(FILE *f, char *str)
125 {
126         char *bp = qword_buf;
127         int len = sizeof(qword_buf);
128         qword_add(&bp, &len, str);
129         if (fwrite(qword_buf, bp-qword_buf, 1, f) != 1) {
130                 xlog_warn("qword_print: fwrite failed: errno %d (%s)",
131                         errno, strerror(errno));
132         }
133 }
134
135 void qword_printhex(FILE *f, char *str, int slen)
136 {
137         char *bp = qword_buf;
138         int len = sizeof(qword_buf);
139         qword_addhex(&bp, &len, str, slen);
140         if (fwrite(qword_buf, bp-qword_buf, 1, f) != 1) {
141                 xlog_warn("qword_printhex: fwrite failed: errno %d (%s)",
142                         errno, strerror(errno));
143         }
144 }
145
146 void qword_printint(FILE *f, int num)
147 {
148         fprintf(f, "%d ", num);
149 }
150
151 void qword_printuint(FILE *f, unsigned int num)
152 {
153         fprintf(f, "%u ", num);
154 }
155
156 void qword_printtimefrom(FILE *f, unsigned int num)
157 {
158         fprintf(f, "%lu ", time(0) + num);
159 }
160
161 int qword_eol(FILE *f)
162 {
163         int err;
164
165         fprintf(f,"\n");
166         err = fflush(f);
167         if (err) {
168                 xlog_warn("qword_eol: fflush failed: errno %d (%s)",
169                             errno, strerror(errno));
170         }
171         /*
172          * We must send one line (and one line only) in a single write
173          * call.  In case of a write error, libc may accumulate the
174          * unwritten data and try to write it again later, resulting in a
175          * multi-line write.  So we must explicitly ask it to throw away
176          * any such cached data.  But we return any original error
177          * indication to the caller.
178          */
179         __fpurge(f);
180         fflush(f);
181         return err;
182 }
183
184
185
186 #define isodigit(c) (isdigit(c) && c <= '7')
187 int qword_get(char **bpp, char *dest, int bufsize)
188 {
189         /* return bytes copied, or -1 on error */
190         char *bp = *bpp;
191         int len = 0;
192
193         while (*bp == ' ') bp++;
194
195         if (bp[0] == '\\' && bp[1] == 'x') {
196                 /* HEX STRING */
197                 bp += 2;
198                 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
199                         int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
200                         bp++;
201                         byte <<= 4;
202                         byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
203                         *dest++ = byte;
204                         bp++;
205                         len++;
206                 }
207         } else {
208                 /* text with \nnn octal quoting */
209                 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
210                         if (*bp == '\\' &&
211                             isodigit(bp[1]) && (bp[1] <= '3') &&
212                             isodigit(bp[2]) &&
213                             isodigit(bp[3])) {
214                                 int byte = (*++bp -'0');
215                                 bp++;
216                                 byte = (byte << 3) | (*bp++ - '0');
217                                 byte = (byte << 3) | (*bp++ - '0');
218                                 *dest++ = byte;
219                                 len++;
220                         } else {
221                                 *dest++ = *bp++;
222                                 len++;
223                         }
224                 }
225         }
226
227         if (*bp != ' ' && *bp != '\n' && *bp != '\0')
228                 return -1;
229         while (*bp == ' ') bp++;
230         *bpp = bp;
231         *dest = '\0';
232         return len;
233 }
234
235 int qword_get_int(char **bpp, int *anint)
236 {
237         char buf[50];
238         char *ep;
239         int rv;
240         int len = qword_get(bpp, buf, 50);
241         if (len < 0) return -1;
242         if (len ==0) return -1;
243         rv = strtol(buf, &ep, 0);
244         if (*ep) return -1;
245         *anint = rv;
246         return 0;
247 }
248
249 int qword_get_uint(char **bpp, unsigned int *anint)
250 {
251         char buf[50];
252         char *ep;
253         unsigned int rv;
254         int len = qword_get(bpp, buf, 50);
255         if (len < 0) return -1;
256         if (len ==0) return -1;
257         rv = strtoul(buf, &ep, 0);
258         if (*ep) return -1;
259         *anint = rv;
260         return 0;
261 }
262
263 #define READLINE_BUFFER_INCREMENT 2048
264
265 int readline(int fd, char **buf, int *lenp)
266 {
267         /* read a line into *buf, which is malloced *len long
268          * realloc if needed until we find a \n
269          * nul out the \n and return
270          * 0 on eof, 1 on success
271          */
272         int len;
273
274         if (*lenp == 0) {
275                 char *b = malloc(READLINE_BUFFER_INCREMENT);
276                 if (b == NULL)
277                         return 0;
278                 *buf = b;
279                 *lenp = READLINE_BUFFER_INCREMENT;
280         }
281         len = read(fd, *buf, *lenp);
282         if (len <= 0)
283                 return 0;
284         while ((*buf)[len-1] != '\n') {
285         /* now the less common case.  There was no newline,
286          * so we have to keep reading after re-alloc
287          */
288                 char *new;
289                 int nl;
290                 *lenp += READLINE_BUFFER_INCREMENT;
291                 new = realloc(*buf, *lenp);
292                 if (new == NULL)
293                         return 0;
294                 *buf = new;
295                 nl = read(fd, *buf + len, *lenp - len);
296                 if (nl <= 0)
297                         return 0;
298                 len += nl;
299         }
300         (*buf)[len-1] = '\0';
301         return 1;
302 }
303
304
305 /* Check if we should use the new caching interface
306  * This succeeds iff the "nfsd" filesystem is mounted on
307  * /proc/fs/nfs
308  */
309 int
310 check_new_cache(void)
311 {
312         return  (access("/proc/fs/nfs/filehandle", F_OK) == 0) ||
313                 (access("/proc/fs/nfsd/filehandle", F_OK) == 0);
314 }       
315
316
317 /* flush the kNFSd caches.
318  * Set the flush time to the mtime of _PATH_ETAB or
319  * if force, to now.
320  * the caches to flush are:
321  *  auth.unix.ip nfsd.export nfsd.fh
322  */
323
324 void
325 cache_flush(int force)
326 {
327         struct stat stb;
328         int c;
329         char stime[20];
330         char path[200];
331         time_t now;
332         /* Note: the order of these caches is important.
333          * They need to be flushed in dependancy order. So
334          * a cache that references items in another cache,
335          * as nfsd.fh entries reference items in nfsd.export,
336          * must be flushed before the cache that it references.
337          */
338         static char *cachelist[] = {
339                 "auth.unix.ip",
340                 "auth.unix.gid",
341                 "nfsd.fh",
342                 "nfsd.export",
343                 NULL
344         };
345         now = time(0);
346         if (force ||
347             stat(_PATH_ETAB, &stb) != 0 ||
348             stb.st_mtime > now)
349                 stb.st_mtime = time(0);
350         
351         sprintf(stime, "%ld\n", stb.st_mtime);
352         for (c=0; cachelist[c]; c++) {
353                 int fd;
354                 sprintf(path, "/proc/net/rpc/%s/flush", cachelist[c]);
355                 fd = open(path, O_RDWR);
356                 if (fd >= 0) {
357                         if (write(fd, stime, strlen(stime)) != (ssize_t)strlen(stime)) {
358                                 xlog_warn("Writing to '%s' failed: errno %d (%s)",
359                                 path, errno, strerror(errno));
360                         }
361                         close(fd);
362                 }
363         }
364 }