prioritise reading over writing in the select loop. (this is another
[rsync/rsync.git] / io.c
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 /*
21   Utilities used in rsync 
22
23   tridge, June 1996
24   */
25 #include "rsync.h"
26
27 static int io_multiplexing_out;
28 static int io_multiplexing_in;
29 static int multiplex_in_fd;
30 static int multiplex_out_fd;
31 static time_t last_io;
32 static int eof_error=1;
33 extern int verbose;
34 extern int io_timeout;
35 extern struct stats stats;
36
37 static int buffer_f_in = -1;
38
39 void setup_readbuffer(int f_in)
40 {
41         buffer_f_in = f_in;
42 }
43
44 static void check_timeout(void)
45 {
46         time_t t;
47         
48         if (!io_timeout) return;
49
50         if (!last_io) {
51                 last_io = time(NULL);
52                 return;
53         }
54
55         t = time(NULL);
56
57         if (last_io && io_timeout && (t-last_io) >= io_timeout) {
58                 rprintf(FERROR,"io timeout after %d second - exiting\n", 
59                         (int)(t-last_io));
60                 exit_cleanup(1);
61         }
62 }
63
64
65 static char *read_buffer;
66 static char *read_buffer_p;
67 static int read_buffer_len;
68 static int read_buffer_size;
69 static int no_flush;
70
71 /* read from a socket with IO timeout. return the number of
72    bytes read. If no bytes can be read then exit, never return
73    a number <= 0 */
74 static int read_timeout(int fd, char *buf, int len)
75 {
76         int n, ret=0;
77
78         io_flush();
79
80         while (ret == 0) {
81                 fd_set fds;
82                 struct timeval tv;
83
84                 FD_ZERO(&fds);
85                 FD_SET(fd, &fds);
86                 tv.tv_sec = io_timeout;
87                 tv.tv_usec = 0;
88
89                 if (select(fd+1, &fds, NULL, NULL, 
90                            io_timeout?&tv:NULL) != 1) {
91                         check_timeout();
92                         continue;
93                 }
94
95                 n = read(fd, buf, len);
96
97                 if (n > 0) {
98                         stats.total_read += n;
99                         buf += n;
100                         len -= n;
101                         ret += n;
102                         if (io_timeout)
103                                 last_io = time(NULL);
104                         continue;
105                 }
106
107                 if (n == -1 && errno == EINTR) {
108                         continue;
109                 }
110
111                 if (n == -1 && 
112                     (errno == EAGAIN || errno == EWOULDBLOCK)) {
113                         /* this shouldn't happen, if it does then
114                            sleep for a short time to prevent us
115                            chewing too much CPU */
116                         u_sleep(100);
117                         continue;
118                 }
119
120                 if (n == 0) {
121                         if (eof_error) {
122                                 rprintf(FERROR,"EOF in read_timeout\n");
123                         }
124                         exit_cleanup(1);
125                 }
126
127                 rprintf(FERROR,"read error: %s\n", strerror(errno));
128                 exit_cleanup(1);
129         }
130
131         return ret;
132 }
133
134 /* continue trying to read len bytes - don't return until len
135    has been read */
136 static void read_loop(int fd, char *buf, int len)
137 {
138         while (len) {
139                 int n = read_timeout(fd, buf, len);
140
141                 buf += n;
142                 len -= n;
143         }
144 }
145
146 /* read from the file descriptor handling multiplexing - 
147    return number of bytes read
148    never return <= 0 */
149 static int read_unbuffered(int fd, char *buf, int len)
150 {
151         static int remaining;
152         char ibuf[4];
153         int tag, ret=0;
154         char line[1024];
155
156         if (!io_multiplexing_in || fd != multiplex_in_fd) 
157                 return read_timeout(fd, buf, len);
158
159         while (ret == 0) {
160                 if (remaining) {
161                         len = MIN(len, remaining);
162                         read_loop(fd, buf, len);
163                         remaining -= len;
164                         ret = len;
165                         continue;
166                 }
167
168                 read_loop(fd, ibuf, 4);
169                 tag = IVAL(ibuf, 0);
170
171                 remaining = tag & 0xFFFFFF;
172                 tag = tag >> 24;
173
174                 if (tag == MPLEX_BASE) continue;
175
176                 tag -= MPLEX_BASE;
177
178                 if (tag != FERROR && tag != FINFO) {
179                         rprintf(FERROR,"unexpected tag %d\n", tag);
180                         exit_cleanup(1);
181                 }
182
183                 if (remaining > sizeof(line)-1) {
184                         rprintf(FERROR,"multiplexing overflow %d\n\n", 
185                                 remaining);
186                         exit_cleanup(1);
187                 }
188
189                 read_loop(fd, line, remaining);
190                 line[remaining] = 0;
191
192                 rprintf(tag,"%s", line);
193                 remaining = 0;
194         }
195
196         return ret;
197 }
198
199
200
201 /* This function was added to overcome a deadlock problem when using
202  * ssh.  It looks like we can't allow our receive queue to get full or
203  * ssh will clag up. Uggh.  */
204 static void read_check(int f)
205 {
206         int n = 8192;
207
208         if (f == -1) return;
209
210         if (read_buffer_len == 0) {
211                 read_buffer_p = read_buffer;
212         }
213
214         if (n > MAX_READ_BUFFER/4)
215                 n = MAX_READ_BUFFER/4;
216
217         if (read_buffer_p != read_buffer) {
218                 memmove(read_buffer,read_buffer_p,read_buffer_len);
219                 read_buffer_p = read_buffer;
220         }
221
222         if (n > (read_buffer_size - read_buffer_len)) {
223                 read_buffer_size += n;
224                 read_buffer = (char *)Realloc(read_buffer,read_buffer_size);
225                 if (!read_buffer) out_of_memory("read check");      
226                 read_buffer_p = read_buffer;      
227         }
228
229         n = read_unbuffered(f,read_buffer+read_buffer_len,n);
230         read_buffer_len += n;
231 }
232
233
234 /* do a buffered read from fd. don't return until all N bytes
235    have been read. If all N can't be read then exit with an error */
236 static void readfd(int fd,char *buffer,int N)
237 {
238         int  ret;
239         int total=0;  
240         
241         if (read_buffer_len < N && N < 1024) {
242                 read_check(buffer_f_in);
243         }
244         
245         while (total < N) {
246                 if (read_buffer_len > 0 && buffer_f_in == fd) {
247                         ret = MIN(read_buffer_len,N-total);
248                         memcpy(buffer+total,read_buffer_p,ret);
249                         read_buffer_p += ret;
250                         read_buffer_len -= ret;
251                         total += ret;
252                         continue;
253                 } 
254
255                 io_flush();
256
257                 ret = read_unbuffered(fd,buffer + total,N-total);
258                 total += ret;
259         }
260 }
261
262
263 int32 read_int(int f)
264 {
265         char b[4];
266         readfd(f,b,4);
267         return IVAL(b,0);
268 }
269
270 int64 read_longint(int f)
271 {
272         extern int remote_version;
273         int64 ret;
274         char b[8];
275         ret = read_int(f);
276
277         if ((int32)ret != (int32)0xffffffff) return ret;
278
279 #ifdef NO_INT64
280         rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
281         exit_cleanup(1);
282 #else
283         if (remote_version >= 16) {
284                 readfd(f,b,8);
285                 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
286         }
287 #endif
288
289         return ret;
290 }
291
292 void read_buf(int f,char *buf,int len)
293 {
294         readfd(f,buf,len);
295 }
296
297 void read_sbuf(int f,char *buf,int len)
298 {
299         read_buf(f,buf,len);
300         buf[len] = 0;
301 }
302
303 unsigned char read_byte(int f)
304 {
305         unsigned char c;
306         read_buf(f,(char *)&c,1);
307         return c;
308 }
309
310
311
312 /* write len bytes to fd, possibly reading from buffer_f_in if set
313    in order to unclog the pipe. don't return until all len
314    bytes have been written */
315 static void writefd_unbuffered(int fd,char *buf,int len)
316 {
317         int total = 0;
318         fd_set w_fds, r_fds;
319         int fd_count, count;
320         struct timeval tv;
321         int reading;
322         int blocked=0;
323
324         no_flush++;
325
326         while (total < len) {
327                 FD_ZERO(&w_fds);
328                 FD_ZERO(&r_fds);
329                 FD_SET(fd,&w_fds);
330                 fd_count = fd+1;
331
332                 reading = (buffer_f_in != -1 && 
333                            read_buffer_len < MAX_READ_BUFFER);
334
335                 if (reading) {
336                         FD_SET(buffer_f_in,&r_fds);
337                         if (buffer_f_in > fd) 
338                                 fd_count = buffer_f_in+1;
339                 }
340
341                 tv.tv_sec = io_timeout;
342                 tv.tv_usec = 0;
343
344                 count = select(fd_count,
345                                reading?&r_fds:NULL,
346                                &w_fds,NULL,
347                                io_timeout?&tv:NULL);
348
349                 if (count <= 0) {
350                         check_timeout();
351                         continue;
352                 }
353
354                 if (reading && FD_ISSET(buffer_f_in, &r_fds)) {
355                         read_check(buffer_f_in);
356                 }
357
358                 if (FD_ISSET(fd, &w_fds)) {
359                         int n = (len-total)>>blocked;
360                         int ret = write(fd,buf+total,n?n:1);
361
362                         if (ret == -1 && errno == EINTR) {
363                                 continue;
364                         }
365
366                         if (ret == -1 && 
367                             (errno == EAGAIN || errno == EWOULDBLOCK)) {
368                                 blocked++;
369                                 continue;
370                         }
371
372                         if (ret <= 0) {
373                                 rprintf(FERROR,"erroring writing %d bytes - exiting\n", len);
374                                 exit_cleanup(1);
375                         }
376
377                         blocked = 0;
378                         total += ret;
379                         stats.total_written += ret;
380
381                         if (io_timeout)
382                                 last_io = time(NULL);
383                         continue;
384                 }
385         }
386
387         no_flush--;
388 }
389
390
391 static char *io_buffer;
392 static int io_buffer_count;
393
394 void io_start_buffering(int fd)
395 {
396         if (io_buffer) return;
397         multiplex_out_fd = fd;
398         io_buffer = (char *)malloc(IO_BUFFER_SIZE+4);
399         if (!io_buffer) out_of_memory("writefd");
400         io_buffer_count = 0;
401
402         /* leave room for the multiplex header in case it's needed */
403         io_buffer += 4;
404 }
405
406 void io_flush(void)
407 {
408         int fd = multiplex_out_fd;
409         if (!io_buffer_count || no_flush) return;
410
411         if (io_multiplexing_out) {
412                 SIVAL(io_buffer-4, 0, (MPLEX_BASE<<24) + io_buffer_count);
413                 writefd_unbuffered(fd, io_buffer-4, io_buffer_count+4);
414         } else {
415                 writefd_unbuffered(fd, io_buffer, io_buffer_count);
416         }
417         io_buffer_count = 0;
418 }
419
420 void io_end_buffering(int fd)
421 {
422         io_flush();
423         if (!io_multiplexing_out) {
424                 free(io_buffer-4);
425                 io_buffer = NULL;
426         }
427 }
428
429 static void writefd(int fd,char *buf,int len)
430 {
431         if (!io_buffer) {
432                 writefd_unbuffered(fd, buf, len);
433                 return;
434         }
435
436         while (len) {
437                 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
438                 if (n > 0) {
439                         memcpy(io_buffer+io_buffer_count, buf, n);
440                         buf += n;
441                         len -= n;
442                         io_buffer_count += n;
443                 }
444                 
445                 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
446         }
447 }
448
449
450 void write_int(int f,int32 x)
451 {
452         char b[4];
453         SIVAL(b,0,x);
454         writefd(f,b,4);
455 }
456
457 void write_longint(int f, int64 x)
458 {
459         extern int remote_version;
460         char b[8];
461
462         if (remote_version < 16 || x <= 0x7FFFFFFF) {
463                 write_int(f, (int)x);
464                 return;
465         }
466
467         write_int(f, -1);
468         SIVAL(b,0,(x&0xFFFFFFFF));
469         SIVAL(b,4,((x>>32)&0xFFFFFFFF));
470
471         writefd(f,b,8);
472 }
473
474 void write_buf(int f,char *buf,int len)
475 {
476         writefd(f,buf,len);
477 }
478
479 /* write a string to the connection */
480 void write_sbuf(int f,char *buf)
481 {
482         write_buf(f, buf, strlen(buf));
483 }
484
485
486 void write_byte(int f,unsigned char c)
487 {
488         write_buf(f,(char *)&c,1);
489 }
490
491 int read_line(int f, char *buf, int maxlen)
492 {
493         eof_error = 0;
494
495         while (maxlen) {
496                 buf[0] = 0;
497                 read_buf(f, buf, 1);
498                 if (buf[0] == 0) return 0;
499                 if (buf[0] == '\n') {
500                         buf[0] = 0;
501                         break;
502                 }
503                 if (buf[0] != '\r') {
504                         buf++;
505                         maxlen--;
506                 }
507         }
508         if (maxlen == 0) {
509                 *buf = 0;
510                 return 0;
511         }
512
513         eof_error = 1;
514
515         return 1;
516 }
517
518
519 void io_printf(int fd, const char *format, ...)
520 {
521         va_list ap;  
522         char buf[1024];
523         int len;
524         
525         va_start(ap, format);
526         len = vslprintf(buf, sizeof(buf)-1, format, ap);
527         va_end(ap);
528
529         if (len < 0) exit_cleanup(1);
530
531         write_sbuf(fd, buf);
532 }
533
534
535 /* setup for multiplexing an error stream with the data stream */
536 void io_start_multiplex_out(int fd)
537 {
538         multiplex_out_fd = fd;
539         io_flush();
540         io_start_buffering(fd);
541         io_multiplexing_out = 1;
542 }
543
544 /* setup for multiplexing an error stream with the data stream */
545 void io_start_multiplex_in(int fd)
546 {
547         multiplex_in_fd = fd;
548         io_flush();
549         if (read_buffer_len) {
550                 fprintf(stderr,"ERROR: data in read buffer at mplx start\n");
551                 exit_cleanup(1);
552         }
553
554         io_multiplexing_in = 1;
555 }
556
557 /* write an message to the error stream */
558 int io_multiplex_write(int f, char *buf, int len)
559 {
560         if (!io_multiplexing_out) return 0;
561
562         io_flush();
563
564         SIVAL(io_buffer-4, 0, ((MPLEX_BASE + f)<<24) + len);
565         memcpy(io_buffer, buf, len);
566
567         writefd_unbuffered(multiplex_out_fd, io_buffer-4, len+4);
568         return 1;
569 }
570
571 void io_close_input(int fd)
572 {
573         buffer_f_in = -1;
574 }