2 * End-of-run cleanup routines.
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
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.
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.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
27 extern int keep_partial;
28 extern int log_got_error;
29 extern char *partial_dir;
32 static struct sigaction sigact;
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.
42 #ifdef SHUTDOWN_ALL_SOCKETS
48 max_fd = sysconf(_SC_OPEN_MAX) - 1;
49 for (fd = max_fd; fd >= 0; fd--) {
50 if ((ret = do_fstat(fd, &st)) == 0) {
52 ret = shutdown(fd, 2);
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.
70 * Set to True once literal data has been sent across the link for the
71 * current file. (????)
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.
77 int cleanup_got_literal = 0;
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;
85 pid_t cleanup_child_pid = -1;
88 * Eventually calls exit(), passing @p code, therefore does not return.
90 * @param code one of the RERR_* codes from errcode.h.
92 void _exit_cleanup(int code, const char *file, int line)
95 static int inside_cleanup = 0;
97 if (inside_cleanup > 10) {
98 /* prevent the occasional infinite recursion */
103 SIGACTION(SIGUSR1, SIG_IGN);
104 SIGACTION(SIGUSR2, SIG_IGN);
107 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
111 if (cleanup_child_pid != -1) {
113 if (wait_process(cleanup_child_pid, &status, WNOHANG)
114 == cleanup_child_pid) {
115 status = WEXITSTATUS(status);
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)
127 if (cleanup_fd_w != -1) {
128 flush_write_file(cleanup_fd_w);
131 finish_transfer(cleanup_new_fname, fname, NULL,
132 cleanup_file, 0, !partial_dir);
134 io_flush(FULL_FLUSH);
136 do_unlink(cleanup_fname);
139 if (cleanup_pid && cleanup_pid == getpid()) {
140 char *pidf = lp_pid_file();
142 unlink(lp_pid_file());
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)
155 log_exit(code, file, line);
158 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
159 ocode, file, line, code);
166 void cleanup_disable(void)
168 cleanup_fname = NULL;
169 cleanup_got_literal = 0;
173 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
176 cleanup_fname = fname ? fnametmp : NULL;
177 cleanup_new_fname = fname;
183 void cleanup_set_pid(pid_t pid)