- Updated the address for the FSF in the opening comment.
[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, 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  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "rsync.h"
25
26 extern int io_error;
27 extern int keep_partial;
28 extern int log_got_error;
29 extern char *partial_dir;
30
31 #ifdef HAVE_SIGACTION
32 static struct sigaction sigact;
33 #endif
34
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  **/
40 void close_all(void)
41 {
42 #ifdef SHUTDOWN_ALL_SOCKETS
43         int max_fd;
44         int fd;
45         int ret;
46         STRUCT_STAT st;
47
48         max_fd = sysconf(_SC_OPEN_MAX) - 1;
49         for (fd = max_fd; fd >= 0; fd--) {
50                 if ((ret = do_fstat(fd, &st)) == 0) {
51                         if (is_a_socket(fd))
52                                 ret = shutdown(fd, 2);
53                         ret = close(fd);
54                 }
55         }
56 #endif
57 }
58
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  **/
77 int cleanup_got_literal = 0;
78
79 static char *cleanup_fname;
80 static char *cleanup_new_fname;
81 static struct file_struct *cleanup_file;
82 static int cleanup_fd_r, cleanup_fd_w;
83 static pid_t cleanup_pid = 0;
84
85 pid_t cleanup_child_pid = -1;
86
87 /**
88  * Eventually calls exit(), passing @p code, therefore does not return.
89  *
90  * @param code one of the RERR_* codes from errcode.h.
91  **/
92 void _exit_cleanup(int code, const char *file, int line)
93 {
94         int ocode = code;
95         static int inside_cleanup = 0;
96
97         if (inside_cleanup > 10) {
98                 /* prevent the occasional infinite recursion */
99                 return;
100         }
101         inside_cleanup++;
102
103         SIGACTION(SIGUSR1, SIG_IGN);
104         SIGACTION(SIGUSR2, SIG_IGN);
105
106         if (verbose > 3) {
107                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
108                         code, file, line);
109         }
110
111         if (cleanup_child_pid != -1) {
112                 int status;
113                 if (wait_process(cleanup_child_pid, &status, WNOHANG)
114                  == cleanup_child_pid) {
115                         status = WEXITSTATUS(status);
116                         if (status > code)
117                                 code = status;
118                 }
119         }
120
121         if (cleanup_got_literal && cleanup_fname && keep_partial
122             && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
123                 char *fname = cleanup_fname;
124                 cleanup_fname = NULL;
125                 if (cleanup_fd_r != -1)
126                         close(cleanup_fd_r);
127                 if (cleanup_fd_w != -1) {
128                         flush_write_file(cleanup_fd_w);
129                         close(cleanup_fd_w);
130                 }
131                 finish_transfer(cleanup_new_fname, fname, NULL,
132                                 cleanup_file, 0, !partial_dir);
133         }
134         io_flush(FULL_FLUSH);
135         if (cleanup_fname)
136                 do_unlink(cleanup_fname);
137         if (code)
138                 kill_all(SIGUSR1);
139         if (cleanup_pid && cleanup_pid == getpid()) {
140                 char *pidf = lp_pid_file();
141                 if (pidf && *pidf)
142                         unlink(lp_pid_file());
143         }
144
145         if (code == 0) {
146                 if (io_error & IOERR_DEL_LIMIT)
147                         code = RERR_DEL_LIMIT;
148                 if (io_error & IOERR_VANISHED)
149                         code = RERR_VANISHED;
150                 if (io_error & IOERR_GENERAL || log_got_error)
151                         code = RERR_PARTIAL;
152         }
153
154         if (code)
155                 log_exit(code, file, line);
156
157         if (verbose > 2) {
158                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
159                         ocode, file, line, code);
160         }
161
162         close_all();
163         exit(code);
164 }
165
166 void cleanup_disable(void)
167 {
168         cleanup_fname = NULL;
169         cleanup_got_literal = 0;
170 }
171
172
173 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
174                  int fd_r, int fd_w)
175 {
176         cleanup_fname = fname ? fnametmp : NULL;
177         cleanup_new_fname = fname;
178         cleanup_file = file;
179         cleanup_fd_r = fd_r;
180         cleanup_fd_w = fd_w;
181 }
182
183 void cleanup_set_pid(pid_t pid)
184 {
185         cleanup_pid = pid;
186 }