1 /* Code for the buffer data structure. */
6 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
11 # include <sys/socket.h>
14 /* OS/2 doesn't have EIO. FIXME: this whole notion of turning
15 a different error into EIO strikes me as pretty dubious. */
20 /* Linked list of available buffer_data structures. */
21 static struct buffer_data *free_buffer_data;
23 /* Local functions. */
24 static void buf_default_memory_error (struct buffer *);
25 static void allocate_buffer_datas (void);
26 static struct buffer_data *get_buffer_data (void);
30 /* Initialize a buffer structure. */
32 buf_initialize (int (*input) (void *, char *, size_t, size_t, size_t *),
33 int (*output) (void *, const char *, size_t, size_t *),
34 int (*flush) (void *),
35 int (*block) (void *, bool),
36 int (*get_fd) (void *),
37 int (*shutdown) (struct buffer *),
38 void (*memory) (struct buffer *),
43 buf = xmalloc (sizeof (struct buffer));
46 buf->nonblocking = false;
52 buf->shutdown = shutdown;
53 buf->memory_error = memory ? memory : buf_default_memory_error;
54 buf->closure = closure;
60 /* Free a buffer structure. */
62 buf_free (struct buffer *buf)
64 if (buf->closure != NULL)
69 if (buf->data != NULL)
71 buf->last->next = free_buffer_data;
72 free_buffer_data = buf->data;
79 /* Initialize a buffer structure which is not to be used for I/O. */
81 buf_nonio_initialize( void (*memory) (struct buffer *) )
83 return buf_initialize (NULL, NULL, NULL, NULL, NULL, NULL, memory, NULL);
88 /* Default memory error handler. */
90 buf_default_memory_error (struct buffer *buf)
92 error (1, 0, "out of memory");
97 /* Allocate more buffer_data structures. */
99 allocate_buffer_datas (void)
101 struct buffer_data *alc;
105 /* Allocate buffer_data structures in blocks of 16. */
106 # define ALLOC_COUNT (16)
108 alc = xmalloc (ALLOC_COUNT * sizeof (struct buffer_data));
109 space = valloc (ALLOC_COUNT * BUFFER_DATA_SIZE);
110 if (alc == NULL || space == NULL)
112 for (i = 0; i < ALLOC_COUNT; i++, alc++, space += BUFFER_DATA_SIZE)
114 alc->next = free_buffer_data;
115 free_buffer_data = alc;
122 /* Get a new buffer_data structure. */
123 static struct buffer_data *
124 get_buffer_data (void)
126 struct buffer_data *ret;
128 if (free_buffer_data == NULL)
130 allocate_buffer_datas ();
131 if (free_buffer_data == NULL)
135 ret = free_buffer_data;
136 free_buffer_data = ret->next;
142 /* See whether a buffer and its file descriptor is empty. */
147 /* Try and read any data on the file descriptor first.
148 * We already know the descriptor is non-blocking.
150 buf_input_data (buf, NULL);
151 return buf_empty_p (buf);
156 /* See whether a buffer is empty. */
158 buf_empty_p (struct buffer *buf)
160 struct buffer_data *data;
162 for (data = buf->data; data != NULL; data = data->next)
170 # if defined (SERVER_FLOWCONTROL) || defined (PROXY_SUPPORT)
172 * Count how much data is stored in the buffer..
173 * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
176 buf_count_mem (struct buffer *buf)
178 struct buffer_data *data;
181 for (data = buf->data; data != NULL; data = data->next)
182 mem += BUFFER_DATA_SIZE;
186 # endif /* SERVER_FLOWCONTROL || PROXY_SUPPORT */
190 /* Add data DATA of length LEN to BUF. */
192 buf_output (struct buffer *buf, const char *data, size_t len)
194 if (buf->data != NULL
195 && (((buf->last->text + BUFFER_DATA_SIZE)
196 - (buf->last->bufp + buf->last->size))
199 memcpy (buf->last->bufp + buf->last->size, data, len);
200 buf->last->size += len;
206 struct buffer_data *newdata;
208 newdata = get_buffer_data ();
211 (*buf->memory_error) (buf);
215 if (buf->data == NULL)
218 buf->last->next = newdata;
219 newdata->next = NULL;
222 newdata->bufp = newdata->text;
224 if (len <= BUFFER_DATA_SIZE)
227 memcpy (newdata->text, data, len);
231 newdata->size = BUFFER_DATA_SIZE;
232 memcpy (newdata->text, data, BUFFER_DATA_SIZE);
234 data += BUFFER_DATA_SIZE;
235 len -= BUFFER_DATA_SIZE;
243 /* Add a '\0' terminated string to BUF. */
245 buf_output0 (struct buffer *buf, const char *string)
247 buf_output (buf, string, strlen (string));
252 /* Add a single character to BUF. */
254 buf_append_char (struct buffer *buf, int ch)
256 if (buf->data != NULL
257 && (buf->last->text + BUFFER_DATA_SIZE
258 != buf->last->bufp + buf->last->size))
260 *(buf->last->bufp + buf->last->size) = ch;
268 buf_output (buf, &b, 1);
275 * Send all the output we've been saving up. Returns 0 for success or
276 * errno code. If the buffer has been set to be nonblocking, this
277 * will just write until the write would block.
280 buf_send_output (struct buffer *buf)
282 assert (buf->output != NULL);
284 while (buf->data != NULL)
286 struct buffer_data *data;
295 status = (*buf->output) (buf->closure, data->bufp, data->size,
299 /* Some sort of error. Discard the data, and return. */
301 buf->last->next = free_buffer_data;
302 free_buffer_data = buf->data;
309 if (nbytes != data->size)
311 /* Not all the data was written out. This is only
312 permitted in nonblocking mode. Adjust the buffer,
315 assert (buf->nonblocking);
317 data->size -= nbytes;
318 data->bufp += nbytes;
324 buf->data = data->next;
325 data->next = free_buffer_data;
326 free_buffer_data = data;
337 * Flush any data queued up in the buffer. If BLOCK is nonzero, then
338 * if the buffer is in nonblocking mode, put it into blocking mode for
339 * the duration of the flush. This returns 0 on success, or an error
343 buf_flush (struct buffer *buf, bool block)
348 assert (buf->flush != NULL);
350 nonblocking = buf->nonblocking;
351 if (nonblocking && block)
353 status = set_block (buf);
358 status = buf_send_output (buf);
360 status = (*buf->flush) (buf->closure);
362 if (nonblocking && block)
366 blockstat = set_nonblock (buf);
377 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno
381 set_nonblock (struct buffer *buf)
385 if (buf->nonblocking)
387 assert (buf->block != NULL);
388 status = (*buf->block) (buf->closure, 0);
391 buf->nonblocking = true;
398 * Set buffer BUF to blocking I/O. Returns 0 for success or errno
402 set_block (struct buffer *buf)
406 if (! buf->nonblocking)
408 assert (buf->block != NULL);
409 status = (*buf->block) (buf->closure, 1);
412 buf->nonblocking = false;
419 * Send a character count and some output. Returns errno code or 0 for
422 * Sending the count in binary is OK since this is only used on a pipe
423 * within the same system.
426 buf_send_counted (struct buffer *buf)
429 struct buffer_data *data;
432 for (data = buf->data; data != NULL; data = data->next)
435 data = get_buffer_data ();
438 (*buf->memory_error) (buf);
442 data->next = buf->data;
444 if (buf->last == NULL)
447 data->bufp = data->text;
448 data->size = sizeof (int);
450 *((int *) data->text) = size;
452 return buf_send_output (buf);
458 * Send a special count. COUNT should be negative. It will be
459 * handled specially by buf_copy_counted. This function returns 0 or
462 * Sending the count in binary is OK since this is only used on a pipe
463 * within the same system.
466 buf_send_special_count (struct buffer *buf, int count)
468 struct buffer_data *data;
470 data = get_buffer_data ();
473 (*buf->memory_error) (buf);
477 data->next = buf->data;
479 if (buf->last == NULL)
482 data->bufp = data->text;
483 data->size = sizeof (int);
485 *((int *) data->text) = count;
487 return buf_send_output (buf);
492 /* Append a list of buffer_data structures to an buffer. */
494 buf_append_data (struct buffer *buf, struct buffer_data *data,
495 struct buffer_data *last)
499 if (buf->data == NULL)
502 buf->last->next = data;
509 # ifdef PROXY_SUPPORT
510 /* Copy data structures and append them to a buffer.
513 * Failure to allocate memory here is fatal.
516 buf_copy_data (struct buffer *buf, struct buffer_data *data,
517 struct buffer_data *last)
519 struct buffer_data *first, *new, *cur, *prev;
528 new = get_buffer_data ();
529 if (!new) error (1, errno, "Failed to allocate buffer data.");
531 if (!first) first = new;
532 memcpy (new->text, cur->bufp, cur->size);
533 new->bufp = new->text;
534 new->size = cur->size;
536 if (prev) prev->next = new;
537 if (cur == last) break;
542 buf_append_data (buf, first, new);
547 /* Dispose of any remaining data in the buffer. */
549 buf_free_data (struct buffer *buffer)
551 if (buf_empty_p (buffer)) return;
552 buffer->last->next = free_buffer_data;
553 free_buffer_data = buffer->data;
554 buffer->data = buffer->last = NULL;
556 # endif /* PROXY_SUPPORT */
560 /* Append the data in one buffer to another. This removes the data
561 * from the source buffer.
564 buf_append_buffer (struct buffer *to, struct buffer *from)
566 struct buffer_data *n;
568 /* Copy the data pointer to the new buf. */
569 buf_append_data (to, from->data, from->last);
574 if (n == from->last) break;
578 /* Remove from the original location. */
586 * Copy the contents of file F into buffer_data structures. We can't
587 * copy directly into an buffer, because we want to handle failure and
588 * success differently. Returns 0 on success, or -2 if out of
589 * memory, or a status code on error. Since the caller happens to
590 * know the size of the file, it is passed in as SIZE. On success,
591 * this function sets *RETP and *LASTP, which may be passed to
595 buf_read_file (FILE *f, long int size, struct buffer_data **retp,
596 struct buffer_data **lastp)
605 struct buffer_data *data;
608 data = get_buffer_data ();
618 (*lastp)->next = data;
622 data->bufp = data->text;
625 if (size > BUFFER_DATA_SIZE)
626 get = BUFFER_DATA_SIZE;
631 if (fread (data->text, get, 1, f) != 1)
646 (*lastp)->next = free_buffer_data;
647 free_buffer_data = *retp;
655 * Copy the contents of file F into buffer_data structures. We can't
656 * copy directly into an buffer, because we want to handle failure and
657 * success differently. Returns 0 on success, or -2 if out of
658 * memory, or a status code on error. On success, this function sets
659 * *RETP and *LASTP, which may be passed to buf_append_data.
662 buf_read_file_to_eof (FILE *f, struct buffer_data **retp,
663 struct buffer_data **lastp)
672 struct buffer_data *data;
675 data = get_buffer_data ();
685 (*lastp)->next = data;
689 data->bufp = data->text;
692 get = BUFFER_DATA_SIZE;
695 nread = fread (data->text, 1, get, f);
696 if (nread == 0 && !feof (f))
710 (*lastp)->next = free_buffer_data;
711 free_buffer_data = *retp;
718 /* Return the number of bytes in a chain of buffer_data structures. */
720 buf_chain_length (struct buffer_data *buf)
733 /* Return the number of bytes in a buffer. */
735 buf_length (struct buffer *buf)
737 return buf_chain_length (buf->data);
743 * Read an arbitrary amount of data into an input buffer. The buffer
744 * will be in nonblocking mode, and we just grab what we can. Return
745 * 0 on success, or -1 on end of file, or -2 if out of memory, or an
746 * error code. If COUNTP is not NULL, *COUNTP is set to the number of
750 buf_input_data (struct buffer *buf, size_t *countp)
752 assert (buf->input != NULL);
762 if (buf->data == NULL
763 || (buf->last->bufp + buf->last->size
764 == buf->last->text + BUFFER_DATA_SIZE))
766 struct buffer_data *data;
768 data = get_buffer_data ();
771 (*buf->memory_error) (buf);
775 if (buf->data == NULL)
778 buf->last->next = data;
782 data->bufp = data->text;
786 get = ((buf->last->text + BUFFER_DATA_SIZE)
787 - (buf->last->bufp + buf->last->size));
789 status = (*buf->input) (buf->closure,
790 buf->last->bufp + buf->last->size,
795 buf->last->size += nbytes;
801 /* If we did not fill the buffer, then presumably we read
802 all the available data. */
813 * Read a line (characters up to a \012) from an input buffer. (We
814 * use \012 rather than \n for the benefit of non Unix clients for
815 * which \n means something else). This returns 0 on success, or -1
816 * on end of file, or -2 if out of memory, or an error code. If it
817 * succeeds, it sets *LINE to an allocated buffer holding the contents
818 * of the line. The trailing \012 is not included in the buffer. If
819 * LENP is not NULL, then *LENP is set to the number of bytes read;
820 * strlen may not work, because there may be embedded null bytes.
823 buf_read_line (struct buffer *buf, char **line, size_t *lenp)
825 return buf_read_short_line (buf, line, lenp, SIZE_MAX);
830 /* Like buf_read_line, but return -2 if no newline is found in MAX characters.
833 buf_read_short_line (struct buffer *buf, char **line, size_t *lenp,
836 assert (buf->input != NULL);
842 size_t len, finallen, predicted_len;
843 struct buffer_data *data;
846 /* See if there is a newline in BUF. */
848 for (data = buf->data; data != NULL; data = data->next)
850 nl = memchr (data->bufp, '\012', data->size);
853 finallen = nl - data->bufp;
854 if (xsum (len, finallen) >= max) return -2;
858 else if (xsum (len, data->size) >= max) return -2;
862 /* If we found a newline, copy the line into a memory buffer,
863 and remove it from BUF. */
867 struct buffer_data *nldata;
869 p = xmalloc (len + 1);
876 while (data != nldata)
878 struct buffer_data *next;
880 memcpy (p, data->bufp, data->size);
883 data->next = free_buffer_data;
884 free_buffer_data = data;
888 memcpy (p, data->bufp, finallen);
891 data->size -= finallen + 1;
901 /* Read more data until we get a newline or MAX characters. */
909 if (buf->data == NULL
910 || (buf->last->bufp + buf->last->size
911 == buf->last->text + BUFFER_DATA_SIZE))
913 data = get_buffer_data ();
916 (*buf->memory_error) (buf);
920 if (buf->data == NULL)
923 buf->last->next = data;
927 data->bufp = data->text;
931 mem = buf->last->bufp + buf->last->size;
932 size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
934 /* We need to read at least 1 byte. We can handle up to
935 SIZE bytes. This will only be efficient if the
936 underlying communication stream does its own buffering,
937 or is clever about getting more than 1 byte at a time. */
938 status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
942 predicted_len += nbytes;
943 buf->last->size += nbytes;
945 /* Optimize slightly to avoid an unnecessary call to memchr. */
953 if (memchr (mem, '\012', nbytes) != NULL)
956 if (xsum (len, predicted_len) >= max) return -2;
964 * Extract data from the input buffer BUF. This will read up to WANT
965 * bytes from the buffer. It will set *RETDATA to point at the bytes,
966 * and set *GOT to the number of bytes to be found there. Any buffer
967 * call which uses BUF may change the contents of the buffer at *DATA,
968 * so the data should be fully processed before any further calls are
969 * made. This returns 0 on success, or -1 on end of file, or -2 if
970 * out of memory, or an error code.
973 buf_read_data (struct buffer *buf, size_t want, char **retdata, size_t *got)
975 assert (buf->input != NULL);
977 while (buf->data != NULL && buf->data->size == 0)
979 struct buffer_data *next;
981 next = buf->data->next;
982 buf->data->next = free_buffer_data;
983 free_buffer_data = buf->data;
989 if (buf->data == NULL)
991 struct buffer_data *data;
995 data = get_buffer_data ();
998 (*buf->memory_error) (buf);
1005 data->bufp = data->text;
1008 if (want < BUFFER_DATA_SIZE)
1011 get = BUFFER_DATA_SIZE;
1012 status = (*buf->input) (buf->closure, data->bufp, get,
1013 BUFFER_DATA_SIZE, &nbytes);
1017 data->size = nbytes;
1020 *retdata = buf->data->bufp;
1021 if (want < buf->data->size)
1024 buf->data->size -= want;
1025 buf->data->bufp += want;
1029 *got = buf->data->size;
1030 buf->data->size = 0;
1039 * Copy lines from an input buffer to an output buffer.
1040 * This copies all complete lines (characters up to a
1041 * newline) from INBUF to OUTBUF. Each line in OUTBUF is preceded by the
1042 * character COMMAND and a space.
1045 buf_copy_lines (struct buffer *outbuf, struct buffer *inbuf, int command)
1049 struct buffer_data *data;
1050 struct buffer_data *nldata;
1054 /* See if there is a newline in INBUF. */
1057 for (data = inbuf->data; data != NULL; data = data->next)
1059 nl = memchr (data->bufp, '\n', data->size);
1069 /* There are no more lines in INBUF. */
1073 /* Put in the command. */
1074 buf_append_char (outbuf, command);
1075 buf_append_char (outbuf, ' ');
1077 if (inbuf->data != nldata)
1080 * Simply move over all the buffers up to the one containing
1083 for (data = inbuf->data; data->next != nldata; data = data->next);
1085 buf_append_data (outbuf, inbuf->data, data);
1086 inbuf->data = nldata;
1090 * If the newline is at the very end of the buffer, just move
1091 * the buffer onto OUTBUF. Otherwise we must copy the data.
1093 len = nl + 1 - nldata->bufp;
1094 if (len == nldata->size)
1096 inbuf->data = nldata->next;
1097 if (inbuf->data == NULL)
1100 nldata->next = NULL;
1101 buf_append_data (outbuf, nldata, nldata);
1105 buf_output (outbuf, nldata->bufp, len);
1106 nldata->bufp += len;
1107 nldata->size -= len;
1115 * Copy counted data from one buffer to another. The count is an
1116 * integer, host size, host byte order (it is only used across a
1117 * pipe). If there is enough data, it should be moved over. If there
1118 * is not enough data, it should remain on the original buffer. A
1119 * negative count is a special case. if one is seen, *SPECIAL is set
1120 * to the (negative) count value and no additional data is gathered
1121 * from the buffer; normally *SPECIAL is set to 0. This function
1122 * returns the number of bytes it needs to see in order to actually
1123 * copy something over.
1126 buf_copy_counted (struct buffer *outbuf, struct buffer *inbuf, int *special)
1132 struct buffer_data *data;
1136 char intbuf[sizeof (int)];
1141 struct buffer_data *start;
1143 struct buffer_data *stop;
1146 /* See if we have enough bytes to figure out the count. */
1147 need = sizeof (int);
1149 for (data = inbuf->data; data != NULL; data = data->next)
1151 if (data->size >= need)
1153 memcpy (intp, data->bufp, need);
1156 memcpy (intp, data->bufp, data->size);
1162 /* We don't have enough bytes to form an integer. */
1172 /* A negative COUNT is a special case meaning that we
1173 don't need any further information. */
1180 * We have an integer in COUNT. We have gotten all the
1181 * data from INBUF in all buffers before START, and we
1182 * have gotten STARTOFF bytes from START. See if we have
1183 * enough bytes remaining in INBUF.
1185 need = count - (start->size - startoff);
1193 for (data = start->next; data != NULL; data = data->next)
1195 if (need <= data->size)
1201 /* We don't have enough bytes. */
1210 * We have enough bytes. Free any buffers in INBUF before
1211 * START, and remove STARTOFF bytes from START, so that we can
1212 * forget about STARTOFF.
1214 start->bufp += startoff;
1215 start->size -= startoff;
1217 if (start->size == 0)
1218 start = start->next;
1220 if (stop->size == stopwant)
1226 while (inbuf->data != start)
1229 inbuf->data = data->next;
1230 data->next = free_buffer_data;
1231 free_buffer_data = data;
1234 /* If COUNT is negative, set *SPECIAL and get out now. */
1242 * We want to copy over the bytes from START through STOP. We
1243 * only want STOPWANT bytes from STOP.
1248 /* Attach the buffers from START through STOP to OUTBUF. */
1249 for (data = start; data->next != stop; data = data->next);
1252 buf_append_data (outbuf, start, data);
1257 buf_output (outbuf, stop->bufp, stopwant);
1258 stop->bufp += stopwant;
1259 stop->size -= stopwant;
1269 buf_get_fd (struct buffer *buf)
1272 return (*buf->get_fd) (buf->closure);
1278 /* Shut down a buffer. This returns 0 on success, or an errno code. */
1280 buf_shutdown (struct buffer *buf)
1282 if (buf->shutdown) return (*buf->shutdown) (buf);
1288 /* Certain types of communication input and output data in packets,
1289 where each packet is translated in some fashion. The packetizing
1290 buffer type supports that, given a buffer which handles lower level
1291 I/O and a routine to translate the data in a packet.
1293 This code uses two bytes for the size of a packet, so packets are
1294 restricted to 65536 bytes in total.
1296 The translation functions should just translate; they may not
1297 significantly increase or decrease the amount of data. The actual
1298 size of the initial data is part of the translated data. The
1299 output translation routine may add up to PACKET_SLOP additional
1300 bytes, and the input translation routine should shrink the data
1303 # define PACKET_SLOP (100)
1305 /* This structure is the closure field of a packetizing buffer. */
1307 struct packetizing_buffer
1309 /* The underlying buffer. */
1311 /* The input translation function. Exactly one of inpfn and outfn
1312 will be NULL. The input translation function should
1313 untranslate the data in INPUT, storing the result in OUTPUT.
1314 SIZE is the amount of data in INPUT, and is also the size of
1315 OUTPUT. This should return 0 on success, or an errno code. */
1316 int (*inpfn) (void *fnclosure, const char *input, char *output,
1318 /* The output translation function. This should translate the
1319 data in INPUT, storing the result in OUTPUT. The first two
1320 bytes in INPUT will be the size of the data, and so will SIZE.
1321 This should set *TRANSLATED to the amount of translated data in
1322 OUTPUT. OUTPUT is large enough to hold SIZE + PACKET_SLOP
1323 bytes. This should return 0 on success, or an errno code. */
1324 int (*outfn) (void *fnclosure, const char *input, char *output,
1325 size_t size, size_t *translated);
1326 /* A closure for the translation function. */
1328 /* For an input buffer, we may have to buffer up data here. */
1329 /* This is non-zero if the buffered data has been translated.
1330 Otherwise, the buffered data has not been translated, and starts
1331 with the two byte packet size. */
1333 /* The amount of buffered data. */
1335 /* The buffer allocated to hold the data. */
1337 /* The size of holdbuf. */
1339 /* If translated is set, we need another data pointer to track
1340 where we are in holdbuf. If translated is clear, then this
1341 pointer is not used. */
1347 static int packetizing_buffer_input (void *, char *, size_t, size_t, size_t *);
1348 static int packetizing_buffer_output (void *, const char *, size_t, size_t *);
1349 static int packetizing_buffer_flush (void *);
1350 static int packetizing_buffer_block (void *, bool);
1351 static int packetizing_buffer_get_fd (void *);
1352 static int packetizing_buffer_shutdown (struct buffer *);
1356 /* Create a packetizing buffer. */
1358 packetizing_buffer_initialize (struct buffer *buf,
1359 int (*inpfn) (void *, const char *, char *,
1361 int (*outfn) (void *, const char *, char *,
1364 void (*memory) (struct buffer *))
1366 struct packetizing_buffer *pb;
1368 pb = xmalloc (sizeof *pb);
1369 memset (pb, 0, sizeof *pb);
1374 pb->fnclosure = fnclosure;
1378 /* Add PACKET_SLOP to handle larger translated packets, and
1379 add 2 for the count. This buffer is increased if
1381 pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
1382 pb->holdbuf = xmalloc (pb->holdbufsize);
1385 return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
1386 inpfn != NULL ? NULL : packetizing_buffer_output,
1387 inpfn != NULL ? NULL : packetizing_buffer_flush,
1388 packetizing_buffer_block,
1389 packetizing_buffer_get_fd,
1390 packetizing_buffer_shutdown,
1397 /* Input data from a packetizing buffer. */
1399 packetizing_buffer_input (void *closure, char *data, size_t need, size_t size,
1402 struct packetizing_buffer *pb = closure;
1406 if (pb->holdsize > 0 && pb->translated)
1410 copy = pb->holdsize;
1414 memcpy (data, pb->holddata, size);
1415 pb->holdsize -= size;
1416 pb->holddata += size;
1421 memcpy (data, pb->holddata, copy);
1423 pb->translated = false;
1431 while (need > 0 || *got == 0)
1434 size_t get, nread, count, tcount;
1436 char stackoutbuf[BUFFER_DATA_SIZE + PACKET_SLOP];
1437 char *inbuf, *outbuf;
1439 /* If we don't already have the two byte count, get it. */
1440 if (pb->holdsize < 2)
1442 get = 2 - pb->holdsize;
1443 status = buf_read_data (pb->buf, get, &bytes, &nread);
1446 /* buf_read_data can return -2, but a buffer input
1447 function is only supposed to return -1, 0, or an
1456 /* The buffer is in nonblocking mode, and we didn't
1457 manage to read anything. */
1462 pb->holdbuf[1] = bytes[0];
1465 pb->holdbuf[0] = bytes[0];
1468 /* We only got one byte, but we needed two. Stash
1469 the byte we got, and try again. */
1473 pb->holdbuf[1] = bytes[1];
1478 /* Read the packet. */
1480 count = (((pb->holdbuf[0] & 0xff) << 8)
1481 + (pb->holdbuf[1] & 0xff));
1483 if (count + 2 > pb->holdbufsize)
1487 /* We didn't allocate enough space in the initialize
1490 n = xrealloc (pb->holdbuf, count + 2);
1493 (*pb->buf->memory_error) (pb->buf);
1497 pb->holdbufsize = count + 2;
1500 get = count - (pb->holdsize - 2);
1502 status = buf_read_data (pb->buf, get, &bytes, &nread);
1505 /* buf_read_data can return -2, but a buffer input
1506 function is only supposed to return -1, 0, or an error
1515 /* We did not get any data. Presumably the buffer is in
1516 nonblocking mode. */
1522 /* We did not get all the data we need to fill the packet.
1523 buf_read_data does not promise to return all the bytes
1524 requested, so we must try again. */
1525 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1526 pb->holdsize += nread;
1530 /* We have a complete untranslated packet of COUNT bytes. */
1532 if (pb->holdsize == 2)
1534 /* We just read the entire packet (the 2 bytes in
1535 PB->HOLDBUF are the size). Save a memcpy by
1536 translating directly from BYTES. */
1541 /* We already had a partial packet in PB->HOLDBUF. We
1542 need to copy the new data over to make the input
1544 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1545 inbuf = pb->holdbuf + 2;
1548 if (count <= sizeof stackoutbuf)
1549 outbuf = stackoutbuf;
1552 outbuf = xmalloc (count);
1555 (*pb->buf->memory_error) (pb->buf);
1560 status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1564 /* The first two bytes in the translated buffer are the real
1565 length of the translated data. */
1566 tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
1569 error (1, 0, "Input translation failure");
1573 /* We have more data than the caller has provided space
1574 for. We need to save some of it for the next call. */
1576 memcpy (data, outbuf + 2, size);
1579 pb->holdsize = tcount - size;
1580 memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
1581 pb->holddata = pb->holdbuf;
1582 pb->translated = true;
1584 if (outbuf != stackoutbuf)
1590 memcpy (data, outbuf + 2, tcount);
1592 if (outbuf != stackoutbuf)
1608 /* Output data to a packetizing buffer. */
1610 packetizing_buffer_output (void *closure, const char *data, size_t have,
1613 struct packetizing_buffer *pb = closure;
1614 char inbuf[BUFFER_DATA_SIZE + 2];
1615 char stack_outbuf[BUFFER_DATA_SIZE + PACKET_SLOP + 4];
1616 struct buffer_data *outdata = NULL; /* Initialize to silence -Wall. Dumb.
1619 size_t size, translated;
1622 /* It would be easy to xmalloc a buffer, but I don't think this
1623 case can ever arise. */
1624 assert (have <= BUFFER_DATA_SIZE);
1626 inbuf[0] = (have >> 8) & 0xff;
1627 inbuf[1] = have & 0xff;
1628 memcpy (inbuf + 2, data, have);
1632 /* The output function is permitted to add up to PACKET_SLOP
1633 bytes, and we need 2 bytes for the size of the translated data.
1634 If we can guarantee that the result will fit in a buffer_data,
1635 we translate directly into one to avoid a memcpy in buf_output. */
1636 if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
1637 outbuf = stack_outbuf;
1640 outdata = get_buffer_data ();
1641 if (outdata == NULL)
1643 (*pb->buf->memory_error) (pb->buf);
1647 outdata->next = NULL;
1648 outdata->bufp = outdata->text;
1650 outbuf = outdata->text;
1653 status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1658 /* The output function is permitted to add up to PACKET_SLOP
1660 assert (translated <= size + PACKET_SLOP);
1662 outbuf[0] = (translated >> 8) & 0xff;
1663 outbuf[1] = translated & 0xff;
1665 if (outbuf == stack_outbuf)
1666 buf_output (pb->buf, outbuf, translated + 2);
1669 outdata->size = translated + 2;
1670 buf_append_data (pb->buf, outdata, outdata);
1675 /* We will only be here because buf_send_output was called on the
1676 packetizing buffer. That means that we should now call
1677 buf_send_output on the underlying buffer. */
1678 return buf_send_output (pb->buf);
1683 /* Flush data to a packetizing buffer. */
1685 packetizing_buffer_flush (void *closure)
1687 struct packetizing_buffer *pb = closure;
1689 /* Flush the underlying buffer. Note that if the original call to
1690 buf_flush passed 1 for the BLOCK argument, then the buffer will
1691 already have been set into blocking mode, so we should always
1693 return buf_flush (pb->buf, 0);
1698 /* The block routine for a packetizing buffer. */
1700 packetizing_buffer_block (void *closure, bool block)
1702 struct packetizing_buffer *pb = closure;
1705 return set_block (pb->buf);
1707 return set_nonblock (pb->buf);
1712 /* Return the file descriptor underlying any child buffers. */
1714 packetizing_buffer_get_fd (void *closure)
1716 struct packetizing_buffer *cb = closure;
1717 return buf_get_fd (cb->buf);
1722 /* Shut down a packetizing buffer. */
1724 packetizing_buffer_shutdown (struct buffer *buf)
1726 struct packetizing_buffer *pb = buf->closure;
1728 return buf_shutdown (pb->buf);
1733 /* All server communication goes through buffer structures. Most of
1734 the buffers are built on top of a file descriptor. This structure
1735 is used as the closure field in a buffer. */
1739 /* The file descriptor. */
1741 /* Nonzero if the file descriptor is in blocking mode. */
1743 /* The child process id when fd is a pipe. */
1745 /* The connection info, when fd is a pipe to a server. */
1749 static int fd_buffer_input (void *, char *, size_t, size_t, size_t *);
1750 static int fd_buffer_output (void *, const char *, size_t, size_t *);
1751 static int fd_buffer_flush (void *);
1752 static int fd_buffer_block (void *, bool);
1753 static int fd_buffer_get_fd (void *);
1754 static int fd_buffer_shutdown (struct buffer *);
1756 /* Initialize a buffer built on a file descriptor. FD is the file
1757 descriptor. INPUT is nonzero if this is for input, zero if this is
1758 for output. MEMORY is the function to call when a memory error
1762 fd_buffer_initialize (int fd, pid_t child_pid, cvsroot_t *root, bool input,
1763 void (*memory) (struct buffer *))
1765 struct fd_buffer *n;
1767 n = xmalloc (sizeof *n);
1769 n->child_pid = child_pid;
1771 fd_buffer_block (n, true);
1772 return buf_initialize (input ? fd_buffer_input : NULL,
1773 input ? NULL : fd_buffer_output,
1774 input ? NULL : fd_buffer_flush,
1775 fd_buffer_block, fd_buffer_get_fd,
1783 /* The buffer input function for a buffer built on a file descriptor.
1785 * In non-blocing mode, this function will read as many bytes as it can in a
1786 * single try, up to SIZE bytes, and return.
1788 * In blocking mode with NEED > 0, this function will read as many bytes as it
1789 * can but will not return until it has read at least NEED bytes.
1791 * In blocking mode with NEED == 0, this function will block until it can read
1792 * either at least one byte or EOF, then read as many bytes as are available
1793 * and return. At the very least, compress_buffer_shutdown depends on this
1794 * behavior to read EOF and can loop indefinitely without it.
1800 * closure Our FD_BUFFER struct.
1801 * data The start of our input buffer.
1802 * need How many bytes our caller needs.
1803 * size How many bytes are available in DATA.
1804 * got Where to store the number of bytes read.
1807 * data Filled with bytes read.
1808 * *got Number of bytes actually read into DATA.
1816 * This function can return an error if fd_buffer_block(), or the system
1817 * read() or select() calls do.
1820 fd_buffer_input (void *closure, char *data, size_t need, size_t size,
1823 struct fd_buffer *fb = closure;
1826 assert (need <= size);
1835 /* Set non-block. */
1836 status = fd_buffer_block (fb, false);
1837 if (status != 0) return status;
1840 FD_SET (fb->fd, &readfds);
1846 /* This used to select on exceptions too, but as far
1847 as I know there was never any reason to do that and
1848 SCO doesn't let you select on exceptions on pipes. */
1849 numfds = select (fb->fd + 1, &readfds, NULL, NULL, NULL);
1850 if (numfds < 0 && errno != EINTR)
1855 } while (numfds < 0);
1857 nbytes = read (fb->fd, data + *got, size - *got);
1861 /* End of file. This assumes that we are using POSIX or BSD
1862 style nonblocking I/O. On System V we will get a zero
1863 return if there is no data, even when not at EOF. */
1866 /* We already read some data, so return no error, counting
1867 * on the fact that we will read EOF again next time.
1882 /* Some error occurred. */
1883 if (!blocking_error (errno))
1888 /* else Everything's fine, we just didn't get any data. */
1892 } while (*got < need);
1895 if (status == 0 || status == -1)
1899 /* OK or EOF - Reset block. */
1900 newstatus = fd_buffer_block (fb, true);
1901 if (newstatus) status = newstatus;
1906 /* The above will always return. Handle non-blocking read. */
1907 nbytes = read (fb->fd, data, size);
1916 /* End of file. This assumes that we are using POSIX or BSD
1917 style nonblocking I/O. On System V we will get a zero
1918 return if there is no data, even when not at EOF. */
1921 /* Some error occurred. */
1922 if (blocking_error (errno))
1923 /* Everything's fine, we just didn't get any data. */
1931 /* The buffer output function for a buffer built on a file descriptor. */
1934 fd_buffer_output (void *closure, const char *data, size_t have, size_t *wrote)
1936 struct fd_buffer *fd = closure;
1944 nbytes = write (fd->fd, data, have);
1949 && (nbytes == 0 || blocking_error (errno)))
1951 /* A nonblocking write failed to write any data. Just
1956 /* Some sort of error occurred. */
1974 /* The buffer flush function for a buffer built on a file descriptor. */
1976 fd_buffer_flush (void *closure)
1978 /* We don't need to do anything here. Our fd doesn't have its own buffer
1979 * and syncing won't do anything but slow us down.
1981 * struct fd_buffer *fb = closure;
1983 * if (fsync (fb->fd) < 0 && errno != EROFS && errno != EINVAL)
1991 static struct stat devnull;
1992 static int devnull_set = -1;
1994 /* The buffer block function for a buffer built on a file descriptor. */
1996 fd_buffer_block (void *closure, bool block)
1998 struct fd_buffer *fb = closure;
1999 # if defined (F_GETFL) && defined (O_NONBLOCK) && defined (F_SETFL)
2002 flags = fcntl (fb->fd, F_GETFL, 0);
2007 flags &= ~O_NONBLOCK;
2009 flags |= O_NONBLOCK;
2011 if (fcntl (fb->fd, F_SETFL, flags) < 0)
2014 * BSD returns ENODEV when we try to set block/nonblock on /dev/null.
2015 * BSDI returns ENOTTY when we try to set block/nonblock on /dev/null.
2018 int save_errno = errno;
2019 bool isdevnull = false;
2021 if (devnull_set == -1)
2022 devnull_set = stat ("/dev/null", &devnull);
2024 if (devnull_set >= 0)
2025 /* Equivalent to /dev/null ? */
2026 isdevnull = (fstat (fb->fd, &sb) >= 0
2027 && sb.st_dev == devnull.st_dev
2028 && sb.st_ino == devnull.st_ino
2029 && sb.st_mode == devnull.st_mode
2030 && sb.st_uid == devnull.st_uid
2031 && sb.st_gid == devnull.st_gid
2032 && sb.st_size == devnull.st_size
2033 && sb.st_blocks == devnull.st_blocks
2034 && sb.st_blksize == devnull.st_blksize);
2043 # endif /* F_GETFL && O_NONBLOCK && F_SETFL */
2045 fb->blocking = block;
2053 fd_buffer_get_fd (void *closure)
2055 struct fd_buffer *fb = closure;
2061 /* The buffer shutdown function for a buffer built on a file descriptor.
2063 * This function disposes of memory allocated for this buffer.
2066 fd_buffer_shutdown (struct buffer *buf)
2068 struct fd_buffer *fb = buf->closure;
2070 bool closefd, statted;
2072 /* Must be an open pipe, socket, or file. What could go wrong? */
2073 if (fstat (fb->fd, &s) == -1) statted = false;
2074 else statted = true;
2075 /* Don't bother to try closing the FD if we couldn't stat it. This
2076 * probably won't work.
2078 * (buf_shutdown() on some of the server/child communication pipes is
2079 * getting EBADF on both the fstat and the close. I'm not sure why -
2080 * perhaps they were alredy closed somehow?
2084 /* Flush the buffer if possible. */
2093 /* There used to be a check here for unread data in the buffer of
2094 * the pipe, but it was deemed unnecessary and possibly dangerous. In
2095 * some sense it could be second-guessing the caller who requested it
2101 * This mess of #ifdefs is hard to read. There must be some relation between
2102 * the macros being checked which at least deserves comments - if
2103 * SHUTDOWN_SERVER, NO_SOCKET_TO_FD, & START_RSH_WITH_POPEN_RW were completely
2104 * independant, then the next few lines could easily refuse to compile.
2106 * The note below about START_RSH_WITH_POPEN_RW never being set when
2107 * SHUTDOWN_SERVER is defined means that this code would always break on
2108 * systems with SHUTDOWN_SERVER defined and thus the comment must now be
2109 * incorrect or the code was broken since the comment was written.
2111 # ifdef SHUTDOWN_SERVER
2112 if (fb->root && fb->root->method != server_method)
2114 # ifndef NO_SOCKET_TO_FD
2116 /* shutdown() sockets */
2117 if (statted && S_ISSOCK (s.st_mode))
2118 shutdown (fb->fd, 0);
2120 # endif /* NO_SOCKET_TO_FD */
2121 # ifdef START_RSH_WITH_POPEN_RW
2122 /* Can't be set with SHUTDOWN_SERVER defined */
2123 /* FIXME: This is now certainly broken since pclose is defined by ANSI
2124 * C to accept a FILE * argument. The switch will need to happen at a
2125 * higher abstraction level to switch between initializing stdio & fd
2126 * buffers on systems that need this (or maybe an fd buffer that keeps
2127 * track of the FILE * could be used - I think flushing the stream
2128 * before beginning exclusive access via the FD is OK.
2130 else if (fb->root && pclose (fb->fd) == EOF)
2132 error (1, errno, "closing connection to %s",
2133 fb->root->hostname);
2136 # endif /* START_RSH_WITH_POPEN_RW */
2140 else if (buf->output)
2142 # ifdef SHUTDOWN_SERVER
2143 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
2144 * SHUTDOWN_SERVER_OUTPUT
2146 if (fb->root && fb->root->method == server_method)
2147 SHUTDOWN_SERVER (fb->fd);
2150 # ifndef NO_SOCKET_TO_FD
2151 /* shutdown() sockets */
2152 if (statted && S_ISSOCK (s.st_mode))
2153 shutdown (fb->fd, 1);
2156 /* I'm not sure I like this empty block, but the alternative
2157 * is another nested NO_SOCKET_TO_FD switch as above.
2160 # endif /* NO_SOCKET_TO_FD */
2165 if (statted && closefd && close (fb->fd) == -1)
2168 # ifdef SERVER_SUPPORT
2170 # endif /* SERVER_SUPPORT */
2175 # ifdef CLIENT_SUPPORT
2177 error (1, errno, "closing down connection to %s",
2178 fb->root->hostname);
2180 # endif /* CLIENT_SUPPORT */
2182 error (0, errno, "closing down buffer");
2185 /* If we were talking to a process, make sure it exited */
2191 w = waitpid (fb->child_pid, (int *) 0, 0);
2192 while (w == -1 && errno == EINTR);
2194 error (1, errno, "waiting for process %d", fb->child_pid);
2197 free (buf->closure);
2198 buf->closure = NULL;
2202 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */