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