a very simple fix - if I'd only thought if it last week :)
[rsync/rsync.git] / io.c
CommitLineData
720b47f2
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
08f15335 21 socket and pipe IO utilities used in rsync
720b47f2
AT
22
23 tridge, June 1996
24 */
25#include "rsync.h"
26
8cd9fd4e
AT
27/* if no timeout is specified then use a 60 second select timeout */
28#define SELECT_TIMEOUT 60
29
8d9dc9f9
AT
30static int io_multiplexing_out;
31static int io_multiplexing_in;
679e7657
AT
32static int multiplex_in_fd;
33static int multiplex_out_fd;
8d9dc9f9 34static time_t last_io;
528bfcd7 35static int eof_error=1;
720b47f2 36extern int verbose;
6ba9279f 37extern int io_timeout;
a800434a 38extern struct stats stats;
720b47f2
AT
39
40static int buffer_f_in = -1;
554e0a8d 41static int io_error_fd = -1;
720b47f2 42
ff41a59f
AT
43static void read_loop(int fd, char *buf, int len);
44
4c36ddbe 45void setup_readbuffer(int f_in)
720b47f2 46{
22d6234e 47 buffer_f_in = f_in;
720b47f2
AT
48}
49
8d9dc9f9
AT
50static void check_timeout(void)
51{
52 time_t t;
53
54 if (!io_timeout) return;
55
56 if (!last_io) {
57 last_io = time(NULL);
58 return;
59 }
60
61 t = time(NULL);
62
86ffe37f
AT
63 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
64 rprintf(FERROR,"io timeout after %d second - exiting\n",
8d9dc9f9 65 (int)(t-last_io));
65417579 66 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
67 }
68}
69
554e0a8d
AT
70/* setup the fd used to propogate errors */
71void io_set_error_fd(int fd)
72{
73 io_error_fd = fd;
74}
75
ff41a59f 76/* read some data from the error fd and write it to the write log code */
554e0a8d
AT
77static void read_error_fd(void)
78{
79 char buf[200];
80 int n;
81 int fd = io_error_fd;
ff41a59f
AT
82 int tag, len;
83
554e0a8d
AT
84 io_error_fd = -1;
85
ff41a59f
AT
86 read_loop(fd, buf, 4);
87 tag = IVAL(buf, 0);
88
89 len = tag & 0xFFFFFF;
90 tag = tag >> 24;
91 tag -= MPLEX_BASE;
92
93 while (len) {
94 n = len;
95 if (n > (sizeof(buf)-1)) n = sizeof(buf)-1;
96 read_loop(fd, buf, n);
97 rwrite((enum logcode)tag, buf, n);
98 len -= n;
554e0a8d
AT
99 }
100
101 io_error_fd = fd;
102}
103
720b47f2 104
e44f9a12 105static int no_flush;
720b47f2 106
4c36ddbe
AT
107/* read from a socket with IO timeout. return the number of
108 bytes read. If no bytes can be read then exit, never return
109 a number <= 0 */
110static int read_timeout(int fd, char *buf, int len)
8d9dc9f9 111{
4c36ddbe
AT
112 int n, ret=0;
113
ea2111d1
AT
114 io_flush();
115
4c36ddbe
AT
116 while (ret == 0) {
117 fd_set fds;
118 struct timeval tv;
554e0a8d 119 int fd_count = fd+1;
4c36ddbe
AT
120
121 FD_ZERO(&fds);
122 FD_SET(fd, &fds);
554e0a8d
AT
123 if (io_error_fd != -1) {
124 FD_SET(io_error_fd, &fds);
125 if (io_error_fd > fd) fd_count = io_error_fd+1;
126 }
127
8cd9fd4e 128 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
4c36ddbe
AT
129 tv.tv_usec = 0;
130
554e0a8d
AT
131 errno = 0;
132
133 if (select(fd_count, &fds, NULL, NULL, &tv) < 1) {
134 if (errno == EBADF) {
135 exit_cleanup(RERR_SOCKETIO);
136 }
4c36ddbe
AT
137 check_timeout();
138 continue;
139 }
140
554e0a8d
AT
141 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
142 read_error_fd();
143 }
144
145 if (!FD_ISSET(fd, &fds)) continue;
146
4c36ddbe
AT
147 n = read(fd, buf, len);
148
8d9dc9f9
AT
149 if (n > 0) {
150 buf += n;
151 len -= n;
4c36ddbe
AT
152 ret += n;
153 if (io_timeout)
154 last_io = time(NULL);
155 continue;
8d9dc9f9 156 }
4c36ddbe
AT
157
158 if (n == -1 && errno == EINTR) {
159 continue;
160 }
161
f0359dd0
AT
162 if (n == -1 &&
163 (errno == EWOULDBLOCK || errno == EAGAIN)) {
164 continue;
165 }
166
5d1e1dcf 167
8d9dc9f9 168 if (n == 0) {
528bfcd7 169 if (eof_error) {
ca8e9694 170 rprintf(FERROR,"unexpected EOF in read_timeout\n");
528bfcd7 171 }
65417579 172 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 173 }
8d9dc9f9 174
5d1e1dcf 175 /* this prevents us trying to write errors on a dead socket */
554e0a8d 176 io_multiplexing_close();
5d1e1dcf 177
4c36ddbe 178 rprintf(FERROR,"read error: %s\n", strerror(errno));
65417579 179 exit_cleanup(RERR_STREAMIO);
4c36ddbe 180 }
8d9dc9f9 181
4c36ddbe
AT
182 return ret;
183}
8d9dc9f9 184
4c36ddbe
AT
185/* continue trying to read len bytes - don't return until len
186 has been read */
187static void read_loop(int fd, char *buf, int len)
188{
189 while (len) {
190 int n = read_timeout(fd, buf, len);
191
192 buf += n;
193 len -= n;
8d9dc9f9
AT
194 }
195}
196
cad2bba7 197/* read from the file descriptor handling multiplexing -
4c36ddbe
AT
198 return number of bytes read
199 never return <= 0 */
8d9dc9f9
AT
200static int read_unbuffered(int fd, char *buf, int len)
201{
202 static int remaining;
8d9dc9f9
AT
203 int tag, ret=0;
204 char line[1024];
205
679e7657 206 if (!io_multiplexing_in || fd != multiplex_in_fd)
4c36ddbe 207 return read_timeout(fd, buf, len);
8d9dc9f9
AT
208
209 while (ret == 0) {
210 if (remaining) {
211 len = MIN(len, remaining);
212 read_loop(fd, buf, len);
213 remaining -= len;
214 ret = len;
215 continue;
216 }
217
ff41a59f
AT
218 read_loop(fd, line, 4);
219 tag = IVAL(line, 0);
679e7657 220
8d9dc9f9
AT
221 remaining = tag & 0xFFFFFF;
222 tag = tag >> 24;
223
224 if (tag == MPLEX_BASE) continue;
225
226 tag -= MPLEX_BASE;
227
228 if (tag != FERROR && tag != FINFO) {
229 rprintf(FERROR,"unexpected tag %d\n", tag);
65417579 230 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
231 }
232
233 if (remaining > sizeof(line)-1) {
234 rprintf(FERROR,"multiplexing overflow %d\n\n",
235 remaining);
65417579 236 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
237 }
238
239 read_loop(fd, line, remaining);
240 line[remaining] = 0;
241
ff41a59f 242 rprintf((enum logcode)tag,"%s", line);
8d9dc9f9
AT
243 remaining = 0;
244 }
245
246 return ret;
247}
248
249
4c36ddbe
AT
250/* do a buffered read from fd. don't return until all N bytes
251 have been read. If all N can't be read then exit with an error */
252static void readfd(int fd,char *buffer,int N)
720b47f2 253{
6ba9279f
AT
254 int ret;
255 int total=0;
6ba9279f 256
6ba9279f 257 while (total < N) {
8d9dc9f9
AT
258 io_flush();
259
4c36ddbe 260 ret = read_unbuffered(fd,buffer + total,N-total);
6ba9279f 261 total += ret;
7f28dbee 262 }
1b7c47cb
AT
263
264 stats.total_read += total;
720b47f2
AT
265}
266
267
b7922338 268int32 read_int(int f)
720b47f2 269{
4c36ddbe 270 char b[4];
d730b113
AT
271 int32 ret;
272
4c36ddbe 273 readfd(f,b,4);
d730b113
AT
274 ret = IVAL(b,0);
275 if (ret == (int32)0xffffffff) return -1;
276 return ret;
720b47f2
AT
277}
278
71c46176 279int64 read_longint(int f)
3a6a366f
AT
280{
281 extern int remote_version;
71c46176 282 int64 ret;
3a6a366f
AT
283 char b[8];
284 ret = read_int(f);
71c46176 285
8de330a3
AT
286 if ((int32)ret != (int32)0xffffffff) {
287 return ret;
288 }
71c46176 289
3bee6733 290#ifdef NO_INT64
9486289c 291 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
65417579 292 exit_cleanup(RERR_UNSUPPORTED);
71c46176
AT
293#else
294 if (remote_version >= 16) {
4c36ddbe 295 readfd(f,b,8);
71c46176 296 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
3a6a366f 297 }
71c46176
AT
298#endif
299
3a6a366f
AT
300 return ret;
301}
302
720b47f2
AT
303void read_buf(int f,char *buf,int len)
304{
4c36ddbe 305 readfd(f,buf,len);
720b47f2
AT
306}
307
575f2fca
AT
308void read_sbuf(int f,char *buf,int len)
309{
310 read_buf(f,buf,len);
311 buf[len] = 0;
312}
313
182dca5c
AT
314unsigned char read_byte(int f)
315{
4c36ddbe
AT
316 unsigned char c;
317 read_buf(f,(char *)&c,1);
318 return c;
182dca5c 319}
720b47f2 320
7bec6a5c 321
7bec6a5c 322
4c36ddbe
AT
323/* write len bytes to fd, possibly reading from buffer_f_in if set
324 in order to unclog the pipe. don't return until all len
325 bytes have been written */
326static void writefd_unbuffered(int fd,char *buf,int len)
720b47f2 327{
8d9dc9f9
AT
328 int total = 0;
329 fd_set w_fds, r_fds;
4c36ddbe 330 int fd_count, count;
8d9dc9f9 331 struct timeval tv;
720b47f2 332
e44f9a12
AT
333 no_flush++;
334
4c36ddbe 335 while (total < len) {
8d9dc9f9
AT
336 FD_ZERO(&w_fds);
337 FD_ZERO(&r_fds);
338 FD_SET(fd,&w_fds);
554e0a8d 339 fd_count = fd;
4c36ddbe 340
554e0a8d
AT
341 if (io_error_fd != -1) {
342 FD_SET(io_error_fd,&r_fds);
343 if (io_error_fd > fd_count)
344 fd_count = io_error_fd;
8d9dc9f9
AT
345 }
346
8cd9fd4e 347 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
8d9dc9f9 348 tv.tv_usec = 0;
4c36ddbe 349
554e0a8d
AT
350 errno = 0;
351
352 count = select(fd_count+1,
08f15335 353 io_error_fd != -1?&r_fds:NULL,
4c36ddbe 354 &w_fds,NULL,
8cd9fd4e 355 &tv);
4c36ddbe
AT
356
357 if (count <= 0) {
554e0a8d
AT
358 if (errno == EBADF) {
359 exit_cleanup(RERR_SOCKETIO);
360 }
8d9dc9f9
AT
361 check_timeout();
362 continue;
363 }
4c36ddbe 364
554e0a8d
AT
365 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
366 read_error_fd();
367 }
368
8d9dc9f9 369 if (FD_ISSET(fd, &w_fds)) {
07b7c86c
AT
370 int ret, n = len-total;
371
f0359dd0 372 ret = write(fd,buf+total,n);
4c36ddbe
AT
373
374 if (ret == -1 && errno == EINTR) {
375 continue;
376 }
377
f0359dd0
AT
378 if (ret == -1 &&
379 (errno == EWOULDBLOCK || errno == EAGAIN)) {
380 continue;
381 }
382
4c36ddbe
AT
383 if (ret <= 0) {
384 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
65417579 385 exit_cleanup(RERR_STREAMIO);
4c36ddbe
AT
386 }
387
388 total += ret;
a800434a 389
4c36ddbe
AT
390 if (io_timeout)
391 last_io = time(NULL);
8d9dc9f9 392 }
4c36ddbe 393 }
e44f9a12
AT
394
395 no_flush--;
720b47f2
AT
396}
397
8d9dc9f9 398
d6dead6b
AT
399static char *io_buffer;
400static int io_buffer_count;
401
402void io_start_buffering(int fd)
403{
8d9dc9f9 404 if (io_buffer) return;
679e7657 405 multiplex_out_fd = fd;
ff41a59f 406 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
d6dead6b
AT
407 if (!io_buffer) out_of_memory("writefd");
408 io_buffer_count = 0;
ff41a59f
AT
409}
410
411/* write an message to a multiplexed stream. If this fails then rsync
412 exits */
413static void mplex_write(int fd, enum logcode code, char *buf, int len)
414{
415 char buffer[4096];
416 int n = len;
8d9dc9f9 417
ff41a59f
AT
418 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
419
6d7b6081
AT
420 if (n > (sizeof(buffer)-4)) {
421 n = sizeof(buffer)-4;
ff41a59f
AT
422 }
423
424 memcpy(&buffer[4], buf, n);
425 writefd_unbuffered(fd, buffer, n+4);
426
427 len -= n;
428 buf += n;
429
6d7b6081
AT
430 if (len) {
431 writefd_unbuffered(fd, buf, len);
432 }
d6dead6b
AT
433}
434
ff41a59f 435
8d9dc9f9 436void io_flush(void)
d6dead6b 437{
679e7657 438 int fd = multiplex_out_fd;
e44f9a12 439 if (!io_buffer_count || no_flush) return;
8d9dc9f9
AT
440
441 if (io_multiplexing_out) {
0f3203c3 442 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
8d9dc9f9 443 } else {
4c36ddbe 444 writefd_unbuffered(fd, io_buffer, io_buffer_count);
d6dead6b 445 }
8d9dc9f9
AT
446 io_buffer_count = 0;
447}
448
449void io_end_buffering(int fd)
450{
451 io_flush();
452 if (!io_multiplexing_out) {
ff41a59f 453 free(io_buffer);
8d9dc9f9
AT
454 io_buffer = NULL;
455 }
d6dead6b
AT
456}
457
4c36ddbe 458static void writefd(int fd,char *buf,int len)
d6dead6b 459{
1b7c47cb
AT
460 stats.total_written += len;
461
554e0a8d 462 if (!io_buffer || fd != multiplex_out_fd) {
4c36ddbe
AT
463 writefd_unbuffered(fd, buf, len);
464 return;
465 }
d6dead6b
AT
466
467 while (len) {
468 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
469 if (n > 0) {
470 memcpy(io_buffer+io_buffer_count, buf, n);
471 buf += n;
472 len -= n;
473 io_buffer_count += n;
474 }
475
8d9dc9f9 476 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
d6dead6b 477 }
d6dead6b 478}
720b47f2
AT
479
480
b7922338 481void write_int(int f,int32 x)
720b47f2 482{
8d9dc9f9
AT
483 char b[4];
484 SIVAL(b,0,x);
4c36ddbe 485 writefd(f,b,4);
720b47f2
AT
486}
487
71c46176 488void write_longint(int f, int64 x)
3a6a366f
AT
489{
490 extern int remote_version;
491 char b[8];
3a6a366f
AT
492
493 if (remote_version < 16 || x <= 0x7FFFFFFF) {
494 write_int(f, (int)x);
495 return;
496 }
497
8de330a3 498 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
499 SIVAL(b,0,(x&0xFFFFFFFF));
500 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
501
4c36ddbe 502 writefd(f,b,8);
3a6a366f
AT
503}
504
720b47f2
AT
505void write_buf(int f,char *buf,int len)
506{
4c36ddbe 507 writefd(f,buf,len);
720b47f2
AT
508}
509
f0fca04e 510/* write a string to the connection */
6e4fb64e 511static void write_sbuf(int f,char *buf)
f0fca04e
AT
512{
513 write_buf(f, buf, strlen(buf));
514}
515
720b47f2 516
182dca5c
AT
517void write_byte(int f,unsigned char c)
518{
f0fca04e 519 write_buf(f,(char *)&c,1);
182dca5c
AT
520}
521
f0fca04e
AT
522int read_line(int f, char *buf, int maxlen)
523{
528bfcd7
AT
524 eof_error = 0;
525
f0fca04e 526 while (maxlen) {
528bfcd7 527 buf[0] = 0;
f0fca04e 528 read_buf(f, buf, 1);
528bfcd7 529 if (buf[0] == 0) return 0;
f0fca04e
AT
530 if (buf[0] == '\n') {
531 buf[0] = 0;
532 break;
533 }
534 if (buf[0] != '\r') {
535 buf++;
536 maxlen--;
537 }
538 }
539 if (maxlen == 0) {
540 *buf = 0;
541 return 0;
542 }
528bfcd7
AT
543
544 eof_error = 1;
545
f0fca04e
AT
546 return 1;
547}
548
549
550void io_printf(int fd, const char *format, ...)
551{
552 va_list ap;
553 char buf[1024];
554 int len;
555
556 va_start(ap, format);
37f9805d 557 len = vslprintf(buf, sizeof(buf), format, ap);
f0fca04e
AT
558 va_end(ap);
559
65417579 560 if (len < 0) exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
561
562 write_sbuf(fd, buf);
563}
8d9dc9f9
AT
564
565
566/* setup for multiplexing an error stream with the data stream */
567void io_start_multiplex_out(int fd)
568{
679e7657
AT
569 multiplex_out_fd = fd;
570 io_flush();
8d9dc9f9
AT
571 io_start_buffering(fd);
572 io_multiplexing_out = 1;
573}
574
575/* setup for multiplexing an error stream with the data stream */
576void io_start_multiplex_in(int fd)
577{
679e7657
AT
578 multiplex_in_fd = fd;
579 io_flush();
8d9dc9f9
AT
580 io_multiplexing_in = 1;
581}
582
554e0a8d 583/* write an message to the multiplexed error stream */
ff41a59f 584int io_multiplex_write(enum logcode code, char *buf, int len)
8d9dc9f9
AT
585{
586 if (!io_multiplexing_out) return 0;
587
588 io_flush();
1b7c47cb 589 stats.total_written += (len+4);
ff41a59f 590 mplex_write(multiplex_out_fd, code, buf, len);
8d9dc9f9
AT
591 return 1;
592}
593
554e0a8d 594/* write a message to the special error fd */
ff41a59f 595int io_error_write(int f, enum logcode code, char *buf, int len)
554e0a8d
AT
596{
597 if (f == -1) return 0;
ff41a59f 598 mplex_write(f, code, buf, len);
554e0a8d
AT
599 return 1;
600}
601
602/* stop output multiplexing */
603void io_multiplexing_close(void)
604{
605 io_multiplexing_out = 0;
606}
607
8d9dc9f9
AT
608void io_close_input(int fd)
609{
610 buffer_f_in = -1;
611}
8b35435f 612