2 * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu)
3 * All rights reserved. See COPYRIGHT.
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.
14 #endif /* HAVE_CONFIG_H */
25 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <atalk/logger.h>
30 #include <atalk/dsi.h>
31 #include <netatalk/endian.h>
32 #include <atalk/util.h>
34 #define min(a,b) ((a) < (b) ? (a) : (b))
37 #define MSG_MORE 0x8000
41 #define MSG_DONTWAIT 0x40
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.
50 * @returns 0 when is possible to send again, -1 on error
52 static int dsi_peek(DSI *dsi)
54 static int warned = 0;
55 fd_set readfds, writefds;
60 LOG(log_debug, logtype_dsi, "dsi_peek");
62 maxfd = dsi->socket + 1;
65 if (dsi->socket == -1)
66 /* eg dsi_disconnect() might have disconnected us */
71 if (dsi->eof < dsi->end) {
72 /* space in read buffer */
73 FD_SET( dsi->socket, &readfds);
77 LOG(log_note, logtype_dsi, "dsi_peek: readahead buffer is full, possibly increase -dsireadbuf option");
78 LOG(log_note, logtype_dsi, "dsi_peek: dsireadbuf: %d, DSI quantum: %d, effective buffer size: %d",
80 dsi->server_quantum ? dsi->server_quantum : DSI_SERVQUANT_DEF,
81 dsi->end - dsi->buffer);
85 FD_SET( dsi->socket, &writefds);
87 /* No timeout: if there's nothing to read nor nothing to write,
88 * we've got nothing to do at all */
89 if ((ret = select( maxfd, &readfds, &writefds, NULL, NULL)) <= 0) {
90 if (ret == -1 && errno == EINTR)
91 /* we might have been interrupted by out timer, so restart select */
94 LOG(log_error, logtype_dsi, "dsi_peek: unexpected select return: %d %s",
95 ret, ret < 0 ? strerror(errno) : "");
99 if (FD_ISSET(dsi->socket, &writefds)) {
100 /* we can write again */
101 LOG(log_debug, logtype_dsi, "dsi_peek: can write again");
105 /* Check if there's sth to read, hopefully reading that will unblock the client */
106 if (FD_ISSET(dsi->socket, &readfds)) {
107 len = dsi->end - dsi->eof; /* it's ensured above that there's space */
109 if ((len = read(dsi->socket, dsi->eof, len)) <= 0) {
111 LOG(log_error, logtype_dsi, "dsi_peek: EOF");
114 LOG(log_error, logtype_dsi, "dsi_peek: read: %s", strerror(errno));
119 LOG(log_debug, logtype_dsi, "dsi_peek: read %d bytes", len);
129 * Return all bytes up to count from dsi->buffer if there are any buffered there
131 static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count)
135 if (dsi->buffer == NULL)
136 /* afpd master has no DSI buffering */
139 LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count);
141 nbe = dsi->eof - dsi->start;
144 nbe = min((size_t)nbe, count);
145 memcpy(buf, dsi->start, nbe);
148 if (dsi->eof == dsi->start)
149 dsi->start = dsi->eof = dsi->buffer;
152 LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u",
153 dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe);
159 * Get bytes from buffer dsi->buffer or read from socket
161 * 1. Check if there are bytes in the the dsi->buffer buffer.
162 * 2. Return bytes from (1) if yes.
163 * Note: this may return fewer bytes then requested in count !!
164 * 3. If the buffer was empty, read from the socket.
166 static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count)
170 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);
175 len = from_buf(dsi, buf, count); /* 1. */
179 len = readt(dsi->socket, buf, count, 0, 1); /* 3. */
181 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len);
187 * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads
188 * this tries to read larger chunks (8192 bytes) into a buffer.
190 static size_t dsi_buffered_stream_read(DSI *dsi, u_int8_t *data, const size_t length)
195 LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length);
197 len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */
198 dsi->read_count += len;
199 if (len == length) { /* got enough bytes from there ? */
200 return len; /* yes */
203 /* fill the buffer with 8192 bytes or until buffer is full */
204 buflen = min(8192, dsi->end - dsi->eof);
207 ret = read(dsi->socket, dsi->eof, buflen);
212 /* now get the remaining data */
213 if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len)
220 /* ---------------------------------------
222 static void block_sig(DSI *dsi)
227 /* ---------------------------------------
229 static void unblock_sig(DSI *dsi)
234 /*********************************************************************************
236 *********************************************************************************/
239 * Communication error with the client, enter disconnected state
241 * 1. close the socket
242 * 2. set the DSI_DISCONNECTED flag, remove possible sleep flags
244 * @returns 0 if successfully entered disconnected state
245 * -1 if ppid is 1 which means afpd master died
246 * or euid == 0 ie where still running as root (unauthenticated session)
248 int dsi_disconnect(DSI *dsi)
250 LOG(log_note, logtype_dsi, "dsi_disconnect: entering disconnected state");
251 dsi->proto_close(dsi); /* 1 */
252 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP); /* 2 */
253 dsi->flags |= DSI_DISCONNECTED;
259 /* ------------------------------
260 * write raw data. return actual bytes read. checks against EINTR
261 * aren't necessary if all of the signals have SA_RESTART
263 ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode)
267 unsigned int flags = 0;
272 LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): START", length);
274 if (dsi->flags & DSI_DISCONNECTED)
277 while (written < length) {
278 len = send(dsi->socket, (u_int8_t *) data + written, length - written, flags);
287 if (errno == EAGAIN || errno == EWOULDBLOCK) {
288 LOG(log_debug, logtype_dsi, "dsi_stream_write: send: %s", strerror(errno));
290 if (mode == DSI_NOWAIT && written == 0) {
291 /* DSI_NOWAIT is used by attention give up in this case. */
296 /* Try to read sth. in order to break up possible deadlock */
297 if (dsi_peek(dsi) != 0) {
301 /* Now try writing again */
305 LOG(log_error, logtype_dsi, "dsi_stream_write: %s", strerror(errno));
310 dsi->write_count += written;
311 LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): END", length);
319 /* ---------------------------------
322 ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t length)
329 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file(send %zd bytes): START", length);
331 if (dsi->flags & DSI_DISCONNECTED)
337 while (written < length) {
338 len = sys_sendfile(dsi->socket, fromfd, &pos, length - written);
341 if (errno == EINVAL || errno == ENOSYS) {
345 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
347 * May return EINTR too, see:
348 * http://wesunsolve.net/bugid/id/6408517
349 * https://issues.apache.org/bugzilla/show_bug.cgi?id=44550
351 #if defined(SOLARIS) || defined(FREEBSD)
353 /* we actually have sent sth., adjust counters and keep trying */
360 /* can't go back to blocking mode, exit, the next read
361 will return with an error and afpd will die.
367 LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno));
371 /* afpd is going to exit */
379 dsi->write_count += written;
383 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sent: %zd", written);
392 * Essentially a loop around buf_read() to ensure "length" bytes are read
393 * from dsi->buffer and/or the socket.
395 * @returns length on success, some value smaller then length indicates an error
397 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
402 if (dsi->flags & DSI_DISCONNECTED)
405 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length);
408 while (stored < length) {
409 len = buf_read(dsi, (u_int8_t *) data + stored, length - stored);
410 if (len == -1 && (errno == EINTR || errno == EAGAIN)) {
411 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read: select read loop");
413 } else if (len > 0) {
415 } else { /* eof or error */
416 /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
418 if (errno == ECONNRESET)
419 dsi->flags |= DSI_GOT_ECONNRESET;
421 if (len || stored || dsi->read_count) {
422 if (! (dsi->flags & DSI_DISCONNECTED)) {
423 LOG(log_error, logtype_dsi, "dsi_stream_read: len:%d, %s",
424 len, (len < 0) ? strerror(errno) : "unexpected EOF");
432 dsi->read_count += stored;
434 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes): got: %u", length, stored);
438 /* ---------------------------------------
439 * write data. 0 on failure. this assumes that dsi_len will never
440 * cause an overflow in the data buffer.
442 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
444 char block[DSI_BLOCKSIZ];
449 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): START", length);
451 if (dsi->flags & DSI_DISCONNECTED)
454 block[0] = dsi->header.dsi_flags;
455 block[1] = dsi->header.dsi_command;
456 memcpy(block + 2, &dsi->header.dsi_requestID,
457 sizeof(dsi->header.dsi_requestID));
458 memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
459 memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
460 memcpy(block + 12, &dsi->header.dsi_reserved,
461 sizeof(dsi->header.dsi_reserved));
463 if (!length) { /* just write the header */
464 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): DSI header, no data", sizeof(block));
465 length = (dsi_stream_write(dsi, block, sizeof(block), 0) == sizeof(block));
466 return length; /* really 0 on failure, 1 on success */
471 iov[0].iov_base = block;
472 iov[0].iov_len = sizeof(block);
473 iov[1].iov_base = buf;
474 iov[1].iov_len = length;
476 towrite = sizeof(block) + length;
477 dsi->write_count += towrite;
478 while (towrite > 0) {
479 if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) || (len == 0))
482 if ((size_t)len == towrite) /* wrote everything out */
484 else if (len < 0) { /* error */
485 if (errno == EAGAIN || errno == EWOULDBLOCK) {
486 if (!dsi_peek(dsi)) {
490 LOG(log_error, logtype_dsi, "dsi_stream_send: %s", strerror(errno));
496 if (towrite > length) { /* skip part of header */
497 iov[0].iov_base = (char *) iov[0].iov_base + len;
498 iov[0].iov_len -= len;
499 } else { /* skip to data */
500 if (iov[0].iov_len) {
501 len -= iov[0].iov_len;
504 iov[1].iov_base = (char *) iov[1].iov_base + len;
505 iov[1].iov_len -= len;
509 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): END", length);
517 * Read DSI command and data
519 * @param dsi (rw) DSI handle
521 * @return DSI function on success, 0 on failure
523 int dsi_stream_receive(DSI *dsi)
525 char block[DSI_BLOCKSIZ];
527 LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: START");
529 if (dsi->flags & DSI_DISCONNECTED)
532 /* read in the header */
533 if (dsi_buffered_stream_read(dsi, (u_int8_t *)block, sizeof(block)) != sizeof(block))
536 dsi->header.dsi_flags = block[0];
537 dsi->header.dsi_command = block[1];
539 if (dsi->header.dsi_command == 0)
542 memcpy(&dsi->header.dsi_requestID, block + 2, sizeof(dsi->header.dsi_requestID));
543 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
544 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
545 memcpy(&dsi->header.dsi_reserved, block + 12, sizeof(dsi->header.dsi_reserved));
546 dsi->clientID = ntohs(dsi->header.dsi_requestID);
548 /* make sure we don't over-write our buffers. */
549 dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
550 if (dsi_stream_read(dsi, dsi->commands, dsi->cmdlen) != dsi->cmdlen)
553 LOG(log_debug, logtype_dsi, "dsi_stream_receive: DSI cmdlen: %zd", dsi->cmdlen);