dss.c: Make functions and global variables static.
[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 static struct timeval next_snapshot_time;
45 /** The pid of the pre-create hook. */
46 static pid_t pre_create_hook_pid;
47 /** The pid of the post-create hook. */
48 static pid_t post_create_hook_pid;
49 /* Creation time of the snapshot currently being created. */
50 static 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) static int com_ ##x(void);
67 COMMANDS
68 #undef COMMAND
69 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
70 static 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 static __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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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
452 static int handle_rm_exit(int status)
453 {
454 rm_pid = 0;
455 if (!WIFEXITED(status))
456 return -E_INVOLUNTARY_EXIT;
457 if (WEXITSTATUS(status))
458 return -E_BAD_EXIT_CODE;
459 return 1;
460 }
461
462 static int wait_for_rm_process(void)
463 {
464 int status, ret = wait_for_process(rm_pid, &status);
465
466 if (ret < 0)
467 return ret;
468 return handle_rm_exit(status);
469 }
470
471 static int handle_rsync_exit(int status)
472 {
473 int es, ret;
474
475 if (!WIFEXITED(status)) {
476 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)rsync_pid);
477 ret = -E_INVOLUNTARY_EXIT;
478 snapshot_creation_status = SCS_READY;
479 compute_next_snapshot_time();
480 goto out;
481 }
482 es = WEXITSTATUS(status);
483 if (es != 0 && es != 23 && es != 24) {
484 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_pid, es);
485 ret = -E_BAD_EXIT_CODE;
486 snapshot_creation_status = SCS_READY;
487 compute_next_snapshot_time();
488 goto out;
489 }
490 ret = rename_incomplete_snapshot(current_snapshot_creation_time);
491 if (ret < 0)
492 goto out;
493 snapshot_creation_status = SCS_RSYNC_SUCCESS;
494 out:
495 rsync_pid = 0;
496 rsync_stopped = 0;
497 return ret;
498 }
499
500 static int handle_pre_create_hook_exit(int status)
501 {
502 int es, ret;
503
504 if (!WIFEXITED(status)) {
505 snapshot_creation_status = SCS_READY;
506 compute_next_snapshot_time();
507 ret = -E_INVOLUNTARY_EXIT;
508 goto out;
509 }
510 es = WEXITSTATUS(status);
511 if (es) {
512 snapshot_creation_status = SCS_READY;
513 compute_next_snapshot_time();
514 ret = -E_BAD_EXIT_CODE;
515 goto out;
516 }
517 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
518 ret = 1;
519 out:
520 pre_create_hook_pid = 0;
521 return ret;
522 }
523
524 static int handle_sigchld(void)
525 {
526 pid_t pid;
527 int status, ret = reap_child(&pid, &status);
528
529 if (ret <= 0)
530 return ret;
531 if (pid == rsync_pid)
532 return handle_rsync_exit(status);
533 if (pid == rm_pid)
534 return handle_rm_exit(status);
535 if (pid == pre_create_hook_pid)
536 return handle_pre_create_hook_exit(status);
537 if (pid == post_create_hook_pid) {
538 snapshot_creation_status = SCS_READY;
539 compute_next_snapshot_time();
540 return 1;
541 }
542 DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
543 exit(EXIT_FAILURE);
544 }
545
546
547 static int check_config(void)
548 {
549 if (conf.unit_interval_arg <= 0) {
550 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
551 return -E_INVALID_NUMBER;
552 }
553 DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
554 if (conf.num_intervals_arg <= 0) {
555 DSS_ERROR_LOG("bad number of intervals %i\n", conf.num_intervals_arg);
556 return -E_INVALID_NUMBER;
557 }
558 DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
559 return 1;
560 }
561
562 /* exits on errors */
563 static void parse_config_file(int override)
564 {
565 int ret;
566 char *config_file;
567 struct stat statbuf;
568 char *old_logfile_arg = NULL;
569 int old_daemon_given = 0;
570
571 if (conf.config_file_given)
572 config_file = dss_strdup(conf.config_file_arg);
573 else {
574 char *home = get_homedir();
575 config_file = make_message("%s/.dssrc", home);
576 free(home);
577 }
578 if (override) { /* SIGHUP */
579 if (conf.logfile_given)
580 old_logfile_arg = dss_strdup(conf.logfile_arg);
581 old_daemon_given = conf.daemon_given;
582 }
583
584 ret = stat(config_file, &statbuf);
585 if (ret && conf.config_file_given) {
586 ret = -ERRNO_TO_DSS_ERROR(errno);
587 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
588 goto out;
589 }
590 if (!ret) {
591 struct cmdline_parser_params params = {
592 .override = override,
593 .initialize = 0,
594 .check_required = 1,
595 .check_ambiguity = 0
596 };
597 cmdline_parser_config_file(config_file, &conf, &params);
598 }
599 ret = check_config();
600 if (ret < 0)
601 goto out;
602 if (override) {
603 /* don't change daemon mode on SIGHUP */
604 conf.daemon_given = old_daemon_given;
605 close_log(logfile);
606 logfile = NULL;
607 if (conf.logfile_given)
608 free(old_logfile_arg);
609 else if (conf.daemon_given) { /* re-use old logfile */
610 conf.logfile_arg = old_logfile_arg;
611 conf.logfile_given = 1;
612 }
613 }
614 if (conf.logfile_given) {
615 logfile = open_log(conf.logfile_arg);
616 log_welcome(conf.loglevel_arg);
617 }
618 DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
619 // cmdline_parser_dump(logfile? logfile : stdout, &conf);
620 ret = dss_chdir(conf.dest_dir_arg);
621 out:
622 free(config_file);
623 if (ret >= 0)
624 return;
625 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
626 exit(EXIT_FAILURE);
627 }
628
629 static void handle_sighup(void)
630 {
631 DSS_NOTICE_LOG("SIGHUP\n");
632 parse_config_file(1);
633 }
634
635 static void handle_signal(void)
636 {
637 int sig, ret = next_signal();
638
639 if (ret <= 0)
640 goto out;
641 sig = ret;
642 switch (sig) {
643 case SIGINT:
644 case SIGTERM:
645 restart_rsync_process();
646 kill_process(rsync_pid);
647 kill_process(rm_pid);
648 exit(EXIT_FAILURE);
649 case SIGHUP:
650 handle_sighup();
651 ret = 1;
652 break;
653 case SIGCHLD:
654 ret = handle_sigchld();
655 break;
656 }
657 out:
658 if (ret < 0)
659 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
660 }
661
662 static void create_rsync_argv(char ***argv, int64_t *num)
663 {
664 char *logname, *newest;
665 int i = 0, j;
666 struct snapshot_list sl;
667
668 dss_get_snapshot_list(&sl);
669 newest = name_of_newest_complete_snapshot(&sl);
670 free_snapshot_list(&sl);
671
672 *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
673 (*argv)[i++] = dss_strdup("rsync");
674 (*argv)[i++] = dss_strdup("-aq");
675 (*argv)[i++] = dss_strdup("--delete");
676 for (j = 0; j < conf.rsync_option_given; j++)
677 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
678 if (newest) {
679 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
680 (*argv)[i++] = make_message("--link-dest=../%s", newest);
681 free(newest);
682 } else
683 DSS_INFO_LOG("no previous snapshot found\n");
684 if (conf.exclude_patterns_given) {
685 (*argv)[i++] = dss_strdup("--exclude-from");
686 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
687
688 }
689 logname = dss_logname();
690 if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
691 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
692 else
693 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
694 conf.remote_user_arg : logname,
695 conf.remote_host_arg, conf.source_dir_arg);
696 free(logname);
697 *num = get_current_time();
698 (*argv)[i++] = incomplete_name(*num);
699 (*argv)[i++] = NULL;
700 for (j = 0; j < i; j++)
701 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
702 }
703
704 static void free_rsync_argv(char **argv)
705 {
706 int i;
707 for (i = 0; argv[i]; i++)
708 free(argv[i]);
709 free(argv);
710 }
711
712 static int create_snapshot(char **argv)
713 {
714 int ret, fds[3] = {0, 0, 0};
715 char *name;
716
717 name = incomplete_name(current_snapshot_creation_time);
718 DSS_NOTICE_LOG("creating new snapshot %s\n", name);
719 free(name);
720 ret = dss_exec(&rsync_pid, argv[0], argv, fds);
721 if (ret < 0)
722 return ret;
723 snapshot_creation_status = SCS_RSYNC_RUNNING;
724 return ret;
725 }
726
727 static int select_loop(void)
728 {
729 int ret;
730 struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
731
732 for (;;) {
733 fd_set rfds;
734 int low_disk_space;
735 char **rsync_argv;
736 struct timeval now, *tvp = &tv;
737
738 if (rsync_pid)
739 tv.tv_sec = 60; /* check every 60 seconds for free disk space */
740 else if (rm_pid)
741 tvp = NULL; /* sleep until rm process dies */
742 FD_ZERO(&rfds);
743 FD_SET(signal_pipe, &rfds);
744 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
745 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
746 if (ret < 0)
747 return ret;
748 if (FD_ISSET(signal_pipe, &rfds))
749 handle_signal();
750 if (rm_pid)
751 continue;
752 ret = disk_space_low();
753 if (ret < 0)
754 break;
755 low_disk_space = ret;
756 ret = try_to_free_disk_space(low_disk_space);
757 if (ret < 0)
758 break;
759 if (rm_pid) {
760 stop_rsync_process();
761 continue;
762 }
763 restart_rsync_process();
764 gettimeofday(&now, NULL);
765 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
766 continue;
767 switch (snapshot_creation_status) {
768 case SCS_READY:
769 ret = pre_create_hook();
770 if (ret < 0)
771 goto out;
772 continue;
773 case SCS_PRE_HOOK_RUNNING:
774 continue;
775 case SCS_PRE_HOOK_SUCCESS:
776 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
777 ret = create_snapshot(rsync_argv);
778 free_rsync_argv(rsync_argv);
779 if (ret < 0)
780 goto out;
781 continue;
782 case SCS_RSYNC_RUNNING:
783 continue;
784 case SCS_RSYNC_SUCCESS:
785 ret = post_create_hook();
786 if (ret < 0)
787 goto out;
788 continue;
789 case SCS_POST_HOOK_RUNNING:
790 continue;
791 }
792 }
793 out:
794 return ret;
795 }
796
797 static int com_run(void)
798 {
799 int ret;
800
801 if (conf.dry_run_given) {
802 DSS_ERROR_LOG("dry_run not supported by this command\n");
803 return -E_SYNTAX;
804 }
805 ret = install_sighandler(SIGHUP);
806 if (ret < 0)
807 return ret;
808 compute_next_snapshot_time();
809 return select_loop();
810 }
811
812 static int com_prune(void)
813 {
814 int ret;
815 struct snapshot_list sl;
816 struct disk_space ds;
817
818 ret = get_disk_space(".", &ds);
819 if (ret < 0)
820 return ret;
821 log_disk_space(&ds);
822 for (;;) {
823 dss_get_snapshot_list(&sl);
824 ret = remove_outdated_snapshot(&sl);
825 free_snapshot_list(&sl);
826 if (ret < 0)
827 return ret;
828 if (!ret)
829 break;
830 ret = wait_for_rm_process();
831 if (ret < 0)
832 goto out;
833 }
834 for (;;) {
835 dss_get_snapshot_list(&sl);
836 ret = remove_redundant_snapshot(&sl);
837 free_snapshot_list(&sl);
838 if (ret < 0)
839 return ret;
840 if (!ret)
841 break;
842 ret = wait_for_rm_process();
843 if (ret < 0)
844 goto out;
845 }
846 return 1;
847 out:
848 return ret;
849 }
850
851 static int com_create(void)
852 {
853 int ret, status;
854 char **rsync_argv;
855
856 if (conf.dry_run_given) {
857 int i;
858 char *msg = NULL;
859 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
860 for (i = 0; rsync_argv[i]; i++) {
861 char *tmp = msg;
862 msg = make_message("%s%s%s", tmp? tmp : "",
863 tmp? " " : "", rsync_argv[i]);
864 free(tmp);
865 }
866 free_rsync_argv(rsync_argv);
867 dss_msg("%s\n", msg);
868 free(msg);
869 return 1;
870 }
871 ret = pre_create_hook();
872 if (ret < 0)
873 return ret;
874 if (pre_create_hook_pid) {
875 ret = wait_for_process(pre_create_hook_pid, &status);
876 if (ret < 0)
877 return ret;
878 ret = handle_pre_create_hook_exit(status);
879 if (ret < 0)
880 return ret;
881 }
882 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
883 ret = create_snapshot(rsync_argv);
884 if (ret < 0)
885 goto out;
886 ret = wait_for_process(rsync_pid, &status);
887 if (ret < 0)
888 goto out;
889 ret = handle_rsync_exit(status);
890 if (ret < 0)
891 goto out;
892 post_create_hook();
893 if (post_create_hook_pid)
894 ret = wait_for_process(post_create_hook_pid, &status);
895 out:
896 free_rsync_argv(rsync_argv);
897 return ret;
898 }
899
900 static int com_ls(void)
901 {
902 int i;
903 struct snapshot_list sl;
904 struct snapshot *s;
905
906 dss_get_snapshot_list(&sl);
907 FOR_EACH_SNAPSHOT(s, i, &sl)
908 dss_msg("%u\t%s\n", s->interval, s->name);
909 free_snapshot_list(&sl);
910 return 1;
911 }
912
913 static void setup_signal_handling(void)
914 {
915 int ret;
916
917 DSS_INFO_LOG("setting up signal handlers\n");
918 signal_pipe = signal_init(); /* always successful */
919 ret = install_sighandler(SIGINT);
920 if (ret < 0)
921 goto err;
922 ret = install_sighandler(SIGTERM);
923 if (ret < 0)
924 goto err;
925 ret = install_sighandler(SIGCHLD);
926 if (ret < 0)
927 goto err;
928 return;
929 err:
930 DSS_EMERG_LOG("could not install signal handlers\n");
931 exit(EXIT_FAILURE);
932 }
933
934
935 int main(int argc, char **argv)
936 {
937 int ret;
938 struct cmdline_parser_params params = {
939 .override = 0,
940 .initialize = 1,
941 .check_required = 0,
942 .check_ambiguity = 0
943 };
944
945 cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
946 parse_config_file(0);
947
948 if (conf.daemon_given)
949 daemon_init();
950 setup_signal_handling();
951 ret = call_command_handler();
952 if (ret < 0)
953 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
954 exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
955 }