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;
67 if (dsi->eof < dsi->end) {
68 /* space in read buffer */
69 FD_SET( dsi->socket, &readfds);
73 LOG(log_note, logtype_dsi, "dsi_peek: readahead buffer is full, possibly increase -dsireadbuf option");
74 LOG(log_note, logtype_dsi, "dsi_peek: dsireadbuf: %d, DSI quantum: %d, effective buffer size: %d",
76 dsi->server_quantum ? dsi->server_quantum : DSI_SERVQUANT_DEF,
77 dsi->end - dsi->buffer);
81 FD_SET( dsi->socket, &writefds);
83 /* No timeout: if there's nothing to read nor nothing to write,
84 * we've got nothing to do at all */
85 if ((ret = select( maxfd, &readfds, &writefds, NULL, NULL)) <= 0) {
86 if (ret == -1 && errno == EINTR)
87 /* we might have been interrupted by out timer, so restart select */
90 LOG(log_error, logtype_dsi, "dsi_peek: unexpected select return: %d %s",
91 ret, ret < 0 ? strerror(errno) : "");
95 if (FD_ISSET(dsi->socket, &writefds)) {
96 /* we can write again */
97 LOG(log_debug, logtype_dsi, "dsi_peek: can write again");
101 /* Check if there's sth to read, hopefully reading that will unblock the client */
102 if (FD_ISSET(dsi->socket, &readfds)) {
103 len = dsi->end - dsi->eof; /* it's ensured above that there's space */
105 if ((len = read(dsi->socket, dsi->eof, len)) <= 0) {
107 LOG(log_error, logtype_dsi, "dsi_peek: EOF");
110 LOG(log_error, logtype_dsi, "dsi_peek: read: %s", strerror(errno));
115 LOG(log_debug, logtype_dsi, "dsi_peek: read %d bytes", len);
125 * Return all bytes up to count from dsi->buffer if there are any buffered there
127 static size_t from_buf(DSI *dsi, uint8_t *buf, size_t count)
131 if (dsi->buffer == NULL)
132 /* afpd master has no DSI buffering */
135 LOG(log_maxdebug, logtype_dsi, "from_buf: %u bytes", count);
137 nbe = dsi->eof - dsi->start;
140 nbe = min((size_t)nbe, count);
141 memcpy(buf, dsi->start, nbe);
144 if (dsi->eof == dsi->start)
145 dsi->start = dsi->eof = dsi->buffer;
148 LOG(log_debug, logtype_dsi, "from_buf(read: %u, unread:%u , space left: %u): returning %u",
149 dsi->start - dsi->buffer, dsi->eof - dsi->start, dsi->end - dsi->eof, nbe);
155 * Get bytes from buffer dsi->buffer or read from socket
157 * 1. Check if there are bytes in the the dsi->buffer buffer.
158 * 2. Return bytes from (1) if yes.
159 * Note: this may return fewer bytes then requested in count !!
160 * 3. If the buffer was empty, read from the socket.
162 static ssize_t buf_read(DSI *dsi, uint8_t *buf, size_t count)
166 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes)", count);
171 len = from_buf(dsi, buf, count); /* 1. */
175 len = readt(dsi->socket, buf, count, 0, 1); /* 3. */
177 LOG(log_maxdebug, logtype_dsi, "buf_read(%u bytes): got: %d", count, len);
183 * Get "length" bytes from buffer and/or socket. In order to avoid frequent small reads
184 * this tries to read larger chunks (8192 bytes) into a buffer.
186 static size_t dsi_buffered_stream_read(DSI *dsi, uint8_t *data, const size_t length)
191 LOG(log_maxdebug, logtype_dsi, "dsi_buffered_stream_read: %u bytes", length);
193 len = from_buf(dsi, data, length); /* read from buffer dsi->buffer */
194 dsi->read_count += len;
195 if (len == length) { /* got enough bytes from there ? */
196 return len; /* yes */
199 /* fill the buffer with 8192 bytes or until buffer is full */
200 buflen = min(8192, dsi->end - dsi->eof);
203 ret = read(dsi->socket, dsi->eof, buflen);
208 /* now get the remaining data */
209 if ((buflen = dsi_stream_read(dsi, data + len, length - len)) != length - len)
216 /* ---------------------------------------
218 static void block_sig(DSI *dsi)
223 /* ---------------------------------------
225 static void unblock_sig(DSI *dsi)
230 /*********************************************************************************
232 *********************************************************************************/
235 * Communication error with the client, enter disconnected state
237 * 1. close the socket
238 * 2. set the DSI_DISCONNECTED flag
240 * @returns 0 if successfully entered disconnected state
241 * -1 if ppid is 1 which means afpd master died
242 * or euid == 0 ie where still running as root (unauthenticated session)
244 int dsi_disconnect(DSI *dsi)
246 dsi->proto_close(dsi); /* 1 */
247 dsi->flags |= DSI_DISCONNECTED; /* 2 */
253 /* ------------------------------
254 * write raw data. return actual bytes read. checks against EINTR
255 * aren't necessary if all of the signals have SA_RESTART
257 ssize_t dsi_stream_write(DSI *dsi, void *data, const size_t length, int mode)
261 unsigned int flags = 0;
266 LOG(log_maxdebug, logtype_dsi, "dsi_stream_write: sending %u bytes", length);
268 if (dsi->flags & DSI_DISCONNECTED)
271 while (written < length) {
272 len = send(dsi->socket, (uint8_t *) data + written, length - written, flags);
281 if (errno == EAGAIN || errno == EWOULDBLOCK) {
282 LOG(log_debug, logtype_dsi, "dsi_stream_write: send: %s", strerror(errno));
284 if (mode == DSI_NOWAIT && written == 0) {
285 /* DSI_NOWAIT is used by attention give up in this case. */
290 /* Try to read sth. in order to break up possible deadlock */
291 if (dsi_peek(dsi) != 0) {
295 /* Now try writing again */
299 LOG(log_error, logtype_dsi, "dsi_stream_write: %s", strerror(errno));
304 dsi->write_count += written;
312 /* ---------------------------------
315 ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t length)
320 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sending %u bytes", length);
322 if (dsi->flags & DSI_DISCONNECTED)
328 while (written < length) {
329 len = sys_sendfile(dsi->socket, fromfd, &offset, length - written);
334 if (errno == EINVAL || errno == ENOSYS)
337 if (errno == EAGAIN || errno == EWOULDBLOCK) {
339 /* can't go back to blocking mode, exit, the next read
340 will return with an error and afpd will die.
346 LOG(log_error, logtype_dsi, "dsi_stream_read_file: %s", strerror(errno));
350 /* afpd is going to exit */
352 return -1; /* I think we're at EOF here... */
358 dsi->write_count += written;
366 * Essentially a loop around buf_read() to ensure "length" bytes are read
367 * from dsi->buffer and/or the socket.
369 * @returns length on success, some value smaller then length indicates an error
371 size_t dsi_stream_read(DSI *dsi, void *data, const size_t length)
376 if (dsi->flags & DSI_DISCONNECTED)
379 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes)", length);
382 while (stored < length) {
383 len = buf_read(dsi, (uint8_t *) data + stored, length - stored);
384 if (len == -1 && (errno == EINTR || errno == EAGAIN)) {
385 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read: select read loop");
387 } else if (len > 0) {
389 } else { /* eof or error */
390 /* don't log EOF error if it's just after connect (OSX 10.3 probe) */
391 if (errno == ECONNRESET)
392 dsi->flags |= DSI_GOT_ECONNRESET;
393 if (len || stored || dsi->read_count) {
394 if (! (dsi->flags & DSI_DISCONNECTED)) {
395 LOG(log_error, logtype_dsi, "dsi_stream_read: len:%d, %s",
396 len, (len < 0) ? strerror(errno) : "unexpected EOF");
404 dsi->read_count += stored;
406 LOG(log_maxdebug, logtype_dsi, "dsi_stream_read(%u bytes): got: %u", length, stored);
410 /* ---------------------------------------
411 * write data. 0 on failure. this assumes that dsi_len will never
412 * cause an overflow in the data buffer.
414 int dsi_stream_send(DSI *dsi, void *buf, size_t length)
416 char block[DSI_BLOCKSIZ];
421 LOG(log_maxdebug, logtype_dsi, "dsi_stream_send: %u bytes",
422 length ? length : sizeof(block));
424 if (dsi->flags & DSI_DISCONNECTED)
427 block[0] = dsi->header.dsi_flags;
428 block[1] = dsi->header.dsi_command;
429 memcpy(block + 2, &dsi->header.dsi_requestID,
430 sizeof(dsi->header.dsi_requestID));
431 memcpy(block + 4, &dsi->header.dsi_code, sizeof(dsi->header.dsi_code));
432 memcpy(block + 8, &dsi->header.dsi_len, sizeof(dsi->header.dsi_len));
433 memcpy(block + 12, &dsi->header.dsi_reserved,
434 sizeof(dsi->header.dsi_reserved));
436 if (!length) { /* just write the header */
437 length = (dsi_stream_write(dsi, block, sizeof(block), 0) == sizeof(block));
438 return length; /* really 0 on failure, 1 on success */
443 iov[0].iov_base = block;
444 iov[0].iov_len = sizeof(block);
445 iov[1].iov_base = buf;
446 iov[1].iov_len = length;
448 towrite = sizeof(block) + length;
449 dsi->write_count += towrite;
450 while (towrite > 0) {
451 if (((len = writev(dsi->socket, iov, 2)) == -1 && errno == EINTR) || (len == 0))
454 if ((size_t)len == towrite) /* wrote everything out */
456 else if (len < 0) { /* error */
457 if (errno == EAGAIN || errno == EWOULDBLOCK) {
458 if (!dsi_peek(dsi)) {
462 LOG(log_error, logtype_dsi, "dsi_stream_send: %s", strerror(errno));
468 if (towrite > length) { /* skip part of header */
469 iov[0].iov_base = (char *) iov[0].iov_base + len;
470 iov[0].iov_len -= len;
471 } else { /* skip to data */
472 if (iov[0].iov_len) {
473 len -= iov[0].iov_len;
476 iov[1].iov_base = (char *) iov[1].iov_base + len;
477 iov[1].iov_len -= len;
486 /* ---------------------------------------
487 * read data. function on success. 0 on failure. data length gets
488 * stored in length variable. this should really use size_t's, but
489 * that would require changes elsewhere. */
490 int dsi_stream_receive(DSI *dsi, void *buf, const size_t ilength,
493 char block[DSI_BLOCKSIZ];
495 LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: %u bytes", ilength);
497 if (dsi->flags & DSI_DISCONNECTED)
500 /* read in the header */
501 if (dsi_buffered_stream_read(dsi, (uint8_t *)block, sizeof(block)) != sizeof(block))
504 dsi->header.dsi_flags = block[0];
505 dsi->header.dsi_command = block[1];
506 /* FIXME, not the right place,
507 but we get a server disconnect without reason in the log
510 LOG(log_error, logtype_dsi, "dsi_stream_receive: invalid packet, fatal");
514 memcpy(&dsi->header.dsi_requestID, block + 2,
515 sizeof(dsi->header.dsi_requestID));
516 memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
517 memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
518 memcpy(&dsi->header.dsi_reserved, block + 12,
519 sizeof(dsi->header.dsi_reserved));
520 dsi->clientID = ntohs(dsi->header.dsi_requestID);
522 /* make sure we don't over-write our buffers. */
523 *rlength = min(ntohl(dsi->header.dsi_len), ilength);
524 if (dsi_stream_read(dsi, buf, *rlength) != *rlength)