Commit | Line | Data |
---|---|---|
ef1aa910 MP |
1 | /* -*- c-file-style: "linux" -*- |
2 | ||
3 | Copyright (C) 1996-2000 by Andrew Tridgell | |
2f03f956 | 4 | Copyright (C) Paul Mackerras 1996 |
e0fde757 | 5 | Copyright (C) 2002 by Martin Pool |
2f03f956 AT |
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 | ||
9f639210 DD |
24 | /** |
25 | * Close all open sockets and files, allowing a (somewhat) graceful | |
26 | * shutdown() of socket connections. This eliminates the abortive | |
27 | * TCP RST sent by a Winsock-based system when the close() occurs. | |
28 | **/ | |
18d6b679 | 29 | void close_all(void) |
9f639210 DD |
30 | { |
31 | #ifdef SHUTDOWN_ALL_SOCKETS | |
32 | int max_fd; | |
33 | int fd; | |
34 | int ret; | |
35 | struct stat st; | |
36 | ||
37 | max_fd = sysconf(_SC_OPEN_MAX) - 1; | |
38 | for (fd = max_fd; fd >= 0; fd--) { | |
39 | ret = fstat(fd,&st); | |
40 | if (fstat(fd,&st) == 0) { | |
41 | if (is_a_socket(fd)) { | |
42 | ret = shutdown(fd, 2); | |
43 | } | |
44 | ret = close(fd); | |
45 | } | |
46 | } | |
47 | #endif | |
48 | } | |
49 | ||
e0fde757 MP |
50 | /** |
51 | * @file cleanup.c | |
52 | * | |
53 | * Code for handling interrupted transfers. Depending on the @c | |
54 | * --partial option, we may either delete the temporary file, or go | |
55 | * ahead and overwrite the destination. This second behaviour only | |
56 | * occurs if we've sent literal data and therefore hopefully made | |
57 | * progress on the transfer. | |
58 | **/ | |
59 | ||
60 | /** | |
61 | * Set to True once literal data has been sent across the link for the | |
62 | * current file. (????) | |
63 | * | |
64 | * Handling the cleanup when a transfer is interrupted is tricky when | |
65 | * --partial is selected. We need to ensure that the partial file is | |
66 | * kept if any real data has been transferred. | |
67 | **/ | |
2f03f956 AT |
68 | int cleanup_got_literal=0; |
69 | ||
70 | static char *cleanup_fname; | |
71 | static char *cleanup_new_fname; | |
72 | static struct file_struct *cleanup_file; | |
c6b81a98 AT |
73 | static int cleanup_fd1, cleanup_fd2; |
74 | static struct map_struct *cleanup_buf; | |
65fc84b3 | 75 | static pid_t cleanup_pid = 0; |
9bd65976 | 76 | extern int io_error; |
2f03f956 | 77 | |
19b27a48 | 78 | pid_t cleanup_child_pid = -1; |
ef1aa910 | 79 | |
e0fde757 | 80 | /** |
b0f451eb | 81 | * Eventually calls exit(), passing @p code, therefore does not return. |
e0fde757 MP |
82 | * |
83 | * @param code one of the RERR_* codes from errcode.h. | |
84 | **/ | |
a9766ef1 | 85 | void _exit_cleanup(int code, const char *file, int line) |
2f03f956 | 86 | { |
9098bbf3 | 87 | int ocode = code; |
2f03f956 | 88 | extern int keep_partial; |
ff81e809 | 89 | extern int log_got_error; |
b765ec32 DD |
90 | static int inside_cleanup = 0; |
91 | ||
aa2c47d8 | 92 | if (inside_cleanup > 10) { |
b765ec32 DD |
93 | /* prevent the occasional infinite recursion */ |
94 | return; | |
95 | } | |
aa2c47d8 | 96 | inside_cleanup++; |
2f03f956 AT |
97 | |
98 | signal(SIGUSR1, SIG_IGN); | |
8b35435f | 99 | signal(SIGUSR2, SIG_IGN); |
2f03f956 | 100 | |
9098bbf3 MP |
101 | if (verbose > 3) |
102 | rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n", | |
103 | code, file, line); | |
104 | ||
19b27a48 AT |
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) code = status; | |
110 | } | |
111 | } | |
112 | ||
2f03f956 AT |
113 | if (cleanup_got_literal && cleanup_fname && keep_partial) { |
114 | char *fname = cleanup_fname; | |
115 | cleanup_fname = NULL; | |
c6b81a98 AT |
116 | if (cleanup_buf) unmap_file(cleanup_buf); |
117 | if (cleanup_fd1 != -1) close(cleanup_fd1); | |
118 | if (cleanup_fd2 != -1) close(cleanup_fd2); | |
2f03f956 AT |
119 | finish_transfer(cleanup_new_fname, fname, cleanup_file); |
120 | } | |
ef732c3b | 121 | io_flush(FULL_FLUSH); |
2f03f956 AT |
122 | if (cleanup_fname) |
123 | do_unlink(cleanup_fname); | |
124 | if (code) { | |
125 | kill_all(SIGUSR1); | |
126 | } | |
65fc84b3 | 127 | if (cleanup_pid && cleanup_pid == getpid()) { |
27d3cdbc AT |
128 | char *pidf = lp_pid_file(); |
129 | if (pidf && *pidf) { | |
130 | unlink(lp_pid_file()); | |
131 | } | |
132 | } | |
9b73d1c0 | 133 | |
6e35c72f WD |
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; | |
ff81e809 AT |
139 | } |
140 | ||
19b27a48 AT |
141 | if (code) log_exit(code, file, line); |
142 | ||
9098bbf3 MP |
143 | if (verbose > 2) |
144 | rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n", | |
145 | ocode, file, line, code); | |
146 | ||
9f639210 | 147 | close_all(); |
2f03f956 AT |
148 | exit(code); |
149 | } | |
150 | ||
151 | void cleanup_disable(void) | |
152 | { | |
153 | cleanup_fname = NULL; | |
154 | cleanup_got_literal = 0; | |
155 | } | |
156 | ||
157 | ||
c6b81a98 AT |
158 | void cleanup_set(char *fnametmp, char *fname, struct file_struct *file, |
159 | struct map_struct *buf, int fd1, int fd2) | |
2f03f956 AT |
160 | { |
161 | cleanup_fname = fnametmp; | |
162 | cleanup_new_fname = fname; | |
163 | cleanup_file = file; | |
c6b81a98 AT |
164 | cleanup_buf = buf; |
165 | cleanup_fd1 = fd1; | |
166 | cleanup_fd2 = fd2; | |
2f03f956 | 167 | } |
8638dd48 | 168 | |
65fc84b3 | 169 | void cleanup_set_pid(pid_t pid) |
8638dd48 DD |
170 | { |
171 | cleanup_pid = pid; | |
172 | } |