080b369b0dea7558268568b7c6c8bb712f00231d
[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 void compute_next_snapshot_time(struct snapshot_list *sl)
592 {
593         struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
594                 tmp, diff;
595         int64_t x = 0;
596         unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
597         int i, ret;
598         struct snapshot *s;
599
600         gettimeofday(&now, NULL);
601         FOR_EACH_SNAPSHOT(s, i, sl) {
602                 if (!(s->flags & SS_COMPLETE))
603                         continue;
604                 num_complete_snapshots++;
605                 x += s->completion_time - s->creation_time;
606         }
607         assert(x >= 0);
608         if (num_complete_snapshots)
609                 x /= num_complete_snapshots; /* avg time to create one snapshot */
610         x *= wanted; /* time to create all snapshots in interval 0 */
611         tmp.tv_sec = x;
612         tmp.tv_usec = 0;
613         ret = tv_diff(&unit_interval, &tmp, &diff); /* time between creation */
614         if (ret < 0) {
615                 next_snapshot_time = now;
616                 return;
617         }
618         tv_divide(wanted, &diff, &tmp);
619         tv_add(&now, &tmp, &next_snapshot_time);
620 }
621
622 void handle_signal(struct snapshot_list *sl)
623 {
624         int sig, ret = next_signal();
625
626         if (ret <= 0)
627                 goto out;
628         sig = ret;
629         switch (sig) {
630                 int status;
631                 pid_t pid;
632         case SIGINT:
633         case SIGTERM:
634                 restart_rsync_process();
635                 kill_process(rsync_pid);
636                 kill_process(rm_pid);
637                 exit(EXIT_FAILURE);
638         case SIGHUP:
639                 handle_sighup();
640                 break;
641         case SIGCHLD:
642                 ret = reap_child(&pid, &status);
643                 if (ret <= 0)
644                         break;
645                 assert(pid == rsync_pid || pid == rm_pid);
646                 if (pid == rsync_pid)
647                         ret = handle_rsync_exit(status);
648                 else
649                         ret = handle_rm_exit(status);
650                 free_snapshot_list(sl);
651                 get_snapshot_list(sl);
652                 compute_next_snapshot_time(sl);
653         }
654 out:
655         if (ret < 0)
656                 log_err_msg(ERROR, -ret);
657 }
658
659 int get_oldest(const char *dirname, void *private)
660 {
661         struct edge_snapshot_data *esd = private;
662         struct snapshot s;
663         int ret = is_snapshot(dirname, esd->now, &s);
664
665         if (ret <= 0)
666                 return 1;
667         if (s.creation_time > esd->snap.creation_time)
668                 return 1;
669         free(esd->snap.name);
670         esd->snap = s;
671         return 1;
672 }
673
674 int remove_oldest_snapshot()
675 {
676         int ret;
677         struct edge_snapshot_data esd = {
678                 .now = get_current_time(),
679                 .snap = {.creation_time = LLONG_MAX}
680         };
681         for_each_subdir(get_oldest, &esd);
682         if (!esd.snap.name) /* no snapshot found */
683                 return 0;
684         DSS_INFO_LOG("oldest snapshot: %s\n", esd.snap.name);
685         ret = 0;
686         if (esd.snap.creation_time == current_snapshot_creation_time)
687                 goto out; /* do not remove the snapshot currently being created */
688         ret = remove_snapshot(&esd.snap);
689 out:
690         free(esd.snap.name);
691         return ret;
692 }
693
694 /* TODO: Also consider number of inodes. */
695 int disk_space_low(void)
696 {
697         struct disk_space ds;
698         int ret = get_disk_space(".", &ds);
699
700         if (ret < 0)
701                 return ret;
702         if (conf.min_free_mb_arg)
703                 if (ds.free_mb < conf.min_free_mb_arg)
704                         return 1;
705         if (conf.min_free_percent_arg)
706                 if (ds.percent_free < conf.min_free_percent_arg)
707                         return 1;
708         return 0;
709 }
710
711 int try_to_free_disk_space(int low_disk_space, struct snapshot_list *sl)
712 {
713         int ret;
714
715         ret = remove_outdated_snapshot(sl);
716         if (ret) /* error, or we are removing something */
717                 return ret;
718         /* no outdated snapshot */
719         ret = remove_redundant_snapshot(sl);
720         if (ret)
721                 return ret;
722         if (!low_disk_space)
723                 return 0;
724         DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
725         ret = remove_oldest_snapshot();
726         if (ret)
727                 return ret;
728         make_err_msg("uhuhu: not enough disk space for a single snapshot");
729         return  -ENOSPC;
730 }
731
732 int select_loop(void)
733 {
734         int ret;
735         struct timeval tv = {.tv_sec = 60, .tv_usec = 0};
736         struct snapshot_list sl = {.num_snapshots = 0};
737
738         get_snapshot_list(&sl);
739         compute_next_snapshot_time(&sl);
740         for (;;) {
741                 struct timeval now;
742                 fd_set rfds;
743                 int low_disk_space;
744
745                 FD_ZERO(&rfds);
746                 ret = dss_select(signal_pipe + 1, &rfds, NULL, &tv);
747                 if (ret < 0)
748                         return ret;
749                 if (FD_ISSET(signal_pipe, &rfds))
750                         handle_signal(&sl);
751                 if (rm_pid)
752                         continue;
753                 ret = disk_space_low();
754                 if (ret < 0)
755                         break;
756                 low_disk_space = ret;
757                 if (low_disk_space)
758                         stop_rsync_process();
759                 ret = try_to_free_disk_space(low_disk_space, &sl);
760                 if (ret < 0)
761                         break;
762                 if (rm_pid)
763                         continue;
764                 if (rsync_pid) {
765                         restart_rsync_process();
766                         continue;
767                 }
768                 /* neither rsync nor rm are running. Start rsync? */
769                 gettimeofday(&now, NULL);
770                 if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
771                         continue;
772         }
773         free_snapshot_list(&sl);
774         return ret;
775 }
776
777 int com_run(void)
778 {
779         int ret;
780
781         if (conf.dry_run_given) {
782                 make_err_msg("dry_run not supported by this command");
783                 return -E_SYNTAX;
784         }
785         ret = install_sighandler(SIGHUP);
786         if (ret < 0)
787                 return ret;
788         return select_loop();
789 }
790
791 void log_disk_space(struct disk_space *ds)
792 {
793         DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
794                 ds->free_mb, ds->total_mb, ds->percent_free,
795                 ds->percent_free_inodes);
796 }
797
798 int com_prune(void)
799 {
800         int ret;
801         struct snapshot_list sl;
802         struct disk_space ds;
803
804         ret = get_disk_space(".", &ds);
805         if (ret < 0)
806                 return ret;
807         log_disk_space(&ds);
808         for (;;) {
809                 get_snapshot_list(&sl);
810                 ret = remove_outdated_snapshot(&sl);
811                 free_snapshot_list(&sl);
812                 if (ret < 0)
813                         return ret;
814                 if (!ret)
815                         break;
816                 ret = wait_for_rm_process();
817                 if (ret < 0)
818                         goto out;
819         }
820         for (;;) {
821                 get_snapshot_list(&sl);
822                 ret = remove_redundant_snapshot(&sl);
823                 free_snapshot_list(&sl);
824                 if (ret < 0)
825                         return ret;
826                 if (!ret)
827                         break;
828                 ret = wait_for_rm_process();
829                 if (ret < 0)
830                         goto out;
831         }
832         return 1;
833 out:
834         return ret;
835 }
836
837 int get_newest_complete(const char *dirname, void *private)
838 {
839         struct edge_snapshot_data *esd = private;
840         struct snapshot s;
841         int ret = is_snapshot(dirname, esd->now, &s);
842
843         if (ret <= 0)
844                 return 1;
845         if (s.flags != SS_COMPLETE) /* incomplete or being deleted */
846                 return 1;
847         if (s.creation_time < esd->snap.creation_time)
848                 return 1;
849         free(esd->snap.name);
850         esd->snap = s;
851         return 1;
852 }
853
854 __malloc char *name_of_newest_complete_snapshot(void)
855 {
856         struct edge_snapshot_data esd = {
857                 .now = get_current_time(),
858                 .snap = {.creation_time = -1}
859         };
860         for_each_subdir(get_newest_complete, &esd);
861         return esd.snap.name;
862 }
863
864 void create_rsync_argv(char ***argv, int64_t *num)
865 {
866         char *logname, *newest = name_of_newest_complete_snapshot();
867         int i = 0, j;
868
869         *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
870         (*argv)[i++] = dss_strdup("rsync");
871         (*argv)[i++] = dss_strdup("-aq");
872         (*argv)[i++] = dss_strdup("--delete");
873         for (j = 0; j < conf.rsync_option_given; j++)
874                 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
875         if (newest) {
876                 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
877                 (*argv)[i++] = make_message("--link-dest=../%s", newest);
878                 free(newest);
879         } else
880                 DSS_INFO_LOG("no previous snapshot found");
881         if (conf.exclude_patterns_given) {
882                 (*argv)[i++] = dss_strdup("--exclude-from");
883                 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
884
885         }
886         logname = dss_logname();
887         if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
888                 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
889         else
890                 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
891                         conf.remote_user_arg : logname,
892                         conf.remote_host_arg, conf.source_dir_arg);
893         free(logname);
894         *num = get_current_time();
895         (*argv)[i++] = incomplete_name(*num);
896         (*argv)[i++] = NULL;
897         for (j = 0; j < i; j++)
898                 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
899 }
900
901 void free_rsync_argv(char **argv)
902 {
903         int i;
904         for (i = 0; argv[i]; i++)
905                 free(argv[i]);
906         free(argv);
907 }
908
909 int create_snapshot(char **argv)
910 {
911         int fds[3] = {0, 0, 0};
912
913         return dss_exec(&rsync_pid, argv[0], argv, fds);
914 }
915
916 int com_create(void)
917 {
918         int ret, status;
919         char **rsync_argv;
920
921         create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
922         if (conf.dry_run_given) {
923                 int i;
924                 char *msg = NULL;
925                 for (i = 0; rsync_argv[i]; i++) {
926                         char *tmp = msg;
927                         msg = make_message("%s%s%s", tmp? tmp : "",
928                                 tmp? " " : "", rsync_argv[i]);
929                         free(tmp);
930                 }
931                 dss_msg("%s\n", msg);
932                 free(msg);
933                 return 1;
934         }
935         DSS_NOTICE_LOG("creating snapshot %lli\n",
936                 (long long)current_snapshot_creation_time);
937         ret = create_snapshot(rsync_argv);
938         if (ret < 0)
939                 goto out;
940         ret = wait_for_process(rsync_pid, &status);
941         if (ret < 0)
942                 goto out;
943         ret = handle_rsync_exit(status);
944 out:
945         free_rsync_argv(rsync_argv);
946         return ret;
947 }
948
949 int com_ls(void)
950 {
951         int i;
952         struct snapshot_list sl;
953         struct snapshot *s;
954         get_snapshot_list(&sl);
955         FOR_EACH_SNAPSHOT(s, i, &sl)
956                 dss_msg("%u\t%s\n", s->interval, s->name);
957         free_snapshot_list(&sl);
958         return 1;
959 }
960
961 /* TODO: Unlink pid file */
962 __noreturn void clean_exit(int status)
963 {
964         //kill(0, SIGTERM);
965         free(dss_error_txt);
966         exit(status);
967 }
968
969 int read_config_file(void)
970 {
971         int ret;
972         char *config_file;
973         struct stat statbuf;
974
975         if (conf.config_file_given)
976                 config_file = dss_strdup(conf.config_file_arg);
977         else {
978                 char *home = get_homedir();
979                 config_file = make_message("%s/.dssrc", home);
980                 free(home);
981         }
982         ret = stat(config_file, &statbuf);
983         if (ret && conf.config_file_given) {
984                 ret = -ERRNO_TO_DSS_ERROR(errno);
985                 make_err_msg("failed to stat config file %s", config_file);
986                 goto out;
987         }
988         if (!ret) {
989                 struct cmdline_parser_params params = {
990                         .override = 0,
991                         .initialize = 0,
992                         .check_required = 0,
993                         .check_ambiguity = 0
994                 };
995                 cmdline_parser_config_file(config_file, &conf, &params);
996         }
997         if (!conf.source_dir_given || !conf.dest_dir_given) {
998                 ret = -E_SYNTAX;
999                 make_err_msg("you need to specify both source_dir and dest_dir");
1000                 goto out;
1001         }
1002         ret = 1;
1003 out:
1004         free(config_file);
1005         return ret;
1006 }
1007
1008 int check_config(void)
1009 {
1010         if (conf.unit_interval_arg <= 0) {
1011                 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
1012                 return -E_INVALID_NUMBER;
1013         }
1014         DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
1015         if (conf.num_intervals_arg <= 0) {
1016                 make_err_msg("bad number of intervals  %i", conf.num_intervals_arg);
1017                 return -E_INVALID_NUMBER;
1018         }
1019         DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
1020         return 1;
1021 }
1022
1023 static void setup_signal_handling(void)
1024 {
1025         int ret;
1026
1027         DSS_NOTICE_LOG("setting up signal handlers\n");
1028         signal_pipe = signal_init(); /* always successful */
1029         ret = install_sighandler(SIGINT);
1030         if (ret < 0)
1031                 goto err;
1032         ret = install_sighandler(SIGTERM);
1033         if (ret < 0)
1034                 goto err;
1035         ret = install_sighandler(SIGCHLD);
1036         if (ret < 0)
1037                 goto err;
1038         return;
1039 err:
1040         DSS_EMERG_LOG("could not install signal handlers\n");
1041         exit(EXIT_FAILURE);
1042 }
1043
1044
1045 int main(int argc, char **argv)
1046 {
1047         int ret;
1048
1049         cmdline_parser(argc, argv, &conf); /* aborts on errors */
1050         if (conf.inputs_num) {
1051                 ret = -E_SYNTAX;
1052                 make_err_msg("additional non-options given");
1053                 goto out;
1054         }
1055         ret = read_config_file();
1056         if (ret < 0)
1057                 goto out;
1058         ret = check_config();
1059         if (ret < 0)
1060                 goto out;
1061         if (conf.logfile_given) {
1062                 logfile = open_log(conf.logfile_arg);
1063                 log_welcome(conf.loglevel_arg);
1064         }
1065         ret = dss_chdir(conf.dest_dir_arg);
1066         if (ret < 0)
1067                 goto out;
1068         if (conf.daemon_given)
1069                 daemon_init();
1070         setup_signal_handling();
1071         ret = call_command_handler();
1072 out:
1073         if (ret < 0)
1074                 log_err_msg(EMERG, -ret);
1075         clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
1076 }