Decided on a better option name --itemize-changes (-i).
[rsync/rsync.git] / main.c
1 /* -*- c-file-style: "linux" -*-
2
3    Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org>
4    Copyright (C) Paul Mackerras 1996
5    Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "rsync.h"
23
24 time_t starttime = 0;
25
26 extern struct stats stats;
27 extern int am_root;
28 extern int am_server;
29 extern int am_sender;
30 extern int am_generator;
31 extern int am_daemon;
32 extern int verbose;
33 extern int itemize_changes;
34 extern int blocking_io;
35 extern int delete_before;
36 extern int daemon_over_rsh;
37 extern int do_stats;
38 extern int dry_run;
39 extern int list_only;
40 extern int log_got_error;
41 extern int module_id;
42 extern int orig_umask;
43 extern int copy_links;
44 extern int keep_dirlinks;
45 extern int preserve_hard_links;
46 extern int protocol_version;
47 extern int recurse;
48 extern int fuzzy_basis;
49 extern int relative_paths;
50 extern int rsync_port;
51 extern int whole_file;
52 extern int read_batch;
53 extern int write_batch;
54 extern int batch_fd;
55 extern int batch_gen_fd;
56 extern int filesfrom_fd;
57 extern pid_t cleanup_child_pid;
58 extern char *files_from;
59 extern char *remote_filesfrom_file;
60 extern char *partial_dir;
61 extern char *basis_dir[];
62 extern char *rsync_path;
63 extern char *shell_cmd;
64 extern char *batch_name;
65
66 int local_server = 0;
67
68 /* There's probably never more than at most 2 outstanding child processes,
69  * but set it higher, just in case. */
70 #define MAXCHILDPROCS 5
71
72 struct pid_status {
73         pid_t pid;
74         int   status;
75 } pid_stat_table[MAXCHILDPROCS];
76
77 static void show_malloc_stats(void);
78
79 /****************************************************************************
80 wait for a process to exit, calling io_flush while waiting
81 ****************************************************************************/
82 void wait_process(pid_t pid, int *status)
83 {
84         pid_t waited_pid;
85         int cnt;
86
87         while ((waited_pid = waitpid(pid, status, WNOHANG)) == 0) {
88                 msleep(20);
89                 io_flush(FULL_FLUSH);
90         }
91
92         if (waited_pid == -1 && errno == ECHILD) {
93                 /* status of requested child no longer available.
94                  * check to see if it was processed by the sigchld_handler.
95                  */
96                 for (cnt = 0;  cnt < MAXCHILDPROCS; cnt++) {
97                         if (pid == pid_stat_table[cnt].pid) {
98                                 *status = pid_stat_table[cnt].status;
99                                 pid_stat_table[cnt].pid = 0;
100                                 break;
101                         }
102                 }
103         }
104
105         /* TODO: If the child exited on a signal, then log an
106          * appropriate error message.  Perhaps we should also accept a
107          * message describing the purpose of the child.  Also indicate
108          * this to the caller so that thhey know something went
109          * wrong.  */
110         *status = WEXITSTATUS(*status);
111 }
112
113 /* This function gets called from all 3 processes.  We want the client side
114  * to actually output the text, but the sender is the only process that has
115  * all the stats we need.  So, if we're a client sender, we do the report.
116  * If we're a server sender, we write the stats on the supplied fd.  If
117  * we're the client receiver we read the stats from the supplied fd and do
118  * the report.  All processes might also generate a set of debug stats, if
119  * the verbose level is high enough (this is the only thing that the
120  * generator process and the server receiver ever do here). */
121 static void report(int f)
122 {
123         /* Cache two stats because the read/write code can change it. */
124         int64 total_read = stats.total_read;
125         int64 total_written = stats.total_written;
126         time_t t = time(NULL);
127
128         if (do_stats && verbose > 1) {
129                 /* These come out from every process */
130                 show_malloc_stats();
131                 show_flist_stats();
132         }
133
134         if (am_generator)
135                 return;
136
137         if (am_daemon) {
138                 log_exit(0, __FILE__, __LINE__);
139                 if (f == -1 || !am_sender)
140                         return;
141         }
142
143         if (am_server) {
144                 if (am_sender) {
145                         write_longint(f, total_read);
146                         write_longint(f, total_written);
147                         write_longint(f, stats.total_size);
148                         if (protocol_version >= 29) {
149                                 write_longint(f, stats.flist_buildtime);
150                                 write_longint(f, stats.flist_xfertime);
151                         }
152                 }
153                 return;
154         }
155
156         /* this is the client */
157
158         if (!am_sender) {
159                 /* Read the first two in opposite order because the meaning of
160                  * read/write swaps when switching from sender to receiver. */
161                 total_written = read_longint(f);
162                 total_read = read_longint(f);
163                 stats.total_size = read_longint(f);
164                 if (protocol_version >= 29) {
165                         stats.flist_buildtime = read_longint(f);
166                         stats.flist_xfertime = read_longint(f);
167                 }
168         } else if (write_batch) {
169                 /* The --read-batch process is going to be a client
170                  * receiver, so we need to give it the stats. */
171                 write_longint(batch_fd, total_read);
172                 write_longint(batch_fd, total_written);
173                 write_longint(batch_fd, stats.total_size);
174                 if (protocol_version >= 29) {
175                         write_longint(batch_fd, stats.flist_buildtime);
176                         write_longint(batch_fd, stats.flist_xfertime);
177                 }
178         }
179
180         if (do_stats) {
181                 rprintf(FINFO,"\nNumber of files: %d\n", stats.num_files);
182                 rprintf(FINFO,"Number of files transferred: %d\n",
183                         stats.num_transferred_files);
184                 rprintf(FINFO,"Total file size: %.0f bytes\n",
185                         (double)stats.total_size);
186                 rprintf(FINFO,"Total transferred file size: %.0f bytes\n",
187                         (double)stats.total_transferred_size);
188                 rprintf(FINFO,"Literal data: %.0f bytes\n",
189                         (double)stats.literal_data);
190                 rprintf(FINFO,"Matched data: %.0f bytes\n",
191                         (double)stats.matched_data);
192                 rprintf(FINFO,"File list size: %d\n", stats.flist_size);
193                 if (stats.flist_buildtime) {
194                         rprintf(FINFO,
195                                 "File list generation time: %.3f seconds\n",
196                                 (double)stats.flist_buildtime / 1000);
197                         rprintf(FINFO,
198                                 "File list transfer time: %.3f seconds\n",
199                                 (double)stats.flist_xfertime / 1000);
200                 }
201                 rprintf(FINFO,"Total bytes sent: %.0f\n",
202                         (double)total_written);
203                 rprintf(FINFO,"Total bytes received: %.0f\n",
204                         (double)total_read);
205         }
206
207         if (verbose || do_stats) {
208                 rprintf(FINFO,
209                         "\nsent %.0f bytes  received %.0f bytes  %.2f bytes/sec\n",
210                         (double)total_written, (double)total_read,
211                         (total_written + total_read)/(0.5 + (t - starttime)));
212                 rprintf(FINFO, "total size is %.0f  speedup is %.2f\n",
213                         (double)stats.total_size,
214                         (double)stats.total_size / (total_written+total_read));
215         }
216
217         fflush(stdout);
218         fflush(stderr);
219 }
220
221
222 /**
223  * If our C library can get malloc statistics, then show them to FINFO
224  **/
225 static void show_malloc_stats(void)
226 {
227 #ifdef HAVE_MALLINFO
228         struct mallinfo mi;
229
230         mi = mallinfo();
231
232         rprintf(FINFO, "\n" RSYNC_NAME "[%d] (%s%s%s) heap statistics:\n",
233                 getpid(), am_server ? "server " : "",
234                 am_daemon ? "daemon " : "", who_am_i());
235         rprintf(FINFO, "  arena:     %10ld   (bytes from sbrk)\n",
236                 (long)mi.arena);
237         rprintf(FINFO, "  ordblks:   %10ld   (chunks not in use)\n",
238                 (long)mi.ordblks);
239         rprintf(FINFO, "  smblks:    %10ld\n",
240                 (long)mi.smblks);
241         rprintf(FINFO, "  hblks:     %10ld   (chunks from mmap)\n",
242                 (long)mi.hblks);
243         rprintf(FINFO, "  hblkhd:    %10ld   (bytes from mmap)\n",
244                 (long)mi.hblkhd);
245         rprintf(FINFO, "  allmem:    %10ld   (bytes from sbrk + mmap)\n",
246                 (long)mi.arena + mi.hblkhd);
247         rprintf(FINFO, "  usmblks:   %10ld\n",
248                 (long)mi.usmblks);
249         rprintf(FINFO, "  fsmblks:   %10ld\n",
250                 (long)mi.fsmblks);
251         rprintf(FINFO, "  uordblks:  %10ld   (bytes used)\n",
252                 (long)mi.uordblks);
253         rprintf(FINFO, "  fordblks:  %10ld   (bytes free)\n",
254                 (long)mi.fordblks);
255         rprintf(FINFO, "  keepcost:  %10ld   (bytes in releasable chunk)\n",
256                 (long)mi.keepcost);
257 #endif /* HAVE_MALLINFO */
258 }
259
260
261 /* Start the remote shell.   cmd may be NULL to use the default. */
262 static pid_t do_cmd(char *cmd, char *machine, char *user, char *path,
263                     int *f_in, int *f_out)
264 {
265         int i, argc = 0;
266         char *args[MAX_ARGS];
267         pid_t ret;
268         char *tok, *dir = NULL;
269         int dash_l_set = 0;
270
271         if (!read_batch && !local_server) {
272                 char *rsh_env = getenv(RSYNC_RSH_ENV);
273                 if (!cmd)
274                         cmd = rsh_env;
275                 if (!cmd)
276                         cmd = RSYNC_RSH;
277                 cmd = strdup(cmd);
278                 if (!cmd)
279                         goto oom;
280
281                 for (tok = strtok(cmd, " "); tok; tok = strtok(NULL, " ")) {
282                         /* Comparison leaves rooms for server_options(). */
283                         if (argc >= MAX_ARGS - 100) {
284                                 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
285                                 exit_cleanup(RERR_SYNTAX);
286                         }
287                         args[argc++] = tok;
288                 }
289
290                 /* check to see if we've already been given '-l user' in
291                  * the remote-shell command */
292                 for (i = 0; i < argc-1; i++) {
293                         if (!strcmp(args[i], "-l") && args[i+1][0] != '-')
294                                 dash_l_set = 1;
295                 }
296
297 #ifdef HAVE_REMSH
298                 /* remsh (on HPUX) takes the arguments the other way around */
299                 args[argc++] = machine;
300                 if (user && !(daemon_over_rsh && dash_l_set)) {
301                         args[argc++] = "-l";
302                         args[argc++] = user;
303                 }
304 #else
305                 if (user && !(daemon_over_rsh && dash_l_set)) {
306                         args[argc++] = "-l";
307                         args[argc++] = user;
308                 }
309                 args[argc++] = machine;
310 #endif
311
312                 args[argc++] = rsync_path;
313
314                 if (blocking_io < 0) {
315                         char *cp;
316                         if ((cp = strrchr(cmd, '/')) != NULL)
317                                 cp++;
318                         else
319                                 cp = cmd;
320                         if (strcmp(cp, "rsh") == 0 || strcmp(cp, "remsh") == 0)
321                                 blocking_io = 1;
322                 }
323
324                 server_options(args,&argc);
325
326                 if (argc >= MAX_ARGS - 2) {
327                         rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
328                         exit_cleanup(RERR_SYNTAX);
329                 }
330         }
331
332         args[argc++] = ".";
333
334         if (!daemon_over_rsh && path && *path)
335                 args[argc++] = path;
336
337         args[argc] = NULL;
338
339         if (verbose > 3) {
340                 rprintf(FINFO,"cmd=");
341                 for (i = 0; i < argc; i++)
342                         rprintf(FINFO, "%s ", safe_fname(args[i]));
343                 rprintf(FINFO,"\n");
344         }
345
346         if (read_batch) {
347                 int from_gen_pipe[2];
348                 if (fd_pair(from_gen_pipe) < 0) {
349                         rsyserr(FERROR, errno, "pipe");
350                         exit_cleanup(RERR_IPC);
351                 }
352                 batch_gen_fd = from_gen_pipe[0];
353                 *f_out = from_gen_pipe[1];
354                 *f_in = batch_fd;
355                 ret = -1; /* no child pid */
356         } else if (local_server) {
357                 /* If the user didn't request --[no-]whole-file, force
358                  * it on, but only if we're not batch processing. */
359                 if (whole_file < 0 && !write_batch)
360                         whole_file = 1;
361                 ret = local_child(argc, args, f_in, f_out, child_main);
362         } else
363                 ret = piped_child(args,f_in,f_out);
364
365         if (dir)
366                 free(dir);
367
368         return ret;
369
370 oom:
371         out_of_memory("do_cmd");
372         return 0; /* not reached */
373 }
374
375
376 static char *get_local_name(struct file_list *flist,char *name)
377 {
378         STRUCT_STAT st;
379         int e;
380
381         if (verbose > 2)
382                 rprintf(FINFO,"get_local_name count=%d %s\n",
383                         flist->count, NS(name));
384
385         if (!name)
386                 return NULL;
387
388         if (do_stat(name,&st) == 0) {
389                 if (S_ISDIR(st.st_mode)) {
390                         if (!push_dir(name)) {
391                                 rsyserr(FERROR, errno, "push_dir#1 %s failed",
392                                         full_fname(name));
393                                 exit_cleanup(RERR_FILESELECT);
394                         }
395                         return NULL;
396                 }
397                 if (flist->count > 1) {
398                         rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
399                         exit_cleanup(RERR_FILESELECT);
400                 }
401                 return name;
402         }
403
404         if (flist->count <= 1 && ((e = strlen(name)) <= 1 || name[e-1] != '/'))
405                 return name;
406
407         if (do_mkdir(name,0777 & ~orig_umask) != 0) {
408                 rsyserr(FERROR, errno, "mkdir %s failed", full_fname(name));
409                 exit_cleanup(RERR_FILEIO);
410         }
411         if (verbose > 0)
412                 rprintf(FINFO, "created directory %s\n", safe_fname(name));
413
414         if (dry_run) {
415                 dry_run++;
416                 return NULL;
417         }
418
419         if (!push_dir(name)) {
420                 rsyserr(FERROR, errno, "push_dir#2 %s failed",
421                         full_fname(name));
422                 exit_cleanup(RERR_FILESELECT);
423         }
424
425         return NULL;
426 }
427
428
429 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
430 {
431         int i;
432         struct file_list *flist;
433         char *dir = argv[0];
434
435         if (verbose > 2) {
436                 rprintf(FINFO, "server_sender starting pid=%ld\n",
437                         (long)getpid());
438         }
439
440         if (am_daemon && lp_write_only(module_id)) {
441                 rprintf(FERROR, "ERROR: module is write only\n");
442                 exit_cleanup(RERR_SYNTAX);
443                 return;
444         }
445
446         if (!relative_paths && !push_dir(dir)) {
447                 rsyserr(FERROR, errno, "push_dir#3 %s failed",
448                         full_fname(dir));
449                 exit_cleanup(RERR_FILESELECT);
450         }
451         argc--;
452         argv++;
453
454         if (strcmp(dir,".")) {
455                 int l = strlen(dir);
456                 if (strcmp(dir,"/") == 0)
457                         l = 0;
458                 for (i = 0; i < argc; i++)
459                         argv[i] += l+1;
460         }
461
462         if (argc == 0 && (recurse || list_only)) {
463                 argc = 1;
464                 argv--;
465                 argv[0] = ".";
466         }
467
468         flist = send_file_list(f_out,argc,argv);
469         if (!flist || flist->count == 0) {
470                 exit_cleanup(0);
471         }
472
473         io_start_buffering_in();
474         io_start_buffering_out();
475
476         send_files(flist,f_out,f_in);
477         io_flush(FULL_FLUSH);
478         report(f_out);
479         if (protocol_version >= 24) {
480                 /* final goodbye message */
481                 read_int(f_in);
482         }
483         io_flush(FULL_FLUSH);
484         exit_cleanup(0);
485 }
486
487
488 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
489 {
490         int pid;
491         int status = 0;
492         int error_pipe[2], name_pipe[2];
493         BOOL need_name_pipe = (basis_dir[0] || partial_dir || fuzzy_basis)
494                             && !dry_run;
495
496         /* The receiving side mustn't obey this, or an existing symlink that
497          * points to an identical file won't be replaced by the referent. */
498         copy_links = 0;
499
500         if (preserve_hard_links)
501                 init_hard_links(flist);
502
503         if (delete_before && !local_name && flist->count > 0) {
504                 /* Moved here from recv_files() to prevent a race condition */
505                 delete_files(flist);
506         }
507
508         if (fd_pair(error_pipe) < 0
509             || (need_name_pipe && fd_pair(name_pipe) < 0)) {
510                 rsyserr(FERROR, errno, "pipe failed in do_recv");
511                 exit_cleanup(RERR_IPC);
512         }
513
514         io_flush(NORMAL_FLUSH);
515
516         if ((pid = do_fork()) == -1) {
517                 rsyserr(FERROR, errno, "fork failed in do_recv");
518                 exit_cleanup(RERR_IPC);
519         }
520
521         if (pid == 0) {
522                 close(error_pipe[0]);
523                 if (need_name_pipe) {
524                         close(name_pipe[1]);
525                         set_blocking(name_pipe[0]);
526                 } else
527                         name_pipe[0] = -1;
528                 if (f_in != f_out)
529                         close(f_out);
530
531                 /* we can't let two processes write to the socket at one time */
532                 close_multiplexing_out();
533
534                 /* set place to send errors */
535                 set_msg_fd_out(error_pipe[1]);
536
537                 recv_files(f_in, flist, local_name, name_pipe[0]);
538                 io_flush(FULL_FLUSH);
539                 report(f_in);
540
541                 send_msg(MSG_DONE, "", 0);
542                 io_flush(FULL_FLUSH);
543                 /* finally we go to sleep until our parent kills us
544                  * with a USR2 signal. We sleep for a short time as on
545                  * some OSes a signal won't interrupt a sleep! */
546                 while (1)
547                         msleep(20);
548         }
549
550         am_generator = 1;
551         close_multiplexing_in();
552         if (write_batch)
553                 stop_write_batch();
554
555         close(error_pipe[1]);
556         if (need_name_pipe) {
557                 close(name_pipe[0]);
558                 set_nonblocking(name_pipe[1]);
559         } else
560                 name_pipe[1] = -1;
561         if (f_in != f_out)
562                 close(f_in);
563
564         io_start_buffering_out();
565
566         set_msg_fd_in(error_pipe[0]);
567
568         generate_files(f_out, flist, local_name, name_pipe[1]);
569
570         report(-1);
571         io_flush(FULL_FLUSH);
572         if (protocol_version >= 24) {
573                 /* send a final goodbye message */
574                 write_int(f_out, -1);
575         }
576         io_flush(FULL_FLUSH);
577
578         set_msg_fd_in(-1);
579         kill(pid, SIGUSR2);
580         wait_process(pid, &status);
581         return status;
582 }
583
584
585 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
586 {
587         int status;
588         struct file_list *flist;
589         char *local_name = NULL;
590         char *dir = NULL;
591         int save_verbose = verbose;
592
593         if (filesfrom_fd >= 0) {
594                 /* We can't mix messages with files-from data on the socket,
595                  * so temporarily turn off verbose messages. */
596                 verbose = 0;
597         }
598
599         if (verbose > 2) {
600                 rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
601                         argc, (long)getpid());
602         }
603
604         if (am_daemon && lp_read_only(module_id)) {
605                 rprintf(FERROR,"ERROR: module is read only\n");
606                 exit_cleanup(RERR_SYNTAX);
607                 return;
608         }
609
610
611         if (argc > 0) {
612                 dir = argv[0];
613                 argc--;
614                 argv++;
615                 if (!am_daemon && !push_dir(dir)) {
616                         rsyserr(FERROR, errno, "push_dir#4 %s failed",
617                                 full_fname(dir));
618                         exit_cleanup(RERR_FILESELECT);
619                 }
620         }
621
622         io_start_buffering_in();
623         recv_filter_list(f_in);
624
625         if (filesfrom_fd >= 0) {
626                 /* We need to send the files-from names to the sender at the
627                  * same time that we receive the file-list from them, so we
628                  * need the IO routines to automatically write out the names
629                  * onto our f_out socket as we read the file-list.  This
630                  * avoids both deadlock and extra delays/buffers. */
631                 io_set_filesfrom_fds(filesfrom_fd, f_out);
632                 filesfrom_fd = -1;
633         }
634
635         flist = recv_file_list(f_in);
636         verbose = save_verbose;
637         if (!flist) {
638                 rprintf(FERROR,"server_recv: recv_file_list error\n");
639                 exit_cleanup(RERR_FILESELECT);
640         }
641
642         if (argc > 0) {
643                 if (strcmp(dir,".")) {
644                         argv[0] += strlen(dir);
645                         if (argv[0][0] == '/')
646                                 argv[0]++;
647                 }
648                 local_name = get_local_name(flist,argv[0]);
649         }
650
651         status = do_recv(f_in,f_out,flist,local_name);
652         exit_cleanup(status);
653 }
654
655
656 int child_main(int argc, char *argv[])
657 {
658         start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
659         return 0;
660 }
661
662
663 void start_server(int f_in, int f_out, int argc, char *argv[])
664 {
665         set_nonblocking(f_in);
666         set_nonblocking(f_out);
667
668         io_set_sock_fds(f_in, f_out);
669         setup_protocol(f_out, f_in);
670
671         if (protocol_version >= 23)
672                 io_start_multiplex_out();
673
674         if (am_sender) {
675                 keep_dirlinks = 0; /* Must be disabled on the sender. */
676
677                 recv_filter_list(f_in);
678                 do_server_sender(f_in, f_out, argc, argv);
679         } else {
680                 do_server_recv(f_in, f_out, argc, argv);
681         }
682         exit_cleanup(0);
683 }
684
685
686 /*
687  * This is called once the connection has been negotiated.  It is used
688  * for rsyncd, remote-shell, and local connections.
689  */
690 int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
691 {
692         struct file_list *flist = NULL;
693         int status = 0, status2 = 0;
694         char *local_name = NULL;
695
696         cleanup_child_pid = pid;
697         if (!read_batch) {
698                 set_nonblocking(f_in);
699                 set_nonblocking(f_out);
700         }
701
702         io_set_sock_fds(f_in, f_out);
703         setup_protocol(f_out,f_in);
704
705         if (protocol_version >= 23 && !read_batch)
706                 io_start_multiplex_in();
707
708         /* We set our stderr file handle to blocking because ssh might have
709          * set it to non-blocking.  This can be particularly troublesome if
710          * stderr is a clone of stdout, because ssh would have set our stdout
711          * to non-blocking at the same time (which can easily cause us to lose
712          * output from our print statements).  This kluge shouldn't cause ssh
713          * any problems for how we use it.  Note also that we delayed setting
714          * this until after the above protocol setup so that we know for sure
715          * that ssh is done twiddling its file descriptors.  */
716         set_blocking(STDERR_FILENO);
717
718         if (am_sender) {
719                 keep_dirlinks = 0; /* Must be disabled on the sender. */
720                 io_start_buffering_out();
721                 if (!remote_filesfrom_file)
722                         set_msg_fd_in(f_in);
723                 send_filter_list(f_out);
724                 if (remote_filesfrom_file)
725                         filesfrom_fd = f_in;
726
727                 if (write_batch)
728                         start_write_batch(f_out);
729                 if (!read_batch) /* don't write to pipe */
730                         flist = send_file_list(f_out,argc,argv);
731                 set_msg_fd_in(-1);
732                 if (verbose > 3)
733                         rprintf(FINFO,"file list sent\n");
734
735                 io_flush(NORMAL_FLUSH);
736                 send_files(flist,f_out,f_in);
737                 io_flush(FULL_FLUSH);
738                 if (protocol_version >= 24) {
739                         /* final goodbye message */
740                         read_int(f_in);
741                 }
742                 if (pid != -1) {
743                         if (verbose > 3)
744                                 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
745                         io_flush(FULL_FLUSH);
746                         wait_process(pid, &status);
747                 }
748                 report(-1);
749                 io_flush(FULL_FLUSH);
750                 exit_cleanup(status);
751         }
752
753         if (argc == 0)
754                 list_only |= 1;
755
756         send_filter_list(read_batch ? -1 : f_out);
757
758         if (filesfrom_fd >= 0) {
759                 io_set_filesfrom_fds(filesfrom_fd, f_out);
760                 filesfrom_fd = -1;
761         }
762
763         if (write_batch)
764                 start_write_batch(f_in);
765         flist = recv_file_list(f_in);
766         if (!flist || flist->count == 0) {
767                 rprintf(FINFO, "client: nothing to do: "
768                         "perhaps you need to specify some filenames or "
769                         "the --recursive option?\n");
770                 exit_cleanup(0);
771         }
772
773         local_name = get_local_name(flist,argv[0]);
774
775         status2 = do_recv(f_in,f_out,flist,local_name);
776
777         if (pid != -1) {
778                 if (verbose > 3)
779                         rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
780                 io_flush(FULL_FLUSH);
781                 wait_process(pid, &status);
782         }
783
784         return MAX(status, status2);
785 }
786
787 static int copy_argv (char *argv[])
788 {
789         int i;
790
791         for (i = 0; argv[i]; i++) {
792                 if (!(argv[i] = strdup(argv[i]))) {
793                         rprintf (FERROR, "out of memory at %s(%d)\n",
794                                  __FILE__, __LINE__);
795                         return RERR_MALLOC;
796                 }
797         }
798
799         return 0;
800 }
801
802
803 /**
804  * Start a client for either type of remote connection.  Work out
805  * whether the arguments request a remote shell or rsyncd connection,
806  * and call the appropriate connection function, then run_client.
807  *
808  * Calls either start_socket_client (for sockets) or do_cmd and
809  * client_run (for ssh).
810  **/
811 static int start_client(int argc, char *argv[])
812 {
813         char *p;
814         char *shell_machine = NULL;
815         char *shell_path = NULL;
816         char *shell_user = NULL;
817         int ret;
818         pid_t pid;
819         int f_in,f_out;
820         int rc;
821
822         /* Don't clobber argv[] so that ps(1) can still show the right
823          * command line. */
824         if ((rc = copy_argv(argv)))
825                 return rc;
826
827         /* rsync:// always uses rsync server over direct socket connection */
828         if (strncasecmp(URL_PREFIX, argv[0], strlen(URL_PREFIX)) == 0
829             && !read_batch) {
830                 char *host, *path;
831
832                 host = argv[0] + strlen(URL_PREFIX);
833                 p = strchr(host,'/');
834                 if (p) {
835                         *p = '\0';
836                         path = p+1;
837                 } else
838                         path = "";
839                 if (*host == '[' && (p = strchr(host, ']')) != NULL) {
840                         host++;
841                         *p++ = '\0';
842                         if (*p != ':')
843                                 p = NULL;
844                 } else
845                         p = strchr(host, ':');
846                 if (p) {
847                         rsync_port = atoi(p+1);
848                         *p = '\0';
849                 }
850                 return start_socket_client(host, path, argc-1, argv+1);
851         }
852
853         if (!read_batch) { /* for read_batch, NO source is specified */
854                 p = find_colon(argv[0]);
855                 if (p) { /* source is remote */
856                         if (remote_filesfrom_file
857                          && remote_filesfrom_file != files_from + 1
858                          && strncmp(files_from, argv[0], p-argv[0]+1) != 0) {
859                                 rprintf(FERROR,
860                                         "--files-from hostname is not the same as the transfer hostname\n");
861                                 exit_cleanup(RERR_SYNTAX);
862                         }
863                         if (p[1] == ':') { /* double colon */
864                                 *p = 0;
865                                 if (!shell_cmd) {
866                                         return start_socket_client(argv[0], p+2,
867                                                                    argc-1, argv+1);
868                                 }
869                                 p++;
870                                 daemon_over_rsh = 1;
871                         }
872
873                         if (argc < 1) { /* destination required */
874                                 usage(FERROR);
875                                 exit_cleanup(RERR_SYNTAX);
876                         }
877
878                         am_sender = 0;
879                         *p = 0;
880                         shell_machine = argv[0];
881                         shell_path = p+1;
882                         argv++;
883                 } else { /* source is local */
884                         am_sender = 1;
885
886                         /* rsync:// destination uses rsync server over direct socket */
887                         if (strncasecmp(URL_PREFIX, argv[argc-1], strlen(URL_PREFIX)) == 0) {
888                                 char *host, *path;
889
890                                 host = argv[argc-1] + strlen(URL_PREFIX);
891                                 p = strchr(host,'/');
892                                 if (p) {
893                                         *p = '\0';
894                                         path = p+1;
895                                 } else
896                                         path = "";
897                                 if (*host == '[' && (p = strchr(host, ']')) != NULL) {
898                                         host++;
899                                         *p++ = '\0';
900                                         if (*p != ':')
901                                                 p = NULL;
902                                 } else
903                                         p = strchr(host, ':');
904                                 if (p) {
905                                         rsync_port = atoi(p+1);
906                                         *p = '\0';
907                                 }
908                                 return start_socket_client(host, path, argc-1, argv);
909                         }
910
911                         p = find_colon(argv[argc-1]); /* look in dest arg */
912                         if (p && remote_filesfrom_file
913                          && remote_filesfrom_file != files_from + 1
914                          && strncmp(files_from, argv[argc-1], p-argv[argc-1]+1) != 0) {
915                                 rprintf(FERROR,
916                                         "--files-from hostname is not the same as the transfer hostname\n");
917                                 exit_cleanup(RERR_SYNTAX);
918                         }
919                         if (!p) { /* no colon found, so src & dest are local */
920                                 local_server = 1;
921                                 if (remote_filesfrom_file) {
922                                         rprintf(FERROR,
923                                                 "--files-from cannot be remote when the transfer is local\n");
924                                         exit_cleanup(RERR_SYNTAX);
925                                 }
926                         } else if (p[1] == ':') { /* double colon */
927                                 *p = 0;
928                                 if (!shell_cmd) {
929                                         return start_socket_client(argv[argc-1], p+2,
930                                                                    argc-1, argv);
931                                 }
932                                 p++;
933                                 daemon_over_rsh = 1;
934                         }
935
936                         if (argc < 2) {
937                                 usage(FERROR);
938                                 exit_cleanup(RERR_SYNTAX);
939                         }
940
941                         if (local_server) {
942                                 shell_machine = NULL;
943                                 shell_path = argv[argc-1];
944                         } else {
945                                 *p = 0;
946                                 shell_machine = argv[argc-1];
947                                 shell_path = p+1;
948                         }
949                 }
950                 argc--;
951         } else {  /* read_batch */
952                 local_server = 1;
953                 shell_path = argv[argc-1];
954                 if (find_colon(shell_path)) {
955                         rprintf(FERROR, "remote destination is not allowed with --read-batch\n");
956                         exit_cleanup(RERR_SYNTAX);
957                 }
958         }
959
960         if (shell_machine) {
961                 p = strrchr(shell_machine,'@');
962                 if (p) {
963                         *p = 0;
964                         shell_user = shell_machine;
965                         shell_machine = p+1;
966                 }
967         }
968
969         if (verbose > 3) {
970                 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
971                         shell_cmd ? safe_fname(shell_cmd) : "",
972                         shell_machine ? safe_fname(shell_machine) : "",
973                         shell_user ? safe_fname(shell_user) : "",
974                         shell_path ? safe_fname(shell_path) : "");
975         }
976
977         /* for remote source, only single dest arg can remain ... */
978         if (!am_sender && argc > 1) {
979                 usage(FERROR);
980                 exit_cleanup(RERR_SYNTAX);
981         }
982
983         /* ... or no dest at all */
984         if (!am_sender && argc == 0)
985                 list_only |= 1;
986
987         pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,
988                      &f_in,&f_out);
989
990         /* if we're running an rsync server on the remote host over a
991          * remote shell command, we need to do the RSYNCD protocol first */
992         if (daemon_over_rsh) {
993                 int tmpret;
994                 tmpret = start_inband_exchange(shell_user, shell_path,
995                                                f_in, f_out, argc);
996                 if (tmpret < 0)
997                         return tmpret;
998         }
999
1000         ret = client_run(f_in, f_out, pid, argc, argv);
1001
1002         fflush(stdout);
1003         fflush(stderr);
1004
1005         return ret;
1006 }
1007
1008
1009 static RETSIGTYPE sigusr1_handler(UNUSED(int val))
1010 {
1011         exit_cleanup(RERR_SIGNAL);
1012 }
1013
1014 static RETSIGTYPE sigusr2_handler(UNUSED(int val))
1015 {
1016         if (log_got_error) _exit(RERR_PARTIAL);
1017         _exit(0);
1018 }
1019
1020 static RETSIGTYPE sigchld_handler(UNUSED(int val))
1021 {
1022 #ifdef WNOHANG
1023         int cnt, status;
1024         pid_t pid;
1025         /* An empty waitpid() loop was put here by Tridge and we could never
1026          * get him to explain why he put it in, so rather than taking it
1027          * out we're instead saving the child exit statuses for later use.
1028          * The waitpid() loop presumably eliminates all possibility of leaving
1029          * zombie children, maybe that's why he did it.
1030          */
1031         while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1032                 /* save the child's exit status */
1033                 for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) {
1034                         if (pid_stat_table[cnt].pid == 0) {
1035                                 pid_stat_table[cnt].pid = pid;
1036                                 pid_stat_table[cnt].status = status;
1037                                 break;
1038                         }
1039                 }
1040         }
1041 #endif
1042 }
1043
1044
1045 /**
1046  * This routine catches signals and tries to send them to gdb.
1047  *
1048  * Because it's called from inside a signal handler it ought not to
1049  * use too many library routines.
1050  *
1051  * @todo Perhaps use "screen -X" instead/as well, to help people
1052  * debugging without easy access to X.  Perhaps use an environment
1053  * variable, or just call a script?
1054  *
1055  * @todo The /proc/ magic probably only works on Linux (and
1056  * Solaris?)  Can we be more portable?
1057  **/
1058 #ifdef MAINTAINER_MODE
1059 const char *get_panic_action(void)
1060 {
1061         const char *cmd_fmt = getenv("RSYNC_PANIC_ACTION");
1062
1063         if (cmd_fmt)
1064                 return cmd_fmt;
1065         else
1066                 return "xterm -display :0 -T Panic -n Panic "
1067                         "-e gdb /proc/%d/exe %d";
1068 }
1069
1070
1071 /**
1072  * Handle a fatal signal by launching a debugger, controlled by $RSYNC_PANIC_ACTION.
1073  *
1074  * This signal handler is only installed if we were configured with
1075  * --enable-maintainer-mode.  Perhaps it should always be on and we
1076  * should just look at the environment variable, but I'm a bit leery
1077  * of a signal sending us into a busy loop.
1078  **/
1079 static RETSIGTYPE rsync_panic_handler(UNUSED(int whatsig))
1080 {
1081         char cmd_buf[300];
1082         int ret;
1083
1084         sprintf(cmd_buf, get_panic_action(),
1085                 getpid(), getpid());
1086
1087         /* Unless we failed to execute gdb, we allow the process to
1088          * continue.  I'm not sure if that's right. */
1089         ret = system(cmd_buf);
1090         if (ret)
1091                 _exit(ret);
1092 }
1093 #endif
1094
1095
1096 int main(int argc,char *argv[])
1097 {
1098         int ret;
1099         int orig_argc = argc;
1100         char **orig_argv = argv;
1101
1102         signal(SIGUSR1, sigusr1_handler);
1103         signal(SIGUSR2, sigusr2_handler);
1104         signal(SIGCHLD, sigchld_handler);
1105 #ifdef MAINTAINER_MODE
1106         signal(SIGSEGV, rsync_panic_handler);
1107         signal(SIGFPE, rsync_panic_handler);
1108         signal(SIGABRT, rsync_panic_handler);
1109         signal(SIGBUS, rsync_panic_handler);
1110 #endif /* def MAINTAINER_MODE */
1111
1112         starttime = time(NULL);
1113         am_root = (MY_UID() == 0);
1114
1115         memset(&stats, 0, sizeof(stats));
1116
1117         if (argc < 2) {
1118                 usage(FERROR);
1119                 exit_cleanup(RERR_SYNTAX);
1120         }
1121
1122         /* we set a 0 umask so that correct file permissions can be
1123          * carried across */
1124         orig_umask = (int)umask(0);
1125
1126         if (!parse_arguments(&argc, (const char ***) &argv, 1)) {
1127                 /* FIXME: We ought to call the same error-handling
1128                  * code here, rather than relying on getopt. */
1129                 option_error();
1130                 exit_cleanup(RERR_SYNTAX);
1131         }
1132
1133         signal(SIGINT,SIGNAL_CAST sig_int);
1134         signal(SIGHUP,SIGNAL_CAST sig_int);
1135         signal(SIGTERM,SIGNAL_CAST sig_int);
1136
1137         /* Ignore SIGPIPE; we consistently check error codes and will
1138          * see the EPIPE. */
1139         signal(SIGPIPE, SIG_IGN);
1140
1141         /* Initialize push_dir here because on some old systems getcwd
1142          * (implemented by forking "pwd" and reading its output) doesn't
1143          * work when there are other child processes.  Also, on all systems
1144          * that implement getcwd that way "pwd" can't be found after chroot. */
1145         push_dir(NULL);
1146
1147         init_flist();
1148
1149         if (write_batch || read_batch) {
1150                 if (write_batch)
1151                         write_batch_shell_file(orig_argc, orig_argv, argc);
1152
1153                 if (read_batch && strcmp(batch_name, "-") == 0)
1154                         batch_fd = STDIN_FILENO;
1155                 else {
1156                         batch_fd = do_open(batch_name,
1157                                    write_batch ? O_WRONLY | O_CREAT | O_TRUNC
1158                                    : O_RDONLY, S_IRUSR | S_IWUSR);
1159                 }
1160                 if (batch_fd < 0) {
1161                         rsyserr(FERROR, errno, "Batch file %s open error",
1162                                 full_fname(batch_name));
1163                         exit_cleanup(RERR_FILEIO);
1164                 }
1165                 if (read_batch)
1166                         read_stream_flags(batch_fd);
1167         }
1168
1169         if (am_daemon && !am_server)
1170                 return daemon_main();
1171
1172         if (argc < 1) {
1173                 usage(FERROR);
1174                 exit_cleanup(RERR_SYNTAX);
1175         }
1176
1177         if (dry_run && !verbose && !itemize_changes)
1178                 verbose = 1;
1179
1180         if (am_server) {
1181                 set_nonblocking(STDIN_FILENO);
1182                 set_nonblocking(STDOUT_FILENO);
1183                 if (am_daemon)
1184                         return start_daemon(STDIN_FILENO, STDOUT_FILENO);
1185                 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
1186         }
1187
1188         ret = start_client(argc, argv);
1189         if (ret == -1)
1190                 exit_cleanup(RERR_STARTCLIENT);
1191         else
1192                 exit_cleanup(ret);
1193
1194         return ret;
1195 }