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