Martin gave his approval to use GPLv3 with this code.
[rsync/rsync.git] / cleanup.c
CommitLineData
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
ba2133d6 7 * Copyright (C) 2003-2007 Wayne Davison
0f78b815
WD
8 *
9 * This program is free software; you can redistribute it and/or modify
4fd842f9 10 * it under the terms of the GNU General Public License version 3 as
ba2133d6 11 * published by the Free Software Foundation.
0f78b815
WD
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
e7c67065 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
2f03f956
AT
21
22#include "rsync.h"
23
1cfcb8af
WD
24extern int am_server;
25extern int am_daemon;
6e86c951
WD
26extern int io_error;
27extern int keep_partial;
28extern int log_got_error;
d45898df 29extern char *partial_dir;
1cfcb8af 30extern char *logfile_name;
6e86c951 31
44e604f4 32#ifdef HAVE_SIGACTION
2b28968d
WD
33static struct sigaction sigact;
34#endif
35
9f639210
DD
36/**
37 * Close all open sockets and files, allowing a (somewhat) graceful
38 * shutdown() of socket connections. This eliminates the abortive
39 * TCP RST sent by a Winsock-based system when the close() occurs.
40 **/
18d6b679 41void close_all(void)
9f639210
DD
42{
43#ifdef SHUTDOWN_ALL_SOCKETS
44 int max_fd;
45 int fd;
46 int ret;
58fef0ac 47 STRUCT_STAT st;
9f639210
DD
48
49 max_fd = sysconf(_SC_OPEN_MAX) - 1;
50 for (fd = max_fd; fd >= 0; fd--) {
58fef0ac 51 if ((ret = do_fstat(fd, &st)) == 0) {
8186ae6b 52 if (is_a_socket(fd))
9f639210 53 ret = shutdown(fd, 2);
9f639210
DD
54 ret = close(fd);
55 }
56 }
57#endif
58}
59
e0fde757
MP
60/**
61 * @file cleanup.c
62 *
63 * Code for handling interrupted transfers. Depending on the @c
64 * --partial option, we may either delete the temporary file, or go
65 * ahead and overwrite the destination. This second behaviour only
66 * occurs if we've sent literal data and therefore hopefully made
67 * progress on the transfer.
68 **/
69
70/**
71 * Set to True once literal data has been sent across the link for the
72 * current file. (????)
73 *
74 * Handling the cleanup when a transfer is interrupted is tricky when
75 * --partial is selected. We need to ensure that the partial file is
76 * kept if any real data has been transferred.
77 **/
8186ae6b 78int cleanup_got_literal = 0;
2f03f956
AT
79
80static char *cleanup_fname;
81static char *cleanup_new_fname;
82static struct file_struct *cleanup_file;
b6609caf 83static int cleanup_fd_r, cleanup_fd_w;
65fc84b3 84static pid_t cleanup_pid = 0;
2f03f956 85
19b27a48 86pid_t cleanup_child_pid = -1;
ef1aa910 87
e0fde757 88/**
b0f451eb 89 * Eventually calls exit(), passing @p code, therefore does not return.
e0fde757
MP
90 *
91 * @param code one of the RERR_* codes from errcode.h.
92 **/
b1b54199 93NORETURN void _exit_cleanup(int code, const char *file, int line)
2f03f956 94{
154c345d
WD
95 static int cleanup_step = 0;
96 static int exit_code = 0;
97 static int unmodified_code = 0;
2f03f956 98
2b28968d
WD
99 SIGACTION(SIGUSR1, SIG_IGN);
100 SIGACTION(SIGUSR2, SIG_IGN);
2f03f956 101
154c345d
WD
102 if (exit_code) /* Preserve first error code when recursing. */
103 code = exit_code;
b1b54199 104
154c345d
WD
105 /* Some of our actions might cause a recursive call back here, so we
106 * keep track of where we are in the cleanup and never repeat a step. */
107 switch (cleanup_step) {
e3794138 108#include "case_N.h" /* case 0: cleanup_step++; */
154c345d
WD
109
110 exit_code = unmodified_code = code;
9098bbf3 111
154c345d
WD
112 if (verbose > 3) {
113 rprintf(FINFO,
114 "_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
115 code, file, line);
19b27a48 116 }
19b27a48 117
154c345d 118 /* FALLTHROUGH */
e3794138 119#include "case_N.h"
154c345d
WD
120
121 if (cleanup_child_pid != -1) {
122 int status;
123 int pid = wait_process(cleanup_child_pid, &status, WNOHANG);
124 if (pid == cleanup_child_pid) {
125 status = WEXITSTATUS(status);
126 if (status > code)
127 code = exit_code = status;
128 }
09e2bbce 129 }
9b73d1c0 130
154c345d 131 /* FALLTHROUGH */
e3794138 132#include "case_N.h"
154c345d
WD
133
134 if (cleanup_got_literal && cleanup_fname && cleanup_new_fname
135 && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
136 char *fname = cleanup_fname;
137 cleanup_fname = NULL;
138 if (cleanup_fd_r != -1)
139 close(cleanup_fd_r);
140 if (cleanup_fd_w != -1) {
141 flush_write_file(cleanup_fd_w);
142 close(cleanup_fd_w);
143 }
16edf865 144 finish_transfer(cleanup_new_fname, fname, NULL, NULL,
154c345d
WD
145 cleanup_file, 0, !partial_dir);
146 }
147
148 /* FALLTHROUGH */
e3794138 149#include "case_N.h"
154c345d
WD
150
151 io_flush(FULL_FLUSH);
152
153 /* FALLTHROUGH */
e3794138 154#include "case_N.h"
154c345d
WD
155
156 if (cleanup_fname)
157 do_unlink(cleanup_fname);
158 if (code)
159 kill_all(SIGUSR1);
160 if (cleanup_pid && cleanup_pid == getpid()) {
161 char *pidf = lp_pid_file();
162 if (pidf && *pidf)
163 unlink(lp_pid_file());
164 }
165
166 if (code == 0) {
167 if (io_error & IOERR_DEL_LIMIT)
168 code = exit_code = RERR_DEL_LIMIT;
169 if (io_error & IOERR_VANISHED)
170 code = exit_code = RERR_VANISHED;
171 if (io_error & IOERR_GENERAL || log_got_error)
172 code = exit_code = RERR_PARTIAL;
173 }
174
175 if (code || am_daemon || (logfile_name && (am_server || !verbose)))
176 log_exit(code, file, line);
177
178 /* FALLTHROUGH */
e3794138 179#include "case_N.h"
154c345d
WD
180
181 if (verbose > 2) {
182 rprintf(FINFO,
183 "_exit_cleanup(code=%d, file=%s, line=%d): "
184 "about to call exit(%d)\n",
185 unmodified_code, file, line, code);
186 }
187
188 /* FALLTHROUGH */
e3794138 189#include "case_N.h"
ff81e809 190
377d22ab
WD
191 if (am_server && code)
192 msleep(100);
154c345d 193 close_all();
19b27a48 194
154c345d
WD
195 /* FALLTHROUGH */
196 default:
197 break;
6e86c951 198 }
9098bbf3 199
2f03f956
AT
200 exit(code);
201}
202
203void cleanup_disable(void)
204{
03dbc0b8 205 cleanup_fname = cleanup_new_fname = NULL;
2f03f956
AT
206 cleanup_got_literal = 0;
207}
208
209
c6b81a98 210void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
b6609caf 211 int fd_r, int fd_w)
2f03f956 212{
03dbc0b8
WD
213 cleanup_fname = fnametmp;
214 cleanup_new_fname = fname; /* can be NULL on a partial-dir failure */
2f03f956 215 cleanup_file = file;
b6609caf
WD
216 cleanup_fd_r = fd_r;
217 cleanup_fd_w = fd_w;
2f03f956 218}
8638dd48 219
65fc84b3 220void cleanup_set_pid(pid_t pid)
8638dd48
DD
221{
222 cleanup_pid = pid;
223}