cleanup.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * End-of-run cleanup routines.
  3. *
  4. * Copyright (C) 1996-2000 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2002 Martin Pool
  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. extern int am_server;
  24. extern int am_daemon;
  25. extern int am_receiver;
  26. extern int io_error;
  27. extern int keep_partial;
  28. extern int got_xfer_error;
  29. extern char *partial_dir;
  30. extern char *logfile_name;
  31. #ifdef HAVE_SIGACTION
  32. static struct sigaction sigact;
  33. #endif
  34. /**
  35. * Close all open sockets and files, allowing a (somewhat) graceful
  36. * shutdown() of socket connections. This eliminates the abortive
  37. * TCP RST sent by a Winsock-based system when the close() occurs.
  38. **/
  39. void close_all(void)
  40. {
  41. #ifdef SHUTDOWN_ALL_SOCKETS
  42. int max_fd;
  43. int fd;
  44. int ret;
  45. STRUCT_STAT st;
  46. max_fd = sysconf(_SC_OPEN_MAX) - 1;
  47. for (fd = max_fd; fd >= 0; fd--) {
  48. if ((ret = do_fstat(fd, &st)) == 0) {
  49. if (is_a_socket(fd))
  50. ret = shutdown(fd, 2);
  51. ret = close(fd);
  52. }
  53. }
  54. #endif
  55. }
  56. /**
  57. * @file cleanup.c
  58. *
  59. * Code for handling interrupted transfers. Depending on the @c
  60. * --partial option, we may either delete the temporary file, or go
  61. * ahead and overwrite the destination. This second behaviour only
  62. * occurs if we've sent literal data and therefore hopefully made
  63. * progress on the transfer.
  64. **/
  65. /**
  66. * Set to True once literal data has been sent across the link for the
  67. * current file. (????)
  68. *
  69. * Handling the cleanup when a transfer is interrupted is tricky when
  70. * --partial is selected. We need to ensure that the partial file is
  71. * kept if any real data has been transferred.
  72. **/
  73. int cleanup_got_literal = 0;
  74. static const char *cleanup_fname;
  75. static const char *cleanup_new_fname;
  76. static struct file_struct *cleanup_file;
  77. static int cleanup_fd_r, cleanup_fd_w;
  78. static pid_t cleanup_pid = 0;
  79. pid_t cleanup_child_pid = -1;
  80. /**
  81. * Eventually calls exit(), passing @p code, therefore does not return.
  82. *
  83. * @param code one of the RERR_* codes from errcode.h.
  84. **/
  85. NORETURN void _exit_cleanup(int code, const char *file, int line)
  86. {
  87. static int switch_step = 0;
  88. static int exit_code = 0, exit_line = 0;
  89. static const char *exit_file = NULL;
  90. static int unmodified_code = 0;
  91. SIGACTION(SIGUSR1, SIG_IGN);
  92. SIGACTION(SIGUSR2, SIG_IGN);
  93. if (exit_code) { /* Preserve first exit info when recursing. */
  94. code = exit_code;
  95. file = exit_file;
  96. line = exit_line;
  97. }
  98. /* If this is the exit at the end of the run, the server side
  99. * should not attempt to output a message (see log_exit()). */
  100. if (am_server && code == 0)
  101. am_server = 2;
  102. /* Some of our actions might cause a recursive call back here, so we
  103. * keep track of where we are in the cleanup and never repeat a step. */
  104. switch (switch_step) {
  105. #include "case_N.h" /* case 0: */
  106. switch_step++;
  107. exit_code = unmodified_code = code;
  108. exit_file = file;
  109. exit_line = line;
  110. if (verbose > 3) {
  111. rprintf(FINFO,
  112. "[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n",
  113. who_am_i(), code, file, line);
  114. }
  115. /* FALLTHROUGH */
  116. #include "case_N.h"
  117. switch_step++;
  118. if (cleanup_child_pid != -1) {
  119. int status;
  120. int pid = wait_process(cleanup_child_pid, &status, WNOHANG);
  121. if (pid == cleanup_child_pid) {
  122. status = WEXITSTATUS(status);
  123. if (status > code)
  124. code = exit_code = status;
  125. }
  126. }
  127. /* FALLTHROUGH */
  128. #include "case_N.h"
  129. switch_step++;
  130. if (cleanup_got_literal && cleanup_fname && cleanup_new_fname
  131. && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
  132. const char *fname = cleanup_fname;
  133. cleanup_fname = NULL;
  134. if (cleanup_fd_r != -1)
  135. close(cleanup_fd_r);
  136. if (cleanup_fd_w != -1) {
  137. flush_write_file(cleanup_fd_w);
  138. close(cleanup_fd_w);
  139. }
  140. finish_transfer(cleanup_new_fname, fname, NULL, NULL,
  141. cleanup_file, 0, !partial_dir);
  142. }
  143. /* FALLTHROUGH */
  144. #include "case_N.h"
  145. switch_step++;
  146. if (!code || am_server || am_receiver)
  147. io_flush(FULL_FLUSH);
  148. /* FALLTHROUGH */
  149. #include "case_N.h"
  150. switch_step++;
  151. if (cleanup_fname)
  152. do_unlink(cleanup_fname);
  153. if (code)
  154. kill_all(SIGUSR1);
  155. if (cleanup_pid && cleanup_pid == getpid()) {
  156. char *pidf = lp_pid_file();
  157. if (pidf && *pidf)
  158. unlink(lp_pid_file());
  159. }
  160. if (code == 0) {
  161. if (io_error & IOERR_DEL_LIMIT)
  162. code = exit_code = RERR_DEL_LIMIT;
  163. if (io_error & IOERR_VANISHED)
  164. code = exit_code = RERR_VANISHED;
  165. if (io_error & IOERR_GENERAL || got_xfer_error)
  166. code = exit_code = RERR_PARTIAL;
  167. }
  168. if (code || am_daemon || (logfile_name && (am_server || !verbose)))
  169. log_exit(code, file, line);
  170. /* FALLTHROUGH */
  171. #include "case_N.h"
  172. switch_step++;
  173. if (verbose > 2) {
  174. rprintf(FINFO,
  175. "[%s] _exit_cleanup(code=%d, file=%s, line=%d): "
  176. "about to call exit(%d)\n",
  177. who_am_i(), unmodified_code, file, line, code);
  178. }
  179. /* FALLTHROUGH */
  180. #include "case_N.h"
  181. switch_step++;
  182. if (am_server && code)
  183. msleep(100);
  184. close_all();
  185. /* FALLTHROUGH */
  186. default:
  187. break;
  188. }
  189. exit(code);
  190. }
  191. void cleanup_disable(void)
  192. {
  193. cleanup_fname = cleanup_new_fname = NULL;
  194. cleanup_got_literal = 0;
  195. }
  196. void cleanup_set(const char *fnametmp, const char *fname, struct file_struct *file,
  197. int fd_r, int fd_w)
  198. {
  199. cleanup_fname = fnametmp;
  200. cleanup_new_fname = fname; /* can be NULL on a partial-dir failure */
  201. cleanup_file = file;
  202. cleanup_fd_r = fd_r;
  203. cleanup_fd_w = fd_w;
  204. }
  205. void cleanup_set_pid(pid_t pid)
  206. {
  207. cleanup_pid = pid;
  208. }