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