main.c 40 KB

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