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-2007 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 version 3 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
27 extern int keep_partial;
28 extern int log_got_error;
29 extern char *partial_dir;
30 extern char *logfile_name;
33 static struct sigaction sigact;
37 * Close all open sockets and files, allowing a (somewhat) graceful
38 * shutdown() of socket connections. This eliminates the abortive
39 * TCP RST sent by a Winsock-based system when the close() occurs.
43 #ifdef SHUTDOWN_ALL_SOCKETS
49 max_fd = sysconf(_SC_OPEN_MAX) - 1;
50 for (fd = max_fd; fd >= 0; fd--) {
51 if ((ret = do_fstat(fd, &st)) == 0) {
53 ret = shutdown(fd, 2);
63 * Code for handling interrupted transfers. Depending on the @c
64 * --partial option, we may either delete the temporary file, or go
65 * ahead and overwrite the destination. This second behaviour only
66 * occurs if we've sent literal data and therefore hopefully made
67 * progress on the transfer.
71 * Set to True once literal data has been sent across the link for the
72 * current file. (????)
74 * Handling the cleanup when a transfer is interrupted is tricky when
75 * --partial is selected. We need to ensure that the partial file is
76 * kept if any real data has been transferred.
78 int cleanup_got_literal = 0;
80 static char *cleanup_fname;
81 static char *cleanup_new_fname;
82 static struct file_struct *cleanup_file;
83 static int cleanup_fd_r, cleanup_fd_w;
84 static pid_t cleanup_pid = 0;
86 pid_t cleanup_child_pid = -1;
89 * Eventually calls exit(), passing @p code, therefore does not return.
91 * @param code one of the RERR_* codes from errcode.h.
93 NORETURN void _exit_cleanup(int code, const char *file, int line)
95 static int cleanup_step = 0;
96 static int exit_code = 0;
97 static int unmodified_code = 0;
99 SIGACTION(SIGUSR1, SIG_IGN);
100 SIGACTION(SIGUSR2, SIG_IGN);
102 if (exit_code) /* Preserve first error code when recursing. */
105 /* Some of our actions might cause a recursive call back here, so we
106 * keep track of where we are in the cleanup and never repeat a step. */
107 switch (cleanup_step) {
108 #include "case_N.h" /* case 0: cleanup_step++; */
110 exit_code = unmodified_code = code;
114 "_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
121 if (cleanup_child_pid != -1) {
123 int pid = wait_process(cleanup_child_pid, &status, WNOHANG);
124 if (pid == cleanup_child_pid) {
125 status = WEXITSTATUS(status);
127 code = exit_code = status;
134 if (cleanup_got_literal && cleanup_fname && cleanup_new_fname
135 && keep_partial && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
136 char *fname = cleanup_fname;
137 cleanup_fname = NULL;
138 if (cleanup_fd_r != -1)
140 if (cleanup_fd_w != -1) {
141 flush_write_file(cleanup_fd_w);
144 finish_transfer(cleanup_new_fname, fname, NULL, NULL,
145 cleanup_file, 0, !partial_dir);
151 io_flush(FULL_FLUSH);
157 do_unlink(cleanup_fname);
160 if (cleanup_pid && cleanup_pid == getpid()) {
161 char *pidf = lp_pid_file();
163 unlink(lp_pid_file());
167 if (io_error & IOERR_DEL_LIMIT)
168 code = exit_code = RERR_DEL_LIMIT;
169 if (io_error & IOERR_VANISHED)
170 code = exit_code = RERR_VANISHED;
171 if (io_error & IOERR_GENERAL || log_got_error)
172 code = exit_code = RERR_PARTIAL;
175 if (code || am_daemon || (logfile_name && (am_server || !verbose)))
176 log_exit(code, file, line);
183 "_exit_cleanup(code=%d, file=%s, line=%d): "
184 "about to call exit(%d)\n",
185 unmodified_code, file, line, code);
191 if (am_server && code)
203 void cleanup_disable(void)
205 cleanup_fname = cleanup_new_fname = NULL;
206 cleanup_got_literal = 0;
210 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
213 cleanup_fname = fnametmp;
214 cleanup_new_fname = fname; /* can be NULL on a partial-dir failure */
220 void cleanup_set_pid(pid_t pid)