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