X-Git-Url: http://git.tuebingen.mpg.de/?p=dss.git;a=blobdiff_plain;f=dss.c;h=24ef6ccc83549231252ee093bedded5a911951d0;hp=4ca1743a63aa9011348f579bebc78199c28bbaae;hb=f446dc1416090076177df45a568e195aaa9a8d1b;hpb=70e306b60780c8bd3574b5668adc7f18f3669c9f diff --git a/dss.c b/dss.c index 4ca1743..24ef6cc 100644 --- a/dss.c +++ b/dss.c @@ -32,7 +32,7 @@ struct gengetopt_args_info conf; char *dss_error_txt = NULL; static FILE *logfile; -int signal_pipe; +static int signal_pipe; /** Process id of current rsync process. */ static pid_t rsync_pid; @@ -75,7 +75,9 @@ int call_command_handler(void) * incomplete, being deleted: 1204565370-incomplete.being_deleted */ enum snapshot_status_flags { + /** The rsync process terminated successfully. */ SS_COMPLETE = 1, + /** The rm process is running to remove this snapshot. */ SS_BEING_DELETED = 2, }; @@ -149,6 +151,7 @@ unsigned num_snapshots(int interval) return 1 << n; } +/* return: Whether dirname is a snapshot directory (0: no, 1: yes) */ int is_snapshot(const char *dirname, int64_t now, struct snapshot *s) { int i, ret; @@ -327,6 +330,22 @@ void get_snapshot_list(struct snapshot_list *sl) compare_snapshots); } +void free_snapshot_list(struct snapshot_list *sl) +{ + int i; + struct snapshot *s; + + FOR_EACH_SNAPSHOT(s, i, sl) { + free(s->name); + free(s); + } + free(sl->interval_count); + sl->interval_count = NULL; + free(sl->snapshots); + sl->snapshots = NULL; + sl->num_snapshots = 0; +} + void stop_rsync_process(void) { if (!rsync_pid || rsync_stopped) @@ -343,19 +362,6 @@ void restart_rsync_process(void) rsync_stopped = 0; } -void free_snapshot_list(struct snapshot_list *sl) -{ - int i; - struct snapshot *s; - - FOR_EACH_SNAPSHOT(s, i, sl) { - free(s->name); - free(s); - } - free(sl->interval_count); - free(sl->snapshots); -} - /** * Print a log message about the exit status of a child. */ @@ -379,7 +385,7 @@ int wait_for_process(pid_t pid, int *status) for (;;) { pause(); ret = next_signal(); - if (ret < 0) + if (ret < 0) break; if (!ret) continue; @@ -387,16 +393,18 @@ int wait_for_process(pid_t pid, int *status) ret = waitpid(pid, status, 0); if (ret >= 0) break; - if (errno != EINTR) /* error */ + if (errno != EINTR) { /* error */ + ret = -ERRNO_TO_DSS_ERROR(errno); break; + } } + /* SIGINT or SIGTERM */ DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid); kill(pid, SIGTERM); } - if (ret < 0) { - ret = -ERRNO_TO_DSS_ERROR(errno); + if (ret < 0) make_err_msg("failed to wait for process %d", (int)pid); - } else + else log_termination_msg(pid, *status); return ret; } @@ -418,6 +426,7 @@ out: free(new_name); return ret; } + /* * return: 0: no redundant snapshots, 1: rm process started, negative: error */ @@ -523,7 +532,6 @@ out: return ret; } - int wait_for_rm_process(void) { int status, ret = wait_for_process(rm_pid, &status); @@ -541,9 +549,95 @@ void kill_process(pid_t pid) kill(pid, SIGTERM); } -void handle_sighup() +int check_config(void) +{ + if (conf.unit_interval_arg <= 0) { + make_err_msg("bad unit interval: %i", conf.unit_interval_arg); + return -E_INVALID_NUMBER; + } + DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg); + if (conf.num_intervals_arg <= 0) { + make_err_msg("bad number of intervals %i", conf.num_intervals_arg); + return -E_INVALID_NUMBER; + } + DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg); + return 1; +} + +/* exits on errors */ +void parse_config_file(int override) +{ + int ret; + char *config_file; + struct stat statbuf; + char *old_logfile_arg = NULL; + int old_daemon_given = 0; + + if (conf.config_file_given) + config_file = dss_strdup(conf.config_file_arg); + else { + char *home = get_homedir(); + config_file = make_message("%s/.dssrc", home); + free(home); + } + if (override) { /* SIGHUP */ + if (conf.logfile_given) + old_logfile_arg = dss_strdup(conf.logfile_arg); + old_daemon_given = conf.daemon_given; + } + + ret = stat(config_file, &statbuf); + if (ret && conf.config_file_given) { + ret = -ERRNO_TO_DSS_ERROR(errno); + make_err_msg("failed to stat config file %s", config_file); + goto out; + } + if (!ret) { + struct cmdline_parser_params params = { + .override = override, + .initialize = 0, + .check_required = 0, + .check_ambiguity = 0 + }; + cmdline_parser_config_file(config_file, &conf, ¶ms); + } + if (!conf.source_dir_given || !conf.dest_dir_given) { + ret = -E_SYNTAX; + make_err_msg("you need to specify both source_dir and dest_dir"); + goto out; + } + ret = check_config(); + if (ret < 0) + goto out; + if (override) { + /* don't change daemon mode on SIGHUP */ + conf.daemon_given = old_daemon_given; + close_log(logfile); + logfile = NULL; + if (conf.logfile_given) + free(old_logfile_arg); + else if (conf.daemon_given) { /* re-use old logfile */ + conf.logfile_arg = old_logfile_arg; + conf.logfile_given = 1; + } + } + if (conf.logfile_given) { + logfile = open_log(conf.logfile_arg); + log_welcome(conf.loglevel_arg); + } + ret = dss_chdir(conf.dest_dir_arg); +out: + free(config_file); + if (ret >= 0) + return; + log_err_msg(EMERG, -ret); + exit(EXIT_FAILURE); +} + +void handle_sighup(void) { - DSS_INFO_LOG("FIXME: no sighup handling yet\n"); + DSS_NOTICE_LOG("SIGHUP\n"); + parse_config_file(1); } int rename_incomplete_snapshot(int64_t start) @@ -563,7 +657,6 @@ int rename_incomplete_snapshot(int64_t start) return ret; } - int handle_rsync_exit(int status) { int es, ret; @@ -587,7 +680,6 @@ out: return ret; } - int get_newest_complete(const char *dirname, void *private) { struct edge_snapshot_data *esd = private; @@ -701,7 +793,7 @@ void compute_next_snapshot_time(struct snapshot_list *sl) tv_add(&now, &tmp, &next_snapshot_time); } -void handle_signal(struct snapshot_list *sl) +void handle_signal(void) { int sig, ret = next_signal(); @@ -719,6 +811,7 @@ void handle_signal(struct snapshot_list *sl) exit(EXIT_FAILURE); case SIGHUP: handle_sighup(); + ret = 1; break; case SIGCHLD: ret = reap_child(&pid, &status); @@ -729,9 +822,6 @@ void handle_signal(struct snapshot_list *sl) ret = handle_rsync_exit(status); else ret = handle_rm_exit(status); - free_snapshot_list(sl); - get_snapshot_list(sl); - compute_next_snapshot_time(sl); } out: if (ret < 0) @@ -817,14 +907,15 @@ int select_loop(void) struct timeval tv = {.tv_sec = 0, .tv_usec = 0}; struct snapshot_list sl = {.num_snapshots = 0}; - get_snapshot_list(&sl); - compute_next_snapshot_time(&sl); for (;;) { struct timeval now, *tvp = &tv; fd_set rfds; int low_disk_space; char **rsync_argv; + free_snapshot_list(&sl); + get_snapshot_list(&sl); + compute_next_snapshot_time(&sl); FD_ZERO(&rfds); FD_SET(signal_pipe, &rfds); if (rsync_pid) @@ -834,8 +925,10 @@ int select_loop(void) ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp); if (ret < 0) return ret; - if (FD_ISSET(signal_pipe, &rfds)) - handle_signal(&sl); + if (FD_ISSET(signal_pipe, &rfds)) { + handle_signal(); + continue; + } if (rm_pid) continue; ret = disk_space_low(); @@ -970,68 +1063,11 @@ int com_ls(void) return 1; } -/* TODO: Unlink pid file */ __noreturn void clean_exit(int status) { - //kill(0, SIGTERM); free(dss_error_txt); exit(status); } - -int read_config_file(void) -{ - int ret; - char *config_file; - struct stat statbuf; - - if (conf.config_file_given) - config_file = dss_strdup(conf.config_file_arg); - else { - char *home = get_homedir(); - config_file = make_message("%s/.dssrc", home); - free(home); - } - ret = stat(config_file, &statbuf); - if (ret && conf.config_file_given) { - ret = -ERRNO_TO_DSS_ERROR(errno); - make_err_msg("failed to stat config file %s", config_file); - goto out; - } - if (!ret) { - struct cmdline_parser_params params = { - .override = 0, - .initialize = 0, - .check_required = 0, - .check_ambiguity = 0 - }; - cmdline_parser_config_file(config_file, &conf, ¶ms); - } - if (!conf.source_dir_given || !conf.dest_dir_given) { - ret = -E_SYNTAX; - make_err_msg("you need to specify both source_dir and dest_dir"); - goto out; - } - ret = 1; -out: - free(config_file); - return ret; -} - -int check_config(void) -{ - if (conf.unit_interval_arg <= 0) { - make_err_msg("bad unit interval: %i", conf.unit_interval_arg); - return -E_INVALID_NUMBER; - } - DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg); - if (conf.num_intervals_arg <= 0) { - make_err_msg("bad number of intervals %i", conf.num_intervals_arg); - return -E_INVALID_NUMBER; - } - DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg); - return 1; -} - static void setup_signal_handling(void) { int ret; @@ -1053,30 +1089,16 @@ err: exit(EXIT_FAILURE); } - int main(int argc, char **argv) { int ret; cmdline_parser(argc, argv, &conf); /* aborts on errors */ - ret = read_config_file(); - if (ret < 0) - goto out; - ret = check_config(); - if (ret < 0) - goto out; - if (conf.logfile_given) { - logfile = open_log(conf.logfile_arg); - log_welcome(conf.loglevel_arg); - } - ret = dss_chdir(conf.dest_dir_arg); - if (ret < 0) - goto out; + parse_config_file(0); if (conf.daemon_given) daemon_init(); setup_signal_handling(); ret = call_command_handler(); -out: if (ret < 0) log_err_msg(EMERG, -ret); clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);