]> git.tuebingen.mpg.de Git - dss.git/blob - dss.c
Implement SIGHUP handling.
[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
31
32 struct gengetopt_args_info conf;
33 char *dss_error_txt = NULL;
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 /* Creation time of the snapshot currently being created. */
46 int64_t current_snapshot_creation_time;
47
48
49 DEFINE_DSS_ERRLIST;
50
51
52 /* a litte cpp magic helps to DRY */
53 #define COMMANDS \
54         COMMAND(ls) \
55         COMMAND(create) \
56         COMMAND(prune) \
57         COMMAND(run)
58 #define COMMAND(x) int com_ ##x(void);
59 COMMANDS
60 #undef COMMAND
61 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
62 int call_command_handler(void)
63 {
64         COMMANDS
65         DSS_EMERG_LOG("BUG: did not find command handler\n");
66         exit(EXIT_FAILURE);
67 }
68 #undef COMMAND
69 #undef COMMANDS
70
71 /*
72  * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
73  * complete, being deleted: 1204565370-1204565371.being_deleted
74  * incomplete, not being deleted: 1204565370-incomplete
75  * incomplete, being deleted: 1204565370-incomplete.being_deleted
76  */
77 enum snapshot_status_flags {
78         /** The rsync process terminated successfully. */
79         SS_COMPLETE = 1,
80         /** The rm process is running to remove this snapshot. */
81         SS_BEING_DELETED = 2,
82 };
83
84 struct snapshot {
85         char *name;
86         int64_t creation_time;
87         int64_t completion_time;
88         enum snapshot_status_flags flags;
89         unsigned interval;
90 };
91
92 /*
93  * An edge snapshot is either the oldest one or the newest one.
94  *
95  * We need to find either of them occasionally: The create code
96  * needs to know the newest snapshot because that is the one
97  * used as the link destination dir. The pruning code needs to
98  * find the oldest one in case disk space becomes low.
99  */
100 struct edge_snapshot_data {
101         int64_t now;
102         struct snapshot snap;
103 };
104
105 __printf_2_3 void dss_log(int ll, const char* fmt,...)
106 {
107         va_list argp;
108         FILE *outfd;
109         struct tm *tm;
110         time_t t1;
111         char str[255] = "";
112
113         if (ll < conf.loglevel_arg)
114                 return;
115         outfd = logfile? logfile : stderr;
116         time(&t1);
117         tm = localtime(&t1);
118         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
119         fprintf(outfd, "%s ", str);
120         if (conf.loglevel_arg <= INFO)
121                 fprintf(outfd, "%i: ", ll);
122         va_start(argp, fmt);
123         vfprintf(outfd, fmt, argp);
124         va_end(argp);
125 }
126
127 /**
128  * Print a message either to stdout or to the log file.
129  */
130 __printf_1_2 void dss_msg(const char* fmt,...)
131 {
132         FILE *outfd = conf.daemon_given? logfile : stdout;
133         va_list argp;
134         va_start(argp, fmt);
135         vfprintf(outfd, fmt, argp);
136         va_end(argp);
137 }
138
139 /**
140  * Return the desired number of snapshots of an interval.
141  */
142 unsigned num_snapshots(int interval)
143 {
144         unsigned n;
145
146         assert(interval >= 0);
147
148         if (interval >= conf.num_intervals_arg)
149                 return 0;
150         n = conf.num_intervals_arg - interval - 1;
151         return 1 << n;
152 }
153
154 /* return: Whether dirname is a snapshot directory (0: no, 1: yes) */
155 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
156 {
157         int i, ret;
158         char *dash, *dot, *tmp;
159         int64_t num;
160
161         assert(dirname);
162         dash = strchr(dirname, '-');
163         if (!dash || !dash[1] || dash == dirname)
164                 return 0;
165         for (i = 0; dirname[i] != '-'; i++)
166                 if (!isdigit(dirname[i]))
167                         return 0;
168         tmp = dss_strdup(dirname);
169         tmp[i] = '\0';
170         ret = dss_atoi64(tmp, &num);
171         free(tmp);
172         if (ret < 0) {
173                 free(dss_error_txt);
174                 return 0;
175         }
176         assert(num >= 0);
177         if (num > now)
178                 return 0;
179         s->creation_time = num;
180         //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
181         s->interval = (long long) ((now - s->creation_time)
182                 / conf.unit_interval_arg / 24 / 3600);
183         if (!strcmp(dash + 1, "incomplete")) {
184                 s->completion_time = -1;
185                 s->flags = 0; /* neither complete, nor being deleted */
186                 goto success;
187         }
188         if (!strcmp(dash + 1, "incomplete.being_deleted")) {
189                 s->completion_time = -1;
190                 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
191                 goto success;
192         }
193         tmp = dash + 1;
194         dot = strchr(tmp, '.');
195         if (!dot || !dot[1] || dot == tmp)
196                 return 0;
197         for (i = 0; tmp[i] != '.'; i++)
198                 if (!isdigit(tmp[i]))
199                         return 0;
200         tmp = dss_strdup(dash + 1);
201         tmp[i] = '\0';
202         ret = dss_atoi64(tmp, &num);
203         free(tmp);
204         if (ret < 0) {
205                 free(dss_error_txt);
206                 return 0;
207         }
208         if (num > now)
209                 return 0;
210         s->completion_time = num;
211         s->flags = SS_COMPLETE;
212         if (!strcmp(dot + 1, "being_deleted"))
213                 s->flags |= SS_BEING_DELETED;
214 success:
215         s->name = dss_strdup(dirname);
216         return 1;
217 }
218
219 int64_t get_current_time(void)
220 {
221         time_t now;
222         time(&now);
223         DSS_DEBUG_LOG("now: %lli\n", (long long) now);
224         return (int64_t)now;
225 }
226
227 char *incomplete_name(int64_t start)
228 {
229         return make_message("%lli-incomplete", (long long)start);
230 }
231
232 char *being_deleted_name(struct snapshot *s)
233 {
234         if (s->flags & SS_COMPLETE)
235                 return make_message("%lli-%lli.being_deleted",
236                         (long long)s->creation_time,
237                         (long long)s->completion_time);
238         return make_message("%lli-incomplete.being_deleted",
239                 (long long)s->creation_time);
240 }
241
242 int complete_name(int64_t start, int64_t end, char **result)
243 {
244         struct tm start_tm, end_tm;
245         time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
246         time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
247         char start_str[200], end_str[200];
248
249         if (!localtime_r(start_seconds, &start_tm)) {
250                 make_err_msg("%lli", (long long)start);
251                 return -E_LOCALTIME;
252         }
253         if (!localtime_r(end_seconds, &end_tm)) {
254                 make_err_msg("%lli", (long long)end);
255                 return -E_LOCALTIME;
256         }
257         if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
258                 make_err_msg("%lli", (long long)start);
259                 return -E_STRFTIME;
260         }
261         if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
262                 make_err_msg("%lli", (long long)end);
263                 return -E_STRFTIME;
264         }
265         *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
266                 start_str, end_str);
267         return 1;
268 }
269
270 struct snapshot_list {
271         int64_t now;
272         unsigned num_snapshots;
273         unsigned array_size;
274         struct snapshot **snapshots;
275         /**
276          * Array of size num_intervals + 1
277          *
278          * It contains the number of snapshots in each interval. interval_count[num_intervals]
279          * is the number of snapshots which belong to any interval greater than num_intervals.
280          */
281         unsigned *interval_count;
282 };
283
284 #define FOR_EACH_SNAPSHOT(s, i, sl) \
285         for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
286
287
288
289 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
290
291 static int compare_snapshots(const void *a, const void *b)
292 {
293         struct snapshot *s1 = *(struct snapshot **)a;
294         struct snapshot *s2 = *(struct snapshot **)b;
295         return NUM_COMPARE(s2->creation_time, s1->creation_time);
296 }
297
298 /** Compute the minimum of \a a and \a b. */
299 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
300
301 int add_snapshot(const char *dirname, void *private)
302 {
303         struct snapshot_list *sl = private;
304         struct snapshot s;
305         int ret = is_snapshot(dirname, sl->now, &s);
306
307         if (!ret)
308                 return 1;
309         if (sl->num_snapshots >= sl->array_size) {
310                 sl->array_size = 2 * sl->array_size + 1;
311                 sl->snapshots = dss_realloc(sl->snapshots,
312                         sl->array_size * sizeof(struct snapshot *));
313         }
314         sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
315         *(sl->snapshots[sl->num_snapshots]) = s;
316         sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
317         sl->num_snapshots++;
318         return 1;
319 }
320
321 void get_snapshot_list(struct snapshot_list *sl)
322 {
323         sl->now = get_current_time();
324         sl->num_snapshots = 0;
325         sl->array_size = 0;
326         sl->snapshots = NULL;
327         sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
328         for_each_subdir(add_snapshot, sl);
329         qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
330                 compare_snapshots);
331 }
332
333 void free_snapshot_list(struct snapshot_list *sl)
334 {
335         int i;
336         struct snapshot *s;
337
338         FOR_EACH_SNAPSHOT(s, i, sl) {
339                 free(s->name);
340                 free(s);
341         }
342         free(sl->interval_count);
343         sl->interval_count = NULL;
344         free(sl->snapshots);
345         sl->snapshots = NULL;
346         sl->num_snapshots = 0;
347 }
348
349 void stop_rsync_process(void)
350 {
351         if (!rsync_pid || rsync_stopped)
352                 return;
353         kill(SIGSTOP, rsync_pid);
354         rsync_stopped = 1;
355 }
356
357 void restart_rsync_process(void)
358 {
359         if (!rsync_pid || !rsync_stopped)
360                 return;
361         kill (SIGCONT, rsync_pid);
362         rsync_stopped = 0;
363 }
364
365 /**
366  * Print a log message about the exit status of a child.
367  */
368 void log_termination_msg(pid_t pid, int status)
369 {
370         if (WIFEXITED(status))
371                 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
372                         WEXITSTATUS(status));
373         else if (WIFSIGNALED(status))
374                 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
375                         WTERMSIG(status));
376         else
377                 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
378 }
379
380 int wait_for_process(pid_t pid, int *status)
381 {
382         int ret;
383
384         DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
385         for (;;) {
386                 pause();
387                 ret = next_signal();
388                 if (ret < 0)
389                         break;
390                 if (!ret)
391                         continue;
392                 if (ret == SIGCHLD) {
393                         ret = waitpid(pid, status, 0);
394                         if (ret >= 0)
395                                 break;
396                         if (errno != EINTR) { /* error */
397                                 ret = -ERRNO_TO_DSS_ERROR(errno);
398                                 break;
399                         }
400                 }
401                 /* SIGINT or SIGTERM */
402                 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
403                 kill(pid, SIGTERM);
404         }
405         if (ret < 0)
406                 make_err_msg("failed to wait for process %d", (int)pid);
407         else
408                 log_termination_msg(pid, *status);
409         return ret;
410 }
411
412 int remove_snapshot(struct snapshot *s)
413 {
414         int fds[3] = {0, 0, 0};
415         assert(!rm_pid);
416         char *new_name = being_deleted_name(s);
417         int ret = dss_rename(s->name, new_name);
418         char *argv[] = {"rm", "-rf", new_name, NULL};
419
420         if (ret < 0)
421                 goto out;
422         DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
423         stop_rsync_process();
424         ret = dss_exec(&rm_pid, argv[0], argv, fds);
425 out:
426         free(new_name);
427         return ret;
428 }
429
430 /*
431  * return: 0: no redundant snapshots, 1: rm process started, negative: error
432  */
433 int remove_redundant_snapshot(struct snapshot_list *sl)
434 {
435         int ret, i, interval;
436         struct snapshot *s;
437         unsigned missing = 0;
438
439         DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
440         for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
441                 unsigned keep = num_snapshots(interval);
442                 unsigned num = sl->interval_count[interval];
443                 struct snapshot *victim = NULL, *prev = NULL;
444                 int64_t score = LONG_MAX;
445
446                 if (keep >= num)
447                         missing += keep - num;
448                 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
449                         interval, keep, num, missing);
450                 if (keep + missing >= num)
451                         continue;
452                 /* redundant snapshot in this interval, pick snapshot with lowest score */
453                 FOR_EACH_SNAPSHOT(s, i, sl) {
454                         int64_t this_score;
455
456                         //DSS_DEBUG_LOG("checking %s\n", s->name);
457                         if (s->interval > interval) {
458                                 prev = s;
459                                 continue;
460                         }
461                         if (s->interval < interval)
462                                 break;
463                         if (!victim) {
464                                 victim = s;
465                                 prev = s;
466                                 continue;
467                         }
468                         assert(prev);
469                         /* check if s is a better victim */
470                         this_score = s->creation_time - prev->creation_time;
471                         assert(this_score >= 0);
472                         //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
473                         if (this_score < score) {
474                                 score = this_score;
475                                 victim = s;
476                         }
477                         prev = s;
478                 }
479                 assert(victim);
480                 if (conf.dry_run_given) {
481                         dss_msg("%s would be removed (interval = %i)\n",
482                                 victim->name, victim->interval);
483                         continue;
484                 }
485                 ret = remove_snapshot(victim);
486                 return ret < 0? ret : 1;
487         }
488         return 0;
489 }
490
491 int remove_outdated_snapshot(struct snapshot_list *sl)
492 {
493         int i, ret;
494         struct snapshot *s;
495
496         DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
497                 conf.num_intervals_arg);
498         FOR_EACH_SNAPSHOT(s, i, sl) {
499                 if (s->interval <= conf.num_intervals_arg)
500                         continue;
501                 if (conf.dry_run_given) {
502                         dss_msg("%s would be removed (interval = %i)\n",
503                                 s->name, s->interval);
504                         continue;
505                 }
506                 ret = remove_snapshot(s);
507                 if (ret < 0)
508                         return ret;
509                 return 1;
510         }
511         return 0;
512 }
513
514 int handle_rm_exit(int status)
515 {
516         int es, ret;
517
518         if (!WIFEXITED(status)) {
519                 make_err_msg("rm process %d died involuntary", (int)rm_pid);
520                 ret = -E_INVOLUNTARY_EXIT;
521                 goto out;
522         }
523         es = WEXITSTATUS(status);
524         if (es) {
525                 make_err_msg("rm process %d returned %d", (int)rm_pid, es);
526                 ret = -E_BAD_EXIT_CODE;
527                 goto out;
528         }
529         ret = 1;
530         rm_pid = 0;
531 out:
532         return ret;
533 }
534
535 int wait_for_rm_process(void)
536 {
537         int status, ret = wait_for_process(rm_pid, &status);
538
539         if (ret < 0)
540                 return ret;
541         return handle_rm_exit(status);
542 }
543
544 void kill_process(pid_t pid)
545 {
546         if (!pid)
547                 return;
548         DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
549         kill(pid, SIGTERM);
550 }
551
552 int check_config(void)
553 {
554         if (conf.unit_interval_arg <= 0) {
555                 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
556                 return -E_INVALID_NUMBER;
557         }
558         DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
559         if (conf.num_intervals_arg <= 0) {
560                 make_err_msg("bad number of intervals  %i", conf.num_intervals_arg);
561                 return -E_INVALID_NUMBER;
562         }
563         DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
564         return 1;
565 }
566
567 /* exits on errors */
568 void parse_config_file(int override)
569 {
570         int ret;
571         char *config_file;
572         struct stat statbuf;
573         char *old_logfile_arg = NULL;
574         int old_daemon_given = 0;
575
576         if (conf.config_file_given)
577                 config_file = dss_strdup(conf.config_file_arg);
578         else {
579                 char *home = get_homedir();
580                 config_file = make_message("%s/.dssrc", home);
581                 free(home);
582         }
583         if (override) { /* SIGHUP */
584                 if (conf.logfile_given)
585                         old_logfile_arg = dss_strdup(conf.logfile_arg);
586                 old_daemon_given = conf.daemon_given;
587         }
588
589         ret = stat(config_file, &statbuf);
590         if (ret && conf.config_file_given) {
591                 ret = -ERRNO_TO_DSS_ERROR(errno);
592                 make_err_msg("failed to stat config file %s", config_file);
593                 goto out;
594         }
595         if (!ret) {
596                 struct cmdline_parser_params params = {
597                         .override = override,
598                         .initialize = 0,
599                         .check_required = 0,
600                         .check_ambiguity = 0
601                 };
602                 cmdline_parser_config_file(config_file, &conf, &params);
603         }
604         if (!conf.source_dir_given || !conf.dest_dir_given) {
605                 ret = -E_SYNTAX;
606                 make_err_msg("you need to specify both source_dir and dest_dir");
607                 goto out;
608         }
609         ret = check_config();
610         if (ret < 0)
611                 goto out;
612         if (override) {
613                 /* don't change daemon mode on SIGHUP */
614                 conf.daemon_given = old_daemon_given;
615                 close_log(logfile);
616                 logfile = NULL;
617                 if (conf.logfile_given)
618                         free(old_logfile_arg);
619                 else if (conf.daemon_given) { /* re-use old logfile */
620                         conf.logfile_arg = old_logfile_arg;
621                         conf.logfile_given = 1;
622                 }
623         }
624         if (conf.logfile_given) {
625                 logfile = open_log(conf.logfile_arg);
626                 log_welcome(conf.loglevel_arg);
627         }
628         ret = dss_chdir(conf.dest_dir_arg);
629 out:
630         free(config_file);
631         if (ret >= 0)
632                 return;
633         log_err_msg(EMERG, -ret);
634         exit(EXIT_FAILURE);
635 }
636
637 void handle_sighup(void)
638 {
639         DSS_NOTICE_LOG("SIGHUP\n");
640         parse_config_file(1);
641 }
642
643 int rename_incomplete_snapshot(int64_t start)
644 {
645         char *old_name, *new_name;
646         int ret;
647
648         ret = complete_name(start, get_current_time(), &new_name);
649         if (ret < 0)
650                 return ret;
651         old_name = incomplete_name(start);
652         ret = dss_rename(old_name, new_name);
653         if (ret >= 0)
654                 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
655         free(old_name);
656         free(new_name);
657         return ret;
658 }
659
660 int handle_rsync_exit(int status)
661 {
662         int es, ret;
663
664         if (!WIFEXITED(status)) {
665                 make_err_msg("rsync process %d died involuntary", (int)rsync_pid);
666                 ret = -E_INVOLUNTARY_EXIT;
667                 goto out;
668         }
669         es = WEXITSTATUS(status);
670         if (es != 0 && es != 23 && es != 24) {
671                 make_err_msg("rsync process %d returned %d", (int)rsync_pid, es);
672                 ret = -E_BAD_EXIT_CODE;
673                 goto out;
674         }
675         ret = rename_incomplete_snapshot(current_snapshot_creation_time);
676 out:
677         rsync_pid = 0;
678         current_snapshot_creation_time = 0;
679         rsync_stopped = 0;
680         return ret;
681 }
682
683 int get_newest_complete(const char *dirname, void *private)
684 {
685         struct edge_snapshot_data *esd = private;
686         struct snapshot s;
687         int ret = is_snapshot(dirname, esd->now, &s);
688
689         if (ret <= 0)
690                 return 1;
691         if (s.flags != SS_COMPLETE) /* incomplete or being deleted */
692                 return 1;
693         if (s.creation_time < esd->snap.creation_time)
694                 return 1;
695         free(esd->snap.name);
696         esd->snap = s;
697         return 1;
698 }
699
700 __malloc char *name_of_newest_complete_snapshot(void)
701 {
702         struct edge_snapshot_data esd = {
703                 .now = get_current_time(),
704                 .snap = {.creation_time = -1}
705         };
706         for_each_subdir(get_newest_complete, &esd);
707         return esd.snap.name;
708 }
709
710 void create_rsync_argv(char ***argv, int64_t *num)
711 {
712         char *logname, *newest = name_of_newest_complete_snapshot();
713         int i = 0, j;
714
715         *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
716         (*argv)[i++] = dss_strdup("rsync");
717         (*argv)[i++] = dss_strdup("-aq");
718         (*argv)[i++] = dss_strdup("--delete");
719         for (j = 0; j < conf.rsync_option_given; j++)
720                 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
721         if (newest) {
722                 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
723                 (*argv)[i++] = make_message("--link-dest=../%s", newest);
724                 free(newest);
725         } else
726                 DSS_INFO_LOG("no previous snapshot found\n");
727         if (conf.exclude_patterns_given) {
728                 (*argv)[i++] = dss_strdup("--exclude-from");
729                 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
730
731         }
732         logname = dss_logname();
733         if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
734                 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
735         else
736                 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
737                         conf.remote_user_arg : logname,
738                         conf.remote_host_arg, conf.source_dir_arg);
739         free(logname);
740         *num = get_current_time();
741         (*argv)[i++] = incomplete_name(*num);
742         (*argv)[i++] = NULL;
743         for (j = 0; j < i; j++)
744                 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
745 }
746
747 void free_rsync_argv(char **argv)
748 {
749         int i;
750         for (i = 0; argv[i]; i++)
751                 free(argv[i]);
752         free(argv);
753 }
754
755 int create_snapshot(char **argv)
756 {
757         int fds[3] = {0, 0, 0};
758         char *name = incomplete_name(current_snapshot_creation_time);
759
760         DSS_NOTICE_LOG("creating new snapshot %s\n", name);
761         free(name);
762         return dss_exec(&rsync_pid, argv[0], argv, fds);
763 }
764
765 void compute_next_snapshot_time(struct snapshot_list *sl)
766 {
767         struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
768                 tmp, diff;
769         int64_t x = 0;
770         unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
771         int i, ret;
772         struct snapshot *s;
773
774         gettimeofday(&now, NULL);
775         FOR_EACH_SNAPSHOT(s, i, sl) {
776                 if (!(s->flags & SS_COMPLETE))
777                         continue;
778                 num_complete_snapshots++;
779                 x += s->completion_time - s->creation_time;
780         }
781         assert(x >= 0);
782         if (num_complete_snapshots)
783                 x /= num_complete_snapshots; /* avg time to create one snapshot */
784         x *= wanted; /* time to create all snapshots in interval 0 */
785         tmp.tv_sec = x;
786         tmp.tv_usec = 0;
787         ret = tv_diff(&unit_interval, &tmp, &diff); /* time between creation */
788         if (ret < 0) {
789                 next_snapshot_time = now;
790                 return;
791         }
792         tv_divide(wanted, &diff, &tmp);
793         tv_add(&now, &tmp, &next_snapshot_time);
794 }
795
796 void handle_signal(void)
797 {
798         int sig, ret = next_signal();
799
800         if (ret <= 0)
801                 goto out;
802         sig = ret;
803         switch (sig) {
804                 int status;
805                 pid_t pid;
806         case SIGINT:
807         case SIGTERM:
808                 restart_rsync_process();
809                 kill_process(rsync_pid);
810                 kill_process(rm_pid);
811                 exit(EXIT_FAILURE);
812         case SIGHUP:
813                 handle_sighup();
814                 ret = 1;
815                 break;
816         case SIGCHLD:
817                 ret = reap_child(&pid, &status);
818                 if (ret <= 0)
819                         break;
820                 assert(pid == rsync_pid || pid == rm_pid);
821                 if (pid == rsync_pid)
822                         ret = handle_rsync_exit(status);
823                 else
824                         ret = handle_rm_exit(status);
825         }
826 out:
827         if (ret < 0)
828                 log_err_msg(ERROR, -ret);
829 }
830
831 int get_oldest(const char *dirname, void *private)
832 {
833         struct edge_snapshot_data *esd = private;
834         struct snapshot s;
835         int ret = is_snapshot(dirname, esd->now, &s);
836
837         if (ret <= 0)
838                 return 1;
839         if (s.creation_time > esd->snap.creation_time)
840                 return 1;
841         free(esd->snap.name);
842         esd->snap = s;
843         return 1;
844 }
845
846 int remove_oldest_snapshot()
847 {
848         int ret;
849         struct edge_snapshot_data esd = {
850                 .now = get_current_time(),
851                 .snap = {.creation_time = LLONG_MAX}
852         };
853         for_each_subdir(get_oldest, &esd);
854         if (!esd.snap.name) /* no snapshot found */
855                 return 0;
856         DSS_INFO_LOG("oldest snapshot: %s\n", esd.snap.name);
857         ret = 0;
858         if (esd.snap.creation_time == current_snapshot_creation_time)
859                 goto out; /* do not remove the snapshot currently being created */
860         ret = remove_snapshot(&esd.snap);
861 out:
862         free(esd.snap.name);
863         return ret;
864 }
865
866 /* TODO: Also consider number of inodes. */
867 int disk_space_low(void)
868 {
869         struct disk_space ds;
870         int ret = get_disk_space(".", &ds);
871
872         if (ret < 0)
873                 return ret;
874         if (conf.min_free_mb_arg)
875                 if (ds.free_mb < conf.min_free_mb_arg)
876                         return 1;
877         if (conf.min_free_percent_arg)
878                 if (ds.percent_free < conf.min_free_percent_arg)
879                         return 1;
880         return 0;
881 }
882
883 int try_to_free_disk_space(int low_disk_space, struct snapshot_list *sl)
884 {
885         int ret;
886
887         ret = remove_outdated_snapshot(sl);
888         if (ret) /* error, or we are removing something */
889                 return ret;
890         /* no outdated snapshot */
891         ret = remove_redundant_snapshot(sl);
892         if (ret)
893                 return ret;
894         if (!low_disk_space)
895                 return 0;
896         DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
897         ret = remove_oldest_snapshot();
898         if (ret)
899                 return ret;
900         make_err_msg("uhuhu: not enough disk space for a single snapshot");
901         return  -ENOSPC;
902 }
903
904 int select_loop(void)
905 {
906         int ret;
907         struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
908         struct snapshot_list sl = {.num_snapshots = 0};
909
910         for (;;) {
911                 struct timeval now, *tvp = &tv;
912                 fd_set rfds;
913                 int low_disk_space;
914                 char **rsync_argv;
915
916                 free_snapshot_list(&sl);
917                 get_snapshot_list(&sl);
918                 compute_next_snapshot_time(&sl);
919                 FD_ZERO(&rfds);
920                 FD_SET(signal_pipe, &rfds);
921                 if (rsync_pid)
922                         tv.tv_sec = 60;
923                 else if (rm_pid)
924                         tvp = NULL;
925                 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
926                 if (ret < 0)
927                         return ret;
928                 if (FD_ISSET(signal_pipe, &rfds)) {
929                         handle_signal();
930                         continue;
931                 }
932                 if (rm_pid)
933                         continue;
934                 ret = disk_space_low();
935                 if (ret < 0)
936                         break;
937                 low_disk_space = ret;
938                 if (low_disk_space)
939                         stop_rsync_process();
940                 ret = try_to_free_disk_space(low_disk_space, &sl);
941                 if (ret < 0)
942                         break;
943                 if (rm_pid)
944                         continue;
945                 if (rsync_pid) {
946                         restart_rsync_process();
947                         continue;
948                 }
949                 /* neither rsync nor rm are running. Start rsync? */
950                 gettimeofday(&now, NULL);
951                 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
952                         continue;
953                 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
954                 ret = create_snapshot(rsync_argv);
955                 free_rsync_argv(rsync_argv);
956                 if (ret < 0)
957                         break;
958         }
959         free_snapshot_list(&sl);
960         return ret;
961 }
962
963 int com_run(void)
964 {
965         int ret;
966
967         if (conf.dry_run_given) {
968                 make_err_msg("dry_run not supported by this command");
969                 return -E_SYNTAX;
970         }
971         ret = install_sighandler(SIGHUP);
972         if (ret < 0)
973                 return ret;
974         return select_loop();
975 }
976
977 void log_disk_space(struct disk_space *ds)
978 {
979         DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
980                 ds->free_mb, ds->total_mb, ds->percent_free,
981                 ds->percent_free_inodes);
982 }
983
984 int com_prune(void)
985 {
986         int ret;
987         struct snapshot_list sl;
988         struct disk_space ds;
989
990         ret = get_disk_space(".", &ds);
991         if (ret < 0)
992                 return ret;
993         log_disk_space(&ds);
994         for (;;) {
995                 get_snapshot_list(&sl);
996                 ret = remove_outdated_snapshot(&sl);
997                 free_snapshot_list(&sl);
998                 if (ret < 0)
999                         return ret;
1000                 if (!ret)
1001                         break;
1002                 ret = wait_for_rm_process();
1003                 if (ret < 0)
1004                         goto out;
1005         }
1006         for (;;) {
1007                 get_snapshot_list(&sl);
1008                 ret = remove_redundant_snapshot(&sl);
1009                 free_snapshot_list(&sl);
1010                 if (ret < 0)
1011                         return ret;
1012                 if (!ret)
1013                         break;
1014                 ret = wait_for_rm_process();
1015                 if (ret < 0)
1016                         goto out;
1017         }
1018         return 1;
1019 out:
1020         return ret;
1021 }
1022
1023 int com_create(void)
1024 {
1025         int ret, status;
1026         char **rsync_argv;
1027
1028         create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1029         if (conf.dry_run_given) {
1030                 int i;
1031                 char *msg = NULL;
1032                 for (i = 0; rsync_argv[i]; i++) {
1033                         char *tmp = msg;
1034                         msg = make_message("%s%s%s", tmp? tmp : "",
1035                                 tmp? " " : "", rsync_argv[i]);
1036                         free(tmp);
1037                 }
1038                 dss_msg("%s\n", msg);
1039                 free(msg);
1040                 return 1;
1041         }
1042         ret = create_snapshot(rsync_argv);
1043         if (ret < 0)
1044                 goto out;
1045         ret = wait_for_process(rsync_pid, &status);
1046         if (ret < 0)
1047                 goto out;
1048         ret = handle_rsync_exit(status);
1049 out:
1050         free_rsync_argv(rsync_argv);
1051         return ret;
1052 }
1053
1054 int com_ls(void)
1055 {
1056         int i;
1057         struct snapshot_list sl;
1058         struct snapshot *s;
1059         get_snapshot_list(&sl);
1060         FOR_EACH_SNAPSHOT(s, i, &sl)
1061                 dss_msg("%u\t%s\n", s->interval, s->name);
1062         free_snapshot_list(&sl);
1063         return 1;
1064 }
1065
1066 __noreturn void clean_exit(int status)
1067 {
1068         free(dss_error_txt);
1069         exit(status);
1070 }
1071 static void setup_signal_handling(void)
1072 {
1073         int ret;
1074
1075         DSS_INFO_LOG("setting up signal handlers\n");
1076         signal_pipe = signal_init(); /* always successful */
1077         ret = install_sighandler(SIGINT);
1078         if (ret < 0)
1079                 goto err;
1080         ret = install_sighandler(SIGTERM);
1081         if (ret < 0)
1082                 goto err;
1083         ret = install_sighandler(SIGCHLD);
1084         if (ret < 0)
1085                 goto err;
1086         return;
1087 err:
1088         DSS_EMERG_LOG("could not install signal handlers\n");
1089         exit(EXIT_FAILURE);
1090 }
1091
1092 int main(int argc, char **argv)
1093 {
1094         int ret;
1095
1096         cmdline_parser(argc, argv, &conf); /* aborts on errors */
1097         parse_config_file(0);
1098         if (conf.daemon_given)
1099                 daemon_init();
1100         setup_signal_handling();
1101         ret = call_command_handler();
1102         if (ret < 0)
1103                 log_err_msg(EMERG, -ret);
1104         clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
1105 }