2 * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu)
3 * Copyright (c) 2010,2011,2012 Frank Lahm <franklahm@googlemail.com>
4 * All rights reserved. See COPYRIGHT.
6 * this file provides the following functions:
7 * dsi_stream_write: just write a bunch of bytes.
8 * dsi_stream_read: just read a bunch of bytes.
9 * dsi_stream_send: send a DSI header + data.
10 * dsi_stream_receive: read a DSI header + data.
15 #endif /* HAVE_CONFIG_H */
22 #include <sys/types.h>
23 #include <sys/socket.h>
27 #include <sys/sendfile.h>
30 #include <atalk/logger.h>
31 #include <atalk/dsi.h>
32 #include <atalk/util.h>
35 #define MSG_MORE 0x8000
39 #define MSG_DONTWAIT 0x40
42 /* Pack a DSI header in wire format */
43 static void dsi_header_pack_reply(const DSI *dsi, char *buf)
45 buf[0] = dsi->header.dsi_flags;
46 buf[1] = dsi->header.dsi_command;
47 memcpy(buf + 2, &dsi->header.dsi_requestID, sizeof(dsi->header.dsi_requestID));
48 memcpy(buf + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
49 memcpy(buf + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
50 memcpy(buf + 12, &dsi->header.dsi_reserved, sizeof(dsi->header.dsi_reserved));
54 * afpd is sleeping too much while trying to send something.
55 * May be there's no reader or the reader is also sleeping in write,
56 * look if there's some data for us to read, hopefully it will wake up
57 * the reader so we can write again.
59 * @returns 0 when is possible to send again, -1 on error
61 static int dsi_peek(DSI *dsi)
63 static int warned = 0;
64 fd_set readfds, writefds;
69 LOG(log_debug, logtype_dsi, "dsi_peek");
71 maxfd = dsi->socket + 1;
74 if (dsi->socket == -1)
75 /* eg dsi_disconnect() might have disconnected us */
80 if (dsi->eof < dsi->end) {
81 /* space in read buffer */
82 FD_SET( dsi->socket, &readfds);
86 LOG(log_note, logtype_dsi, "dsi_peek: readahead buffer is full, possibly increase -dsireadbuf option");
87 LOG(log_note, logtype_dsi, "dsi_peek: dsireadbuf: %d, DSI quantum: %d, effective buffer size: %d",
89 dsi->server_quantum ? dsi->server_quantum : DSI_SERVQUANT_DEF,
90 dsi->end - dsi->buffer);
94 FD_SET( dsi->socket, &writefds);
96 /* No timeout: if there's nothing to read nor nothing to write,
97 * we've got nothing to do at all */
98 if ((ret = select( maxfd, &readfds, &writefds, NULL, NULL)) <= 0) {
99 if (ret == -1 && errno == EINTR)
100 /* we might have been interrupted by out timer, so restart select */
103 LOG(log_error, logtype_dsi, "dsi_peek: unexpected select return: %d %s",
104 ret, ret < 0 ? strerror(errno) : "");
108 if (FD_ISSET(dsi->socket, &writefds)) {
109 /* we can write again */
110 LOG(log_debug, logtype_dsi, "dsi_peek: can write again");
114 /* Check if there's sth to read, hopefully reading that will unblock the client */
115 if (FD_ISSET(dsi->socket, &readfds)) {
116 len = dsi->end - dsi->eof; /* it's ensured above that there's space */
118 if ((len = read(dsi->socket, dsi->eof, len)) <= 0) {
120 LOG(log_error, logtype_dsi, "dsi_peek: EOF");
123 LOG(log_error, logtype_dsi, "dsi_peek: read: %s", strerror(errno));
128 LOG(log_debug, logtype_dsi, "dsi_peek: read %d bytes", len);
138 * Return all bytes up to count from dsi->buffer if there are any buffered there
140 static size_t from_buf(DSI *dsi, uint8_t *buf, size_t count)
144 if (dsi->buffer == NULL)
145 /* afpd master has no DSI buffering */
148 LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count);
150 nbe = dsi->eof - dsi->start;
153 nbe = MIN((size_t)nbe, count);
154 memcpy(buf, dsi->start, nbe);
157 if (dsi->eof == dsi->start)
158 dsi->start = dsi->eof = dsi->buffer;
161 LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u",
162 dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe);
168 * Get bytes from buffer dsi->buffer or read from socket
170 * 1. Check if there are bytes in the the dsi->buffer buffer.
171 * 2. Return bytes from (1) if yes.
172 * Note: this may return fewer bytes then requested in count !!
173 * 3. If the buffer was empty, read from the socket.
175 static ssize_t buf_read(DSI *dsi, uint8_t *buf, size_t count)
179 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);
184 len = from_buf(dsi, buf, count); /* 1. */
188 len = readt(dsi->socket, buf, count, 0, 1); /* 3. */
190 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len);
196 * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads
197 * this tries to read larger chunks (8192 bytes) into a buffer.
199 static size_t dsi_buffered_stream_read(DSI *dsi, uint8_t *data, const size_t length)
204 LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length);
206 len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */
207 dsi->read_count += len;
208 if (len == length) { /* got enough bytes from there ? */
209 return len; /* yes */
212 /* fill the buffer with 8192 bytes or until buffer is full */
213 buflen = MIN(8192, dsi->end - dsi->eof);
216 ret = read(dsi->socket, dsi->eof, buflen);
221 /* now get the remaining data */
222 if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len)
229 /* ---------------------------------------
231 static void block_sig(DSI *dsi)
236 /* ---------------------------------------
238 static void unblock_sig(DSI *dsi)
243 /*********************************************************************************
245 *********************************************************************************/
248 * Communication error with the client, enter disconnected state
250 * 1. close the socket
251 * 2. set the DSI_DISCONNECTED flag, remove possible sleep flags
253 * @returns 0 if successfully entered disconnected state
254 * -1 if ppid is 1 which means afpd master died
255 * or euid == 0 ie where still running as root (unauthenticated session)
257 int dsi_disconnect(DSI *dsi)
259 LOG(log_note, logtype_dsi, "dsi_disconnect: entering disconnected state");
260 dsi->proto_close(dsi); /* 1 */
261 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP); /* 2 */
262 dsi->flags |= DSI_DISCONNECTED;
268 /* ------------------------------
269 * write raw data. return actual bytes read. checks against EINTR
270 * aren't necessary if all of the signals have SA_RESTART
272 ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode)
281 LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): START", length);
283 if (dsi->flags & DSI_DISCONNECTED)
286 if (mode & DSI_MSG_MORE)
291 while (written < length) {
292 len = send(dsi->socket, (uint8_t *) data + written, length - written, flags);
301 if (errno == EAGAIN || errno == EWOULDBLOCK) {
302 LOG(log_debug, logtype_dsi, "dsi_stream_write: send: %s", strerror(errno));
304 if (mode == DSI_NOWAIT && written == 0) {
305 /* DSI_NOWAIT is used by attention give up in this case. */
310 /* Try to read sth. in order to break up possible deadlock */
311 if (dsi_peek(dsi) != 0) {
315 /* Now try writing again */
319 LOG(log_error, logtype_dsi, "dsi_stream_write: %s", strerror(errno));
324 dsi->write_count += written;
325 LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): END", length);
332 /* ---------------------------------
335 ssize_t dsi_stream_read_file(DSI *dsi, const int fromfd, off_t offset, const size_t length, const int err)
339 size_t total = length;
342 char block[DSI_BLOCKSIZ];
343 #ifdef HAVE_SENDFILEV
345 struct sendfilevec vec[2];
349 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file(off: %jd, len: %zu)", (intmax_t)offset, length);
351 if (dsi->flags & DSI_DISCONNECTED)
356 dsi->flags |= DSI_NOREPLY;
357 dsi->header.dsi_flags = DSIFL_REPLY;
358 dsi->header.dsi_len = htonl(length);
359 dsi->header.dsi_code = htonl(err);
360 dsi_header_pack_reply(dsi, block);
362 #ifdef HAVE_SENDFILEV
363 total += DSI_BLOCKSIZ;
365 vec[0].sfv_fd = SFV_FD_SELF;
367 /* Cast to unsigned long to prevent sign extension of the
368 * pointer value for the LFS case; see Apache PR 39463. */
369 vec[0].sfv_off = (unsigned long)block;
370 vec[0].sfv_len = DSI_BLOCKSIZ;
371 vec[1].sfv_fd = fromfd;
373 vec[1].sfv_off = offset;
374 vec[1].sfv_len = length;
376 dsi_stream_write(dsi, block, sizeof(block), DSI_MSG_MORE);
379 while (written < total) {
380 #ifdef HAVE_SENDFILEV
382 len = sendfilev(dsi->socket, vec, sfvcnt, &nwritten);
384 len = sys_sendfile(dsi->socket, fromfd, &pos, total - written);
391 #ifdef HAVE_SENDFILEV
392 len = (size_t)nwritten;
394 #if defined(SOLARIS) || defined(FREEBSD)
396 /* we actually have sent sth., adjust counters and keep trying */
400 #endif /* defined(SOLARIS) || defined(FREEBSD) */
401 #endif /* HAVE_SENDFILEV */
403 if (dsi_peek(dsi) != 0) {
409 LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno));
413 } else if (len == 0) {
414 /* afpd is going to exit */
418 #ifdef HAVE_SENDFILEV
419 if (sfvcnt == 2 && len >= vec[0].sfv_len) {
420 vec[1].sfv_off += len - vec[0].sfv_len;
421 vec[1].sfv_len -= len - vec[0].sfv_len;
426 vec[0].sfv_off += len;
427 vec[0].sfv_len -= len;
429 #endif /* HAVE_SENDFILEV */
430 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: wrote: %zd", len);
433 #ifdef HAVE_SENDFILEV
434 written -= DSI_BLOCKSIZ;
436 dsi->write_count += written;
440 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: written: %zd", written);
449 * Essentially a loop around buf_read() to ensure "length" bytes are read
450 * from dsi->buffer and/or the socket.
452 * @returns length on success, some value smaller then length indicates an error
454 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
459 if (dsi->flags & DSI_DISCONNECTED)
462 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length);
465 while (stored < length) {
466 len = buf_read(dsi, (uint8_t *) data + stored, length - stored);
467 if (len == -1 && (errno == EINTR || errno == EAGAIN)) {
468 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read: select read loop");
470 } else if (len > 0) {
472 } else { /* eof or error */
473 /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
475 if (errno == ECONNRESET)
476 dsi->flags |= DSI_GOT_ECONNRESET;
478 if (len || stored || dsi->read_count) {
479 if (! (dsi->flags & DSI_DISCONNECTED)) {
480 LOG(log_error, logtype_dsi, "dsi_stream_read: len:%d, %s",
481 len, (len < 0) ? strerror(errno) : "unexpected EOF");
489 dsi->read_count += stored;
491 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes): got: %u", length, stored);
495 /* ---------------------------------------
496 * write data. 0 on failure. this assumes that dsi_len will never
497 * cause an overflow in the data buffer.
499 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
501 char block[DSI_BLOCKSIZ];
506 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): START", length);
508 if (dsi->flags & DSI_DISCONNECTED)
511 dsi_header_pack_reply(dsi, block);
513 if (!length) { /* just write the header */
514 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): DSI header, no data", sizeof(block));
515 length = (dsi_stream_write(dsi, block, sizeof(block), 0) == sizeof(block));
516 return length; /* really 0 on failure, 1 on success */
521 iov[0].iov_base = block;
522 iov[0].iov_len = sizeof(block);
523 iov[1].iov_base = buf;
524 iov[1].iov_len = length;
526 towrite = sizeof(block) + length;
527 dsi->write_count += towrite;
528 while (towrite > 0) {
529 if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) || (len == 0))
532 if ((size_t)len == towrite) /* wrote everything out */
534 else if (len < 0) { /* error */
535 if (errno == EAGAIN || errno == EWOULDBLOCK) {
536 if (dsi_peek(dsi) == 0) {
540 LOG(log_error, logtype_dsi, "dsi_stream_send: %s", strerror(errno));
546 if (towrite > length) { /* skip part of header */
547 iov[0].iov_base = (char *) iov[0].iov_base + len;
548 iov[0].iov_len -= len;
549 } else { /* skip to data */
550 if (iov[0].iov_len) {
551 len -= iov[0].iov_len;
554 iov[1].iov_base = (char *) iov[1].iov_base + len;
555 iov[1].iov_len -= len;
559 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): END", length);
567 * Read DSI command and data
569 * @param dsi (rw) DSI handle
571 * @return DSI function on success, 0 on failure
573 int dsi_stream_receive(DSI *dsi)
575 char block[DSI_BLOCKSIZ];
577 LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: START");
579 if (dsi->flags & DSI_DISCONNECTED)
582 /* read in the header */
583 if (dsi_buffered_stream_read(dsi, (uint8_t *)block, sizeof(block)) != sizeof(block))
586 dsi->header.dsi_flags = block[0];
587 dsi->header.dsi_command = block[1];
589 if (dsi->header.dsi_command == 0)
592 memcpy(&dsi->header.dsi_requestID, block + 2, sizeof(dsi->header.dsi_requestID));
593 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
594 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
595 memcpy(&dsi->header.dsi_reserved, block + 12, sizeof(dsi->header.dsi_reserved));
596 dsi->clientID = ntohs(dsi->header.dsi_requestID);
598 /* make sure we don't over-write our buffers. */
599 dsi->cmdlen = MIN(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
600 if (dsi_stream_read(dsi, dsi->commands, dsi->cmdlen) != dsi->cmdlen)
603 LOG(log_debug, logtype_dsi, "dsi_stream_receive: DSI cmdlen: %zd", dsi->cmdlen);