cleanup.c

説明を見る。
00001 /* -*- c-file-style: "linux" -*-
00002 
00003    Copyright (C) 1996-2000 by Andrew Tridgell
00004    Copyright (C) Paul Mackerras 1996
00005    Copyright (C) 2002 by Martin Pool
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 #include "rsync.h"
00023 
00024 extern int io_error;
00025 extern int keep_partial;
00026 extern int log_got_error;
00027 extern char *partial_dir;
00028 
00029 #ifdef HAVE_SIGACTION
00030 static struct sigaction sigact;
00031 #endif
00032 
00033 /**
00034  * Close all open sockets and files, allowing a (somewhat) graceful
00035  * shutdown() of socket connections.  This eliminates the abortive
00036  * TCP RST sent by a Winsock-based system when the close() occurs.
00037  **/
00038 void close_all(void)
00039 {
00040 #ifdef SHUTDOWN_ALL_SOCKETS
00041         int max_fd;
00042         int fd;
00043         int ret;
00044         STRUCT_STAT st;
00045 
00046         max_fd = sysconf(_SC_OPEN_MAX) - 1;
00047         for (fd = max_fd; fd >= 0; fd--) {
00048                 if ((ret = do_fstat(fd, &st)) == 0) {
00049                         if (is_a_socket(fd))
00050                                 ret = shutdown(fd, 2);
00051                         ret = close(fd);
00052                 }
00053         }
00054 #endif
00055 }
00056 
00057 /**
00058  * @file cleanup.c
00059  *
00060  * Code for handling interrupted transfers.  Depending on the @c
00061  * --partial option, we may either delete the temporary file, or go
00062  * ahead and overwrite the destination.  This second behaviour only
00063  * occurs if we've sent literal data and therefore hopefully made
00064  * progress on the transfer.
00065  **/
00066 
00067 /**
00068  * Set to True once literal data has been sent across the link for the
00069  * current file. (????)
00070  *
00071  * Handling the cleanup when a transfer is interrupted is tricky when
00072  * --partial is selected.  We need to ensure that the partial file is
00073  * kept if any real data has been transferred.
00074  **/
00075 int cleanup_got_literal = 0;
00076 
00077 static char *cleanup_fname;
00078 static char *cleanup_new_fname;
00079 static struct file_struct *cleanup_file;
00080 static int cleanup_fd_r, cleanup_fd_w;
00081 static pid_t cleanup_pid = 0;
00082 
00083 pid_t cleanup_child_pid = -1;
00084 
00085 /**
00086  * Eventually calls exit(), passing @p code, therefore does not return.
00087  *
00088  * @param code one of the RERR_* codes from errcode.h.
00089  **/
00090 void _exit_cleanup(int code, const char *file, int line)
00091 {
00092         int ocode = code;
00093         static int inside_cleanup = 0;
00094 
00095         if (inside_cleanup > 10) {
00096                 /* prevent the occasional infinite recursion */
00097                 return;
00098         }
00099         inside_cleanup++;
00100 
00101         SIGACTION(SIGUSR1, SIG_IGN);
00102         SIGACTION(SIGUSR2, SIG_IGN);
00103 
00104         if (verbose > 3) {
00105                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): entered\n",
00106                         code, file, line);
00107         }
00108 
00109         if (cleanup_child_pid != -1) {
00110                 int status;
00111                 if (wait_process(cleanup_child_pid, &status, WNOHANG)
00112                  == cleanup_child_pid) {
00113                         status = WEXITSTATUS(status);
00114                         if (status > code)
00115                                 code = status;
00116                 }
00117         }
00118 
00119         if (cleanup_got_literal && cleanup_fname && keep_partial
00120             && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
00121                 char *fname = cleanup_fname;
00122                 cleanup_fname = NULL;
00123                 if (cleanup_fd_r != -1)
00124                         close(cleanup_fd_r);
00125                 if (cleanup_fd_w != -1) {
00126                         flush_write_file(cleanup_fd_w);
00127                         close(cleanup_fd_w);
00128                 }
00129                 finish_transfer(cleanup_new_fname, fname, NULL,
00130                                 cleanup_file, 0, !partial_dir);
00131         }
00132         io_flush(FULL_FLUSH);
00133         if (cleanup_fname)
00134                 do_unlink(cleanup_fname);
00135         if (code)
00136                 kill_all(SIGUSR1);
00137         if (cleanup_pid && cleanup_pid == getpid()) {
00138                 char *pidf = lp_pid_file();
00139                 if (pidf && *pidf)
00140                         unlink(lp_pid_file());
00141         }
00142 
00143         if (code == 0) {
00144                 if (io_error & IOERR_DEL_LIMIT)
00145                         code = RERR_DEL_LIMIT;
00146                 if (io_error & IOERR_VANISHED)
00147                         code = RERR_VANISHED;
00148                 if (io_error & IOERR_GENERAL || log_got_error)
00149                         code = RERR_PARTIAL;
00150         }
00151 
00152         if (code)
00153                 log_exit(code, file, line);
00154 
00155         if (verbose > 2) {
00156                 rprintf(FINFO,"_exit_cleanup(code=%d, file=%s, line=%d): about to call exit(%d)\n",
00157                         ocode, file, line, code);
00158         }
00159 
00160         close_all();
00161         exit(code);
00162 }
00163 
00164 void cleanup_disable(void)
00165 {
00166         cleanup_fname = NULL;
00167         cleanup_got_literal = 0;
00168 }
00169 
00170 
00171 void cleanup_set(char *fnametmp, char *fname, struct file_struct *file,
00172                  int fd_r, int fd_w)
00173 {
00174         cleanup_fname = fname ? fnametmp : NULL;
00175         cleanup_new_fname = fname;
00176         cleanup_file = file;
00177         cleanup_fd_r = fd_r;
00178         cleanup_fd_w = fd_w;
00179 }
00180 
00181 void cleanup_set_pid(pid_t pid)
00182 {
00183         cleanup_pid = pid;
00184 }

rsyncに対してSat Dec 5 19:45:40 2009に生成されました。  doxygen 1.4.7