]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_stream.c
Fix for bug id #6: uncaught error condition from dsi_stream_read function call.
[netatalk.git] / libatalk / dsi / dsi_stream.c
1 /*
2  * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved. See COPYRIGHT.
4  *
5  * this file provides the following functions:
6  * dsi_stream_write:    just write a bunch of bytes.
7  * dsi_stream_read:     just read a bunch of bytes.
8  * dsi_stream_send:     send a DSI header + data.
9  * dsi_stream_receive:  read a DSI header + data.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif /* HAVE_CONFIG_H */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/uio.h>
28
29 #include <atalk/logger.h>
30 #include <atalk/dsi.h>
31 #include <netatalk/endian.h>
32 #include <atalk/util.h>
33
34 #define min(a,b)  ((a) < (b) ? (a) : (b))
35
36 #ifndef MSG_MORE
37 #define MSG_MORE 0x8000
38 #endif
39
40 #ifndef MSG_DONTWAIT
41 #define MSG_DONTWAIT 0x40
42 #endif
43
44 /*
45  * afpd is sleeping too much while trying to send something.
46  * May be there's no reader or the reader is also sleeping in write,
47  * look if there's some data for us to read, hopefully it will wake up
48  * the reader so we can write again.
49  *
50  * @returns 0 when is possible to send again, -1 on error
51  */
52 static int dsi_peek(DSI *dsi)
53 {
54     static int warned = 0;
55     fd_set readfds, writefds;
56     int    len;
57     int    maxfd;
58     int    ret;
59
60     LOG(log_debug, logtype_dsi, "dsi_peek");
61
62     maxfd = dsi->socket + 1;
63
64     while (1) {
65         FD_ZERO(&readfds);
66         FD_ZERO(&writefds);
67
68         if (dsi->eof < dsi->end) {
69             /* space in read buffer */
70             FD_SET( dsi->socket, &readfds);
71         } else {
72             if (!warned) {
73                 warned = 1;
74                 LOG(log_note, logtype_dsi, "dsi_peek: readahead buffer is full, possibly increase -dsireadbuf option");
75                 LOG(log_note, logtype_dsi, "dsi_peek: dsireadbuf: %d, DSI quantum: %d, effective buffer size: %d",
76                     dsi->dsireadbuf,
77                     dsi->server_quantum ? dsi->server_quantum : DSI_SERVQUANT_DEF,
78                     dsi->end - dsi->buffer);
79             }
80         }
81
82         FD_SET( dsi->socket, &writefds);
83
84         /* No timeout: if there's nothing to read nor nothing to write,
85          * we've got nothing to do at all */
86         if ((ret = select( maxfd, &readfds, &writefds, NULL, NULL)) <= 0) {
87             if (ret == -1 && errno == EINTR)
88                 /* we might have been interrupted by out timer, so restart select */
89                 continue;
90             /* give up */
91             LOG(log_error, logtype_dsi, "dsi_peek: unexpected select return: %d %s",
92                 ret, ret < 0 ? strerror(errno) : "");
93             return -1;
94         }
95
96         if (FD_ISSET(dsi->socket, &writefds)) {
97             /* we can write again */
98             LOG(log_debug, logtype_dsi, "dsi_peek: can write again");
99             break;
100         }
101
102         /* Check if there's sth to read, hopefully reading that will unblock the client */
103         if (FD_ISSET(dsi->socket, &readfds)) {
104             len = dsi->end - dsi->eof; /* it's ensured above that there's space */
105
106             if ((len = read(dsi->socket, dsi->eof, len)) <= 0) {
107                 if (len == 0) {
108                     LOG(log_error, logtype_dsi, "dsi_peek: EOF");
109                     return -1;
110                 }
111                 LOG(log_error, logtype_dsi, "dsi_peek: read: %s", strerror(errno));
112                 if (errno == EAGAIN)
113                     continue;
114                 return -1;
115             }
116             LOG(log_debug, logtype_dsi, "dsi_peek: read %d bytes", len);
117
118             dsi->eof += len;
119         }
120     }
121
122     return 0;
123 }
124
125 /*!
126  * Communication error with the client, enter disconnected state
127  *
128  * 1. close the socket
129  * 2. set the DSI_DISCONNECTED flag
130  */
131 int dsi_disconnect(DSI *dsi)
132 {
133     dsi->proto_close(dsi);          /* 1 */
134     dsi->flags |= DSI_DISCONNECTED; /* 2 */
135     return 0;
136 }
137
138 /* ------------------------------
139  * write raw data. return actual bytes read. checks against EINTR
140  * aren't necessary if all of the signals have SA_RESTART
141  * specified. */
142 ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode)
143 {
144   size_t written;
145   ssize_t len;
146   unsigned int flags = 0;
147
148   dsi->in_write++;
149   written = 0;
150
151   LOG(log_maxdebug, logtype_dsi, "dsi_stream_write: sending %u bytes", length);
152
153   while (written < length) {
154       len = send(dsi->socket, (u_int8_t *) data + written, length - written, flags);
155       if (len >= 0) {
156           written += len;
157           continue;
158       }
159
160       if (errno == EINTR)
161           continue;
162
163       if (errno == EAGAIN || errno == EWOULDBLOCK) {
164           LOG(log_debug, logtype_dsi, "dsi_stream_write: send: %s", strerror(errno));
165
166           if (mode == DSI_NOWAIT && written == 0) {
167               /* DSI_NOWAIT is used by attention give up in this case. */
168               written = -1;
169               goto exit;
170           }
171
172           /* Try to read sth. in order to break up possible deadlock */
173           if (dsi_peek(dsi) != 0) {
174               written = -1;
175               goto exit;
176           }
177           /* Now try writing again */
178           continue;
179       }
180
181       LOG(log_error, logtype_dsi, "dsi_stream_write: %s", strerror(errno));
182       written = -1;
183       goto exit;
184   }
185
186   dsi->write_count += written;
187
188 exit:
189   dsi->in_write--;
190   return written;
191 }
192
193
194 /* ---------------------------------
195 */
196 #ifdef WITH_SENDFILE
197 ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t length)
198 {
199   size_t written;
200   ssize_t len;
201
202   LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sending %u bytes", length);
203
204   dsi->in_write++;
205   written = 0;
206
207   while (written < length) {
208     len = sys_sendfile(dsi->socket, fromfd, &offset, length - written);
209         
210     if (len < 0) {
211       if (errno == EINTR)
212           continue;
213       if (errno == EINVAL || errno == ENOSYS)
214           return -1;
215           
216       if (errno == EAGAIN || errno == EWOULDBLOCK) {
217           if (dsi_peek(dsi)) {
218               /* can't go back to blocking mode, exit, the next read
219                  will return with an error and afpd will die.
220               */
221               break;
222           }
223           continue;
224       }
225       LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno));
226       break;
227     }
228     else if (!len) {
229         /* afpd is going to exit */
230         errno = EIO;
231         return -1; /* I think we're at EOF here... */
232     }
233     else 
234         written += len;
235   }
236
237   dsi->write_count += written;
238   dsi->in_write--;
239   return written;
240 }
241 #endif
242
243 /* 
244  * Return all bytes up to count from dsi->buffer if there are any buffered there
245  */
246 static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count)
247 {
248     size_t nbe = 0;
249
250     if (dsi->buffer == NULL)
251         /* afpd master has no DSI buffering */
252         return 0;
253
254     LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count);
255     
256     nbe = dsi->eof - dsi->start;
257
258     if (nbe > 0) {
259         nbe = min((size_t)nbe, count);
260         memcpy(buf, dsi->start, nbe);
261         dsi->start += nbe;
262
263         if (dsi->eof == dsi->start)
264             dsi->start = dsi->eof = dsi->buffer;
265     }
266
267     LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u",
268         dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe);
269
270     return nbe;
271 }
272
273 /*
274  * Get bytes from buffer dsi->buffer or read from socket
275  *
276  * 1. Check if there are bytes in the the dsi->buffer buffer.
277  * 2. Return bytes from (1) if yes.
278  *    Note: this may return fewer bytes then requested in count !!
279  * 3. If the buffer was empty, read from the socket.
280  */
281 static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count)
282 {
283     ssize_t len;
284
285     LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);
286
287     if (!count)
288         return 0;
289
290     len = from_buf(dsi, buf, count); /* 1. */
291     if (len)
292         return len;             /* 2. */
293   
294     len = readt(dsi->socket, buf, count, 0, 1); /* 3. */
295
296     LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len);
297
298     return len;
299 }
300
301 /*
302  * Essentially a loop around buf_read() to ensure "length" bytes are read
303  * from dsi->buffer and/or the socket.
304  *
305  * @returns length on success, some value smaller then length indicates an error
306  */
307 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
308 {
309   size_t stored;
310   ssize_t len;
311
312   LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length);
313
314   stored = 0;
315   while (stored < length) {
316       len = buf_read(dsi, (u_int8_t *) data + stored, length - stored);
317       if (len == -1 && (errno == EINTR || errno == EAGAIN)) {
318           LOG(log_debug, logtype_dsi, "dsi_stream_read: select read loop");
319           continue;
320       } else if (len > 0) {
321           stored += len;
322       } else { /* eof or error */
323           /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
324           if (len || stored || dsi->read_count) {
325               if (! (dsi->flags & DSI_DISCONNECTED)) {
326                   LOG(log_error, logtype_dsi, "dsi_stream_read: len:%d, %s",
327                       len, (len < 0) ? strerror(errno) : "unexpected EOF");
328               }
329               return 0;
330           }
331           break;
332       }
333   }
334
335   dsi->read_count += stored;
336
337   LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes): got: %u", length, stored);
338   return stored;
339 }
340
341 /*
342  * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads
343  * this tries to read larger chunks (8192 bytes) into a buffer.
344  */
345 static size_t dsi_buffered_stream_read(DSI *dsi, u_int8_t *data, const size_t length)
346 {
347   size_t len;
348   size_t buflen;
349
350   LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length);
351   
352   len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */
353   dsi->read_count += len;
354   if (len == length) {          /* got enough bytes from there ? */
355       return len;               /* yes */
356   }
357
358   /* fill the buffer with 8192 bytes or until buffer is full */
359   buflen = min(8192, dsi->end - dsi->eof);
360   if (buflen > 0) {
361       ssize_t ret;
362       ret = read(dsi->socket, dsi->eof, buflen);
363       if (ret > 0)
364           dsi->eof += ret;
365   }
366
367   /* now get the remaining data */
368   if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len)
369       return 0;
370   len += buflen;
371
372   return len;
373 }
374
375 /* ---------------------------------------
376 */
377 static void block_sig(DSI *dsi)
378 {
379   dsi->in_write++;
380 }
381
382 /* ---------------------------------------
383 */
384 static void unblock_sig(DSI *dsi)
385 {
386   dsi->in_write--;
387 }
388
389 /* ---------------------------------------
390  * write data. 0 on failure. this assumes that dsi_len will never
391  * cause an overflow in the data buffer. 
392  */
393 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
394 {
395   char block[DSI_BLOCKSIZ];
396   struct iovec iov[2];
397   size_t towrite;
398   ssize_t len;
399
400   LOG(log_maxdebug, logtype_dsi, "dsi_stream_send: %u bytes",
401       length ? length : sizeof(block));
402
403   block[0] = dsi->header.dsi_flags;
404   block[1] = dsi->header.dsi_command;
405   memcpy(block + 2, &dsi->header.dsi_requestID, 
406          sizeof(dsi->header.dsi_requestID));
407   memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
408   memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
409   memcpy(block + 12, &dsi->header.dsi_reserved,
410          sizeof(dsi->header.dsi_reserved));
411
412   if (!length) { /* just write the header */
413     length = (dsi_stream_write(dsi, block, sizeof(block), 0) == sizeof(block));
414     return length; /* really 0 on failure, 1 on success */
415   }
416   
417   /* block signals */
418   block_sig(dsi);
419   iov[0].iov_base = block;
420   iov[0].iov_len = sizeof(block);
421   iov[1].iov_base = buf;
422   iov[1].iov_len = length;
423   
424   towrite = sizeof(block) + length;
425   dsi->write_count += towrite;
426   while (towrite > 0) {
427       if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) || (len == 0))
428           continue;
429     
430       if ((size_t)len == towrite) /* wrote everything out */
431           break;
432       else if (len < 0) { /* error */
433           if (errno == EAGAIN || errno == EWOULDBLOCK) {
434               if (!dsi_peek(dsi)) {
435                   continue;
436               }
437           }
438           LOG(log_error, logtype_dsi, "dsi_stream_send: %s", strerror(errno));
439           unblock_sig(dsi);
440           return 0;
441       }
442     
443       towrite -= len;
444       if (towrite > length) { /* skip part of header */
445           iov[0].iov_base = (char *) iov[0].iov_base + len;
446           iov[0].iov_len -= len;
447       } else { /* skip to data */
448           if (iov[0].iov_len) {
449               len -= iov[0].iov_len;
450               iov[0].iov_len = 0;
451           }
452           iov[1].iov_base = (char *) iov[1].iov_base + len;
453           iov[1].iov_len -= len;
454       }
455   }
456   
457   unblock_sig(dsi);
458   return 1;
459 }
460
461
462 /* ---------------------------------------
463  * read data. function on success. 0 on failure. data length gets
464  * stored in length variable. this should really use size_t's, but
465  * that would require changes elsewhere. */
466 int dsi_stream_receive(DSI *dsi, void *buf, const size_t ilength,
467                        size_t *rlength)
468 {
469   char block[DSI_BLOCKSIZ];
470
471   LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: %u bytes", ilength);
472
473   /* read in the header */
474   if (dsi_buffered_stream_read(dsi, (u_int8_t *)block, sizeof(block)) != sizeof(block)) 
475     return 0;
476
477   dsi->header.dsi_flags = block[0];
478   dsi->header.dsi_command = block[1];
479   /* FIXME, not the right place, 
480      but we get a server disconnect without reason in the log
481   */
482   if (!block[1]) {
483       LOG(log_error, logtype_dsi, "dsi_stream_receive: invalid packet, fatal");
484       return 0;
485   }
486
487   memcpy(&dsi->header.dsi_requestID, block + 2, 
488          sizeof(dsi->header.dsi_requestID));
489   memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
490   memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
491   memcpy(&dsi->header.dsi_reserved, block + 12,
492          sizeof(dsi->header.dsi_reserved));
493   dsi->clientID = ntohs(dsi->header.dsi_requestID);
494   
495   /* make sure we don't over-write our buffers. */
496   *rlength = min(ntohl(dsi->header.dsi_len), ilength);
497   if (dsi_stream_read(dsi, buf, *rlength) != *rlength) 
498     return 0;
499
500   return block[1];
501 }