00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "rsync.h"
00023
00024 extern int io_error;
00025 extern int keep_partial;
00026 extern int log_got_error;
00027 extern char *partial_dir;
00028
00029 #ifdef HAVE_SIGACTION
00030 static struct sigaction sigact;
00031 #endif
00032
00033
00034
00035
00036
00037
00038 void close_all(void)
00039 {
00040 #ifdef SHUTDOWN_ALL_SOCKETS
00041 int max_fd;
00042 int fd;
00043 int ret;
00044 STRUCT_STAT st;
00045
00046 max_fd = sysconf(_SC_OPEN_MAX) - 1;
00047 for (fd = max_fd; fd >= 0; fd--) {
00048 if ((ret = do_fstat(fd, &st)) == 0) {
00049 if (is_a_socket(fd))
00050 ret = shutdown(fd, 2);
00051 ret = close(fd);
00052 }
00053 }
00054 #endif
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 int cleanup_got_literal = 0;
00076
00077 static char *cleanup_fname;
00078 static char *cleanup_new_fname;
00079 static struct file_struct *cleanup_file;
00080 static int cleanup_fd_r, cleanup_fd_w;
00081 static pid_t cleanup_pid = 0;
00082
00083 pid_t cleanup_child_pid = -1;
00084
00085
00086
00087
00088
00089
00090 void _exit_cleanup(int code, const char *file, int line)
00091 {
00092 int ocode = code;
00093 static int inside_cleanup = 0;
00094
00095 if (inside_cleanup > 10) {
00096
00097 return;
00098 }
00099 inside_cleanup++;
00100
00101 SIGACTION(SIGUSR1, SIG_IGN);
00102 SIGACTION(SIGUSR2, SIG_IGN);
00103
00104 if (verbose > 3) {
00105 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
00106 code, file, line);
00107 }
00108
00109 if (cleanup_child_pid != -1) {
00110 int status;
00111 if (wait_process(cleanup_child_pid, &status, WNOHANG)
00112 == cleanup_child_pid) {
00113 status = WEXITSTATUS(status);
00114 if (status > code)
00115 code = status;
00116 }
00117 }
00118
00119 if (cleanup_got_literal && cleanup_fname && keep_partial
00120 && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
00121 char *fname = cleanup_fname;
00122 cleanup_fname = NULL;
00123 if (cleanup_fd_r != -1)
00124 close(cleanup_fd_r);
00125 if (cleanup_fd_w != -1) {
00126 flush_write_file(cleanup_fd_w);
00127 close(cleanup_fd_w);
00128 }
00129 finish_transfer(cleanup_new_fname, fname, NULL,
00130 cleanup_file, 0, !partial_dir);
00131 }
00132 io_flush(FULL_FLUSH);
00133 if (cleanup_fname)
00134 do_unlink(cleanup_fname);
00135 if (code)
00136 kill_all(SIGUSR1);
00137 if (cleanup_pid && cleanup_pid == getpid()) {
00138 char *pidf = lp_pid_file();
00139 if (pidf && *pidf)
00140 unlink(lp_pid_file());
00141 }
00142
00143 if (code == 0) {
00144 if (io_error & IOERR_DEL_LIMIT)
00145 code = RERR_DEL_LIMIT;
00146 if (io_error & IOERR_VANISHED)
00147 code = RERR_VANISHED;
00148 if (io_error & IOERR_GENERAL || log_got_error)
00149 code = RERR_PARTIAL;
00150 }
00151
00152 if (code)
00153 log_exit(code, file, line);
00154
00155 if (verbose > 2) {
00156 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
00157 ocode, file, line, code);
00158 }
00159
00160 close_all();
00161 exit(code);
00162 }
00163
00164 void cleanup_disable(void)
00165 {
00166 cleanup_fname = NULL;
00167 cleanup_got_literal = 0;
00168 }
00169
00170
00171 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
00172 int fd_r, int fd_w)
00173 {
00174 cleanup_fname = fname ? fnametmp : NULL;
00175 cleanup_new_fname = fname;
00176 cleanup_file = file;
00177 cleanup_fd_r = fd_r;
00178 cleanup_fd_w = fd_w;
00179 }
00180
00181 void cleanup_set_pid(pid_t pid)
00182 {
00183 cleanup_pid = pid;
00184 }