Move snapshot helpers to new file snap.[ch].
[dss.git] / dss.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <signal.h>
8 #include <ctype.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include <sys/time.h>
13 #include <time.h>
14 #include <sys/wait.h>
15 #include <fnmatch.h>
16 #include <limits.h>
17
18
19 #include "gcc-compat.h"
20 #include "cmdline.h"
21 #include "log.h"
22 #include "string.h"
23 #include "error.h"
24 #include "fd.h"
25 #include "exec.h"
26 #include "daemon.h"
27 #include "signal.h"
28 #include "df.h"
29 #include "time.h"
30 #include "snap.h"
31
32
33 static struct gengetopt_args_info conf;
34 static FILE *logfile;
35 static int signal_pipe;
36
37 /** Process id of current rsync process. */
38 static pid_t rsync_pid;
39 /** Whether the rsync process is currently stopped */
40 static int rsync_stopped;
41 /** Process id of current rm process. */
42 static pid_t rm_pid;
43 /** When the next snapshot is due. */
44 struct timeval next_snapshot_time;
45 /** The pid of the pre-create hook. */
46 pid_t pre_create_hook_pid;
47 /** The pid of the post-create hook. */
48 pid_t post_create_hook_pid;
49 /* Creation time of the snapshot currently being created. */
50 int64_t current_snapshot_creation_time;
51
52 static char *path_to_last_complete_snapshot;
53
54 static unsigned snapshot_creation_status;
55
56
57 DEFINE_DSS_ERRLIST;
58
59
60 /* a litte cpp magic helps to DRY */
61 #define COMMANDS \
62 COMMAND(ls) \
63 COMMAND(create) \
64 COMMAND(prune) \
65 COMMAND(run)
66 #define COMMAND(x) int com_ ##x(void);
67 COMMANDS
68 #undef COMMAND
69 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
70 int call_command_handler(void)
71 {
72 COMMANDS
73 DSS_EMERG_LOG("BUG: did not find command handler\n");
74 exit(EXIT_FAILURE);
75 }
76 #undef COMMAND
77 #undef COMMANDS
78
79 __printf_2_3 void dss_log(int ll, const char* fmt,...)
80 {
81 va_list argp;
82 FILE *outfd;
83 struct tm *tm;
84 time_t t1;
85 char str[255] = "";
86
87 if (ll < conf.loglevel_arg)
88 return;
89 outfd = logfile? logfile : stderr;
90 time(&t1);
91 tm = localtime(&t1);
92 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
93 fprintf(outfd, "%s ", str);
94 if (conf.loglevel_arg <= INFO)
95 fprintf(outfd, "%i: ", ll);
96 va_start(argp, fmt);
97 vfprintf(outfd, fmt, argp);
98 va_end(argp);
99 }
100
101 /**
102 * Print a message either to stdout or to the log file.
103 */
104 __printf_1_2 void dss_msg(const char* fmt,...)
105 {
106 FILE *outfd = conf.daemon_given? logfile : stdout;
107 va_list argp;
108 va_start(argp, fmt);
109 vfprintf(outfd, fmt, argp);
110 va_end(argp);
111 }
112
113 /* TODO: Also consider number of inodes. */
114 int disk_space_low(void)
115 {
116 struct disk_space ds;
117 int ret = get_disk_space(".", &ds);
118
119 if (ret < 0)
120 return ret;
121 if (conf.min_free_mb_arg)
122 if (ds.free_mb < conf.min_free_mb_arg)
123 return 1;
124 if (conf.min_free_percent_arg)
125 if (ds.percent_free < conf.min_free_percent_arg)
126 return 1;
127 return 0;
128 }
129
130 void dss_get_snapshot_list(struct snapshot_list *sl)
131 {
132 get_snapshot_list(sl, conf.unit_interval_arg, conf.num_intervals_arg);
133 }
134
135 void compute_next_snapshot_time(void)
136 {
137 struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
138 tmp, diff;
139 int64_t x = 0;
140 unsigned wanted = desired_number_of_snapshots(0, conf.num_intervals_arg),
141 num_complete_snapshots = 0;
142 int i, ret;
143 struct snapshot *s = NULL;
144 struct snapshot_list sl;
145
146 assert(snapshot_creation_status == SCS_READY);
147 current_snapshot_creation_time = 0;
148 dss_get_snapshot_list(&sl);
149 FOR_EACH_SNAPSHOT(s, i, &sl) {
150 if (!(s->flags & SS_COMPLETE))
151 continue;
152 num_complete_snapshots++;
153 x += s->completion_time - s->creation_time;
154 }
155 assert(x >= 0);
156 if (num_complete_snapshots)
157 x /= num_complete_snapshots; /* avg time to create one snapshot */
158 x *= wanted; /* time to create all snapshots in interval 0 */
159 tmp.tv_sec = x;
160 tmp.tv_usec = 0;
161 ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
162 gettimeofday(&now, NULL);
163 if (ret < 0 || !s)
164 goto min_sleep;
165 tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */
166 diff.tv_sec = s->completion_time;
167 diff.tv_usec = 0;
168 tv_add(&diff, &tmp, &next_snapshot_time);
169 if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
170 goto out;
171 min_sleep:
172 next_snapshot_time = now;
173 next_snapshot_time.tv_sec += 60;
174 out:
175 free_snapshot_list(&sl);
176 }
177
178
179 int remove_snapshot(struct snapshot *s)
180 {
181 int fds[3] = {0, 0, 0};
182 assert(!rm_pid);
183 char *new_name = being_deleted_name(s);
184 int ret = dss_rename(s->name, new_name);
185 char *argv[] = {"rm", "-rf", new_name, NULL};
186
187 if (ret < 0)
188 goto out;
189 DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
190 ret = dss_exec(&rm_pid, argv[0], argv, fds);
191 out:
192 free(new_name);
193 return ret;
194 }
195
196 /*
197 * return: 0: no redundant snapshots, 1: rm process started, negative: error
198 */
199 int remove_redundant_snapshot(struct snapshot_list *sl)
200 {
201 int ret, i, interval;
202 struct snapshot *s;
203 unsigned missing = 0;
204
205 DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
206 for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
207 unsigned keep = desired_number_of_snapshots(interval, conf.num_intervals_arg);
208 unsigned num = sl->interval_count[interval];
209 struct snapshot *victim = NULL, *prev = NULL;
210 int64_t score = LONG_MAX;
211
212 if (keep >= num)
213 missing += keep - num;
214 // DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
215 // interval, keep, num, missing);
216 if (keep + missing >= num)
217 continue;
218 /* redundant snapshot in this interval, pick snapshot with lowest score */
219 FOR_EACH_SNAPSHOT(s, i, sl) {
220 int64_t this_score;
221
222 //DSS_DEBUG_LOG("checking %s\n", s->name);
223 if (s->interval > interval) {
224 prev = s;
225 continue;
226 }
227 if (s->interval < interval)
228 break;
229 if (!victim) {
230 victim = s;
231 prev = s;
232 continue;
233 }
234 assert(prev);
235 /* check if s is a better victim */
236 this_score = s->creation_time - prev->creation_time;
237 assert(this_score >= 0);
238 //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
239 if (this_score < score) {
240 score = this_score;
241 victim = s;
242 }
243 prev = s;
244 }
245 assert(victim);
246 if (conf.dry_run_given) {
247 dss_msg("%s would be removed (interval = %i)\n",
248 victim->name, victim->interval);
249 continue;
250 }
251 ret = remove_snapshot(victim);
252 return ret < 0? ret : 1;
253 }
254 return 0;
255 }
256
257 int remove_outdated_snapshot(struct snapshot_list *sl)
258 {
259 int i, ret;
260 struct snapshot *s;
261
262 DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
263 conf.num_intervals_arg);
264 FOR_EACH_SNAPSHOT(s, i, sl) {
265 if (s->interval <= conf.num_intervals_arg)
266 continue;
267 if (conf.dry_run_given) {
268 dss_msg("%s would be removed (interval = %i)\n",
269 s->name, s->interval);
270 continue;
271 }
272 ret = remove_snapshot(s);
273 if (ret < 0)
274 return ret;
275 return 1;
276 }
277 return 0;
278 }
279
280 int remove_oldest_snapshot(struct snapshot_list *sl)
281 {
282 struct snapshot *s = get_oldest_snapshot(sl);
283
284 if (!s) /* no snapshot found */
285 return 0;
286 DSS_INFO_LOG("oldest snapshot: %s\n", s->name);
287 if (s->creation_time == current_snapshot_creation_time)
288 return 0; /* do not remove the snapshot currently being created */
289 return remove_snapshot(s);
290 }
291
292 int rename_incomplete_snapshot(int64_t start)
293 {
294 char *old_name;
295 int ret;
296
297 free(path_to_last_complete_snapshot);
298 ret = complete_name(start, get_current_time(),
299 &path_to_last_complete_snapshot);
300 if (ret < 0)
301 return ret;
302 old_name = incomplete_name(start);
303 ret = dss_rename(old_name, path_to_last_complete_snapshot);
304 if (ret >= 0)
305 DSS_NOTICE_LOG("%s -> %s\n", old_name,
306 path_to_last_complete_snapshot);
307 free(old_name);
308 return ret;
309 }
310
311 int try_to_free_disk_space(int low_disk_space)
312 {
313 int ret;
314 struct snapshot_list sl;
315
316 dss_get_snapshot_list(&sl);
317 ret = remove_outdated_snapshot(&sl);
318 if (ret) /* error, or we are removing something */
319 goto out;
320 /* no outdated snapshot */
321 ret = remove_redundant_snapshot(&sl);
322 if (ret)
323 goto out;
324 ret = 0;
325 if (!low_disk_space)
326 goto out;
327 DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
328 ret = remove_oldest_snapshot(&sl);
329 if (ret)
330 goto out;
331 DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
332 ret= -ENOSPC;
333 out:
334 free_snapshot_list(&sl);
335 return ret;
336 }
337
338 int pre_create_hook(void)
339 {
340 int ret, fds[3] = {0, 0, 0};
341
342 if (!conf.pre_create_hook_given) {
343 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
344 return 0;
345 }
346 DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
347 ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
348 conf.pre_create_hook_arg, fds);
349 if (ret < 0)
350 return ret;
351 snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
352 return ret;
353 }
354
355 int post_create_hook(void)
356 {
357 int ret, fds[3] = {0, 0, 0};
358 char *cmd;
359
360 if (!conf.post_create_hook_given) {
361 snapshot_creation_status = SCS_READY;
362 compute_next_snapshot_time();
363 return 0;
364 }
365 cmd = make_message("%s %s", conf.post_create_hook_arg,
366 path_to_last_complete_snapshot);
367 DSS_NOTICE_LOG("executing %s\n", cmd);
368 ret = dss_exec_cmdline_pid(&post_create_hook_pid, cmd, fds);
369 free(cmd);
370 if (ret < 0)
371 return ret;
372 snapshot_creation_status = SCS_POST_HOOK_RUNNING;
373 return ret;
374 }
375
376 void kill_process(pid_t pid)
377 {
378 if (!pid)
379 return;
380 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
381 kill(pid, SIGTERM);
382 }
383
384 void stop_rsync_process(void)
385 {
386 if (!rsync_pid || rsync_stopped)
387 return;
388 kill(SIGSTOP, rsync_pid);
389 rsync_stopped = 1;
390 }
391
392 void restart_rsync_process(void)
393 {
394 if (!rsync_pid || !rsync_stopped)
395 return;
396 kill (SIGCONT, rsync_pid);
397 rsync_stopped = 0;
398 }
399
400
401 /**
402 * Print a log message about the exit status of a child.
403 */
404 void log_termination_msg(pid_t pid, int status)
405 {
406 if (WIFEXITED(status))
407 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
408 WEXITSTATUS(status));
409 else if (WIFSIGNALED(status))
410 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
411 WTERMSIG(status));
412 else
413 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
414 }
415
416 int wait_for_process(pid_t pid, int *status)
417 {
418 int ret;
419
420 DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
421 for (;;) {
422 fd_set rfds;
423
424 FD_ZERO(&rfds);
425 FD_SET(signal_pipe, &rfds);
426 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
427 if (ret < 0)
428 break;
429 ret = next_signal();
430 if (!ret)
431 continue;
432 if (ret == SIGCHLD) {
433 ret = waitpid(pid, status, 0);
434 if (ret >= 0)
435 break;
436 if (errno != EINTR) { /* error */
437 ret = -ERRNO_TO_DSS_ERROR(errno);
438 break;
439 }
440 }
441 /* SIGINT or SIGTERM */
442 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
443 kill(pid, SIGTERM);
444 }
445 if (ret < 0)
446 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
447 else
448 log_termination_msg(pid, *status);
449 return ret;
450 }
451 int handle_rm_exit(int status)
452 {
453 rm_pid = 0;
454 if (!WIFEXITED(status))
455 return -E_INVOLUNTARY_EXIT;
456 if (WEXITSTATUS(status))
457 return -E_BAD_EXIT_CODE;
458 return 1;
459 }
460
461 int wait_for_rm_process(void)
462 {
463 int status, ret = wait_for_process(rm_pid, &status);
464
465 if (ret < 0)
466 return ret;
467 return handle_rm_exit(status);
468 }
469
470 int handle_rsync_exit(int status)
471 {
472 int es, ret;
473
474 if (!WIFEXITED(status)) {
475 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)rsync_pid);
476 ret = -E_INVOLUNTARY_EXIT;
477 snapshot_creation_status = SCS_READY;
478 compute_next_snapshot_time();
479 goto out;
480 }
481 es = WEXITSTATUS(status);
482 if (es != 0 && es != 23 && es != 24) {
483 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_pid, es);
484 ret = -E_BAD_EXIT_CODE;
485 snapshot_creation_status = SCS_READY;
486 compute_next_snapshot_time();
487 goto out;
488 }
489 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
490 if (ret < 0)
491 goto out;
492 snapshot_creation_status = SCS_RSYNC_SUCCESS;
493 out:
494 rsync_pid = 0;
495 rsync_stopped = 0;
496 return ret;
497 }
498
499 int handle_pre_create_hook_exit(int status)
500 {
501 int es, ret;
502
503 if (!WIFEXITED(status)) {
504 snapshot_creation_status = SCS_READY;
505 compute_next_snapshot_time();
506 ret = -E_INVOLUNTARY_EXIT;
507 goto out;
508 }
509 es = WEXITSTATUS(status);
510 if (es) {
511 snapshot_creation_status = SCS_READY;
512 compute_next_snapshot_time();
513 ret = -E_BAD_EXIT_CODE;
514 goto out;
515 }
516 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
517 ret = 1;
518 out:
519 pre_create_hook_pid = 0;
520 return ret;
521 }
522
523 int handle_sigchld(void)
524 {
525 pid_t pid;
526 int status, ret = reap_child(&pid, &status);
527
528 if (ret <= 0)
529 return ret;
530 if (pid == rsync_pid)
531 return handle_rsync_exit(status);
532 if (pid == rm_pid)
533 return handle_rm_exit(status);
534 if (pid == pre_create_hook_pid)
535 return handle_pre_create_hook_exit(status);
536 if (pid == post_create_hook_pid) {
537 snapshot_creation_status = SCS_READY;
538 compute_next_snapshot_time();
539 return 1;
540 }
541 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
542 exit(EXIT_FAILURE);
543 }
544
545
546 int check_config(void)
547 {
548 if (conf.unit_interval_arg <= 0) {
549 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
550 return -E_INVALID_NUMBER;
551 }
552 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
553 if (conf.num_intervals_arg <= 0) {
554 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
555 return -E_INVALID_NUMBER;
556 }
557 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
558 return 1;
559 }
560
561 /* exits on errors */
562 void parse_config_file(int override)
563 {
564 int ret;
565 char *config_file;
566 struct stat statbuf;
567 char *old_logfile_arg = NULL;
568 int old_daemon_given = 0;
569
570 if (conf.config_file_given)
571 config_file = dss_strdup(conf.config_file_arg);
572 else {
573 char *home = get_homedir();
574 config_file = make_message("%s/.dssrc", home);
575 free(home);
576 }
577 if (override) { /* SIGHUP */
578 if (conf.logfile_given)
579 old_logfile_arg = dss_strdup(conf.logfile_arg);
580 old_daemon_given = conf.daemon_given;
581 }
582
583 ret = stat(config_file, &statbuf);
584 if (ret && conf.config_file_given) {
585 ret = -ERRNO_TO_DSS_ERROR(errno);
586 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
587 goto out;
588 }
589 if (!ret) {
590 struct cmdline_parser_params params = {
591 .override = override,
592 .initialize = 0,
593 .check_required = 1,
594 .check_ambiguity = 0
595 };
596 cmdline_parser_config_file(config_file, &conf, &params);
597 }
598 ret = check_config();
599 if (ret < 0)
600 goto out;
601 if (override) {
602 /* don't change daemon mode on SIGHUP */
603 conf.daemon_given = old_daemon_given;
604 close_log(logfile);
605 logfile = NULL;
606 if (conf.logfile_given)
607 free(old_logfile_arg);
608 else if (conf.daemon_given) { /* re-use old logfile */
609 conf.logfile_arg = old_logfile_arg;
610 conf.logfile_given = 1;
611 }
612 }
613 if (conf.logfile_given) {
614 logfile = open_log(conf.logfile_arg);
615 log_welcome(conf.loglevel_arg);
616 }
617 DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
618 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
619 ret = dss_chdir(conf.dest_dir_arg);
620 out:
621 free(config_file);
622 if (ret >= 0)
623 return;
624 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
625 exit(EXIT_FAILURE);
626 }
627
628 void handle_sighup(void)
629 {
630 DSS_NOTICE_LOG("SIGHUP\n");
631 parse_config_file(1);
632 }
633
634 void handle_signal(void)
635 {
636 int sig, ret = next_signal();
637
638 if (ret <= 0)
639 goto out;
640 sig = ret;
641 switch (sig) {
642 case SIGINT:
643 case SIGTERM:
644 restart_rsync_process();
645 kill_process(rsync_pid);
646 kill_process(rm_pid);
647 exit(EXIT_FAILURE);
648 case SIGHUP:
649 handle_sighup();
650 ret = 1;
651 break;
652 case SIGCHLD:
653 ret = handle_sigchld();
654 break;
655 }
656 out:
657 if (ret < 0)
658 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
659 }
660
661 void create_rsync_argv(char ***argv, int64_t *num)
662 {
663 char *logname, *newest;
664 int i = 0, j;
665 struct snapshot_list sl;
666
667 dss_get_snapshot_list(&sl);
668 newest = name_of_newest_complete_snapshot(&sl);
669 free_snapshot_list(&sl);
670
671 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
672 (*argv)[i++] = dss_strdup("rsync");
673 (*argv)[i++] = dss_strdup("-aq");
674 (*argv)[i++] = dss_strdup("--delete");
675 for (j = 0; j < conf.rsync_option_given; j++)
676 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
677 if (newest) {
678 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
679 (*argv)[i++] = make_message("--link-dest=../%s", newest);
680 free(newest);
681 } else
682 DSS_INFO_LOG("no previous snapshot found\n");
683 if (conf.exclude_patterns_given) {
684 (*argv)[i++] = dss_strdup("--exclude-from");
685 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
686
687 }
688 logname = dss_logname();
689 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
690 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
691 else
692 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
693 conf.remote_user_arg : logname,
694 conf.remote_host_arg, conf.source_dir_arg);
695 free(logname);
696 *num = get_current_time();
697 (*argv)[i++] = incomplete_name(*num);
698 (*argv)[i++] = NULL;
699 for (j = 0; j < i; j++)
700 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
701 }
702
703 void free_rsync_argv(char **argv)
704 {
705 int i;
706 for (i = 0; argv[i]; i++)
707 free(argv[i]);
708 free(argv);
709 }
710
711 int create_snapshot(char **argv)
712 {
713 int ret, fds[3] = {0, 0, 0};
714 char *name;
715
716 name = incomplete_name(current_snapshot_creation_time);
717 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
718 free(name);
719 ret = dss_exec(&rsync_pid, argv[0], argv, fds);
720 if (ret < 0)
721 return ret;
722 snapshot_creation_status = SCS_RSYNC_RUNNING;
723 return ret;
724 }
725
726 int select_loop(void)
727 {
728 int ret;
729 struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
730
731 for (;;) {
732 fd_set rfds;
733 int low_disk_space;
734 char **rsync_argv;
735 struct timeval now, *tvp = &tv;
736
737 if (rsync_pid)
738 tv.tv_sec = 60; /* check every 60 seconds for free disk space */
739 else if (rm_pid)
740 tvp = NULL; /* sleep until rm process dies */
741 FD_ZERO(&rfds);
742 FD_SET(signal_pipe, &rfds);
743 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
744 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
745 if (ret < 0)
746 return ret;
747 if (FD_ISSET(signal_pipe, &rfds))
748 handle_signal();
749 if (rm_pid)
750 continue;
751 ret = disk_space_low();
752 if (ret < 0)
753 break;
754 low_disk_space = ret;
755 ret = try_to_free_disk_space(low_disk_space);
756 if (ret < 0)
757 break;
758 if (rm_pid) {
759 stop_rsync_process();
760 continue;
761 }
762 restart_rsync_process();
763 gettimeofday(&now, NULL);
764 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
765 continue;
766 switch (snapshot_creation_status) {
767 case SCS_READY:
768 ret = pre_create_hook();
769 if (ret < 0)
770 goto out;
771 continue;
772 case SCS_PRE_HOOK_RUNNING:
773 continue;
774 case SCS_PRE_HOOK_SUCCESS:
775 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
776 ret = create_snapshot(rsync_argv);
777 free_rsync_argv(rsync_argv);
778 if (ret < 0)
779 goto out;
780 continue;
781 case SCS_RSYNC_RUNNING:
782 continue;
783 case SCS_RSYNC_SUCCESS:
784 ret = post_create_hook();
785 if (ret < 0)
786 goto out;
787 continue;
788 case SCS_POST_HOOK_RUNNING:
789 continue;
790 }
791 }
792 out:
793 return ret;
794 }
795
796 int com_run(void)
797 {
798 int ret;
799
800 if (conf.dry_run_given) {
801 DSS_ERROR_LOG("dry_run not supported by this command\n");
802 return -E_SYNTAX;
803 }
804 ret = install_sighandler(SIGHUP);
805 if (ret < 0)
806 return ret;
807 compute_next_snapshot_time();
808 return select_loop();
809 }
810
811 int com_prune(void)
812 {
813 int ret;
814 struct snapshot_list sl;
815 struct disk_space ds;
816
817 ret = get_disk_space(".", &ds);
818 if (ret < 0)
819 return ret;
820 log_disk_space(&ds);
821 for (;;) {
822 dss_get_snapshot_list(&sl);
823 ret = remove_outdated_snapshot(&sl);
824 free_snapshot_list(&sl);
825 if (ret < 0)
826 return ret;
827 if (!ret)
828 break;
829 ret = wait_for_rm_process();
830 if (ret < 0)
831 goto out;
832 }
833 for (;;) {
834 dss_get_snapshot_list(&sl);
835 ret = remove_redundant_snapshot(&sl);
836 free_snapshot_list(&sl);
837 if (ret < 0)
838 return ret;
839 if (!ret)
840 break;
841 ret = wait_for_rm_process();
842 if (ret < 0)
843 goto out;
844 }
845 return 1;
846 out:
847 return ret;
848 }
849
850 int com_create(void)
851 {
852 int ret, status;
853 char **rsync_argv;
854
855 if (conf.dry_run_given) {
856 int i;
857 char *msg = NULL;
858 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
859 for (i = 0; rsync_argv[i]; i++) {
860 char *tmp = msg;
861 msg = make_message("%s%s%s", tmp? tmp : "",
862 tmp? " " : "", rsync_argv[i]);
863 free(tmp);
864 }
865 free_rsync_argv(rsync_argv);
866 dss_msg("%s\n", msg);
867 free(msg);
868 return 1;
869 }
870 ret = pre_create_hook();
871 if (ret < 0)
872 return ret;
873 if (pre_create_hook_pid) {
874 ret = wait_for_process(pre_create_hook_pid, &status);
875 if (ret < 0)
876 return ret;
877 ret = handle_pre_create_hook_exit(status);
878 if (ret < 0)
879 return ret;
880 }
881 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
882 ret = create_snapshot(rsync_argv);
883 if (ret < 0)
884 goto out;
885 ret = wait_for_process(rsync_pid, &status);
886 if (ret < 0)
887 goto out;
888 ret = handle_rsync_exit(status);
889 if (ret < 0)
890 goto out;
891 post_create_hook();
892 if (post_create_hook_pid)
893 ret = wait_for_process(post_create_hook_pid, &status);
894 out:
895 free_rsync_argv(rsync_argv);
896 return ret;
897 }
898
899 int com_ls(void)
900 {
901 int i;
902 struct snapshot_list sl;
903 struct snapshot *s;
904
905 dss_get_snapshot_list(&sl);
906 FOR_EACH_SNAPSHOT(s, i, &sl)
907 dss_msg("%u\t%s\n", s->interval, s->name);
908 free_snapshot_list(&sl);
909 return 1;
910 }
911
912 static void setup_signal_handling(void)
913 {
914 int ret;
915
916 DSS_INFO_LOG("setting up signal handlers\n");
917 signal_pipe = signal_init(); /* always successful */
918 ret = install_sighandler(SIGINT);
919 if (ret < 0)
920 goto err;
921 ret = install_sighandler(SIGTERM);
922 if (ret < 0)
923 goto err;
924 ret = install_sighandler(SIGCHLD);
925 if (ret < 0)
926 goto err;
927 return;
928 err:
929 DSS_EMERG_LOG("could not install signal handlers\n");
930 exit(EXIT_FAILURE);
931 }
932
933
934 int main(int argc, char **argv)
935 {
936 int ret;
937 struct cmdline_parser_params params = {
938 .override = 0,
939 .initialize = 1,
940 .check_required = 0,
941 .check_ambiguity = 0
942 };
943
944 cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
945 parse_config_file(0);
946
947 if (conf.daemon_given)
948 daemon_init();
949 setup_signal_handling();
950 ret = call_command_handler();
951 if (ret < 0)
952 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
953 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
954 }