In --version, say whether we have socketpair() or not
[rsync/rsync.git] / io.c
CommitLineData
7a24c346
MP
1/* -*- c-file-style: "linux" -*-
2
3 Copyright (C) 1996-2000 by Andrew Tridgell
720b47f2
AT
4 Copyright (C) Paul Mackerras 1996
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
08f15335 22 socket and pipe IO utilities used in rsync
720b47f2
AT
23
24 tridge, June 1996
25 */
26#include "rsync.h"
27
8cd9fd4e
AT
28/* if no timeout is specified then use a 60 second select timeout */
29#define SELECT_TIMEOUT 60
30
ef5d23eb
DD
31extern int bwlimit;
32
8d9dc9f9
AT
33static int io_multiplexing_out;
34static int io_multiplexing_in;
679e7657
AT
35static int multiplex_in_fd;
36static int multiplex_out_fd;
8d9dc9f9 37static time_t last_io;
528bfcd7 38static int eof_error=1;
720b47f2 39extern int verbose;
6ba9279f 40extern int io_timeout;
a800434a 41extern struct stats stats;
720b47f2 42
554e0a8d 43static int io_error_fd = -1;
720b47f2 44
ff41a59f
AT
45static void read_loop(int fd, char *buf, int len);
46
8d9dc9f9
AT
47static void check_timeout(void)
48{
0adb99b9 49 extern int am_server, am_daemon;
8d9dc9f9
AT
50 time_t t;
51
52 if (!io_timeout) return;
53
54 if (!last_io) {
55 last_io = time(NULL);
56 return;
57 }
58
59 t = time(NULL);
60
86ffe37f 61 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
0adb99b9
AT
62 if (!am_server && !am_daemon) {
63 rprintf(FERROR,"io timeout after %d second - exiting\n",
64 (int)(t-last_io));
65 }
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
ae682c3e 321/* write len bytes to fd */
4c36ddbe 322static void writefd_unbuffered(int fd,char *buf,int len)
720b47f2 323{
8d9dc9f9
AT
324 int total = 0;
325 fd_set w_fds, r_fds;
4c36ddbe 326 int fd_count, count;
8d9dc9f9 327 struct timeval tv;
720b47f2 328
e44f9a12
AT
329 no_flush++;
330
4c36ddbe 331 while (total < len) {
8d9dc9f9
AT
332 FD_ZERO(&w_fds);
333 FD_ZERO(&r_fds);
334 FD_SET(fd,&w_fds);
554e0a8d 335 fd_count = fd;
4c36ddbe 336
554e0a8d
AT
337 if (io_error_fd != -1) {
338 FD_SET(io_error_fd,&r_fds);
339 if (io_error_fd > fd_count)
340 fd_count = io_error_fd;
8d9dc9f9
AT
341 }
342
8cd9fd4e 343 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
8d9dc9f9 344 tv.tv_usec = 0;
4c36ddbe 345
554e0a8d
AT
346 errno = 0;
347
348 count = select(fd_count+1,
08f15335 349 io_error_fd != -1?&r_fds:NULL,
4c36ddbe 350 &w_fds,NULL,
8cd9fd4e 351 &tv);
4c36ddbe
AT
352
353 if (count <= 0) {
554e0a8d
AT
354 if (errno == EBADF) {
355 exit_cleanup(RERR_SOCKETIO);
356 }
8d9dc9f9
AT
357 check_timeout();
358 continue;
359 }
4c36ddbe 360
554e0a8d
AT
361 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
362 read_error_fd();
363 }
364
8d9dc9f9 365 if (FD_ISSET(fd, &w_fds)) {
07b7c86c
AT
366 int ret, n = len-total;
367
f0359dd0 368 ret = write(fd,buf+total,n);
4c36ddbe
AT
369
370 if (ret == -1 && errno == EINTR) {
371 continue;
372 }
373
f0359dd0
AT
374 if (ret == -1 &&
375 (errno == EWOULDBLOCK || errno == EAGAIN)) {
e92ee128 376 msleep(1);
f0359dd0
AT
377 continue;
378 }
379
4c36ddbe
AT
380 if (ret <= 0) {
381 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
65417579 382 exit_cleanup(RERR_STREAMIO);
4c36ddbe
AT
383 }
384
ef5d23eb
DD
385 /* Sleep after writing to limit I/O bandwidth */
386 if (bwlimit)
387 {
388 tv.tv_sec = 0;
389 tv.tv_usec = ret * 1000 / bwlimit;
390 while (tv.tv_usec > 1000000)
391 {
392 tv.tv_sec++;
393 tv.tv_usec -= 1000000;
394 }
395 select(0, NULL, NULL, NULL, &tv);
396 }
397
4c36ddbe 398 total += ret;
a800434a 399
4c36ddbe
AT
400 if (io_timeout)
401 last_io = time(NULL);
8d9dc9f9 402 }
4c36ddbe 403 }
e44f9a12
AT
404
405 no_flush--;
720b47f2
AT
406}
407
8d9dc9f9 408
d6dead6b
AT
409static char *io_buffer;
410static int io_buffer_count;
411
412void io_start_buffering(int fd)
413{
8d9dc9f9 414 if (io_buffer) return;
679e7657 415 multiplex_out_fd = fd;
ff41a59f 416 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
d6dead6b
AT
417 if (!io_buffer) out_of_memory("writefd");
418 io_buffer_count = 0;
ff41a59f
AT
419}
420
421/* write an message to a multiplexed stream. If this fails then rsync
422 exits */
423static void mplex_write(int fd, enum logcode code, char *buf, int len)
424{
425 char buffer[4096];
426 int n = len;
8d9dc9f9 427
ff41a59f
AT
428 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
429
6d7b6081
AT
430 if (n > (sizeof(buffer)-4)) {
431 n = sizeof(buffer)-4;
ff41a59f
AT
432 }
433
434 memcpy(&buffer[4], buf, n);
435 writefd_unbuffered(fd, buffer, n+4);
436
437 len -= n;
438 buf += n;
439
6d7b6081
AT
440 if (len) {
441 writefd_unbuffered(fd, buf, len);
442 }
d6dead6b
AT
443}
444
ff41a59f 445
8d9dc9f9 446void io_flush(void)
d6dead6b 447{
679e7657 448 int fd = multiplex_out_fd;
e44f9a12 449 if (!io_buffer_count || no_flush) return;
8d9dc9f9
AT
450
451 if (io_multiplexing_out) {
0f3203c3 452 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
8d9dc9f9 453 } else {
4c36ddbe 454 writefd_unbuffered(fd, io_buffer, io_buffer_count);
d6dead6b 455 }
8d9dc9f9
AT
456 io_buffer_count = 0;
457}
458
0ba48136
MP
459
460/* XXX: fd is ignored, which seems a little strange. */
8d9dc9f9
AT
461void io_end_buffering(int fd)
462{
463 io_flush();
464 if (!io_multiplexing_out) {
ff41a59f 465 free(io_buffer);
8d9dc9f9
AT
466 io_buffer = NULL;
467 }
d6dead6b
AT
468}
469
e92ee128
AT
470/* some OSes have a bug where an exit causes the pending writes on
471 a socket to be flushed. Do an explicit shutdown to try to prevent this */
472void io_shutdown(void)
473{
474 if (multiplex_out_fd != -1) close(multiplex_out_fd);
475 if (io_error_fd != -1) close(io_error_fd);
476 multiplex_out_fd = -1;
477 io_error_fd = -1;
478}
479
480
4c36ddbe 481static void writefd(int fd,char *buf,int len)
d6dead6b 482{
1b7c47cb
AT
483 stats.total_written += len;
484
554e0a8d 485 if (!io_buffer || fd != multiplex_out_fd) {
4c36ddbe
AT
486 writefd_unbuffered(fd, buf, len);
487 return;
488 }
d6dead6b
AT
489
490 while (len) {
491 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
492 if (n > 0) {
493 memcpy(io_buffer+io_buffer_count, buf, n);
494 buf += n;
495 len -= n;
496 io_buffer_count += n;
497 }
498
8d9dc9f9 499 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
d6dead6b 500 }
d6dead6b 501}
720b47f2
AT
502
503
b7922338 504void write_int(int f,int32 x)
720b47f2 505{
8d9dc9f9
AT
506 char b[4];
507 SIVAL(b,0,x);
4c36ddbe 508 writefd(f,b,4);
720b47f2
AT
509}
510
7a24c346
MP
511
512/*
513 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
514 * 64-bit types on this platform.
515 */
71c46176 516void write_longint(int f, int64 x)
3a6a366f
AT
517{
518 extern int remote_version;
519 char b[8];
3a6a366f
AT
520
521 if (remote_version < 16 || x <= 0x7FFFFFFF) {
522 write_int(f, (int)x);
523 return;
524 }
525
8de330a3 526 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
527 SIVAL(b,0,(x&0xFFFFFFFF));
528 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
529
4c36ddbe 530 writefd(f,b,8);
3a6a366f
AT
531}
532
720b47f2
AT
533void write_buf(int f,char *buf,int len)
534{
4c36ddbe 535 writefd(f,buf,len);
720b47f2
AT
536}
537
f0fca04e 538/* write a string to the connection */
6e4fb64e 539static void write_sbuf(int f,char *buf)
f0fca04e
AT
540{
541 write_buf(f, buf, strlen(buf));
542}
543
720b47f2 544
182dca5c
AT
545void write_byte(int f,unsigned char c)
546{
f0fca04e 547 write_buf(f,(char *)&c,1);
182dca5c
AT
548}
549
f0fca04e
AT
550int read_line(int f, char *buf, int maxlen)
551{
528bfcd7
AT
552 eof_error = 0;
553
f0fca04e 554 while (maxlen) {
528bfcd7 555 buf[0] = 0;
f0fca04e 556 read_buf(f, buf, 1);
528bfcd7 557 if (buf[0] == 0) return 0;
f0fca04e
AT
558 if (buf[0] == '\n') {
559 buf[0] = 0;
560 break;
561 }
562 if (buf[0] != '\r') {
563 buf++;
564 maxlen--;
565 }
566 }
567 if (maxlen == 0) {
568 *buf = 0;
569 return 0;
570 }
528bfcd7
AT
571
572 eof_error = 1;
573
f0fca04e
AT
574 return 1;
575}
576
577
578void io_printf(int fd, const char *format, ...)
579{
580 va_list ap;
581 char buf[1024];
582 int len;
583
584 va_start(ap, format);
37f9805d 585 len = vslprintf(buf, sizeof(buf), format, ap);
f0fca04e
AT
586 va_end(ap);
587
65417579 588 if (len < 0) exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
589
590 write_sbuf(fd, buf);
591}
8d9dc9f9
AT
592
593
594/* setup for multiplexing an error stream with the data stream */
595void io_start_multiplex_out(int fd)
596{
679e7657
AT
597 multiplex_out_fd = fd;
598 io_flush();
8d9dc9f9
AT
599 io_start_buffering(fd);
600 io_multiplexing_out = 1;
601}
602
603/* setup for multiplexing an error stream with the data stream */
604void io_start_multiplex_in(int fd)
605{
679e7657
AT
606 multiplex_in_fd = fd;
607 io_flush();
8d9dc9f9
AT
608 io_multiplexing_in = 1;
609}
610
554e0a8d 611/* write an message to the multiplexed error stream */
ff41a59f 612int io_multiplex_write(enum logcode code, char *buf, int len)
8d9dc9f9
AT
613{
614 if (!io_multiplexing_out) return 0;
615
616 io_flush();
1b7c47cb 617 stats.total_written += (len+4);
ff41a59f 618 mplex_write(multiplex_out_fd, code, buf, len);
8d9dc9f9
AT
619 return 1;
620}
621
554e0a8d 622/* write a message to the special error fd */
ff41a59f 623int io_error_write(int f, enum logcode code, char *buf, int len)
554e0a8d
AT
624{
625 if (f == -1) return 0;
ff41a59f 626 mplex_write(f, code, buf, len);
554e0a8d
AT
627 return 1;
628}
629
630/* stop output multiplexing */
631void io_multiplexing_close(void)
632{
633 io_multiplexing_out = 0;
634}
635