We sanitize the --*-dest args here so they can be relative to the
[rsync/rsync.git] / main.c
CommitLineData
0f78b815
WD
1/*
2 * The startup routines, including main(), for rsync.
3 *
4 * Copyright (C) 1996-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
e7c67065
WD
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 22 */
c627d613
AT
23
24#include "rsync.h"
d9401514
WD
25#if defined CONFIG_LOCALE && defined HAVE_LOCALE_H
26#include <locale.h>
27#endif
c627d613 28
50839b4b
WD
29extern int verbose;
30extern int dry_run;
31extern int list_only;
32eda096 32extern int am_root;
d9c7edf6
WD
33extern int am_server;
34extern int am_sender;
b695f242 35extern int am_generator;
d9c7edf6 36extern int am_daemon;
32eda096 37extern int blocking_io;
bf26aa22 38extern int remove_sent_files;
32eda096 39extern int daemon_over_rsh;
bf26aa22 40extern int need_messages_from_generator;
051182cb 41extern int kluge_around_eof;
32eda096 42extern int do_stats;
32eda096
WD
43extern int log_got_error;
44extern int module_id;
bb6721dc 45extern int copy_links;
a695b379 46extern int copy_dirlinks;
83926d3c 47extern int keep_dirlinks;
32eda096 48extern int preserve_hard_links;
d04e9c51 49extern int protocol_version;
32eda096
WD
50extern int recurse;
51extern int relative_paths;
7c58c991
WD
52extern int sanitize_paths;
53extern int module_id;
32eda096 54extern int rsync_port;
b1df18d7 55extern int whole_file;
32eda096
WD
56extern int read_batch;
57extern int write_batch;
b9f592fb 58extern int batch_fd;
c0d8e84c 59extern int batch_gen_fd;
32eda096
WD
60extern int filesfrom_fd;
61extern pid_t cleanup_child_pid;
50839b4b 62extern struct stats stats;
860236bf 63extern char *filesfrom_host;
7c58c991 64extern char *basis_dir[];
32eda096
WD
65extern char *rsync_path;
66extern char *shell_cmd;
9b3318b0 67extern char *batch_name;
57dee64e 68
7c58c991
WD
69extern char curr_dir[MAXPATHLEN];
70
57dee64e 71int local_server = 0;
05278935 72mode_t orig_umask = 0;
a00628b3 73struct file_list *the_file_list;
c627d613 74
91c5833b
WD
75/* There's probably never more than at most 2 outstanding child processes,
76 * but set it higher, just in case. */
84e6d6fd 77#define MAXCHILDPROCS 7
ee7118a8 78
8261af74
WD
79#ifdef HAVE_SIGACTION
80# ifdef HAVE_SIGPROCMASK
81# define SIGACTMASK(n,h) SIGACTION(n,h), sigaddset(&sigmask,(n))
82# else
83# define SIGACTMASK(n,h) SIGACTION(n,h)
84# endif
60ee01f5
WD
85static struct sigaction sigact;
86#endif
87
ee7118a8
DD
88struct pid_status {
89 pid_t pid;
84e6d6fd 90 int status;
ee7118a8
DD
91} pid_stat_table[MAXCHILDPROCS];
92
33c4b445
WD
93static time_t starttime, endtime;
94static int64 total_read, total_written;
95
e5a2b854 96static void show_malloc_stats(void);
82980a23 97
97d8e709 98/* Works like waitpid(), but if we already harvested the child pid in our
6d59ac19 99 * remember_children(), we succeed instead of returning an error. */
97d8e709 100pid_t wait_process(pid_t pid, int *status_ptr, int flags)
82980a23 101{
97d8e709 102 pid_t waited_pid = waitpid(pid, status_ptr, flags);
d9c7edf6 103
6fb812f7 104 if (waited_pid == -1 && errno == ECHILD) {
97d8e709 105 /* Status of requested child no longer available: check to
6d59ac19 106 * see if it was processed by remember_children(). */
97d8e709 107 int cnt;
84e6d6fd 108 for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) {
ee7118a8 109 if (pid == pid_stat_table[cnt].pid) {
97d8e709 110 *status_ptr = pid_stat_table[cnt].status;
ee7118a8 111 pid_stat_table[cnt].pid = 0;
97d8e709 112 return pid;
ee7118a8
DD
113 }
114 }
115 }
116
97d8e709
WD
117 return waited_pid;
118}
119
120/* Wait for a process to exit, calling io_flush while waiting. */
84e6d6fd 121static void wait_process_with_flush(pid_t pid, int *exit_code_ptr)
97d8e709
WD
122{
123 pid_t waited_pid;
124 int status;
125
126 while ((waited_pid = wait_process(pid, &status, WNOHANG)) == 0) {
127 msleep(20);
128 io_flush(FULL_FLUSH);
129 }
130
d9c7edf6
WD
131 /* TODO: If the child exited on a signal, then log an
132 * appropriate error message. Perhaps we should also accept a
133 * message describing the purpose of the child. Also indicate
dbefb6b4 134 * this to the caller so that they know something went wrong. */
84e6d6fd
WD
135 if (waited_pid < 0) {
136 rsyserr(FERROR, errno, "waitpid");
137 *exit_code_ptr = RERR_WAITCHILD;
138 } else if (!WIFEXITED(status)) {
40e6752f 139#ifdef WCOREDUMP
dbefb6b4 140 if (WCOREDUMP(status))
84e6d6fd 141 *exit_code_ptr = RERR_CRASHED;
40e6752f
WD
142 else
143#endif
144 if (WIFSIGNALED(status))
84e6d6fd 145 *exit_code_ptr = RERR_TERMINATED;
dbefb6b4 146 else
84e6d6fd 147 *exit_code_ptr = RERR_WAITCHILD;
dbefb6b4 148 } else
84e6d6fd 149 *exit_code_ptr = WEXITSTATUS(status);
82980a23
AT
150}
151
b9f592fb
WD
152/* This function gets called from all 3 processes. We want the client side
153 * to actually output the text, but the sender is the only process that has
154 * all the stats we need. So, if we're a client sender, we do the report.
155 * If we're a server sender, we write the stats on the supplied fd. If
156 * we're the client receiver we read the stats from the supplied fd and do
157 * the report. All processes might also generate a set of debug stats, if
158 * the verbose level is high enough (this is the only thing that the
159 * generator process and the server receiver ever do here). */
33c4b445 160static void handle_stats(int f)
c627d613 161{
33c4b445
WD
162 endtime = time(NULL);
163
b9f592fb 164 /* Cache two stats because the read/write code can change it. */
33c4b445
WD
165 total_read = stats.total_read;
166 total_written = stats.total_written;
7a6421fa 167
7bb7058e 168 if (do_stats && verbose > 1) {
5c15e29f 169 /* These come out from every process */
7bb7058e 170 show_malloc_stats();
86943126 171 show_flist_stats();
5c15e29f
MP
172 }
173
e5fbaa71
S
174 if (am_generator)
175 return;
176
248fbb8c 177 if (am_daemon) {
a9766ef1 178 log_exit(0, __FILE__, __LINE__);
83926d3c
WD
179 if (f == -1 || !am_sender)
180 return;
248fbb8c
AT
181 }
182
e19452a9 183 if (am_server) {
3e491682 184 if (am_sender) {
b9f592fb
WD
185 write_longint(f, total_read);
186 write_longint(f, total_written);
187 write_longint(f, stats.total_size);
f0f7e760
WD
188 if (protocol_version >= 29) {
189 write_longint(f, stats.flist_buildtime);
190 write_longint(f, stats.flist_xfertime);
191 }
e19452a9 192 }
7a6421fa
AT
193 return;
194 }
e19452a9
DD
195
196 /* this is the client */
d9c7edf6 197
9d19f8a5 198 if (f < 0 && !am_sender) /* e.g. when we got an empty file list. */
0f78b815 199 ;
9d19f8a5 200 else if (!am_sender) {
58c5c245
WD
201 /* Read the first two in opposite order because the meaning of
202 * read/write swaps when switching from sender to receiver. */
b9f592fb
WD
203 total_written = read_longint(f);
204 total_read = read_longint(f);
a800434a 205 stats.total_size = read_longint(f);
f0f7e760
WD
206 if (protocol_version >= 29) {
207 stats.flist_buildtime = read_longint(f);
208 stats.flist_xfertime = read_longint(f);
209 }
e4676bb5 210 } else if (write_batch) {
b9f592fb
WD
211 /* The --read-batch process is going to be a client
212 * receiver, so we need to give it the stats. */
213 write_longint(batch_fd, total_read);
214 write_longint(batch_fd, total_written);
215 write_longint(batch_fd, stats.total_size);
f0f7e760
WD
216 if (protocol_version >= 29) {
217 write_longint(batch_fd, stats.flist_buildtime);
218 write_longint(batch_fd, stats.flist_xfertime);
219 }
a800434a 220 }
e4676bb5 221}
a800434a 222
e4676bb5
WD
223static void output_summary(void)
224{
a800434a 225 if (do_stats) {
1f658d42 226 rprintf(FINFO,"\nNumber of files: %d\n", stats.num_files);
d9c7edf6
WD
227 rprintf(FINFO,"Number of files transferred: %d\n",
228 stats.num_transferred_files);
60613dc8
WD
229 rprintf(FINFO,"Total file size: %s bytes\n",
230 human_num(stats.total_size));
231 rprintf(FINFO,"Total transferred file size: %s bytes\n",
232 human_num(stats.total_transferred_size));
233 rprintf(FINFO,"Literal data: %s bytes\n",
234 human_num(stats.literal_data));
235 rprintf(FINFO,"Matched data: %s bytes\n",
236 human_num(stats.matched_data));
1f658d42 237 rprintf(FINFO,"File list size: %d\n", stats.flist_size);
f0f7e760
WD
238 if (stats.flist_buildtime) {
239 rprintf(FINFO,
240 "File list generation time: %.3f seconds\n",
241 (double)stats.flist_buildtime / 1000);
242 rprintf(FINFO,
243 "File list transfer time: %.3f seconds\n",
244 (double)stats.flist_xfertime / 1000);
245 }
60613dc8
WD
246 rprintf(FINFO,"Total bytes sent: %s\n",
247 human_num(total_written));
248 rprintf(FINFO,"Total bytes received: %s\n",
249 human_num(total_read));
7a6421fa 250 }
d9c7edf6 251
e19452a9 252 if (verbose || do_stats) {
b9f592fb 253 rprintf(FINFO,
60613dc8
WD
254 "\nsent %s bytes received %s bytes %s bytes/sec\n",
255 human_num(total_written), human_num(total_read),
256 human_dnum((total_written + total_read)/(0.5 + (endtime - starttime)), 2));
257 rprintf(FINFO, "total size is %s speedup is %.2f\n",
258 human_num(stats.total_size),
b9f592fb 259 (double)stats.total_size / (total_written+total_read));
e19452a9 260 }
fc8a6b97
AT
261
262 fflush(stdout);
263 fflush(stderr);
c627d613
AT
264}
265
266
e5a2b854
MP
267/**
268 * If our C library can get malloc statistics, then show them to FINFO
269 **/
270static void show_malloc_stats(void)
271{
4f5b0756 272#ifdef HAVE_MALLINFO
e5a2b854
MP
273 struct mallinfo mi;
274
275 mi = mallinfo();
276
ce672562 277 rprintf(FINFO, "\n" RSYNC_NAME "[%d] (%s%s%s) heap statistics:\n",
f846a9bf
WD
278 getpid(), am_server ? "server " : "",
279 am_daemon ? "daemon " : "", who_am_i());
280 rprintf(FINFO, " arena: %10ld (bytes from sbrk)\n",
281 (long)mi.arena);
282 rprintf(FINFO, " ordblks: %10ld (chunks not in use)\n",
283 (long)mi.ordblks);
284 rprintf(FINFO, " smblks: %10ld\n",
285 (long)mi.smblks);
286 rprintf(FINFO, " hblks: %10ld (chunks from mmap)\n",
287 (long)mi.hblks);
288 rprintf(FINFO, " hblkhd: %10ld (bytes from mmap)\n",
289 (long)mi.hblkhd);
290 rprintf(FINFO, " allmem: %10ld (bytes from sbrk + mmap)\n",
291 (long)mi.arena + mi.hblkhd);
292 rprintf(FINFO, " usmblks: %10ld\n",
293 (long)mi.usmblks);
294 rprintf(FINFO, " fsmblks: %10ld\n",
295 (long)mi.fsmblks);
296 rprintf(FINFO, " uordblks: %10ld (bytes used)\n",
297 (long)mi.uordblks);
298 rprintf(FINFO, " fordblks: %10ld (bytes free)\n",
299 (long)mi.fordblks);
300 rprintf(FINFO, " keepcost: %10ld (bytes in releasable chunk)\n",
301 (long)mi.keepcost);
e5a2b854
MP
302#endif /* HAVE_MALLINFO */
303}
304
305
0882faa2 306/* Start the remote shell. cmd may be NULL to use the default. */
6c2e5b56
WD
307static pid_t do_cmd(char *cmd, char *machine, char *user, char *path,
308 int *f_in, int *f_out)
c627d613 309{
6c2e5b56 310 int i, argc = 0;
887e553f 311 char *args[MAX_ARGS];
19b27a48 312 pid_t ret;
82f0c63e 313 char *dir = NULL;
bb4aa89c 314 int dash_l_set = 0;
366345fe 315
088aac85 316 if (!read_batch && !local_server) {
82f0c63e 317 char *t, *f, in_quote = '\0';
9af87151 318 char *rsh_env = getenv(RSYNC_RSH_ENV);
366345fe 319 if (!cmd)
9af87151 320 cmd = rsh_env;
366345fe
AT
321 if (!cmd)
322 cmd = RSYNC_RSH;
323 cmd = strdup(cmd);
d9c7edf6 324 if (!cmd)
366345fe
AT
325 goto oom;
326
82f0c63e
WD
327 for (t = f = cmd; *f; f++) {
328 if (*f == ' ')
329 continue;
e7a392c7 330 /* Comparison leaves rooms for server_options(). */
d8c4d6de 331 if (argc >= MAX_ARGS - MAX_SERVER_ARGS) {
e7a392c7 332 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
887e553f
WD
333 exit_cleanup(RERR_SYNTAX);
334 }
82f0c63e
WD
335 args[argc++] = t;
336 while (*f != ' ' || in_quote) {
337 if (!*f) {
338 if (in_quote) {
339 rprintf(FERROR,
340 "Missing trailing-%c in remote-shell command.\n",
341 in_quote);
342 exit_cleanup(RERR_SYNTAX);
343 }
344 f--;
345 break;
346 }
347 if (*f == '\'' || *f == '"') {
348 if (!in_quote) {
349 in_quote = *f++;
350 continue;
351 }
352 if (*f == in_quote && *++f != in_quote) {
353 in_quote = '\0';
354 continue;
355 }
356 }
357 *t++ = *f++;
358 }
359 *t++ = '\0';
887e553f 360 }
c627d613 361
d9c7edf6 362 /* check to see if we've already been given '-l user' in
9af87151 363 * the remote-shell command */
bb4aa89c
WD
364 for (i = 0; i < argc-1; i++) {
365 if (!strcmp(args[i], "-l") && args[i+1][0] != '-')
366 dash_l_set = 1;
367 }
368
4f5b0756 369#ifdef HAVE_REMSH
366345fe
AT
370 /* remsh (on HPUX) takes the arguments the other way around */
371 args[argc++] = machine;
bb4aa89c 372 if (user && !(daemon_over_rsh && dash_l_set)) {
366345fe
AT
373 args[argc++] = "-l";
374 args[argc++] = user;
375 }
7b8356d0 376#else
bb4aa89c 377 if (user && !(daemon_over_rsh && dash_l_set)) {
366345fe
AT
378 args[argc++] = "-l";
379 args[argc++] = user;
380 }
381 args[argc++] = machine;
7b8356d0 382#endif
c627d613 383
366345fe 384 args[argc++] = rsync_path;
c627d613 385
9af87151 386 if (blocking_io < 0) {
90e22f4b
WD
387 char *cp;
388 if ((cp = strrchr(cmd, '/')) != NULL)
389 cp++;
390 else
391 cp = cmd;
392 if (strcmp(cp, "rsh") == 0 || strcmp(cp, "remsh") == 0)
393 blocking_io = 1;
66b71163 394 }
e384bfbd 395
93689aa5 396 server_options(args,&argc);
e7a392c7
WD
397
398 if (argc >= MAX_ARGS - 2) {
399 rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
400 exit_cleanup(RERR_SYNTAX);
401 }
366345fe 402 }
c627d613 403
366345fe 404 args[argc++] = ".";
76076c4b 405
d9c7edf6 406 if (!daemon_over_rsh && path && *path)
366345fe 407 args[argc++] = path;
c627d613 408
366345fe 409 args[argc] = NULL;
c627d613 410
366345fe 411 if (verbose > 3) {
e7a392c7 412 for (i = 0; i < argc; i++)
45c49b52 413 rprintf(FINFO, "cmd[%d]=%s ", i, args[i]);
82f0c63e 414 rprintf(FINFO, "\n");
366345fe
AT
415 }
416
c0d8e84c
WD
417 if (read_batch) {
418 int from_gen_pipe[2];
419 if (fd_pair(from_gen_pipe) < 0) {
420 rsyserr(FERROR, errno, "pipe");
421 exit_cleanup(RERR_IPC);
422 }
423 batch_gen_fd = from_gen_pipe[0];
424 *f_out = from_gen_pipe[1];
425 *f_in = batch_fd;
426 ret = -1; /* no child pid */
427 } else if (local_server) {
b1df18d7
WD
428 /* If the user didn't request --[no-]whole-file, force
429 * it on, but only if we're not batch processing. */
c0d8e84c 430 if (whole_file < 0 && !write_batch)
b1df18d7 431 whole_file = 1;
25d34a5c 432 ret = local_child(argc, args, f_in, f_out, child_main);
c0d8e84c 433 } else
366345fe 434 ret = piped_child(args,f_in,f_out);
c627d613 435
3a69fad0
WD
436 if (dir)
437 free(dir);
82306bf6 438
366345fe 439 return ret;
c627d613 440
acee11fc 441 oom:
366345fe
AT
442 out_of_memory("do_cmd");
443 return 0; /* not reached */
c627d613
AT
444}
445
eb598fac
WD
446/* The receiving side operates in one of two modes:
447 *
cc8c5057
WD
448 * 1. it receives any number of files into a destination directory,
449 * placing them according to their names in the file-list.
eb598fac
WD
450 *
451 * 2. it receives a single file and saves it using the name in the
452 * destination path instead of its file-list name. This requires a
453 * "local name" for writing out the destination file.
454 *
455 * So, our task is to figure out what mode/local-name we need and return
456 * either a NULL for mode 1, or the local-name for mode 2. We also
457 * change directory if there are any path components in dest_path. */
458static char *get_local_name(struct file_list *flist, char *dest_path)
c627d613 459{
7a6421fa 460 STRUCT_STAT st;
eb598fac 461 char *cp;
c627d613 462
eb598fac
WD
463 if (verbose > 2) {
464 rprintf(FINFO, "get_local_name count=%d %s\n",
465 flist->count, NS(dest_path));
466 }
1f0610ef 467
ee8e2b15 468 if (!dest_path || list_only)
1f0610ef 469 return NULL;
c95da96a 470
eb598fac
WD
471 /* If the destination path refers to an existing directory, enter
472 * it and use mode 1. If there is something other than a directory
473 * at the destination path, we must be transferring one file
474 * (anything at the destination will be overwritten). */
475 if (do_stat(dest_path, &st) == 0) {
1ff5450d 476 if (S_ISDIR(st.st_mode)) {
eb598fac 477 if (!push_dir(dest_path)) {
982e05bb 478 rsyserr(FERROR, errno, "push_dir#1 %s failed",
eb598fac 479 full_fname(dest_path));
65417579 480 exit_cleanup(RERR_FILESELECT);
1ff5450d
AT
481 }
482 return NULL;
483 }
484 if (flist->count > 1) {
eb598fac
WD
485 rprintf(FERROR,
486 "ERROR: destination must be a directory when"
487 " copying more than 1 file\n");
65417579 488 exit_cleanup(RERR_FILESELECT);
1ff5450d 489 }
44c26bf2
WD
490 /* Caution: flist->count could be 0! */
491 if (flist->count == 1 && S_ISDIR(flist->files[0]->mode)) {
492 rprintf(FERROR,
493 "ERROR: cannot overwrite non-directory"
494 " with a directory\n");
495 exit_cleanup(RERR_FILESELECT);
496 }
cc8c5057
WD
497 } else if (errno != ENOENT) {
498 rsyserr(FERROR, errno, "cannot stat destination %s",
499 full_fname(dest_path));
500 exit_cleanup(RERR_FILESELECT);
1ff5450d
AT
501 }
502
eb598fac
WD
503 cp = strrchr(dest_path, '/');
504
505 /* If the destination path ends in a slash or we are transferring
506 * multiple files, create a directory at the destination path,
507 * enter the new directory, and use mode 1. */
508 if (flist->count > 1 || (cp && !cp[1])) {
509 /* Lop off the final slash (if any). */
510 if (cp && !cp[1])
511 *cp = '\0';
512
05278935 513 if (mkdir_defmode(dest_path) != 0) {
eb598fac
WD
514 rsyserr(FERROR, errno, "mkdir %s failed",
515 full_fname(dest_path));
516 exit_cleanup(RERR_FILEIO);
517 }
1ff5450d 518
eb598fac
WD
519 if (verbose)
520 rprintf(FINFO, "created directory %s\n", dest_path);
521
522 if (dry_run) {
523 /* Indicate that the destination directory doesn't
524 * really exist and return mode 1. */
525 dry_run++;
526 return NULL;
527 }
528
529 if (!push_dir(dest_path)) {
530 rsyserr(FERROR, errno, "push_dir#2 %s failed",
531 full_fname(dest_path));
532 exit_cleanup(RERR_FILESELECT);
533 }
e5a96f0f 534
e5a96f0f 535 return NULL;
1ff5450d
AT
536 }
537
eb598fac
WD
538 /* Otherwise, we are writing a single file, possibly on top of an
539 * existing non-directory. Change to the item's parent directory
540 * (if it has a path component), return the basename of the
541 * destination file as the local name, and use mode 2. */
542 if (!cp)
543 return dest_path;
544
545 if (cp == dest_path)
546 dest_path = "/";
547
548 *cp = '\0';
549 if (!push_dir(dest_path)) {
550 rsyserr(FERROR, errno, "push_dir#3 %s failed",
551 full_fname(dest_path));
65417579 552 exit_cleanup(RERR_FILESELECT);
1ff5450d 553 }
eb598fac 554 *cp = '/';
1ff5450d 555
eb598fac 556 return cp + 1;
c627d613
AT
557}
558
559
1a8e5c97 560/* This is only called by the sender. */
3485ae83 561static void read_final_goodbye(int f_in, int f_out)
4e0fcd85 562{
1a8e5c97
WD
563 int i;
564
565 if (protocol_version < 29)
566 i = read_int(f_in);
567 else {
3485ae83 568 while ((i = read_int(f_in)) == the_file_list->count
1a8e5c97
WD
569 && read_shortint(f_in) == ITEM_IS_NEW) {
570 /* Forward the keep-alive (no-op) to the receiver. */
3485ae83 571 write_int(f_out, the_file_list->count);
1a8e5c97
WD
572 write_shortint(f_out, ITEM_IS_NEW);
573 }
4e0fcd85
WD
574 }
575
1a8e5c97 576 if (i != -1) {
c7791b8c
WD
577 rprintf(FERROR, "Invalid packet at end of run (%d) [%s]\n",
578 i, who_am_i());
1a8e5c97 579 exit_cleanup(RERR_PROTOCOL);
4e0fcd85
WD
580 }
581}
582
583
eb598fac 584static void do_server_sender(int f_in, int f_out, int argc, char *argv[])
c627d613 585{
7a6421fa
AT
586 struct file_list *flist;
587 char *dir = argv[0];
c627d613 588
45e08edb
WD
589 if (verbose > 2) {
590 rprintf(FINFO, "server_sender starting pid=%ld\n",
591 (long)getpid());
592 }
d9c7edf6 593
2adbcdc7 594 if (am_daemon && lp_write_only(module_id)) {
7a92ded3
WD
595 rprintf(FERROR, "ERROR: module is write only\n");
596 exit_cleanup(RERR_SYNTAX);
597 return;
598 }
bf26aa22
WD
599 if (am_daemon && lp_read_only(module_id) && remove_sent_files) {
600 rprintf(FERROR,
601 "ERROR: --remove-sent-files cannot be used with a read-only module\n");
602 exit_cleanup(RERR_SYNTAX);
603 return;
604 }
7a92ded3 605
59187666 606 if (!relative_paths && !push_dir(dir)) {
982e05bb
WD
607 rsyserr(FERROR, errno, "push_dir#3 %s failed",
608 full_fname(dir));
65417579 609 exit_cleanup(RERR_FILESELECT);
7a6421fa
AT
610 }
611 argc--;
612 argv++;
d9c7edf6 613
48a1ff0d 614 if (argc == 0 && (recurse || list_only)) {
e1f67417 615 argc = 1;
7a6421fa
AT
616 argv--;
617 argv[0] = ".";
618 }
d9c7edf6 619
7a6421fa 620 flist = send_file_list(f_out,argc,argv);
8d9dc9f9
AT
621 if (!flist || flist->count == 0) {
622 exit_cleanup(0);
623 }
a00628b3 624 the_file_list = flist;
8d9dc9f9 625
da3478b2
WD
626 io_start_buffering_in();
627 io_start_buffering_out();
628
7a6421fa 629 send_files(flist,f_out,f_in);
f1e3656e 630 io_flush(FULL_FLUSH);
33c4b445 631 handle_stats(f_out);
4e0fcd85 632 if (protocol_version >= 24)
3485ae83 633 read_final_goodbye(f_in, f_out);
f1e3656e 634 io_flush(FULL_FLUSH);
7a6421fa 635 exit_cleanup(0);
c627d613
AT
636}
637
638
dc5ddbcc
AT
639static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
640{
d186eb1a 641 int pid;
84e6d6fd 642 int exit_code = 0;
c70e07d9 643 int error_pipe[2];
dc5ddbcc 644
bb6721dc
WD
645 /* The receiving side mustn't obey this, or an existing symlink that
646 * points to an identical file won't be replaced by the referent. */
a695b379 647 copy_links = copy_dirlinks = 0;
bb6721dc 648
d186eb1a 649 if (preserve_hard_links)
3485ae83 650 init_hard_links();
dc5ddbcc 651
c70e07d9 652 if (fd_pair(error_pipe) < 0) {
ab759cd2 653 rsyserr(FERROR, errno, "pipe failed in do_recv");
c8f2f857 654 exit_cleanup(RERR_IPC);
554e0a8d 655 }
d9c7edf6 656
f1e3656e 657 io_flush(NORMAL_FLUSH);
c6e7fcb4 658
ba449e44 659 if ((pid = do_fork()) == -1) {
c8f2f857 660 rsyserr(FERROR, errno, "fork failed in do_recv");
ba449e44
WD
661 exit_cleanup(RERR_IPC);
662 }
663
664 if (pid == 0) {
554e0a8d 665 close(error_pipe[0]);
3a69fad0
WD
666 if (f_in != f_out)
667 close(f_out);
e08c9610 668
554e0a8d 669 /* we can't let two processes write to the socket at one time */
9eeb3b9c 670 close_multiplexing_out();
554e0a8d
AT
671
672 /* set place to send errors */
f1e3656e 673 set_msg_fd_out(error_pipe[1]);
554e0a8d 674
c70e07d9 675 recv_files(f_in, flist, local_name);
f1e3656e 676 io_flush(FULL_FLUSH);
33c4b445 677 handle_stats(f_in);
e08c9610 678
f1e3656e
WD
679 send_msg(MSG_DONE, "", 0);
680 io_flush(FULL_FLUSH);
4e0fcd85 681
40df65fd
WD
682 /* Handle any keep-alive packets from the post-processing work
683 * that the generator does. */
4e0fcd85 684 if (protocol_version >= 29) {
051182cb 685 kluge_around_eof = -1;
1a8e5c97
WD
686
687 /* This should only get stopped via a USR2 signal. */
688 while (read_int(f_in) == flist->count
689 && read_shortint(f_in) == ITEM_IS_NEW) {}
690
3485ae83
WD
691 rprintf(FERROR, "Invalid packet at end of run [%s]\n",
692 who_am_i());
1a8e5c97 693 exit_cleanup(RERR_PROTOCOL);
4e0fcd85
WD
694 }
695
9e0582f9
WD
696 /* Finally, we go to sleep until our parent kills us with a
697 * USR2 signal. We sleep for a short time, as on some OSes
698 * a signal won't interrupt a sleep! */
f1e3656e
WD
699 while (1)
700 msleep(20);
d186eb1a 701 }
dc5ddbcc 702
b695f242 703 am_generator = 1;
9eeb3b9c 704 close_multiplexing_in();
e8a96e27 705 if (write_batch && !am_server)
b9f592fb 706 stop_write_batch();
b695f242 707
554e0a8d 708 close(error_pipe[1]);
3a69fad0
WD
709 if (f_in != f_out)
710 close(f_in);
e1b3d5c4 711
da3478b2 712 io_start_buffering_out();
b3e10ed7 713
f1e3656e 714 set_msg_fd_in(error_pipe[0]);
554e0a8d 715
c70e07d9 716 generate_files(f_out, flist, local_name);
8d9dc9f9 717
33c4b445 718 handle_stats(-1);
f1e3656e 719 io_flush(FULL_FLUSH);
d04e9c51 720 if (protocol_version >= 24) {
8ada7518
AT
721 /* send a final goodbye message */
722 write_int(f_out, -1);
723 }
f1e3656e 724 io_flush(FULL_FLUSH);
8ada7518 725
f1e3656e 726 set_msg_fd_in(-1);
089a2435 727 kill(pid, SIGUSR2);
84e6d6fd
WD
728 wait_process_with_flush(pid, &exit_code);
729 return exit_code;
dc5ddbcc
AT
730}
731
c627d613 732
9486289c 733static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
c627d613 734{
84e6d6fd 735 int exit_code;
7a6421fa 736 struct file_list *flist;
6c2e5b56 737 char *local_name = NULL;
7a6421fa 738 char *dir = NULL;
07bff66f
WD
739 int save_verbose = verbose;
740
741 if (filesfrom_fd >= 0) {
742 /* We can't mix messages with files-from data on the socket,
743 * so temporarily turn off verbose messages. */
744 verbose = 0;
745 }
f0fca04e 746
45e08edb
WD
747 if (verbose > 2) {
748 rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
749 argc, (long)getpid());
750 }
09b7f5db 751
2adbcdc7 752 if (am_daemon && lp_read_only(module_id)) {
09b7f5db
AT
753 rprintf(FERROR,"ERROR: module is read only\n");
754 exit_cleanup(RERR_SYNTAX);
755 return;
756 }
757
7a6421fa
AT
758 if (argc > 0) {
759 dir = argv[0];
760 argc--;
761 argv++;
59187666 762 if (!am_daemon && !push_dir(dir)) {
982e05bb
WD
763 rsyserr(FERROR, errno, "push_dir#4 %s failed",
764 full_fname(dir));
65417579 765 exit_cleanup(RERR_FILESELECT);
d9c7edf6 766 }
7a6421fa 767 }
c627d613 768
da3478b2 769 io_start_buffering_in();
57dee64e 770 recv_filter_list(f_in);
c627d613 771
7c2a9e76 772 if (filesfrom_fd >= 0) {
07bff66f
WD
773 /* We need to send the files-from names to the sender at the
774 * same time that we receive the file-list from them, so we
775 * need the IO routines to automatically write out the names
776 * onto our f_out socket as we read the file-list. This
777 * avoids both deadlock and extra delays/buffers. */
7c2a9e76
WD
778 io_set_filesfrom_fds(filesfrom_fd, f_out);
779 filesfrom_fd = -1;
780 }
781
b9f592fb 782 flist = recv_file_list(f_in);
07bff66f 783 verbose = save_verbose;
4c36a13e
AT
784 if (!flist) {
785 rprintf(FERROR,"server_recv: recv_file_list error\n");
65417579 786 exit_cleanup(RERR_FILESELECT);
7a6421fa 787 }
a00628b3 788 the_file_list = flist;
d9c7edf6 789
29fad7a3 790 if (argc > 0)
7a6421fa 791 local_name = get_local_name(flist,argv[0]);
c627d613 792
7c58c991
WD
793 /* Now that we know what our destination directory turned out to be,
794 * we can sanitize the --link-/copy-/compare-dest args correctly. */
795 if (sanitize_paths) {
796 char *dest_path = curr_dir + strlen(lp_path(module_id));
797 int dest_depth = count_dir_elements(dest_path);
798 char **dir;
799 for (dir = basis_dir; *dir; dir++)
800 *dir = sanitize_path(NULL, *dir, NULL, dest_depth);
801 }
802
84e6d6fd
WD
803 exit_code = do_recv(f_in,f_out,flist,local_name);
804 exit_cleanup(exit_code);
c627d613
AT
805}
806
807
734a94a2 808int child_main(int argc, char *argv[])
25d34a5c
MP
809{
810 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
734a94a2 811 return 0;
25d34a5c
MP
812}
813
814
9486289c 815void start_server(int f_in, int f_out, int argc, char *argv[])
366345fe 816{
f0359dd0
AT
817 set_nonblocking(f_in);
818 set_nonblocking(f_out);
819
da3478b2
WD
820 io_set_sock_fds(f_in, f_out);
821 setup_protocol(f_out, f_in);
595251de 822#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9656de5d
WD
823 setup_iconv();
824#endif
da3478b2 825
d04e9c51 826 if (protocol_version >= 23)
da3478b2 827 io_start_multiplex_out();
7a6421fa 828
7a6421fa 829 if (am_sender) {
83926d3c 830 keep_dirlinks = 0; /* Must be disabled on the sender. */
bf26aa22
WD
831 if (need_messages_from_generator)
832 io_start_multiplex_in();
9b3318b0 833
7842418b 834 recv_filter_list(f_in);
7a6421fa
AT
835 do_server_sender(f_in, f_out, argc, argv);
836 } else {
837 do_server_recv(f_in, f_out, argc, argv);
838 }
839 exit_cleanup(0);
366345fe
AT
840}
841
0ba48136
MP
842
843/*
844 * This is called once the connection has been negotiated. It is used
845 * for rsyncd, remote-shell, and local connections.
846 */
19b27a48 847int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
9486289c 848{
088aac85 849 struct file_list *flist = NULL;
84e6d6fd 850 int exit_code = 0, exit_code2 = 0;
9486289c 851 char *local_name = NULL;
19b27a48
AT
852
853 cleanup_child_pid = pid;
67a28eb2 854 if (!read_batch) {
b9f592fb
WD
855 set_nonblocking(f_in);
856 set_nonblocking(f_out);
857 }
f0359dd0 858
da3478b2 859 io_set_sock_fds(f_in, f_out);
6d7b6081 860 setup_protocol(f_out,f_in);
595251de 861#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9656de5d
WD
862 setup_iconv();
863#endif
6d7b6081 864
b9f592fb 865 if (protocol_version >= 23 && !read_batch)
da3478b2 866 io_start_multiplex_in();
d9c7edf6 867
18882701
WD
868 /* We set our stderr file handle to blocking because ssh might have
869 * set it to non-blocking. This can be particularly troublesome if
870 * stderr is a clone of stdout, because ssh would have set our stdout
871 * to non-blocking at the same time (which can easily cause us to lose
872 * output from our print statements). This kluge shouldn't cause ssh
873 * any problems for how we use it. Note also that we delayed setting
874 * this until after the above protocol setup so that we know for sure
875 * that ssh is done twiddling its file descriptors. */
876 set_blocking(STDERR_FILENO);
877
9486289c 878 if (am_sender) {
83926d3c 879 keep_dirlinks = 0; /* Must be disabled on the sender. */
da3478b2 880 io_start_buffering_out();
860236bf 881 if (!filesfrom_host)
a0a33ee5 882 set_msg_fd_in(f_in);
57dee64e 883 send_filter_list(f_out);
860236bf 884 if (filesfrom_host)
7c2a9e76 885 filesfrom_fd = f_in;
b9f592fb 886
e8a96e27 887 if (write_batch && !am_server)
b9f592fb 888 start_write_batch(f_out);
a00628b3 889 flist = send_file_list(f_out, argc, argv);
a0a33ee5 890 set_msg_fd_in(-1);
d9c7edf6 891 if (verbose > 3)
9486289c 892 rprintf(FINFO,"file list sent\n");
a00628b3 893 the_file_list = flist;
e1b3d5c4 894
f1e3656e 895 io_flush(NORMAL_FLUSH);
9486289c 896 send_files(flist,f_out,f_in);
f1e3656e 897 io_flush(FULL_FLUSH);
33c4b445 898 handle_stats(-1);
4e0fcd85 899 if (protocol_version >= 24)
3485ae83 900 read_final_goodbye(f_in, f_out);
9486289c
AT
901 if (pid != -1) {
902 if (verbose > 3)
08a740ff 903 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
f1e3656e 904 io_flush(FULL_FLUSH);
84e6d6fd 905 wait_process_with_flush(pid, &exit_code);
9486289c 906 }
33c4b445 907 output_summary();
c87ae64a 908 io_flush(FULL_FLUSH);
84e6d6fd 909 exit_cleanup(exit_code);
9486289c 910 }
f7632fc6 911
bf26aa22
WD
912 if (need_messages_from_generator && !read_batch)
913 io_start_multiplex_out();
914
48a1ff0d
WD
915 if (argc == 0)
916 list_only |= 1;
d9c7edf6 917
57dee64e 918 send_filter_list(read_batch ? -1 : f_out);
d9c7edf6 919
7c2a9e76
WD
920 if (filesfrom_fd >= 0) {
921 io_set_filesfrom_fds(filesfrom_fd, f_out);
922 filesfrom_fd = -1;
923 }
924
e8a96e27 925 if (write_batch && !am_server)
b9f592fb 926 start_write_batch(f_in);
9486289c 927 flist = recv_file_list(f_in);
a00628b3 928 the_file_list = flist;
d9c7edf6 929
9d19f8a5
WD
930 if (flist && flist->count > 0) {
931 local_name = get_local_name(flist, argv[0]);
d9c7edf6 932
84e6d6fd 933 exit_code2 = do_recv(f_in, f_out, flist, local_name);
9d19f8a5
WD
934 } else {
935 handle_stats(-1);
936 output_summary();
937 }
d9c7edf6 938
9486289c 939 if (pid != -1) {
8d9dc9f9 940 if (verbose > 3)
08a740ff 941 rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
f1e3656e 942 io_flush(FULL_FLUSH);
84e6d6fd 943 wait_process_with_flush(pid, &exit_code);
9486289c 944 }
d9c7edf6 945
84e6d6fd 946 return MAX(exit_code, exit_code2);
9486289c
AT
947}
948
7169bb4a
MP
949static int copy_argv (char *argv[])
950{
951 int i;
952
953 for (i = 0; argv[i]; i++) {
954 if (!(argv[i] = strdup(argv[i]))) {
955 rprintf (FERROR, "out of memory at %s(%d)\n",
956 __FILE__, __LINE__);
957 return RERR_MALLOC;
958 }
959 }
960
961 return 0;
962}
963
964
c1a04ecb 965/**
0ba48136
MP
966 * Start a client for either type of remote connection. Work out
967 * whether the arguments request a remote shell or rsyncd connection,
968 * and call the appropriate connection function, then run_client.
0b4af330
MP
969 *
970 * Calls either start_socket_client (for sockets) or do_cmd and
971 * client_run (for ssh).
c1a04ecb 972 **/
fc8a6b97 973static int start_client(int argc, char *argv[])
5d6bcd44
AT
974{
975 char *p;
976 char *shell_machine = NULL;
977 char *shell_path = NULL;
978 char *shell_user = NULL;
19b27a48
AT
979 int ret;
980 pid_t pid;
5d6bcd44 981 int f_in,f_out;
7169bb4a
MP
982 int rc;
983
984 /* Don't clobber argv[] so that ps(1) can still show the right
d9c7edf6 985 * command line. */
75aeac44 986 if ((rc = copy_argv(argv)))
7169bb4a 987 return rc;
5d6bcd44 988
d16c245f 989 if (!read_batch) { /* for read_batch, NO source is specified */
860236bf
WD
990 shell_path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
991 if (shell_path) { /* source is remote */
9425918d
WD
992 char *dummy1;
993 int dummy2;
ee8e2b15
WD
994 if (--argc
995 && check_for_hostspec(argv[argc], &dummy1, &dummy2)) {
9425918d
WD
996 rprintf(FERROR,
997 "The source and destination cannot both be remote.\n");
998 exit_cleanup(RERR_SYNTAX);
999 }
42ccb4c0 1000 argv++;
860236bf
WD
1001 if (filesfrom_host && *filesfrom_host
1002 && strcmp(filesfrom_host, shell_machine) != 0) {
7c2a9e76 1003 rprintf(FERROR,
50b31539 1004 "--files-from hostname is not the same as the transfer hostname\n");
7c2a9e76
WD
1005 exit_cleanup(RERR_SYNTAX);
1006 }
860236bf 1007 if (rsync_port) {
d9c7edf6 1008 if (!shell_cmd) {
860236bf
WD
1009 return start_socket_client(shell_machine,
1010 shell_path,
42ccb4c0 1011 argc, argv);
d9c7edf6 1012 }
d9c7edf6 1013 daemon_over_rsh = 1;
75aeac44 1014 }
3591c066 1015
42ccb4c0
WD
1016 am_sender = 0;
1017 } else { /* source is local, check dest arg */
1018 am_sender = 1;
1019
ee8e2b15
WD
1020 if (argc > 1)
1021 p = argv[--argc];
1022 else {
1023 p = ".";
1024 list_only = 1;
d9c7edf6 1025 }
a125c82a 1026
ee8e2b15 1027 shell_path = check_for_hostspec(p, &shell_machine, &rsync_port);
860236bf
WD
1028 if (shell_path && filesfrom_host && *filesfrom_host
1029 && strcmp(filesfrom_host, shell_machine) != 0) {
7c2a9e76 1030 rprintf(FERROR,
50b31539 1031 "--files-from hostname is not the same as the transfer hostname\n");
7c2a9e76
WD
1032 exit_cleanup(RERR_SYNTAX);
1033 }
860236bf 1034 if (!shell_path) { /* no hostspec found, so src & dest are local */
d9c7edf6 1035 local_server = 1;
860236bf 1036 if (filesfrom_host) {
7c2a9e76 1037 rprintf(FERROR,
50b31539 1038 "--files-from cannot be remote when the transfer is local\n");
7c2a9e76
WD
1039 exit_cleanup(RERR_SYNTAX);
1040 }
860236bf 1041 shell_machine = NULL;
ee8e2b15 1042 shell_path = p;
860236bf 1043 } else if (rsync_port) {
d9c7edf6 1044 if (!shell_cmd) {
860236bf
WD
1045 return start_socket_client(shell_machine,
1046 shell_path,
42ccb4c0 1047 argc, argv);
d9c7edf6 1048 }
d9c7edf6 1049 daemon_over_rsh = 1;
a125c82a 1050 }
5d6bcd44 1051 }
d16c245f 1052 } else { /* read_batch */
d9c7edf6
WD
1053 local_server = 1;
1054 shell_path = argv[argc-1];
860236bf 1055 if (check_for_hostspec(shell_path, &shell_machine, &rsync_port)) {
b9f592fb
WD
1056 rprintf(FERROR, "remote destination is not allowed with --read-batch\n");
1057 exit_cleanup(RERR_SYNTAX);
1058 }
6902ed17
MP
1059 }
1060
5d6bcd44 1061 if (shell_machine) {
6fc048f4 1062 p = strrchr(shell_machine,'@');
5d6bcd44
AT
1063 if (p) {
1064 *p = 0;
1065 shell_user = shell_machine;
1066 shell_machine = p+1;
1067 }
1068 }
1069
1070 if (verbose > 3) {
9486289c 1071 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
45c49b52
WD
1072 shell_cmd ? shell_cmd : "",
1073 shell_machine ? shell_machine : "",
1074 shell_user ? shell_user : "",
1075 shell_path ? shell_path : "");
5d6bcd44 1076 }
d9c7edf6 1077
d16c245f 1078 /* for remote source, only single dest arg can remain ... */
f7632fc6 1079 if (!am_sender && argc > 1) {
5d6bcd44 1080 usage(FERROR);
65417579 1081 exit_cleanup(RERR_SYNTAX);
5d6bcd44 1082 }
27e3e9c9 1083
d16c245f 1084 /* ... or no dest at all */
48a1ff0d
WD
1085 if (!am_sender && argc == 0)
1086 list_only |= 1;
d9c7edf6 1087
75aeac44
WD
1088 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,
1089 &f_in,&f_out);
1090
1091 /* if we're running an rsync server on the remote host over a
9af87151 1092 * remote shell command, we need to do the RSYNCD protocol first */
75aeac44
WD
1093 if (daemon_over_rsh) {
1094 int tmpret;
1095 tmpret = start_inband_exchange(shell_user, shell_path,
1096 f_in, f_out, argc);
1097 if (tmpret < 0)
1098 return tmpret;
1099 }
1100
fc8a6b97
AT
1101 ret = client_run(f_in, f_out, pid, argc, argv);
1102
1103 fflush(stdout);
1104 fflush(stderr);
1105
1106 return ret;
5d6bcd44
AT
1107}
1108
366345fe 1109
067669da
WD
1110static RETSIGTYPE sigusr1_handler(UNUSED(int val))
1111{
6bf32edb 1112 exit_cleanup(RERR_SIGNAL1);
82306bf6
AT
1113}
1114
067669da
WD
1115static RETSIGTYPE sigusr2_handler(UNUSED(int val))
1116{
33c4b445
WD
1117 if (!am_server)
1118 output_summary();
40df65fd 1119 close_all();
33c4b445
WD
1120 if (log_got_error)
1121 _exit(RERR_PARTIAL);
8b35435f
AT
1122 _exit(0);
1123}
1124
6d59ac19 1125RETSIGTYPE remember_children(UNUSED(int val))
067669da 1126{
029c1713 1127#ifdef WNOHANG
ee7118a8
DD
1128 int cnt, status;
1129 pid_t pid;
1130 /* An empty waitpid() loop was put here by Tridge and we could never
d9c7edf6 1131 * get him to explain why he put it in, so rather than taking it
ee7118a8
DD
1132 * out we're instead saving the child exit statuses for later use.
1133 * The waitpid() loop presumably eliminates all possibility of leaving
97d8e709 1134 * zombie children, maybe that's why he did it. */
ee7118a8 1135 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
9af87151
WD
1136 /* save the child's exit status */
1137 for (cnt = 0; cnt < MAXCHILDPROCS; cnt++) {
1138 if (pid_stat_table[cnt].pid == 0) {
1139 pid_stat_table[cnt].pid = pid;
1140 pid_stat_table[cnt].status = status;
1141 break;
1142 }
1143 }
ee7118a8 1144 }
029c1713 1145#endif
8261af74 1146#ifndef HAVE_SIGACTION
6d59ac19 1147 signal(SIGCHLD, remember_children);
60ee01f5 1148#endif
19b27a48
AT
1149}
1150
c0531332
MP
1151
1152/**
1153 * This routine catches signals and tries to send them to gdb.
1154 *
1155 * Because it's called from inside a signal handler it ought not to
1156 * use too many library routines.
1157 *
1158 * @todo Perhaps use "screen -X" instead/as well, to help people
1159 * debugging without easy access to X. Perhaps use an environment
1160 * variable, or just call a script?
1161 *
1162 * @todo The /proc/ magic probably only works on Linux (and
1163 * Solaris?) Can we be more portable?
1164 **/
1165#ifdef MAINTAINER_MODE
4fdc39dd
MP
1166const char *get_panic_action(void)
1167{
1168 const char *cmd_fmt = getenv("RSYNC_PANIC_ACTION");
1169
1170 if (cmd_fmt)
1171 return cmd_fmt;
1172 else
1173 return "xterm -display :0 -T Panic -n Panic "
1174 "-e gdb /proc/%d/exe %d";
1175}
1176
1177
9fb3f7a9
MP
1178/**
1179 * Handle a fatal signal by launching a debugger, controlled by $RSYNC_PANIC_ACTION.
1180 *
1181 * This signal handler is only installed if we were configured with
1182 * --enable-maintainer-mode. Perhaps it should always be on and we
1183 * should just look at the environment variable, but I'm a bit leery
1184 * of a signal sending us into a busy loop.
1185 **/
067669da 1186static RETSIGTYPE rsync_panic_handler(UNUSED(int whatsig))
c0531332
MP
1187{
1188 char cmd_buf[300];
1189 int ret;
4fdc39dd
MP
1190
1191 sprintf(cmd_buf, get_panic_action(),
c0531332
MP
1192 getpid(), getpid());
1193
1194 /* Unless we failed to execute gdb, we allow the process to
1195 * continue. I'm not sure if that's right. */
1196 ret = system(cmd_buf);
1197 if (ret)
1198 _exit(ret);
1199}
1200#endif
1201
1202
5d6bcd44 1203int main(int argc,char *argv[])
d9c7edf6 1204{
ff81e809 1205 int ret;
66a9dc96
WD
1206 int orig_argc = argc;
1207 char **orig_argv = argv;
8261af74
WD
1208#ifdef HAVE_SIGACTION
1209# ifdef HAVE_SIGPROCMASK
60ee01f5 1210 sigset_t sigmask;
5d6bcd44 1211
60ee01f5 1212 sigemptyset(&sigmask);
8261af74 1213# endif
60ee01f5
WD
1214 sigact.sa_flags = SA_NOCLDSTOP;
1215#endif
1216 SIGACTMASK(SIGUSR1, sigusr1_handler);
1217 SIGACTMASK(SIGUSR2, sigusr2_handler);
6d59ac19 1218 SIGACTMASK(SIGCHLD, remember_children);
c0531332 1219#ifdef MAINTAINER_MODE
60ee01f5
WD
1220 SIGACTMASK(SIGSEGV, rsync_panic_handler);
1221 SIGACTMASK(SIGFPE, rsync_panic_handler);
1222 SIGACTMASK(SIGABRT, rsync_panic_handler);
1223 SIGACTMASK(SIGBUS, rsync_panic_handler);
1224#endif
5d6bcd44 1225
7a6421fa 1226 starttime = time(NULL);
6fe05820 1227 am_root = (MY_UID() == 0);
c627d613 1228
a800434a
AT
1229 memset(&stats, 0, sizeof(stats));
1230
df5e03da
AT
1231 if (argc < 2) {
1232 usage(FERROR);
65417579 1233 exit_cleanup(RERR_SYNTAX);
df5e03da
AT
1234 }
1235
7a6421fa 1236 /* we set a 0 umask so that correct file permissions can be
9af87151 1237 * carried across */
05278935 1238 orig_umask = umask(0);
5d6bcd44 1239
eb598fac
WD
1240#if defined CONFIG_LOCALE && defined HAVE_SETLOCALE
1241 setlocale(LC_CTYPE, "");
1242#endif
1243
50135767 1244 if (!parse_arguments(&argc, (const char ***) &argv, 1)) {
d9c7edf6
WD
1245 /* FIXME: We ought to call the same error-handling
1246 * code here, rather than relying on getopt. */
50135767 1247 option_error();
65417579 1248 exit_cleanup(RERR_SYNTAX);
b11ed3b1 1249 }
5d6bcd44 1250
60ee01f5
WD
1251 SIGACTMASK(SIGINT, sig_int);
1252 SIGACTMASK(SIGHUP, sig_int);
1253 SIGACTMASK(SIGTERM, sig_int);
1254#if defined HAVE_SIGACTION && HAVE_SIGPROCMASK
1255 sigprocmask(SIG_UNBLOCK, &sigmask, NULL);
1256#endif
6b83141d 1257
34758d5c
MP
1258 /* Ignore SIGPIPE; we consistently check error codes and will
1259 * see the EPIPE. */
60ee01f5 1260 SIGACTION(SIGPIPE, SIG_IGN);
55e54e46
WD
1261#ifdef SIGXFSZ
1262 SIGACTION(SIGXFSZ, SIG_IGN);
1263#endif
34758d5c 1264
c226b7c2 1265 /* Initialize push_dir here because on some old systems getcwd
9af87151
WD
1266 * (implemented by forking "pwd" and reading its output) doesn't
1267 * work when there are other child processes. Also, on all systems
1268 * that implement getcwd that way "pwd" can't be found after chroot. */
59187666 1269 push_dir(NULL);
c226b7c2 1270
44e9e221
WD
1271 init_flist();
1272
e8a96e27 1273 if ((write_batch || read_batch) && !am_server) {
b9f592fb 1274 if (write_batch)
66a9dc96 1275 write_batch_shell_file(orig_argc, orig_argv, argc);
b9f592fb 1276
dbbab0c4
WD
1277 if (read_batch && strcmp(batch_name, "-") == 0)
1278 batch_fd = STDIN_FILENO;
1279 else {
1280 batch_fd = do_open(batch_name,
b9f592fb
WD
1281 write_batch ? O_WRONLY | O_CREAT | O_TRUNC
1282 : O_RDONLY, S_IRUSR | S_IWUSR);
dbbab0c4 1283 }
b9f592fb
WD
1284 if (batch_fd < 0) {
1285 rsyserr(FERROR, errno, "Batch file %s open error",
71903f60 1286 full_fname(batch_name));
b9f592fb
WD
1287 exit_cleanup(RERR_FILEIO);
1288 }
9459290a
WD
1289 if (read_batch)
1290 read_stream_flags(batch_fd);
6902ed17 1291 }
e8a96e27
WD
1292 if (write_batch < 0)
1293 dry_run = 1;
6902ed17 1294
75aeac44 1295 if (am_daemon && !am_server)
7a6421fa 1296 return daemon_main();
f0fca04e 1297
08ac228f
AT
1298 if (argc < 1) {
1299 usage(FERROR);
65417579 1300 exit_cleanup(RERR_SYNTAX);
08ac228f
AT
1301 }
1302
7a6421fa 1303 if (am_server) {
f0359dd0
AT
1304 set_nonblocking(STDIN_FILENO);
1305 set_nonblocking(STDOUT_FILENO);
75aeac44
WD
1306 if (am_daemon)
1307 return start_daemon(STDIN_FILENO, STDOUT_FILENO);
7a6421fa
AT
1308 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
1309 }
c627d613 1310
ff81e809 1311 ret = start_client(argc, argv);
d9c7edf6 1312 if (ret == -1)
9098bbf3 1313 exit_cleanup(RERR_STARTCLIENT);
088aac85 1314 else
9098bbf3
MP
1315 exit_cleanup(ret);
1316
0f5a04e3 1317 return ret;
c627d613 1318}