]> git.tuebingen.mpg.de Git - dss.git/blob - dss.c
8b8e4bb5f69a0629381548de70ec2c43c3b30154
[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                 fd_set rfds;
387
388                 FD_ZERO(&rfds);
389                 FD_SET(signal_pipe, &rfds);
390                 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
391                 if (ret < 0)
392                         break;
393                 ret = next_signal();
394                 if (!ret)
395                         continue;
396                 if (ret == SIGCHLD) {
397                         ret = waitpid(pid, status, 0);
398                         if (ret >= 0)
399                                 break;
400                         if (errno != EINTR) { /* error */
401                                 ret = -ERRNO_TO_DSS_ERROR(errno);
402                                 break;
403                         }
404                 }
405                 /* SIGINT or SIGTERM */
406                 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
407                 kill(pid, SIGTERM);
408         }
409         if (ret < 0)
410                 make_err_msg("failed to wait for process %d", (int)pid);
411         else
412                 log_termination_msg(pid, *status);
413         return ret;
414 }
415
416 int remove_snapshot(struct snapshot *s)
417 {
418         int fds[3] = {0, 0, 0};
419         assert(!rm_pid);
420         char *new_name = being_deleted_name(s);
421         int ret = dss_rename(s->name, new_name);
422         char *argv[] = {"rm", "-rf", new_name, NULL};
423
424         if (ret < 0)
425                 goto out;
426         DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
427         stop_rsync_process();
428         ret = dss_exec(&rm_pid, argv[0], argv, fds);
429 out:
430         free(new_name);
431         return ret;
432 }
433
434 /*
435  * return: 0: no redundant snapshots, 1: rm process started, negative: error
436  */
437 int remove_redundant_snapshot(struct snapshot_list *sl)
438 {
439         int ret, i, interval;
440         struct snapshot *s;
441         unsigned missing = 0;
442
443         DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
444         for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
445                 unsigned keep = num_snapshots(interval);
446                 unsigned num = sl->interval_count[interval];
447                 struct snapshot *victim = NULL, *prev = NULL;
448                 int64_t score = LONG_MAX;
449
450                 if (keep >= num)
451                         missing += keep - num;
452                 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
453                         interval, keep, num, missing);
454                 if (keep + missing >= num)
455                         continue;
456                 /* redundant snapshot in this interval, pick snapshot with lowest score */
457                 FOR_EACH_SNAPSHOT(s, i, sl) {
458                         int64_t this_score;
459
460                         //DSS_DEBUG_LOG("checking %s\n", s->name);
461                         if (s->interval > interval) {
462                                 prev = s;
463                                 continue;
464                         }
465                         if (s->interval < interval)
466                                 break;
467                         if (!victim) {
468                                 victim = s;
469                                 prev = s;
470                                 continue;
471                         }
472                         assert(prev);
473                         /* check if s is a better victim */
474                         this_score = s->creation_time - prev->creation_time;
475                         assert(this_score >= 0);
476                         //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
477                         if (this_score < score) {
478                                 score = this_score;
479                                 victim = s;
480                         }
481                         prev = s;
482                 }
483                 assert(victim);
484                 if (conf.dry_run_given) {
485                         dss_msg("%s would be removed (interval = %i)\n",
486                                 victim->name, victim->interval);
487                         continue;
488                 }
489                 ret = remove_snapshot(victim);
490                 return ret < 0? ret : 1;
491         }
492         return 0;
493 }
494
495 int remove_outdated_snapshot(struct snapshot_list *sl)
496 {
497         int i, ret;
498         struct snapshot *s;
499
500         DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
501                 conf.num_intervals_arg);
502         FOR_EACH_SNAPSHOT(s, i, sl) {
503                 if (s->interval <= conf.num_intervals_arg)
504                         continue;
505                 if (conf.dry_run_given) {
506                         dss_msg("%s would be removed (interval = %i)\n",
507                                 s->name, s->interval);
508                         continue;
509                 }
510                 ret = remove_snapshot(s);
511                 if (ret < 0)
512                         return ret;
513                 return 1;
514         }
515         return 0;
516 }
517
518 int handle_rm_exit(int status)
519 {
520         int es, ret;
521
522         if (!WIFEXITED(status)) {
523                 make_err_msg("rm process %d died involuntary", (int)rm_pid);
524                 ret = -E_INVOLUNTARY_EXIT;
525                 goto out;
526         }
527         es = WEXITSTATUS(status);
528         if (es) {
529                 make_err_msg("rm process %d returned %d", (int)rm_pid, es);
530                 ret = -E_BAD_EXIT_CODE;
531                 goto out;
532         }
533         ret = 1;
534         rm_pid = 0;
535 out:
536         return ret;
537 }
538
539 int wait_for_rm_process(void)
540 {
541         int status, ret = wait_for_process(rm_pid, &status);
542
543         if (ret < 0)
544                 return ret;
545         return handle_rm_exit(status);
546 }
547
548 void kill_process(pid_t pid)
549 {
550         if (!pid)
551                 return;
552         DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
553         kill(pid, SIGTERM);
554 }
555
556 int check_config(void)
557 {
558         if (conf.unit_interval_arg <= 0) {
559                 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
560                 return -E_INVALID_NUMBER;
561         }
562         DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
563         if (conf.num_intervals_arg <= 0) {
564                 make_err_msg("bad number of intervals  %i", conf.num_intervals_arg);
565                 return -E_INVALID_NUMBER;
566         }
567         DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
568         return 1;
569 }
570
571 /* exits on errors */
572 void parse_config_file(int override)
573 {
574         int ret;
575         char *config_file;
576         struct stat statbuf;
577         char *old_logfile_arg = NULL;
578         int old_daemon_given = 0;
579
580         if (conf.config_file_given)
581                 config_file = dss_strdup(conf.config_file_arg);
582         else {
583                 char *home = get_homedir();
584                 config_file = make_message("%s/.dssrc", home);
585                 free(home);
586         }
587         if (override) { /* SIGHUP */
588                 if (conf.logfile_given)
589                         old_logfile_arg = dss_strdup(conf.logfile_arg);
590                 old_daemon_given = conf.daemon_given;
591         }
592
593         ret = stat(config_file, &statbuf);
594         if (ret && conf.config_file_given) {
595                 ret = -ERRNO_TO_DSS_ERROR(errno);
596                 make_err_msg("failed to stat config file %s", config_file);
597                 goto out;
598         }
599         if (!ret) {
600                 struct cmdline_parser_params params = {
601                         .override = override,
602                         .initialize = 0,
603                         .check_required = 1,
604                         .check_ambiguity = 0
605                 };
606                 cmdline_parser_config_file(config_file, &conf, &params);
607         }
608         ret = check_config();
609         if (ret < 0)
610                 goto out;
611         if (override) {
612                 /* don't change daemon mode on SIGHUP */
613                 conf.daemon_given = old_daemon_given;
614                 close_log(logfile);
615                 logfile = NULL;
616                 if (conf.logfile_given)
617                         free(old_logfile_arg);
618                 else if (conf.daemon_given) { /* re-use old logfile */
619                         conf.logfile_arg = old_logfile_arg;
620                         conf.logfile_given = 1;
621                 }
622         }
623         if (conf.logfile_given) {
624                 logfile = open_log(conf.logfile_arg);
625                 log_welcome(conf.loglevel_arg);
626         }
627         ret = dss_chdir(conf.dest_dir_arg);
628 out:
629         free(config_file);
630         if (ret >= 0)
631                 return;
632         log_err_msg(EMERG, -ret);
633         exit(EXIT_FAILURE);
634 }
635
636 void handle_sighup(void)
637 {
638         DSS_NOTICE_LOG("SIGHUP\n");
639         parse_config_file(1);
640 }
641
642 int rename_incomplete_snapshot(int64_t start)
643 {
644         char *old_name, *new_name;
645         int ret;
646
647         ret = complete_name(start, get_current_time(), &new_name);
648         if (ret < 0)
649                 return ret;
650         old_name = incomplete_name(start);
651         ret = dss_rename(old_name, new_name);
652         if (ret >= 0)
653                 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
654         free(old_name);
655         free(new_name);
656         return ret;
657 }
658
659 int handle_rsync_exit(int status)
660 {
661         int es, ret;
662
663         if (!WIFEXITED(status)) {
664                 make_err_msg("rsync process %d died involuntary", (int)rsync_pid);
665                 ret = -E_INVOLUNTARY_EXIT;
666                 goto out;
667         }
668         es = WEXITSTATUS(status);
669         if (es != 0 && es != 23 && es != 24) {
670                 make_err_msg("rsync process %d returned %d", (int)rsync_pid, es);
671                 ret = -E_BAD_EXIT_CODE;
672                 goto out;
673         }
674         ret = rename_incomplete_snapshot(current_snapshot_creation_time);
675 out:
676         rsync_pid = 0;
677         current_snapshot_creation_time = 0;
678         rsync_stopped = 0;
679         return ret;
680 }
681
682 int get_newest_complete(const char *dirname, void *private)
683 {
684         struct edge_snapshot_data *esd = private;
685         struct snapshot s;
686         int ret = is_snapshot(dirname, esd->now, &s);
687
688         if (ret <= 0)
689                 return 1;
690         if (s.flags != SS_COMPLETE) /* incomplete or being deleted */
691                 return 1;
692         if (s.creation_time < esd->snap.creation_time)
693                 return 1;
694         free(esd->snap.name);
695         esd->snap = s;
696         return 1;
697 }
698
699 __malloc char *name_of_newest_complete_snapshot(void)
700 {
701         struct edge_snapshot_data esd = {
702                 .now = get_current_time(),
703                 .snap = {.creation_time = -1}
704         };
705         for_each_subdir(get_newest_complete, &esd);
706         return esd.snap.name;
707 }
708
709 void create_rsync_argv(char ***argv, int64_t *num)
710 {
711         char *logname, *newest = name_of_newest_complete_snapshot();
712         int i = 0, j;
713
714         *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
715         (*argv)[i++] = dss_strdup("rsync");
716         (*argv)[i++] = dss_strdup("-aq");
717         (*argv)[i++] = dss_strdup("--delete");
718         for (j = 0; j < conf.rsync_option_given; j++)
719                 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
720         if (newest) {
721                 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
722                 (*argv)[i++] = make_message("--link-dest=../%s", newest);
723                 free(newest);
724         } else
725                 DSS_INFO_LOG("no previous snapshot found\n");
726         if (conf.exclude_patterns_given) {
727                 (*argv)[i++] = dss_strdup("--exclude-from");
728                 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
729
730         }
731         logname = dss_logname();
732         if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
733                 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
734         else
735                 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
736                         conf.remote_user_arg : logname,
737                         conf.remote_host_arg, conf.source_dir_arg);
738         free(logname);
739         *num = get_current_time();
740         (*argv)[i++] = incomplete_name(*num);
741         (*argv)[i++] = NULL;
742         for (j = 0; j < i; j++)
743                 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
744 }
745
746 void free_rsync_argv(char **argv)
747 {
748         int i;
749         for (i = 0; argv[i]; i++)
750                 free(argv[i]);
751         free(argv);
752 }
753
754 int create_snapshot(char **argv)
755 {
756         int fds[3] = {0, 0, 0};
757         char *name = incomplete_name(current_snapshot_creation_time);
758
759         DSS_NOTICE_LOG("creating new snapshot %s\n", name);
760         free(name);
761         return dss_exec(&rsync_pid, argv[0], argv, fds);
762 }
763
764 void compute_next_snapshot_time(struct snapshot_list *sl)
765 {
766         struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
767                 tmp, diff;
768         int64_t x = 0;
769         unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
770         int i, ret;
771         struct snapshot *s;
772
773         gettimeofday(&now, NULL);
774         FOR_EACH_SNAPSHOT(s, i, sl) {
775                 if (!(s->flags & SS_COMPLETE))
776                         continue;
777                 num_complete_snapshots++;
778                 x += s->completion_time - s->creation_time;
779         }
780         assert(x >= 0);
781         if (num_complete_snapshots)
782                 x /= num_complete_snapshots; /* avg time to create one snapshot */
783         x *= wanted; /* time to create all snapshots in interval 0 */
784         tmp.tv_sec = x;
785         tmp.tv_usec = 0;
786         ret = tv_diff(&unit_interval, &tmp, &diff); /* time between creation */
787         if (ret < 0) {
788                 next_snapshot_time = now;
789                 return;
790         }
791         tv_divide(wanted, &diff, &tmp);
792         tv_add(&now, &tmp, &next_snapshot_time);
793 }
794
795 void handle_signal(void)
796 {
797         int sig, ret = next_signal();
798
799         if (ret <= 0)
800                 goto out;
801         sig = ret;
802         switch (sig) {
803                 int status;
804                 pid_t pid;
805         case SIGINT:
806         case SIGTERM:
807                 restart_rsync_process();
808                 kill_process(rsync_pid);
809                 kill_process(rm_pid);
810                 exit(EXIT_FAILURE);
811         case SIGHUP:
812                 handle_sighup();
813                 ret = 1;
814                 break;
815         case SIGCHLD:
816                 ret = reap_child(&pid, &status);
817                 if (ret <= 0)
818                         break;
819                 assert(pid == rsync_pid || pid == rm_pid);
820                 if (pid == rsync_pid)
821                         ret = handle_rsync_exit(status);
822                 else
823                         ret = handle_rm_exit(status);
824         }
825 out:
826         if (ret < 0)
827                 log_err_msg(ERROR, -ret);
828 }
829
830 int get_oldest(const char *dirname, void *private)
831 {
832         struct edge_snapshot_data *esd = private;
833         struct snapshot s;
834         int ret = is_snapshot(dirname, esd->now, &s);
835
836         if (ret <= 0)
837                 return 1;
838         if (s.creation_time > esd->snap.creation_time)
839                 return 1;
840         free(esd->snap.name);
841         esd->snap = s;
842         return 1;
843 }
844
845 int remove_oldest_snapshot()
846 {
847         int ret;
848         struct edge_snapshot_data esd = {
849                 .now = get_current_time(),
850                 .snap = {.creation_time = LLONG_MAX}
851         };
852         for_each_subdir(get_oldest, &esd);
853         if (!esd.snap.name) /* no snapshot found */
854                 return 0;
855         DSS_INFO_LOG("oldest snapshot: %s\n", esd.snap.name);
856         ret = 0;
857         if (esd.snap.creation_time == current_snapshot_creation_time)
858                 goto out; /* do not remove the snapshot currently being created */
859         ret = remove_snapshot(&esd.snap);
860 out:
861         free(esd.snap.name);
862         return ret;
863 }
864
865 /* TODO: Also consider number of inodes. */
866 int disk_space_low(void)
867 {
868         struct disk_space ds;
869         int ret = get_disk_space(".", &ds);
870
871         if (ret < 0)
872                 return ret;
873         if (conf.min_free_mb_arg)
874                 if (ds.free_mb < conf.min_free_mb_arg)
875                         return 1;
876         if (conf.min_free_percent_arg)
877                 if (ds.percent_free < conf.min_free_percent_arg)
878                         return 1;
879         return 0;
880 }
881
882 int try_to_free_disk_space(int low_disk_space, struct snapshot_list *sl)
883 {
884         int ret;
885
886         ret = remove_outdated_snapshot(sl);
887         if (ret) /* error, or we are removing something */
888                 return ret;
889         /* no outdated snapshot */
890         ret = remove_redundant_snapshot(sl);
891         if (ret)
892                 return ret;
893         if (!low_disk_space)
894                 return 0;
895         DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
896         ret = remove_oldest_snapshot();
897         if (ret)
898                 return ret;
899         make_err_msg("uhuhu: not enough disk space for a single snapshot");
900         return  -ENOSPC;
901 }
902
903 int select_loop(void)
904 {
905         int ret;
906         struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
907         struct snapshot_list sl = {.num_snapshots = 0};
908
909         for (;;) {
910                 struct timeval now, *tvp = &tv;
911                 fd_set rfds;
912                 int low_disk_space;
913                 char **rsync_argv;
914
915                 free_snapshot_list(&sl);
916                 get_snapshot_list(&sl);
917                 compute_next_snapshot_time(&sl);
918                 FD_ZERO(&rfds);
919                 FD_SET(signal_pipe, &rfds);
920                 if (rsync_pid)
921                         tv.tv_sec = 60;
922                 else if (rm_pid)
923                         tvp = NULL;
924                 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
925                 if (ret < 0)
926                         return ret;
927                 if (FD_ISSET(signal_pipe, &rfds)) {
928                         handle_signal();
929                         continue;
930                 }
931                 if (rm_pid)
932                         continue;
933                 ret = disk_space_low();
934                 if (ret < 0)
935                         break;
936                 low_disk_space = ret;
937                 if (low_disk_space)
938                         stop_rsync_process();
939                 ret = try_to_free_disk_space(low_disk_space, &sl);
940                 if (ret < 0)
941                         break;
942                 if (rm_pid)
943                         continue;
944                 if (rsync_pid) {
945                         restart_rsync_process();
946                         continue;
947                 }
948                 /* neither rsync nor rm are running. Start rsync? */
949                 gettimeofday(&now, NULL);
950                 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
951                         continue;
952                 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
953                 ret = create_snapshot(rsync_argv);
954                 free_rsync_argv(rsync_argv);
955                 if (ret < 0)
956                         break;
957         }
958         free_snapshot_list(&sl);
959         return ret;
960 }
961
962 int com_run(void)
963 {
964         int ret;
965
966         if (conf.dry_run_given) {
967                 make_err_msg("dry_run not supported by this command");
968                 return -E_SYNTAX;
969         }
970         ret = install_sighandler(SIGHUP);
971         if (ret < 0)
972                 return ret;
973         return select_loop();
974 }
975
976 void log_disk_space(struct disk_space *ds)
977 {
978         DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
979                 ds->free_mb, ds->total_mb, ds->percent_free,
980                 ds->percent_free_inodes);
981 }
982
983 int com_prune(void)
984 {
985         int ret;
986         struct snapshot_list sl;
987         struct disk_space ds;
988
989         ret = get_disk_space(".", &ds);
990         if (ret < 0)
991                 return ret;
992         log_disk_space(&ds);
993         for (;;) {
994                 get_snapshot_list(&sl);
995                 ret = remove_outdated_snapshot(&sl);
996                 free_snapshot_list(&sl);
997                 if (ret < 0)
998                         return ret;
999                 if (!ret)
1000                         break;
1001                 ret = wait_for_rm_process();
1002                 if (ret < 0)
1003                         goto out;
1004         }
1005         for (;;) {
1006                 get_snapshot_list(&sl);
1007                 ret = remove_redundant_snapshot(&sl);
1008                 free_snapshot_list(&sl);
1009                 if (ret < 0)
1010                         return ret;
1011                 if (!ret)
1012                         break;
1013                 ret = wait_for_rm_process();
1014                 if (ret < 0)
1015                         goto out;
1016         }
1017         return 1;
1018 out:
1019         return ret;
1020 }
1021
1022 int com_create(void)
1023 {
1024         int ret, status;
1025         char **rsync_argv;
1026
1027         create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1028         if (conf.dry_run_given) {
1029                 int i;
1030                 char *msg = NULL;
1031                 for (i = 0; rsync_argv[i]; i++) {
1032                         char *tmp = msg;
1033                         msg = make_message("%s%s%s", tmp? tmp : "",
1034                                 tmp? " " : "", rsync_argv[i]);
1035                         free(tmp);
1036                 }
1037                 dss_msg("%s\n", msg);
1038                 free(msg);
1039                 return 1;
1040         }
1041         ret = create_snapshot(rsync_argv);
1042         if (ret < 0)
1043                 goto out;
1044         ret = wait_for_process(rsync_pid, &status);
1045         if (ret < 0)
1046                 goto out;
1047         ret = handle_rsync_exit(status);
1048 out:
1049         free_rsync_argv(rsync_argv);
1050         return ret;
1051 }
1052
1053 int com_ls(void)
1054 {
1055         int i;
1056         struct snapshot_list sl;
1057         struct snapshot *s;
1058         get_snapshot_list(&sl);
1059         FOR_EACH_SNAPSHOT(s, i, &sl)
1060                 dss_msg("%u\t%s\n", s->interval, s->name);
1061         free_snapshot_list(&sl);
1062         return 1;
1063 }
1064
1065 __noreturn void clean_exit(int status)
1066 {
1067         free(dss_error_txt);
1068         exit(status);
1069 }
1070 static void setup_signal_handling(void)
1071 {
1072         int ret;
1073
1074         DSS_INFO_LOG("setting up signal handlers\n");
1075         signal_pipe = signal_init(); /* always successful */
1076         ret = install_sighandler(SIGINT);
1077         if (ret < 0)
1078                 goto err;
1079         ret = install_sighandler(SIGTERM);
1080         if (ret < 0)
1081                 goto err;
1082         ret = install_sighandler(SIGCHLD);
1083         if (ret < 0)
1084                 goto err;
1085         return;
1086 err:
1087         DSS_EMERG_LOG("could not install signal handlers\n");
1088         exit(EXIT_FAILURE);
1089 }
1090
1091 int main(int argc, char **argv)
1092 {
1093         int ret;
1094         struct cmdline_parser_params params = {
1095                 .override = 0,
1096                 .initialize = 1,
1097                 .check_required = 0,
1098                 .check_ambiguity = 0
1099         };
1100
1101         cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
1102         parse_config_file(0);
1103
1104         if (conf.daemon_given)
1105                 daemon_init();
1106         setup_signal_handling();
1107         ret = call_command_handler();
1108         if (ret < 0)
1109                 log_err_msg(EMERG, -ret);
1110         clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
1111 }