Avoid leaving a file open on error return.
[rsync.git] / cleanup.c
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-2018 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
23 #include "rsync.h"
24
25 extern int dry_run;
26 extern int am_server;
27 extern int am_daemon;
28 extern int am_receiver;
29 extern int io_error;
30 extern int keep_partial;
31 extern int got_xfer_error;
32 extern int protocol_version;
33 extern int output_needs_newline;
34 extern char *partial_dir;
35 extern char *logfile_name;
36
37 BOOL shutting_down = False;
38 BOOL flush_ok_after_signal = False;
39
40 #ifdef HAVE_SIGACTION
41 static struct sigaction sigact;
42 #endif
43
44 /**
45  * Close all open sockets and files, allowing a (somewhat) graceful
46  * shutdown() of socket connections.  This eliminates the abortive
47  * TCP RST sent by a Winsock-based system when the close() occurs.
48  **/
49 void close_all(void)
50 {
51 #ifdef SHUTDOWN_ALL_SOCKETS
52         int max_fd;
53         int fd;
54         int ret;
55         STRUCT_STAT st;
56
57         max_fd = sysconf(_SC_OPEN_MAX) - 1;
58         for (fd = max_fd; fd >= 0; fd--) {
59                 if ((ret = do_fstat(fd, &st)) == 0) {
60                         if (is_a_socket(fd))
61                                 ret = shutdown(fd, 2);
62                         ret = close(fd);
63                 }
64         }
65 #endif
66 }
67
68 /**
69  * @file cleanup.c
70  *
71  * Code for handling interrupted transfers.  Depending on the @c
72  * --partial option, we may either delete the temporary file, or go
73  * ahead and overwrite the destination.  This second behaviour only
74  * occurs if we've sent literal data and therefore hopefully made
75  * progress on the transfer.
76  **/
77
78 /**
79  * Set to True once literal data has been sent across the link for the
80  * current file. (????)
81  *
82  * Handling the cleanup when a transfer is interrupted is tricky when
83  * --partial is selected.  We need to ensure that the partial file is
84  * kept if any real data has been transferred.
85  **/
86 int cleanup_got_literal = 0;
87
88 static const char *cleanup_fname;
89 static const char *cleanup_new_fname;
90 static struct file_struct *cleanup_file;
91 static int cleanup_fd_r = -1, cleanup_fd_w = -1;
92 static pid_t cleanup_pid = 0;
93
94 pid_t cleanup_child_pid = -1;
95
96 /**
97  * Eventually calls exit(), passing @p code, therefore does not return.
98  *
99  * @param code one of the RERR_* codes from errcode.h.
100  **/
101 NORETURN void _exit_cleanup(int code, const char *file, int line)
102 {
103         static int switch_step = 0;
104         static int exit_code = 0, exit_line = 0;
105         static const char *exit_file = NULL;
106         static int first_code = 0;
107
108         SIGACTION(SIGUSR1, SIG_IGN);
109         SIGACTION(SIGUSR2, SIG_IGN);
110
111         if (!exit_code) { /* Preserve first error exit info when recursing. */
112                 exit_code = code;
113                 exit_file = file;
114                 exit_line = line < 0 ? -line : line;
115         }
116
117         /* If this is the exit at the end of the run, the server side
118          * should not attempt to output a message (see log_exit()). */
119         if (am_server && code == 0)
120                 am_server = 2;
121
122         /* Some of our actions might cause a recursive call back here, so we
123          * keep track of where we are in the cleanup and never repeat a step. */
124         switch (switch_step) {
125 #include "case_N.h" /* case 0: */
126                 switch_step++;
127
128                 first_code = code;
129
130                 if (output_needs_newline) {
131                         fputc('\n', stdout);
132                         output_needs_newline = 0;
133                 }
134
135                 if (DEBUG_GTE(EXIT, 2)) {
136                         rprintf(FINFO,
137                                 "[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n",
138                                 who_am_i(), code, file, line);
139                 }
140
141 #include "case_N.h"
142                 switch_step++;
143
144                 if (cleanup_child_pid != -1) {
145                         int status;
146                         int pid = wait_process(cleanup_child_pid, &status, WNOHANG);
147                         if (pid == cleanup_child_pid) {
148                                 status = WEXITSTATUS(status);
149                                 if (status > exit_code)
150                                         exit_code = status;
151                         }
152                 }
153
154 #include "case_N.h"
155                 switch_step++;
156
157                 if (cleanup_got_literal && (cleanup_fname || cleanup_fd_w != -1)) {
158                         if (cleanup_fd_r != -1) {
159                                 close(cleanup_fd_r);
160                                 cleanup_fd_r = -1;
161                         }
162                         if (cleanup_fd_w != -1) {
163                                 flush_write_file(cleanup_fd_w);
164                                 close(cleanup_fd_w);
165                                 cleanup_fd_w = -1;
166                         }
167                         if (cleanup_fname && cleanup_new_fname && keep_partial
168                          && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
169                                 int tweak_modtime = 0;
170                                 const char *fname = cleanup_fname;
171                                 cleanup_fname = NULL;
172                                 if (!partial_dir) {
173                                     /* We don't want to leave a partial file with a modern time or it
174                                      * could be skipped via --update.  Setting the time to something
175                                      * really old also helps it to stand out as unfinished in an ls. */
176                                     tweak_modtime = 1;
177                                     cleanup_file->modtime = 0;
178                                 }
179                                 finish_transfer(cleanup_new_fname, fname, NULL, NULL,
180                                                 cleanup_file, tweak_modtime, !partial_dir);
181                         }
182                 }
183
184 #include "case_N.h"
185                 switch_step++;
186
187                 if (flush_ok_after_signal) {
188                         flush_ok_after_signal = False;
189                         if (code == RERR_SIGNAL)
190                                 io_flush(FULL_FLUSH);
191                 }
192                 if (!exit_code && !code)
193                         io_flush(FULL_FLUSH);
194
195 #include "case_N.h"
196                 switch_step++;
197
198                 if (cleanup_fname)
199                         do_unlink(cleanup_fname);
200                 if (exit_code)
201                         kill_all(SIGUSR1);
202                 if (cleanup_pid && cleanup_pid == getpid()) {
203                         char *pidf = lp_pid_file();
204                         if (pidf && *pidf)
205                                 unlink(lp_pid_file());
206                 }
207
208                 if (exit_code == 0) {
209                         if (code)
210                                 exit_code = code;
211                         if (io_error & IOERR_DEL_LIMIT)
212                                 exit_code = RERR_DEL_LIMIT;
213                         if (io_error & IOERR_VANISHED)
214                                 exit_code = RERR_VANISHED;
215                         if (io_error & IOERR_GENERAL || got_xfer_error)
216                                 exit_code = RERR_PARTIAL;
217                 }
218
219                 /* If line < 0, this exit is after a MSG_ERROR_EXIT event, so
220                  * we don't want to output a duplicate error. */
221                 if ((exit_code && line > 0)
222                  || am_daemon || (logfile_name && (am_server || !INFO_GTE(STATS, 1))))
223                         log_exit(exit_code, exit_file, exit_line);
224
225 #include "case_N.h"
226                 switch_step++;
227
228                 if (DEBUG_GTE(EXIT, 1)) {
229                         rprintf(FINFO,
230                                 "[%s] _exit_cleanup(code=%d, file=%s, line=%d): "
231                                 "about to call exit(%d)%s\n",
232                                 who_am_i(), first_code, exit_file, exit_line, exit_code,
233                                 dry_run ? " (DRY RUN)" : "");
234                 }
235
236 #include "case_N.h"
237                 switch_step++;
238
239                 if (exit_code && exit_code != RERR_SOCKETIO && exit_code != RERR_STREAMIO && exit_code != RERR_SIGNAL1
240                  && exit_code != RERR_TIMEOUT && !shutting_down && (protocol_version >= 31 || am_receiver)) {
241                         if (line > 0) {
242                                 if (DEBUG_GTE(EXIT, 3)) {
243                                         rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
244                                                 who_am_i(), exit_code);
245                                 }
246                                 send_msg_int(MSG_ERROR_EXIT, exit_code);
247                         }
248                         noop_io_until_death();
249                 }
250
251 #include "case_N.h"
252                 switch_step++;
253
254                 if (am_server && exit_code)
255                         msleep(100);
256                 close_all();
257
258                 /* FALLTHROUGH */
259         default:
260                 break;
261         }
262
263         exit(exit_code);
264 }
265
266 void cleanup_disable(void)
267 {
268         cleanup_fname = cleanup_new_fname = NULL;
269         cleanup_fd_r = cleanup_fd_w = -1;
270         cleanup_got_literal = 0;
271 }
272
273
274 void cleanup_set(const char *fnametmp, const char *fname, struct file_struct *file,
275                  int fd_r, int fd_w)
276 {
277         cleanup_fname = fnametmp;
278         cleanup_new_fname = fname; /* can be NULL on a partial-dir failure */
279         cleanup_file = file;
280         cleanup_fd_r = fd_r;
281         cleanup_fd_w = fd_w;
282 }
283
284 void cleanup_set_pid(pid_t pid)
285 {
286         cleanup_pid = pid;
287 }