kernel: Remove the FFS_ROOT option. It was a no-op since 4.9.
[dragonfly.git] / contrib / cvs-1.12 / src / buffer.c
blob365d91c1062974c5f2f8077bb139bfc45e41b867
1 /*
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)
7 * any later version.
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. */
17 #include "cvs.h"
18 #include "buffer.h"
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. */
27 # if !defined( EIO )
28 # define EIO EBADPOS
29 # endif
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. */
38 struct buffer *
39 buf_initialize (type_buf_input input,
40 type_buf_output output,
41 type_buf_flush flush,
42 type_buf_block block,
43 type_buf_get_fd get_fd,
44 type_buf_shutdown shutdown,
45 type_buf_memory_error memory_error,
46 void *closure)
48 struct buffer *buf;
50 buf = xmalloc (sizeof (struct buffer));
51 buf->data = NULL;
52 buf->last = NULL;
53 buf->nonblocking = false;
54 buf->input = input;
55 buf->output = output;
56 buf->flush = flush;
57 buf->block = block;
58 buf->get_fd = get_fd;
59 buf->shutdown = shutdown;
60 buf->memory_error = memory_error ? memory_error : buf_default_memory_error;
61 buf->closure = closure;
62 return buf;
67 /* Free a buffer structure. */
68 void
69 buf_free (struct buffer *buf)
71 if (buf->closure != NULL)
73 free (buf->closure);
74 buf->closure = NULL;
76 buf_free_data (buf);
77 free (buf);
82 /* Initialize a buffer structure which is not to be used for I/O. */
83 struct buffer *
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. */
92 static void
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);
110 return ret;
115 /* See whether a buffer and its file descriptor is empty. */
117 buf_empty (buf)
118 struct buffer *buf;
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)
136 if (data->size > 0)
137 return 0;
138 return 1;
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;
152 int mem = 0;
154 for (data = buf->data; data != NULL; data = data->next)
155 mem += BUFFER_DATA_SIZE;
157 return mem;
159 # endif /* SERVER_FLOWCONTROL || PROXY_SUPPORT */
163 /* Add data DATA of length LEN to BUF. */
164 void
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))
170 >= len))
172 memcpy (buf->last->bufp + buf->last->size, data, len);
173 buf->last->size += len;
174 return;
177 while (1)
179 struct buffer_data *newdata;
181 newdata = get_buffer_data ();
182 if (newdata == NULL)
184 (*buf->memory_error) (buf);
185 return;
188 if (buf->data == NULL)
189 buf->data = newdata;
190 else
191 buf->last->next = newdata;
192 newdata->next = NULL;
193 buf->last = newdata;
195 newdata->bufp = newdata->text;
197 if (len <= BUFFER_DATA_SIZE)
199 newdata->size = len;
200 memcpy (newdata->text, data, len);
201 return;
204 newdata->size = BUFFER_DATA_SIZE;
205 memcpy (newdata->text, data, BUFFER_DATA_SIZE);
207 data += BUFFER_DATA_SIZE;
208 len -= BUFFER_DATA_SIZE;
211 /*NOTREACHED*/
216 /* Add a '\0' terminated string to BUF. */
217 void
218 buf_output0 (struct buffer *buf, const char *string)
220 buf_output (buf, string, strlen (string));
225 /* Add a single character to BUF. */
226 void
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;
234 ++buf->last->size;
236 else
238 char b;
240 b = ch;
241 buf_output (buf, &b, 1);
247 /* Free struct buffer_data's from the list starting with FIRST and ending at
248 * LAST, inclusive.
250 static inline void
251 buf_free_datas (struct buffer_data *first, struct buffer_data *last)
253 struct buffer_data *b, *n, *p;
254 b = first;
257 p = b;
258 n = b->next;
259 pagealign_free (b->text);
260 free (b);
261 b = n;
262 } while (p != last);
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;
281 data = buf->data;
283 if (data->size > 0)
285 int status;
286 size_t nbytes;
288 status = (*buf->output) (buf->closure, data->bufp, data->size,
289 &nbytes);
290 if (status != 0)
292 /* Some sort of error. Discard the data, and return. */
293 buf_free_data (buf);
294 return status;
297 if (nbytes != data->size)
299 /* Not all the data was written out. This is only
300 permitted in nonblocking mode. Adjust the buffer,
301 and return. */
303 assert (buf->nonblocking);
305 data->size -= nbytes;
306 data->bufp += nbytes;
308 return 0;
312 buf->data = data->next;
313 buf_free_datas (data, data);
316 buf->last = NULL;
318 return 0;
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
327 * code.
330 buf_flush (struct buffer *buf, bool block)
332 int nonblocking;
333 int status;
335 assert (buf->flush != NULL);
337 nonblocking = buf->nonblocking;
338 if (nonblocking && block)
340 status = set_block (buf);
341 if (status != 0)
342 return status;
345 status = buf_send_output (buf);
346 if (status == 0)
347 status = (*buf->flush) (buf->closure);
349 if (nonblocking && block)
351 int blockstat;
353 blockstat = set_nonblock (buf);
354 if (status == 0)
355 status = blockstat;
358 return status;
364 * Set buffer BUF to nonblocking I/O. Returns 0 for success or errno
365 * code.
368 set_nonblock (struct buffer *buf)
370 int status;
372 if (buf->nonblocking)
373 return 0;
374 assert (buf->block != NULL);
375 status = (*buf->block) (buf->closure, 0);
376 if (status != 0)
377 return status;
378 buf->nonblocking = true;
379 return 0;
385 * Set buffer BUF to blocking I/O. Returns 0 for success or errno
386 * code.
389 set_block (struct buffer *buf)
391 int status;
393 if (! buf->nonblocking)
394 return 0;
395 assert (buf->block != NULL);
396 status = (*buf->block) (buf->closure, 1);
397 if (status != 0)
398 return status;
399 buf->nonblocking = false;
400 return 0;
406 * Send a character count and some output. Returns errno code or 0 for
407 * success.
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)
415 int size;
416 struct buffer_data *data;
418 size = 0;
419 for (data = buf->data; data != NULL; data = data->next)
420 size += data->size;
422 data = get_buffer_data ();
423 if (data == NULL)
425 (*buf->memory_error) (buf);
426 return ENOMEM;
429 data->next = buf->data;
430 buf->data = data;
431 if (buf->last == NULL)
432 buf->last = data;
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
447 * an errno code.
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 ();
458 if (data == NULL)
460 (*buf->memory_error) (buf);
461 return ENOMEM;
464 data->next = buf->data;
465 buf->data = data;
466 if (buf->last == NULL)
467 buf->last = data;
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. */
480 void
481 buf_append_data (struct buffer *buf, struct buffer_data *data,
482 struct buffer_data *last)
484 if (data != NULL)
486 if (buf->data == NULL)
487 buf->data = data;
488 else
489 buf->last->next = data;
490 buf->last = last;
496 # ifdef PROXY_SUPPORT
497 /* Copy data structures and append them to a buffer.
499 * ERRORS
500 * Failure to allocate memory here is fatal.
502 void
503 buf_copy_data (struct buffer *buf, struct buffer_data *data,
504 struct buffer_data *last)
506 struct buffer_data *first, *new, *cur, *prev;
508 assert (buf);
509 assert (data);
511 prev = first = NULL;
512 cur = data;
513 while (1)
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;
522 new->next = NULL;
523 if (prev) prev->next = new;
524 if (cur == last) break;
525 prev = new;
526 cur = cur->next;
529 buf_append_data (buf, first, new);
531 # endif /* PROXY_SUPPORT */
535 /* Dispose of any remaining data in the buffer. */
536 void
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.
549 void
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);
557 n = from->data;
558 while (n)
560 if (n == from->last) break;
561 n = n->next;
564 /* Remove from the original location. */
565 from->data = NULL;
566 from->last = NULL;
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
578 * buf_append_data.
581 buf_read_file (FILE *f, long int size, struct buffer_data **retp,
582 struct buffer_data **lastp)
584 int status;
586 *retp = NULL;
587 *lastp = NULL;
589 while (size > 0)
591 struct buffer_data *data;
592 int get;
594 data = get_buffer_data ();
595 if (data == NULL)
597 status = -2;
598 goto error_return;
601 if (*retp == NULL)
602 *retp = data;
603 else
604 (*lastp)->next = data;
605 data->next = NULL;
606 *lastp = data;
608 data->bufp = data->text;
609 data->size = 0;
611 if (size > BUFFER_DATA_SIZE)
612 get = BUFFER_DATA_SIZE;
613 else
614 get = size;
616 errno = EIO;
617 if (fread (data->text, get, 1, f) != 1)
619 status = errno;
620 goto error_return;
623 data->size += get;
624 size -= get;
627 return 0;
629 error_return:
630 if (*retp != NULL)
631 buf_free_datas (*retp, (*lastp)->next);
632 return status;
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)
648 int status;
650 *retp = NULL;
651 *lastp = NULL;
653 while (!feof (f))
655 struct buffer_data *data;
656 int get, nread;
658 data = get_buffer_data ();
659 if (data == NULL)
661 status = -2;
662 goto error_return;
665 if (*retp == NULL)
666 *retp = data;
667 else
668 (*lastp)->next = data;
669 data->next = NULL;
670 *lastp = data;
672 data->bufp = data->text;
673 data->size = 0;
675 get = BUFFER_DATA_SIZE;
677 errno = EIO;
678 nread = fread (data->text, 1, get, f);
679 if (nread == 0 && !feof (f))
681 status = errno;
682 goto error_return;
685 data->size = nread;
688 return 0;
690 error_return:
691 if (*retp != NULL)
692 buf_free_datas (*retp, (*lastp)->next);
693 return status;
698 /* Return the number of bytes in a chain of buffer_data structures. */
700 buf_chain_length (struct buffer_data *buf)
702 int size = 0;
703 while (buf)
705 size += buf->size;
706 buf = buf->next;
708 return size;
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
727 * bytes read.
730 buf_input_data (struct buffer *buf, size_t *countp)
732 assert (buf->input != NULL);
734 if (countp != NULL)
735 *countp = 0;
737 while (1)
739 int status;
740 size_t get, nbytes;
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 ();
749 if (data == NULL)
751 (*buf->memory_error) (buf);
752 return -2;
755 if (buf->data == NULL)
756 buf->data = data;
757 else
758 buf->last->next = data;
759 data->next = NULL;
760 buf->last = data;
762 data->bufp = data->text;
763 data->size = 0;
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,
771 0, get, &nbytes);
772 if (status != 0)
773 return status;
775 buf->last->size += nbytes;
776 if (countp != NULL)
777 *countp += nbytes;
779 if (nbytes < get)
781 /* If we did not fill the buffer, then presumably we read
782 all the available data. */
783 return 0;
787 /*NOTREACHED*/
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,
814 size_t max)
816 assert (buf->input != NULL);
818 *line = NULL;
820 while (1)
822 size_t len, finallen, predicted_len;
823 struct buffer_data *data;
824 char *nl;
826 /* See if there is a newline in BUF. */
827 len = 0;
828 for (data = buf->data; data != NULL; data = data->next)
830 nl = memchr (data->bufp, '\012', data->size);
831 if (nl != NULL)
833 finallen = nl - data->bufp;
834 if (xsum (len, finallen) >= max) return -2;
835 len += finallen;
836 break;
838 else if (xsum (len, data->size) >= max) return -2;
839 len += data->size;
842 /* If we found a newline, copy the line into a memory buffer,
843 and remove it from BUF. */
844 if (data != NULL)
846 char *p;
847 struct buffer_data *nldata;
849 p = xmalloc (len + 1);
850 if (p == NULL)
851 return -2;
852 *line = p;
854 nldata = data;
855 data = buf->data;
856 while (data != nldata)
858 struct buffer_data *next;
860 memcpy (p, data->bufp, data->size);
861 p += data->size;
862 next = data->next;
863 buf_free_datas (data, data);
864 data = next;
867 memcpy (p, data->bufp, finallen);
868 p[finallen] = '\0';
870 data->size -= finallen + 1;
871 data->bufp = nl + 1;
872 buf->data = data;
874 if (lenp != NULL)
875 *lenp = len;
877 return 0;
880 /* Read more data until we get a newline or MAX characters. */
881 predicted_len = 0;
882 while (1)
884 int status;
885 size_t size, nbytes;
886 char *mem;
888 if (buf->data == NULL
889 || (buf->last->bufp + buf->last->size
890 == buf->last->text + BUFFER_DATA_SIZE))
892 data = get_buffer_data ();
893 if (data == NULL)
895 (*buf->memory_error) (buf);
896 return -2;
899 if (buf->data == NULL)
900 buf->data = data;
901 else
902 buf->last->next = data;
903 data->next = NULL;
904 buf->last = data;
906 data->bufp = data->text;
907 data->size = 0;
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);
918 if (status != 0)
919 return status;
921 predicted_len += nbytes;
922 buf->last->size += nbytes;
924 /* Optimize slightly to avoid an unnecessary call to memchr. */
925 if (nbytes == 1)
927 if (*mem == '\012')
928 break;
930 else
932 if (memchr (mem, '\012', nbytes) != NULL)
933 break;
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);
962 buf->data = next;
963 if (next == NULL)
964 buf->last = NULL;
967 if (buf->data == NULL)
969 struct buffer_data *data;
970 int status;
971 size_t get, nbytes;
973 data = get_buffer_data ();
974 if (data == NULL)
976 (*buf->memory_error) (buf);
977 return -2;
980 buf->data = data;
981 buf->last = data;
982 data->next = NULL;
983 data->bufp = data->text;
984 data->size = 0;
986 if (want < BUFFER_DATA_SIZE)
987 get = want;
988 else
989 get = BUFFER_DATA_SIZE;
990 status = (*buf->input) (buf->closure, data->bufp, get,
991 BUFFER_DATA_SIZE, &nbytes);
992 if (status != 0)
993 return status;
995 data->size = nbytes;
998 *retdata = buf->data->bufp;
999 if (want < buf->data->size)
1001 *got = want;
1002 buf->data->size -= want;
1003 buf->data->bufp += want;
1005 else
1007 *got = buf->data->size;
1008 buf->data->size = 0;
1011 return 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.
1022 void
1023 buf_copy_lines (struct buffer *outbuf, struct buffer *inbuf, int command)
1025 while (1)
1027 struct buffer_data *data;
1028 struct buffer_data *nldata;
1029 char *nl;
1030 int len;
1032 /* See if there is a newline in INBUF. */
1033 nldata = NULL;
1034 nl = NULL;
1035 for (data = inbuf->data; data != NULL; data = data->next)
1037 nl = memchr (data->bufp, '\n', data->size);
1038 if (nl != NULL)
1040 nldata = data;
1041 break;
1045 if (nldata == NULL)
1047 /* There are no more lines in INBUF. */
1048 return;
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
1059 * the newline.
1061 for (data = inbuf->data; data->next != nldata; data = data->next);
1062 data->next = NULL;
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)
1076 inbuf->last = NULL;
1078 nldata->next = NULL;
1079 buf_append_data (outbuf, nldata, nldata);
1081 else
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)
1106 *special = 0;
1108 while (1)
1110 struct buffer_data *data;
1111 int need;
1112 union
1114 char intbuf[sizeof (int)];
1115 int i;
1116 } u;
1117 char *intp;
1118 int count;
1119 struct buffer_data *start;
1120 int startoff;
1121 struct buffer_data *stop;
1122 int stopwant;
1124 /* See if we have enough bytes to figure out the count. */
1125 need = sizeof (int);
1126 intp = u.intbuf;
1127 for (data = inbuf->data; data != NULL; data = data->next)
1129 if (data->size >= need)
1131 memcpy (intp, data->bufp, need);
1132 break;
1134 memcpy (intp, data->bufp, data->size);
1135 intp += data->size;
1136 need -= data->size;
1138 if (data == NULL)
1140 /* We don't have enough bytes to form an integer. */
1141 return need;
1144 count = u.i;
1145 start = data;
1146 startoff = need;
1148 if (count < 0)
1150 /* A negative COUNT is a special case meaning that we
1151 don't need any further information. */
1152 stop = start;
1153 stopwant = 0;
1155 else
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);
1164 if (need <= 0)
1166 stop = start;
1167 stopwant = count;
1169 else
1171 for (data = start->next; data != NULL; data = data->next)
1173 if (need <= data->size)
1174 break;
1175 need -= data->size;
1177 if (data == NULL)
1179 /* We don't have enough bytes. */
1180 return need;
1182 stop = data;
1183 stopwant = need;
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)
1200 stop = stop->next;
1201 stopwant = 0;
1204 while (inbuf->data != start)
1206 data = inbuf->data;
1207 inbuf->data = data->next;
1208 buf_free_datas (data, data);
1211 /* If COUNT is negative, set *SPECIAL and get out now. */
1212 if (count < 0)
1214 *special = count;
1215 return 0;
1219 * We want to copy over the bytes from START through STOP. We
1220 * only want STOPWANT bytes from STOP.
1223 if (start != stop)
1225 /* Attach the buffers from START through STOP to OUTBUF. */
1226 for (data = start; data->next != stop; data = data->next);
1227 inbuf->data = stop;
1228 data->next = NULL;
1229 buf_append_data (outbuf, start, data);
1232 if (stopwant > 0)
1234 buf_output (outbuf, stop->bufp, stopwant);
1235 stop->bufp += stopwant;
1236 stop->size -= stopwant;
1240 /*NOTREACHED*/
1246 buf_get_fd (struct buffer *buf)
1248 if (buf->get_fd)
1249 return (*buf->get_fd) (buf->closure);
1250 return -1;
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);
1260 return 0;
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
1278 correspondingly. */
1280 # define PACKET_SLOP (100)
1282 /* This structure is the closure field of a packetizing buffer. */
1284 struct packetizing_buffer
1286 /* The underlying buffer. */
1287 struct buffer *buf;
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,
1294 size_t size);
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. */
1304 void *fnclosure;
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. */
1309 bool translated;
1310 /* The amount of buffered data. */
1311 size_t holdsize;
1312 /* The buffer allocated to hold the data. */
1313 char *holdbuf;
1314 /* The size of holdbuf. */
1315 size_t holdbufsize;
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. */
1319 char *holddata;
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. */
1334 struct buffer *
1335 packetizing_buffer_initialize (struct buffer *buf,
1336 int (*inpfn) (void *, const char *, char *,
1337 size_t),
1338 int (*outfn) (void *, const char *, char *,
1339 size_t, size_t *),
1340 void *fnclosure,
1341 void (*memory) (struct buffer *))
1343 struct packetizing_buffer *pb;
1345 pb = xmalloc (sizeof *pb);
1346 memset (pb, 0, sizeof *pb);
1348 pb->buf = buf;
1349 pb->inpfn = inpfn;
1350 pb->outfn = outfn;
1351 pb->fnclosure = fnclosure;
1353 if (inpfn != NULL)
1355 /* Add PACKET_SLOP to handle larger translated packets, and
1356 add 2 for the count. This buffer is increased if
1357 necessary. */
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,
1368 memory,
1369 pb);
1374 /* Input data from a packetizing buffer. */
1375 static int
1376 packetizing_buffer_input (void *closure, char *data, size_t need, size_t size,
1377 size_t *got)
1379 struct packetizing_buffer *pb = closure;
1381 *got = 0;
1383 if (pb->holdsize > 0 && pb->translated)
1385 size_t copy;
1387 copy = pb->holdsize;
1389 if (copy > size)
1391 memcpy (data, pb->holddata, size);
1392 pb->holdsize -= size;
1393 pb->holddata += size;
1394 *got = size;
1395 return 0;
1398 memcpy (data, pb->holddata, copy);
1399 pb->holdsize = 0;
1400 pb->translated = false;
1402 data += copy;
1403 need -= copy;
1404 size -= copy;
1405 *got = copy;
1408 while (need > 0 || *got == 0)
1410 int status;
1411 size_t get, nread, count, tcount;
1412 char *bytes;
1413 static char *stackoutbuf = NULL;
1414 char *inbuf, *outbuf;
1416 if (!stackoutbuf)
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);
1424 if (status != 0)
1426 /* buf_read_data can return -2, but a buffer input
1427 function is only supposed to return -1, 0, or an
1428 error code. */
1429 if (status == -2)
1430 status = ENOMEM;
1431 return status;
1434 if (nread == 0)
1436 /* The buffer is in nonblocking mode, and we didn't
1437 manage to read anything. */
1438 return 0;
1441 if (get == 1)
1442 pb->holdbuf[1] = bytes[0];
1443 else
1445 pb->holdbuf[0] = bytes[0];
1446 if (nread < 2)
1448 /* We only got one byte, but we needed two. Stash
1449 the byte we got, and try again. */
1450 pb->holdsize = 1;
1451 continue;
1453 pb->holdbuf[1] = bytes[1];
1455 pb->holdsize = 2;
1458 /* Read the packet. */
1460 count = (((pb->holdbuf[0] & 0xff) << 8)
1461 + (pb->holdbuf[1] & 0xff));
1463 if (count + 2 > pb->holdbufsize)
1465 char *n;
1467 /* We didn't allocate enough space in the initialize
1468 function. */
1470 n = xrealloc (pb->holdbuf, count + 2);
1471 if (n == NULL)
1473 (*pb->buf->memory_error) (pb->buf);
1474 return ENOMEM;
1476 pb->holdbuf = n;
1477 pb->holdbufsize = count + 2;
1480 get = count - (pb->holdsize - 2);
1482 status = buf_read_data (pb->buf, get, &bytes, &nread);
1483 if (status != 0)
1485 /* buf_read_data can return -2, but a buffer input
1486 function is only supposed to return -1, 0, or an error
1487 code. */
1488 if (status == -2)
1489 status = ENOMEM;
1490 return status;
1493 if (nread == 0)
1495 /* We did not get any data. Presumably the buffer is in
1496 nonblocking mode. */
1497 return 0;
1500 if (nread < get)
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;
1507 continue;
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. */
1517 inbuf = bytes;
1519 else
1521 /* We already had a partial packet in PB->HOLDBUF. We
1522 need to copy the new data over to make the input
1523 contiguous. */
1524 memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1525 inbuf = pb->holdbuf + 2;
1528 if (count <= BUFFER_DATA_SIZE + PACKET_SLOP)
1529 outbuf = stackoutbuf;
1530 else
1532 outbuf = xmalloc (count);
1533 if (outbuf == NULL)
1535 (*pb->buf->memory_error) (pb->buf);
1536 return ENOMEM;
1540 status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1541 if (status != 0)
1542 return status;
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);
1548 if (tcount > count)
1549 error (1, 0, "Input translation failure");
1551 if (tcount > size)
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);
1557 *got += 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)
1565 free (outbuf);
1567 return 0;
1570 memcpy (data, outbuf + 2, tcount);
1572 if (outbuf != stackoutbuf)
1573 free (outbuf);
1575 pb->holdsize = 0;
1577 data += tcount;
1578 need -= tcount;
1579 size -= tcount;
1580 *got += tcount;
1583 return 0;
1588 /* Output data to a packetizing buffer. */
1589 static int
1590 packetizing_buffer_output (void *closure, const char *data, size_t have,
1591 size_t *wrote)
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.
1601 char *outbuf;
1602 size_t size, translated;
1603 int status;
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);
1609 if (!inbuf)
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);
1619 size = have + 2;
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;
1627 else
1629 outdata = get_buffer_data ();
1630 if (outdata == NULL)
1632 (*pb->buf->memory_error) (pb->buf);
1633 return ENOMEM;
1636 outdata->next = NULL;
1637 outdata->bufp = outdata->text;
1639 outbuf = outdata->text;
1642 status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1643 &translated);
1644 if (status != 0)
1645 return status;
1647 /* The output function is permitted to add up to PACKET_SLOP
1648 bytes. */
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);
1656 else
1658 outdata->size = translated + 2;
1659 buf_append_data (pb->buf, outdata, outdata);
1662 *wrote = have;
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. */
1673 static int
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
1681 pass 0 here. */
1682 return buf_flush (pb->buf, 0);
1687 /* The block routine for a packetizing buffer. */
1688 static int
1689 packetizing_buffer_block (void *closure, bool block)
1691 struct packetizing_buffer *pb = closure;
1693 if (block)
1694 return set_block (pb->buf);
1695 else
1696 return set_nonblock (pb->buf);
1701 /* Return the file descriptor underlying any child buffers. */
1702 static int
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. */
1712 static int
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. */
1726 struct fd_buffer
1728 /* The file descriptor. */
1729 int fd;
1730 /* Nonzero if the file descriptor is in blocking mode. */
1731 int blocking;
1732 /* The child process id when fd is a pipe. */
1733 pid_t child_pid;
1734 /* The connection info, when fd is a pipe to a server. */
1735 cvsroot_t *root;
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
1748 occurs. */
1750 struct buffer *
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);
1757 n->fd = fd;
1758 n->child_pid = child_pid;
1759 n->root = root;
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,
1765 fd_buffer_shutdown,
1766 memory,
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.
1785 * ASSUMPTIONS
1786 * NEED <= SIZE.
1788 * INPUTS
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.
1795 * OUTPUTS
1796 * data Filled with bytes read.
1797 * *got Number of bytes actually read into DATA.
1799 * RETURNS
1800 * errno On error.
1801 * -1 On EOF.
1802 * 0 Otherwise.
1804 * ERRORS
1805 * This function can return an error if fd_buffer_block(), or the system
1806 * read() or select() calls do.
1808 static int
1809 fd_buffer_input (void *closure, char *data, size_t need, size_t size,
1810 size_t *got)
1812 struct fd_buffer *fb = closure;
1813 int nbytes;
1815 assert (need <= size);
1817 *got = 0;
1819 if (fb->blocking)
1821 int status;
1822 fd_set readfds;
1824 /* Set non-block. */
1825 status = fd_buffer_block (fb, false);
1826 if (status != 0) return status;
1828 FD_ZERO (&readfds);
1829 FD_SET (fb->fd, &readfds);
1832 int numfds;
1834 do {
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)
1841 status = errno;
1842 goto block_done;
1844 } while (numfds < 0);
1846 nbytes = read (fb->fd, data + *got, size - *got);
1848 if (nbytes == 0)
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. */
1853 if (*got)
1855 /* We already read some data, so return no error, counting
1856 * on the fact that we will read EOF again next time.
1858 status = 0;
1859 break;
1861 else
1863 /* Return EOF. */
1864 status = -1;
1865 break;
1869 if (nbytes < 0)
1871 /* Some error occurred. */
1872 if (!blocking_error (errno))
1874 status = errno;
1875 break;
1877 /* else Everything's fine, we just didn't get any data. */
1880 *got += nbytes;
1881 } while (*got < need);
1883 block_done:
1884 if (status == 0 || status == -1)
1886 int newstatus;
1888 /* OK or EOF - Reset block. */
1889 newstatus = fd_buffer_block (fb, true);
1890 if (newstatus) status = newstatus;
1892 return status;
1895 /* The above will always return. Handle non-blocking read. */
1896 nbytes = read (fb->fd, data, size);
1898 if (nbytes > 0)
1900 *got = nbytes;
1901 return 0;
1904 if (nbytes == 0)
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. */
1908 return -1;
1910 /* Some error occurred. */
1911 if (blocking_error (errno))
1912 /* Everything's fine, we just didn't get any data. */
1913 return 0;
1915 return errno;
1920 /* The buffer output function for a buffer built on a file descriptor. */
1922 static int
1923 fd_buffer_output (void *closure, const char *data, size_t have, size_t *wrote)
1925 struct fd_buffer *fd = closure;
1927 *wrote = 0;
1929 while (have > 0)
1931 int nbytes;
1933 nbytes = write (fd->fd, data, have);
1935 if (nbytes <= 0)
1937 if (! fd->blocking
1938 && (nbytes == 0 || blocking_error (errno)))
1940 /* A nonblocking write failed to write any data. Just
1941 return. */
1942 return 0;
1945 /* Some sort of error occurred. */
1947 if (nbytes == 0)
1948 return EIO;
1950 return errno;
1953 *wrote += nbytes;
1954 data += nbytes;
1955 have -= nbytes;
1958 return 0;
1963 /* The buffer flush function for a buffer built on a file descriptor. */
1964 static int
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)
1973 * return errno;
1975 return 0;
1980 static struct stat devnull;
1981 static int devnull_set = -1;
1983 /* The buffer block function for a buffer built on a file descriptor. */
1984 static int
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)
1989 int flags;
1991 flags = fcntl (fb->fd, F_GETFL, 0);
1992 if (flags < 0)
1993 return errno;
1995 if (block)
1996 flags &= ~O_NONBLOCK;
1997 else
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.
2006 struct stat sb;
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);
2024 if (isdevnull)
2025 errno = 0;
2026 else
2028 errno = save_errno;
2029 return errno;
2032 # endif /* F_GETFL && O_NONBLOCK && F_SETFL */
2034 fb->blocking = block;
2036 return 0;
2041 static int
2042 fd_buffer_get_fd (void *closure)
2044 struct fd_buffer *fb = closure;
2045 return fb->fd;
2050 /* The buffer shutdown function for a buffer built on a file descriptor.
2052 * This function disposes of memory allocated for this buffer.
2054 static int
2055 fd_buffer_shutdown (struct buffer *buf)
2057 struct fd_buffer *fb = buf->closure;
2058 struct stat s;
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?
2071 closefd = statted;
2073 /* Flush the buffer if possible. */
2074 if (buf->flush)
2076 buf_flush (buf, 1);
2077 buf->flush = NULL;
2080 if (buf->input)
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
2085 * closed, as well.
2088 /* FIXME:
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)
2102 # endif
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);
2123 closefd = false;
2125 # endif /* START_RSH_WITH_POPEN_RW */
2127 buf->input = NULL;
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);
2137 else
2138 # endif
2139 # ifndef NO_SOCKET_TO_FD
2140 /* shutdown() sockets */
2141 if (statted && S_ISSOCK (s.st_mode))
2142 shutdown (fb->fd, 1);
2143 # else
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 */
2151 buf->output = NULL;
2154 if (statted && closefd && close (fb->fd) == -1)
2156 if (server_active)
2158 /* Syslog this? */
2160 # ifdef CLIENT_SUPPORT
2161 else if (fb->root)
2162 error (1, errno, "closing down connection to %s",
2163 fb->root->hostname);
2164 /* EXITS */
2165 # endif /* CLIENT_SUPPORT */
2167 error (0, errno, "closing down buffer");
2170 /* If we were talking to a process, make sure it exited */
2171 if (fb->child_pid)
2173 int w;
2176 w = waitpid (fb->child_pid, NULL, 0);
2177 while (w == -1 && errno == EINTR);
2178 if (w == -1)
2179 error (1, errno, "waiting for process %d", fb->child_pid);
2182 free (buf->closure);
2183 buf->closure = NULL;
2185 return 0;
2187 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */