X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=blobdiff_plain;f=libatalk%2Fdsi%2Fdsi_stream.c;h=c8f859ce1ca4ff8264f59dadddbbb8ceaf190fb7;hp=bc84a5fa2bb47849ac5cc2129116e5ad2f137aba;hb=42a0a094b72577aee3fee7f186da52dc83db001d;hpb=c09a6a6dd71e8597a5ae34b8e483ea2804444153 diff --git a/libatalk/dsi/dsi_stream.c b/libatalk/dsi/dsi_stream.c index bc84a5fa..c8f859ce 100644 --- a/libatalk/dsi/dsi_stream.c +++ b/libatalk/dsi/dsi_stream.c @@ -1,6 +1,7 @@ /* * Copyright (c) 1998 Adrian Sun (asun@zoology.washington.edu) - * All rights reserved. See COPYRIGHT. + * Copyright (c) 2010,2011,2012 Frank Lahm +> * All rights reserved. See COPYRIGHT. * * this file provides the following functions: * dsi_stream_write: just write a bunch of bytes. @@ -15,24 +16,21 @@ #include #include - -#ifdef HAVE_UNISTD_H #include -#endif - #include #include #include #include #include +#ifdef HAVE_SENDFILEV +#include +#endif + #include #include -#include #include -#define min(a,b) ((a) < (b) ? (a) : (b)) - #ifndef MSG_MORE #define MSG_MORE 0x8000 #endif @@ -41,6 +39,17 @@ #define MSG_DONTWAIT 0x40 #endif +/* Pack a DSI header in wire format */ +static void dsi_header_pack_reply(const DSI *dsi, char *buf) +{ + buf[0] = dsi->header.dsi_flags; + buf[1] = dsi->header.dsi_command; + memcpy(buf + 2, &dsi->header.dsi_requestID, sizeof(dsi->header.dsi_requestID)); + memcpy(buf + 4, &dsi->header.dsi_data.dsi_code, sizeof(dsi->header.dsi_data.dsi_code)); + memcpy(buf + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len)); + memcpy(buf + 12, &dsi->header.dsi_reserved, sizeof(dsi->header.dsi_reserved)); +} + /* * afpd is sleeping too much while trying to send something. * May be there's no reader or the reader is also sleeping in write, @@ -62,6 +71,9 @@ static int dsi_peek(DSI *dsi) maxfd = dsi->socket + 1; while (1) { + if (dsi->socket == -1) + /* eg dsi_disconnect() might have disconnected us */ + return -1; FD_ZERO(&readfds); FD_ZERO(&writefds); @@ -103,7 +115,7 @@ static int dsi_peek(DSI *dsi) if (FD_ISSET(dsi->socket, &readfds)) { len = dsi->end - dsi->eof; /* it's ensured above that there's space */ - if ((len = read(dsi->socket, dsi->eof, len)) <= 0) { + if ((len = recv(dsi->socket, dsi->eof, len, 0)) <= 0) { if (len == 0) { LOG(log_error, logtype_dsi, "dsi_peek: EOF"); return -1; @@ -125,7 +137,7 @@ static int dsi_peek(DSI *dsi) /* * Return all bytes up to count from dsi->buffer if there are any buffered there */ -static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count) +static size_t from_buf(DSI *dsi, uint8_t *buf, size_t count) { size_t nbe = 0; @@ -138,7 +150,7 @@ static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count) nbe = dsi->eof - dsi->start; if (nbe > 0) { - nbe = min((size_t)nbe, count); + nbe = MIN((size_t)nbe, count); memcpy(buf, dsi->start, nbe); dsi->start += nbe; @@ -160,7 +172,7 @@ static size_t from_buf(DSI *dsi, u_int8_t *buf, size_t count) * Note: this may return fewer bytes then requested in count !! * 3. If the buffer was empty, read from the socket. */ -static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count) +static ssize_t buf_read(DSI *dsi, uint8_t *buf, size_t count) { ssize_t len; @@ -173,7 +185,7 @@ static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count) if (len) return len; /* 2. */ - len = readt(dsi->socket, buf, count, 0, 1); /* 3. */ + len = readt(dsi->socket, buf, count, 0, 0); /* 3. */ LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len); @@ -184,7 +196,7 @@ static ssize_t buf_read(DSI *dsi, u_int8_t *buf, size_t count) * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads * this tries to read larger chunks (8192 bytes) into a buffer. */ -static size_t dsi_buffered_stream_read(DSI *dsi, u_int8_t *data, const size_t length) +static size_t dsi_buffered_stream_read(DSI *dsi, uint8_t *data, const size_t length) { size_t len; size_t buflen; @@ -198,10 +210,10 @@ static size_t dsi_buffered_stream_read(DSI *dsi, u_int8_t *data, const size_t le } /* fill the buffer with 8192 bytes or until buffer is full */ - buflen = min(8192, dsi->end - dsi->eof); + buflen = MIN(8192, dsi->end - dsi->eof); if (buflen > 0) { ssize_t ret; - ret = read(dsi->socket, dsi->eof, buflen); + ret = recv(dsi->socket, dsi->eof, buflen, 0); if (ret > 0) dsi->eof += ret; } @@ -261,7 +273,7 @@ ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode) { size_t written; ssize_t len; - unsigned int flags = 0; + unsigned int flags; dsi->in_write++; written = 0; @@ -271,8 +283,13 @@ ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode) if (dsi->flags & DSI_DISCONNECTED) return -1; + if (mode & DSI_MSG_MORE) + flags = MSG_MORE; + else + flags = 0; + while (written < length) { - len = send(dsi->socket, (u_int8_t *) data + written, length - written, flags); + len = send(dsi->socket, (uint8_t *) data + written, length - written, flags); if (len >= 0) { written += len; continue; @@ -312,72 +329,145 @@ exit: return written; } - /* --------------------------------- */ #ifdef WITH_SENDFILE -ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t length) +ssize_t dsi_stream_read_file(DSI *dsi, const int fromfd, off_t offset, const size_t length, const int err) { - int ret = 0; - size_t written; - ssize_t len; - off_t pos = offset; + int ret = 0; + size_t written = 0; + size_t total = length; + ssize_t len; + off_t pos = offset; + char block[DSI_BLOCKSIZ]; +#ifdef HAVE_SENDFILEV + int sfvcnt; + struct sendfilevec vec[2]; + ssize_t nwritten; +#elif defined(FREEBSD) + ssize_t nwritten; + void *hdrp; + struct sf_hdtr hdr; + struct iovec iovec; + hdr.headers = &iovec; + hdr.hdr_cnt = 1; + hdr.trailers = NULL; + hdr.trl_cnt = 0; + hdrp = &hdr; +#endif - LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file(send %zd bytes): START", length); + LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file(off: %jd, len: %zu)", (intmax_t)offset, length); - if (dsi->flags & DSI_DISCONNECTED) - return -1; + if (dsi->flags & DSI_DISCONNECTED) + return -1; - dsi->in_write++; - written = 0; + dsi->in_write++; + + dsi->flags |= DSI_NOREPLY; + dsi->header.dsi_flags = DSIFL_REPLY; + dsi->header.dsi_len = htonl(length); + dsi->header.dsi_data.dsi_code = htonl(err); + dsi_header_pack_reply(dsi, block); + +#ifdef HAVE_SENDFILEV + total += DSI_BLOCKSIZ; + sfvcnt = 2; + vec[0].sfv_fd = SFV_FD_SELF; + vec[0].sfv_flag = 0; + /* Cast to unsigned long to prevent sign extension of the + * pointer value for the LFS case; see Apache PR 39463. */ + vec[0].sfv_off = (unsigned long)block; + vec[0].sfv_len = DSI_BLOCKSIZ; + vec[1].sfv_fd = fromfd; + vec[1].sfv_flag = 0; + vec[1].sfv_off = offset; + vec[1].sfv_len = length; +#elif defined(FREEBSD) + iovec.iov_base = block; + iovec.iov_len = DSI_BLOCKSIZ; +#else + dsi_stream_write(dsi, block, sizeof(block), DSI_MSG_MORE); +#endif - while (written < length) { - len = sys_sendfile(dsi->socket, fromfd, &pos, length - written); - - if (len < 0) { - if (errno == EINTR) - continue; - if (errno == EINVAL || errno == ENOSYS) { - ret = -1; - goto exit; - } - if (errno == EAGAIN || errno == EWOULDBLOCK) { -#if defined(SOLARIS) || defined(FREEBSD) - if (pos > offset) { - /* we actually have sent sth., adjust counters and keep trying */ - len = pos - offset; - written += len; - offset = pos; - } + while (written < total) { +#ifdef HAVE_SENDFILEV + nwritten = 0; + len = sendfilev(dsi->socket, vec, sfvcnt, &nwritten); +#elif defined(FREEBSD) + len = sendfile(fromfd, dsi->socket, pos, total - written, hdrp, &nwritten, 0); + if (len == 0) + len = nwritten; +#else + len = sys_sendfile(dsi->socket, fromfd, &pos, total - written); #endif - if (dsi_peek(dsi)) { - /* can't go back to blocking mode, exit, the next read - will return with an error and afpd will die. - */ - break; - } - continue; - } - LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno)); - break; - } - else if (!len) { - /* afpd is going to exit */ - ret = -1; - goto exit; - } - else - written += len; - } + if (len < 0) { + switch (errno) { + case EINTR: + case EAGAIN: + len = 0; +#if defined(HAVE_SENDFILEV) || defined(FREEBSD) + len = (size_t)nwritten; +#elif defined(SOLARIS) + if (pos > offset) { + /* we actually have sent sth., adjust counters and keep trying */ + len = pos - offset; + offset = pos; + } +#endif /* HAVE_SENDFILEV */ - dsi->write_count += written; + if (dsi_peek(dsi) != 0) { + ret = -1; + goto exit; + } + break; + default: + LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno)); + ret = -1; + goto exit; + } + } else if (len == 0) { + /* afpd is going to exit */ + ret = -1; + goto exit; + } +#ifdef HAVE_SENDFILEV + if (sfvcnt == 2 && len >= vec[0].sfv_len) { + vec[1].sfv_off += len - vec[0].sfv_len; + vec[1].sfv_len -= len - vec[0].sfv_len; + + vec[0] = vec[1]; + sfvcnt = 1; + } else { + vec[0].sfv_off += len; + vec[0].sfv_len -= len; + } +#elif defined(FREEBSD) + if (hdrp) { + if (len >= iovec.iov_len) { + hdrp = NULL; + len -= iovec.iov_len; /* len now contains how much sendfile() actually sent from the file */ + } else { + iovec.iov_len -= len; + iovec.iov_base += len; + len = 0; + } + } + pos += len; +#endif /* HAVE_SENDFILEV */ + LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: wrote: %zd", len); + written += len; + } +#ifdef HAVE_SENDFILEV + written -= DSI_BLOCKSIZ; +#endif + dsi->write_count += written; exit: - dsi->in_write--; - LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sent: %zd", written); - if (ret != 0) - return -1; - return written; + dsi->in_write--; + LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: written: %zd", written); + if (ret != 0) + return -1; + return written; } #endif @@ -400,7 +490,7 @@ size_t dsi_stream_read(DSI *dsi, void *data, const size_t length) stored = 0; while (stored < length) { - len = buf_read(dsi, (u_int8_t *) data + stored, length - stored); + len = buf_read(dsi, (uint8_t *) data + stored, length - stored); if (len == -1 && (errno == EINTR || errno == EAGAIN)) { LOG(log_maxdebug, logtype_dsi, "dsi_stream_read: select read loop"); continue; @@ -408,10 +498,6 @@ size_t dsi_stream_read(DSI *dsi, void *data, const size_t length) stored += len; } else { /* eof or error */ /* don't log EOF error if it's just after connect (OSX 10.3 probe) */ -#if 0 - if (errno == ECONNRESET) - dsi->flags |= DSI_GOT_ECONNRESET; -#endif if (len || stored || dsi->read_count) { if (! (dsi->flags & DSI_DISCONNECTED)) { LOG(log_error, logtype_dsi, "dsi_stream_read: len:%d, %s", @@ -437,6 +523,7 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length) { char block[DSI_BLOCKSIZ]; struct iovec iov[2]; + int iovecs = 2; size_t towrite; ssize_t len; @@ -445,14 +532,7 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length) if (dsi->flags & DSI_DISCONNECTED) return 0; - block[0] = dsi->header.dsi_flags; - block[1] = dsi->header.dsi_command; - memcpy(block + 2, &dsi->header.dsi_requestID, - sizeof(dsi->header.dsi_requestID)); - memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code)); - memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len)); - memcpy(block + 12, &dsi->header.dsi_reserved, - sizeof(dsi->header.dsi_reserved)); + dsi_header_pack_reply(dsi, block); if (!length) { /* just write the header */ LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): DSI header, no data", sizeof(block)); @@ -470,14 +550,14 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length) towrite = sizeof(block) + length; dsi->write_count += towrite; while (towrite > 0) { - if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) || (len == 0)) + if (((len = writev(dsi->socket, iov, iovecs)) == -1 && errno == EINTR) || (len == 0)) continue; if ((size_t)len == towrite) /* wrote everything out */ break; else if (len < 0) { /* error */ if (errno == EAGAIN || errno == EWOULDBLOCK) { - if (!dsi_peek(dsi)) { + if (dsi_peek(dsi) == 0) { continue; } } @@ -491,12 +571,13 @@ int dsi_stream_send(DSI *dsi, void *buf, size_t length) iov[0].iov_base = (char *) iov[0].iov_base + len; iov[0].iov_len -= len; } else { /* skip to data */ - if (iov[0].iov_len) { + if (iovecs == 2) { + iovecs = 1; len -= iov[0].iov_len; - iov[0].iov_len = 0; + iov[0] = iov[1]; } - iov[1].iov_base = (char *) iov[1].iov_base + len; - iov[1].iov_len -= len; + iov[0].iov_base = (char *) iov[0].iov_base + len; + iov[0].iov_len -= len; } } @@ -524,7 +605,7 @@ int dsi_stream_receive(DSI *dsi) return 0; /* read in the header */ - if (dsi_buffered_stream_read(dsi, (u_int8_t *)block, sizeof(block)) != sizeof(block)) + if (dsi_buffered_stream_read(dsi, (uint8_t *)block, sizeof(block)) != sizeof(block)) return 0; dsi->header.dsi_flags = block[0]; @@ -534,14 +615,23 @@ int dsi_stream_receive(DSI *dsi) return 0; memcpy(&dsi->header.dsi_requestID, block + 2, sizeof(dsi->header.dsi_requestID)); - memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code)); + memcpy(&dsi->header.dsi_data.dsi_doff, block + 4, sizeof(dsi->header.dsi_data.dsi_doff)); + dsi->header.dsi_data.dsi_doff = htonl(dsi->header.dsi_data.dsi_doff); memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len)); + memcpy(&dsi->header.dsi_reserved, block + 12, sizeof(dsi->header.dsi_reserved)); dsi->clientID = ntohs(dsi->header.dsi_requestID); /* make sure we don't over-write our buffers. */ - dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ); - if (dsi_stream_read(dsi, dsi->commands, dsi->cmdlen) != dsi->cmdlen) + dsi->cmdlen = MIN(ntohl(dsi->header.dsi_len), dsi->server_quantum); + + /* Receiving DSIWrite data is done in AFP function, not here */ + if (dsi->header.dsi_data.dsi_doff) { + LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: write request"); + dsi->cmdlen = dsi->header.dsi_data.dsi_doff; + } + + if (dsi_stream_read(dsi, dsi->commands, dsi->cmdlen) != dsi->cmdlen) return 0; LOG(log_debug, logtype_dsi, "dsi_stream_receive: DSI cmdlen: %zd", dsi->cmdlen);