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 <atalk/util.h>
33 #define min(a,b) ((a) < (b) ? (a) : (b))
36 #define MSG_MORE 0x8000
40 #define MSG_DONTWAIT 0x40
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.
49 * @returns 0 when is possible to send again, -1 on error
51 static int dsi_peek(DSI *dsi)
53 static int warned = 0;
54 fd_set readfds, writefds;
59 LOG(log_debug, logtype_dsi, "dsi_peek");
61 maxfd = dsi->socket + 1;
64 if (dsi->socket == -1)
65 /* eg dsi_disconnect() might have disconnected us */
70 if (dsi->eof < dsi->end) {
71 /* space in read buffer */
72 FD_SET( dsi->socket, &readfds);
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",
79 dsi->server_quantum ? dsi->server_quantum : DSI_SERVQUANT_DEF,
80 dsi->end - dsi->buffer);
84 FD_SET( dsi->socket, &writefds);
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 */
93 LOG(log_error, logtype_dsi, "dsi_peek: unexpected select return: %d %s",
94 ret, ret < 0 ? strerror(errno) : "");
98 if (FD_ISSET(dsi->socket, &writefds)) {
99 /* we can write again */
100 LOG(log_debug, logtype_dsi, "dsi_peek: can write again");
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 */
108 if ((len = read(dsi->socket, dsi->eof, len)) <= 0) {
110 LOG(log_error, logtype_dsi, "dsi_peek: EOF");
113 LOG(log_error, logtype_dsi, "dsi_peek: read: %s", strerror(errno));
118 LOG(log_debug, logtype_dsi, "dsi_peek: read %d bytes", len);
128 * Return all bytes up to count from dsi->buffer if there are any buffered there
130 static size_t from_buf(DSI *dsi, uint8_t *buf, size_t count)
134 if (dsi->buffer == NULL)
135 /* afpd master has no DSI buffering */
138 LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count);
140 nbe = dsi->eof - dsi->start;
143 nbe = min((size_t)nbe, count);
144 memcpy(buf, dsi->start, nbe);
147 if (dsi->eof == dsi->start)
148 dsi->start = dsi->eof = dsi->buffer;
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);
158 * Get bytes from buffer dsi->buffer or read from socket
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.
165 static ssize_t buf_read(DSI *dsi, uint8_t *buf, size_t count)
169 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);
174 len = from_buf(dsi, buf, count); /* 1. */
178 len = readt(dsi->socket, buf, count, 0, 1); /* 3. */
180 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len);
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.
189 static size_t dsi_buffered_stream_read(DSI *dsi, uint8_t *data, const size_t length)
194 LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length);
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 */
202 /* fill the buffer with 8192 bytes or until buffer is full */
203 buflen = min(8192, dsi->end - dsi->eof);
206 ret = read(dsi->socket, dsi->eof, buflen);
211 /* now get the remaining data */
212 if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len)
219 /* ---------------------------------------
221 static void block_sig(DSI *dsi)
226 /* ---------------------------------------
228 static void unblock_sig(DSI *dsi)
233 /*********************************************************************************
235 *********************************************************************************/
238 * Communication error with the client, enter disconnected state
240 * 1. close the socket
241 * 2. set the DSI_DISCONNECTED flag, remove possible sleep flags
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)
247 int dsi_disconnect(DSI *dsi)
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;
258 /* ------------------------------
259 * write raw data. return actual bytes read. checks against EINTR
260 * aren't necessary if all of the signals have SA_RESTART
262 ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode)
266 unsigned int flags = 0;
271 LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): START", length);
273 if (dsi->flags & DSI_DISCONNECTED)
276 while (written < length) {
277 len = send(dsi->socket, (uint8_t *) data + written, length - written, flags);
286 if (errno == EAGAIN || errno == EWOULDBLOCK) {
287 LOG(log_debug, logtype_dsi, "dsi_stream_write: send: %s", strerror(errno));
289 if (mode == DSI_NOWAIT && written == 0) {
290 /* DSI_NOWAIT is used by attention give up in this case. */
295 /* Try to read sth. in order to break up possible deadlock */
296 if (dsi_peek(dsi) != 0) {
300 /* Now try writing again */
304 LOG(log_error, logtype_dsi, "dsi_stream_write: %s", strerror(errno));
309 dsi->write_count += written;
310 LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): END", length);
318 /* ---------------------------------
321 ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t length)
328 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file(send %zd bytes): START", length);
330 if (dsi->flags & DSI_DISCONNECTED)
336 while (written < length) {
337 len = sys_sendfile(dsi->socket, fromfd, &pos, length - written);
342 if (errno == EINVAL || errno == ENOSYS) {
346 if (errno == EAGAIN || errno == EWOULDBLOCK) {
347 #if defined(SOLARIS) || defined(FREEBSD)
349 /* we actually have sent sth., adjust counters and keep trying */
356 /* can't go back to blocking mode, exit, the next read
357 will return with an error and afpd will die.
363 LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno));
367 /* afpd is going to exit */
375 dsi->write_count += written;
379 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sent: %zd", written);
388 * Essentially a loop around buf_read() to ensure "length" bytes are read
389 * from dsi->buffer and/or the socket.
391 * @returns length on success, some value smaller then length indicates an error
393 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
398 if (dsi->flags & DSI_DISCONNECTED)
401 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length);
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");
409 } else if (len > 0) {
411 } else { /* eof or error */
412 /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
414 if (errno == ECONNRESET)
415 dsi->flags |= DSI_GOT_ECONNRESET;
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");
428 dsi->read_count += stored;
430 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes): got: %u", length, stored);
434 /* ---------------------------------------
435 * write data. 0 on failure. this assumes that dsi_len will never
436 * cause an overflow in the data buffer.
438 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
440 char block[DSI_BLOCKSIZ];
445 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): START", length);
447 if (dsi->flags & DSI_DISCONNECTED)
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));
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 */
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;
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))
478 if ((size_t)len == towrite) /* wrote everything out */
480 else if (len < 0) { /* error */
481 if (errno == EAGAIN || errno == EWOULDBLOCK) {
482 if (!dsi_peek(dsi)) {
486 LOG(log_error, logtype_dsi, "dsi_stream_send: %s", strerror(errno));
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;
500 iov[1].iov_base = (char *) iov[1].iov_base + len;
501 iov[1].iov_len -= len;
505 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): END", length);
513 * Read DSI command and data
515 * @param dsi (rw) DSI handle
517 * @return DSI function on success, 0 on failure
519 int dsi_stream_receive(DSI *dsi)
521 char block[DSI_BLOCKSIZ];
523 LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: START");
525 if (dsi->flags & DSI_DISCONNECTED)
528 /* read in the header */
529 if (dsi_buffered_stream_read(dsi, (uint8_t *)block, sizeof(block)) != sizeof(block))
532 dsi->header.dsi_flags = block[0];
533 dsi->header.dsi_command = block[1];
535 if (dsi->header.dsi_command == 0)
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);
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)
549 LOG(log_debug, logtype_dsi, "dsi_stream_receive: DSI cmdlen: %zd", dsi->cmdlen);