1 /* -*- c-file-style: "linux" -*-
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>
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.
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.
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.
30 extern int am_generator;
32 extern int blocking_io;
33 extern int remove_sent_files;
34 extern int daemon_over_rsh;
35 extern int need_messages_from_generator;
36 extern int kluge_around_eof;
38 extern int log_got_error;
40 extern int orig_umask;
41 extern int copy_links;
42 extern int keep_dirlinks;
43 extern int preserve_hard_links;
44 extern int protocol_version;
46 extern int fuzzy_basis;
47 extern int relative_paths;
48 extern int rsync_port;
50 extern int make_backups;
51 extern int whole_file;
52 extern int read_batch;
53 extern int write_batch;
55 extern int batch_gen_fd;
56 extern int filesfrom_fd;
57 extern pid_t cleanup_child_pid;
58 extern struct stats stats;
59 extern char *filesfrom_host;
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;
67 struct file_list *the_file_list;
69 /* There's probably never more than at most 2 outstanding child processes,
70 * but set it higher, just in case. */
71 #define MAXCHILDPROCS 5
76 } pid_stat_table[MAXCHILDPROCS];
78 static time_t starttime, endtime;
79 static int64 total_read, total_written;
81 static void show_malloc_stats(void);
83 /****************************************************************************
84 wait for a process to exit, calling io_flush while waiting
85 ****************************************************************************/
86 void wait_process(pid_t pid, int *status)
91 while ((waited_pid = waitpid(pid, status, WNOHANG)) == 0) {
96 if (waited_pid == -1 && errno == ECHILD) {
97 /* status of requested child no longer available.
98 * check to see if it was processed by the sigchld_handler.
100 for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) {
101 if (pid == pid_stat_table[cnt].pid) {
102 *status = pid_stat_table[cnt].status;
103 pid_stat_table[cnt].pid = 0;
109 /* TODO: If the child exited on a signal, then log an
110 * appropriate error message. Perhaps we should also accept a
111 * message describing the purpose of the child. Also indicate
112 * this to the caller so that thhey know something went
114 *status = WEXITSTATUS(*status);
117 /* This function gets called from all 3 processes. We want the client side
118 * to actually output the text, but the sender is the only process that has
119 * all the stats we need. So, if we're a client sender, we do the report.
120 * If we're a server sender, we write the stats on the supplied fd. If
121 * we're the client receiver we read the stats from the supplied fd and do
122 * the report. All processes might also generate a set of debug stats, if
123 * the verbose level is high enough (this is the only thing that the
124 * generator process and the server receiver ever do here). */
125 static void handle_stats(int f)
127 endtime = time(NULL);
129 /* Cache two stats because the read/write code can change it. */
130 total_read = stats.total_read;
131 total_written = stats.total_written;
133 if (do_stats && verbose > 1) {
134 /* These come out from every process */
143 log_exit(0, __FILE__, __LINE__);
144 if (f == -1 || !am_sender)
150 write_longint(f, total_read);
151 write_longint(f, total_written);
152 write_longint(f, stats.total_size);
153 if (protocol_version >= 29) {
154 write_longint(f, stats.flist_buildtime);
155 write_longint(f, stats.flist_xfertime);
161 /* this is the client */
164 /* Read the first two in opposite order because the meaning of
165 * read/write swaps when switching from sender to receiver. */
166 total_written = read_longint(f);
167 total_read = read_longint(f);
168 stats.total_size = read_longint(f);
169 if (protocol_version >= 29) {
170 stats.flist_buildtime = read_longint(f);
171 stats.flist_xfertime = read_longint(f);
173 } else if (write_batch) {
174 /* The --read-batch process is going to be a client
175 * receiver, so we need to give it the stats. */
176 write_longint(batch_fd, total_read);
177 write_longint(batch_fd, total_written);
178 write_longint(batch_fd, stats.total_size);
179 if (protocol_version >= 29) {
180 write_longint(batch_fd, stats.flist_buildtime);
181 write_longint(batch_fd, stats.flist_xfertime);
186 rprintf(FINFO,"\nNumber of files: %d\n", stats.num_files);
187 rprintf(FINFO,"Number of files transferred: %d\n",
188 stats.num_transferred_files);
189 rprintf(FINFO,"Total file size: %.0f bytes\n",
190 (double)stats.total_size);
191 rprintf(FINFO,"Total transferred file size: %.0f bytes\n",
192 (double)stats.total_transferred_size);
193 rprintf(FINFO,"Literal data: %.0f bytes\n",
194 (double)stats.literal_data);
195 rprintf(FINFO,"Matched data: %.0f bytes\n",
196 (double)stats.matched_data);
197 rprintf(FINFO,"File list size: %d\n", stats.flist_size);
198 if (stats.flist_buildtime) {
200 "File list generation time: %.3f seconds\n",
201 (double)stats.flist_buildtime / 1000);
203 "File list transfer time: %.3f seconds\n",
204 (double)stats.flist_xfertime / 1000);
206 rprintf(FINFO,"Total bytes sent: %.0f\n",
207 (double)total_written);
208 rprintf(FINFO,"Total bytes received: %.0f\n",
216 static void output_summary(void)
218 if (verbose || do_stats) {
220 "\nsent %.0f bytes received %.0f bytes %.2f bytes/sec\n",
221 (double)total_written, (double)total_read,
222 (total_written + total_read)/(0.5 + (endtime - starttime)));
223 rprintf(FINFO, "total size is %.0f speedup is %.2f\n",
224 (double)stats.total_size,
225 (double)stats.total_size / (total_written+total_read));
234 * If our C library can get malloc statistics, then show them to FINFO
236 static void show_malloc_stats(void)
243 rprintf(FINFO, "\n" RSYNC_NAME "[%d] (%s%s%s) heap statistics:\n",
244 getpid(), am_server ? "server " : "",
245 am_daemon ? "daemon " : "", who_am_i());
246 rprintf(FINFO, " arena: %10ld (bytes from sbrk)\n",
248 rprintf(FINFO, " ordblks: %10ld (chunks not in use)\n",
250 rprintf(FINFO, " smblks: %10ld\n",
252 rprintf(FINFO, " hblks: %10ld (chunks from mmap)\n",
254 rprintf(FINFO, " hblkhd: %10ld (bytes from mmap)\n",
256 rprintf(FINFO, " allmem: %10ld (bytes from sbrk + mmap)\n",
257 (long)mi.arena + mi.hblkhd);
258 rprintf(FINFO, " usmblks: %10ld\n",
260 rprintf(FINFO, " fsmblks: %10ld\n",
262 rprintf(FINFO, " uordblks: %10ld (bytes used)\n",
264 rprintf(FINFO, " fordblks: %10ld (bytes free)\n",
266 rprintf(FINFO, " keepcost: %10ld (bytes in releasable chunk)\n",
268 #endif /* HAVE_MALLINFO */
272 /* Start the remote shell. cmd may be NULL to use the default. */
273 static pid_t do_cmd(char *cmd, char *machine, char *user, char *path,
274 int *f_in, int *f_out)
277 char *args[MAX_ARGS];
279 char *tok, *dir = NULL;
282 if (!read_batch && !local_server) {
283 char *rsh_env = getenv(RSYNC_RSH_ENV);
292 for (tok = strtok(cmd, " "); tok; tok = strtok(NULL, " ")) {
293 /* Comparison leaves rooms for server_options(). */
294 if (argc >= MAX_ARGS - 100) {
295 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
296 exit_cleanup(RERR_SYNTAX);
301 /* check to see if we've already been given '-l user' in
302 * the remote-shell command */
303 for (i = 0; i < argc-1; i++) {
304 if (!strcmp(args[i], "-l") && args[i+1][0] != '-')
309 /* remsh (on HPUX) takes the arguments the other way around */
310 args[argc++] = machine;
311 if (user && !(daemon_over_rsh && dash_l_set)) {
316 if (user && !(daemon_over_rsh && dash_l_set)) {
320 args[argc++] = machine;
323 args[argc++] = rsync_path;
325 if (blocking_io < 0) {
327 if ((cp = strrchr(cmd, '/')) != NULL)
331 if (strcmp(cp, "rsh") == 0 || strcmp(cp, "remsh") == 0)
335 server_options(args,&argc);
337 if (argc >= MAX_ARGS - 2) {
338 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
339 exit_cleanup(RERR_SYNTAX);
345 if (!daemon_over_rsh && path && *path)
351 rprintf(FINFO,"cmd=");
352 for (i = 0; i < argc; i++)
353 rprintf(FINFO, "%s ", safe_fname(args[i]));
358 int from_gen_pipe[2];
359 if (fd_pair(from_gen_pipe) < 0) {
360 rsyserr(FERROR, errno, "pipe");
361 exit_cleanup(RERR_IPC);
363 batch_gen_fd = from_gen_pipe[0];
364 *f_out = from_gen_pipe[1];
366 ret = -1; /* no child pid */
367 } else if (local_server) {
368 /* If the user didn't request --[no-]whole-file, force
369 * it on, but only if we're not batch processing. */
370 if (whole_file < 0 && !write_batch)
372 ret = local_child(argc, args, f_in, f_out, child_main);
374 ret = piped_child(args,f_in,f_out);
382 out_of_memory("do_cmd");
383 return 0; /* not reached */
387 static char *get_local_name(struct file_list *flist,char *name)
393 rprintf(FINFO,"get_local_name count=%d %s\n",
394 flist->count, NS(name));
399 if (do_stat(name,&st) == 0) {
400 if (S_ISDIR(st.st_mode)) {
401 if (!push_dir(name)) {
402 rsyserr(FERROR, errno, "push_dir#1 %s failed",
404 exit_cleanup(RERR_FILESELECT);
408 if (flist->count > 1) {
409 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
410 exit_cleanup(RERR_FILESELECT);
415 if (flist->count <= 1 && ((e = strlen(name)) <= 1 || name[e-1] != '/'))
418 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
419 rsyserr(FERROR, errno, "mkdir %s failed", full_fname(name));
420 exit_cleanup(RERR_FILEIO);
423 rprintf(FINFO, "created directory %s\n", safe_fname(name));
430 if (!push_dir(name)) {
431 rsyserr(FERROR, errno, "push_dir#2 %s failed",
433 exit_cleanup(RERR_FILESELECT);
440 /* This is only called by the sender. */
441 static void read_final_goodbye(int f_in, int f_out)
445 if (protocol_version < 29)
448 while ((i = read_int(f_in)) == the_file_list->count
449 && read_shortint(f_in) == ITEM_IS_NEW) {
450 /* Forward the keep-alive (no-op) to the receiver. */
451 write_int(f_out, the_file_list->count);
452 write_shortint(f_out, ITEM_IS_NEW);
457 rprintf(FERROR, "Invalid packet at end of run (%d) [%s]\n",
459 exit_cleanup(RERR_PROTOCOL);
464 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
467 struct file_list *flist;
471 rprintf(FINFO, "server_sender starting pid=%ld\n",
475 if (am_daemon && lp_write_only(module_id)) {
476 rprintf(FERROR, "ERROR: module is write only\n");
477 exit_cleanup(RERR_SYNTAX);
480 if (am_daemon && lp_read_only(module_id) && remove_sent_files) {
482 "ERROR: --remove-sent-files cannot be used with a read-only module\n");
483 exit_cleanup(RERR_SYNTAX);
487 if (!relative_paths && !push_dir(dir)) {
488 rsyserr(FERROR, errno, "push_dir#3 %s failed",
490 exit_cleanup(RERR_FILESELECT);
495 if (strcmp(dir,".")) {
497 if (strcmp(dir,"/") == 0)
499 for (i = 0; i < argc; i++)
503 if (argc == 0 && (recurse || list_only)) {
509 flist = send_file_list(f_out,argc,argv);
510 if (!flist || flist->count == 0) {
513 the_file_list = flist;
515 io_start_buffering_in();
516 io_start_buffering_out();
518 send_files(flist,f_out,f_in);
519 io_flush(FULL_FLUSH);
521 if (protocol_version >= 24)
522 read_final_goodbye(f_in, f_out);
523 io_flush(FULL_FLUSH);
528 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
534 /* The receiving side mustn't obey this, or an existing symlink that
535 * points to an identical file won't be replaced by the referent. */
538 if (preserve_hard_links)
541 if (fd_pair(error_pipe) < 0) {
542 rsyserr(FERROR, errno, "pipe failed in do_recv");
543 exit_cleanup(RERR_IPC);
546 io_flush(NORMAL_FLUSH);
548 if ((pid = do_fork()) == -1) {
549 rsyserr(FERROR, errno, "fork failed in do_recv");
550 exit_cleanup(RERR_IPC);
554 close(error_pipe[0]);
558 /* we can't let two processes write to the socket at one time */
559 close_multiplexing_out();
561 /* set place to send errors */
562 set_msg_fd_out(error_pipe[1]);
564 recv_files(f_in, flist, local_name);
565 io_flush(FULL_FLUSH);
568 send_msg(MSG_DONE, "", 0);
569 io_flush(FULL_FLUSH);
571 /* Handle any keep-alive packets from the post-processing work
572 * that the generator does. */
573 if (protocol_version >= 29) {
574 kluge_around_eof = -1;
576 /* This should only get stopped via a USR2 signal. */
577 while (read_int(f_in) == flist->count
578 && read_shortint(f_in) == ITEM_IS_NEW) {}
580 rprintf(FERROR, "Invalid packet at end of run [%s]\n",
582 exit_cleanup(RERR_PROTOCOL);
585 /* Finally, we go to sleep until our parent kills us with a
586 * USR2 signal. We sleep for a short time, as on some OSes
587 * a signal won't interrupt a sleep! */
593 close_multiplexing_in();
597 close(error_pipe[1]);
601 io_start_buffering_out();
603 set_msg_fd_in(error_pipe[0]);
605 generate_files(f_out, flist, local_name);
608 io_flush(FULL_FLUSH);
609 if (protocol_version >= 24) {
610 /* send a final goodbye message */
611 write_int(f_out, -1);
613 io_flush(FULL_FLUSH);
617 wait_process(pid, &status);
622 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
625 struct file_list *flist;
626 char *local_name = NULL;
628 int save_verbose = verbose;
630 if (filesfrom_fd >= 0) {
631 /* We can't mix messages with files-from data on the socket,
632 * so temporarily turn off verbose messages. */
637 rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
638 argc, (long)getpid());
641 if (am_daemon && lp_read_only(module_id)) {
642 rprintf(FERROR,"ERROR: module is read only\n");
643 exit_cleanup(RERR_SYNTAX);
652 if (!am_daemon && !push_dir(dir)) {
653 rsyserr(FERROR, errno, "push_dir#4 %s failed",
655 exit_cleanup(RERR_FILESELECT);
659 io_start_buffering_in();
660 recv_filter_list(f_in);
662 if (filesfrom_fd >= 0) {
663 /* We need to send the files-from names to the sender at the
664 * same time that we receive the file-list from them, so we
665 * need the IO routines to automatically write out the names
666 * onto our f_out socket as we read the file-list. This
667 * avoids both deadlock and extra delays/buffers. */
668 io_set_filesfrom_fds(filesfrom_fd, f_out);
672 flist = recv_file_list(f_in);
673 verbose = save_verbose;
675 rprintf(FERROR,"server_recv: recv_file_list error\n");
676 exit_cleanup(RERR_FILESELECT);
678 the_file_list = flist;
681 if (strcmp(dir,".")) {
682 argv[0] += strlen(dir);
683 if (argv[0][0] == '/')
686 local_name = get_local_name(flist,argv[0]);
689 status = do_recv(f_in,f_out,flist,local_name);
690 exit_cleanup(status);
694 int child_main(int argc, char *argv[])
696 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
701 void start_server(int f_in, int f_out, int argc, char *argv[])
703 set_nonblocking(f_in);
704 set_nonblocking(f_out);
706 io_set_sock_fds(f_in, f_out);
707 setup_protocol(f_out, f_in);
709 if (protocol_version >= 23)
710 io_start_multiplex_out();
713 keep_dirlinks = 0; /* Must be disabled on the sender. */
714 if (need_messages_from_generator)
715 io_start_multiplex_in();
717 recv_filter_list(f_in);
718 do_server_sender(f_in, f_out, argc, argv);
720 do_server_recv(f_in, f_out, argc, argv);
727 * This is called once the connection has been negotiated. It is used
728 * for rsyncd, remote-shell, and local connections.
730 int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
732 struct file_list *flist = NULL;
733 int status = 0, status2 = 0;
734 char *local_name = NULL;
736 cleanup_child_pid = pid;
738 set_nonblocking(f_in);
739 set_nonblocking(f_out);
742 io_set_sock_fds(f_in, f_out);
743 setup_protocol(f_out,f_in);
745 if (protocol_version >= 23 && !read_batch)
746 io_start_multiplex_in();
748 /* We set our stderr file handle to blocking because ssh might have
749 * set it to non-blocking. This can be particularly troublesome if
750 * stderr is a clone of stdout, because ssh would have set our stdout
751 * to non-blocking at the same time (which can easily cause us to lose
752 * output from our print statements). This kluge shouldn't cause ssh
753 * any problems for how we use it. Note also that we delayed setting
754 * this until after the above protocol setup so that we know for sure
755 * that ssh is done twiddling its file descriptors. */
756 set_blocking(STDERR_FILENO);
759 keep_dirlinks = 0; /* Must be disabled on the sender. */
760 io_start_buffering_out();
763 send_filter_list(f_out);
768 start_write_batch(f_out);
769 flist = send_file_list(f_out, argc, argv);
772 rprintf(FINFO,"file list sent\n");
773 the_file_list = flist;
775 io_flush(NORMAL_FLUSH);
776 send_files(flist,f_out,f_in);
777 io_flush(FULL_FLUSH);
779 if (protocol_version >= 24)
780 read_final_goodbye(f_in, f_out);
783 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
784 io_flush(FULL_FLUSH);
785 wait_process(pid, &status);
788 io_flush(FULL_FLUSH);
789 exit_cleanup(status);
792 if (need_messages_from_generator && !read_batch)
793 io_start_multiplex_out();
798 send_filter_list(read_batch ? -1 : f_out);
800 if (filesfrom_fd >= 0) {
801 io_set_filesfrom_fds(filesfrom_fd, f_out);
806 start_write_batch(f_in);
807 flist = recv_file_list(f_in);
808 if (!flist || flist->count == 0) {
809 rprintf(FINFO, "client: nothing to do: "
810 "perhaps you need to specify some filenames or "
811 "the --recursive option?\n");
814 the_file_list = flist;
816 local_name = get_local_name(flist,argv[0]);
818 status2 = do_recv(f_in,f_out,flist,local_name);
822 rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
823 io_flush(FULL_FLUSH);
824 wait_process(pid, &status);
827 return MAX(status, status2);
830 static int copy_argv (char *argv[])
834 for (i = 0; argv[i]; i++) {
835 if (!(argv[i] = strdup(argv[i]))) {
836 rprintf (FERROR, "out of memory at %s(%d)\n",
847 * Start a client for either type of remote connection. Work out
848 * whether the arguments request a remote shell or rsyncd connection,
849 * and call the appropriate connection function, then run_client.
851 * Calls either start_socket_client (for sockets) or do_cmd and
852 * client_run (for ssh).
854 static int start_client(int argc, char *argv[])
857 char *shell_machine = NULL;
858 char *shell_path = NULL;
859 char *shell_user = NULL;
865 /* Don't clobber argv[] so that ps(1) can still show the right
867 if ((rc = copy_argv(argv)))
870 if (!read_batch) { /* for read_batch, NO source is specified */
872 shell_path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
873 if (shell_path) { /* source is remote */
875 if (filesfrom_host && *filesfrom_host
876 && strcmp(filesfrom_host, shell_machine) != 0) {
878 "--files-from hostname is not the same as the transfer hostname\n");
879 exit_cleanup(RERR_SYNTAX);
883 return start_socket_client(shell_machine,
891 } else { /* source is local, check dest arg */
894 if (argc < 1) { /* destination required */
896 exit_cleanup(RERR_SYNTAX);
899 shell_path = check_for_hostspec(argv[argc], &shell_machine, &rsync_port);
900 if (shell_path && filesfrom_host && *filesfrom_host
901 && strcmp(filesfrom_host, shell_machine) != 0) {
903 "--files-from hostname is not the same as the transfer hostname\n");
904 exit_cleanup(RERR_SYNTAX);
906 if (!shell_path) { /* no hostspec found, so src & dest are local */
908 if (filesfrom_host) {
910 "--files-from cannot be remote when the transfer is local\n");
911 exit_cleanup(RERR_SYNTAX);
913 shell_machine = NULL;
914 shell_path = argv[argc];
915 } else if (rsync_port) {
917 return start_socket_client(shell_machine,
924 } else { /* read_batch */
926 shell_path = argv[argc-1];
927 if (check_for_hostspec(shell_path, &shell_machine, &rsync_port)) {
928 rprintf(FERROR, "remote destination is not allowed with --read-batch\n");
929 exit_cleanup(RERR_SYNTAX);
934 p = strrchr(shell_machine,'@');
937 shell_user = shell_machine;
943 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
944 shell_cmd ? safe_fname(shell_cmd) : "",
945 shell_machine ? safe_fname(shell_machine) : "",
946 shell_user ? safe_fname(shell_user) : "",
947 shell_path ? safe_fname(shell_path) : "");
950 /* for remote source, only single dest arg can remain ... */
951 if (!am_sender && argc > 1) {
953 exit_cleanup(RERR_SYNTAX);
956 /* ... or no dest at all */
957 if (!am_sender && argc == 0)
960 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,
963 /* if we're running an rsync server on the remote host over a
964 * remote shell command, we need to do the RSYNCD protocol first */
965 if (daemon_over_rsh) {
967 tmpret = start_inband_exchange(shell_user, shell_path,
973 ret = client_run(f_in, f_out, pid, argc, argv);
982 static RETSIGTYPE sigusr1_handler(UNUSED(int val))
984 exit_cleanup(RERR_SIGNAL);
987 static RETSIGTYPE sigusr2_handler(UNUSED(int val))
997 static RETSIGTYPE sigchld_handler(UNUSED(int val))
1002 /* An empty waitpid() loop was put here by Tridge and we could never
1003 * get him to explain why he put it in, so rather than taking it
1004 * out we're instead saving the child exit statuses for later use.
1005 * The waitpid() loop presumably eliminates all possibility of leaving
1006 * zombie children, maybe that's why he did it.
1008 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
1009 /* save the child's exit status */
1010 for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) {
1011 if (pid_stat_table[cnt].pid == 0) {
1012 pid_stat_table[cnt].pid = pid;
1013 pid_stat_table[cnt].status = status;
1023 * This routine catches signals and tries to send them to gdb.
1025 * Because it's called from inside a signal handler it ought not to
1026 * use too many library routines.
1028 * @todo Perhaps use "screen -X" instead/as well, to help people
1029 * debugging without easy access to X. Perhaps use an environment
1030 * variable, or just call a script?
1032 * @todo The /proc/ magic probably only works on Linux (and
1033 * Solaris?) Can we be more portable?
1035 #ifdef MAINTAINER_MODE
1036 const char *get_panic_action(void)
1038 const char *cmd_fmt = getenv("RSYNC_PANIC_ACTION");
1043 return "xterm -display :0 -T Panic -n Panic "
1044 "-e gdb /proc/%d/exe %d";
1049 * Handle a fatal signal by launching a debugger, controlled by $RSYNC_PANIC_ACTION.
1051 * This signal handler is only installed if we were configured with
1052 * --enable-maintainer-mode. Perhaps it should always be on and we
1053 * should just look at the environment variable, but I'm a bit leery
1054 * of a signal sending us into a busy loop.
1056 static RETSIGTYPE rsync_panic_handler(UNUSED(int whatsig))
1061 sprintf(cmd_buf, get_panic_action(),
1062 getpid(), getpid());
1064 /* Unless we failed to execute gdb, we allow the process to
1065 * continue. I'm not sure if that's right. */
1066 ret = system(cmd_buf);
1073 int main(int argc,char *argv[])
1076 int orig_argc = argc;
1077 char **orig_argv = argv;
1079 signal(SIGUSR1, sigusr1_handler);
1080 signal(SIGUSR2, sigusr2_handler);
1081 signal(SIGCHLD, sigchld_handler);
1082 #ifdef MAINTAINER_MODE
1083 signal(SIGSEGV, rsync_panic_handler);
1084 signal(SIGFPE, rsync_panic_handler);
1085 signal(SIGABRT, rsync_panic_handler);
1086 signal(SIGBUS, rsync_panic_handler);
1087 #endif /* def MAINTAINER_MODE */
1089 starttime = time(NULL);
1090 am_root = (MY_UID() == 0);
1092 memset(&stats, 0, sizeof(stats));
1096 exit_cleanup(RERR_SYNTAX);
1099 /* we set a 0 umask so that correct file permissions can be
1101 orig_umask = (int)umask(0);
1103 if (!parse_arguments(&argc, (const char ***) &argv, 1)) {
1104 /* FIXME: We ought to call the same error-handling
1105 * code here, rather than relying on getopt. */
1107 exit_cleanup(RERR_SYNTAX);
1110 signal(SIGINT,SIGNAL_CAST sig_int);
1111 signal(SIGHUP,SIGNAL_CAST sig_int);
1112 signal(SIGTERM,SIGNAL_CAST sig_int);
1114 /* Ignore SIGPIPE; we consistently check error codes and will
1116 signal(SIGPIPE, SIG_IGN);
1118 /* Initialize push_dir here because on some old systems getcwd
1119 * (implemented by forking "pwd" and reading its output) doesn't
1120 * work when there are other child processes. Also, on all systems
1121 * that implement getcwd that way "pwd" can't be found after chroot. */
1126 if (write_batch || read_batch) {
1128 write_batch_shell_file(orig_argc, orig_argv, argc);
1130 if (read_batch && strcmp(batch_name, "-") == 0)
1131 batch_fd = STDIN_FILENO;
1133 batch_fd = do_open(batch_name,
1134 write_batch ? O_WRONLY | O_CREAT | O_TRUNC
1135 : O_RDONLY, S_IRUSR | S_IWUSR);
1138 rsyserr(FERROR, errno, "Batch file %s open error",
1139 full_fname(batch_name));
1140 exit_cleanup(RERR_FILEIO);
1143 read_stream_flags(batch_fd);
1146 if (am_daemon && !am_server)
1147 return daemon_main();
1151 exit_cleanup(RERR_SYNTAX);
1155 set_nonblocking(STDIN_FILENO);
1156 set_nonblocking(STDOUT_FILENO);
1158 return start_daemon(STDIN_FILENO, STDOUT_FILENO);
1159 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
1162 ret = start_client(argc, argv);
1164 exit_cleanup(RERR_STARTCLIENT);