Whamcloud - gitweb
3b393162e00e33408a1476a393c63e32aacbf4a1
[fs/lustre-release.git] / lustre / utils / gss / cacheio.c
1 /*
2   Copyright (c) 2004 The Regents of the University of Michigan.
3   All rights reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14   3. Neither the name of the University nor the names of its
15      contributors may be used to endorse or promote products derived
16      from this software without specific prior written permission.
17
18   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32  * support/nfs/cacheio.c
33  * support IO on the cache channel files in 2.5 and beyond.
34  * These use 'qwords' which are like words, but with a little quoting.
35  *
36  */
37
38
39 /*
40  * Support routines for text-based upcalls.
41  * Fields are separated by spaces.
42  * Fields are either mangled to quote space tab newline slosh with slosh
43  * or a hexified with a leading \x
44  * Record is terminated with newline.
45  *
46  */
47
48 #include "cacheio.h"
49 #include <stdio.h>
50 #include <ctype.h>
51 #include <unistd.h>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <fcntl.h>
55 #include <time.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <errno.h>
59 #include "err_util.h"
60
61 void qword_add(char **bpp, int *lp, char *str)
62 {
63         char *bp = *bpp;
64         int len = *lp;
65         char c;
66
67         if (len < 0) return;
68
69         while ((c=*str++) && len)
70                 switch(c) {
71                 case ' ':
72                 case '\t':
73                 case '\n':
74                 case '\\':
75                         if (len >= 4) {
76                                 *bp++ = '\\';
77                                 *bp++ = '0' + ((c & 0300)>>6);
78                                 *bp++ = '0' + ((c & 0070)>>3);
79                                 *bp++ = '0' + ((c & 0007)>>0);
80                         }
81                         len -= 4;
82                         break;
83                 default:
84                         *bp++ = c;
85                         len--;
86                 }
87         if (c || len <1) len = -1;
88         else {
89                 *bp++ = ' ';
90                 len--;
91         }
92         *bpp = bp;
93         *lp = len;
94 }
95
96 void qword_addhex(char **bpp, int *lp, char *buf, int blen)
97 {
98         char *bp = *bpp;
99         int len = *lp;
100
101         if (len < 0) return;
102
103         if (len > 2) {
104                 *bp++ = '\\';
105                 *bp++ = 'x';
106                 len -= 2;
107                 while (blen && len >= 2) {
108                         unsigned char c = *buf++;
109                         *bp++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
110                         *bp++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
111                         len -= 2;
112                         blen--;
113                 }
114         }
115         if (blen || len<1) len = -1;
116         else {
117                 *bp++ = ' ';
118                 len--;
119         }
120         *bpp = bp;
121         *lp = len;
122 }
123
124 void qword_addint(char **bpp, int *lp, int n)
125 {
126         int len;
127
128         len = snprintf(*bpp, *lp, "%d ", n);
129         if (len > *lp)
130                 len = *lp;
131         *bpp += len;
132         *lp -= len;
133 }
134
135 void qword_addeol(char **bpp, int *lp)
136 {
137         if (*lp <= 0)
138                 return;
139         **bpp = '\n';
140         (*bpp)++;
141         (*lp)--;
142 }
143
144 static char qword_buf[8192];
145 static char tmp_buf[8192];
146 void qword_print(FILE *f, char *str)
147 {
148         char *bp = qword_buf;
149         int len = sizeof(qword_buf);
150         qword_add(&bp, &len, str);
151         fwrite(qword_buf, bp-qword_buf, 1, f);
152         /* XXX: */
153         memcpy(tmp_buf, qword_buf, bp-qword_buf);
154         tmp_buf[bp-qword_buf] = '\0';
155         printerr(2, "%s", tmp_buf);
156 }
157
158 void qword_printhex(FILE *f, char *str, int slen)
159 {
160         char *bp = qword_buf;
161         int len = sizeof(qword_buf);
162         qword_addhex(&bp, &len, str, slen);
163         fwrite(qword_buf, bp-qword_buf, 1, f);
164         /* XXX: */
165         memcpy(tmp_buf, qword_buf, bp-qword_buf);
166         tmp_buf[bp-qword_buf] = '\0';
167         printerr(2, "%s", tmp_buf);
168 }
169
170 void qword_printint(FILE *f, int num)
171 {
172         fprintf(f, "%d ", num);
173         printerr(2, "%d ", num);
174 }
175
176 void qword_eol(FILE *f)
177 {
178         fprintf(f,"\n");
179         fflush(f);
180         printerr(2, "\n");
181 }
182
183
184
185 #define isodigit(c) (isdigit(c) && c <= '7')
186 int qword_get(char **bpp, char *dest, int bufsize)
187 {
188         /* return bytes copied, or -1 on error */
189         char *bp = *bpp;
190         int len = 0;
191
192         while (*bp == ' ') bp++;
193
194         if (bp[0] == '\\' && bp[1] == 'x') {
195                 /* HEX STRING */
196                 bp += 2;
197                 while (isxdigit(bp[0]) && isxdigit(bp[1]) && len < bufsize) {
198                         int byte = isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
199                         bp++;
200                         byte <<= 4;
201                         byte |= isdigit(*bp) ? *bp-'0' : toupper(*bp)-'A'+10;
202                         *dest++ = byte;
203                         bp++;
204                         len++;
205                 }
206         } else {
207                 /* text with \nnn octal quoting */
208                 while (*bp != ' ' && *bp != '\n' && *bp && len < bufsize-1) {
209                         if (*bp == '\\' &&
210                             isodigit(bp[1]) && (bp[1] <= '3') &&
211                             isodigit(bp[2]) &&
212                             isodigit(bp[3])) {
213                                 int byte = (*++bp -'0');
214                                 bp++;
215                                 byte = (byte << 3) | (*bp++ - '0');
216                                 byte = (byte << 3) | (*bp++ - '0');
217                                 *dest++ = byte;
218                                 len++;
219                         } else {
220                                 *dest++ = *bp++;
221                                 len++;
222                         }
223                 }
224         }
225
226         if (*bp != ' ' && *bp != '\n' && *bp != '\0')
227                 return -1;
228         while (*bp == ' ') bp++;
229         *bpp = bp;
230 // why should we clear *dest???
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 #define READLINE_BUFFER_INCREMENT 2048
250
251 int readline(int fd, char **buf, int *lenp)
252 {
253         /* read a line into *buf, which is malloced *len long
254          * realloc if needed until we find a \n
255          * nul out the \n and return
256          * 0 of eof, 1 of success
257          */
258         int len;
259
260         if (*lenp == 0) {
261                 char *b = malloc(READLINE_BUFFER_INCREMENT);
262                 if (b == NULL)
263                         return 0;
264                 *buf = b;
265                 *lenp = READLINE_BUFFER_INCREMENT;
266         }
267         len = read(fd, *buf, *lenp);
268         if (len <= 0) {
269                 printerr(0, "readline: read error: len %d errno %d (%s)\n",
270                          len, errno, strerror(errno));
271                 return 0;
272         }
273         while ((*buf)[len-1] != '\n') {
274         /* now the less common case.  There was no newline,
275          * so we have to keep reading after re-alloc
276          */
277                 char *new;
278                 int nl;
279                 *lenp += READLINE_BUFFER_INCREMENT;
280                 new = realloc(*buf, *lenp);
281                 if (new == NULL)
282                         return 0;
283                 *buf = new;
284                 nl = read(fd, *buf +len, *lenp - len);
285                 if (nl <= 0 ) {
286                         printerr(0, "readline: read error: len %d "
287                                  "errno %d (%s)\n", nl, errno, strerror(errno));
288                         return 0;
289                 }
290                 len += nl;
291         }
292         (*buf)[len-1] = 0;
293         printerr(3, "readline: read %d chars into buffer of size %d:\n%s\n",
294                  len, *lenp, *buf);
295         return 1;
296 }