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