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