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