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
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 int cleanup_got_literal=0;
00043
00044 static char *cleanup_fname;
00045 static char *cleanup_new_fname;
00046 static struct file_struct *cleanup_file;
00047 static int cleanup_fd1, cleanup_fd2;
00048 static struct map_struct *cleanup_buf;
00049 static int cleanup_pid = 0;
00050 extern int io_error;
00051
00052 pid_t cleanup_child_pid = -1;
00053
00054
00055
00056
00057
00058
00059 void _exit_cleanup(int code, const char *file, int line)
00060 {
00061 int ocode = code;
00062 extern int keep_partial;
00063 extern int log_got_error;
00064
00065 signal(SIGUSR1, SIG_IGN);
00066 signal(SIGUSR2, SIG_IGN);
00067
00068 if (verbose > 3)
00069 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
00070 code, file, line);
00071
00072 if (cleanup_child_pid != -1) {
00073 int status;
00074 if (waitpid(cleanup_child_pid, &status, WNOHANG) == cleanup_child_pid) {
00075 status = WEXITSTATUS(status);
00076 if (status > code) code = status;
00077 }
00078 }
00079
00080 if (cleanup_got_literal && cleanup_fname && keep_partial) {
00081 char *fname = cleanup_fname;
00082 cleanup_fname = NULL;
00083 if (cleanup_buf) unmap_file(cleanup_buf);
00084 if (cleanup_fd1 != -1) close(cleanup_fd1);
00085 if (cleanup_fd2 != -1) close(cleanup_fd2);
00086 finish_transfer(cleanup_new_fname, fname, cleanup_file);
00087 }
00088 io_flush();
00089 if (cleanup_fname)
00090 do_unlink(cleanup_fname);
00091 if (code) {
00092 kill_all(SIGUSR1);
00093 }
00094 if ((cleanup_pid != 0) && (cleanup_pid == (int) getpid())) {
00095 char *pidf = lp_pid_file();
00096 if (pidf && *pidf) {
00097 unlink(lp_pid_file());
00098 }
00099 }
00100
00101 if (code == 0 && (io_error || log_got_error)) {
00102 code = RERR_PARTIAL;
00103 }
00104
00105 if (code) log_exit(code, file, line);
00106
00107 if (verbose > 2)
00108 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
00109 ocode, file, line, code);
00110
00111 exit(code);
00112 }
00113
00114 void cleanup_disable(void)
00115 {
00116 cleanup_fname = NULL;
00117 cleanup_got_literal = 0;
00118 }
00119
00120
00121 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
00122 struct map_struct *buf, int fd1, int fd2)
00123 {
00124 cleanup_fname = fnametmp;
00125 cleanup_new_fname = fname;
00126 cleanup_file = file;
00127 cleanup_buf = buf;
00128 cleanup_fd1 = fd1;
00129 cleanup_fd2 = fd2;
00130 }
00131
00132 void cleanup_set_pid(int pid)
00133 {
00134 cleanup_pid = pid;
00135 }