cleanup.c 5.6 KB

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