File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / rsync / cleanup.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:32:36 2021 UTC (3 years, 3 months ago) by misho
Branches: rsync, MAIN
CVS tags: v3_2_3, HEAD
rsync 3.2.3

    1: /*
    2:  * End-of-run cleanup routines.
    3:  *
    4:  * Copyright (C) 1996-2000 Andrew Tridgell
    5:  * Copyright (C) 1996 Paul Mackerras
    6:  * Copyright (C) 2002 Martin Pool
    7:  * Copyright (C) 2003-2020 Wayne Davison
    8:  *
    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 3 of the License, or
   12:  * (at your option) any later version.
   13:  *
   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.
   18:  *
   19:  * You should have received a copy of the GNU General Public License along
   20:  * with this program; if not, visit the http://fsf.org website.
   21:  */
   22: 
   23: #include "rsync.h"
   24: 
   25: extern int dry_run;
   26: extern int am_server;
   27: extern int am_daemon;
   28: extern int am_receiver;
   29: extern int am_sender;
   30: extern int io_error;
   31: extern int use_db;
   32: extern int keep_partial;
   33: extern int got_xfer_error;
   34: extern int protocol_version;
   35: extern int output_needs_newline;
   36: extern char *partial_dir;
   37: extern char *logfile_name;
   38: 
   39: int called_from_signal_handler = 0;
   40: BOOL shutting_down = False;
   41: BOOL flush_ok_after_signal = False;
   42: 
   43: #ifdef HAVE_SIGACTION
   44: static struct sigaction sigact;
   45: #endif
   46: 
   47: /**
   48:  * Close all open sockets and files, allowing a (somewhat) graceful
   49:  * shutdown() of socket connections.  This eliminates the abortive
   50:  * TCP RST sent by a Winsock-based system when the close() occurs.
   51:  **/
   52: void close_all(void)
   53: {
   54: #ifdef SHUTDOWN_ALL_SOCKETS
   55: 	int max_fd;
   56: 	int fd;
   57: 	int ret;
   58: 	STRUCT_STAT st;
   59: 
   60: 	max_fd = sysconf(_SC_OPEN_MAX) - 1;
   61: 	for (fd = max_fd; fd >= 0; fd--) {
   62: 		if ((ret = do_fstat(fd, &st)) == 0) {
   63: 			if (is_a_socket(fd))
   64: 				ret = shutdown(fd, 2);
   65: 			ret = close(fd);
   66: 		}
   67: 	}
   68: #endif
   69: }
   70: 
   71: /**
   72:  * @file cleanup.c
   73:  *
   74:  * Code for handling interrupted transfers.  Depending on the @c
   75:  * --partial option, we may either delete the temporary file, or go
   76:  * ahead and overwrite the destination.  This second behaviour only
   77:  * occurs if we've sent literal data and therefore hopefully made
   78:  * progress on the transfer.
   79:  **/
   80: 
   81: /**
   82:  * Set to True once literal data has been sent across the link for the
   83:  * current file. (????)
   84:  *
   85:  * Handling the cleanup when a transfer is interrupted is tricky when
   86:  * --partial is selected.  We need to ensure that the partial file is
   87:  * kept if any real data has been transferred.
   88:  **/
   89: int cleanup_got_literal = 0;
   90: 
   91: static const char *cleanup_fname;
   92: static const char *cleanup_new_fname;
   93: static struct file_struct *cleanup_file;
   94: static int cleanup_fd_r = -1, cleanup_fd_w = -1;
   95: static pid_t cleanup_pid = 0;
   96: 
   97: pid_t cleanup_child_pid = -1;
   98: 
   99: /**
  100:  * Eventually calls exit(), passing @p code, therefore does not return.
  101:  *
  102:  * @param code one of the RERR_* codes from errcode.h.
  103:  **/
  104: NORETURN void _exit_cleanup(int code, const char *file, int line)
  105: {
  106: 	static int switch_step = 0;
  107: 	static int exit_code = 0, exit_line = 0;
  108: 	static const char *exit_file = NULL;
  109: 	static int first_code = 0;
  110: 
  111: 	SIGACTION(SIGUSR1, SIG_IGN);
  112: 	SIGACTION(SIGUSR2, SIG_IGN);
  113: 
  114: 	if (!exit_code) { /* Preserve first error exit info when recursing. */
  115: 		exit_code = code;
  116: 		exit_file = file;
  117: 		exit_line = line < 0 ? -line : line;
  118: 	}
  119: 
  120: 	/* If this is the exit at the end of the run, the server side
  121: 	 * should not attempt to output a message (see log_exit()). */
  122: 	if (am_server && code == 0)
  123: 		am_server = 2;
  124: 
  125: 	/* Some of our actions might cause a recursive call back here, so we
  126: 	 * keep track of where we are in the cleanup and never repeat a step. */
  127: 	switch (switch_step) {
  128: #include "case_N.h" /* case 0: */
  129: 		switch_step++;
  130: 
  131: 		first_code = code;
  132: 
  133: 		if (output_needs_newline) {
  134: 			fputc('\n', stdout);
  135: 			output_needs_newline = 0;
  136: 		}
  137: 
  138: 		if (DEBUG_GTE(EXIT, 2)) {
  139: 			rprintf(FINFO,
  140: 				"[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n",
  141: 				who_am_i(), code, src_file(file), line);
  142: 		}
  143: 
  144: #include "case_N.h"
  145: 		switch_step++;
  146: 
  147: 		if (use_db)
  148: 			db_disconnect(False);
  149: 
  150: 		/* FALLTHROUGH */
  151: #include "case_N.h"
  152: 
  153: 		if (cleanup_child_pid != -1) {
  154: 			int status;
  155: 			int pid = wait_process(cleanup_child_pid, &status, WNOHANG);
  156: 			if (pid == cleanup_child_pid) {
  157: 				status = WEXITSTATUS(status);
  158: 				if (status > exit_code)
  159: 					exit_code = status;
  160: 			}
  161: 		}
  162: 
  163: #include "case_N.h"
  164: 		switch_step++;
  165: 
  166: 		if (cleanup_got_literal && (cleanup_fname || cleanup_fd_w != -1)) {
  167: 			if (cleanup_fd_r != -1) {
  168: 				close(cleanup_fd_r);
  169: 				cleanup_fd_r = -1;
  170: 			}
  171: 			if (cleanup_fd_w != -1) {
  172: 				flush_write_file(cleanup_fd_w);
  173: 				close(cleanup_fd_w);
  174: 				cleanup_fd_w = -1;
  175: 			}
  176: 			if (cleanup_fname && cleanup_new_fname && keep_partial
  177: 			 && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
  178: 				int tweak_modtime = 0;
  179: 				const char *fname = cleanup_fname;
  180: 				cleanup_fname = NULL;
  181: 				if (!partial_dir) {
  182: 					/* We don't want to leave a partial file with a modern time or it
  183: 					 * could be skipped via --update.  Setting the time to something
  184: 					 * really old also helps it to stand out as unfinished in an ls. */
  185: 					tweak_modtime = 1;
  186: 					cleanup_file->modtime = 0;
  187: 				}
  188: 				finish_transfer(cleanup_new_fname, fname, NULL, NULL,
  189: 						cleanup_file, tweak_modtime, !partial_dir);
  190: 			}
  191: 		}
  192: 
  193: #include "case_N.h"
  194: 		switch_step++;
  195: 
  196: 		if (flush_ok_after_signal) {
  197: 			flush_ok_after_signal = False;
  198: 			if (code == RERR_SIGNAL)
  199: 				io_flush(FULL_FLUSH);
  200: 		}
  201: 		if (!exit_code && !code)
  202: 			io_flush(FULL_FLUSH);
  203: 
  204: #include "case_N.h"
  205: 		switch_step++;
  206: 
  207: 		if (cleanup_fname)
  208: 			do_unlink(cleanup_fname);
  209: 		if (exit_code)
  210: 			kill_all(SIGUSR1);
  211: 		if (cleanup_pid && cleanup_pid == getpid()) {
  212: 			char *pidf = lp_pid_file();
  213: 			if (pidf && *pidf)
  214: 				unlink(lp_pid_file());
  215: 		}
  216: 
  217: 		if (exit_code == 0) {
  218: 			if (code)
  219: 				exit_code = code;
  220: 			if (io_error & IOERR_DEL_LIMIT)
  221: 				exit_code = RERR_DEL_LIMIT;
  222: 			if (io_error & IOERR_VANISHED)
  223: 				exit_code = RERR_VANISHED;
  224: 			if (io_error & IOERR_GENERAL || got_xfer_error)
  225: 				exit_code = RERR_PARTIAL;
  226: 		}
  227: 
  228: 		/* If line < 0, this exit is after a MSG_ERROR_EXIT event, so
  229: 		 * we don't want to output a duplicate error. */
  230: 		if ((exit_code && line > 0)
  231: 		 || am_daemon || (logfile_name && (am_server || !INFO_GTE(STATS, 1)))) {
  232: 			log_exit(exit_code, exit_file, exit_line);
  233: 		}
  234: 
  235: #include "case_N.h"
  236: 		switch_step++;
  237: 
  238: 		if (DEBUG_GTE(EXIT, 1)) {
  239: 			rprintf(FINFO,
  240: 				"[%s] _exit_cleanup(code=%d, file=%s, line=%d): "
  241: 				"about to call exit(%d)%s\n",
  242: 				who_am_i(), first_code, exit_file, exit_line, exit_code,
  243: 				dry_run ? " (DRY RUN)" : "");
  244: 		}
  245: 
  246: #include "case_N.h"
  247: 		switch_step++;
  248: 
  249: 		if (exit_code && exit_code != RERR_SOCKETIO && exit_code != RERR_STREAMIO && exit_code != RERR_SIGNAL1
  250: 		 && exit_code != RERR_TIMEOUT && !shutting_down) {
  251: 			if (protocol_version >= 31 || am_receiver) {
  252: 				if (line > 0) {
  253: 					if (DEBUG_GTE(EXIT, 3)) {
  254: 						rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
  255: 							who_am_i(), exit_code);
  256: 					}
  257: 					send_msg_int(MSG_ERROR_EXIT, exit_code);
  258: 				}
  259: 				if (!am_sender)
  260: 					io_flush(MSG_FLUSH); /* Be sure to send all messages */
  261: 				noop_io_until_death();
  262: 			}
  263: 			else if (!am_sender)
  264: 				io_flush(MSG_FLUSH); /* Be sure to send all messages */
  265: 		}
  266: 
  267: #include "case_N.h"
  268: 		switch_step++;
  269: 
  270: 		if (am_server && exit_code)
  271: 			msleep(100);
  272: 		close_all();
  273: 
  274: 		/* FALLTHROUGH */
  275: 	default:
  276: 		break;
  277: 	}
  278: 
  279: 	if (called_from_signal_handler)
  280: 		_exit(exit_code);
  281: 	exit(exit_code);
  282: }
  283: 
  284: void cleanup_disable(void)
  285: {
  286: 	cleanup_fname = cleanup_new_fname = NULL;
  287: 	cleanup_fd_r = cleanup_fd_w = -1;
  288: 	cleanup_got_literal = 0;
  289: }
  290: 
  291: 
  292: void cleanup_set(const char *fnametmp, const char *fname, struct file_struct *file,
  293: 		 int fd_r, int fd_w)
  294: {
  295: 	cleanup_fname = fnametmp;
  296: 	cleanup_new_fname = fname; /* can be NULL on a partial-dir failure */
  297: 	cleanup_file = file;
  298: 	cleanup_fd_r = fd_r;
  299: 	cleanup_fd_w = fd_w;
  300: }
  301: 
  302: void cleanup_set_pid(pid_t pid)
  303: {
  304: 	cleanup_pid = pid;
  305: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>