| 1 | This patch changes the receiving side to have the receiving code use a thread |
| 2 | instead of a forked process. This extra thread does read from the socket, but |
| 3 | it sends any stdout/stderr messages to the generator (main thread) to output. |
| 4 | |
| 5 | ** This is very new code. ** Yes, it passes the "make test" testsuite, but |
| 6 | there may still be some problems, especially in some of the untested features. |
| 7 | (For one thing, I haven't yet added code to properly handle any keep-alive |
| 8 | messages that arrive on the receiving side during the --delete-after phase!) |
| 9 | |
| 10 | This code just uses pthread.h directly, so configure changes will probably be |
| 11 | needed to make this compatible with more systems. I have also tested that |
| 12 | this code works fine using the GNU pth library without any code changes if |
| 13 | you configured it with --enable-syscall-soft --enable-pthread (you may need |
| 14 | to twiddle the Makefile options if you didn't install the library, though). |
| 15 | |
| 16 | NOTE: we still need to duplicate the partial_fname static in util.c! |
| 17 | |
| 18 | If you try this out, please send some email to wayned@samba.org or the rsync |
| 19 | mailing list with your results, build changes, bug reports, etc. Thanks! |
| 20 | |
| 21 | After applying this patch, run these commands for a successful build: |
| 22 | |
| 23 | ./prepare-source |
| 24 | ./configure (optional if already run) |
| 25 | make |
| 26 | |
| 27 | --- old/Makefile.in |
| 28 | +++ new/Makefile.in |
| 29 | @@ -6,7 +6,7 @@ exec_prefix=@exec_prefix@ |
| 30 | bindir=@bindir@ |
| 31 | mandir=@mandir@ |
| 32 | |
| 33 | -LIBS=@LIBS@ |
| 34 | +LIBS=@LIBS@ -lpthread |
| 35 | CC=@CC@ |
| 36 | CFLAGS=@CFLAGS@ |
| 37 | CPPFLAGS=@CPPFLAGS@ |
| 38 | --- old/cleanup.c |
| 39 | +++ new/cleanup.c |
| 40 | @@ -31,10 +31,6 @@ extern int log_got_error; |
| 41 | extern char *partial_dir; |
| 42 | extern char *logfile_name; |
| 43 | |
| 44 | -#ifdef HAVE_SIGACTION |
| 45 | -static struct sigaction sigact; |
| 46 | -#endif |
| 47 | - |
| 48 | /** |
| 49 | * Close all open sockets and files, allowing a (somewhat) graceful |
| 50 | * shutdown() of socket connections. This eliminates the abortive |
| 51 | @@ -103,9 +99,6 @@ void _exit_cleanup(int code, const char |
| 52 | } |
| 53 | inside_cleanup++; |
| 54 | |
| 55 | - SIGACTION(SIGUSR1, SIG_IGN); |
| 56 | - SIGACTION(SIGUSR2, SIG_IGN); |
| 57 | - |
| 58 | if (verbose > 3) { |
| 59 | rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n", |
| 60 | code, file, line); |
| 61 | @@ -137,8 +130,6 @@ void _exit_cleanup(int code, const char |
| 62 | io_flush(FULL_FLUSH); |
| 63 | if (cleanup_fname) |
| 64 | do_unlink(cleanup_fname); |
| 65 | - if (code) |
| 66 | - kill_all(SIGUSR1); |
| 67 | if (cleanup_pid && cleanup_pid == getpid()) { |
| 68 | char *pidf = lp_pid_file(); |
| 69 | if (pidf && *pidf) |
| 70 | --- old/errcode.h |
| 71 | +++ new/errcode.h |
| 72 | @@ -37,7 +37,6 @@ |
| 73 | #define RERR_CRASHED 15 /* sibling crashed */ |
| 74 | #define RERR_TERMINATED 16 /* sibling terminated abnormally */ |
| 75 | |
| 76 | -#define RERR_SIGNAL1 19 /* status returned when sent SIGUSR1 */ |
| 77 | #define RERR_SIGNAL 20 /* status returned when sent SIGINT, SIGTERM, SIGHUP */ |
| 78 | #define RERR_WAITCHILD 21 /* some error returned by waitpid() */ |
| 79 | #define RERR_MALLOC 22 /* error allocating core memory buffers */ |
| 80 | --- old/generator.c |
| 81 | +++ new/generator.c |
| 82 | @@ -67,7 +67,6 @@ extern OFF_T min_size; |
| 83 | extern int io_error; |
| 84 | extern int allowed_lull; |
| 85 | extern int sock_f_out; |
| 86 | -extern int ignore_timeout; |
| 87 | extern int protocol_version; |
| 88 | extern int fuzzy_basis; |
| 89 | extern int always_checksum; |
| 90 | @@ -95,6 +94,11 @@ extern struct filter_list_struct server_ |
| 91 | |
| 92 | static int deletion_count = 0; /* used to implement --max-delete */ |
| 93 | |
| 94 | +/* These vars are local copies so that the receiver can use the originals. */ |
| 95 | +static int GEN_append_mode; |
| 96 | +static int GEN_make_backups; |
| 97 | +static int GEN_csum_length; |
| 98 | + |
| 99 | /* For calling delete_file() */ |
| 100 | #define DEL_FORCE_RECURSE (1<<1) /* recurse even w/o --force */ |
| 101 | #define DEL_TERSE (1<<3) |
| 102 | @@ -443,8 +447,8 @@ static void sum_sizes_sqroot(struct sum_ |
| 103 | } |
| 104 | |
| 105 | if (protocol_version < 27) { |
| 106 | - s2length = csum_length; |
| 107 | - } else if (csum_length == SUM_LENGTH) { |
| 108 | + s2length = GEN_csum_length; |
| 109 | + } else if (GEN_csum_length == SUM_LENGTH) { |
| 110 | s2length = SUM_LENGTH; |
| 111 | } else { |
| 112 | int32 c; |
| 113 | @@ -454,7 +458,7 @@ static void sum_sizes_sqroot(struct sum_ |
| 114 | for (c = blength; c >>= 1 && b; b--) {} |
| 115 | /* add a bit, subtract rollsum, round up. */ |
| 116 | s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */ |
| 117 | - s2length = MAX(s2length, csum_length); |
| 118 | + s2length = MAX(s2length, GEN_csum_length); |
| 119 | s2length = MIN(s2length, SUM_LENGTH); |
| 120 | } |
| 121 | |
| 122 | @@ -488,7 +492,7 @@ static void generate_and_send_sums(int f |
| 123 | sum_sizes_sqroot(&sum, len); |
| 124 | write_sum_head(f_out, &sum); |
| 125 | |
| 126 | - if (append_mode > 0 && f_copy < 0) |
| 127 | + if (GEN_append_mode > 0 && f_copy < 0) |
| 128 | return; |
| 129 | |
| 130 | if (len > 0) |
| 131 | @@ -507,7 +511,7 @@ static void generate_and_send_sums(int f |
| 132 | |
| 133 | if (f_copy >= 0) { |
| 134 | full_write(f_copy, map, n1); |
| 135 | - if (append_mode > 0) |
| 136 | + if (GEN_append_mode > 0) |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | @@ -1182,7 +1186,7 @@ static void recv_generator(char *fname, |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | - if (append_mode && st.st_size > file->length) |
| 145 | + if (GEN_append_mode && st.st_size > file->length) |
| 146 | return; |
| 147 | |
| 148 | if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH) |
| 149 | @@ -1237,7 +1241,7 @@ static void recv_generator(char *fname, |
| 150 | goto notify_others; |
| 151 | } |
| 152 | |
| 153 | - if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) { |
| 154 | + if (inplace && GEN_make_backups && fnamecmp_type == FNAMECMP_FNAME) { |
| 155 | if (!(backupptr = get_backup_name(fname))) { |
| 156 | close(fd); |
| 157 | return; |
| 158 | @@ -1328,9 +1332,12 @@ void generate_files(int f_out, struct fi |
| 159 | int save_ignore_existing = ignore_existing; |
| 160 | int save_ignore_non_existing = ignore_non_existing; |
| 161 | int save_do_progress = do_progress; |
| 162 | - int save_make_backups = make_backups; |
| 163 | + int save_make_backups = GEN_make_backups = make_backups; |
| 164 | int dir_tweaking = !(list_only || local_name || dry_run); |
| 165 | |
| 166 | + GEN_append_mode = append_mode; |
| 167 | + GEN_csum_length = csum_length; |
| 168 | + |
| 169 | if (protocol_version >= 29) { |
| 170 | itemizing = 1; |
| 171 | maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT; |
| 172 | @@ -1358,7 +1365,7 @@ void generate_files(int f_out, struct fi |
| 173 | do_delete_pass(flist); |
| 174 | do_progress = 0; |
| 175 | |
| 176 | - if (append_mode || whole_file < 0) |
| 177 | + if (GEN_append_mode || whole_file < 0) |
| 178 | whole_file = 0; |
| 179 | if (verbose >= 2) { |
| 180 | rprintf(FINFO, "delta-transmission %s\n", |
| 181 | @@ -1367,12 +1374,6 @@ void generate_files(int f_out, struct fi |
| 182 | : "enabled"); |
| 183 | } |
| 184 | |
| 185 | - /* Since we often fill up the outgoing socket and then just sit around |
| 186 | - * waiting for the other 2 processes to do their thing, we don't want |
| 187 | - * to exit on a timeout. If the data stops flowing, the receiver will |
| 188 | - * notice that and let us know via the redo pipe (or its closing). */ |
| 189 | - ignore_timeout = 1; |
| 190 | - |
| 191 | for (i = 0; i < flist->count; i++) { |
| 192 | struct file_struct *file = flist->files[i]; |
| 193 | |
| 194 | @@ -1416,23 +1417,34 @@ void generate_files(int f_out, struct fi |
| 195 | delete_in_dir(NULL, NULL, NULL, NULL); |
| 196 | |
| 197 | phase++; |
| 198 | - csum_length = SUM_LENGTH; |
| 199 | + GEN_csum_length = SUM_LENGTH; /* csum_length is set by the receiver */ |
| 200 | max_size = min_size = ignore_existing = ignore_non_existing = 0; |
| 201 | update_only = always_checksum = size_only = 0; |
| 202 | ignore_times = 1; |
| 203 | - if (append_mode) /* resend w/o append mode */ |
| 204 | - append_mode = -1; /* ... but only longer files */ |
| 205 | - make_backups = 0; /* avoid a duplicate backup for inplace processing */ |
| 206 | + if (GEN_append_mode) /* resend w/o append mode */ |
| 207 | + GEN_append_mode = -1; /* ... but only longer files */ |
| 208 | + GEN_make_backups = 0; /* avoid a duplicate backup for inplace processing */ |
| 209 | |
| 210 | if (verbose > 2) |
| 211 | rprintf(FINFO,"generate_files phase=%d\n",phase); |
| 212 | |
| 213 | write_int(f_out, -1); |
| 214 | + io_flush(NORMAL_FLUSH); |
| 215 | |
| 216 | /* files can cycle through the system more than once |
| 217 | * to catch initial checksum errors */ |
| 218 | - while ((i = get_redo_num(itemizing, code)) != -1) { |
| 219 | - struct file_struct *file = flist->files[i]; |
| 220 | + while (1) { |
| 221 | + struct file_struct *file; |
| 222 | + if (preserve_hard_links) |
| 223 | + check_for_finished_hlinks(itemizing, code); |
| 224 | + if ((i = get_redo_num()) < 0) { |
| 225 | + if (i == -2) |
| 226 | + break; |
| 227 | + io_flush(NORMAL_FLUSH); |
| 228 | + msleep(20); |
| 229 | + continue; |
| 230 | + } |
| 231 | + file = flist->files[i]; |
| 232 | if (local_name) |
| 233 | strlcpy(fbuf, local_name, sizeof fbuf); |
| 234 | else |
| 235 | @@ -1444,27 +1456,43 @@ void generate_files(int f_out, struct fi |
| 236 | phase++; |
| 237 | ignore_non_existing = save_ignore_non_existing; |
| 238 | ignore_existing = save_ignore_existing; |
| 239 | - make_backups = save_make_backups; |
| 240 | + GEN_make_backups = save_make_backups; |
| 241 | |
| 242 | if (verbose > 2) |
| 243 | rprintf(FINFO,"generate_files phase=%d\n",phase); |
| 244 | |
| 245 | write_int(f_out, -1); |
| 246 | + io_flush(NORMAL_FLUSH); |
| 247 | + |
| 248 | /* Reduce round-trip lag-time for a useless delay-updates phase. */ |
| 249 | - if (protocol_version >= 29 && !delay_updates) |
| 250 | + if (protocol_version >= 29 && !delay_updates) { |
| 251 | write_int(f_out, -1); |
| 252 | + io_flush(NORMAL_FLUSH); |
| 253 | + } |
| 254 | |
| 255 | - /* Read MSG_DONE for the redo phase (and any prior messages). */ |
| 256 | - get_redo_num(itemizing, code); |
| 257 | + /* Read end marker for the redo phase (and any prior messages). */ |
| 258 | + while (1) { |
| 259 | + if (preserve_hard_links) |
| 260 | + check_for_finished_hlinks(itemizing, code); |
| 261 | + if (get_redo_num() == -2) |
| 262 | + break; |
| 263 | + io_flush(NORMAL_FLUSH); |
| 264 | + msleep(20); |
| 265 | + } |
| 266 | |
| 267 | if (protocol_version >= 29) { |
| 268 | phase++; |
| 269 | if (verbose > 2) |
| 270 | rprintf(FINFO, "generate_files phase=%d\n", phase); |
| 271 | - if (delay_updates) |
| 272 | + if (delay_updates) { |
| 273 | write_int(f_out, -1); |
| 274 | - /* Read MSG_DONE for delay-updates phase & prior messages. */ |
| 275 | - get_redo_num(itemizing, code); |
| 276 | + io_flush(NORMAL_FLUSH); |
| 277 | + } |
| 278 | + /* Read end marker for delay-updates phase & prior messages. */ |
| 279 | + while (get_redo_num() != -2) { |
| 280 | + io_flush(NORMAL_FLUSH); |
| 281 | + msleep(20); |
| 282 | + } |
| 283 | } |
| 284 | |
| 285 | do_progress = save_do_progress; |
| 286 | --- old/io.c |
| 287 | +++ new/io.c |
| 288 | @@ -40,20 +40,17 @@ extern int allowed_lull; |
| 289 | extern int am_server; |
| 290 | extern int am_daemon; |
| 291 | extern int am_sender; |
| 292 | -extern int am_generator; |
| 293 | extern int eol_nulls; |
| 294 | extern int read_batch; |
| 295 | extern int csum_length; |
| 296 | extern int checksum_seed; |
| 297 | extern int protocol_version; |
| 298 | -extern int remove_sent_files; |
| 299 | extern int preserve_hard_links; |
| 300 | extern char *filesfrom_host; |
| 301 | extern struct stats stats; |
| 302 | extern struct file_list *the_file_list; |
| 303 | |
| 304 | const char phase_unknown[] = "unknown"; |
| 305 | -int ignore_timeout = 0; |
| 306 | int batch_fd = -1; |
| 307 | int batch_gen_fd = -1; |
| 308 | |
| 309 | @@ -61,7 +58,6 @@ int batch_gen_fd = -1; |
| 310 | int kluge_around_eof = 0; |
| 311 | |
| 312 | int msg_fd_in = -1; |
| 313 | -int msg_fd_out = -1; |
| 314 | int sock_f_in = -1; |
| 315 | int sock_f_out = -1; |
| 316 | |
| 317 | @@ -88,27 +84,31 @@ static OFF_T active_bytecnt = 0; |
| 318 | static void read_loop(int fd, char *buf, size_t len); |
| 319 | |
| 320 | struct flist_ndx_item { |
| 321 | - struct flist_ndx_item *next; |
| 322 | + volatile struct flist_ndx_item *next; |
| 323 | int ndx; |
| 324 | }; |
| 325 | |
| 326 | struct flist_ndx_list { |
| 327 | - struct flist_ndx_item *head, *tail; |
| 328 | + volatile struct flist_ndx_item *head, *tail; |
| 329 | + pthread_mutex_t mutex; |
| 330 | }; |
| 331 | |
| 332 | -static struct flist_ndx_list redo_list, hlink_list; |
| 333 | +static struct flist_ndx_list redo_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER }; |
| 334 | +static struct flist_ndx_list hlink_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER }; |
| 335 | |
| 336 | struct msg_list_item { |
| 337 | - struct msg_list_item *next; |
| 338 | + volatile struct msg_list_item *next; |
| 339 | int len; |
| 340 | + enum msgcode code; |
| 341 | char buf[1]; |
| 342 | }; |
| 343 | |
| 344 | struct msg_list { |
| 345 | - struct msg_list_item *head, *tail; |
| 346 | + volatile struct msg_list_item *head, *tail; |
| 347 | + pthread_mutex_t mutex; |
| 348 | }; |
| 349 | |
| 350 | -static struct msg_list msg2genr, msg2sndr; |
| 351 | +static struct msg_list msg_list = { NULL, NULL, PTHREAD_MUTEX_INITIALIZER }; |
| 352 | |
| 353 | static void flist_ndx_push(struct flist_ndx_list *lp, int ndx) |
| 354 | { |
| 355 | @@ -118,27 +118,31 @@ static void flist_ndx_push(struct flist_ |
| 356 | out_of_memory("flist_ndx_push"); |
| 357 | item->next = NULL; |
| 358 | item->ndx = ndx; |
| 359 | + pthread_mutex_lock(&redo_list.mutex); |
| 360 | if (lp->tail) |
| 361 | lp->tail->next = item; |
| 362 | else |
| 363 | lp->head = item; |
| 364 | lp->tail = item; |
| 365 | + pthread_mutex_unlock(&redo_list.mutex); |
| 366 | } |
| 367 | |
| 368 | static int flist_ndx_pop(struct flist_ndx_list *lp) |
| 369 | { |
| 370 | - struct flist_ndx_item *next; |
| 371 | + struct flist_ndx_item *head, *next; |
| 372 | int ndx; |
| 373 | |
| 374 | if (!lp->head) |
| 375 | return -1; |
| 376 | |
| 377 | - ndx = lp->head->ndx; |
| 378 | - next = lp->head->next; |
| 379 | - free(lp->head); |
| 380 | - lp->head = next; |
| 381 | - if (!next) |
| 382 | + pthread_mutex_lock(&hlink_list.mutex); |
| 383 | + head = (struct flist_ndx_item *)lp->head; |
| 384 | + next = (struct flist_ndx_item *)head->next; |
| 385 | + ndx = head->ndx; |
| 386 | + if (!(lp->head = next)) |
| 387 | lp->tail = NULL; |
| 388 | + pthread_mutex_unlock(&hlink_list.mutex); |
| 389 | + free(head); |
| 390 | |
| 391 | return ndx; |
| 392 | } |
| 393 | @@ -147,7 +151,7 @@ static void check_timeout(void) |
| 394 | { |
| 395 | time_t t; |
| 396 | |
| 397 | - if (!io_timeout || ignore_timeout) |
| 398 | + if (!io_timeout) |
| 399 | return; |
| 400 | |
| 401 | if (!last_io_in) { |
| 402 | @@ -188,44 +192,38 @@ void set_io_timeout(int secs) |
| 403 | |
| 404 | /* Setup the fd used to receive MSG_* messages. Only needed during the |
| 405 | * early stages of being a local sender (up through the sending of the |
| 406 | - * file list) or when we're the generator (to fetch the messages from |
| 407 | - * the receiver). */ |
| 408 | + * file list). */ |
| 409 | void set_msg_fd_in(int fd) |
| 410 | { |
| 411 | msg_fd_in = fd; |
| 412 | } |
| 413 | |
| 414 | -/* Setup the fd used to send our MSG_* messages. Only needed when |
| 415 | - * we're the receiver (to send our messages to the generator). */ |
| 416 | -void set_msg_fd_out(int fd) |
| 417 | -{ |
| 418 | - msg_fd_out = fd; |
| 419 | - set_nonblocking(msg_fd_out); |
| 420 | -} |
| 421 | - |
| 422 | /* Add a message to the pending MSG_* list. */ |
| 423 | -static void msg_list_add(struct msg_list *lst, int code, char *buf, int len) |
| 424 | +static void msg_list_add(int code, char *buf, int len) |
| 425 | { |
| 426 | struct msg_list_item *m; |
| 427 | - int sz = len + 4 + sizeof m[0] - 1; |
| 428 | + int sz = len + sizeof m[0] - 1; |
| 429 | |
| 430 | + assert(am_receiver()); |
| 431 | if (!(m = (struct msg_list_item *)new_array(char, sz))) |
| 432 | out_of_memory("msg_list_add"); |
| 433 | m->next = NULL; |
| 434 | - m->len = len + 4; |
| 435 | - SIVAL(m->buf, 0, ((code+MPLEX_BASE)<<24) | len); |
| 436 | - memcpy(m->buf + 4, buf, len); |
| 437 | - if (lst->tail) |
| 438 | - lst->tail->next = m; |
| 439 | + m->len = len; |
| 440 | + m->code = code; |
| 441 | + memcpy(m->buf, buf, len); |
| 442 | + |
| 443 | + pthread_mutex_lock(&msg_list.mutex); |
| 444 | + if (msg_list.tail) |
| 445 | + msg_list.tail->next = m; |
| 446 | else |
| 447 | - lst->head = m; |
| 448 | - lst->tail = m; |
| 449 | + msg_list.head = m; |
| 450 | + msg_list.tail = m; |
| 451 | + pthread_mutex_unlock(&msg_list.mutex); |
| 452 | } |
| 453 | |
| 454 | -/* Read a message from the MSG_* fd and handle it. This is called either |
| 455 | +/* Read a message from the MSG_* fd and handle it. This is only called |
| 456 | * during the early stages of being a local sender (up through the sending |
| 457 | - * of the file list) or when we're the generator (to fetch the messages |
| 458 | - * from the receiver). */ |
| 459 | + * of the file list). */ |
| 460 | static void read_msg_fd(void) |
| 461 | { |
| 462 | char buf[2048]; |
| 463 | @@ -244,51 +242,6 @@ static void read_msg_fd(void) |
| 464 | tag = (tag >> 24) - MPLEX_BASE; |
| 465 | |
| 466 | switch (tag) { |
| 467 | - case MSG_DONE: |
| 468 | - if (len != 0 || !am_generator) { |
| 469 | - rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
| 470 | - exit_cleanup(RERR_STREAMIO); |
| 471 | - } |
| 472 | - flist_ndx_push(&redo_list, -1); |
| 473 | - break; |
| 474 | - case MSG_REDO: |
| 475 | - if (len != 4 || !am_generator) { |
| 476 | - rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
| 477 | - exit_cleanup(RERR_STREAMIO); |
| 478 | - } |
| 479 | - read_loop(fd, buf, 4); |
| 480 | - if (remove_sent_files) |
| 481 | - decrement_active_files(IVAL(buf,0)); |
| 482 | - flist_ndx_push(&redo_list, IVAL(buf,0)); |
| 483 | - break; |
| 484 | - case MSG_DELETED: |
| 485 | - if (len >= (int)sizeof buf || !am_generator) { |
| 486 | - rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
| 487 | - exit_cleanup(RERR_STREAMIO); |
| 488 | - } |
| 489 | - read_loop(fd, buf, len); |
| 490 | - send_msg(MSG_DELETED, buf, len); |
| 491 | - break; |
| 492 | - case MSG_SUCCESS: |
| 493 | - if (len != 4 || !am_generator) { |
| 494 | - rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
| 495 | - exit_cleanup(RERR_STREAMIO); |
| 496 | - } |
| 497 | - read_loop(fd, buf, len); |
| 498 | - if (remove_sent_files) { |
| 499 | - decrement_active_files(IVAL(buf,0)); |
| 500 | - send_msg(MSG_SUCCESS, buf, len); |
| 501 | - } |
| 502 | - if (preserve_hard_links) |
| 503 | - flist_ndx_push(&hlink_list, IVAL(buf,0)); |
| 504 | - break; |
| 505 | - case MSG_SOCKERR: |
| 506 | - if (!am_generator) { |
| 507 | - rprintf(FERROR, "invalid message %d:%d\n", tag, len); |
| 508 | - exit_cleanup(RERR_STREAMIO); |
| 509 | - } |
| 510 | - close_multiplexing_out(); |
| 511 | - /* FALL THROUGH */ |
| 512 | case MSG_INFO: |
| 513 | case MSG_ERROR: |
| 514 | case MSG_LOG: |
| 515 | @@ -332,75 +285,80 @@ void decrement_active_files(int ndx) |
| 516 | active_bytecnt -= the_file_list->files[ndx]->length; |
| 517 | } |
| 518 | |
| 519 | -/* Try to push messages off the list onto the wire. If we leave with more |
| 520 | +/* Try to pop messages off the list onto the wire. If we leave with more |
| 521 | * to do, return 0. On error, return -1. If everything flushed, return 1. |
| 522 | - * This is only active in the receiver. */ |
| 523 | -static int msg2genr_flush(int flush_it_all) |
| 524 | + * This is only called by the generator. */ |
| 525 | +static void msg_list_flush(void) |
| 526 | { |
| 527 | - static int written = 0; |
| 528 | - struct timeval tv; |
| 529 | - fd_set fds; |
| 530 | + assert(am_generator()); |
| 531 | |
| 532 | - if (msg_fd_out < 0) |
| 533 | - return -1; |
| 534 | + if (defer_forwarding_messages) |
| 535 | + return; |
| 536 | |
| 537 | - while (msg2genr.head) { |
| 538 | - struct msg_list_item *m = msg2genr.head; |
| 539 | - int n = write(msg_fd_out, m->buf + written, m->len - written); |
| 540 | - if (n < 0) { |
| 541 | - if (errno == EINTR) |
| 542 | - continue; |
| 543 | - if (errno != EWOULDBLOCK && errno != EAGAIN) |
| 544 | - return -1; |
| 545 | - if (!flush_it_all) |
| 546 | - return 0; |
| 547 | - FD_ZERO(&fds); |
| 548 | - FD_SET(msg_fd_out, &fds); |
| 549 | - tv.tv_sec = select_timeout; |
| 550 | - tv.tv_usec = 0; |
| 551 | - if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv)) |
| 552 | - check_timeout(); |
| 553 | - } else if ((written += n) == m->len) { |
| 554 | - msg2genr.head = m->next; |
| 555 | - if (!msg2genr.head) |
| 556 | - msg2genr.tail = NULL; |
| 557 | - free(m); |
| 558 | - written = 0; |
| 559 | + no_flush++; |
| 560 | + defer_forwarding_messages = 1; |
| 561 | + while (msg_list.head) { |
| 562 | + struct msg_list_item *m = (struct msg_list_item *)msg_list.head; |
| 563 | + pthread_mutex_lock(&msg_list.mutex); |
| 564 | + if (!(msg_list.head = m->next)) |
| 565 | + msg_list.tail = NULL; |
| 566 | + pthread_mutex_unlock(&msg_list.mutex); |
| 567 | + switch (m->code) { |
| 568 | + case MSG_SOCKERR: |
| 569 | + close_multiplexing_out(); |
| 570 | + /* FALL THROUGH */ |
| 571 | + case MSG_INFO: |
| 572 | + case MSG_ERROR: |
| 573 | + case MSG_LOG: |
| 574 | + rwrite(m->code, m->buf, m->len); |
| 575 | + break; |
| 576 | + default: |
| 577 | + io_multiplex_write(m->code, m->buf, m->len); |
| 578 | + break; |
| 579 | } |
| 580 | + free(m); |
| 581 | } |
| 582 | - return 1; |
| 583 | + defer_forwarding_messages = 0; |
| 584 | + no_flush--; |
| 585 | } |
| 586 | |
| 587 | int send_msg(enum msgcode code, char *buf, int len) |
| 588 | { |
| 589 | - if (msg_fd_out < 0) { |
| 590 | + if (!am_receiver()) { |
| 591 | if (!defer_forwarding_messages) |
| 592 | return io_multiplex_write(code, buf, len); |
| 593 | if (!io_multiplexing_out) |
| 594 | return 0; |
| 595 | - msg_list_add(&msg2sndr, code, buf, len); |
| 596 | - return 1; |
| 597 | } |
| 598 | - msg_list_add(&msg2genr, code, buf, len); |
| 599 | - msg2genr_flush(NORMAL_FLUSH); |
| 600 | + msg_list_add(code, buf, len); |
| 601 | return 1; |
| 602 | } |
| 603 | |
| 604 | -int get_redo_num(int itemizing, enum logcode code) |
| 605 | +/* This is only used by the receiver. */ |
| 606 | +void push_redo_num(int ndx) |
| 607 | { |
| 608 | - while (1) { |
| 609 | - if (hlink_list.head) |
| 610 | - check_for_finished_hlinks(itemizing, code); |
| 611 | - if (redo_list.head) |
| 612 | - break; |
| 613 | - read_msg_fd(); |
| 614 | - } |
| 615 | + assert(am_receiver()); |
| 616 | + flist_ndx_push(&redo_list, ndx); |
| 617 | +} |
| 618 | |
| 619 | +/* This is only used by the generator. */ |
| 620 | +int get_redo_num(void) |
| 621 | +{ |
| 622 | + assert(am_generator()); |
| 623 | return flist_ndx_pop(&redo_list); |
| 624 | } |
| 625 | |
| 626 | +/* This is only used by the receiver. */ |
| 627 | +void push_hlink_num(int ndx) |
| 628 | +{ |
| 629 | + assert(am_receiver()); |
| 630 | + flist_ndx_push(&hlink_list, ndx); |
| 631 | +} |
| 632 | + |
| 633 | +/* This is only used by the generator. */ |
| 634 | int get_hlink_num(void) |
| 635 | { |
| 636 | + assert(am_generator()); |
| 637 | return flist_ndx_pop(&hlink_list); |
| 638 | } |
| 639 | |
| 640 | @@ -480,11 +438,6 @@ static int read_timeout(int fd, char *bu |
| 641 | FD_ZERO(&r_fds); |
| 642 | FD_ZERO(&w_fds); |
| 643 | FD_SET(fd, &r_fds); |
| 644 | - if (msg2genr.head) { |
| 645 | - FD_SET(msg_fd_out, &w_fds); |
| 646 | - if (msg_fd_out > maxfd) |
| 647 | - maxfd = msg_fd_out; |
| 648 | - } |
| 649 | if (io_filesfrom_f_out >= 0) { |
| 650 | int new_fd; |
| 651 | if (io_filesfrom_buflen == 0) { |
| 652 | @@ -517,9 +470,6 @@ static int read_timeout(int fd, char *bu |
| 653 | continue; |
| 654 | } |
| 655 | |
| 656 | - if (msg2genr.head && FD_ISSET(msg_fd_out, &w_fds)) |
| 657 | - msg2genr_flush(NORMAL_FLUSH); |
| 658 | - |
| 659 | if (io_filesfrom_f_out >= 0) { |
| 660 | if (io_filesfrom_buflen) { |
| 661 | if (FD_ISSET(io_filesfrom_f_out, &w_fds)) { |
| 662 | @@ -841,6 +791,8 @@ static void readfd(int fd, char *buffer, |
| 663 | } |
| 664 | |
| 665 | if (fd == write_batch_monitor_in) { |
| 666 | + if (am_generator()) |
| 667 | + rprintf(FINFO, "writing %d bytes to batch file from generator\n", total); |
| 668 | if ((size_t)write(batch_fd, buffer, total) != total) |
| 669 | exit_cleanup(RERR_FILEIO); |
| 670 | } |
| 671 | @@ -1102,7 +1054,6 @@ static void writefd_unbuffered(int fd,ch |
| 672 | * to grab any messages they sent before they died. */ |
| 673 | while (fd == sock_f_out && io_multiplexing_in) { |
| 674 | set_io_timeout(30); |
| 675 | - ignore_timeout = 0; |
| 676 | readfd_unbuffered(sock_f_in, io_filesfrom_buf, |
| 677 | sizeof io_filesfrom_buf); |
| 678 | } |
| 679 | @@ -1113,7 +1064,7 @@ static void writefd_unbuffered(int fd,ch |
| 680 | defer_forwarding_messages = 1; |
| 681 | |
| 682 | if (fd == sock_f_out) { |
| 683 | - if (io_timeout || am_generator) |
| 684 | + if (io_timeout || am_generator()) |
| 685 | last_io_out = time(NULL); |
| 686 | sleep_for_bwlimit(cnt); |
| 687 | } |
| 688 | @@ -1123,23 +1074,6 @@ static void writefd_unbuffered(int fd,ch |
| 689 | no_flush--; |
| 690 | } |
| 691 | |
| 692 | -static void msg2sndr_flush(void) |
| 693 | -{ |
| 694 | - if (defer_forwarding_messages) |
| 695 | - return; |
| 696 | - |
| 697 | - while (msg2sndr.head && io_multiplexing_out) { |
| 698 | - struct msg_list_item *m = msg2sndr.head; |
| 699 | - if (!(msg2sndr.head = m->next)) |
| 700 | - msg2sndr.tail = NULL; |
| 701 | - stats.total_written += m->len; |
| 702 | - defer_forwarding_messages = 1; |
| 703 | - writefd_unbuffered(sock_f_out, m->buf, m->len); |
| 704 | - defer_forwarding_messages = 0; |
| 705 | - free(m); |
| 706 | - } |
| 707 | -} |
| 708 | - |
| 709 | /** |
| 710 | * Write an message to a multiplexed stream. If this fails then rsync |
| 711 | * exits. |
| 712 | @@ -1165,14 +1099,15 @@ static void mplex_write(enum msgcode cod |
| 713 | defer_forwarding_messages = 1; |
| 714 | writefd_unbuffered(sock_f_out, buf, len); |
| 715 | defer_forwarding_messages = 0; |
| 716 | - msg2sndr_flush(); |
| 717 | + if (am_generator()) |
| 718 | + msg_list_flush(); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | void io_flush(int flush_it_all) |
| 723 | { |
| 724 | - msg2genr_flush(flush_it_all); |
| 725 | - msg2sndr_flush(); |
| 726 | + if (am_generator()) |
| 727 | + msg_list_flush(); |
| 728 | |
| 729 | if (!iobuf_out_cnt || no_flush) |
| 730 | return; |
| 731 | @@ -1186,11 +1121,6 @@ void io_flush(int flush_it_all) |
| 732 | |
| 733 | static void writefd(int fd,char *buf,size_t len) |
| 734 | { |
| 735 | - if (fd == msg_fd_out) { |
| 736 | - rprintf(FERROR, "Internal error: wrong write used in receiver.\n"); |
| 737 | - exit_cleanup(RERR_PROTOCOL); |
| 738 | - } |
| 739 | - |
| 740 | if (fd == sock_f_out) |
| 741 | stats.total_written += len; |
| 742 | |
| 743 | @@ -1396,9 +1326,3 @@ void start_write_batch(int fd) |
| 744 | else |
| 745 | write_batch_monitor_in = fd; |
| 746 | } |
| 747 | - |
| 748 | -void stop_write_batch(void) |
| 749 | -{ |
| 750 | - write_batch_monitor_out = -1; |
| 751 | - write_batch_monitor_in = -1; |
| 752 | -} |
| 753 | --- old/log.c |
| 754 | +++ new/log.c |
| 755 | @@ -33,7 +33,6 @@ extern int am_sender; |
| 756 | extern int local_server; |
| 757 | extern int quiet; |
| 758 | extern int module_id; |
| 759 | -extern int msg_fd_out; |
| 760 | extern int allow_8bit_chars; |
| 761 | extern int protocol_version; |
| 762 | extern int preserve_times; |
| 763 | @@ -72,7 +71,6 @@ struct { |
| 764 | { RERR_IPC , "error in IPC code" }, |
| 765 | { RERR_CRASHED , "sibling process crashed" }, |
| 766 | { RERR_TERMINATED , "sibling process terminated abnormally" }, |
| 767 | - { RERR_SIGNAL1 , "received SIGUSR1" }, |
| 768 | { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" }, |
| 769 | { RERR_WAITCHILD , "waitpid() failed" }, |
| 770 | { RERR_MALLOC , "error allocating core memory buffers" }, |
| 771 | @@ -224,8 +222,8 @@ void rwrite(enum logcode code, char *buf |
| 772 | if (len < 0) |
| 773 | exit_cleanup(RERR_MESSAGEIO); |
| 774 | |
| 775 | - if (am_server && msg_fd_out >= 0) { |
| 776 | - /* Pass the message to our sibling. */ |
| 777 | + if (am_receiver()) { |
| 778 | + /* Pass the message to the generator thread. */ |
| 779 | send_msg((enum msgcode)code, buf, len); |
| 780 | return; |
| 781 | } |
| 782 | --- old/main.c |
| 783 | +++ new/main.c |
| 784 | @@ -32,7 +32,6 @@ extern int list_only; |
| 785 | extern int am_root; |
| 786 | extern int am_server; |
| 787 | extern int am_sender; |
| 788 | -extern int am_generator; |
| 789 | extern int am_daemon; |
| 790 | extern int blocking_io; |
| 791 | extern int remove_sent_files; |
| 792 | @@ -93,9 +92,20 @@ struct pid_status { |
| 793 | |
| 794 | static time_t starttime, endtime; |
| 795 | static int64 total_read, total_written; |
| 796 | +static pthread_t receiver_tid; |
| 797 | |
| 798 | static void show_malloc_stats(void); |
| 799 | |
| 800 | +int am_generator() |
| 801 | +{ |
| 802 | + return receiver_tid != 0 && pthread_self() != receiver_tid; |
| 803 | +} |
| 804 | + |
| 805 | +int am_receiver() |
| 806 | +{ |
| 807 | + return receiver_tid != 0 && pthread_self() == receiver_tid; |
| 808 | +} |
| 809 | + |
| 810 | /* Works like waitpid(), but if we already harvested the child pid in our |
| 811 | * remember_children(), we succeed instead of returning an error. */ |
| 812 | pid_t wait_process(pid_t pid, int *status_ptr, int flags) |
| 813 | @@ -172,7 +182,7 @@ static void handle_stats(int f) |
| 814 | show_flist_stats(); |
| 815 | } |
| 816 | |
| 817 | - if (am_generator) |
| 818 | + if (am_generator()) |
| 819 | return; |
| 820 | |
| 821 | if (am_daemon) { |
| 822 | @@ -647,12 +657,30 @@ static void do_server_sender(int f_in, i |
| 823 | exit_cleanup(0); |
| 824 | } |
| 825 | |
| 826 | +struct thread_args { |
| 827 | + struct file_list *flist; |
| 828 | + char *local_name; |
| 829 | + int f_in; |
| 830 | +}; |
| 831 | + |
| 832 | +static void *start_receiver_thread(void *arg) |
| 833 | +{ |
| 834 | + static int exit_code; |
| 835 | + struct thread_args *ta = (struct thread_args *)arg; |
| 836 | + |
| 837 | + recv_files(ta->f_in, ta->flist, ta->local_name); |
| 838 | + handle_stats(ta->f_in); |
| 839 | + |
| 840 | + push_redo_num(-2); |
| 841 | + |
| 842 | + exit_code = log_got_error ? RERR_PARTIAL : 0; |
| 843 | + return &exit_code; |
| 844 | +} |
| 845 | |
| 846 | static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) |
| 847 | { |
| 848 | - int pid; |
| 849 | - int exit_code = 0; |
| 850 | - int error_pipe[2]; |
| 851 | + void *value_ptr; |
| 852 | + struct thread_args args; |
| 853 | |
| 854 | /* The receiving side mustn't obey this, or an existing symlink that |
| 855 | * points to an identical file won't be replaced by the referent. */ |
| 856 | @@ -661,70 +689,16 @@ static int do_recv(int f_in,int f_out,st |
| 857 | if (preserve_hard_links) |
| 858 | init_hard_links(); |
| 859 | |
| 860 | - if (fd_pair(error_pipe) < 0) { |
| 861 | - rsyserr(FERROR, errno, "pipe failed in do_recv"); |
| 862 | + args.f_in = f_in; |
| 863 | + args.flist = flist; |
| 864 | + args.local_name = local_name; |
| 865 | + if (pthread_create(&receiver_tid, NULL, start_receiver_thread, &args) < 0) { |
| 866 | + rsyserr(FERROR, errno, "pthread_create failed in do_recv"); |
| 867 | exit_cleanup(RERR_IPC); |
| 868 | } |
| 869 | |
| 870 | - io_flush(NORMAL_FLUSH); |
| 871 | - |
| 872 | - if ((pid = do_fork()) == -1) { |
| 873 | - rsyserr(FERROR, errno, "fork failed in do_recv"); |
| 874 | - exit_cleanup(RERR_IPC); |
| 875 | - } |
| 876 | - |
| 877 | - if (pid == 0) { |
| 878 | - close(error_pipe[0]); |
| 879 | - if (f_in != f_out) |
| 880 | - close(f_out); |
| 881 | - |
| 882 | - /* we can't let two processes write to the socket at one time */ |
| 883 | - close_multiplexing_out(); |
| 884 | - |
| 885 | - /* set place to send errors */ |
| 886 | - set_msg_fd_out(error_pipe[1]); |
| 887 | - |
| 888 | - recv_files(f_in, flist, local_name); |
| 889 | - io_flush(FULL_FLUSH); |
| 890 | - handle_stats(f_in); |
| 891 | - |
| 892 | - send_msg(MSG_DONE, "", 0); |
| 893 | - io_flush(FULL_FLUSH); |
| 894 | - |
| 895 | - /* Handle any keep-alive packets from the post-processing work |
| 896 | - * that the generator does. */ |
| 897 | - if (protocol_version >= 29) { |
| 898 | - kluge_around_eof = -1; |
| 899 | - |
| 900 | - /* This should only get stopped via a USR2 signal. */ |
| 901 | - while (read_int(f_in) == flist->count |
| 902 | - && read_shortint(f_in) == ITEM_IS_NEW) {} |
| 903 | - |
| 904 | - rprintf(FERROR, "Invalid packet at end of run [%s]\n", |
| 905 | - who_am_i()); |
| 906 | - exit_cleanup(RERR_PROTOCOL); |
| 907 | - } |
| 908 | - |
| 909 | - /* Finally, we go to sleep until our parent kills us with a |
| 910 | - * USR2 signal. We sleep for a short time, as on some OSes |
| 911 | - * a signal won't interrupt a sleep! */ |
| 912 | - while (1) |
| 913 | - msleep(20); |
| 914 | - } |
| 915 | - |
| 916 | - am_generator = 1; |
| 917 | - close_multiplexing_in(); |
| 918 | - if (write_batch && !am_server) |
| 919 | - stop_write_batch(); |
| 920 | - |
| 921 | - close(error_pipe[1]); |
| 922 | - if (f_in != f_out) |
| 923 | - close(f_in); |
| 924 | - |
| 925 | io_start_buffering_out(); |
| 926 | |
| 927 | - set_msg_fd_in(error_pipe[0]); |
| 928 | - |
| 929 | generate_files(f_out, flist, local_name); |
| 930 | |
| 931 | handle_stats(-1); |
| 932 | @@ -735,10 +709,13 @@ static int do_recv(int f_in,int f_out,st |
| 933 | } |
| 934 | io_flush(FULL_FLUSH); |
| 935 | |
| 936 | - set_msg_fd_in(-1); |
| 937 | - kill(pid, SIGUSR2); |
| 938 | - wait_process_with_flush(pid, &exit_code); |
| 939 | - return exit_code; |
| 940 | + pthread_join(receiver_tid, &value_ptr); |
| 941 | + if (!am_server) |
| 942 | + output_summary(); |
| 943 | + |
| 944 | + close_all(); |
| 945 | + |
| 946 | + return *(int*)value_ptr; |
| 947 | } |
| 948 | |
| 949 | |
| 950 | @@ -1140,22 +1117,6 @@ static int start_client(int argc, char * |
| 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | - |
| 955 | -static RETSIGTYPE sigusr1_handler(UNUSED(int val)) |
| 956 | -{ |
| 957 | - exit_cleanup(RERR_SIGNAL1); |
| 958 | -} |
| 959 | - |
| 960 | -static RETSIGTYPE sigusr2_handler(UNUSED(int val)) |
| 961 | -{ |
| 962 | - if (!am_server) |
| 963 | - output_summary(); |
| 964 | - close_all(); |
| 965 | - if (log_got_error) |
| 966 | - _exit(RERR_PARTIAL); |
| 967 | - _exit(0); |
| 968 | -} |
| 969 | - |
| 970 | RETSIGTYPE remember_children(UNUSED(int val)) |
| 971 | { |
| 972 | #ifdef WNOHANG |
| 973 | @@ -1247,8 +1208,6 @@ int main(int argc,char *argv[]) |
| 974 | # endif |
| 975 | sigact.sa_flags = SA_NOCLDSTOP; |
| 976 | #endif |
| 977 | - SIGACTMASK(SIGUSR1, sigusr1_handler); |
| 978 | - SIGACTMASK(SIGUSR2, sigusr2_handler); |
| 979 | SIGACTMASK(SIGCHLD, remember_children); |
| 980 | #ifdef MAINTAINER_MODE |
| 981 | SIGACTMASK(SIGSEGV, rsync_panic_handler); |
| 982 | --- old/match.c |
| 983 | +++ new/match.c |
| 984 | @@ -23,7 +23,7 @@ |
| 985 | #include "rsync.h" |
| 986 | |
| 987 | extern int verbose; |
| 988 | -extern int do_progress; |
| 989 | +extern int recv_progress; |
| 990 | extern int checksum_seed; |
| 991 | extern int append_mode; |
| 992 | |
| 993 | @@ -113,7 +113,7 @@ static void matched(int f, struct sum_st |
| 994 | else |
| 995 | last_match = offset; |
| 996 | |
| 997 | - if (buf && do_progress) |
| 998 | + if (buf && recv_progress) |
| 999 | show_progress(last_match, buf->file_size); |
| 1000 | } |
| 1001 | |
| 1002 | @@ -317,7 +317,7 @@ void match_sums(int f, struct sum_struct |
| 1003 | if (append_mode) { |
| 1004 | OFF_T j = 0; |
| 1005 | for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) { |
| 1006 | - if (buf && do_progress) |
| 1007 | + if (buf && recv_progress) |
| 1008 | show_progress(last_match, buf->file_size); |
| 1009 | sum_update(map_ptr(buf, last_match, CHUNK_SIZE), |
| 1010 | CHUNK_SIZE); |
| 1011 | @@ -325,7 +325,7 @@ void match_sums(int f, struct sum_struct |
| 1012 | } |
| 1013 | if (last_match < s->flength) { |
| 1014 | int32 len = s->flength - last_match; |
| 1015 | - if (buf && do_progress) |
| 1016 | + if (buf && recv_progress) |
| 1017 | show_progress(last_match, buf->file_size); |
| 1018 | sum_update(map_ptr(buf, last_match, len), len); |
| 1019 | last_match = s->flength; |
| 1020 | --- old/options.c |
| 1021 | +++ new/options.c |
| 1022 | @@ -74,7 +74,6 @@ int def_compress_level = Z_DEFAULT_COMPR |
| 1023 | int am_root = 0; |
| 1024 | int am_server = 0; |
| 1025 | int am_sender = 0; |
| 1026 | -int am_generator = 0; |
| 1027 | int am_starting_up = 1; |
| 1028 | int relative_paths = -1; |
| 1029 | int implied_dirs = 1; |
| 1030 | @@ -95,6 +94,7 @@ int am_daemon = 0; |
| 1031 | int daemon_over_rsh = 0; |
| 1032 | int do_stats = 0; |
| 1033 | int do_progress = 0; |
| 1034 | +int recv_progress = 0; |
| 1035 | int keep_partial = 0; |
| 1036 | int safe_symlinks = 0; |
| 1037 | int copy_unsafe_links = 0; |
| 1038 | @@ -1304,6 +1304,7 @@ int parse_arguments(int *argc, const cha |
| 1039 | |
| 1040 | if (do_progress && !verbose && !log_before_transfer && !am_server) |
| 1041 | verbose = 1; |
| 1042 | + recv_progress = do_progress; |
| 1043 | |
| 1044 | if (dry_run) |
| 1045 | do_xfers = 0; |
| 1046 | --- old/pipe.c |
| 1047 | +++ new/pipe.c |
| 1048 | @@ -59,7 +59,7 @@ pid_t piped_child(char **command, int *f |
| 1049 | exit_cleanup(RERR_IPC); |
| 1050 | } |
| 1051 | |
| 1052 | - pid = do_fork(); |
| 1053 | + pid = fork(); |
| 1054 | if (pid == -1) { |
| 1055 | rsyserr(FERROR, errno, "fork"); |
| 1056 | exit_cleanup(RERR_IPC); |
| 1057 | @@ -123,7 +123,7 @@ pid_t local_child(int argc, char **argv, |
| 1058 | exit_cleanup(RERR_IPC); |
| 1059 | } |
| 1060 | |
| 1061 | - pid = do_fork(); |
| 1062 | + pid = fork(); |
| 1063 | if (pid == -1) { |
| 1064 | rsyserr(FERROR, errno, "fork"); |
| 1065 | exit_cleanup(RERR_IPC); |
| 1066 | --- old/receiver.c |
| 1067 | +++ new/receiver.c |
| 1068 | @@ -25,7 +25,7 @@ |
| 1069 | extern int verbose; |
| 1070 | extern int do_xfers; |
| 1071 | extern int am_server; |
| 1072 | -extern int do_progress; |
| 1073 | +extern int recv_progress; |
| 1074 | extern int log_before_transfer; |
| 1075 | extern int stdout_format_has_i; |
| 1076 | extern int logfile_format_has_i; |
| 1077 | @@ -154,7 +154,7 @@ static int receive_data(int f_in, char * |
| 1078 | if (sum.remainder) |
| 1079 | sum.flength -= sum.blength - sum.remainder; |
| 1080 | for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) { |
| 1081 | - if (do_progress) |
| 1082 | + if (recv_progress) |
| 1083 | show_progress(offset, total_size); |
| 1084 | sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE), |
| 1085 | CHUNK_SIZE); |
| 1086 | @@ -162,7 +162,7 @@ static int receive_data(int f_in, char * |
| 1087 | } |
| 1088 | if (offset < sum.flength) { |
| 1089 | int32 len = sum.flength - offset; |
| 1090 | - if (do_progress) |
| 1091 | + if (recv_progress) |
| 1092 | show_progress(offset, total_size); |
| 1093 | sum_update(map_ptr(mapbuf, offset, len), len); |
| 1094 | offset = sum.flength; |
| 1095 | @@ -175,7 +175,7 @@ static int receive_data(int f_in, char * |
| 1096 | } |
| 1097 | |
| 1098 | while ((i = recv_token(f_in, &data)) != 0) { |
| 1099 | - if (do_progress) |
| 1100 | + if (recv_progress) |
| 1101 | show_progress(offset, total_size); |
| 1102 | |
| 1103 | if (i > 0) { |
| 1104 | @@ -245,7 +245,7 @@ static int receive_data(int f_in, char * |
| 1105 | ftruncate(fd, offset); |
| 1106 | #endif |
| 1107 | |
| 1108 | - if (do_progress) |
| 1109 | + if (recv_progress) |
| 1110 | end_progress(total_size); |
| 1111 | |
| 1112 | if (fd != -1 && offset > 0 && sparse_end(fd) != 0) { |
| 1113 | @@ -296,12 +296,12 @@ static void handle_delayed_updates(struc |
| 1114 | "rename failed for %s (from %s)", |
| 1115 | full_fname(fname), partialptr); |
| 1116 | } else { |
| 1117 | - if (remove_sent_files |
| 1118 | - || (preserve_hard_links |
| 1119 | - && file->link_u.links)) { |
| 1120 | + if (remove_sent_files) { |
| 1121 | SIVAL(numbuf, 0, i); |
| 1122 | send_msg(MSG_SUCCESS,numbuf,4); |
| 1123 | } |
| 1124 | + if (preserve_hard_links && file->link_u.links) |
| 1125 | + push_hlink_num(i); |
| 1126 | handle_partial_dir(partialptr, PDIR_DELETE); |
| 1127 | } |
| 1128 | } |
| 1129 | @@ -352,11 +352,6 @@ int recv_files(int f_in, struct file_lis |
| 1130 | if (verbose > 2) |
| 1131 | rprintf(FINFO,"recv_files(%d) starting\n",flist->count); |
| 1132 | |
| 1133 | - if (flist->hlink_pool) { |
| 1134 | - pool_destroy(flist->hlink_pool); |
| 1135 | - flist->hlink_pool = NULL; |
| 1136 | - } |
| 1137 | - |
| 1138 | if (delay_updates) |
| 1139 | delayed_bits = bitbag_create(flist->count); |
| 1140 | |
| 1141 | @@ -377,7 +372,7 @@ int recv_files(int f_in, struct file_lis |
| 1142 | rprintf(FINFO, "recv_files phase=%d\n", phase); |
| 1143 | if (phase == 2 && delay_updates) |
| 1144 | handle_delayed_updates(flist, local_name); |
| 1145 | - send_msg(MSG_DONE, "", 0); |
| 1146 | + push_redo_num(-2); |
| 1147 | if (keep_partial && !partial_dir) |
| 1148 | make_backups = 0; /* prevents double backup */ |
| 1149 | if (append_mode) { |
| 1150 | @@ -597,7 +592,7 @@ int recv_files(int f_in, struct file_lis |
| 1151 | /* log the transfer */ |
| 1152 | if (log_before_transfer) |
| 1153 | log_item(FNAME, file, &initial_stats, iflags, NULL); |
| 1154 | - else if (!am_server && verbose && do_progress) |
| 1155 | + else if (!am_server && verbose && recv_progress) |
| 1156 | rprintf(FINFO, "%s\n", fname); |
| 1157 | |
| 1158 | /* recv file data */ |
| 1159 | @@ -644,11 +639,13 @@ int recv_files(int f_in, struct file_lis |
| 1160 | cleanup_disable(); |
| 1161 | |
| 1162 | if (recv_ok > 0) { |
| 1163 | - if (remove_sent_files |
| 1164 | - || (preserve_hard_links && file->link_u.links)) { |
| 1165 | + if (remove_sent_files) { |
| 1166 | + decrement_active_files(i); |
| 1167 | SIVAL(numbuf, 0, i); |
| 1168 | send_msg(MSG_SUCCESS, numbuf, 4); |
| 1169 | } |
| 1170 | + if (preserve_hard_links && file->link_u.links) |
| 1171 | + push_hlink_num(i); |
| 1172 | } else if (!recv_ok) { |
| 1173 | int msgtype = phase || read_batch ? FERROR : FINFO; |
| 1174 | if (msgtype == FERROR || verbose) { |
| 1175 | @@ -671,8 +668,8 @@ int recv_files(int f_in, struct file_lis |
| 1176 | errstr, fname, keptstr, redostr); |
| 1177 | } |
| 1178 | if (!phase) { |
| 1179 | - SIVAL(numbuf, 0, i); |
| 1180 | - send_msg(MSG_REDO, numbuf, 4); |
| 1181 | + decrement_active_files(i); |
| 1182 | + push_redo_num(i); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | --- old/rsync.c |
| 1187 | +++ new/rsync.c |
| 1188 | @@ -40,7 +40,6 @@ extern int omit_dir_times; |
| 1189 | extern int am_root; |
| 1190 | extern int am_server; |
| 1191 | extern int am_sender; |
| 1192 | -extern int am_generator; |
| 1193 | extern int am_starting_up; |
| 1194 | extern int allow_8bit_chars; |
| 1195 | extern int preserve_uid; |
| 1196 | @@ -302,5 +301,5 @@ const char *who_am_i(void) |
| 1197 | { |
| 1198 | if (am_starting_up) |
| 1199 | return am_server ? "server" : "client"; |
| 1200 | - return am_sender ? "sender" : am_generator ? "generator" : "receiver"; |
| 1201 | + return am_sender ? "sender" : am_generator() ? "generator" : "receiver"; |
| 1202 | } |
| 1203 | --- old/rsync.h |
| 1204 | +++ new/rsync.h |
| 1205 | @@ -169,10 +169,8 @@ enum msgcode { |
| 1206 | MSG_DATA=0, /* raw data on the multiplexed stream */ |
| 1207 | MSG_ERROR=FERROR, MSG_INFO=FINFO, /* remote logging */ |
| 1208 | MSG_LOG=FLOG, MSG_SOCKERR=FSOCKERR, /* sibling logging */ |
| 1209 | - MSG_REDO=9, /* reprocess indicated flist index */ |
| 1210 | MSG_SUCCESS=100,/* successfully updated indicated flist index */ |
| 1211 | MSG_DELETED=101,/* successfully deleted a file on receiving side */ |
| 1212 | - MSG_DONE=86 /* current phase is done */ |
| 1213 | }; |
| 1214 | |
| 1215 | #include "errcode.h" |
| 1216 | @@ -323,6 +321,7 @@ enum msgcode { |
| 1217 | #endif |
| 1218 | |
| 1219 | #include <assert.h> |
| 1220 | +#include <pthread.h> |
| 1221 | |
| 1222 | #include "lib/pool_alloc.h" |
| 1223 | |
| 1224 | --- old/util.c |
| 1225 | +++ new/util.c |
| 1226 | @@ -415,49 +415,6 @@ int robust_rename(char *from, char *to, |
| 1227 | return -1; |
| 1228 | } |
| 1229 | |
| 1230 | -static pid_t all_pids[10]; |
| 1231 | -static int num_pids; |
| 1232 | - |
| 1233 | -/** Fork and record the pid of the child. **/ |
| 1234 | -pid_t do_fork(void) |
| 1235 | -{ |
| 1236 | - pid_t newpid = fork(); |
| 1237 | - |
| 1238 | - if (newpid != 0 && newpid != -1) { |
| 1239 | - all_pids[num_pids++] = newpid; |
| 1240 | - } |
| 1241 | - return newpid; |
| 1242 | -} |
| 1243 | - |
| 1244 | -/** |
| 1245 | - * Kill all children. |
| 1246 | - * |
| 1247 | - * @todo It would be kind of nice to make sure that they are actually |
| 1248 | - * all our children before we kill them, because their pids may have |
| 1249 | - * been recycled by some other process. Perhaps when we wait for a |
| 1250 | - * child, we should remove it from this array. Alternatively we could |
| 1251 | - * perhaps use process groups, but I think that would not work on |
| 1252 | - * ancient Unix versions that don't support them. |
| 1253 | - **/ |
| 1254 | -void kill_all(int sig) |
| 1255 | -{ |
| 1256 | - int i; |
| 1257 | - |
| 1258 | - for (i = 0; i < num_pids; i++) { |
| 1259 | - /* Let's just be a little careful where we |
| 1260 | - * point that gun, hey? See kill(2) for the |
| 1261 | - * magic caused by negative values. */ |
| 1262 | - pid_t p = all_pids[i]; |
| 1263 | - |
| 1264 | - if (p == getpid()) |
| 1265 | - continue; |
| 1266 | - if (p <= 0) |
| 1267 | - continue; |
| 1268 | - |
| 1269 | - kill(p, sig); |
| 1270 | - } |
| 1271 | -} |
| 1272 | - |
| 1273 | /** Turn a user name into a uid */ |
| 1274 | int name_to_uid(char *name, uid_t *uid) |
| 1275 | { |