Made a comment better.
[rsync/rsync.git] / cleanup.c
1 /* -*- c-file-style: "linux" -*-
2
3    Copyright (C) 1996-2000 by Andrew Tridgell
4    Copyright (C) Paul Mackerras 1996
5    Copyright (C) 2002 by Martin Pool
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "rsync.h"
23
24 extern int io_error;
25 extern int keep_partial;
26 extern int log_got_error;
27
28 /**
29  * Close all open sockets and files, allowing a (somewhat) graceful
30  * shutdown() of socket connections.  This eliminates the abortive
31  * TCP RST sent by a Winsock-based system when the close() occurs.
32  **/
33 void close_all(void)
34 {
35 #ifdef SHUTDOWN_ALL_SOCKETS
36         int max_fd;
37         int fd;
38         int ret;
39         struct stat st;
40
41         max_fd = sysconf(_SC_OPEN_MAX) - 1;
42         for (fd = max_fd; fd >= 0; fd--) {
43                 ret = fstat(fd,&st);
44                 if (fstat(fd,&st) == 0) {
45                         if (is_a_socket(fd))
46                                 ret = shutdown(fd, 2);
47                         ret = close(fd);
48                 }
49         }
50 #endif
51 }
52
53 /**
54  * @file cleanup.c
55  *
56  * Code for handling interrupted transfers.  Depending on the @c
57  * --partial option, we may either delete the temporary file, or go
58  * ahead and overwrite the destination.  This second behaviour only
59  * occurs if we've sent literal data and therefore hopefully made
60  * progress on the transfer.
61  **/
62
63 /**
64  * Set to True once literal data has been sent across the link for the
65  * current file. (????)
66  *
67  * Handling the cleanup when a transfer is interrupted is tricky when
68  * --partial is selected.  We need to ensure that the partial file is
69  * kept if any real data has been transferred.
70  **/
71 int cleanup_got_literal = 0;
72
73 static char *cleanup_fname;
74 static char *cleanup_new_fname;
75 static struct file_struct *cleanup_file;
76 static int cleanup_fd_r, cleanup_fd_w;
77 static pid_t cleanup_pid = 0;
78
79 pid_t cleanup_child_pid = -1;
80
81 /**
82  * Eventually calls exit(), passing @p code, therefore does not return.
83  *
84  * @param code one of the RERR_* codes from errcode.h.
85  **/
86 void _exit_cleanup(int code, const char *file, int line)
87 {
88         int ocode = code;
89         static int inside_cleanup = 0;
90
91         if (inside_cleanup > 10) {
92                 /* prevent the occasional infinite recursion */
93                 return;
94         }
95         inside_cleanup++;
96
97         signal(SIGUSR1, SIG_IGN);
98         signal(SIGUSR2, SIG_IGN);
99
100         if (verbose > 3) {
101                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
102                         code, file, line);
103         }
104
105         if (cleanup_child_pid != -1) {
106                 int status;
107                 if (waitpid(cleanup_child_pid, &status, WNOHANG) == cleanup_child_pid) {
108                         status = WEXITSTATUS(status);
109                         if (status > code)
110                                 code = status;
111                 }
112         }
113
114         if (cleanup_got_literal && cleanup_fname && keep_partial) {
115                 char *fname = cleanup_fname;
116                 cleanup_fname = NULL;
117                 if (cleanup_fd_r != -1)
118                         close(cleanup_fd_r);
119                 if (cleanup_fd_w != -1)
120                         close(cleanup_fd_w);
121                 finish_transfer(cleanup_new_fname, fname, cleanup_file, 0);
122         }
123         io_flush(FULL_FLUSH);
124         if (cleanup_fname)
125                 do_unlink(cleanup_fname);
126         if (code)
127                 kill_all(SIGUSR1);
128         if (cleanup_pid && cleanup_pid == getpid()) {
129                 char *pidf = lp_pid_file();
130                 if (pidf && *pidf)
131                         unlink(lp_pid_file());
132         }
133
134         if (code == 0) {
135                 if ((io_error & ~IOERR_VANISHED) || log_got_error)
136                         code = RERR_PARTIAL;
137                 else if (io_error)
138                         code = RERR_VANISHED;
139         }
140
141         if (code)
142                 log_exit(code, file, line);
143
144         if (verbose > 2) {
145                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
146                         ocode, file, line, code);
147         }
148
149         close_all();
150         exit(code);
151 }
152
153 void cleanup_disable(void)
154 {
155         cleanup_fname = NULL;
156         cleanup_got_literal = 0;
157 }
158
159
160 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
161                  int fd_r, int fd_w)
162 {
163         cleanup_fname = fnametmp;
164         cleanup_new_fname = fname;
165         cleanup_file = file;
166         cleanup_fd_r = fd_r;
167         cleanup_fd_w = fd_w;
168 }
169
170 void cleanup_set_pid(pid_t pid)
171 {
172         cleanup_pid = pid;
173 }