Switching to GPL 3.
[rsync/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-2007 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 version 3 as
11  * published by the Free Software Foundation.
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  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, visit the http://fsf.org website.
20  */
21
22 #include "rsync.h"
23
24 extern int am_server;
25 extern int am_daemon;
26 extern int io_error;
27 extern int keep_partial;
28 extern int log_got_error;
29 extern char *partial_dir;
30 extern char *logfile_name;
31
32 #ifdef HAVE_SIGACTION
33 static struct sigaction sigact;
34 #endif
35
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  **/
41 void close_all(void)
42 {
43 #ifdef SHUTDOWN_ALL_SOCKETS
44         int max_fd;
45         int fd;
46         int ret;
47         STRUCT_STAT st;
48
49         max_fd = sysconf(_SC_OPEN_MAX) - 1;
50         for (fd = max_fd; fd >= 0; fd--) {
51                 if ((ret = do_fstat(fd, &st)) == 0) {
52                         if (is_a_socket(fd))
53                                 ret = shutdown(fd, 2);
54                         ret = close(fd);
55                 }
56         }
57 #endif
58 }
59
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  **/
78 int cleanup_got_literal = 0;
79
80 static char *cleanup_fname;
81 static char *cleanup_new_fname;
82 static struct file_struct *cleanup_file;
83 static int cleanup_fd_r, cleanup_fd_w;
84 static pid_t cleanup_pid = 0;
85
86 pid_t cleanup_child_pid = -1;
87
88 /**
89  * Eventually calls exit(), passing @p code, therefore does not return.
90  *
91  * @param code one of the RERR_* codes from errcode.h.
92  **/
93 NORETURN void _exit_cleanup(int code, const char *file, int line)
94 {
95         static int cleanup_step = 0;
96         static int exit_code = 0;
97         static int unmodified_code = 0;
98
99         SIGACTION(SIGUSR1, SIG_IGN);
100         SIGACTION(SIGUSR2, SIG_IGN);
101
102         if (exit_code) /* Preserve first error code when recursing. */
103                 code = exit_code;
104
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) {
108 #include "case_N.h" /* case 0: cleanup_step++; */
109
110                 exit_code = unmodified_code = code;
111
112                 if (verbose > 3) {
113                         rprintf(FINFO,
114                                 "_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
115                                 code, file, line);
116                 }
117
118                 /* FALLTHROUGH */
119 #include "case_N.h"
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                         }
129                 }
130
131                 /* FALLTHROUGH */
132 #include "case_N.h"
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                         }
144                         finish_transfer(cleanup_new_fname, fname, NULL, NULL,
145                                         cleanup_file, 0, !partial_dir);
146                 }
147
148                 /* FALLTHROUGH */
149 #include "case_N.h"
150
151                 io_flush(FULL_FLUSH);
152
153                 /* FALLTHROUGH */
154 #include "case_N.h"
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 */
179 #include "case_N.h"
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 */
189 #include "case_N.h"
190
191                 if (am_server && code)
192                         msleep(100);
193                 close_all();
194
195                 /* FALLTHROUGH */
196         default:
197                 break;
198         }
199
200         exit(code);
201 }
202
203 void cleanup_disable(void)
204 {
205         cleanup_fname = cleanup_new_fname = NULL;
206         cleanup_got_literal = 0;
207 }
208
209
210 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
211                  int fd_r, int fd_w)
212 {
213         cleanup_fname = fnametmp;
214         cleanup_new_fname = fname; /* can be NULL on a partial-dir failure */
215         cleanup_file = file;
216         cleanup_fd_r = fd_r;
217         cleanup_fd_w = fd_w;
218 }
219
220 void cleanup_set_pid(pid_t pid)
221 {
222         cleanup_pid = pid;
223 }