Commit | Line | Data |
---|---|---|
7a24c346 | 1 | /* -*- c-file-style: "linux" -*- |
d62bcc17 WD |
2 | * |
3 | * Copyright (C) 1996-2001 by Andrew Tridgell | |
880da007 MP |
4 | * Copyright (C) Paul Mackerras 1996 |
5 | * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> | |
d62bcc17 | 6 | * |
880da007 MP |
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. | |
d62bcc17 | 11 | * |
880da007 MP |
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. | |
d62bcc17 | 16 | * |
880da007 MP |
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 | */ | |
720b47f2 | 21 | |
87ee2481 | 22 | /** |
87ee2481 MP |
23 | * @file io.c |
24 | * | |
6ed6d7f5 | 25 | * Socket and pipe I/O utilities used in rsync. |
87ee2481 MP |
26 | * |
27 | * rsync provides its own multiplexing system, which is used to send | |
28 | * stderr and stdout over a single socket. We need this because | |
29 | * stdout normally carries the binary data stream, and stderr all our | |
30 | * error messages. | |
31 | * | |
32 | * For historical reasons this is off during the start of the | |
33 | * connection, but it's switched on quite early using | |
34 | * io_start_multiplex_out() and io_start_multiplex_in(). | |
35 | **/ | |
720b47f2 | 36 | |
720b47f2 AT |
37 | #include "rsync.h" |
38 | ||
880da007 | 39 | /** If no timeout is specified then use a 60 second select timeout */ |
8cd9fd4e AT |
40 | #define SELECT_TIMEOUT 60 |
41 | ||
8d9dc9f9 AT |
42 | static int io_multiplexing_out; |
43 | static int io_multiplexing_in; | |
76c21947 WD |
44 | static int multiplex_in_fd = -1; |
45 | static int multiplex_out_fd = -1; | |
8d9dc9f9 | 46 | static time_t last_io; |
7a55d06e MP |
47 | static int no_flush; |
48 | ||
49 | extern int bwlimit; | |
71e58630 | 50 | extern size_t bwlimit_writemax; |
720b47f2 | 51 | extern int verbose; |
6ba9279f | 52 | extern int io_timeout; |
d17e1dd2 WD |
53 | extern int am_server; |
54 | extern int am_daemon; | |
55 | extern int am_sender; | |
e626b29e WD |
56 | extern int eol_nulls; |
57 | extern char *remote_filesfrom_file; | |
a800434a | 58 | extern struct stats stats; |
720b47f2 | 59 | |
a86179f4 | 60 | const char phase_unknown[] = "unknown"; |
e626b29e | 61 | int select_timeout = SELECT_TIMEOUT; |
805edf9d | 62 | |
98b332ed MP |
63 | /** |
64 | * The connection might be dropped at some point; perhaps because the | |
65 | * remote instance crashed. Just giving the offset on the stream is | |
66 | * not very helpful. So instead we try to make io_phase_name point to | |
67 | * something useful. | |
eca2adb4 | 68 | * |
6ed6d7f5 | 69 | * For buffered/multiplexed I/O these names will be somewhat |
805edf9d | 70 | * approximate; perhaps for ease of support we would rather make the |
6ed6d7f5 | 71 | * buffer always flush when a single application-level I/O finishes. |
805edf9d | 72 | * |
eca2adb4 MP |
73 | * @todo Perhaps we want some simple stack functionality, but there's |
74 | * no need to overdo it. | |
98b332ed | 75 | **/ |
805edf9d MP |
76 | const char *io_write_phase = phase_unknown; |
77 | const char *io_read_phase = phase_unknown; | |
98b332ed | 78 | |
7a55d06e MP |
79 | /** Ignore EOF errors while reading a module listing if the remote |
80 | version is 24 or less. */ | |
81 | int kludge_around_eof = False; | |
82 | ||
d17e1dd2 WD |
83 | int msg_fd_in = -1; |
84 | int msg_fd_out = -1; | |
7a55d06e | 85 | |
56014c8c WD |
86 | static int io_filesfrom_f_in = -1; |
87 | static int io_filesfrom_f_out = -1; | |
88 | static char io_filesfrom_buf[2048]; | |
89 | static char *io_filesfrom_bp; | |
90 | static char io_filesfrom_lastchar; | |
91 | static int io_filesfrom_buflen; | |
720b47f2 | 92 | |
9dd891bb | 93 | static void read_loop(int fd, char *buf, size_t len); |
ff41a59f | 94 | |
d17e1dd2 WD |
95 | struct redo_list { |
96 | struct redo_list *next; | |
97 | int num; | |
98 | }; | |
99 | ||
100 | static struct redo_list *redo_list_head; | |
101 | static struct redo_list *redo_list_tail; | |
102 | ||
103 | struct msg_list { | |
104 | struct msg_list *next; | |
105 | char *buf; | |
106 | int len; | |
107 | }; | |
108 | ||
109 | static struct msg_list *msg_list_head; | |
110 | static struct msg_list *msg_list_tail; | |
111 | ||
112 | static void redo_list_add(int num) | |
113 | { | |
114 | struct redo_list *rl; | |
115 | ||
116 | if (!(rl = new(struct redo_list))) | |
117 | exit_cleanup(RERR_MALLOC); | |
118 | rl->next = NULL; | |
119 | rl->num = num; | |
120 | if (redo_list_tail) | |
121 | redo_list_tail->next = rl; | |
122 | else | |
123 | redo_list_head = rl; | |
124 | redo_list_tail = rl; | |
125 | } | |
126 | ||
8d9dc9f9 AT |
127 | static void check_timeout(void) |
128 | { | |
129 | time_t t; | |
90ba34e2 | 130 | |
d17e1dd2 WD |
131 | if (!io_timeout) |
132 | return; | |
8d9dc9f9 AT |
133 | |
134 | if (!last_io) { | |
135 | last_io = time(NULL); | |
136 | return; | |
137 | } | |
138 | ||
139 | t = time(NULL); | |
140 | ||
86ffe37f | 141 | if (last_io && io_timeout && (t-last_io) >= io_timeout) { |
0adb99b9 | 142 | if (!am_server && !am_daemon) { |
d62bcc17 | 143 | rprintf(FERROR, "io timeout after %d seconds - exiting\n", |
0adb99b9 AT |
144 | (int)(t-last_io)); |
145 | } | |
65417579 | 146 | exit_cleanup(RERR_TIMEOUT); |
8d9dc9f9 AT |
147 | } |
148 | } | |
149 | ||
d17e1dd2 WD |
150 | /** Setup the fd used to receive MSG_* messages. Only needed when |
151 | * we're the generator because the sender and receiver both use the | |
6ed6d7f5 | 152 | * multiplexed I/O setup. */ |
d17e1dd2 WD |
153 | void set_msg_fd_in(int fd) |
154 | { | |
155 | msg_fd_in = fd; | |
156 | } | |
157 | ||
158 | /** Setup the fd used to send our MSG_* messages. Only needed when | |
159 | * we're the receiver because the generator and the sender both use | |
6ed6d7f5 | 160 | * the multiplexed I/O setup. */ |
d17e1dd2 WD |
161 | void set_msg_fd_out(int fd) |
162 | { | |
163 | msg_fd_out = fd; | |
164 | set_nonblocking(msg_fd_out); | |
165 | } | |
166 | ||
167 | /* Add a message to the pending MSG_* list. */ | |
168 | static void msg_list_add(int code, char *buf, int len) | |
554e0a8d | 169 | { |
d17e1dd2 WD |
170 | struct msg_list *ml; |
171 | ||
172 | if (!(ml = new(struct msg_list))) | |
173 | exit_cleanup(RERR_MALLOC); | |
174 | ml->next = NULL; | |
175 | if (!(ml->buf = new_array(char, len+4))) | |
176 | exit_cleanup(RERR_MALLOC); | |
177 | SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len); | |
178 | memcpy(ml->buf+4, buf, len); | |
179 | ml->len = len+4; | |
180 | if (msg_list_tail) | |
181 | msg_list_tail->next = ml; | |
182 | else | |
183 | msg_list_head = ml; | |
184 | msg_list_tail = ml; | |
554e0a8d AT |
185 | } |
186 | ||
d17e1dd2 WD |
187 | void send_msg(enum msgcode code, char *buf, int len) |
188 | { | |
189 | msg_list_add(code, buf, len); | |
190 | msg_list_push(NORMAL_FLUSH); | |
191 | } | |
192 | ||
193 | /** Read a message from the MSG_* fd and dispatch it. This is only | |
194 | * called by the generator. */ | |
195 | static void read_msg_fd(void) | |
554e0a8d AT |
196 | { |
197 | char buf[200]; | |
06ce139f | 198 | size_t n; |
d17e1dd2 | 199 | int fd = msg_fd_in; |
ff41a59f AT |
200 | int tag, len; |
201 | ||
d17e1dd2 WD |
202 | /* Temporarily disable msg_fd_in. This is needed because we |
203 | * may call a write routine that could try to call us back. */ | |
204 | msg_fd_in = -1; | |
554e0a8d | 205 | |
ff41a59f AT |
206 | read_loop(fd, buf, 4); |
207 | tag = IVAL(buf, 0); | |
208 | ||
209 | len = tag & 0xFFFFFF; | |
d17e1dd2 | 210 | tag = (tag >> 24) - MPLEX_BASE; |
ff41a59f | 211 | |
d17e1dd2 WD |
212 | switch (tag) { |
213 | case MSG_DONE: | |
13c7bcbb WD |
214 | if (len != 0) { |
215 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); | |
d17e1dd2 | 216 | exit_cleanup(RERR_STREAMIO); |
13c7bcbb | 217 | } |
d17e1dd2 WD |
218 | redo_list_add(-1); |
219 | break; | |
220 | case MSG_REDO: | |
13c7bcbb WD |
221 | if (len != 4) { |
222 | rprintf(FERROR, "invalid message %d:%d\n", tag, len); | |
d17e1dd2 | 223 | exit_cleanup(RERR_STREAMIO); |
13c7bcbb | 224 | } |
d17e1dd2 WD |
225 | read_loop(fd, buf, 4); |
226 | redo_list_add(IVAL(buf,0)); | |
227 | break; | |
228 | case MSG_INFO: | |
229 | case MSG_ERROR: | |
230 | case MSG_LOG: | |
231 | while (len) { | |
232 | n = len; | |
233 | if (n >= sizeof buf) | |
234 | n = sizeof buf - 1; | |
235 | read_loop(fd, buf, n); | |
236 | rwrite((enum logcode)tag, buf, n); | |
237 | len -= n; | |
238 | } | |
239 | break; | |
240 | default: | |
13c7bcbb | 241 | rprintf(FERROR, "unknown message %d:%d\n", tag, len); |
d17e1dd2 WD |
242 | exit_cleanup(RERR_STREAMIO); |
243 | } | |
244 | ||
245 | msg_fd_in = fd; | |
246 | } | |
247 | ||
248 | /* Try to push messages off the list onto the wire. If we leave with more | |
249 | * to do, return 0. On error, return -1. If everything flushed, return 1. | |
250 | * This is only called by the receiver. */ | |
251 | int msg_list_push(int flush_it_all) | |
252 | { | |
253 | static int written = 0; | |
254 | struct timeval tv; | |
255 | fd_set fds; | |
256 | ||
257 | if (msg_fd_out < 0) | |
258 | return -1; | |
259 | ||
260 | while (msg_list_head) { | |
261 | struct msg_list *ml = msg_list_head; | |
262 | int n = write(msg_fd_out, ml->buf + written, ml->len - written); | |
263 | if (n < 0) { | |
264 | if (errno == EINTR) | |
265 | continue; | |
266 | if (errno != EWOULDBLOCK && errno != EAGAIN) | |
267 | return -1; | |
268 | if (!flush_it_all) | |
269 | return 0; | |
270 | FD_ZERO(&fds); | |
271 | FD_SET(msg_fd_out, &fds); | |
e626b29e | 272 | tv.tv_sec = select_timeout; |
d17e1dd2 WD |
273 | tv.tv_usec = 0; |
274 | if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv)) | |
275 | check_timeout(); | |
276 | } else if ((written += n) == ml->len) { | |
277 | free(ml->buf); | |
278 | msg_list_head = ml->next; | |
279 | if (!msg_list_head) | |
280 | msg_list_tail = NULL; | |
281 | free(ml); | |
282 | written = 0; | |
283 | } | |
554e0a8d | 284 | } |
d17e1dd2 WD |
285 | return 1; |
286 | } | |
287 | ||
288 | int get_redo_num(void) | |
289 | { | |
290 | struct redo_list *next; | |
291 | int num; | |
292 | ||
293 | while (!redo_list_head) | |
294 | read_msg_fd(); | |
554e0a8d | 295 | |
d17e1dd2 WD |
296 | num = redo_list_head->num; |
297 | next = redo_list_head->next; | |
298 | free(redo_list_head); | |
299 | redo_list_head = next; | |
300 | if (!next) | |
301 | redo_list_tail = NULL; | |
302 | ||
303 | return num; | |
554e0a8d AT |
304 | } |
305 | ||
56014c8c WD |
306 | /** |
307 | * When we're the receiver and we have a local --files-from list of names | |
308 | * that needs to be sent over the socket to the sender, we have to do two | |
309 | * things at the same time: send the sender a list of what files we're | |
310 | * processing and read the incoming file+info list from the sender. We do | |
311 | * this by augmenting the read_timeout() function to copy this data. It | |
312 | * uses the io_filesfrom_buf to read a block of data from f_in (when it is | |
313 | * ready, since it might be a pipe) and then blast it out f_out (when it | |
314 | * is ready to receive more data). | |
315 | */ | |
316 | void io_set_filesfrom_fds(int f_in, int f_out) | |
317 | { | |
318 | io_filesfrom_f_in = f_in; | |
319 | io_filesfrom_f_out = f_out; | |
320 | io_filesfrom_bp = io_filesfrom_buf; | |
321 | io_filesfrom_lastchar = '\0'; | |
322 | io_filesfrom_buflen = 0; | |
323 | } | |
720b47f2 | 324 | |
880da007 MP |
325 | /** |
326 | * It's almost always an error to get an EOF when we're trying to read | |
327 | * from the network, because the protocol is self-terminating. | |
328 | * | |
329 | * However, there is one unfortunate cases where it is not, which is | |
330 | * rsync <2.4.6 sending a list of modules on a server, since the list | |
331 | * is terminated by closing the socket. So, for the section of the | |
332 | * program where that is a problem (start_socket_client), | |
333 | * kludge_around_eof is True and we just exit. | |
334 | */ | |
3151cbae | 335 | static void whine_about_eof(void) |
7a55d06e | 336 | { |
7a55d06e | 337 | if (kludge_around_eof) |
3151cbae | 338 | exit_cleanup(0); |
7a55d06e | 339 | else { |
d62bcc17 | 340 | rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed " |
3151cbae | 341 | "(%.0f bytes read so far)\n", |
d62bcc17 | 342 | (double)stats.total_read); |
3151cbae WD |
343 | |
344 | exit_cleanup(RERR_STREAMIO); | |
7a55d06e MP |
345 | } |
346 | } | |
720b47f2 | 347 | |
7a55d06e | 348 | |
3151cbae | 349 | static void die_from_readerr(int err) |
7a55d06e MP |
350 | { |
351 | /* this prevents us trying to write errors on a dead socket */ | |
352 | io_multiplexing_close(); | |
3151cbae | 353 | |
d62bcc17 | 354 | rsyserr(FERROR, err, "read error"); |
7a55d06e MP |
355 | exit_cleanup(RERR_STREAMIO); |
356 | } | |
357 | ||
358 | ||
880da007 | 359 | /** |
6ed6d7f5 | 360 | * Read from a socket with I/O timeout. return the number of bytes |
c3563c46 MP |
361 | * read. If no bytes can be read then exit, never return a number <= 0. |
362 | * | |
8886f8d0 MP |
363 | * TODO: If the remote shell connection fails, then current versions |
364 | * actually report an "unexpected EOF" error here. Since it's a | |
365 | * fairly common mistake to try to use rsh when ssh is required, we | |
366 | * should trap that: if we fail to read any data at all, we should | |
367 | * give a better explanation. We can tell whether the connection has | |
368 | * started by looking e.g. at whether the remote version is known yet. | |
c3563c46 | 369 | */ |
3151cbae | 370 | static int read_timeout(int fd, char *buf, size_t len) |
8d9dc9f9 | 371 | { |
d62bcc17 | 372 | int n, ret = 0; |
4c36ddbe | 373 | |
d17e1dd2 | 374 | io_flush(NORMAL_FLUSH); |
ea2111d1 | 375 | |
4c36ddbe | 376 | while (ret == 0) { |
7a55d06e | 377 | /* until we manage to read *something* */ |
56014c8c | 378 | fd_set r_fds, w_fds; |
4c36ddbe | 379 | struct timeval tv; |
554e0a8d | 380 | int fd_count = fd+1; |
a57873b7 | 381 | int count; |
4c36ddbe | 382 | |
56014c8c WD |
383 | FD_ZERO(&r_fds); |
384 | FD_SET(fd, &r_fds); | |
d17e1dd2 WD |
385 | if (msg_fd_in >= 0) { |
386 | FD_SET(msg_fd_in, &r_fds); | |
387 | if (msg_fd_in >= fd_count) | |
388 | fd_count = msg_fd_in+1; | |
4033b80b WD |
389 | } else if (msg_list_head) { |
390 | FD_SET(msg_fd_out, &w_fds); | |
391 | if (msg_fd_out >= fd_count) | |
392 | fd_count = msg_fd_out+1; | |
56014c8c | 393 | } |
3309507d | 394 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
395 | int new_fd; |
396 | if (io_filesfrom_buflen == 0) { | |
3309507d | 397 | if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
398 | FD_SET(io_filesfrom_f_in, &r_fds); |
399 | new_fd = io_filesfrom_f_in; | |
400 | } else { | |
401 | io_filesfrom_f_out = -1; | |
402 | new_fd = -1; | |
403 | } | |
404 | } else { | |
405 | FD_ZERO(&w_fds); | |
406 | FD_SET(io_filesfrom_f_out, &w_fds); | |
407 | new_fd = io_filesfrom_f_out; | |
408 | } | |
d17e1dd2 WD |
409 | if (new_fd >= fd_count) |
410 | fd_count = new_fd+1; | |
554e0a8d AT |
411 | } |
412 | ||
e626b29e | 413 | tv.tv_sec = select_timeout; |
4c36ddbe AT |
414 | tv.tv_usec = 0; |
415 | ||
554e0a8d AT |
416 | errno = 0; |
417 | ||
56014c8c WD |
418 | count = select(fd_count, &r_fds, |
419 | io_filesfrom_buflen? &w_fds : NULL, | |
420 | NULL, &tv); | |
a57873b7 | 421 | |
a57873b7 | 422 | if (count <= 0) { |
4033b80b | 423 | check_timeout(); |
f89b9368 | 424 | if (errno == EBADF) |
554e0a8d | 425 | exit_cleanup(RERR_SOCKETIO); |
4c36ddbe AT |
426 | continue; |
427 | } | |
428 | ||
d17e1dd2 WD |
429 | if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds)) |
430 | read_msg_fd(); | |
4033b80b WD |
431 | else if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds)) |
432 | msg_list_push(NORMAL_FLUSH); | |
554e0a8d | 433 | |
3309507d | 434 | if (io_filesfrom_f_out >= 0) { |
56014c8c WD |
435 | if (io_filesfrom_buflen) { |
436 | if (FD_ISSET(io_filesfrom_f_out, &w_fds)) { | |
437 | int l = write(io_filesfrom_f_out, | |
438 | io_filesfrom_bp, | |
439 | io_filesfrom_buflen); | |
440 | if (l > 0) { | |
441 | if (!(io_filesfrom_buflen -= l)) | |
442 | io_filesfrom_bp = io_filesfrom_buf; | |
443 | else | |
444 | io_filesfrom_bp += l; | |
445 | } else { | |
446 | /* XXX should we complain? */ | |
447 | io_filesfrom_f_out = -1; | |
448 | } | |
449 | } | |
3309507d | 450 | } else if (io_filesfrom_f_in >= 0) { |
56014c8c WD |
451 | if (FD_ISSET(io_filesfrom_f_in, &r_fds)) { |
452 | int l = read(io_filesfrom_f_in, | |
453 | io_filesfrom_buf, | |
454 | sizeof io_filesfrom_buf); | |
455 | if (l <= 0) { | |
456 | /* Send end-of-file marker */ | |
457 | io_filesfrom_buf[0] = '\0'; | |
458 | io_filesfrom_buf[1] = '\0'; | |
459 | io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1; | |
460 | io_filesfrom_f_in = -1; | |
461 | } else { | |
56014c8c WD |
462 | if (!eol_nulls) { |
463 | char *s = io_filesfrom_buf + l; | |
464 | /* Transform CR and/or LF into '\0' */ | |
465 | while (s-- > io_filesfrom_buf) { | |
466 | if (*s == '\n' || *s == '\r') | |
467 | *s = '\0'; | |
468 | } | |
469 | } | |
470 | if (!io_filesfrom_lastchar) { | |
471 | /* Last buf ended with a '\0', so don't | |
472 | * let this buf start with one. */ | |
473 | while (l && !*io_filesfrom_bp) | |
474 | io_filesfrom_bp++, l--; | |
475 | } | |
476 | if (!l) | |
477 | io_filesfrom_bp = io_filesfrom_buf; | |
478 | else { | |
479 | char *f = io_filesfrom_bp; | |
480 | char *t = f; | |
481 | char *eob = f + l; | |
482 | /* Eliminate any multi-'\0' runs. */ | |
483 | while (f != eob) { | |
484 | if (!(*t++ = *f++)) { | |
485 | while (f != eob && !*f) | |
486 | f++, l--; | |
487 | } | |
488 | } | |
489 | io_filesfrom_lastchar = f[-1]; | |
490 | } | |
491 | io_filesfrom_buflen = l; | |
492 | } | |
493 | } | |
494 | } | |
495 | } | |
496 | ||
f89b9368 WD |
497 | if (!FD_ISSET(fd, &r_fds)) |
498 | continue; | |
554e0a8d | 499 | |
4c36ddbe AT |
500 | n = read(fd, buf, len); |
501 | ||
8d9dc9f9 AT |
502 | if (n > 0) { |
503 | buf += n; | |
504 | len -= n; | |
4c36ddbe AT |
505 | ret += n; |
506 | if (io_timeout) | |
507 | last_io = time(NULL); | |
508 | continue; | |
7a55d06e | 509 | } else if (n == 0) { |
3151cbae | 510 | whine_about_eof(); |
7a55d06e | 511 | return -1; /* doesn't return */ |
3309507d | 512 | } else if (n < 0) { |
d62bcc17 WD |
513 | if (errno == EINTR || errno == EWOULDBLOCK |
514 | || errno == EAGAIN) | |
7a55d06e | 515 | continue; |
3151cbae | 516 | die_from_readerr(errno); |
8d9dc9f9 | 517 | } |
4c36ddbe | 518 | } |
8d9dc9f9 | 519 | |
4c36ddbe AT |
520 | return ret; |
521 | } | |
8d9dc9f9 | 522 | |
56014c8c WD |
523 | /** |
524 | * Read a line into the "fname" buffer (which must be at least MAXPATHLEN | |
525 | * characters long). | |
526 | */ | |
527 | int read_filesfrom_line(int fd, char *fname) | |
528 | { | |
529 | char ch, *s, *eob = fname + MAXPATHLEN - 1; | |
530 | int cnt; | |
55d5937d | 531 | int reading_remotely = remote_filesfrom_file != NULL; |
56014c8c WD |
532 | int nulls = eol_nulls || reading_remotely; |
533 | ||
534 | start: | |
535 | s = fname; | |
536 | while (1) { | |
537 | cnt = read(fd, &ch, 1); | |
538 | if (cnt < 0 && (errno == EWOULDBLOCK | |
539 | || errno == EINTR || errno == EAGAIN)) { | |
540 | struct timeval tv; | |
541 | fd_set fds; | |
542 | FD_ZERO(&fds); | |
543 | FD_SET(fd, &fds); | |
e626b29e | 544 | tv.tv_sec = select_timeout; |
56014c8c WD |
545 | tv.tv_usec = 0; |
546 | if (!select(fd+1, &fds, NULL, NULL, &tv)) | |
547 | check_timeout(); | |
548 | continue; | |
549 | } | |
550 | if (cnt != 1) | |
551 | break; | |
552 | if (nulls? !ch : (ch == '\r' || ch == '\n')) { | |
553 | /* Skip empty lines if reading locally. */ | |
554 | if (!reading_remotely && s == fname) | |
555 | continue; | |
556 | break; | |
557 | } | |
558 | if (s < eob) | |
559 | *s++ = ch; | |
560 | } | |
561 | *s = '\0'; | |
7a55d06e | 562 | |
6b45fcf1 WD |
563 | /* Dump comments. */ |
564 | if (*fname == '#' || *fname == ';') | |
56014c8c WD |
565 | goto start; |
566 | ||
567 | return s - fname; | |
568 | } | |
7a55d06e MP |
569 | |
570 | ||
880da007 MP |
571 | /** |
572 | * Continue trying to read len bytes - don't return until len has been | |
573 | * read. | |
574 | **/ | |
3151cbae | 575 | static void read_loop(int fd, char *buf, size_t len) |
4c36ddbe AT |
576 | { |
577 | while (len) { | |
578 | int n = read_timeout(fd, buf, len); | |
579 | ||
580 | buf += n; | |
581 | len -= n; | |
8d9dc9f9 AT |
582 | } |
583 | } | |
584 | ||
7a55d06e MP |
585 | |
586 | /** | |
587 | * Read from the file descriptor handling multiplexing - return number | |
588 | * of bytes read. | |
d62bcc17 WD |
589 | * |
590 | * Never returns <= 0. | |
7a55d06e | 591 | */ |
9dd891bb | 592 | static int read_unbuffered(int fd, char *buf, size_t len) |
8d9dc9f9 | 593 | { |
6fe25398 | 594 | static size_t remaining; |
909ce14f | 595 | int tag, ret = 0; |
8d9dc9f9 | 596 | char line[1024]; |
76c21947 WD |
597 | static char *buffer; |
598 | static size_t bufferIdx = 0; | |
599 | static size_t bufferSz; | |
8d9dc9f9 | 600 | |
76c21947 | 601 | if (fd != multiplex_in_fd) |
4c36ddbe | 602 | return read_timeout(fd, buf, len); |
8d9dc9f9 | 603 | |
76c21947 WD |
604 | if (!io_multiplexing_in && remaining == 0) { |
605 | if (!buffer) { | |
606 | bufferSz = 2 * IO_BUFFER_SIZE; | |
607 | buffer = new_array(char, bufferSz); | |
f89b9368 WD |
608 | if (!buffer) |
609 | out_of_memory("read_unbuffered"); | |
76c21947 WD |
610 | } |
611 | remaining = read_timeout(fd, buffer, bufferSz); | |
612 | bufferIdx = 0; | |
613 | } | |
614 | ||
8d9dc9f9 AT |
615 | while (ret == 0) { |
616 | if (remaining) { | |
617 | len = MIN(len, remaining); | |
76c21947 WD |
618 | memcpy(buf, buffer + bufferIdx, len); |
619 | bufferIdx += len; | |
8d9dc9f9 AT |
620 | remaining -= len; |
621 | ret = len; | |
76c21947 | 622 | break; |
8d9dc9f9 AT |
623 | } |
624 | ||
909ce14f | 625 | read_loop(fd, line, 4); |
ff41a59f | 626 | tag = IVAL(line, 0); |
679e7657 | 627 | |
8d9dc9f9 | 628 | remaining = tag & 0xFFFFFF; |
d17e1dd2 | 629 | tag = (tag >> 24) - MPLEX_BASE; |
8d9dc9f9 | 630 | |
d17e1dd2 WD |
631 | switch (tag) { |
632 | case MSG_DATA: | |
76c21947 WD |
633 | if (!buffer || remaining > bufferSz) { |
634 | buffer = realloc_array(buffer, char, remaining); | |
f89b9368 WD |
635 | if (!buffer) |
636 | out_of_memory("read_unbuffered"); | |
76c21947 WD |
637 | bufferSz = remaining; |
638 | } | |
639 | read_loop(fd, buffer, remaining); | |
640 | bufferIdx = 0; | |
d17e1dd2 WD |
641 | break; |
642 | case MSG_INFO: | |
643 | case MSG_ERROR: | |
644 | if (remaining >= sizeof line) { | |
645 | rprintf(FERROR, "multiplexing overflow %d:%ld\n\n", | |
646 | tag, (long)remaining); | |
647 | exit_cleanup(RERR_STREAMIO); | |
648 | } | |
649 | read_loop(fd, line, remaining); | |
650 | rwrite((enum logcode)tag, line, remaining); | |
651 | remaining = 0; | |
652 | break; | |
653 | default: | |
909ce14f | 654 | rprintf(FERROR, "unexpected tag %d\n", tag); |
65417579 | 655 | exit_cleanup(RERR_STREAMIO); |
8d9dc9f9 | 656 | } |
8d9dc9f9 AT |
657 | } |
658 | ||
76c21947 | 659 | if (remaining == 0) |
d17e1dd2 | 660 | io_flush(NORMAL_FLUSH); |
76c21947 | 661 | |
8d9dc9f9 AT |
662 | return ret; |
663 | } | |
664 | ||
665 | ||
909ce14f | 666 | |
880da007 MP |
667 | /** |
668 | * Do a buffered read from @p fd. Don't return until all @p n bytes | |
669 | * have been read. If all @p n can't be read then exit with an | |
670 | * error. | |
671 | **/ | |
3151cbae | 672 | static void readfd(int fd, char *buffer, size_t N) |
720b47f2 | 673 | { |
6ba9279f | 674 | int ret; |
d62bcc17 | 675 | size_t total = 0; |
3151cbae | 676 | |
6ba9279f | 677 | while (total < N) { |
3151cbae | 678 | ret = read_unbuffered(fd, buffer + total, N-total); |
6ba9279f | 679 | total += ret; |
7f28dbee | 680 | } |
1b7c47cb AT |
681 | |
682 | stats.total_read += total; | |
720b47f2 AT |
683 | } |
684 | ||
685 | ||
b7922338 | 686 | int32 read_int(int f) |
720b47f2 | 687 | { |
4c36ddbe | 688 | char b[4]; |
d730b113 AT |
689 | int32 ret; |
690 | ||
4c36ddbe | 691 | readfd(f,b,4); |
d730b113 | 692 | ret = IVAL(b,0); |
f89b9368 WD |
693 | if (ret == (int32)0xffffffff) |
694 | return -1; | |
d730b113 | 695 | return ret; |
720b47f2 AT |
696 | } |
697 | ||
71c46176 | 698 | int64 read_longint(int f) |
3a6a366f | 699 | { |
71c46176 | 700 | int64 ret; |
3a6a366f AT |
701 | char b[8]; |
702 | ret = read_int(f); | |
71c46176 | 703 | |
f89b9368 | 704 | if ((int32)ret != (int32)0xffffffff) |
8de330a3 | 705 | return ret; |
71c46176 | 706 | |
3bee6733 | 707 | #ifdef NO_INT64 |
9486289c | 708 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); |
65417579 | 709 | exit_cleanup(RERR_UNSUPPORTED); |
71c46176 | 710 | #else |
91c4da3f S |
711 | readfd(f,b,8); |
712 | ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32); | |
71c46176 AT |
713 | #endif |
714 | ||
3a6a366f AT |
715 | return ret; |
716 | } | |
717 | ||
9dd891bb | 718 | void read_buf(int f,char *buf,size_t len) |
720b47f2 | 719 | { |
4c36ddbe | 720 | readfd(f,buf,len); |
720b47f2 AT |
721 | } |
722 | ||
9dd891bb | 723 | void read_sbuf(int f,char *buf,size_t len) |
575f2fca | 724 | { |
3151cbae | 725 | read_buf(f,buf,len); |
575f2fca AT |
726 | buf[len] = 0; |
727 | } | |
728 | ||
182dca5c AT |
729 | unsigned char read_byte(int f) |
730 | { | |
4c36ddbe | 731 | unsigned char c; |
3151cbae | 732 | read_buf(f, (char *)&c, 1); |
4c36ddbe | 733 | return c; |
182dca5c | 734 | } |
720b47f2 | 735 | |
880da007 | 736 | |
08571358 MP |
737 | /** |
738 | * Sleep after writing to limit I/O bandwidth usage. | |
739 | * | |
740 | * @todo Rather than sleeping after each write, it might be better to | |
741 | * use some kind of averaging. The current algorithm seems to always | |
742 | * use a bit less bandwidth than specified, because it doesn't make up | |
743 | * for slow periods. But arguably this is a feature. In addition, we | |
744 | * ought to take the time used to write the data into account. | |
71e58630 WD |
745 | * |
746 | * During some phases of big transfers (file FOO is uptodate) this is | |
747 | * called with a small bytes_written every time. As the kernel has to | |
748 | * round small waits up to guarantee that we actually wait at least the | |
749 | * requested number of microseconds, this can become grossly inaccurate. | |
750 | * We therefore keep track of the bytes we've written over time and only | |
751 | * sleep when the accumulated delay is at least 1 tenth of a second. | |
08571358 MP |
752 | **/ |
753 | static void sleep_for_bwlimit(int bytes_written) | |
754 | { | |
71e58630 WD |
755 | static struct timeval prior_tv; |
756 | static long total_written = 0; | |
757 | struct timeval tv, start_tv; | |
758 | long elapsed_usec, sleep_usec; | |
759 | ||
760 | #define ONE_SEC 1000000L /* # of microseconds in a second */ | |
08571358 MP |
761 | |
762 | if (!bwlimit) | |
763 | return; | |
e681e820 | 764 | |
71e58630 WD |
765 | total_written += bytes_written; |
766 | ||
767 | gettimeofday(&start_tv, NULL); | |
768 | if (prior_tv.tv_sec) { | |
769 | elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC | |
770 | + (start_tv.tv_usec - prior_tv.tv_usec); | |
771 | total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024); | |
772 | if (total_written < 0) | |
773 | total_written = 0; | |
774 | } | |
3151cbae | 775 | |
71e58630 WD |
776 | sleep_usec = total_written * (ONE_SEC/1024) / bwlimit; |
777 | if (sleep_usec < ONE_SEC / 10) { | |
778 | prior_tv = start_tv; | |
779 | return; | |
780 | } | |
08571358 | 781 | |
71e58630 WD |
782 | tv.tv_sec = sleep_usec / ONE_SEC; |
783 | tv.tv_usec = sleep_usec % ONE_SEC; | |
98b332ed | 784 | select(0, NULL, NULL, NULL, &tv); |
71e58630 WD |
785 | |
786 | gettimeofday(&prior_tv, NULL); | |
787 | elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC | |
788 | + (prior_tv.tv_usec - start_tv.tv_usec); | |
789 | total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024); | |
08571358 MP |
790 | } |
791 | ||
792 | ||
880da007 MP |
793 | /** |
794 | * Write len bytes to the file descriptor @p fd. | |
795 | * | |
796 | * This function underlies the multiplexing system. The body of the | |
797 | * application never calls this function directly. | |
798 | **/ | |
9dd891bb | 799 | static void writefd_unbuffered(int fd,char *buf,size_t len) |
720b47f2 | 800 | { |
06ce139f | 801 | size_t total = 0; |
8d9dc9f9 | 802 | fd_set w_fds, r_fds; |
4c36ddbe | 803 | int fd_count, count; |
8d9dc9f9 | 804 | struct timeval tv; |
720b47f2 | 805 | |
4033b80b WD |
806 | if (fd == msg_fd_out) { |
807 | rprintf(FERROR, "Internal error: wrong write used in receiver.\n"); | |
808 | exit_cleanup(RERR_PROTOCOL); | |
809 | } | |
90ba34e2 | 810 | |
e44f9a12 AT |
811 | no_flush++; |
812 | ||
4c36ddbe | 813 | while (total < len) { |
8d9dc9f9 | 814 | FD_ZERO(&w_fds); |
8d9dc9f9 | 815 | FD_SET(fd,&w_fds); |
554e0a8d | 816 | fd_count = fd; |
4c36ddbe | 817 | |
d17e1dd2 | 818 | if (msg_fd_in >= 0) { |
56014c8c | 819 | FD_ZERO(&r_fds); |
d17e1dd2 | 820 | FD_SET(msg_fd_in,&r_fds); |
d62bcc17 | 821 | if (msg_fd_in > fd_count) |
d17e1dd2 | 822 | fd_count = msg_fd_in; |
8d9dc9f9 AT |
823 | } |
824 | ||
e626b29e | 825 | tv.tv_sec = select_timeout; |
8d9dc9f9 | 826 | tv.tv_usec = 0; |
4c36ddbe | 827 | |
554e0a8d | 828 | errno = 0; |
d17e1dd2 WD |
829 | count = select(fd_count+1, msg_fd_in >= 0 ? &r_fds : NULL, |
830 | &w_fds, NULL, &tv); | |
4c36ddbe AT |
831 | |
832 | if (count <= 0) { | |
4033b80b | 833 | check_timeout(); |
f89b9368 | 834 | if (errno == EBADF) |
554e0a8d | 835 | exit_cleanup(RERR_SOCKETIO); |
8d9dc9f9 AT |
836 | continue; |
837 | } | |
4c36ddbe | 838 | |
d17e1dd2 WD |
839 | if (msg_fd_in >= 0 && FD_ISSET(msg_fd_in, &r_fds)) |
840 | read_msg_fd(); | |
554e0a8d | 841 | |
8d9dc9f9 | 842 | if (FD_ISSET(fd, &w_fds)) { |
06ce139f MP |
843 | int ret; |
844 | size_t n = len-total; | |
71e58630 WD |
845 | if (bwlimit && n > bwlimit_writemax) |
846 | n = bwlimit_writemax; | |
f0359dd0 | 847 | ret = write(fd,buf+total,n); |
4c36ddbe | 848 | |
3309507d WD |
849 | if (ret < 0) { |
850 | if (errno == EINTR) | |
851 | continue; | |
852 | if (errno == EWOULDBLOCK || errno == EAGAIN) { | |
853 | msleep(1); | |
854 | continue; | |
855 | } | |
f0359dd0 AT |
856 | } |
857 | ||
4c36ddbe | 858 | if (ret <= 0) { |
befbfe61 MP |
859 | /* Don't try to write errors back |
860 | * across the stream */ | |
861 | io_multiplexing_close(); | |
d62bcc17 WD |
862 | rsyserr(FERROR, errno, |
863 | "writefd_unbuffered failed to write %ld bytes: phase \"%s\"", | |
f89b9368 | 864 | (long)len, io_write_phase); |
65417579 | 865 | exit_cleanup(RERR_STREAMIO); |
4c36ddbe AT |
866 | } |
867 | ||
08571358 | 868 | sleep_for_bwlimit(ret); |
d62bcc17 | 869 | |
4c36ddbe | 870 | total += ret; |
a800434a | 871 | |
4c36ddbe AT |
872 | if (io_timeout) |
873 | last_io = time(NULL); | |
8d9dc9f9 | 874 | } |
4c36ddbe | 875 | } |
e44f9a12 AT |
876 | |
877 | no_flush--; | |
720b47f2 AT |
878 | } |
879 | ||
8d9dc9f9 | 880 | |
d6dead6b AT |
881 | static char *io_buffer; |
882 | static int io_buffer_count; | |
883 | ||
76c21947 | 884 | void io_start_buffering_out(int fd) |
d6dead6b | 885 | { |
f89b9368 WD |
886 | if (io_buffer) |
887 | return; | |
679e7657 | 888 | multiplex_out_fd = fd; |
58cadc86 | 889 | io_buffer = new_array(char, IO_BUFFER_SIZE); |
f89b9368 WD |
890 | if (!io_buffer) |
891 | out_of_memory("writefd"); | |
d6dead6b | 892 | io_buffer_count = 0; |
ff41a59f AT |
893 | } |
894 | ||
76c21947 WD |
895 | void io_start_buffering_in(int fd) |
896 | { | |
897 | multiplex_in_fd = fd; | |
898 | } | |
899 | ||
880da007 MP |
900 | /** |
901 | * Write an message to a multiplexed stream. If this fails then rsync | |
902 | * exits. | |
903 | **/ | |
d17e1dd2 | 904 | static void mplex_write(int fd, enum msgcode code, char *buf, size_t len) |
ff41a59f AT |
905 | { |
906 | char buffer[4096]; | |
06ce139f | 907 | size_t n = len; |
8d9dc9f9 | 908 | |
ff41a59f AT |
909 | SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
910 | ||
3151cbae WD |
911 | if (n > (sizeof buffer - 4)) { |
912 | n = sizeof buffer - 4; | |
ff41a59f AT |
913 | } |
914 | ||
915 | memcpy(&buffer[4], buf, n); | |
916 | writefd_unbuffered(fd, buffer, n+4); | |
917 | ||
918 | len -= n; | |
919 | buf += n; | |
920 | ||
6d7b6081 AT |
921 | if (len) { |
922 | writefd_unbuffered(fd, buf, len); | |
923 | } | |
d6dead6b AT |
924 | } |
925 | ||
ff41a59f | 926 | |
d17e1dd2 | 927 | void io_flush(int flush_it_all) |
d6dead6b | 928 | { |
679e7657 | 929 | int fd = multiplex_out_fd; |
d62bcc17 | 930 | |
d17e1dd2 | 931 | msg_list_push(flush_it_all); |
90ba34e2 | 932 | |
d17e1dd2 WD |
933 | if (!io_buffer_count || no_flush) |
934 | return; | |
8d9dc9f9 | 935 | |
d17e1dd2 WD |
936 | if (io_multiplexing_out) |
937 | mplex_write(fd, MSG_DATA, io_buffer, io_buffer_count); | |
938 | else | |
4c36ddbe | 939 | writefd_unbuffered(fd, io_buffer, io_buffer_count); |
8d9dc9f9 AT |
940 | io_buffer_count = 0; |
941 | } | |
942 | ||
0ba48136 | 943 | |
7b5c3eb0 | 944 | void io_end_buffering(void) |
8d9dc9f9 | 945 | { |
d17e1dd2 | 946 | io_flush(NORMAL_FLUSH); |
8d9dc9f9 | 947 | if (!io_multiplexing_out) { |
ff41a59f | 948 | free(io_buffer); |
8d9dc9f9 AT |
949 | io_buffer = NULL; |
950 | } | |
d6dead6b AT |
951 | } |
952 | ||
9dd891bb | 953 | static void writefd(int fd,char *buf,size_t len) |
d6dead6b | 954 | { |
1b7c47cb AT |
955 | stats.total_written += len; |
956 | ||
4033b80b WD |
957 | if (fd == msg_fd_out) { |
958 | rprintf(FERROR, "Internal error: wrong write used in receiver.\n"); | |
959 | exit_cleanup(RERR_PROTOCOL); | |
960 | } | |
90ba34e2 | 961 | |
554e0a8d | 962 | if (!io_buffer || fd != multiplex_out_fd) { |
4c36ddbe AT |
963 | writefd_unbuffered(fd, buf, len); |
964 | return; | |
965 | } | |
d6dead6b AT |
966 | |
967 | while (len) { | |
f89b9368 | 968 | int n = MIN((int)len, IO_BUFFER_SIZE-io_buffer_count); |
d6dead6b AT |
969 | if (n > 0) { |
970 | memcpy(io_buffer+io_buffer_count, buf, n); | |
971 | buf += n; | |
972 | len -= n; | |
973 | io_buffer_count += n; | |
974 | } | |
3151cbae | 975 | |
d17e1dd2 WD |
976 | if (io_buffer_count == IO_BUFFER_SIZE) |
977 | io_flush(NORMAL_FLUSH); | |
d6dead6b | 978 | } |
d6dead6b | 979 | } |
720b47f2 AT |
980 | |
981 | ||
b7922338 | 982 | void write_int(int f,int32 x) |
720b47f2 | 983 | { |
8d9dc9f9 AT |
984 | char b[4]; |
985 | SIVAL(b,0,x); | |
4c36ddbe | 986 | writefd(f,b,4); |
720b47f2 AT |
987 | } |
988 | ||
7a24c346 | 989 | |
805edf9d MP |
990 | void write_int_named(int f, int32 x, const char *phase) |
991 | { | |
992 | io_write_phase = phase; | |
993 | write_int(f, x); | |
994 | io_write_phase = phase_unknown; | |
995 | } | |
996 | ||
997 | ||
7a24c346 MP |
998 | /* |
999 | * Note: int64 may actually be a 32-bit type if ./configure couldn't find any | |
1000 | * 64-bit types on this platform. | |
1001 | */ | |
71c46176 | 1002 | void write_longint(int f, int64 x) |
3a6a366f | 1003 | { |
3a6a366f | 1004 | char b[8]; |
3a6a366f | 1005 | |
91c4da3f | 1006 | if (x <= 0x7FFFFFFF) { |
3a6a366f AT |
1007 | write_int(f, (int)x); |
1008 | return; | |
1009 | } | |
1010 | ||
67863f46 S |
1011 | #ifdef NO_INT64 |
1012 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); | |
1013 | exit_cleanup(RERR_UNSUPPORTED); | |
1014 | #else | |
8de330a3 | 1015 | write_int(f, (int32)0xFFFFFFFF); |
3a6a366f AT |
1016 | SIVAL(b,0,(x&0xFFFFFFFF)); |
1017 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); | |
1018 | ||
4c36ddbe | 1019 | writefd(f,b,8); |
67863f46 | 1020 | #endif |
3a6a366f AT |
1021 | } |
1022 | ||
9dd891bb | 1023 | void write_buf(int f,char *buf,size_t len) |
720b47f2 | 1024 | { |
4c36ddbe | 1025 | writefd(f,buf,len); |
720b47f2 AT |
1026 | } |
1027 | ||
880da007 | 1028 | /** Write a string to the connection */ |
6e4fb64e | 1029 | static void write_sbuf(int f,char *buf) |
f0fca04e AT |
1030 | { |
1031 | write_buf(f, buf, strlen(buf)); | |
1032 | } | |
1033 | ||
720b47f2 | 1034 | |
182dca5c AT |
1035 | void write_byte(int f,unsigned char c) |
1036 | { | |
f0fca04e | 1037 | write_buf(f,(char *)&c,1); |
182dca5c AT |
1038 | } |
1039 | ||
7a55d06e MP |
1040 | |
1041 | ||
914cc65c | 1042 | /** |
6ed6d7f5 WD |
1043 | * Read a line of up to @p maxlen characters into @p buf (not counting |
1044 | * the trailing null). Strips the (required) trailing newline and all | |
1045 | * carriage returns. | |
914cc65c | 1046 | * |
6ed6d7f5 | 1047 | * @return 1 for success; 0 for I/O error or truncation. |
914cc65c | 1048 | **/ |
9dd891bb | 1049 | int read_line(int f, char *buf, size_t maxlen) |
f0fca04e AT |
1050 | { |
1051 | while (maxlen) { | |
528bfcd7 | 1052 | buf[0] = 0; |
f0fca04e | 1053 | read_buf(f, buf, 1); |
914cc65c MP |
1054 | if (buf[0] == 0) |
1055 | return 0; | |
6ed6d7f5 | 1056 | if (buf[0] == '\n') |
f0fca04e | 1057 | break; |
f0fca04e AT |
1058 | if (buf[0] != '\r') { |
1059 | buf++; | |
1060 | maxlen--; | |
1061 | } | |
1062 | } | |
6ed6d7f5 WD |
1063 | *buf = '\0'; |
1064 | return maxlen > 0; | |
f0fca04e AT |
1065 | } |
1066 | ||
1067 | ||
1068 | void io_printf(int fd, const char *format, ...) | |
1069 | { | |
d62bcc17 | 1070 | va_list ap; |
f0fca04e AT |
1071 | char buf[1024]; |
1072 | int len; | |
3151cbae | 1073 | |
f0fca04e | 1074 | va_start(ap, format); |
3151cbae | 1075 | len = vsnprintf(buf, sizeof buf, format, ap); |
f0fca04e AT |
1076 | va_end(ap); |
1077 | ||
f89b9368 WD |
1078 | if (len < 0) |
1079 | exit_cleanup(RERR_STREAMIO); | |
f0fca04e AT |
1080 | |
1081 | write_sbuf(fd, buf); | |
1082 | } | |
8d9dc9f9 AT |
1083 | |
1084 | ||
d17e1dd2 | 1085 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
8d9dc9f9 AT |
1086 | void io_start_multiplex_out(int fd) |
1087 | { | |
679e7657 | 1088 | multiplex_out_fd = fd; |
d17e1dd2 | 1089 | io_flush(NORMAL_FLUSH); |
76c21947 | 1090 | io_start_buffering_out(fd); |
8d9dc9f9 AT |
1091 | io_multiplexing_out = 1; |
1092 | } | |
1093 | ||
d17e1dd2 | 1094 | /** Setup for multiplexing a MSG_* stream with the data stream. */ |
8d9dc9f9 AT |
1095 | void io_start_multiplex_in(int fd) |
1096 | { | |
679e7657 | 1097 | multiplex_in_fd = fd; |
d17e1dd2 | 1098 | io_flush(NORMAL_FLUSH); |
8d9dc9f9 AT |
1099 | io_multiplexing_in = 1; |
1100 | } | |
1101 | ||
d17e1dd2 WD |
1102 | /** Write an message to the multiplexed data stream. */ |
1103 | int io_multiplex_write(enum msgcode code, char *buf, size_t len) | |
8d9dc9f9 | 1104 | { |
f89b9368 WD |
1105 | if (!io_multiplexing_out) |
1106 | return 0; | |
8d9dc9f9 | 1107 | |
d17e1dd2 | 1108 | io_flush(NORMAL_FLUSH); |
1b7c47cb | 1109 | stats.total_written += (len+4); |
ff41a59f | 1110 | mplex_write(multiplex_out_fd, code, buf, len); |
8d9dc9f9 AT |
1111 | return 1; |
1112 | } | |
1113 | ||
d17e1dd2 | 1114 | /** Stop output multiplexing. */ |
554e0a8d AT |
1115 | void io_multiplexing_close(void) |
1116 | { | |
1117 | io_multiplexing_out = 0; | |
1118 | } | |
1119 |