Commit | Line | Data |
---|---|---|
0f78b815 WD |
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, 2004, 2005, 2006 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 2 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 | * | |
e7c67065 WD |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 22 | */ |
2f03f956 AT |
23 | |
24 | #include "rsync.h" | |
25 | ||
6e86c951 WD |
26 | extern int io_error; |
27 | extern int keep_partial; | |
28 | extern int log_got_error; | |
d45898df | 29 | extern char *partial_dir; |
6e86c951 | 30 | |
44e604f4 | 31 | #ifdef HAVE_SIGACTION |
2b28968d WD |
32 | static struct sigaction sigact; |
33 | #endif | |
34 | ||
9f639210 DD |
35 | /** |
36 | * Close all open sockets and files, allowing a (somewhat) graceful | |
37 | * shutdown() of socket connections. This eliminates the abortive | |
38 | * TCP RST sent by a Winsock-based system when the close() occurs. | |
39 | **/ | |
18d6b679 | 40 | void close_all(void) |
9f639210 DD |
41 | { |
42 | #ifdef SHUTDOWN_ALL_SOCKETS | |
43 | int max_fd; | |
44 | int fd; | |
45 | int ret; | |
58fef0ac | 46 | STRUCT_STAT st; |
9f639210 DD |
47 | |
48 | max_fd = sysconf(_SC_OPEN_MAX) - 1; | |
49 | for (fd = max_fd; fd >= 0; fd--) { | |
58fef0ac | 50 | if ((ret = do_fstat(fd, &st)) == 0) { |
8186ae6b | 51 | if (is_a_socket(fd)) |
9f639210 | 52 | ret = shutdown(fd, 2); |
9f639210 DD |
53 | ret = close(fd); |
54 | } | |
55 | } | |
56 | #endif | |
57 | } | |
58 | ||
e0fde757 MP |
59 | /** |
60 | * @file cleanup.c | |
61 | * | |
62 | * Code for handling interrupted transfers. Depending on the @c | |
63 | * --partial option, we may either delete the temporary file, or go | |
64 | * ahead and overwrite the destination. This second behaviour only | |
65 | * occurs if we've sent literal data and therefore hopefully made | |
66 | * progress on the transfer. | |
67 | **/ | |
68 | ||
69 | /** | |
70 | * Set to True once literal data has been sent across the link for the | |
71 | * current file. (????) | |
72 | * | |
73 | * Handling the cleanup when a transfer is interrupted is tricky when | |
74 | * --partial is selected. We need to ensure that the partial file is | |
75 | * kept if any real data has been transferred. | |
76 | **/ | |
8186ae6b | 77 | int cleanup_got_literal = 0; |
2f03f956 AT |
78 | |
79 | static char *cleanup_fname; | |
80 | static char *cleanup_new_fname; | |
81 | static struct file_struct *cleanup_file; | |
b6609caf | 82 | static int cleanup_fd_r, cleanup_fd_w; |
65fc84b3 | 83 | static pid_t cleanup_pid = 0; |
2f03f956 | 84 | |
19b27a48 | 85 | pid_t cleanup_child_pid = -1; |
ef1aa910 | 86 | |
e0fde757 | 87 | /** |
b0f451eb | 88 | * Eventually calls exit(), passing @p code, therefore does not return. |
e0fde757 MP |
89 | * |
90 | * @param code one of the RERR_* codes from errcode.h. | |
91 | **/ | |
a9766ef1 | 92 | void _exit_cleanup(int code, const char *file, int line) |
2f03f956 | 93 | { |
9098bbf3 | 94 | int ocode = code; |
b765ec32 DD |
95 | static int inside_cleanup = 0; |
96 | ||
aa2c47d8 | 97 | if (inside_cleanup > 10) { |
b765ec32 DD |
98 | /* prevent the occasional infinite recursion */ |
99 | return; | |
100 | } | |
aa2c47d8 | 101 | inside_cleanup++; |
2f03f956 | 102 | |
2b28968d WD |
103 | SIGACTION(SIGUSR1, SIG_IGN); |
104 | SIGACTION(SIGUSR2, SIG_IGN); | |
2f03f956 | 105 | |
6e86c951 WD |
106 | if (verbose > 3) { |
107 | rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n", | |
45c49b52 | 108 | code, file, line); |
6e86c951 | 109 | } |
9098bbf3 | 110 | |
19b27a48 AT |
111 | if (cleanup_child_pid != -1) { |
112 | int status; | |
ef1f6280 WD |
113 | if (wait_process(cleanup_child_pid, &status, WNOHANG) |
114 | == cleanup_child_pid) { | |
19b27a48 | 115 | status = WEXITSTATUS(status); |
8186ae6b WD |
116 | if (status > code) |
117 | code = status; | |
19b27a48 AT |
118 | } |
119 | } | |
120 | ||
a7260c40 WD |
121 | if (cleanup_got_literal && cleanup_fname && keep_partial |
122 | && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) { | |
2f03f956 AT |
123 | char *fname = cleanup_fname; |
124 | cleanup_fname = NULL; | |
b6609caf WD |
125 | if (cleanup_fd_r != -1) |
126 | close(cleanup_fd_r); | |
09e2bbce WD |
127 | if (cleanup_fd_w != -1) { |
128 | flush_write_file(cleanup_fd_w); | |
b6609caf | 129 | close(cleanup_fd_w); |
09e2bbce | 130 | } |
12fa790e WD |
131 | finish_transfer(cleanup_new_fname, fname, NULL, |
132 | cleanup_file, 0, !partial_dir); | |
2f03f956 | 133 | } |
ef732c3b | 134 | io_flush(FULL_FLUSH); |
2f03f956 AT |
135 | if (cleanup_fname) |
136 | do_unlink(cleanup_fname); | |
8186ae6b | 137 | if (code) |
2f03f956 | 138 | kill_all(SIGUSR1); |
65fc84b3 | 139 | if (cleanup_pid && cleanup_pid == getpid()) { |
27d3cdbc | 140 | char *pidf = lp_pid_file(); |
8186ae6b | 141 | if (pidf && *pidf) |
27d3cdbc | 142 | unlink(lp_pid_file()); |
27d3cdbc | 143 | } |
9b73d1c0 | 144 | |
6e35c72f | 145 | if (code == 0) { |
054abde2 WD |
146 | if (io_error & IOERR_DEL_LIMIT) |
147 | code = RERR_DEL_LIMIT; | |
148 | if (io_error & IOERR_VANISHED) | |
6e35c72f | 149 | code = RERR_VANISHED; |
054abde2 WD |
150 | if (io_error & IOERR_GENERAL || log_got_error) |
151 | code = RERR_PARTIAL; | |
ff81e809 AT |
152 | } |
153 | ||
8186ae6b WD |
154 | if (code) |
155 | log_exit(code, file, line); | |
19b27a48 | 156 | |
6e86c951 WD |
157 | if (verbose > 2) { |
158 | rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n", | |
45c49b52 | 159 | ocode, file, line, code); |
6e86c951 | 160 | } |
9098bbf3 | 161 | |
9f639210 | 162 | close_all(); |
2f03f956 AT |
163 | exit(code); |
164 | } | |
165 | ||
166 | void cleanup_disable(void) | |
167 | { | |
168 | cleanup_fname = NULL; | |
169 | cleanup_got_literal = 0; | |
170 | } | |
171 | ||
172 | ||
c6b81a98 | 173 | void cleanup_set(char *fnametmp, char *fname, struct file_struct *file, |
b6609caf | 174 | int fd_r, int fd_w) |
2f03f956 | 175 | { |
3d7cc571 | 176 | cleanup_fname = fname ? fnametmp : NULL; |
2f03f956 AT |
177 | cleanup_new_fname = fname; |
178 | cleanup_file = file; | |
b6609caf WD |
179 | cleanup_fd_r = fd_r; |
180 | cleanup_fd_w = fd_w; | |
2f03f956 | 181 | } |
8638dd48 | 182 | |
65fc84b3 | 183 | void cleanup_set_pid(pid_t pid) |
8638dd48 DD |
184 | { |
185 | cleanup_pid = pid; | |
186 | } |