indent -kr -i8
[rsync/rsync.git] / io.c
CommitLineData
7a24c346
MP
1/* -*- c-file-style: "linux" -*-
2
ce6c7c63 3 Copyright (C) 1996-2001 by Andrew Tridgell
720b47f2 4 Copyright (C) Paul Mackerras 1996
7a55d06e 5 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
720b47f2
AT
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22/*
08f15335 23 socket and pipe IO utilities used in rsync
720b47f2
AT
24
25 tridge, June 1996
26 */
27#include "rsync.h"
28
8cd9fd4e
AT
29/* if no timeout is specified then use a 60 second select timeout */
30#define SELECT_TIMEOUT 60
31
8d9dc9f9
AT
32static int io_multiplexing_out;
33static int io_multiplexing_in;
679e7657
AT
34static int multiplex_in_fd;
35static int multiplex_out_fd;
8d9dc9f9 36static time_t last_io;
7a55d06e
MP
37static int no_flush;
38
39extern int bwlimit;
720b47f2 40extern int verbose;
6ba9279f 41extern int io_timeout;
a800434a 42extern struct stats stats;
720b47f2 43
7a55d06e
MP
44
45/** Ignore EOF errors while reading a module listing if the remote
46 version is 24 or less. */
47int kludge_around_eof = False;
48
49
554e0a8d 50static int io_error_fd = -1;
720b47f2 51
9dd891bb 52static void read_loop(int fd, char *buf, size_t len);
ff41a59f 53
8d9dc9f9
AT
54static void check_timeout(void)
55{
0adb99b9 56 extern int am_server, am_daemon;
8d9dc9f9 57 time_t t;
90ba34e2
AT
58
59 err_list_push();
8d9dc9f9
AT
60
61 if (!io_timeout) return;
62
63 if (!last_io) {
64 last_io = time(NULL);
65 return;
66 }
67
68 t = time(NULL);
69
86ffe37f 70 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
0adb99b9 71 if (!am_server && !am_daemon) {
ce6c7c63 72 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
0adb99b9
AT
73 (int)(t-last_io));
74 }
65417579 75 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
76 }
77}
78
554e0a8d
AT
79/* setup the fd used to propogate errors */
80void io_set_error_fd(int fd)
81{
82 io_error_fd = fd;
83}
84
ff41a59f 85/* read some data from the error fd and write it to the write log code */
554e0a8d
AT
86static void read_error_fd(void)
87{
88 char buf[200];
89 int n;
90 int fd = io_error_fd;
ff41a59f
AT
91 int tag, len;
92
8886f8d0
MP
93 /* io_error_fd is temporarily disabled -- is this meant to
94 * prevent indefinite recursion? */
554e0a8d
AT
95 io_error_fd = -1;
96
ff41a59f
AT
97 read_loop(fd, buf, 4);
98 tag = IVAL(buf, 0);
99
100 len = tag & 0xFFFFFF;
101 tag = tag >> 24;
102 tag -= MPLEX_BASE;
103
104 while (len) {
105 n = len;
106 if (n > (sizeof(buf)-1)) n = sizeof(buf)-1;
107 read_loop(fd, buf, n);
108 rwrite((enum logcode)tag, buf, n);
109 len -= n;
554e0a8d
AT
110 }
111
112 io_error_fd = fd;
113}
114
720b47f2 115
7a55d06e
MP
116static void whine_about_eof (void)
117{
118 /**
119 It's almost always an error to get an EOF when we're trying
120 to read from the network, because the protocol is
121 self-terminating.
122
123 However, there is one unfortunate cases where it is not,
124 which is rsync <2.4.6 sending a list of modules on a
125 server, since the list is terminated by closing the socket.
126 So, for the section of the program where that is a problem
127 (start_socket_client), kludge_around_eof is True and we
128 just exit.
129 */
130
131 if (kludge_around_eof)
132 exit_cleanup (0);
133 else {
134 rprintf (FERROR,
135 "%s: connection unexpectedly closed "
136 "(%.0f bytes read so far)\n",
137 RSYNC_NAME, (double)stats.total_read);
138
139 exit_cleanup (RERR_STREAMIO);
140 }
141}
720b47f2 142
7a55d06e
MP
143
144static void die_from_readerr (int err)
145{
146 /* this prevents us trying to write errors on a dead socket */
147 io_multiplexing_close();
148
149 rprintf(FERROR, "%s: read error: %s\n",
150 RSYNC_NAME, strerror (err));
151 exit_cleanup(RERR_STREAMIO);
152}
153
154
155/*!
c3563c46
MP
156 * Read from a socket with IO timeout. return the number of bytes
157 * read. If no bytes can be read then exit, never return a number <= 0.
158 *
8886f8d0
MP
159 * TODO: If the remote shell connection fails, then current versions
160 * actually report an "unexpected EOF" error here. Since it's a
161 * fairly common mistake to try to use rsh when ssh is required, we
162 * should trap that: if we fail to read any data at all, we should
163 * give a better explanation. We can tell whether the connection has
164 * started by looking e.g. at whether the remote version is known yet.
c3563c46 165 */
9dd891bb 166static int read_timeout (int fd, char *buf, size_t len)
8d9dc9f9 167{
4c36ddbe
AT
168 int n, ret=0;
169
ea2111d1
AT
170 io_flush();
171
4c36ddbe 172 while (ret == 0) {
7a55d06e 173 /* until we manage to read *something* */
4c36ddbe
AT
174 fd_set fds;
175 struct timeval tv;
554e0a8d 176 int fd_count = fd+1;
a57873b7 177 int count;
4c36ddbe
AT
178
179 FD_ZERO(&fds);
180 FD_SET(fd, &fds);
554e0a8d
AT
181 if (io_error_fd != -1) {
182 FD_SET(io_error_fd, &fds);
183 if (io_error_fd > fd) fd_count = io_error_fd+1;
184 }
185
8cd9fd4e 186 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
4c36ddbe
AT
187 tv.tv_usec = 0;
188
554e0a8d
AT
189 errno = 0;
190
a57873b7
AT
191 count = select(fd_count, &fds, NULL, NULL, &tv);
192
193 if (count == 0) {
194 check_timeout();
195 }
196
197 if (count <= 0) {
554e0a8d
AT
198 if (errno == EBADF) {
199 exit_cleanup(RERR_SOCKETIO);
200 }
4c36ddbe
AT
201 continue;
202 }
203
554e0a8d
AT
204 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &fds)) {
205 read_error_fd();
206 }
207
208 if (!FD_ISSET(fd, &fds)) continue;
209
4c36ddbe
AT
210 n = read(fd, buf, len);
211
8d9dc9f9
AT
212 if (n > 0) {
213 buf += n;
214 len -= n;
4c36ddbe
AT
215 ret += n;
216 if (io_timeout)
217 last_io = time(NULL);
218 continue;
7a55d06e
MP
219 } else if (n == 0) {
220 whine_about_eof ();
221 return -1; /* doesn't return */
222 } else if (n == -1) {
223 if (errno == EINTR || errno == EWOULDBLOCK ||
224 errno == EAGAIN)
225 continue;
226 else
227 die_from_readerr (errno);
8d9dc9f9 228 }
4c36ddbe 229 }
8d9dc9f9 230
4c36ddbe
AT
231 return ret;
232}
8d9dc9f9 233
7a55d06e
MP
234
235
236
237/*! Continue trying to read len bytes - don't return until len has
238 been read. */
9dd891bb 239static void read_loop (int fd, char *buf, size_t len)
4c36ddbe
AT
240{
241 while (len) {
242 int n = read_timeout(fd, buf, len);
243
244 buf += n;
245 len -= n;
8d9dc9f9
AT
246 }
247}
248
7a55d06e
MP
249
250/**
251 * Read from the file descriptor handling multiplexing - return number
252 * of bytes read.
253 *
254 * Never returns <= 0.
255 */
9dd891bb 256static int read_unbuffered(int fd, char *buf, size_t len)
8d9dc9f9
AT
257{
258 static int remaining;
909ce14f 259 int tag, ret = 0;
8d9dc9f9
AT
260 char line[1024];
261
7a55d06e 262 if (!io_multiplexing_in || fd != multiplex_in_fd)
4c36ddbe 263 return read_timeout(fd, buf, len);
8d9dc9f9
AT
264
265 while (ret == 0) {
266 if (remaining) {
267 len = MIN(len, remaining);
268 read_loop(fd, buf, len);
269 remaining -= len;
270 ret = len;
271 continue;
272 }
273
909ce14f 274 read_loop(fd, line, 4);
ff41a59f 275 tag = IVAL(line, 0);
679e7657 276
8d9dc9f9
AT
277 remaining = tag & 0xFFFFFF;
278 tag = tag >> 24;
279
909ce14f
MP
280 if (tag == MPLEX_BASE)
281 continue;
8d9dc9f9
AT
282
283 tag -= MPLEX_BASE;
284
285 if (tag != FERROR && tag != FINFO) {
909ce14f 286 rprintf(FERROR, "unexpected tag %d\n", tag);
65417579 287 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
288 }
289
909ce14f
MP
290 if (remaining > sizeof(line) - 1) {
291 rprintf(FERROR, "multiplexing overflow %d\n\n",
8d9dc9f9 292 remaining);
65417579 293 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
294 }
295
296 read_loop(fd, line, remaining);
297 line[remaining] = 0;
298
909ce14f 299 rprintf((enum logcode) tag, "%s", line);
8d9dc9f9
AT
300 remaining = 0;
301 }
302
303 return ret;
304}
305
306
909ce14f 307
4c36ddbe
AT
308/* do a buffered read from fd. don't return until all N bytes
309 have been read. If all N can't be read then exit with an error */
9dd891bb 310static void readfd (int fd, char *buffer, size_t N)
720b47f2 311{
6ba9279f
AT
312 int ret;
313 int total=0;
6ba9279f 314
6ba9279f 315 while (total < N) {
8d9dc9f9
AT
316 io_flush();
317
7a55d06e 318 ret = read_unbuffered (fd, buffer + total, N-total);
6ba9279f 319 total += ret;
7f28dbee 320 }
1b7c47cb
AT
321
322 stats.total_read += total;
720b47f2
AT
323}
324
325
b7922338 326int32 read_int(int f)
720b47f2 327{
4c36ddbe 328 char b[4];
d730b113
AT
329 int32 ret;
330
4c36ddbe 331 readfd(f,b,4);
d730b113
AT
332 ret = IVAL(b,0);
333 if (ret == (int32)0xffffffff) return -1;
334 return ret;
720b47f2
AT
335}
336
71c46176 337int64 read_longint(int f)
3a6a366f
AT
338{
339 extern int remote_version;
71c46176 340 int64 ret;
3a6a366f
AT
341 char b[8];
342 ret = read_int(f);
71c46176 343
8de330a3
AT
344 if ((int32)ret != (int32)0xffffffff) {
345 return ret;
346 }
71c46176 347
3bee6733 348#ifdef NO_INT64
9486289c 349 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
65417579 350 exit_cleanup(RERR_UNSUPPORTED);
71c46176
AT
351#else
352 if (remote_version >= 16) {
4c36ddbe 353 readfd(f,b,8);
71c46176 354 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
3a6a366f 355 }
71c46176
AT
356#endif
357
3a6a366f
AT
358 return ret;
359}
360
9dd891bb 361void read_buf(int f,char *buf,size_t len)
720b47f2 362{
4c36ddbe 363 readfd(f,buf,len);
720b47f2
AT
364}
365
9dd891bb 366void read_sbuf(int f,char *buf,size_t len)
575f2fca 367{
7a55d06e 368 read_buf (f,buf,len);
575f2fca
AT
369 buf[len] = 0;
370}
371
182dca5c
AT
372unsigned char read_byte(int f)
373{
4c36ddbe 374 unsigned char c;
7a55d06e 375 read_buf (f, (char *)&c, 1);
4c36ddbe 376 return c;
182dca5c 377}
720b47f2 378
ae682c3e 379/* write len bytes to fd */
9dd891bb 380static void writefd_unbuffered(int fd,char *buf,size_t len)
720b47f2 381{
8d9dc9f9
AT
382 int total = 0;
383 fd_set w_fds, r_fds;
4c36ddbe 384 int fd_count, count;
8d9dc9f9 385 struct timeval tv;
720b47f2 386
90ba34e2
AT
387 err_list_push();
388
e44f9a12
AT
389 no_flush++;
390
4c36ddbe 391 while (total < len) {
8d9dc9f9
AT
392 FD_ZERO(&w_fds);
393 FD_ZERO(&r_fds);
394 FD_SET(fd,&w_fds);
554e0a8d 395 fd_count = fd;
4c36ddbe 396
554e0a8d
AT
397 if (io_error_fd != -1) {
398 FD_SET(io_error_fd,&r_fds);
399 if (io_error_fd > fd_count)
400 fd_count = io_error_fd;
8d9dc9f9
AT
401 }
402
8cd9fd4e 403 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
8d9dc9f9 404 tv.tv_usec = 0;
4c36ddbe 405
554e0a8d
AT
406 errno = 0;
407
408 count = select(fd_count+1,
08f15335 409 io_error_fd != -1?&r_fds:NULL,
4c36ddbe 410 &w_fds,NULL,
8cd9fd4e 411 &tv);
4c36ddbe 412
a57873b7
AT
413 if (count == 0) {
414 check_timeout();
415 }
416
4c36ddbe 417 if (count <= 0) {
554e0a8d
AT
418 if (errno == EBADF) {
419 exit_cleanup(RERR_SOCKETIO);
420 }
8d9dc9f9
AT
421 continue;
422 }
4c36ddbe 423
554e0a8d
AT
424 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
425 read_error_fd();
426 }
427
8d9dc9f9 428 if (FD_ISSET(fd, &w_fds)) {
07b7c86c 429 int ret, n = len-total;
f0359dd0 430 ret = write(fd,buf+total,n);
4c36ddbe
AT
431
432 if (ret == -1 && errno == EINTR) {
433 continue;
434 }
435
f0359dd0
AT
436 if (ret == -1 &&
437 (errno == EWOULDBLOCK || errno == EAGAIN)) {
e92ee128 438 msleep(1);
f0359dd0
AT
439 continue;
440 }
441
4c36ddbe 442 if (ret <= 0) {
ce6c7c63
MP
443 rprintf(FERROR,
444 "error writing %d unbuffered bytes"
445 " - exiting: %s\n", len,
446 strerror(errno));
65417579 447 exit_cleanup(RERR_STREAMIO);
4c36ddbe
AT
448 }
449
ef5d23eb
DD
450 /* Sleep after writing to limit I/O bandwidth */
451 if (bwlimit)
452 {
453 tv.tv_sec = 0;
454 tv.tv_usec = ret * 1000 / bwlimit;
455 while (tv.tv_usec > 1000000)
456 {
457 tv.tv_sec++;
458 tv.tv_usec -= 1000000;
459 }
460 select(0, NULL, NULL, NULL, &tv);
461 }
462
4c36ddbe 463 total += ret;
a800434a 464
4c36ddbe
AT
465 if (io_timeout)
466 last_io = time(NULL);
8d9dc9f9 467 }
4c36ddbe 468 }
e44f9a12
AT
469
470 no_flush--;
720b47f2
AT
471}
472
8d9dc9f9 473
d6dead6b
AT
474static char *io_buffer;
475static int io_buffer_count;
476
477void io_start_buffering(int fd)
478{
8d9dc9f9 479 if (io_buffer) return;
679e7657 480 multiplex_out_fd = fd;
ff41a59f 481 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
d6dead6b
AT
482 if (!io_buffer) out_of_memory("writefd");
483 io_buffer_count = 0;
ff41a59f
AT
484}
485
486/* write an message to a multiplexed stream. If this fails then rsync
487 exits */
9dd891bb 488static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
ff41a59f
AT
489{
490 char buffer[4096];
491 int n = len;
8d9dc9f9 492
ff41a59f
AT
493 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
494
6d7b6081
AT
495 if (n > (sizeof(buffer)-4)) {
496 n = sizeof(buffer)-4;
ff41a59f
AT
497 }
498
499 memcpy(&buffer[4], buf, n);
500 writefd_unbuffered(fd, buffer, n+4);
501
502 len -= n;
503 buf += n;
504
6d7b6081
AT
505 if (len) {
506 writefd_unbuffered(fd, buf, len);
507 }
d6dead6b
AT
508}
509
ff41a59f 510
8d9dc9f9 511void io_flush(void)
d6dead6b 512{
679e7657 513 int fd = multiplex_out_fd;
90ba34e2
AT
514
515 err_list_push();
516
e44f9a12 517 if (!io_buffer_count || no_flush) return;
8d9dc9f9
AT
518
519 if (io_multiplexing_out) {
0f3203c3 520 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
8d9dc9f9 521 } else {
4c36ddbe 522 writefd_unbuffered(fd, io_buffer, io_buffer_count);
d6dead6b 523 }
8d9dc9f9
AT
524 io_buffer_count = 0;
525}
526
0ba48136
MP
527
528/* XXX: fd is ignored, which seems a little strange. */
8d9dc9f9
AT
529void io_end_buffering(int fd)
530{
531 io_flush();
532 if (!io_multiplexing_out) {
ff41a59f 533 free(io_buffer);
8d9dc9f9
AT
534 io_buffer = NULL;
535 }
d6dead6b
AT
536}
537
9dd891bb 538static void writefd(int fd,char *buf,size_t len)
d6dead6b 539{
1b7c47cb
AT
540 stats.total_written += len;
541
90ba34e2
AT
542 err_list_push();
543
554e0a8d 544 if (!io_buffer || fd != multiplex_out_fd) {
4c36ddbe
AT
545 writefd_unbuffered(fd, buf, len);
546 return;
547 }
d6dead6b
AT
548
549 while (len) {
550 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
551 if (n > 0) {
552 memcpy(io_buffer+io_buffer_count, buf, n);
553 buf += n;
554 len -= n;
555 io_buffer_count += n;
556 }
557
8d9dc9f9 558 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
d6dead6b 559 }
d6dead6b 560}
720b47f2
AT
561
562
b7922338 563void write_int(int f,int32 x)
720b47f2 564{
8d9dc9f9
AT
565 char b[4];
566 SIVAL(b,0,x);
4c36ddbe 567 writefd(f,b,4);
720b47f2
AT
568}
569
7a24c346
MP
570
571/*
572 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
573 * 64-bit types on this platform.
574 */
71c46176 575void write_longint(int f, int64 x)
3a6a366f
AT
576{
577 extern int remote_version;
578 char b[8];
3a6a366f
AT
579
580 if (remote_version < 16 || x <= 0x7FFFFFFF) {
581 write_int(f, (int)x);
582 return;
583 }
584
8de330a3 585 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
586 SIVAL(b,0,(x&0xFFFFFFFF));
587 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
588
4c36ddbe 589 writefd(f,b,8);
3a6a366f
AT
590}
591
9dd891bb 592void write_buf(int f,char *buf,size_t len)
720b47f2 593{
4c36ddbe 594 writefd(f,buf,len);
720b47f2
AT
595}
596
f0fca04e 597/* write a string to the connection */
6e4fb64e 598static void write_sbuf(int f,char *buf)
f0fca04e
AT
599{
600 write_buf(f, buf, strlen(buf));
601}
602
720b47f2 603
182dca5c
AT
604void write_byte(int f,unsigned char c)
605{
f0fca04e 606 write_buf(f,(char *)&c,1);
182dca5c
AT
607}
608
7a55d06e
MP
609
610
9dd891bb 611int read_line(int f, char *buf, size_t maxlen)
f0fca04e
AT
612{
613 while (maxlen) {
528bfcd7 614 buf[0] = 0;
f0fca04e 615 read_buf(f, buf, 1);
528bfcd7 616 if (buf[0] == 0) return 0;
f0fca04e
AT
617 if (buf[0] == '\n') {
618 buf[0] = 0;
619 break;
620 }
621 if (buf[0] != '\r') {
622 buf++;
623 maxlen--;
624 }
625 }
626 if (maxlen == 0) {
627 *buf = 0;
628 return 0;
629 }
528bfcd7 630
f0fca04e
AT
631 return 1;
632}
633
634
635void io_printf(int fd, const char *format, ...)
636{
637 va_list ap;
638 char buf[1024];
639 int len;
640
641 va_start(ap, format);
8950ac03 642 len = vsnprintf(buf, sizeof(buf), format, ap);
f0fca04e
AT
643 va_end(ap);
644
65417579 645 if (len < 0) exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
646
647 write_sbuf(fd, buf);
648}
8d9dc9f9
AT
649
650
651/* setup for multiplexing an error stream with the data stream */
652void io_start_multiplex_out(int fd)
653{
679e7657
AT
654 multiplex_out_fd = fd;
655 io_flush();
8d9dc9f9
AT
656 io_start_buffering(fd);
657 io_multiplexing_out = 1;
658}
659
660/* setup for multiplexing an error stream with the data stream */
661void io_start_multiplex_in(int fd)
662{
679e7657
AT
663 multiplex_in_fd = fd;
664 io_flush();
8d9dc9f9
AT
665 io_multiplexing_in = 1;
666}
667
554e0a8d 668/* write an message to the multiplexed error stream */
9dd891bb 669int io_multiplex_write(enum logcode code, char *buf, size_t len)
8d9dc9f9
AT
670{
671 if (!io_multiplexing_out) return 0;
672
673 io_flush();
1b7c47cb 674 stats.total_written += (len+4);
ff41a59f 675 mplex_write(multiplex_out_fd, code, buf, len);
8d9dc9f9
AT
676 return 1;
677}
678
554e0a8d
AT
679/* stop output multiplexing */
680void io_multiplexing_close(void)
681{
682 io_multiplexing_out = 0;
683}
684