1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
5 Copyright (C) 2002 by Martin Pool
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.
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.
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.
25 extern int keep_partial;
26 extern int log_got_error;
27 extern char *partial_dir;
30 static struct sigaction sigact;
34 * Close all open sockets and files, allowing a (somewhat) graceful
35 * shutdown() of socket connections. This eliminates the abortive
36 * TCP RST sent by a Winsock-based system when the close() occurs.
40 #ifdef SHUTDOWN_ALL_SOCKETS
46 max_fd = sysconf(_SC_OPEN_MAX) - 1;
47 for (fd = max_fd; fd >= 0; fd--) {
48 if ((ret = do_fstat(fd, &st)) == 0) {
50 ret = shutdown(fd, 2);
60 * Code for handling interrupted transfers. Depending on the @c
61 * --partial option, we may either delete the temporary file, or go
62 * ahead and overwrite the destination. This second behaviour only
63 * occurs if we've sent literal data and therefore hopefully made
64 * progress on the transfer.
68 * Set to True once literal data has been sent across the link for the
69 * current file. (????)
71 * Handling the cleanup when a transfer is interrupted is tricky when
72 * --partial is selected. We need to ensure that the partial file is
73 * kept if any real data has been transferred.
75 int cleanup_got_literal = 0;
77 static char *cleanup_fname;
78 static char *cleanup_new_fname;
79 static struct file_struct *cleanup_file;
80 static int cleanup_fd_r, cleanup_fd_w;
81 static pid_t cleanup_pid = 0;
83 pid_t cleanup_child_pid = -1;
86 * Eventually calls exit(), passing @p code, therefore does not return.
88 * @param code one of the RERR_* codes from errcode.h.
90 void _exit_cleanup(int code, const char *file, int line)
93 static int inside_cleanup = 0;
95 if (inside_cleanup > 10) {
96 /* prevent the occasional infinite recursion */
101 SIGACTION(SIGUSR1, SIG_IGN);
102 SIGACTION(SIGUSR2, SIG_IGN);
105 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
109 if (cleanup_child_pid != -1) {
111 if (wait_process(cleanup_child_pid, &status, WNOHANG)
112 == cleanup_child_pid) {
113 status = WEXITSTATUS(status);
119 if (cleanup_got_literal && cleanup_fname && keep_partial
120 && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
121 char *fname = cleanup_fname;
122 cleanup_fname = NULL;
123 if (cleanup_fd_r != -1)
125 if (cleanup_fd_w != -1) {
126 flush_write_file(cleanup_fd_w);
129 finish_transfer(cleanup_new_fname, fname, NULL,
130 cleanup_file, 0, !partial_dir);
132 io_flush(FULL_FLUSH);
134 do_unlink(cleanup_fname);
137 if (cleanup_pid && cleanup_pid == getpid()) {
138 char *pidf = lp_pid_file();
140 unlink(lp_pid_file());
144 if (io_error & IOERR_DEL_LIMIT)
145 code = RERR_DEL_LIMIT;
146 if (io_error & IOERR_VANISHED)
147 code = RERR_VANISHED;
148 if (io_error & IOERR_GENERAL || log_got_error)
153 log_exit(code, file, line);
156 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
157 ocode, file, line, code);
164 void cleanup_disable(void)
166 cleanup_fname = NULL;
167 cleanup_got_literal = 0;
171 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
174 cleanup_fname = fname ? fnametmp : NULL;
175 cleanup_new_fname = fname;
181 void cleanup_set_pid(pid_t pid)