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.
26 extern struct stats stats;
30 extern int am_generator;
33 extern int what_has_changed;
34 extern int blocking_io;
35 extern int delete_before;
36 extern int daemon_over_rsh;
40 extern int log_got_error;
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;
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;
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;
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
75 } pid_stat_table[MAXCHILDPROCS];
77 static void show_malloc_stats(void);
79 /****************************************************************************
80 wait for a process to exit, calling io_flush while waiting
81 ****************************************************************************/
82 void wait_process(pid_t pid, int *status)
87 while ((waited_pid = waitpid(pid, status, WNOHANG)) == 0) {
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.
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;
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
110 *status = WEXITSTATUS(*status);
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)
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);
128 if (do_stats && verbose > 1) {
129 /* These come out from every process */
138 log_exit(0, __FILE__, __LINE__);
139 if (f == -1 || !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);
156 /* this is the client */
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);
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);
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) {
195 "File list generation time: %.3f seconds\n",
196 (double)stats.flist_buildtime / 1000);
198 "File list transfer time: %.3f seconds\n",
199 (double)stats.flist_xfertime / 1000);
201 rprintf(FINFO,"Total bytes sent: %.0f\n",
202 (double)total_written);
203 rprintf(FINFO,"Total bytes received: %.0f\n",
207 if (verbose || do_stats) {
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));
223 * If our C library can get malloc statistics, then show them to FINFO
225 static void show_malloc_stats(void)
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",
237 rprintf(FINFO, " ordblks: %10ld (chunks not in use)\n",
239 rprintf(FINFO, " smblks: %10ld\n",
241 rprintf(FINFO, " hblks: %10ld (chunks from mmap)\n",
243 rprintf(FINFO, " hblkhd: %10ld (bytes from mmap)\n",
245 rprintf(FINFO, " allmem: %10ld (bytes from sbrk + mmap)\n",
246 (long)mi.arena + mi.hblkhd);
247 rprintf(FINFO, " usmblks: %10ld\n",
249 rprintf(FINFO, " fsmblks: %10ld\n",
251 rprintf(FINFO, " uordblks: %10ld (bytes used)\n",
253 rprintf(FINFO, " fordblks: %10ld (bytes free)\n",
255 rprintf(FINFO, " keepcost: %10ld (bytes in releasable chunk)\n",
257 #endif /* HAVE_MALLINFO */
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)
266 char *args[MAX_ARGS];
268 char *tok, *dir = NULL;
271 if (!read_batch && !local_server) {
272 char *rsh_env = getenv(RSYNC_RSH_ENV);
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);
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] != '-')
298 /* remsh (on HPUX) takes the arguments the other way around */
299 args[argc++] = machine;
300 if (user && !(daemon_over_rsh && dash_l_set)) {
305 if (user && !(daemon_over_rsh && dash_l_set)) {
309 args[argc++] = machine;
312 args[argc++] = rsync_path;
314 if (blocking_io < 0) {
316 if ((cp = strrchr(cmd, '/')) != NULL)
320 if (strcmp(cp, "rsh") == 0 || strcmp(cp, "remsh") == 0)
324 server_options(args,&argc);
326 if (argc >= MAX_ARGS - 2) {
327 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
328 exit_cleanup(RERR_SYNTAX);
334 if (!daemon_over_rsh && path && *path)
340 rprintf(FINFO,"cmd=");
341 for (i = 0; i < argc; i++)
342 rprintf(FINFO, "%s ", safe_fname(args[i]));
347 int from_gen_pipe[2];
348 if (fd_pair(from_gen_pipe) < 0) {
349 rsyserr(FERROR, errno, "pipe");
350 exit_cleanup(RERR_IPC);
352 batch_gen_fd = from_gen_pipe[0];
353 *f_out = from_gen_pipe[1];
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)
361 ret = local_child(argc, args, f_in, f_out, child_main);
363 ret = piped_child(args,f_in,f_out);
371 out_of_memory("do_cmd");
372 return 0; /* not reached */
376 static char *get_local_name(struct file_list *flist,char *name)
382 rprintf(FINFO,"get_local_name count=%d %s\n",
383 flist->count, NS(name));
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",
393 exit_cleanup(RERR_FILESELECT);
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);
404 if (flist->count <= 1 && ((e = strlen(name)) <= 1 || name[e-1] != '/'))
407 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
408 rsyserr(FERROR, errno, "mkdir %s failed", full_fname(name));
409 exit_cleanup(RERR_FILEIO);
412 rprintf(FINFO, "created directory %s\n", safe_fname(name));
419 if (!push_dir(name)) {
420 rsyserr(FERROR, errno, "push_dir#2 %s failed",
422 exit_cleanup(RERR_FILESELECT);
429 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
432 struct file_list *flist;
436 rprintf(FINFO, "server_sender starting pid=%ld\n",
440 if (am_daemon && lp_write_only(module_id)) {
441 rprintf(FERROR, "ERROR: module is write only\n");
442 exit_cleanup(RERR_SYNTAX);
446 if (!relative_paths && !push_dir(dir)) {
447 rsyserr(FERROR, errno, "push_dir#3 %s failed",
449 exit_cleanup(RERR_FILESELECT);
454 if (strcmp(dir,".")) {
456 if (strcmp(dir,"/") == 0)
458 for (i = 0; i < argc; i++)
462 if (argc == 0 && (recurse || list_only)) {
468 flist = send_file_list(f_out,argc,argv);
469 if (!flist || flist->count == 0) {
473 io_start_buffering_in();
474 io_start_buffering_out();
476 send_files(flist,f_out,f_in);
477 io_flush(FULL_FLUSH);
479 if (protocol_version >= 24) {
480 /* final goodbye message */
483 io_flush(FULL_FLUSH);
488 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
492 int error_pipe[2], name_pipe[2];
493 BOOL need_name_pipe = (basis_dir[0] || partial_dir || fuzzy_basis)
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. */
500 if (preserve_hard_links)
501 init_hard_links(flist);
503 if (delete_before && !local_name && flist->count > 0) {
504 /* Moved here from recv_files() to prevent a race condition */
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);
514 io_flush(NORMAL_FLUSH);
516 if ((pid = do_fork()) == -1) {
517 rsyserr(FERROR, errno, "fork failed in do_recv");
518 exit_cleanup(RERR_IPC);
522 close(error_pipe[0]);
523 if (need_name_pipe) {
525 set_blocking(name_pipe[0]);
531 /* we can't let two processes write to the socket at one time */
532 close_multiplexing_out();
534 /* set place to send errors */
535 set_msg_fd_out(error_pipe[1]);
537 recv_files(f_in, flist, local_name, name_pipe[0]);
538 io_flush(FULL_FLUSH);
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! */
551 close_multiplexing_in();
555 close(error_pipe[1]);
556 if (need_name_pipe) {
558 set_nonblocking(name_pipe[1]);
564 io_start_buffering_out();
566 set_msg_fd_in(error_pipe[0]);
568 generate_files(f_out, flist, local_name, name_pipe[1]);
571 io_flush(FULL_FLUSH);
572 if (protocol_version >= 24) {
573 /* send a final goodbye message */
574 write_int(f_out, -1);
576 io_flush(FULL_FLUSH);
580 wait_process(pid, &status);
585 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
588 struct file_list *flist;
589 char *local_name = NULL;
591 int save_verbose = verbose;
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. */
600 rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
601 argc, (long)getpid());
604 if (am_daemon && lp_read_only(module_id)) {
605 rprintf(FERROR,"ERROR: module is read only\n");
606 exit_cleanup(RERR_SYNTAX);
615 if (!am_daemon && !push_dir(dir)) {
616 rsyserr(FERROR, errno, "push_dir#4 %s failed",
618 exit_cleanup(RERR_FILESELECT);
622 io_start_buffering_in();
623 recv_filter_list(f_in);
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);
635 flist = recv_file_list(f_in);
636 verbose = save_verbose;
638 rprintf(FERROR,"server_recv: recv_file_list error\n");
639 exit_cleanup(RERR_FILESELECT);
643 if (strcmp(dir,".")) {
644 argv[0] += strlen(dir);
645 if (argv[0][0] == '/')
648 local_name = get_local_name(flist,argv[0]);
651 status = do_recv(f_in,f_out,flist,local_name);
652 exit_cleanup(status);
656 int child_main(int argc, char *argv[])
658 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
663 void start_server(int f_in, int f_out, int argc, char *argv[])
665 set_nonblocking(f_in);
666 set_nonblocking(f_out);
668 io_set_sock_fds(f_in, f_out);
669 setup_protocol(f_out, f_in);
671 if (protocol_version >= 23)
672 io_start_multiplex_out();
675 keep_dirlinks = 0; /* Must be disabled on the sender. */
677 recv_filter_list(f_in);
678 do_server_sender(f_in, f_out, argc, argv);
680 do_server_recv(f_in, f_out, argc, argv);
687 * This is called once the connection has been negotiated. It is used
688 * for rsyncd, remote-shell, and local connections.
690 int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
692 struct file_list *flist = NULL;
693 int status = 0, status2 = 0;
694 char *local_name = NULL;
696 cleanup_child_pid = pid;
698 set_nonblocking(f_in);
699 set_nonblocking(f_out);
702 io_set_sock_fds(f_in, f_out);
703 setup_protocol(f_out,f_in);
705 if (protocol_version >= 23 && !read_batch)
706 io_start_multiplex_in();
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);
719 keep_dirlinks = 0; /* Must be disabled on the sender. */
720 io_start_buffering_out();
721 if (!remote_filesfrom_file)
723 send_filter_list(f_out);
724 if (remote_filesfrom_file)
728 start_write_batch(f_out);
729 if (!read_batch) /* don't write to pipe */
730 flist = send_file_list(f_out,argc,argv);
733 rprintf(FINFO,"file list sent\n");
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 */
744 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
745 io_flush(FULL_FLUSH);
746 wait_process(pid, &status);
749 io_flush(FULL_FLUSH);
750 exit_cleanup(status);
756 send_filter_list(read_batch ? -1 : f_out);
758 if (filesfrom_fd >= 0) {
759 io_set_filesfrom_fds(filesfrom_fd, f_out);
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");
773 local_name = get_local_name(flist,argv[0]);
775 status2 = do_recv(f_in,f_out,flist,local_name);
779 rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
780 io_flush(FULL_FLUSH);
781 wait_process(pid, &status);
784 return MAX(status, status2);
787 static int copy_argv (char *argv[])
791 for (i = 0; argv[i]; i++) {
792 if (!(argv[i] = strdup(argv[i]))) {
793 rprintf (FERROR, "out of memory at %s(%d)\n",
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.
808 * Calls either start_socket_client (for sockets) or do_cmd and
809 * client_run (for ssh).
811 static int start_client(int argc, char *argv[])
814 char *shell_machine = NULL;
815 char *shell_path = NULL;
816 char *shell_user = NULL;
822 /* Don't clobber argv[] so that ps(1) can still show the right
824 if ((rc = copy_argv(argv)))
827 /* rsync:// always uses rsync server over direct socket connection */
828 if (strncasecmp(URL_PREFIX, argv[0], strlen(URL_PREFIX)) == 0
832 host = argv[0] + strlen(URL_PREFIX);
833 p = strchr(host,'/');
839 if (*host == '[' && (p = strchr(host, ']')) != NULL) {
845 p = strchr(host, ':');
847 rsync_port = atoi(p+1);
850 return start_socket_client(host, path, argc-1, argv+1);
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) {
860 "--files-from hostname is not the same as the transfer hostname\n");
861 exit_cleanup(RERR_SYNTAX);
863 if (p[1] == ':') { /* double colon */
866 return start_socket_client(argv[0], p+2,
873 if (argc < 1) { /* destination required */
875 exit_cleanup(RERR_SYNTAX);
880 shell_machine = argv[0];
883 } else { /* source is local */
886 /* rsync:// destination uses rsync server over direct socket */
887 if (strncasecmp(URL_PREFIX, argv[argc-1], strlen(URL_PREFIX)) == 0) {
890 host = argv[argc-1] + strlen(URL_PREFIX);
891 p = strchr(host,'/');
897 if (*host == '[' && (p = strchr(host, ']')) != NULL) {
903 p = strchr(host, ':');
905 rsync_port = atoi(p+1);
908 return start_socket_client(host, path, argc-1, argv);
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) {
916 "--files-from hostname is not the same as the transfer hostname\n");
917 exit_cleanup(RERR_SYNTAX);
919 if (!p) { /* no colon found, so src & dest are local */
921 if (remote_filesfrom_file) {
923 "--files-from cannot be remote when the transfer is local\n");
924 exit_cleanup(RERR_SYNTAX);
926 } else if (p[1] == ':') { /* double colon */
929 return start_socket_client(argv[argc-1], p+2,
938 exit_cleanup(RERR_SYNTAX);
942 shell_machine = NULL;
943 shell_path = argv[argc-1];
946 shell_machine = argv[argc-1];
951 } else { /* read_batch */
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);
961 p = strrchr(shell_machine,'@');
964 shell_user = shell_machine;
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) : "");
977 /* for remote source, only single dest arg can remain ... */
978 if (!am_sender && argc > 1) {
980 exit_cleanup(RERR_SYNTAX);
983 /* ... or no dest at all */
984 if (!am_sender && argc == 0)
987 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,
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) {
994 tmpret = start_inband_exchange(shell_user, shell_path,
1000 ret = client_run(f_in, f_out, pid, argc, argv);
1009 static RETSIGTYPE sigusr1_handler(UNUSED(int val))
1011 exit_cleanup(RERR_SIGNAL);
1014 static RETSIGTYPE sigusr2_handler(UNUSED(int val))
1016 if (log_got_error) _exit(RERR_PARTIAL);
1020 static RETSIGTYPE sigchld_handler(UNUSED(int val))
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.
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;
1046 * This routine catches signals and tries to send them to gdb.
1048 * Because it's called from inside a signal handler it ought not to
1049 * use too many library routines.
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?
1055 * @todo The /proc/ magic probably only works on Linux (and
1056 * Solaris?) Can we be more portable?
1058 #ifdef MAINTAINER_MODE
1059 const char *get_panic_action(void)
1061 const char *cmd_fmt = getenv("RSYNC_PANIC_ACTION");
1066 return "xterm -display :0 -T Panic -n Panic "
1067 "-e gdb /proc/%d/exe %d";
1072 * Handle a fatal signal by launching a debugger, controlled by $RSYNC_PANIC_ACTION.
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.
1079 static RETSIGTYPE rsync_panic_handler(UNUSED(int whatsig))
1084 sprintf(cmd_buf, get_panic_action(),
1085 getpid(), getpid());
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);
1096 int main(int argc,char *argv[])
1099 int orig_argc = argc;
1100 char **orig_argv = argv;
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 */
1112 starttime = time(NULL);
1113 am_root = (MY_UID() == 0);
1115 memset(&stats, 0, sizeof(stats));
1119 exit_cleanup(RERR_SYNTAX);
1122 /* we set a 0 umask so that correct file permissions can be
1124 orig_umask = (int)umask(0);
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. */
1130 exit_cleanup(RERR_SYNTAX);
1133 signal(SIGINT,SIGNAL_CAST sig_int);
1134 signal(SIGHUP,SIGNAL_CAST sig_int);
1135 signal(SIGTERM,SIGNAL_CAST sig_int);
1137 /* Ignore SIGPIPE; we consistently check error codes and will
1139 signal(SIGPIPE, SIG_IGN);
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. */
1149 if (write_batch || read_batch) {
1151 write_batch_shell_file(orig_argc, orig_argv, argc);
1153 if (read_batch && strcmp(batch_name, "-") == 0)
1154 batch_fd = STDIN_FILENO;
1156 batch_fd = do_open(batch_name,
1157 write_batch ? O_WRONLY | O_CREAT | O_TRUNC
1158 : O_RDONLY, S_IRUSR | S_IWUSR);
1161 rsyserr(FERROR, errno, "Batch file %s open error",
1162 full_fname(batch_name));
1163 exit_cleanup(RERR_FILEIO);
1166 read_stream_flags(batch_fd);
1169 if (am_daemon && !am_server)
1170 return daemon_main();
1174 exit_cleanup(RERR_SYNTAX);
1177 if (dry_run && !verbose && !what_has_changed)
1181 set_nonblocking(STDIN_FILENO);
1182 set_nonblocking(STDOUT_FILENO);
1184 return start_daemon(STDIN_FILENO, STDOUT_FILENO);
1185 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
1188 ret = start_client(argc, argv);
1190 exit_cleanup(RERR_STARTCLIENT);