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