Commit | Line | Data |
---|---|---|
ef1aa910 MP |
1 | /* -*- c-file-style: "linux" -*- |
2 | ||
3 | Copyright (C) 1996-2000 by Andrew Tridgell | |
2f03f956 AT |
4 | Copyright (C) Paul Mackerras 1996 |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
21 | #include "rsync.h" | |
22 | ||
23 | /* handling the cleanup when a transfer is interrupted is tricky when | |
24 | --partial is selected. We need to ensure that the partial file is | |
25 | kept if any real data has been transferred */ | |
26 | int cleanup_got_literal=0; | |
27 | ||
28 | static char *cleanup_fname; | |
29 | static char *cleanup_new_fname; | |
30 | static struct file_struct *cleanup_file; | |
c6b81a98 AT |
31 | static int cleanup_fd1, cleanup_fd2; |
32 | static struct map_struct *cleanup_buf; | |
8638dd48 | 33 | static int cleanup_pid = 0; |
9bd65976 | 34 | extern int io_error; |
2f03f956 | 35 | |
19b27a48 | 36 | pid_t cleanup_child_pid = -1; |
ef1aa910 MP |
37 | |
38 | /* | |
39 | * Code is one of the RERR_* codes from errcode.h. | |
40 | */ | |
a9766ef1 | 41 | void _exit_cleanup(int code, const char *file, int line) |
2f03f956 | 42 | { |
9098bbf3 | 43 | int ocode = code; |
2f03f956 | 44 | extern int keep_partial; |
ff81e809 | 45 | extern int log_got_error; |
2f03f956 AT |
46 | |
47 | signal(SIGUSR1, SIG_IGN); | |
8b35435f | 48 | signal(SIGUSR2, SIG_IGN); |
2f03f956 | 49 | |
9098bbf3 MP |
50 | if (verbose > 3) |
51 | rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n", | |
52 | code, file, line); | |
53 | ||
19b27a48 AT |
54 | if (cleanup_child_pid != -1) { |
55 | int status; | |
56 | if (waitpid(cleanup_child_pid, &status, WNOHANG) == cleanup_child_pid) { | |
57 | status = WEXITSTATUS(status); | |
58 | if (status > code) code = status; | |
59 | } | |
60 | } | |
61 | ||
2f03f956 AT |
62 | if (cleanup_got_literal && cleanup_fname && keep_partial) { |
63 | char *fname = cleanup_fname; | |
64 | cleanup_fname = NULL; | |
c6b81a98 AT |
65 | if (cleanup_buf) unmap_file(cleanup_buf); |
66 | if (cleanup_fd1 != -1) close(cleanup_fd1); | |
67 | if (cleanup_fd2 != -1) close(cleanup_fd2); | |
2f03f956 AT |
68 | finish_transfer(cleanup_new_fname, fname, cleanup_file); |
69 | } | |
70 | io_flush(); | |
71 | if (cleanup_fname) | |
72 | do_unlink(cleanup_fname); | |
73 | if (code) { | |
74 | kill_all(SIGUSR1); | |
75 | } | |
27d3cdbc AT |
76 | if ((cleanup_pid != 0) && (cleanup_pid == (int) getpid())) { |
77 | char *pidf = lp_pid_file(); | |
78 | if (pidf && *pidf) { | |
79 | unlink(lp_pid_file()); | |
80 | } | |
81 | } | |
9b73d1c0 | 82 | |
19b27a48 AT |
83 | if (code == 0 && (io_error || log_got_error)) { |
84 | code = RERR_PARTIAL; | |
ff81e809 AT |
85 | } |
86 | ||
19b27a48 AT |
87 | if (code) log_exit(code, file, line); |
88 | ||
9098bbf3 MP |
89 | if (verbose > 2) |
90 | rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n", | |
91 | ocode, file, line, code); | |
92 | ||
2f03f956 AT |
93 | exit(code); |
94 | } | |
95 | ||
96 | void cleanup_disable(void) | |
97 | { | |
98 | cleanup_fname = NULL; | |
99 | cleanup_got_literal = 0; | |
100 | } | |
101 | ||
102 | ||
c6b81a98 AT |
103 | void cleanup_set(char *fnametmp, char *fname, struct file_struct *file, |
104 | struct map_struct *buf, int fd1, int fd2) | |
2f03f956 AT |
105 | { |
106 | cleanup_fname = fnametmp; | |
107 | cleanup_new_fname = fname; | |
108 | cleanup_file = file; | |
c6b81a98 AT |
109 | cleanup_buf = buf; |
110 | cleanup_fd1 = fd1; | |
111 | cleanup_fd2 = fd2; | |
2f03f956 | 112 | } |
8638dd48 DD |
113 | |
114 | void cleanup_set_pid(int pid) | |
115 | { | |
116 | cleanup_pid = pid; | |
117 | } |