1 /* Code for the buffer data structure. */
5 #include "pagealign_alloc.h"
7 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
12 # include <sys/socket.h>
15 /* OS/2 doesn't have EIO. FIXME: this whole notion of turning
16 a different error into EIO strikes me as pretty dubious. */
21 /* Local functions. */
22 static void buf_default_memory_error (struct buffer *);
23 static struct buffer_data *get_buffer_data (void);
27 /* Initialize a buffer structure. */
29 buf_initialize (int (*input) (void *, char *, size_t, size_t, size_t *),
30 int (*output) (void *, const char *, size_t, size_t *),
31 int (*flush) (void *),
32 int (*block) (void *, bool),
33 int (*get_fd) (void *),
34 int (*shutdown) (struct buffer *),
35 void (*memory) (struct buffer *),
40 buf = xmalloc (sizeof (struct buffer));
43 buf->nonblocking = false;
49 buf->shutdown = shutdown;
50 buf->memory_error = memory ? memory : buf_default_memory_error;
51 buf->closure = closure;
57 /* Free a buffer structure. */
59 buf_free (struct buffer *buf)
61 if (buf->closure != NULL)
72 /* Initialize a buffer structure which is not to be used for I/O. */
74 buf_nonio_initialize( void (*memory) (struct buffer *) )
76 return buf_initialize (NULL, NULL, NULL, NULL, NULL, NULL, memory, NULL);
81 /* Default memory error handler. */
83 buf_default_memory_error (struct buffer *buf)
85 error (1, 0, "out of memory");
90 /* Allocate more buffer_data structures. */
91 /* Get a new buffer_data structure. */
92 static struct buffer_data *
93 get_buffer_data (void)
95 struct buffer_data *ret;
97 ret = xmalloc (sizeof (struct buffer_data));
98 ret->text = pagealign_xalloc (BUFFER_DATA_SIZE);
105 /* See whether a buffer and its file descriptor is empty. */
110 /* Try and read any data on the file descriptor first.
111 * We already know the descriptor is non-blocking.
113 buf_input_data (buf, NULL);
114 return buf_empty_p (buf);
119 /* See whether a buffer is empty. */
121 buf_empty_p (struct buffer *buf)
123 struct buffer_data *data;
125 for (data = buf->data; data != NULL; data = data->next)
133 # if defined (SERVER_FLOWCONTROL) || defined (PROXY_SUPPORT)
135 * Count how much data is stored in the buffer..
136 * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
139 buf_count_mem (struct buffer *buf)
141 struct buffer_data *data;
144 for (data = buf->data; data != NULL; data = data->next)
145 mem += BUFFER_DATA_SIZE;
149 # endif /* SERVER_FLOWCONTROL || PROXY_SUPPORT */
153 /* Add data DATA of length LEN to BUF. */
155 buf_output (struct buffer *buf, const char *data, size_t len)
157 if (buf->data != NULL
158 && (((buf->last->text + BUFFER_DATA_SIZE)
159 - (buf->last->bufp + buf->last->size))
162 memcpy (buf->last->bufp + buf->last->size, data, len);
163 buf->last->size += len;
169 struct buffer_data *newdata;
171 newdata = get_buffer_data ();
174 (*buf->memory_error) (buf);
178 if (buf->data == NULL)
181 buf->last->next = newdata;
182 newdata->next = NULL;
185 newdata->bufp = newdata->text;
187 if (len <= BUFFER_DATA_SIZE)
190 memcpy (newdata->text, data, len);
194 newdata->size = BUFFER_DATA_SIZE;
195 memcpy (newdata->text, data, BUFFER_DATA_SIZE);
197 data += BUFFER_DATA_SIZE;
198 len -= BUFFER_DATA_SIZE;
206 /* Add a '\0' terminated string to BUF. */
208 buf_output0 (struct buffer *buf, const char *string)
210 buf_output (buf, string, strlen (string));
215 /* Add a single character to BUF. */
217 buf_append_char (struct buffer *buf, int ch)
219 if (buf->data != NULL
220 && (buf->last->text + BUFFER_DATA_SIZE
221 != buf->last->bufp + buf->last->size))
223 *(buf->last->bufp + buf->last->size) = ch;
231 buf_output (buf, &b, 1);
237 /* Free struct buffer_data's from the list starting with FIRST and ending at
241 buf_free_datas (struct buffer_data *first, struct buffer_data *last)
243 struct buffer_data *b, *n, *p;
249 pagealign_free (b->text);
258 * Send all the output we've been saving up. Returns 0 for success or
259 * errno code. If the buffer has been set to be nonblocking, this
260 * will just write until the write would block.
263 buf_send_output (struct buffer *buf)
265 assert (buf->output != NULL);
267 while (buf->data != NULL)
269 struct buffer_data *data;
278 status = (*buf->output) (buf->closure, data->bufp, data->size,
282 /* Some sort of error. Discard the data, and return. */
287 if (nbytes != data->size)
289 /* Not all the data was written out. This is only
290 permitted in nonblocking mode. Adjust the buffer,
293 assert (buf->nonblocking);
295 data->size -= nbytes;
296 data->bufp += nbytes;
302 buf->data = data->next;
303 buf_free_datas (data, data);
314 * Flush any data queued up in the buffer. If BLOCK is nonzero, then
315 * if the buffer is in nonblocking mode, put it into blocking mode for
316 * the duration of the flush. This returns 0 on success, or an error
320 buf_flush (struct buffer *buf, bool block)
325 assert (buf->flush != NULL);
327 nonblocking = buf->nonblocking;
328 if (nonblocking && block)
330 status = set_block (buf);
335 status = buf_send_output (buf);
337 status = (*buf->flush) (buf->closure);
339 if (nonblocking && block)
343 blockstat = set_nonblock (buf);
354 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno
358 set_nonblock (struct buffer *buf)
362 if (buf->nonblocking)
364 assert (buf->block != NULL);
365 status = (*buf->block) (buf->closure, 0);
368 buf->nonblocking = true;
375 * Set buffer BUF to blocking I/O. Returns 0 for success or errno
379 set_block (struct buffer *buf)
383 if (! buf->nonblocking)
385 assert (buf->block != NULL);
386 status = (*buf->block) (buf->closure, 1);
389 buf->nonblocking = false;
396 * Send a character count and some output. Returns errno code or 0 for
399 * Sending the count in binary is OK since this is only used on a pipe
400 * within the same system.
403 buf_send_counted (struct buffer *buf)
406 struct buffer_data *data;
409 for (data = buf->data; data != NULL; data = data->next)
412 data = get_buffer_data ();
415 (*buf->memory_error) (buf);
419 data->next = buf->data;
421 if (buf->last == NULL)
424 data->bufp = data->text;
425 data->size = sizeof (int);
427 *((int *) data->text) = size;
429 return buf_send_output (buf);
435 * Send a special count. COUNT should be negative. It will be
436 * handled specially by buf_copy_counted. This function returns 0 or
439 * Sending the count in binary is OK since this is only used on a pipe
440 * within the same system.
443 buf_send_special_count (struct buffer *buf, int count)
445 struct buffer_data *data;
447 data = get_buffer_data ();
450 (*buf->memory_error) (buf);
454 data->next = buf->data;
456 if (buf->last == NULL)
459 data->bufp = data->text;
460 data->size = sizeof (int);
462 *((int *) data->text) = count;
464 return buf_send_output (buf);
469 /* Append a list of buffer_data structures to an buffer. */
471 buf_append_data (struct buffer *buf, struct buffer_data *data,
472 struct buffer_data *last)
476 if (buf->data == NULL)
479 buf->last->next = data;
486 # ifdef PROXY_SUPPORT
487 /* Copy data structures and append them to a buffer.
490 * Failure to allocate memory here is fatal.
493 buf_copy_data (struct buffer *buf, struct buffer_data *data,
494 struct buffer_data *last)
496 struct buffer_data *first, *new, *cur, *prev;
505 new = get_buffer_data ();
506 if (!new) error (1, errno, "Failed to allocate buffer data.");
508 if (!first) first = new;
509 memcpy (new->text, cur->bufp, cur->size);
510 new->bufp = new->text;
511 new->size = cur->size;
513 if (prev) prev->next = new;
514 if (cur == last) break;
519 buf_append_data (buf, first, new);
521 # endif /* PROXY_SUPPORT */
525 /* Dispose of any remaining data in the buffer. */
527 buf_free_data (struct buffer *buffer)
529 if (buf_empty_p (buffer)) return;
530 buf_free_datas (buffer->data, buffer->last);
531 buffer->data = buffer->last = NULL;
536 /* Append the data in one buffer to another. This removes the data
537 * from the source buffer.
540 buf_append_buffer (struct buffer *to, struct buffer *from)
542 struct buffer_data *n;
544 /* Copy the data pointer to the new buf. */
545 buf_append_data (to, from->data, from->last);
550 if (n == from->last) break;
554 /* Remove from the original location. */
562 * Copy the contents of file F into buffer_data structures. We can't
563 * copy directly into an buffer, because we want to handle failure and
564 * success differently. Returns 0 on success, or -2 if out of
565 * memory, or a status code on error. Since the caller happens to
566 * know the size of the file, it is passed in as SIZE. On success,
567 * this function sets *RETP and *LASTP, which may be passed to
571 buf_read_file (FILE *f, long int size, struct buffer_data **retp,
572 struct buffer_data **lastp)
581 struct buffer_data *data;
584 data = get_buffer_data ();
594 (*lastp)->next = data;
598 data->bufp = data->text;
601 if (size > BUFFER_DATA_SIZE)
602 get = BUFFER_DATA_SIZE;
607 if (fread (data->text, get, 1, f) != 1)
621 buf_free_datas (*retp, (*lastp)->next);
628 * Copy the contents of file F into buffer_data structures. We can't
629 * copy directly into an buffer, because we want to handle failure and
630 * success differently. Returns 0 on success, or -2 if out of
631 * memory, or a status code on error. On success, this function sets
632 * *RETP and *LASTP, which may be passed to buf_append_data.
635 buf_read_file_to_eof (FILE *f, struct buffer_data **retp,
636 struct buffer_data **lastp)
645 struct buffer_data *data;
648 data = get_buffer_data ();
658 (*lastp)->next = data;
662 data->bufp = data->text;
665 get = BUFFER_DATA_SIZE;
668 nread = fread (data->text, 1, get, f);
669 if (nread == 0 && !feof (f))
682 buf_free_datas (*retp, (*lastp)->next);
688 /* Return the number of bytes in a chain of buffer_data structures. */
690 buf_chain_length (struct buffer_data *buf)
703 /* Return the number of bytes in a buffer. */
705 buf_length (struct buffer *buf)
707 return buf_chain_length (buf->data);
713 * Read an arbitrary amount of data into an input buffer. The buffer
714 * will be in nonblocking mode, and we just grab what we can. Return
715 * 0 on success, or -1 on end of file, or -2 if out of memory, or an
716 * error code. If COUNTP is not NULL, *COUNTP is set to the number of
720 buf_input_data (struct buffer *buf, size_t *countp)
722 assert (buf->input != NULL);
732 if (buf->data == NULL
733 || (buf->last->bufp + buf->last->size
734 == buf->last->text + BUFFER_DATA_SIZE))
736 struct buffer_data *data;
738 data = get_buffer_data ();
741 (*buf->memory_error) (buf);
745 if (buf->data == NULL)
748 buf->last->next = data;
752 data->bufp = data->text;
756 get = ((buf->last->text + BUFFER_DATA_SIZE)
757 - (buf->last->bufp + buf->last->size));
759 status = (*buf->input) (buf->closure,
760 buf->last->bufp + buf->last->size,
765 buf->last->size += nbytes;
771 /* If we did not fill the buffer, then presumably we read
772 all the available data. */
783 * Read a line (characters up to a \012) from an input buffer. (We
784 * use \012 rather than \n for the benefit of non Unix clients for
785 * which \n means something else). This returns 0 on success, or -1
786 * on end of file, or -2 if out of memory, or an error code. If it
787 * succeeds, it sets *LINE to an allocated buffer holding the contents
788 * of the line. The trailing \012 is not included in the buffer. If
789 * LENP is not NULL, then *LENP is set to the number of bytes read;
790 * strlen may not work, because there may be embedded null bytes.
793 buf_read_line (struct buffer *buf, char **line, size_t *lenp)
795 return buf_read_short_line (buf, line, lenp, SIZE_MAX);
800 /* Like buf_read_line, but return -2 if no newline is found in MAX characters.
803 buf_read_short_line (struct buffer *buf, char **line, size_t *lenp,
806 assert (buf->input != NULL);
812 size_t len, finallen, predicted_len;
813 struct buffer_data *data;
816 /* See if there is a newline in BUF. */
818 for (data = buf->data; data != NULL; data = data->next)
820 nl = memchr (data->bufp, '\012', data->size);
823 finallen = nl - data->bufp;
824 if (xsum (len, finallen) >= max) return -2;
828 else if (xsum (len, data->size) >= max) return -2;
832 /* If we found a newline, copy the line into a memory buffer,
833 and remove it from BUF. */
837 struct buffer_data *nldata;
839 p = xmalloc (len + 1);
846 while (data != nldata)
848 struct buffer_data *next;
850 memcpy (p, data->bufp, data->size);
853 buf_free_datas (data, data);
857 memcpy (p, data->bufp, finallen);
860 data->size -= finallen + 1;
870 /* Read more data until we get a newline or MAX characters. */
878 if (buf->data == NULL
879 || (buf->last->bufp + buf->last->size
880 == buf->last->text + BUFFER_DATA_SIZE))
882 data = get_buffer_data ();
885 (*buf->memory_error) (buf);
889 if (buf->data == NULL)
892 buf->last->next = data;
896 data->bufp = data->text;
900 mem = buf->last->bufp + buf->last->size;
901 size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
903 /* We need to read at least 1 byte. We can handle up to
904 SIZE bytes. This will only be efficient if the
905 underlying communication stream does its own buffering,
906 or is clever about getting more than 1 byte at a time. */
907 status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
911 predicted_len += nbytes;
912 buf->last->size += nbytes;
914 /* Optimize slightly to avoid an unnecessary call to memchr. */
922 if (memchr (mem, '\012', nbytes) != NULL)
925 if (xsum (len, predicted_len) >= max) return -2;
933 * Extract data from the input buffer BUF. This will read up to WANT
934 * bytes from the buffer. It will set *RETDATA to point at the bytes,
935 * and set *GOT to the number of bytes to be found there. Any buffer
936 * call which uses BUF may change the contents of the buffer at *DATA,
937 * so the data should be fully processed before any further calls are
938 * made. This returns 0 on success, or -1 on end of file, or -2 if
939 * out of memory, or an error code.
942 buf_read_data (struct buffer *buf, size_t want, char **retdata, size_t *got)
944 assert (buf->input != NULL);
946 while (buf->data != NULL && buf->data->size == 0)
948 struct buffer_data *next;
950 next = buf->data->next;
951 buf_free_datas (buf->data, buf->data);
957 if (buf->data == NULL)
959 struct buffer_data *data;
963 data = get_buffer_data ();
966 (*buf->memory_error) (buf);
973 data->bufp = data->text;
976 if (want < BUFFER_DATA_SIZE)
979 get = BUFFER_DATA_SIZE;
980 status = (*buf->input) (buf->closure, data->bufp, get,
981 BUFFER_DATA_SIZE, &nbytes);
988 *retdata = buf->data->bufp;
989 if (want < buf->data->size)
992 buf->data->size -= want;
993 buf->data->bufp += want;
997 *got = buf->data->size;
1007 * Copy lines from an input buffer to an output buffer.
1008 * This copies all complete lines (characters up to a
1009 * newline) from INBUF to OUTBUF. Each line in OUTBUF is preceded by the
1010 * character COMMAND and a space.
1013 buf_copy_lines (struct buffer *outbuf, struct buffer *inbuf, int command)
1017 struct buffer_data *data;
1018 struct buffer_data *nldata;
1022 /* See if there is a newline in INBUF. */
1025 for (data = inbuf->data; data != NULL; data = data->next)
1027 nl = memchr (data->bufp, '\n', data->size);
1037 /* There are no more lines in INBUF. */
1041 /* Put in the command. */
1042 buf_append_char (outbuf, command);
1043 buf_append_char (outbuf, ' ');
1045 if (inbuf->data != nldata)
1048 * Simply move over all the buffers up to the one containing
1051 for (data = inbuf->data; data->next != nldata; data = data->next);
1053 buf_append_data (outbuf, inbuf->data, data);
1054 inbuf->data = nldata;
1058 * If the newline is at the very end of the buffer, just move
1059 * the buffer onto OUTBUF. Otherwise we must copy the data.
1061 len = nl + 1 - nldata->bufp;
1062 if (len == nldata->size)
1064 inbuf->data = nldata->next;
1065 if (inbuf->data == NULL)
1068 nldata->next = NULL;
1069 buf_append_data (outbuf, nldata, nldata);
1073 buf_output (outbuf, nldata->bufp, len);
1074 nldata->bufp += len;
1075 nldata->size -= len;
1083 * Copy counted data from one buffer to another. The count is an
1084 * integer, host size, host byte order (it is only used across a
1085 * pipe). If there is enough data, it should be moved over. If there
1086 * is not enough data, it should remain on the original buffer. A
1087 * negative count is a special case. if one is seen, *SPECIAL is set
1088 * to the (negative) count value and no additional data is gathered
1089 * from the buffer; normally *SPECIAL is set to 0. This function
1090 * returns the number of bytes it needs to see in order to actually
1091 * copy something over.
1094 buf_copy_counted (struct buffer *outbuf, struct buffer *inbuf, int *special)
1100 struct buffer_data *data;
1104 char intbuf[sizeof (int)];
1109 struct buffer_data *start;
1111 struct buffer_data *stop;
1114 /* See if we have enough bytes to figure out the count. */
1115 need = sizeof (int);
1117 for (data = inbuf->data; data != NULL; data = data->next)
1119 if (data->size >= need)
1121 memcpy (intp, data->bufp, need);
1124 memcpy (intp, data->bufp, data->size);
1130 /* We don't have enough bytes to form an integer. */
1140 /* A negative COUNT is a special case meaning that we
1141 don't need any further information. */
1148 * We have an integer in COUNT. We have gotten all the
1149 * data from INBUF in all buffers before START, and we
1150 * have gotten STARTOFF bytes from START. See if we have
1151 * enough bytes remaining in INBUF.
1153 need = count - (start->size - startoff);
1161 for (data = start->next; data != NULL; data = data->next)
1163 if (need <= data->size)
1169 /* We don't have enough bytes. */
1178 * We have enough bytes. Free any buffers in INBUF before
1179 * START, and remove STARTOFF bytes from START, so that we can
1180 * forget about STARTOFF.
1182 start->bufp += startoff;
1183 start->size -= startoff;
1185 if (start->size == 0)
1186 start = start->next;
1188 if (stop->size == stopwant)
1194 while (inbuf->data != start)
1197 inbuf->data = data->next;
1198 buf_free_datas (data, data);
1201 /* If COUNT is negative, set *SPECIAL and get out now. */
1209 * We want to copy over the bytes from START through STOP. We
1210 * only want STOPWANT bytes from STOP.
1215 /* Attach the buffers from START through STOP to OUTBUF. */
1216 for (data = start; data->next != stop; data = data->next);
1219 buf_append_data (outbuf, start, data);
1224 buf_output (outbuf, stop->bufp, stopwant);
1225 stop->bufp += stopwant;
1226 stop->size -= stopwant;
1236 buf_get_fd (struct buffer *buf)
1239 return (*buf->get_fd) (buf->closure);
1245 /* Shut down a buffer. This returns 0 on success, or an errno code. */
1247 buf_shutdown (struct buffer *buf)
1249 if (buf->shutdown) return (*buf->shutdown) (buf);
1255 /* Certain types of communication input and output data in packets,
1256 where each packet is translated in some fashion. The packetizing
1257 buffer type supports that, given a buffer which handles lower level
1258 I/O and a routine to translate the data in a packet.
1260 This code uses two bytes for the size of a packet, so packets are
1261 restricted to 65536 bytes in total.
1263 The translation functions should just translate; they may not
1264 significantly increase or decrease the amount of data. The actual
1265 size of the initial data is part of the translated data. The
1266 output translation routine may add up to PACKET_SLOP additional
1267 bytes, and the input translation routine should shrink the data
1270 # define PACKET_SLOP (100)
1272 /* This structure is the closure field of a packetizing buffer. */
1274 struct packetizing_buffer
1276 /* The underlying buffer. */
1278 /* The input translation function. Exactly one of inpfn and outfn
1279 will be NULL. The input translation function should
1280 untranslate the data in INPUT, storing the result in OUTPUT.
1281 SIZE is the amount of data in INPUT, and is also the size of
1282 OUTPUT. This should return 0 on success, or an errno code. */
1283 int (*inpfn) (void *fnclosure, const char *input, char *output,
1285 /* The output translation function. This should translate the
1286 data in INPUT, storing the result in OUTPUT. The first two
1287 bytes in INPUT will be the size of the data, and so will SIZE.
1288 This should set *TRANSLATED to the amount of translated data in
1289 OUTPUT. OUTPUT is large enough to hold SIZE + PACKET_SLOP
1290 bytes. This should return 0 on success, or an errno code. */
1291 int (*outfn) (void *fnclosure, const char *input, char *output,
1292 size_t size, size_t *translated);
1293 /* A closure for the translation function. */
1295 /* For an input buffer, we may have to buffer up data here. */
1296 /* This is non-zero if the buffered data has been translated.
1297 Otherwise, the buffered data has not been translated, and starts
1298 with the two byte packet size. */
1300 /* The amount of buffered data. */
1302 /* The buffer allocated to hold the data. */
1304 /* The size of holdbuf. */
1306 /* If translated is set, we need another data pointer to track
1307 where we are in holdbuf. If translated is clear, then this
1308 pointer is not used. */
1314 static int packetizing_buffer_input (void *, char *, size_t, size_t, size_t *);
1315 static int packetizing_buffer_output (void *, const char *, size_t, size_t *);
1316 static int packetizing_buffer_flush (void *);
1317 static int packetizing_buffer_block (void *, bool);
1318 static int packetizing_buffer_get_fd (void *);
1319 static int packetizing_buffer_shutdown (struct buffer *);
1323 /* Create a packetizing buffer. */
1325 packetizing_buffer_initialize (struct buffer *buf,
1326 int (*inpfn) (void *, const char *, char *,
1328 int (*outfn) (void *, const char *, char *,
1331 void (*memory) (struct buffer *))
1333 struct packetizing_buffer *pb;
1335 pb = xmalloc (sizeof *pb);
1336 memset (pb, 0, sizeof *pb);
1341 pb->fnclosure = fnclosure;
1345 /* Add PACKET_SLOP to handle larger translated packets, and
1346 add 2 for the count. This buffer is increased if
1348 pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
1349 pb->holdbuf = xmalloc (pb->holdbufsize);
1352 return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
1353 inpfn != NULL ? NULL : packetizing_buffer_output,
1354 inpfn != NULL ? NULL : packetizing_buffer_flush,
1355 packetizing_buffer_block,
1356 packetizing_buffer_get_fd,
1357 packetizing_buffer_shutdown,
1364 /* Input data from a packetizing buffer. */
1366 packetizing_buffer_input (void *closure, char *data, size_t need, size_t size,
1369 struct packetizing_buffer *pb = closure;
1373 if (pb->holdsize > 0 && pb->translated)
1377 copy = pb->holdsize;
1381 memcpy (data, pb->holddata, size);
1382 pb->holdsize -= size;
1383 pb->holddata += size;
1388 memcpy (data, pb->holddata, copy);
1390 pb->translated = false;
1398 while (need > 0 || *got == 0)
1401 size_t get, nread, count, tcount;
1403 static char *stackoutbuf = NULL;
1404 char *inbuf, *outbuf;
1407 stackoutbuf = xmalloc (BUFFER_DATA_SIZE + PACKET_SLOP);
1409 /* If we don't already have the two byte count, get it. */
1410 if (pb->holdsize < 2)
1412 get = 2 - pb->holdsize;
1413 status = buf_read_data (pb->buf, get, &bytes, &nread);
1416 /* buf_read_data can return -2, but a buffer input
1417 function is only supposed to return -1, 0, or an
1426 /* The buffer is in nonblocking mode, and we didn't
1427 manage to read anything. */
1432 pb->holdbuf[1] = bytes[0];
1435 pb->holdbuf[0] = bytes[0];
1438 /* We only got one byte, but we needed two. Stash
1439 the byte we got, and try again. */
1443 pb->holdbuf[1] = bytes[1];
1448 /* Read the packet. */
1450 count = (((pb->holdbuf[0] & 0xff) << 8)
1451 + (pb->holdbuf[1] & 0xff));
1453 if (count + 2 > pb->holdbufsize)
1457 /* We didn't allocate enough space in the initialize
1460 n = xrealloc (pb->holdbuf, count + 2);
1463 (*pb->buf->memory_error) (pb->buf);
1467 pb->holdbufsize = count + 2;
1470 get = count - (pb->holdsize - 2);
1472 status = buf_read_data (pb->buf, get, &bytes, &nread);
1475 /* buf_read_data can return -2, but a buffer input
1476 function is only supposed to return -1, 0, or an error
1485 /* We did not get any data. Presumably the buffer is in
1486 nonblocking mode. */
1492 /* We did not get all the data we need to fill the packet.
1493 buf_read_data does not promise to return all the bytes
1494 requested, so we must try again. */
1495 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1496 pb->holdsize += nread;
1500 /* We have a complete untranslated packet of COUNT bytes. */
1502 if (pb->holdsize == 2)
1504 /* We just read the entire packet (the 2 bytes in
1505 PB->HOLDBUF are the size). Save a memcpy by
1506 translating directly from BYTES. */
1511 /* We already had a partial packet in PB->HOLDBUF. We
1512 need to copy the new data over to make the input
1514 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1515 inbuf = pb->holdbuf + 2;
1518 if (count <= BUFFER_DATA_SIZE + PACKET_SLOP)
1519 outbuf = stackoutbuf;
1522 outbuf = xmalloc (count);
1525 (*pb->buf->memory_error) (pb->buf);
1530 status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1534 /* The first two bytes in the translated buffer are the real
1535 length of the translated data. */
1536 tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
1539 error (1, 0, "Input translation failure");
1543 /* We have more data than the caller has provided space
1544 for. We need to save some of it for the next call. */
1546 memcpy (data, outbuf + 2, size);
1549 pb->holdsize = tcount - size;
1550 memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
1551 pb->holddata = pb->holdbuf;
1552 pb->translated = true;
1554 if (outbuf != stackoutbuf)
1560 memcpy (data, outbuf + 2, tcount);
1562 if (outbuf != stackoutbuf)
1578 /* Output data to a packetizing buffer. */
1580 packetizing_buffer_output (void *closure, const char *data, size_t have,
1583 struct packetizing_buffer *pb = closure;
1584 static char *inbuf = NULL; /* These two buffers are static so that they
1585 * depend on the size of BUFFER_DATA_SIZE yet
1586 * still only be allocated once per run.
1588 static char *stack_outbuf = NULL;
1589 struct buffer_data *outdata = NULL; /* Initialize to silence -Wall. Dumb.
1592 size_t size, translated;
1595 /* It would be easy to xmalloc a buffer, but I don't think this
1596 case can ever arise. */
1597 assert (have <= BUFFER_DATA_SIZE);
1601 inbuf = xmalloc (BUFFER_DATA_SIZE + 2);
1602 stack_outbuf = xmalloc (BUFFER_DATA_SIZE + PACKET_SLOP + 4);
1605 inbuf[0] = (have >> 8) & 0xff;
1606 inbuf[1] = have & 0xff;
1607 memcpy (inbuf + 2, data, have);
1611 /* The output function is permitted to add up to PACKET_SLOP
1612 bytes, and we need 2 bytes for the size of the translated data.
1613 If we can guarantee that the result will fit in a buffer_data,
1614 we translate directly into one to avoid a memcpy in buf_output. */
1615 if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
1616 outbuf = stack_outbuf;
1619 outdata = get_buffer_data ();
1620 if (outdata == NULL)
1622 (*pb->buf->memory_error) (pb->buf);
1626 outdata->next = NULL;
1627 outdata->bufp = outdata->text;
1629 outbuf = outdata->text;
1632 status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1637 /* The output function is permitted to add up to PACKET_SLOP
1639 assert (translated <= size + PACKET_SLOP);
1641 outbuf[0] = (translated >> 8) & 0xff;
1642 outbuf[1] = translated & 0xff;
1644 if (outbuf == stack_outbuf)
1645 buf_output (pb->buf, outbuf, translated + 2);
1648 outdata->size = translated + 2;
1649 buf_append_data (pb->buf, outdata, outdata);
1654 /* We will only be here because buf_send_output was called on the
1655 packetizing buffer. That means that we should now call
1656 buf_send_output on the underlying buffer. */
1657 return buf_send_output (pb->buf);
1662 /* Flush data to a packetizing buffer. */
1664 packetizing_buffer_flush (void *closure)
1666 struct packetizing_buffer *pb = closure;
1668 /* Flush the underlying buffer. Note that if the original call to
1669 buf_flush passed 1 for the BLOCK argument, then the buffer will
1670 already have been set into blocking mode, so we should always
1672 return buf_flush (pb->buf, 0);
1677 /* The block routine for a packetizing buffer. */
1679 packetizing_buffer_block (void *closure, bool block)
1681 struct packetizing_buffer *pb = closure;
1684 return set_block (pb->buf);
1686 return set_nonblock (pb->buf);
1691 /* Return the file descriptor underlying any child buffers. */
1693 packetizing_buffer_get_fd (void *closure)
1695 struct packetizing_buffer *cb = closure;
1696 return buf_get_fd (cb->buf);
1701 /* Shut down a packetizing buffer. */
1703 packetizing_buffer_shutdown (struct buffer *buf)
1705 struct packetizing_buffer *pb = buf->closure;
1707 return buf_shutdown (pb->buf);
1712 /* All server communication goes through buffer structures. Most of
1713 the buffers are built on top of a file descriptor. This structure
1714 is used as the closure field in a buffer. */
1718 /* The file descriptor. */
1720 /* Nonzero if the file descriptor is in blocking mode. */
1722 /* The child process id when fd is a pipe. */
1724 /* The connection info, when fd is a pipe to a server. */
1728 static int fd_buffer_input (void *, char *, size_t, size_t, size_t *);
1729 static int fd_buffer_output (void *, const char *, size_t, size_t *);
1730 static int fd_buffer_flush (void *);
1731 static int fd_buffer_block (void *, bool);
1732 static int fd_buffer_get_fd (void *);
1733 static int fd_buffer_shutdown (struct buffer *);
1735 /* Initialize a buffer built on a file descriptor. FD is the file
1736 descriptor. INPUT is nonzero if this is for input, zero if this is
1737 for output. MEMORY is the function to call when a memory error
1741 fd_buffer_initialize (int fd, pid_t child_pid, cvsroot_t *root, bool input,
1742 void (*memory) (struct buffer *))
1744 struct fd_buffer *n;
1746 n = xmalloc (sizeof *n);
1748 n->child_pid = child_pid;
1750 fd_buffer_block (n, true);
1751 return buf_initialize (input ? fd_buffer_input : NULL,
1752 input ? NULL : fd_buffer_output,
1753 input ? NULL : fd_buffer_flush,
1754 fd_buffer_block, fd_buffer_get_fd,
1762 /* The buffer input function for a buffer built on a file descriptor.
1764 * In non-blocing mode, this function will read as many bytes as it can in a
1765 * single try, up to SIZE bytes, and return.
1767 * In blocking mode with NEED > 0, this function will read as many bytes as it
1768 * can but will not return until it has read at least NEED bytes.
1770 * In blocking mode with NEED == 0, this function will block until it can read
1771 * either at least one byte or EOF, then read as many bytes as are available
1772 * and return. At the very least, compress_buffer_shutdown depends on this
1773 * behavior to read EOF and can loop indefinitely without it.
1779 * closure Our FD_BUFFER struct.
1780 * data The start of our input buffer.
1781 * need How many bytes our caller needs.
1782 * size How many bytes are available in DATA.
1783 * got Where to store the number of bytes read.
1786 * data Filled with bytes read.
1787 * *got Number of bytes actually read into DATA.
1795 * This function can return an error if fd_buffer_block(), or the system
1796 * read() or select() calls do.
1799 fd_buffer_input (void *closure, char *data, size_t need, size_t size,
1802 struct fd_buffer *fb = closure;
1805 assert (need <= size);
1814 /* Set non-block. */
1815 status = fd_buffer_block (fb, false);
1816 if (status != 0) return status;
1819 FD_SET (fb->fd, &readfds);
1825 /* This used to select on exceptions too, but as far
1826 as I know there was never any reason to do that and
1827 SCO doesn't let you select on exceptions on pipes. */
1828 numfds = select (fb->fd + 1, &readfds, NULL, NULL, NULL);
1829 if (numfds < 0 && errno != EINTR)
1834 } while (numfds < 0);
1836 nbytes = read (fb->fd, data + *got, size - *got);
1840 /* End of file. This assumes that we are using POSIX or BSD
1841 style nonblocking I/O. On System V we will get a zero
1842 return if there is no data, even when not at EOF. */
1845 /* We already read some data, so return no error, counting
1846 * on the fact that we will read EOF again next time.
1861 /* Some error occurred. */
1862 if (!blocking_error (errno))
1867 /* else Everything's fine, we just didn't get any data. */
1871 } while (*got < need);
1874 if (status == 0 || status == -1)
1878 /* OK or EOF - Reset block. */
1879 newstatus = fd_buffer_block (fb, true);
1880 if (newstatus) status = newstatus;
1885 /* The above will always return. Handle non-blocking read. */
1886 nbytes = read (fb->fd, data, size);
1895 /* End of file. This assumes that we are using POSIX or BSD
1896 style nonblocking I/O. On System V we will get a zero
1897 return if there is no data, even when not at EOF. */
1900 /* Some error occurred. */
1901 if (blocking_error (errno))
1902 /* Everything's fine, we just didn't get any data. */
1910 /* The buffer output function for a buffer built on a file descriptor. */
1913 fd_buffer_output (void *closure, const char *data, size_t have, size_t *wrote)
1915 struct fd_buffer *fd = closure;
1923 nbytes = write (fd->fd, data, have);
1928 && (nbytes == 0 || blocking_error (errno)))
1930 /* A nonblocking write failed to write any data. Just
1935 /* Some sort of error occurred. */
1953 /* The buffer flush function for a buffer built on a file descriptor. */
1955 fd_buffer_flush (void *closure)
1957 /* We don't need to do anything here. Our fd doesn't have its own buffer
1958 * and syncing won't do anything but slow us down.
1960 * struct fd_buffer *fb = closure;
1962 * if (fsync (fb->fd) < 0 && errno != EROFS && errno != EINVAL)
1970 static struct stat devnull;
1971 static int devnull_set = -1;
1973 /* The buffer block function for a buffer built on a file descriptor. */
1975 fd_buffer_block (void *closure, bool block)
1977 struct fd_buffer *fb = closure;
1978 # if defined (F_GETFL) && defined (O_NONBLOCK) && defined (F_SETFL)
1981 flags = fcntl (fb->fd, F_GETFL, 0);
1986 flags &= ~O_NONBLOCK;
1988 flags |= O_NONBLOCK;
1990 if (fcntl (fb->fd, F_SETFL, flags) < 0)
1993 * BSD returns ENODEV when we try to set block/nonblock on /dev/null.
1994 * BSDI returns ENOTTY when we try to set block/nonblock on /dev/null.
1997 int save_errno = errno;
1998 bool isdevnull = false;
2000 if (devnull_set == -1)
2001 devnull_set = stat ("/dev/null", &devnull);
2003 if (devnull_set >= 0)
2004 /* Equivalent to /dev/null ? */
2005 isdevnull = (fstat (fb->fd, &sb) >= 0
2006 && sb.st_dev == devnull.st_dev
2007 && sb.st_ino == devnull.st_ino
2008 && sb.st_mode == devnull.st_mode
2009 && sb.st_uid == devnull.st_uid
2010 && sb.st_gid == devnull.st_gid
2011 && sb.st_size == devnull.st_size
2012 && sb.st_blocks == devnull.st_blocks
2013 && sb.st_blksize == devnull.st_blksize);
2022 # endif /* F_GETFL && O_NONBLOCK && F_SETFL */
2024 fb->blocking = block;
2032 fd_buffer_get_fd (void *closure)
2034 struct fd_buffer *fb = closure;
2040 /* The buffer shutdown function for a buffer built on a file descriptor.
2042 * This function disposes of memory allocated for this buffer.
2045 fd_buffer_shutdown (struct buffer *buf)
2047 struct fd_buffer *fb = buf->closure;
2049 bool closefd, statted;
2051 /* Must be an open pipe, socket, or file. What could go wrong? */
2052 if (fstat (fb->fd, &s) == -1) statted = false;
2053 else statted = true;
2054 /* Don't bother to try closing the FD if we couldn't stat it. This
2055 * probably won't work.
2057 * (buf_shutdown() on some of the server/child communication pipes is
2058 * getting EBADF on both the fstat and the close. I'm not sure why -
2059 * perhaps they were alredy closed somehow?
2063 /* Flush the buffer if possible. */
2072 /* There used to be a check here for unread data in the buffer of
2073 * the pipe, but it was deemed unnecessary and possibly dangerous. In
2074 * some sense it could be second-guessing the caller who requested it
2080 * This mess of #ifdefs is hard to read. There must be some relation between
2081 * the macros being checked which at least deserves comments - if
2082 * SHUTDOWN_SERVER, NO_SOCKET_TO_FD, & START_RSH_WITH_POPEN_RW were completely
2083 * independant, then the next few lines could easily refuse to compile.
2085 * The note below about START_RSH_WITH_POPEN_RW never being set when
2086 * SHUTDOWN_SERVER is defined means that this code would always break on
2087 * systems with SHUTDOWN_SERVER defined and thus the comment must now be
2088 * incorrect or the code was broken since the comment was written.
2090 # ifdef SHUTDOWN_SERVER
2091 if (fb->root && fb->root->method != server_method)
2093 # ifndef NO_SOCKET_TO_FD
2095 /* shutdown() sockets */
2096 if (statted && S_ISSOCK (s.st_mode))
2097 shutdown (fb->fd, 0);
2099 # endif /* NO_SOCKET_TO_FD */
2100 # ifdef START_RSH_WITH_POPEN_RW
2101 /* Can't be set with SHUTDOWN_SERVER defined */
2102 /* FIXME: This is now certainly broken since pclose is defined by ANSI
2103 * C to accept a FILE * argument. The switch will need to happen at a
2104 * higher abstraction level to switch between initializing stdio & fd
2105 * buffers on systems that need this (or maybe an fd buffer that keeps
2106 * track of the FILE * could be used - I think flushing the stream
2107 * before beginning exclusive access via the FD is OK.
2109 else if (fb->root && pclose (fb->fd) == EOF)
2111 error (1, errno, "closing connection to %s",
2112 fb->root->hostname);
2115 # endif /* START_RSH_WITH_POPEN_RW */
2119 else if (buf->output)
2121 # ifdef SHUTDOWN_SERVER
2122 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
2123 * SHUTDOWN_SERVER_OUTPUT
2125 if (fb->root && fb->root->method == server_method)
2126 SHUTDOWN_SERVER (fb->fd);
2129 # ifndef NO_SOCKET_TO_FD
2130 /* shutdown() sockets */
2131 if (statted && S_ISSOCK (s.st_mode))
2132 shutdown (fb->fd, 1);
2135 /* I'm not sure I like this empty block, but the alternative
2136 * is another nested NO_SOCKET_TO_FD switch as above.
2139 # endif /* NO_SOCKET_TO_FD */
2144 if (statted && closefd && close (fb->fd) == -1)
2147 # ifdef SERVER_SUPPORT
2149 # endif /* SERVER_SUPPORT */
2154 # ifdef CLIENT_SUPPORT
2156 error (1, errno, "closing down connection to %s",
2157 fb->root->hostname);
2159 # endif /* CLIENT_SUPPORT */
2161 error (0, errno, "closing down buffer");
2164 /* If we were talking to a process, make sure it exited */
2170 w = waitpid (fb->child_pid, NULL, 0);
2171 while (w == -1 && errno == EINTR);
2173 error (1, errno, "waiting for process %d", fb->child_pid);
2176 free (buf->closure);
2177 buf->closure = NULL;
2181 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */