2 * Copyright (C) 1996-2005 The Free Software Foundation, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 /* Code for the buffer data structure. */
19 #include "pagealign_alloc.h"
21 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
23 # include <sys/socket.h>
25 /* OS/2 doesn't have EIO. FIXME: this whole notion of turning
26 a different error into EIO strikes me as pretty dubious. */
31 /* Local functions. */
32 static void buf_default_memory_error (struct buffer
*);
33 static struct buffer_data
*get_buffer_data (void);
37 /* Initialize a buffer structure. */
39 buf_initialize (type_buf_input input
,
40 type_buf_output output
,
43 type_buf_get_fd get_fd
,
44 type_buf_shutdown shutdown
,
45 type_buf_memory_error memory_error
,
50 buf
= xmalloc (sizeof (struct buffer
));
53 buf
->nonblocking
= false;
59 buf
->shutdown
= shutdown
;
60 buf
->memory_error
= memory_error
? memory_error
: buf_default_memory_error
;
61 buf
->closure
= closure
;
67 /* Free a buffer structure. */
69 buf_free (struct buffer
*buf
)
71 if (buf
->closure
!= NULL
)
82 /* Initialize a buffer structure which is not to be used for I/O. */
84 buf_nonio_initialize( void (*memory
) (struct buffer
*) )
86 return buf_initialize (NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, memory
, NULL
);
91 /* Default memory error handler. */
93 buf_default_memory_error (struct buffer
*buf
)
95 error (1, 0, "out of memory");
100 /* Allocate more buffer_data structures. */
101 /* Get a new buffer_data structure. */
102 static struct buffer_data
*
103 get_buffer_data (void)
105 struct buffer_data
*ret
;
107 ret
= xmalloc (sizeof (struct buffer_data
));
108 ret
->text
= pagealign_xalloc (BUFFER_DATA_SIZE
);
115 /* See whether a buffer and its file descriptor is empty. */
120 /* Try and read any data on the file descriptor first.
121 * We already know the descriptor is non-blocking.
123 buf_input_data (buf
, NULL
);
124 return buf_empty_p (buf
);
129 /* See whether a buffer is empty. */
131 buf_empty_p (struct buffer
*buf
)
133 struct buffer_data
*data
;
135 for (data
= buf
->data
; data
!= NULL
; data
= data
->next
)
143 # if defined (SERVER_FLOWCONTROL) || defined (PROXY_SUPPORT)
145 * Count how much data is stored in the buffer..
146 * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
149 buf_count_mem (struct buffer
*buf
)
151 struct buffer_data
*data
;
154 for (data
= buf
->data
; data
!= NULL
; data
= data
->next
)
155 mem
+= BUFFER_DATA_SIZE
;
159 # endif /* SERVER_FLOWCONTROL || PROXY_SUPPORT */
163 /* Add data DATA of length LEN to BUF. */
165 buf_output (struct buffer
*buf
, const char *data
, size_t len
)
167 if (buf
->data
!= NULL
168 && (((buf
->last
->text
+ BUFFER_DATA_SIZE
)
169 - (buf
->last
->bufp
+ buf
->last
->size
))
172 memcpy (buf
->last
->bufp
+ buf
->last
->size
, data
, len
);
173 buf
->last
->size
+= len
;
179 struct buffer_data
*newdata
;
181 newdata
= get_buffer_data ();
184 (*buf
->memory_error
) (buf
);
188 if (buf
->data
== NULL
)
191 buf
->last
->next
= newdata
;
192 newdata
->next
= NULL
;
195 newdata
->bufp
= newdata
->text
;
197 if (len
<= BUFFER_DATA_SIZE
)
200 memcpy (newdata
->text
, data
, len
);
204 newdata
->size
= BUFFER_DATA_SIZE
;
205 memcpy (newdata
->text
, data
, BUFFER_DATA_SIZE
);
207 data
+= BUFFER_DATA_SIZE
;
208 len
-= BUFFER_DATA_SIZE
;
216 /* Add a '\0' terminated string to BUF. */
218 buf_output0 (struct buffer
*buf
, const char *string
)
220 buf_output (buf
, string
, strlen (string
));
225 /* Add a single character to BUF. */
227 buf_append_char (struct buffer
*buf
, int ch
)
229 if (buf
->data
!= NULL
230 && (buf
->last
->text
+ BUFFER_DATA_SIZE
231 != buf
->last
->bufp
+ buf
->last
->size
))
233 *(buf
->last
->bufp
+ buf
->last
->size
) = ch
;
241 buf_output (buf
, &b
, 1);
247 /* Free struct buffer_data's from the list starting with FIRST and ending at
251 buf_free_datas (struct buffer_data
*first
, struct buffer_data
*last
)
253 struct buffer_data
*b
, *n
, *p
;
259 pagealign_free (b
->text
);
268 * Send all the output we've been saving up. Returns 0 for success or
269 * errno code. If the buffer has been set to be nonblocking, this
270 * will just write until the write would block.
273 buf_send_output (struct buffer
*buf
)
275 assert (buf
->output
!= NULL
);
277 while (buf
->data
!= NULL
)
279 struct buffer_data
*data
;
288 status
= (*buf
->output
) (buf
->closure
, data
->bufp
, data
->size
,
292 /* Some sort of error. Discard the data, and return. */
297 if (nbytes
!= data
->size
)
299 /* Not all the data was written out. This is only
300 permitted in nonblocking mode. Adjust the buffer,
303 assert (buf
->nonblocking
);
305 data
->size
-= nbytes
;
306 data
->bufp
+= nbytes
;
312 buf
->data
= data
->next
;
313 buf_free_datas (data
, data
);
324 * Flush any data queued up in the buffer. If BLOCK is nonzero, then
325 * if the buffer is in nonblocking mode, put it into blocking mode for
326 * the duration of the flush. This returns 0 on success, or an error
330 buf_flush (struct buffer
*buf
, bool block
)
335 assert (buf
->flush
!= NULL
);
337 nonblocking
= buf
->nonblocking
;
338 if (nonblocking
&& block
)
340 status
= set_block (buf
);
345 status
= buf_send_output (buf
);
347 status
= (*buf
->flush
) (buf
->closure
);
349 if (nonblocking
&& block
)
353 blockstat
= set_nonblock (buf
);
364 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno
368 set_nonblock (struct buffer
*buf
)
372 if (buf
->nonblocking
)
374 assert (buf
->block
!= NULL
);
375 status
= (*buf
->block
) (buf
->closure
, 0);
378 buf
->nonblocking
= true;
385 * Set buffer BUF to blocking I/O. Returns 0 for success or errno
389 set_block (struct buffer
*buf
)
393 if (! buf
->nonblocking
)
395 assert (buf
->block
!= NULL
);
396 status
= (*buf
->block
) (buf
->closure
, 1);
399 buf
->nonblocking
= false;
406 * Send a character count and some output. Returns errno code or 0 for
409 * Sending the count in binary is OK since this is only used on a pipe
410 * within the same system.
413 buf_send_counted (struct buffer
*buf
)
416 struct buffer_data
*data
;
419 for (data
= buf
->data
; data
!= NULL
; data
= data
->next
)
422 data
= get_buffer_data ();
425 (*buf
->memory_error
) (buf
);
429 data
->next
= buf
->data
;
431 if (buf
->last
== NULL
)
434 data
->bufp
= data
->text
;
435 data
->size
= sizeof (int);
437 *((int *) data
->text
) = size
;
439 return buf_send_output (buf
);
445 * Send a special count. COUNT should be negative. It will be
446 * handled specially by buf_copy_counted. This function returns 0 or
449 * Sending the count in binary is OK since this is only used on a pipe
450 * within the same system.
453 buf_send_special_count (struct buffer
*buf
, int count
)
455 struct buffer_data
*data
;
457 data
= get_buffer_data ();
460 (*buf
->memory_error
) (buf
);
464 data
->next
= buf
->data
;
466 if (buf
->last
== NULL
)
469 data
->bufp
= data
->text
;
470 data
->size
= sizeof (int);
472 *((int *) data
->text
) = count
;
474 return buf_send_output (buf
);
479 /* Append a list of buffer_data structures to an buffer. */
481 buf_append_data (struct buffer
*buf
, struct buffer_data
*data
,
482 struct buffer_data
*last
)
486 if (buf
->data
== NULL
)
489 buf
->last
->next
= data
;
496 # ifdef PROXY_SUPPORT
497 /* Copy data structures and append them to a buffer.
500 * Failure to allocate memory here is fatal.
503 buf_copy_data (struct buffer
*buf
, struct buffer_data
*data
,
504 struct buffer_data
*last
)
506 struct buffer_data
*first
, *new, *cur
, *prev
;
515 new = get_buffer_data ();
516 if (!new) error (1, errno
, "Failed to allocate buffer data.");
518 if (!first
) first
= new;
519 memcpy (new->text
, cur
->bufp
, cur
->size
);
520 new->bufp
= new->text
;
521 new->size
= cur
->size
;
523 if (prev
) prev
->next
= new;
524 if (cur
== last
) break;
529 buf_append_data (buf
, first
, new);
531 # endif /* PROXY_SUPPORT */
535 /* Dispose of any remaining data in the buffer. */
537 buf_free_data (struct buffer
*buffer
)
539 if (buf_empty_p (buffer
)) return;
540 buf_free_datas (buffer
->data
, buffer
->last
);
541 buffer
->data
= buffer
->last
= NULL
;
546 /* Append the data in one buffer to another. This removes the data
547 * from the source buffer.
550 buf_append_buffer (struct buffer
*to
, struct buffer
*from
)
552 struct buffer_data
*n
;
554 /* Copy the data pointer to the new buf. */
555 buf_append_data (to
, from
->data
, from
->last
);
560 if (n
== from
->last
) break;
564 /* Remove from the original location. */
572 * Copy the contents of file F into buffer_data structures. We can't
573 * copy directly into an buffer, because we want to handle failure and
574 * success differently. Returns 0 on success, or -2 if out of
575 * memory, or a status code on error. Since the caller happens to
576 * know the size of the file, it is passed in as SIZE. On success,
577 * this function sets *RETP and *LASTP, which may be passed to
581 buf_read_file (FILE *f
, long int size
, struct buffer_data
**retp
,
582 struct buffer_data
**lastp
)
591 struct buffer_data
*data
;
594 data
= get_buffer_data ();
604 (*lastp
)->next
= data
;
608 data
->bufp
= data
->text
;
611 if (size
> BUFFER_DATA_SIZE
)
612 get
= BUFFER_DATA_SIZE
;
617 if (fread (data
->text
, get
, 1, f
) != 1)
631 buf_free_datas (*retp
, (*lastp
)->next
);
638 * Copy the contents of file F into buffer_data structures. We can't
639 * copy directly into an buffer, because we want to handle failure and
640 * success differently. Returns 0 on success, or -2 if out of
641 * memory, or a status code on error. On success, this function sets
642 * *RETP and *LASTP, which may be passed to buf_append_data.
645 buf_read_file_to_eof (FILE *f
, struct buffer_data
**retp
,
646 struct buffer_data
**lastp
)
655 struct buffer_data
*data
;
658 data
= get_buffer_data ();
668 (*lastp
)->next
= data
;
672 data
->bufp
= data
->text
;
675 get
= BUFFER_DATA_SIZE
;
678 nread
= fread (data
->text
, 1, get
, f
);
679 if (nread
== 0 && !feof (f
))
692 buf_free_datas (*retp
, (*lastp
)->next
);
698 /* Return the number of bytes in a chain of buffer_data structures. */
700 buf_chain_length (struct buffer_data
*buf
)
713 /* Return the number of bytes in a buffer. */
715 buf_length (struct buffer
*buf
)
717 return buf_chain_length (buf
->data
);
723 * Read an arbitrary amount of data into an input buffer. The buffer
724 * will be in nonblocking mode, and we just grab what we can. Return
725 * 0 on success, or -1 on end of file, or -2 if out of memory, or an
726 * error code. If COUNTP is not NULL, *COUNTP is set to the number of
730 buf_input_data (struct buffer
*buf
, size_t *countp
)
732 assert (buf
->input
!= NULL
);
742 if (buf
->data
== NULL
743 || (buf
->last
->bufp
+ buf
->last
->size
744 == buf
->last
->text
+ BUFFER_DATA_SIZE
))
746 struct buffer_data
*data
;
748 data
= get_buffer_data ();
751 (*buf
->memory_error
) (buf
);
755 if (buf
->data
== NULL
)
758 buf
->last
->next
= data
;
762 data
->bufp
= data
->text
;
766 get
= ((buf
->last
->text
+ BUFFER_DATA_SIZE
)
767 - (buf
->last
->bufp
+ buf
->last
->size
));
769 status
= (*buf
->input
) (buf
->closure
,
770 buf
->last
->bufp
+ buf
->last
->size
,
775 buf
->last
->size
+= nbytes
;
781 /* If we did not fill the buffer, then presumably we read
782 all the available data. */
793 * Read a line (characters up to a \012) from an input buffer. (We
794 * use \012 rather than \n for the benefit of non Unix clients for
795 * which \n means something else). This returns 0 on success, or -1
796 * on end of file, or -2 if out of memory, or an error code. If it
797 * succeeds, it sets *LINE to an allocated buffer holding the contents
798 * of the line. The trailing \012 is not included in the buffer. If
799 * LENP is not NULL, then *LENP is set to the number of bytes read;
800 * strlen may not work, because there may be embedded null bytes.
803 buf_read_line (struct buffer
*buf
, char **line
, size_t *lenp
)
805 return buf_read_short_line (buf
, line
, lenp
, SIZE_MAX
);
810 /* Like buf_read_line, but return -2 if no newline is found in MAX characters.
813 buf_read_short_line (struct buffer
*buf
, char **line
, size_t *lenp
,
816 assert (buf
->input
!= NULL
);
822 size_t len
, finallen
, predicted_len
;
823 struct buffer_data
*data
;
826 /* See if there is a newline in BUF. */
828 for (data
= buf
->data
; data
!= NULL
; data
= data
->next
)
830 nl
= memchr (data
->bufp
, '\012', data
->size
);
833 finallen
= nl
- data
->bufp
;
834 if (xsum (len
, finallen
) >= max
) return -2;
838 else if (xsum (len
, data
->size
) >= max
) return -2;
842 /* If we found a newline, copy the line into a memory buffer,
843 and remove it from BUF. */
847 struct buffer_data
*nldata
;
849 p
= xmalloc (len
+ 1);
856 while (data
!= nldata
)
858 struct buffer_data
*next
;
860 memcpy (p
, data
->bufp
, data
->size
);
863 buf_free_datas (data
, data
);
867 memcpy (p
, data
->bufp
, finallen
);
870 data
->size
-= finallen
+ 1;
880 /* Read more data until we get a newline or MAX characters. */
888 if (buf
->data
== NULL
889 || (buf
->last
->bufp
+ buf
->last
->size
890 == buf
->last
->text
+ BUFFER_DATA_SIZE
))
892 data
= get_buffer_data ();
895 (*buf
->memory_error
) (buf
);
899 if (buf
->data
== NULL
)
902 buf
->last
->next
= data
;
906 data
->bufp
= data
->text
;
910 mem
= buf
->last
->bufp
+ buf
->last
->size
;
911 size
= (buf
->last
->text
+ BUFFER_DATA_SIZE
) - mem
;
913 /* We need to read at least 1 byte. We can handle up to
914 SIZE bytes. This will only be efficient if the
915 underlying communication stream does its own buffering,
916 or is clever about getting more than 1 byte at a time. */
917 status
= (*buf
->input
) (buf
->closure
, mem
, 1, size
, &nbytes
);
921 predicted_len
+= nbytes
;
922 buf
->last
->size
+= nbytes
;
924 /* Optimize slightly to avoid an unnecessary call to memchr. */
932 if (memchr (mem
, '\012', nbytes
) != NULL
)
935 if (xsum (len
, predicted_len
) >= max
) return -2;
943 * Extract data from the input buffer BUF. This will read up to WANT
944 * bytes from the buffer. It will set *RETDATA to point at the bytes,
945 * and set *GOT to the number of bytes to be found there. Any buffer
946 * call which uses BUF may change the contents of the buffer at *DATA,
947 * so the data should be fully processed before any further calls are
948 * made. This returns 0 on success, or -1 on end of file, or -2 if
949 * out of memory, or an error code.
952 buf_read_data (struct buffer
*buf
, size_t want
, char **retdata
, size_t *got
)
954 assert (buf
->input
!= NULL
);
956 while (buf
->data
!= NULL
&& buf
->data
->size
== 0)
958 struct buffer_data
*next
;
960 next
= buf
->data
->next
;
961 buf_free_datas (buf
->data
, buf
->data
);
967 if (buf
->data
== NULL
)
969 struct buffer_data
*data
;
973 data
= get_buffer_data ();
976 (*buf
->memory_error
) (buf
);
983 data
->bufp
= data
->text
;
986 if (want
< BUFFER_DATA_SIZE
)
989 get
= BUFFER_DATA_SIZE
;
990 status
= (*buf
->input
) (buf
->closure
, data
->bufp
, get
,
991 BUFFER_DATA_SIZE
, &nbytes
);
998 *retdata
= buf
->data
->bufp
;
999 if (want
< buf
->data
->size
)
1002 buf
->data
->size
-= want
;
1003 buf
->data
->bufp
+= want
;
1007 *got
= buf
->data
->size
;
1008 buf
->data
->size
= 0;
1017 * Copy lines from an input buffer to an output buffer.
1018 * This copies all complete lines (characters up to a
1019 * newline) from INBUF to OUTBUF. Each line in OUTBUF is preceded by the
1020 * character COMMAND and a space.
1023 buf_copy_lines (struct buffer
*outbuf
, struct buffer
*inbuf
, int command
)
1027 struct buffer_data
*data
;
1028 struct buffer_data
*nldata
;
1032 /* See if there is a newline in INBUF. */
1035 for (data
= inbuf
->data
; data
!= NULL
; data
= data
->next
)
1037 nl
= memchr (data
->bufp
, '\n', data
->size
);
1047 /* There are no more lines in INBUF. */
1051 /* Put in the command. */
1052 buf_append_char (outbuf
, command
);
1053 buf_append_char (outbuf
, ' ');
1055 if (inbuf
->data
!= nldata
)
1058 * Simply move over all the buffers up to the one containing
1061 for (data
= inbuf
->data
; data
->next
!= nldata
; data
= data
->next
);
1063 buf_append_data (outbuf
, inbuf
->data
, data
);
1064 inbuf
->data
= nldata
;
1068 * If the newline is at the very end of the buffer, just move
1069 * the buffer onto OUTBUF. Otherwise we must copy the data.
1071 len
= nl
+ 1 - nldata
->bufp
;
1072 if (len
== nldata
->size
)
1074 inbuf
->data
= nldata
->next
;
1075 if (inbuf
->data
== NULL
)
1078 nldata
->next
= NULL
;
1079 buf_append_data (outbuf
, nldata
, nldata
);
1083 buf_output (outbuf
, nldata
->bufp
, len
);
1084 nldata
->bufp
+= len
;
1085 nldata
->size
-= len
;
1093 * Copy counted data from one buffer to another. The count is an
1094 * integer, host size, host byte order (it is only used across a
1095 * pipe). If there is enough data, it should be moved over. If there
1096 * is not enough data, it should remain on the original buffer. A
1097 * negative count is a special case. if one is seen, *SPECIAL is set
1098 * to the (negative) count value and no additional data is gathered
1099 * from the buffer; normally *SPECIAL is set to 0. This function
1100 * returns the number of bytes it needs to see in order to actually
1101 * copy something over.
1104 buf_copy_counted (struct buffer
*outbuf
, struct buffer
*inbuf
, int *special
)
1110 struct buffer_data
*data
;
1114 char intbuf
[sizeof (int)];
1119 struct buffer_data
*start
;
1121 struct buffer_data
*stop
;
1124 /* See if we have enough bytes to figure out the count. */
1125 need
= sizeof (int);
1127 for (data
= inbuf
->data
; data
!= NULL
; data
= data
->next
)
1129 if (data
->size
>= need
)
1131 memcpy (intp
, data
->bufp
, need
);
1134 memcpy (intp
, data
->bufp
, data
->size
);
1140 /* We don't have enough bytes to form an integer. */
1150 /* A negative COUNT is a special case meaning that we
1151 don't need any further information. */
1158 * We have an integer in COUNT. We have gotten all the
1159 * data from INBUF in all buffers before START, and we
1160 * have gotten STARTOFF bytes from START. See if we have
1161 * enough bytes remaining in INBUF.
1163 need
= count
- (start
->size
- startoff
);
1171 for (data
= start
->next
; data
!= NULL
; data
= data
->next
)
1173 if (need
<= data
->size
)
1179 /* We don't have enough bytes. */
1188 * We have enough bytes. Free any buffers in INBUF before
1189 * START, and remove STARTOFF bytes from START, so that we can
1190 * forget about STARTOFF.
1192 start
->bufp
+= startoff
;
1193 start
->size
-= startoff
;
1195 if (start
->size
== 0)
1196 start
= start
->next
;
1198 if (stop
->size
== stopwant
)
1204 while (inbuf
->data
!= start
)
1207 inbuf
->data
= data
->next
;
1208 buf_free_datas (data
, data
);
1211 /* If COUNT is negative, set *SPECIAL and get out now. */
1219 * We want to copy over the bytes from START through STOP. We
1220 * only want STOPWANT bytes from STOP.
1225 /* Attach the buffers from START through STOP to OUTBUF. */
1226 for (data
= start
; data
->next
!= stop
; data
= data
->next
);
1229 buf_append_data (outbuf
, start
, data
);
1234 buf_output (outbuf
, stop
->bufp
, stopwant
);
1235 stop
->bufp
+= stopwant
;
1236 stop
->size
-= stopwant
;
1246 buf_get_fd (struct buffer
*buf
)
1249 return (*buf
->get_fd
) (buf
->closure
);
1255 /* Shut down a buffer. This returns 0 on success, or an errno code. */
1257 buf_shutdown (struct buffer
*buf
)
1259 if (buf
->shutdown
) return (*buf
->shutdown
) (buf
);
1265 /* Certain types of communication input and output data in packets,
1266 where each packet is translated in some fashion. The packetizing
1267 buffer type supports that, given a buffer which handles lower level
1268 I/O and a routine to translate the data in a packet.
1270 This code uses two bytes for the size of a packet, so packets are
1271 restricted to 65536 bytes in total.
1273 The translation functions should just translate; they may not
1274 significantly increase or decrease the amount of data. The actual
1275 size of the initial data is part of the translated data. The
1276 output translation routine may add up to PACKET_SLOP additional
1277 bytes, and the input translation routine should shrink the data
1280 # define PACKET_SLOP (100)
1282 /* This structure is the closure field of a packetizing buffer. */
1284 struct packetizing_buffer
1286 /* The underlying buffer. */
1288 /* The input translation function. Exactly one of inpfn and outfn
1289 will be NULL. The input translation function should
1290 untranslate the data in INPUT, storing the result in OUTPUT.
1291 SIZE is the amount of data in INPUT, and is also the size of
1292 OUTPUT. This should return 0 on success, or an errno code. */
1293 int (*inpfn
) (void *fnclosure
, const char *input
, char *output
,
1295 /* The output translation function. This should translate the
1296 data in INPUT, storing the result in OUTPUT. The first two
1297 bytes in INPUT will be the size of the data, and so will SIZE.
1298 This should set *TRANSLATED to the amount of translated data in
1299 OUTPUT. OUTPUT is large enough to hold SIZE + PACKET_SLOP
1300 bytes. This should return 0 on success, or an errno code. */
1301 int (*outfn
) (void *fnclosure
, const char *input
, char *output
,
1302 size_t size
, size_t *translated
);
1303 /* A closure for the translation function. */
1305 /* For an input buffer, we may have to buffer up data here. */
1306 /* This is non-zero if the buffered data has been translated.
1307 Otherwise, the buffered data has not been translated, and starts
1308 with the two byte packet size. */
1310 /* The amount of buffered data. */
1312 /* The buffer allocated to hold the data. */
1314 /* The size of holdbuf. */
1316 /* If translated is set, we need another data pointer to track
1317 where we are in holdbuf. If translated is clear, then this
1318 pointer is not used. */
1324 static int packetizing_buffer_input (void *, char *, size_t, size_t, size_t *);
1325 static int packetizing_buffer_output (void *, const char *, size_t, size_t *);
1326 static int packetizing_buffer_flush (void *);
1327 static int packetizing_buffer_block (void *, bool);
1328 static int packetizing_buffer_get_fd (void *);
1329 static int packetizing_buffer_shutdown (struct buffer
*);
1333 /* Create a packetizing buffer. */
1335 packetizing_buffer_initialize (struct buffer
*buf
,
1336 int (*inpfn
) (void *, const char *, char *,
1338 int (*outfn
) (void *, const char *, char *,
1341 void (*memory
) (struct buffer
*))
1343 struct packetizing_buffer
*pb
;
1345 pb
= xmalloc (sizeof *pb
);
1346 memset (pb
, 0, sizeof *pb
);
1351 pb
->fnclosure
= fnclosure
;
1355 /* Add PACKET_SLOP to handle larger translated packets, and
1356 add 2 for the count. This buffer is increased if
1358 pb
->holdbufsize
= BUFFER_DATA_SIZE
+ PACKET_SLOP
+ 2;
1359 pb
->holdbuf
= xmalloc (pb
->holdbufsize
);
1362 return buf_initialize (inpfn
!= NULL
? packetizing_buffer_input
: NULL
,
1363 inpfn
!= NULL
? NULL
: packetizing_buffer_output
,
1364 inpfn
!= NULL
? NULL
: packetizing_buffer_flush
,
1365 packetizing_buffer_block
,
1366 packetizing_buffer_get_fd
,
1367 packetizing_buffer_shutdown
,
1374 /* Input data from a packetizing buffer. */
1376 packetizing_buffer_input (void *closure
, char *data
, size_t need
, size_t size
,
1379 struct packetizing_buffer
*pb
= closure
;
1383 if (pb
->holdsize
> 0 && pb
->translated
)
1387 copy
= pb
->holdsize
;
1391 memcpy (data
, pb
->holddata
, size
);
1392 pb
->holdsize
-= size
;
1393 pb
->holddata
+= size
;
1398 memcpy (data
, pb
->holddata
, copy
);
1400 pb
->translated
= false;
1408 while (need
> 0 || *got
== 0)
1411 size_t get
, nread
, count
, tcount
;
1413 static char *stackoutbuf
= NULL
;
1414 char *inbuf
, *outbuf
;
1417 stackoutbuf
= xmalloc (BUFFER_DATA_SIZE
+ PACKET_SLOP
);
1419 /* If we don't already have the two byte count, get it. */
1420 if (pb
->holdsize
< 2)
1422 get
= 2 - pb
->holdsize
;
1423 status
= buf_read_data (pb
->buf
, get
, &bytes
, &nread
);
1426 /* buf_read_data can return -2, but a buffer input
1427 function is only supposed to return -1, 0, or an
1436 /* The buffer is in nonblocking mode, and we didn't
1437 manage to read anything. */
1442 pb
->holdbuf
[1] = bytes
[0];
1445 pb
->holdbuf
[0] = bytes
[0];
1448 /* We only got one byte, but we needed two. Stash
1449 the byte we got, and try again. */
1453 pb
->holdbuf
[1] = bytes
[1];
1458 /* Read the packet. */
1460 count
= (((pb
->holdbuf
[0] & 0xff) << 8)
1461 + (pb
->holdbuf
[1] & 0xff));
1463 if (count
+ 2 > pb
->holdbufsize
)
1467 /* We didn't allocate enough space in the initialize
1470 n
= xrealloc (pb
->holdbuf
, count
+ 2);
1473 (*pb
->buf
->memory_error
) (pb
->buf
);
1477 pb
->holdbufsize
= count
+ 2;
1480 get
= count
- (pb
->holdsize
- 2);
1482 status
= buf_read_data (pb
->buf
, get
, &bytes
, &nread
);
1485 /* buf_read_data can return -2, but a buffer input
1486 function is only supposed to return -1, 0, or an error
1495 /* We did not get any data. Presumably the buffer is in
1496 nonblocking mode. */
1502 /* We did not get all the data we need to fill the packet.
1503 buf_read_data does not promise to return all the bytes
1504 requested, so we must try again. */
1505 memcpy (pb
->holdbuf
+ pb
->holdsize
, bytes
, nread
);
1506 pb
->holdsize
+= nread
;
1510 /* We have a complete untranslated packet of COUNT bytes. */
1512 if (pb
->holdsize
== 2)
1514 /* We just read the entire packet (the 2 bytes in
1515 PB->HOLDBUF are the size). Save a memcpy by
1516 translating directly from BYTES. */
1521 /* We already had a partial packet in PB->HOLDBUF. We
1522 need to copy the new data over to make the input
1524 memcpy (pb
->holdbuf
+ pb
->holdsize
, bytes
, nread
);
1525 inbuf
= pb
->holdbuf
+ 2;
1528 if (count
<= BUFFER_DATA_SIZE
+ PACKET_SLOP
)
1529 outbuf
= stackoutbuf
;
1532 outbuf
= xmalloc (count
);
1535 (*pb
->buf
->memory_error
) (pb
->buf
);
1540 status
= (*pb
->inpfn
) (pb
->fnclosure
, inbuf
, outbuf
, count
);
1544 /* The first two bytes in the translated buffer are the real
1545 length of the translated data. */
1546 tcount
= ((outbuf
[0] & 0xff) << 8) + (outbuf
[1] & 0xff);
1549 error (1, 0, "Input translation failure");
1553 /* We have more data than the caller has provided space
1554 for. We need to save some of it for the next call. */
1556 memcpy (data
, outbuf
+ 2, size
);
1559 pb
->holdsize
= tcount
- size
;
1560 memcpy (pb
->holdbuf
, outbuf
+ 2 + size
, tcount
- size
);
1561 pb
->holddata
= pb
->holdbuf
;
1562 pb
->translated
= true;
1564 if (outbuf
!= stackoutbuf
)
1570 memcpy (data
, outbuf
+ 2, tcount
);
1572 if (outbuf
!= stackoutbuf
)
1588 /* Output data to a packetizing buffer. */
1590 packetizing_buffer_output (void *closure
, const char *data
, size_t have
,
1593 struct packetizing_buffer
*pb
= closure
;
1594 static char *inbuf
= NULL
; /* These two buffers are static so that they
1595 * depend on the size of BUFFER_DATA_SIZE yet
1596 * still only be allocated once per run.
1598 static char *stack_outbuf
= NULL
;
1599 struct buffer_data
*outdata
= NULL
; /* Initialize to silence -Wall. Dumb.
1602 size_t size
, translated
;
1605 /* It would be easy to xmalloc a buffer, but I don't think this
1606 case can ever arise. */
1607 assert (have
<= BUFFER_DATA_SIZE
);
1611 inbuf
= xmalloc (BUFFER_DATA_SIZE
+ 2);
1612 stack_outbuf
= xmalloc (BUFFER_DATA_SIZE
+ PACKET_SLOP
+ 4);
1615 inbuf
[0] = (have
>> 8) & 0xff;
1616 inbuf
[1] = have
& 0xff;
1617 memcpy (inbuf
+ 2, data
, have
);
1621 /* The output function is permitted to add up to PACKET_SLOP
1622 bytes, and we need 2 bytes for the size of the translated data.
1623 If we can guarantee that the result will fit in a buffer_data,
1624 we translate directly into one to avoid a memcpy in buf_output. */
1625 if (size
+ PACKET_SLOP
+ 2 > BUFFER_DATA_SIZE
)
1626 outbuf
= stack_outbuf
;
1629 outdata
= get_buffer_data ();
1630 if (outdata
== NULL
)
1632 (*pb
->buf
->memory_error
) (pb
->buf
);
1636 outdata
->next
= NULL
;
1637 outdata
->bufp
= outdata
->text
;
1639 outbuf
= outdata
->text
;
1642 status
= (*pb
->outfn
) (pb
->fnclosure
, inbuf
, outbuf
+ 2, size
,
1647 /* The output function is permitted to add up to PACKET_SLOP
1649 assert (translated
<= size
+ PACKET_SLOP
);
1651 outbuf
[0] = (translated
>> 8) & 0xff;
1652 outbuf
[1] = translated
& 0xff;
1654 if (outbuf
== stack_outbuf
)
1655 buf_output (pb
->buf
, outbuf
, translated
+ 2);
1658 outdata
->size
= translated
+ 2;
1659 buf_append_data (pb
->buf
, outdata
, outdata
);
1664 /* We will only be here because buf_send_output was called on the
1665 packetizing buffer. That means that we should now call
1666 buf_send_output on the underlying buffer. */
1667 return buf_send_output (pb
->buf
);
1672 /* Flush data to a packetizing buffer. */
1674 packetizing_buffer_flush (void *closure
)
1676 struct packetizing_buffer
*pb
= closure
;
1678 /* Flush the underlying buffer. Note that if the original call to
1679 buf_flush passed 1 for the BLOCK argument, then the buffer will
1680 already have been set into blocking mode, so we should always
1682 return buf_flush (pb
->buf
, 0);
1687 /* The block routine for a packetizing buffer. */
1689 packetizing_buffer_block (void *closure
, bool block
)
1691 struct packetizing_buffer
*pb
= closure
;
1694 return set_block (pb
->buf
);
1696 return set_nonblock (pb
->buf
);
1701 /* Return the file descriptor underlying any child buffers. */
1703 packetizing_buffer_get_fd (void *closure
)
1705 struct packetizing_buffer
*cb
= closure
;
1706 return buf_get_fd (cb
->buf
);
1711 /* Shut down a packetizing buffer. */
1713 packetizing_buffer_shutdown (struct buffer
*buf
)
1715 struct packetizing_buffer
*pb
= buf
->closure
;
1717 return buf_shutdown (pb
->buf
);
1722 /* All server communication goes through buffer structures. Most of
1723 the buffers are built on top of a file descriptor. This structure
1724 is used as the closure field in a buffer. */
1728 /* The file descriptor. */
1730 /* Nonzero if the file descriptor is in blocking mode. */
1732 /* The child process id when fd is a pipe. */
1734 /* The connection info, when fd is a pipe to a server. */
1738 static int fd_buffer_input (void *, char *, size_t, size_t, size_t *);
1739 static int fd_buffer_output (void *, const char *, size_t, size_t *);
1740 static int fd_buffer_flush (void *);
1741 static int fd_buffer_block (void *, bool);
1742 static int fd_buffer_get_fd (void *);
1743 static int fd_buffer_shutdown (struct buffer
*);
1745 /* Initialize a buffer built on a file descriptor. FD is the file
1746 descriptor. INPUT is nonzero if this is for input, zero if this is
1747 for output. MEMORY is the function to call when a memory error
1751 fd_buffer_initialize (int fd
, pid_t child_pid
, cvsroot_t
*root
, bool input
,
1752 void (*memory
) (struct buffer
*))
1754 struct fd_buffer
*n
;
1756 n
= xmalloc (sizeof *n
);
1758 n
->child_pid
= child_pid
;
1760 fd_buffer_block (n
, true);
1761 return buf_initialize (input
? fd_buffer_input
: NULL
,
1762 input
? NULL
: fd_buffer_output
,
1763 input
? NULL
: fd_buffer_flush
,
1764 fd_buffer_block
, fd_buffer_get_fd
,
1772 /* The buffer input function for a buffer built on a file descriptor.
1774 * In non-blocking mode, this function will read as many bytes as it can in a
1775 * single try, up to SIZE bytes, and return.
1777 * In blocking mode with NEED > 0, this function will read as many bytes as it
1778 * can but will not return until it has read at least NEED bytes.
1780 * In blocking mode with NEED == 0, this function will block until it can read
1781 * either at least one byte or EOF, then read as many bytes as are available
1782 * and return. At the very least, compress_buffer_shutdown depends on this
1783 * behavior to read EOF and can loop indefinitely without it.
1789 * closure Our FD_BUFFER struct.
1790 * data The start of our input buffer.
1791 * need How many bytes our caller needs.
1792 * size How many bytes are available in DATA.
1793 * got Where to store the number of bytes read.
1796 * data Filled with bytes read.
1797 * *got Number of bytes actually read into DATA.
1805 * This function can return an error if fd_buffer_block(), or the system
1806 * read() or select() calls do.
1809 fd_buffer_input (void *closure
, char *data
, size_t need
, size_t size
,
1812 struct fd_buffer
*fb
= closure
;
1815 assert (need
<= size
);
1824 /* Set non-block. */
1825 status
= fd_buffer_block (fb
, false);
1826 if (status
!= 0) return status
;
1829 FD_SET (fb
->fd
, &readfds
);
1835 /* This used to select on exceptions too, but as far
1836 as I know there was never any reason to do that and
1837 SCO doesn't let you select on exceptions on pipes. */
1838 numfds
= fd_select (fb
->fd
+ 1, &readfds
, NULL
, NULL
, NULL
);
1839 if (numfds
< 0 && errno
!= EINTR
)
1844 } while (numfds
< 0);
1846 nbytes
= read (fb
->fd
, data
+ *got
, size
- *got
);
1850 /* End of file. This assumes that we are using POSIX or BSD
1851 style nonblocking I/O. On System V we will get a zero
1852 return if there is no data, even when not at EOF. */
1855 /* We already read some data, so return no error, counting
1856 * on the fact that we will read EOF again next time.
1871 /* Some error occurred. */
1872 if (!blocking_error (errno
))
1877 /* else Everything's fine, we just didn't get any data. */
1881 } while (*got
< need
);
1884 if (status
== 0 || status
== -1)
1888 /* OK or EOF - Reset block. */
1889 newstatus
= fd_buffer_block (fb
, true);
1890 if (newstatus
) status
= newstatus
;
1895 /* The above will always return. Handle non-blocking read. */
1896 nbytes
= read (fb
->fd
, data
, size
);
1905 /* End of file. This assumes that we are using POSIX or BSD
1906 style nonblocking I/O. On System V we will get a zero
1907 return if there is no data, even when not at EOF. */
1910 /* Some error occurred. */
1911 if (blocking_error (errno
))
1912 /* Everything's fine, we just didn't get any data. */
1920 /* The buffer output function for a buffer built on a file descriptor. */
1923 fd_buffer_output (void *closure
, const char *data
, size_t have
, size_t *wrote
)
1925 struct fd_buffer
*fd
= closure
;
1933 nbytes
= write (fd
->fd
, data
, have
);
1938 && (nbytes
== 0 || blocking_error (errno
)))
1940 /* A nonblocking write failed to write any data. Just
1945 /* Some sort of error occurred. */
1963 /* The buffer flush function for a buffer built on a file descriptor. */
1965 fd_buffer_flush (void *closure
)
1967 /* We don't need to do anything here. Our fd doesn't have its own buffer
1968 * and syncing won't do anything but slow us down.
1970 * struct fd_buffer *fb = closure;
1972 * if (fsync (fb->fd) < 0 && errno != EROFS && errno != EINVAL)
1980 static struct stat devnull
;
1981 static int devnull_set
= -1;
1983 /* The buffer block function for a buffer built on a file descriptor. */
1985 fd_buffer_block (void *closure
, bool block
)
1987 struct fd_buffer
*fb
= closure
;
1988 # if defined (F_GETFL) && defined (O_NONBLOCK) && defined (F_SETFL)
1991 flags
= fcntl (fb
->fd
, F_GETFL
, 0);
1996 flags
&= ~O_NONBLOCK
;
1998 flags
|= O_NONBLOCK
;
2000 if (fcntl (fb
->fd
, F_SETFL
, flags
) < 0)
2003 * BSD returns ENODEV when we try to set block/nonblock on /dev/null.
2004 * BSDI returns ENOTTY when we try to set block/nonblock on /dev/null.
2007 int save_errno
= errno
;
2008 bool isdevnull
= false;
2010 if (devnull_set
== -1)
2011 devnull_set
= stat ("/dev/null", &devnull
);
2013 if (devnull_set
>= 0)
2014 /* Equivalent to /dev/null ? */
2015 isdevnull
= (fstat (fb
->fd
, &sb
) >= 0
2016 && sb
.st_dev
== devnull
.st_dev
2017 && sb
.st_ino
== devnull
.st_ino
2018 && sb
.st_mode
== devnull
.st_mode
2019 && sb
.st_uid
== devnull
.st_uid
2020 && sb
.st_gid
== devnull
.st_gid
2021 && sb
.st_size
== devnull
.st_size
2022 && sb
.st_blocks
== devnull
.st_blocks
2023 && sb
.st_blksize
== devnull
.st_blksize
);
2032 # endif /* F_GETFL && O_NONBLOCK && F_SETFL */
2034 fb
->blocking
= block
;
2042 fd_buffer_get_fd (void *closure
)
2044 struct fd_buffer
*fb
= closure
;
2050 /* The buffer shutdown function for a buffer built on a file descriptor.
2052 * This function disposes of memory allocated for this buffer.
2055 fd_buffer_shutdown (struct buffer
*buf
)
2057 struct fd_buffer
*fb
= buf
->closure
;
2059 bool closefd
, statted
;
2061 /* Must be an open pipe, socket, or file. What could go wrong? */
2062 if (fstat (fb
->fd
, &s
) == -1) statted
= false;
2063 else statted
= true;
2064 /* Don't bother to try closing the FD if we couldn't stat it. This
2065 * probably won't work.
2067 * (buf_shutdown() on some of the server/child communication pipes is
2068 * getting EBADF on both the fstat and the close. I'm not sure why -
2069 * perhaps they were alredy closed somehow?
2073 /* Flush the buffer if possible. */
2082 /* There used to be a check here for unread data in the buffer of
2083 * the pipe, but it was deemed unnecessary and possibly dangerous. In
2084 * some sense it could be second-guessing the caller who requested it
2090 * This mess of #ifdefs is hard to read. There must be some relation between
2091 * the macros being checked which at least deserves comments - if
2092 * SHUTDOWN_SERVER, NO_SOCKET_TO_FD, & START_RSH_WITH_POPEN_RW were completely
2093 * independant, then the next few lines could easily refuse to compile.
2095 * The note below about START_RSH_WITH_POPEN_RW never being set when
2096 * SHUTDOWN_SERVER is defined means that this code would always break on
2097 * systems with SHUTDOWN_SERVER defined and thus the comment must now be
2098 * incorrect or the code was broken since the comment was written.
2100 # ifdef SHUTDOWN_SERVER
2101 if (fb
->root
&& fb
->root
->method
!= server_method
)
2103 # ifndef NO_SOCKET_TO_FD
2105 /* shutdown() sockets */
2106 if (statted
&& S_ISSOCK (s
.st_mode
))
2107 shutdown (fb
->fd
, 0);
2109 # endif /* NO_SOCKET_TO_FD */
2110 # ifdef START_RSH_WITH_POPEN_RW
2111 /* Can't be set with SHUTDOWN_SERVER defined */
2112 /* FIXME: This is now certainly broken since pclose is defined by ANSI
2113 * C to accept a FILE * argument. The switch will need to happen at a
2114 * higher abstraction level to switch between initializing stdio & fd
2115 * buffers on systems that need this (or maybe an fd buffer that keeps
2116 * track of the FILE * could be used - I think flushing the stream
2117 * before beginning exclusive access via the FD is OK.
2119 else if (fb
->root
&& pclose (fb
->fd
) == EOF
)
2121 error (1, errno
, "closing connection to %s",
2122 fb
->root
->hostname
);
2125 # endif /* START_RSH_WITH_POPEN_RW */
2129 else if (buf
->output
)
2131 # ifdef SHUTDOWN_SERVER
2132 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
2133 * SHUTDOWN_SERVER_OUTPUT
2135 if (fb
->root
&& fb
->root
->method
== server_method
)
2136 SHUTDOWN_SERVER (fb
->fd
);
2139 # ifndef NO_SOCKET_TO_FD
2140 /* shutdown() sockets */
2141 if (statted
&& S_ISSOCK (s
.st_mode
))
2142 shutdown (fb
->fd
, 1);
2145 /* I'm not sure I like this empty block, but the alternative
2146 * is another nested NO_SOCKET_TO_FD switch as above.
2149 # endif /* NO_SOCKET_TO_FD */
2154 if (statted
&& closefd
&& close (fb
->fd
) == -1)
2160 # ifdef CLIENT_SUPPORT
2162 error (1, errno
, "closing down connection to %s",
2163 fb
->root
->hostname
);
2165 # endif /* CLIENT_SUPPORT */
2167 error (0, errno
, "closing down buffer");
2170 /* If we were talking to a process, make sure it exited */
2176 w
= waitpid (fb
->child_pid
, NULL
, 0);
2177 while (w
== -1 && errno
== EINTR
);
2179 error (1, errno
, "waiting for process %d", fb
->child_pid
);
2182 free (buf
->closure
);
2183 buf
->closure
= NULL
;
2187 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */