Commit | Line | Data |
---|---|---|
0ba48136 MP |
1 | /* -*- c-file-style: "linux" -*- |
2 | ||
50135767 | 3 | Copyright (C) 1996-2001 by Andrew Tridgell <tridge@samba.org> |
c627d613 AT |
4 | Copyright (C) Paul Mackerras 1996 |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
21 | #include "rsync.h" | |
22 | ||
f0fca04e | 23 | time_t starttime = 0; |
a800434a AT |
24 | |
25 | struct stats stats; | |
5d6bcd44 | 26 | |
7a6421fa | 27 | extern int verbose; |
c627d613 | 28 | |
82980a23 AT |
29 | |
30 | /**************************************************************************** | |
31 | wait for a process to exit, calling io_flush while waiting | |
32 | ****************************************************************************/ | |
33 | void wait_process(pid_t pid, int *status) | |
34 | { | |
35 | while (waitpid(pid, status, WNOHANG) == 0) { | |
a24c6870 | 36 | msleep(20); |
82980a23 AT |
37 | io_flush(); |
38 | } | |
50135767 MP |
39 | |
40 | /* TODO: If the child exited on a signal, then log an | |
41 | * appropriate error message. Perhaps we should also accept a | |
42 | * message describing the purpose of the child. Also indicate | |
43 | * this to the caller so that thhey know something went | |
44 | * wrong. */ | |
82980a23 AT |
45 | *status = WEXITSTATUS(*status); |
46 | } | |
47 | ||
c627d613 AT |
48 | static void report(int f) |
49 | { | |
7a6421fa AT |
50 | time_t t = time(NULL); |
51 | extern int am_server; | |
52 | extern int am_sender; | |
248fbb8c | 53 | extern int am_daemon; |
a800434a | 54 | extern int do_stats; |
17d31b38 DD |
55 | extern int remote_version; |
56 | int send_stats; | |
7a6421fa | 57 | |
248fbb8c | 58 | if (am_daemon) { |
a9766ef1 | 59 | log_exit(0, __FILE__, __LINE__); |
7b372642 | 60 | if (f == -1 || !am_sender) return; |
248fbb8c AT |
61 | } |
62 | ||
ebb00c8e | 63 | send_stats = verbose || (remote_version >= 20); |
e19452a9 | 64 | if (am_server) { |
17d31b38 | 65 | if (am_sender && send_stats) { |
23c5aef1 DD |
66 | int64 w; |
67 | /* store total_written in a temporary | |
68 | because write_longint changes it */ | |
69 | w = stats.total_written; | |
e19452a9 | 70 | write_longint(f,stats.total_read); |
23c5aef1 | 71 | write_longint(f,w); |
e19452a9 DD |
72 | write_longint(f,stats.total_size); |
73 | } | |
7a6421fa AT |
74 | return; |
75 | } | |
e19452a9 DD |
76 | |
77 | /* this is the client */ | |
78 | ||
17d31b38 | 79 | if (!am_sender && send_stats) { |
23c5aef1 | 80 | int64 r; |
a800434a | 81 | stats.total_written = read_longint(f); |
23c5aef1 DD |
82 | /* store total_read in a temporary, read_longint changes it */ |
83 | r = read_longint(f); | |
a800434a | 84 | stats.total_size = read_longint(f); |
23c5aef1 | 85 | stats.total_read = r; |
a800434a AT |
86 | } |
87 | ||
88 | if (do_stats) { | |
17d31b38 DD |
89 | if (!am_sender && !send_stats) { |
90 | /* missing the bytes written by the generator */ | |
ebb00c8e | 91 | rprintf(FINFO, "\nCannot show stats as receiver because remote protocol version is less than 20\n"); |
17d31b38 DD |
92 | rprintf(FINFO, "Use --stats -v to show stats\n"); |
93 | return; | |
94 | } | |
1f658d42 AT |
95 | rprintf(FINFO,"\nNumber of files: %d\n", stats.num_files); |
96 | rprintf(FINFO,"Number of files transferred: %d\n", | |
a800434a | 97 | stats.num_transferred_files); |
1f658d42 | 98 | rprintf(FINFO,"Total file size: %.0f bytes\n", |
a800434a | 99 | (double)stats.total_size); |
1f658d42 | 100 | rprintf(FINFO,"Total transferred file size: %.0f bytes\n", |
a800434a | 101 | (double)stats.total_transferred_size); |
1f658d42 | 102 | rprintf(FINFO,"Literal data: %.0f bytes\n", |
a800434a | 103 | (double)stats.literal_data); |
1f658d42 | 104 | rprintf(FINFO,"Matched data: %.0f bytes\n", |
a800434a | 105 | (double)stats.matched_data); |
1f658d42 AT |
106 | rprintf(FINFO,"File list size: %d\n", stats.flist_size); |
107 | rprintf(FINFO,"Total bytes written: %.0f\n", | |
a800434a | 108 | (double)stats.total_written); |
1f658d42 | 109 | rprintf(FINFO,"Total bytes read: %.0f\n\n", |
a800434a | 110 | (double)stats.total_read); |
7a6421fa AT |
111 | } |
112 | ||
e19452a9 DD |
113 | if (verbose || do_stats) { |
114 | rprintf(FINFO,"wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n", | |
115 | (double)stats.total_written, | |
116 | (double)stats.total_read, | |
117 | (stats.total_written+stats.total_read)/(0.5 + (t-starttime))); | |
118 | rprintf(FINFO,"total size is %.0f speedup is %.2f\n", | |
119 | (double)stats.total_size, | |
120 | (1.0*stats.total_size)/(stats.total_written+stats.total_read)); | |
121 | } | |
fc8a6b97 AT |
122 | |
123 | fflush(stdout); | |
124 | fflush(stderr); | |
c627d613 AT |
125 | } |
126 | ||
127 | ||
0882faa2 | 128 | /* Start the remote shell. cmd may be NULL to use the default. */ |
19b27a48 | 129 | static pid_t do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out) |
c627d613 | 130 | { |
366345fe | 131 | char *args[100]; |
19b27a48 AT |
132 | int i,argc=0; |
133 | pid_t ret; | |
366345fe | 134 | char *tok,*dir=NULL; |
7a6421fa AT |
135 | extern int local_server; |
136 | extern char *rsync_path; | |
e384bfbd | 137 | extern int blocking_io; |
366345fe AT |
138 | |
139 | if (!local_server) { | |
140 | if (!cmd) | |
141 | cmd = getenv(RSYNC_RSH_ENV); | |
142 | if (!cmd) | |
143 | cmd = RSYNC_RSH; | |
144 | cmd = strdup(cmd); | |
145 | if (!cmd) | |
146 | goto oom; | |
147 | ||
148 | for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) { | |
149 | args[argc++] = tok; | |
150 | } | |
c627d613 | 151 | |
7b8356d0 | 152 | #if HAVE_REMSH |
366345fe AT |
153 | /* remsh (on HPUX) takes the arguments the other way around */ |
154 | args[argc++] = machine; | |
155 | if (user) { | |
156 | args[argc++] = "-l"; | |
157 | args[argc++] = user; | |
158 | } | |
7b8356d0 | 159 | #else |
366345fe AT |
160 | if (user) { |
161 | args[argc++] = "-l"; | |
162 | args[argc++] = user; | |
163 | } | |
164 | args[argc++] = machine; | |
7b8356d0 | 165 | #endif |
c627d613 | 166 | |
366345fe | 167 | args[argc++] = rsync_path; |
c627d613 | 168 | |
366345fe | 169 | server_options(args,&argc); |
e384bfbd AT |
170 | |
171 | ||
172 | if (strcmp(cmd, RSYNC_RSH) == 0) blocking_io = 1; | |
366345fe | 173 | } |
c627d613 | 174 | |
366345fe | 175 | args[argc++] = "."; |
76076c4b | 176 | |
366345fe AT |
177 | if (path && *path) |
178 | args[argc++] = path; | |
c627d613 | 179 | |
366345fe | 180 | args[argc] = NULL; |
c627d613 | 181 | |
366345fe | 182 | if (verbose > 3) { |
9486289c | 183 | rprintf(FINFO,"cmd="); |
366345fe | 184 | for (i=0;i<argc;i++) |
9486289c AT |
185 | rprintf(FINFO,"%s ",args[i]); |
186 | rprintf(FINFO,"\n"); | |
366345fe AT |
187 | } |
188 | ||
189 | if (local_server) { | |
190 | ret = local_child(argc, args, f_in, f_out); | |
191 | } else { | |
192 | ret = piped_child(args,f_in,f_out); | |
193 | } | |
c627d613 | 194 | |
366345fe | 195 | if (dir) free(dir); |
82306bf6 | 196 | |
366345fe | 197 | return ret; |
c627d613 AT |
198 | |
199 | oom: | |
366345fe AT |
200 | out_of_memory("do_cmd"); |
201 | return 0; /* not reached */ | |
c627d613 AT |
202 | } |
203 | ||
204 | ||
205 | ||
206 | ||
207 | static char *get_local_name(struct file_list *flist,char *name) | |
208 | { | |
7a6421fa AT |
209 | STRUCT_STAT st; |
210 | extern int orig_umask; | |
c627d613 | 211 | |
c95da96a AT |
212 | if (verbose > 2) |
213 | rprintf(FINFO,"get_local_name count=%d %s\n", | |
1f0610ef DD |
214 | flist->count, NS(name)); |
215 | ||
216 | if (!name) | |
217 | return NULL; | |
c95da96a | 218 | |
1ff5450d AT |
219 | if (do_stat(name,&st) == 0) { |
220 | if (S_ISDIR(st.st_mode)) { | |
5243c216 AT |
221 | if (!push_dir(name, 0)) { |
222 | rprintf(FERROR,"push_dir %s : %s (1)\n", | |
1ff5450d | 223 | name,strerror(errno)); |
65417579 | 224 | exit_cleanup(RERR_FILESELECT); |
1ff5450d AT |
225 | } |
226 | return NULL; | |
227 | } | |
228 | if (flist->count > 1) { | |
229 | rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); | |
65417579 | 230 | exit_cleanup(RERR_FILESELECT); |
1ff5450d AT |
231 | } |
232 | return name; | |
233 | } | |
234 | ||
cec8aa77 | 235 | if (flist->count <= 1) |
1ff5450d AT |
236 | return name; |
237 | ||
1ff5450d AT |
238 | if (do_mkdir(name,0777 & ~orig_umask) != 0) { |
239 | rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); | |
65417579 | 240 | exit_cleanup(RERR_FILEIO); |
1ff5450d | 241 | } else { |
b536f47e AT |
242 | if (verbose > 0) |
243 | rprintf(FINFO,"created directory %s\n",name); | |
1ff5450d AT |
244 | } |
245 | ||
5243c216 AT |
246 | if (!push_dir(name, 0)) { |
247 | rprintf(FERROR,"push_dir %s : %s (2)\n", | |
248 | name,strerror(errno)); | |
65417579 | 249 | exit_cleanup(RERR_FILESELECT); |
1ff5450d AT |
250 | } |
251 | ||
252 | return NULL; | |
c627d613 AT |
253 | } |
254 | ||
255 | ||
256 | ||
257 | ||
9486289c | 258 | static void do_server_sender(int f_in, int f_out, int argc,char *argv[]) |
c627d613 | 259 | { |
7a6421fa AT |
260 | int i; |
261 | struct file_list *flist; | |
262 | char *dir = argv[0]; | |
263 | extern int relative_paths; | |
7a6421fa | 264 | extern int recurse; |
adc19c98 | 265 | extern int remote_version; |
c627d613 | 266 | |
7a6421fa AT |
267 | if (verbose > 2) |
268 | rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid()); | |
c627d613 | 269 | |
5243c216 AT |
270 | if (!relative_paths && !push_dir(dir, 0)) { |
271 | rprintf(FERROR,"push_dir %s: %s (3)\n",dir,strerror(errno)); | |
65417579 | 272 | exit_cleanup(RERR_FILESELECT); |
7a6421fa AT |
273 | } |
274 | argc--; | |
275 | argv++; | |
c627d613 | 276 | |
7a6421fa AT |
277 | if (strcmp(dir,".")) { |
278 | int l = strlen(dir); | |
279 | if (strcmp(dir,"/") == 0) | |
280 | l = 0; | |
281 | for (i=0;i<argc;i++) | |
282 | argv[i] += l+1; | |
283 | } | |
c627d613 | 284 | |
7a6421fa AT |
285 | if (argc == 0 && recurse) { |
286 | argc=1; | |
287 | argv--; | |
288 | argv[0] = "."; | |
289 | } | |
290 | ||
7a6421fa | 291 | flist = send_file_list(f_out,argc,argv); |
8d9dc9f9 AT |
292 | if (!flist || flist->count == 0) { |
293 | exit_cleanup(0); | |
294 | } | |
295 | ||
7a6421fa | 296 | send_files(flist,f_out,f_in); |
3d382777 | 297 | io_flush(); |
7a6421fa | 298 | report(f_out); |
adc19c98 AT |
299 | if (remote_version >= 24) { |
300 | /* final goodbye message */ | |
301 | read_int(f_in); | |
302 | } | |
8d9dc9f9 | 303 | io_flush(); |
7a6421fa | 304 | exit_cleanup(0); |
c627d613 AT |
305 | } |
306 | ||
307 | ||
dc5ddbcc AT |
308 | static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) |
309 | { | |
d186eb1a AT |
310 | int pid; |
311 | int status=0; | |
312 | int recv_pipe[2]; | |
554e0a8d | 313 | int error_pipe[2]; |
d186eb1a | 314 | extern int preserve_hard_links; |
6957ae33 AT |
315 | extern int delete_after; |
316 | extern int recurse; | |
317 | extern int delete_mode; | |
8ada7518 | 318 | extern int remote_version; |
dc5ddbcc | 319 | |
d186eb1a AT |
320 | if (preserve_hard_links) |
321 | init_hard_links(flist); | |
dc5ddbcc | 322 | |
6957ae33 AT |
323 | if (!delete_after) { |
324 | /* I moved this here from recv_files() to prevent a race condition */ | |
325 | if (recurse && delete_mode && !local_name && flist->count>0) { | |
326 | delete_files(flist); | |
327 | } | |
328 | } | |
329 | ||
08f15335 | 330 | if (fd_pair(recv_pipe) < 0) { |
d186eb1a | 331 | rprintf(FERROR,"pipe failed in do_recv\n"); |
65417579 | 332 | exit_cleanup(RERR_SOCKETIO); |
d186eb1a | 333 | } |
554e0a8d | 334 | |
08f15335 | 335 | if (fd_pair(error_pipe) < 0) { |
554e0a8d AT |
336 | rprintf(FERROR,"error pipe failed in do_recv\n"); |
337 | exit_cleanup(RERR_SOCKETIO); | |
338 | } | |
c6e7fcb4 | 339 | |
8d9dc9f9 | 340 | io_flush(); |
c6e7fcb4 | 341 | |
d186eb1a | 342 | if ((pid=do_fork()) == 0) { |
e08c9610 | 343 | close(recv_pipe[0]); |
554e0a8d | 344 | close(error_pipe[0]); |
e08c9610 AT |
345 | if (f_in != f_out) close(f_out); |
346 | ||
554e0a8d AT |
347 | /* we can't let two processes write to the socket at one time */ |
348 | io_multiplexing_close(); | |
349 | ||
350 | /* set place to send errors */ | |
351 | set_error_fd(error_pipe[1]); | |
352 | ||
e08c9610 | 353 | recv_files(f_in,flist,local_name,recv_pipe[1]); |
3d382777 | 354 | io_flush(); |
ba5e128d | 355 | report(f_in); |
e08c9610 | 356 | |
8b35435f AT |
357 | write_int(recv_pipe[1],1); |
358 | close(recv_pipe[1]); | |
8d9dc9f9 | 359 | io_flush(); |
4a748188 | 360 | /* finally we go to sleep until our parent kills us |
27e3e9c9 | 361 | with a USR2 signal. We sleep for a short time as on |
4a748188 | 362 | some OSes a signal won't interrupt a sleep! */ |
27e3e9c9 | 363 | while (1) msleep(20); |
d186eb1a | 364 | } |
dc5ddbcc | 365 | |
e08c9610 | 366 | close(recv_pipe[1]); |
554e0a8d | 367 | close(error_pipe[1]); |
e08c9610 | 368 | if (f_in != f_out) close(f_in); |
e1b3d5c4 | 369 | |
b3e10ed7 AT |
370 | io_start_buffering(f_out); |
371 | ||
554e0a8d AT |
372 | io_set_error_fd(error_pipe[0]); |
373 | ||
e08c9610 | 374 | generate_files(f_out,flist,local_name,recv_pipe[0]); |
8d9dc9f9 | 375 | |
8b35435f AT |
376 | read_int(recv_pipe[0]); |
377 | close(recv_pipe[0]); | |
8ada7518 AT |
378 | if (remote_version >= 24) { |
379 | /* send a final goodbye message */ | |
380 | write_int(f_out, -1); | |
381 | } | |
8d9dc9f9 | 382 | io_flush(); |
8ada7518 | 383 | |
8b35435f | 384 | kill(pid, SIGUSR2); |
d79d1c69 | 385 | wait_process(pid, &status); |
d186eb1a | 386 | return status; |
dc5ddbcc AT |
387 | } |
388 | ||
c627d613 | 389 | |
9486289c | 390 | static void do_server_recv(int f_in, int f_out, int argc,char *argv[]) |
c627d613 | 391 | { |
7a6421fa AT |
392 | int status; |
393 | struct file_list *flist; | |
394 | char *local_name=NULL; | |
395 | char *dir = NULL; | |
396 | extern int delete_mode; | |
b33b791e | 397 | extern int delete_excluded; |
7a6421fa | 398 | extern int am_daemon; |
09b7f5db AT |
399 | extern int module_id; |
400 | extern int am_sender; | |
f0fca04e | 401 | |
7a6421fa AT |
402 | if (verbose > 2) |
403 | rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid()); | |
09b7f5db AT |
404 | |
405 | if (am_daemon && lp_read_only(module_id) && !am_sender) { | |
406 | rprintf(FERROR,"ERROR: module is read only\n"); | |
407 | exit_cleanup(RERR_SYNTAX); | |
408 | return; | |
409 | } | |
410 | ||
7a6421fa | 411 | |
7a6421fa AT |
412 | if (argc > 0) { |
413 | dir = argv[0]; | |
414 | argc--; | |
415 | argv++; | |
5243c216 AT |
416 | if (!am_daemon && !push_dir(dir, 0)) { |
417 | rprintf(FERROR,"push_dir %s : %s (4)\n", | |
7a6421fa | 418 | dir,strerror(errno)); |
65417579 | 419 | exit_cleanup(RERR_FILESELECT); |
7a6421fa AT |
420 | } |
421 | } | |
c627d613 | 422 | |
b33b791e | 423 | if (delete_mode && !delete_excluded) |
7a6421fa | 424 | recv_exclude_list(f_in); |
c627d613 | 425 | |
7a6421fa | 426 | flist = recv_file_list(f_in); |
4c36a13e AT |
427 | if (!flist) { |
428 | rprintf(FERROR,"server_recv: recv_file_list error\n"); | |
65417579 | 429 | exit_cleanup(RERR_FILESELECT); |
7a6421fa AT |
430 | } |
431 | ||
432 | if (argc > 0) { | |
433 | if (strcmp(dir,".")) { | |
434 | argv[0] += strlen(dir); | |
435 | if (argv[0][0] == '/') argv[0]++; | |
436 | } | |
437 | local_name = get_local_name(flist,argv[0]); | |
438 | } | |
c627d613 | 439 | |
7a6421fa AT |
440 | status = do_recv(f_in,f_out,flist,local_name); |
441 | exit_cleanup(status); | |
c627d613 AT |
442 | } |
443 | ||
444 | ||
9486289c | 445 | void start_server(int f_in, int f_out, int argc, char *argv[]) |
366345fe | 446 | { |
7a6421fa AT |
447 | extern int cvs_exclude; |
448 | extern int am_sender; | |
ff41a59f AT |
449 | extern int remote_version; |
450 | ||
6d7b6081 AT |
451 | setup_protocol(f_out, f_in); |
452 | ||
f0359dd0 AT |
453 | set_nonblocking(f_in); |
454 | set_nonblocking(f_out); | |
455 | ||
ff41a59f AT |
456 | if (remote_version >= 23) |
457 | io_start_multiplex_out(f_out); | |
7a6421fa | 458 | |
7a6421fa AT |
459 | if (am_sender) { |
460 | recv_exclude_list(f_in); | |
461 | if (cvs_exclude) | |
462 | add_cvs_excludes(); | |
463 | do_server_sender(f_in, f_out, argc, argv); | |
464 | } else { | |
465 | do_server_recv(f_in, f_out, argc, argv); | |
466 | } | |
467 | exit_cleanup(0); | |
366345fe AT |
468 | } |
469 | ||
0ba48136 MP |
470 | |
471 | /* | |
472 | * This is called once the connection has been negotiated. It is used | |
473 | * for rsyncd, remote-shell, and local connections. | |
474 | */ | |
19b27a48 | 475 | int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[]) |
9486289c AT |
476 | { |
477 | struct file_list *flist; | |
478 | int status = 0, status2 = 0; | |
479 | char *local_name = NULL; | |
7a6421fa | 480 | extern int am_sender; |
ff41a59f | 481 | extern int remote_version; |
19b27a48 AT |
482 | extern pid_t cleanup_child_pid; |
483 | ||
484 | cleanup_child_pid = pid; | |
ff41a59f | 485 | |
f0359dd0 AT |
486 | set_nonblocking(f_in); |
487 | set_nonblocking(f_out); | |
488 | ||
6d7b6081 AT |
489 | setup_protocol(f_out,f_in); |
490 | ||
ff41a59f AT |
491 | if (remote_version >= 23) |
492 | io_start_multiplex_in(f_in); | |
9486289c AT |
493 | |
494 | if (am_sender) { | |
7a6421fa AT |
495 | extern int cvs_exclude; |
496 | extern int delete_mode; | |
b33b791e | 497 | extern int delete_excluded; |
9486289c AT |
498 | if (cvs_exclude) |
499 | add_cvs_excludes(); | |
b33b791e | 500 | if (delete_mode && !delete_excluded) |
9486289c AT |
501 | send_exclude_list(f_out); |
502 | flist = send_file_list(f_out,argc,argv); | |
503 | if (verbose > 3) | |
504 | rprintf(FINFO,"file list sent\n"); | |
e1b3d5c4 | 505 | |
9486289c AT |
506 | send_files(flist,f_out,f_in); |
507 | if (pid != -1) { | |
508 | if (verbose > 3) | |
8d9dc9f9 AT |
509 | rprintf(FINFO,"client_run waiting on %d\n",pid); |
510 | io_flush(); | |
d79d1c69 | 511 | wait_process(pid, &status); |
9486289c | 512 | } |
8ada7518 AT |
513 | if (remote_version >= 24) { |
514 | /* final goodbye message */ | |
515 | read_int(f_in); | |
516 | } | |
3d382777 | 517 | report(-1); |
9486289c AT |
518 | exit_cleanup(status); |
519 | } | |
f7632fc6 | 520 | |
27e3e9c9 AT |
521 | if (argc == 0) { |
522 | extern int list_only; | |
523 | list_only = 1; | |
524 | } | |
9486289c AT |
525 | |
526 | send_exclude_list(f_out); | |
527 | ||
528 | flist = recv_file_list(f_in); | |
529 | if (!flist || flist->count == 0) { | |
796d484b MP |
530 | rprintf(FINFO, "client: nothing to do: " |
531 | "perhaps you need to specify some filenames or " | |
532 | "the --recursive option?\n"); | |
9486289c AT |
533 | exit_cleanup(0); |
534 | } | |
535 | ||
536 | local_name = get_local_name(flist,argv[0]); | |
537 | ||
538 | status2 = do_recv(f_in,f_out,flist,local_name); | |
539 | ||
9486289c | 540 | if (pid != -1) { |
8d9dc9f9 AT |
541 | if (verbose > 3) |
542 | rprintf(FINFO,"client_run2 waiting on %d\n",pid); | |
543 | io_flush(); | |
d79d1c69 | 544 | wait_process(pid, &status); |
9486289c AT |
545 | } |
546 | ||
ff81e809 | 547 | return MAX(status, status2); |
9486289c AT |
548 | } |
549 | ||
ca6c93f8 AT |
550 | static char *find_colon(char *s) |
551 | { | |
552 | char *p, *p2; | |
553 | ||
554 | p = strchr(s,':'); | |
555 | if (!p) return NULL; | |
556 | ||
557 | /* now check to see if there is a / in the string before the : - if there is then | |
558 | discard the colon on the assumption that the : is part of a filename */ | |
559 | p2 = strchr(s,'/'); | |
560 | if (p2 && p2 < p) return NULL; | |
561 | ||
562 | return p; | |
563 | } | |
9486289c | 564 | |
0ba48136 MP |
565 | |
566 | /* | |
567 | * Start a client for either type of remote connection. Work out | |
568 | * whether the arguments request a remote shell or rsyncd connection, | |
569 | * and call the appropriate connection function, then run_client. | |
570 | */ | |
fc8a6b97 | 571 | static int start_client(int argc, char *argv[]) |
5d6bcd44 AT |
572 | { |
573 | char *p; | |
574 | char *shell_machine = NULL; | |
575 | char *shell_path = NULL; | |
576 | char *shell_user = NULL; | |
19b27a48 AT |
577 | int ret; |
578 | pid_t pid; | |
5d6bcd44 | 579 | int f_in,f_out; |
7a6421fa AT |
580 | extern int local_server; |
581 | extern int am_sender; | |
582 | extern char *shell_cmd; | |
2acf81eb | 583 | extern int rsync_port; |
c32d0240 | 584 | char *argv0 = strdup(argv[0]); |
5d6bcd44 | 585 | |
c32d0240 | 586 | if (strncasecmp(URL_PREFIX, argv0, strlen(URL_PREFIX)) == 0) { |
f7632fc6 AT |
587 | char *host, *path; |
588 | ||
c32d0240 | 589 | host = argv0 + strlen(URL_PREFIX); |
f7632fc6 AT |
590 | p = strchr(host,'/'); |
591 | if (p) { | |
592 | *p = 0; | |
593 | path = p+1; | |
594 | } else { | |
595 | path=""; | |
596 | } | |
2acf81eb DD |
597 | p = strchr(host,':'); |
598 | if (p) { | |
599 | rsync_port = atoi(p+1); | |
600 | *p = 0; | |
601 | } | |
f7632fc6 AT |
602 | return start_socket_client(host, path, argc-1, argv+1); |
603 | } | |
604 | ||
c32d0240 | 605 | p = find_colon(argv0); |
5d6bcd44 AT |
606 | |
607 | if (p) { | |
9486289c AT |
608 | if (p[1] == ':') { |
609 | *p = 0; | |
c32d0240 | 610 | return start_socket_client(argv0, p+2, argc-1, argv+1); |
9486289c | 611 | } |
3591c066 | 612 | |
f7632fc6 | 613 | if (argc < 1) { |
3591c066 | 614 | usage(FERROR); |
65417579 | 615 | exit_cleanup(RERR_SYNTAX); |
3591c066 AT |
616 | } |
617 | ||
5d6bcd44 AT |
618 | am_sender = 0; |
619 | *p = 0; | |
38bf526f | 620 | shell_machine = argv0; |
5d6bcd44 AT |
621 | shell_path = p+1; |
622 | argc--; | |
623 | argv++; | |
624 | } else { | |
625 | am_sender = 1; | |
9486289c | 626 | |
ca6c93f8 | 627 | p = find_colon(argv[argc-1]); |
5d6bcd44 AT |
628 | if (!p) { |
629 | local_server = 1; | |
9486289c AT |
630 | } else if (p[1] == ':') { |
631 | *p = 0; | |
632 | return start_socket_client(argv[argc-1], p+2, argc-1, argv); | |
5d6bcd44 | 633 | } |
3591c066 AT |
634 | |
635 | if (argc < 2) { | |
636 | usage(FERROR); | |
65417579 | 637 | exit_cleanup(RERR_SYNTAX); |
3591c066 | 638 | } |
9486289c | 639 | |
5d6bcd44 AT |
640 | if (local_server) { |
641 | shell_machine = NULL; | |
642 | shell_path = argv[argc-1]; | |
643 | } else { | |
644 | *p = 0; | |
645 | shell_machine = argv[argc-1]; | |
646 | shell_path = p+1; | |
647 | } | |
648 | argc--; | |
649 | } | |
650 | ||
651 | if (shell_machine) { | |
652 | p = strchr(shell_machine,'@'); | |
653 | if (p) { | |
654 | *p = 0; | |
655 | shell_user = shell_machine; | |
656 | shell_machine = p+1; | |
657 | } | |
658 | } | |
659 | ||
660 | if (verbose > 3) { | |
9486289c | 661 | rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n", |
5d6bcd44 AT |
662 | shell_cmd?shell_cmd:"", |
663 | shell_machine?shell_machine:"", | |
664 | shell_user?shell_user:"", | |
665 | shell_path?shell_path:""); | |
666 | } | |
667 | ||
f7632fc6 | 668 | if (!am_sender && argc > 1) { |
5d6bcd44 | 669 | usage(FERROR); |
65417579 | 670 | exit_cleanup(RERR_SYNTAX); |
5d6bcd44 | 671 | } |
27e3e9c9 AT |
672 | |
673 | if (argc == 0 && !am_sender) { | |
674 | extern int list_only; | |
675 | list_only = 1; | |
676 | } | |
5d6bcd44 AT |
677 | |
678 | pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out); | |
679 | ||
fc8a6b97 AT |
680 | ret = client_run(f_in, f_out, pid, argc, argv); |
681 | ||
682 | fflush(stdout); | |
683 | fflush(stderr); | |
684 | ||
685 | return ret; | |
5d6bcd44 AT |
686 | } |
687 | ||
366345fe | 688 | |
6e4fb64e | 689 | static RETSIGTYPE sigusr1_handler(int val) { |
65417579 | 690 | exit_cleanup(RERR_SIGNAL); |
82306bf6 AT |
691 | } |
692 | ||
8b35435f | 693 | static RETSIGTYPE sigusr2_handler(int val) { |
ff81e809 | 694 | extern int log_got_error; |
19b27a48 | 695 | if (log_got_error) _exit(RERR_PARTIAL); |
8b35435f AT |
696 | _exit(0); |
697 | } | |
698 | ||
19b27a48 AT |
699 | static RETSIGTYPE sigchld_handler(int val) { |
700 | } | |
701 | ||
5d6bcd44 | 702 | int main(int argc,char *argv[]) |
7a6421fa AT |
703 | { |
704 | extern int am_root; | |
705 | extern int orig_umask; | |
706 | extern int dry_run; | |
707 | extern int am_daemon; | |
708 | extern int am_server; | |
ff81e809 | 709 | int ret; |
5d6bcd44 | 710 | |
7a6421fa | 711 | signal(SIGUSR1, sigusr1_handler); |
8b35435f | 712 | signal(SIGUSR2, sigusr2_handler); |
19b27a48 | 713 | signal(SIGCHLD, sigchld_handler); |
5d6bcd44 | 714 | |
7a6421fa AT |
715 | starttime = time(NULL); |
716 | am_root = (getuid() == 0); | |
c627d613 | 717 | |
a800434a AT |
718 | memset(&stats, 0, sizeof(stats)); |
719 | ||
df5e03da AT |
720 | if (argc < 2) { |
721 | usage(FERROR); | |
65417579 | 722 | exit_cleanup(RERR_SYNTAX); |
df5e03da AT |
723 | } |
724 | ||
7a6421fa AT |
725 | /* we set a 0 umask so that correct file permissions can be |
726 | carried across */ | |
727 | orig_umask = (int)umask(0); | |
5d6bcd44 | 728 | |
50135767 | 729 | if (!parse_arguments(&argc, (const char ***) &argv, 1)) { |
305ab133 MP |
730 | /* FIXME: We ought to call the same error-handling |
731 | * code here, rather than relying on getopt. */ | |
50135767 | 732 | option_error(); |
65417579 | 733 | exit_cleanup(RERR_SYNTAX); |
b11ed3b1 | 734 | } |
5d6bcd44 | 735 | |
7a6421fa AT |
736 | signal(SIGINT,SIGNAL_CAST sig_int); |
737 | signal(SIGPIPE,SIGNAL_CAST sig_int); | |
738 | signal(SIGHUP,SIGNAL_CAST sig_int); | |
8638dd48 | 739 | signal(SIGTERM,SIGNAL_CAST sig_int); |
6b83141d | 740 | |
c226b7c2 DD |
741 | /* Initialize push_dir here because on some old systems getcwd |
742 | (implemented by forking "pwd" and reading its output) doesn't | |
743 | work when there are other child processes. Also, on all systems | |
744 | that implement getcwd that way "pwd" can't be found after chroot. */ | |
745 | push_dir(NULL,0); | |
746 | ||
7a6421fa AT |
747 | if (am_daemon) { |
748 | return daemon_main(); | |
749 | } | |
f0fca04e | 750 | |
08ac228f AT |
751 | if (argc < 1) { |
752 | usage(FERROR); | |
65417579 | 753 | exit_cleanup(RERR_SYNTAX); |
08ac228f AT |
754 | } |
755 | ||
7a6421fa AT |
756 | if (dry_run) |
757 | verbose = MAX(verbose,1); | |
c627d613 | 758 | |
cbbe4892 | 759 | #ifndef SUPPORT_LINKS |
7a6421fa AT |
760 | if (!am_server && preserve_links) { |
761 | rprintf(FERROR,"ERROR: symbolic links not supported\n"); | |
65417579 | 762 | exit_cleanup(RERR_UNSUPPORTED); |
7a6421fa | 763 | } |
cbbe4892 AT |
764 | #endif |
765 | ||
7a6421fa | 766 | if (am_server) { |
f0359dd0 AT |
767 | set_nonblocking(STDIN_FILENO); |
768 | set_nonblocking(STDOUT_FILENO); | |
7a6421fa AT |
769 | start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv); |
770 | } | |
c627d613 | 771 | |
ff81e809 AT |
772 | ret = start_client(argc, argv); |
773 | exit_cleanup(ret); | |
774 | return ret; | |
c627d613 | 775 | } |
82306bf6 | 776 |