]> git.tuebingen.mpg.de Git - dss.git/blob - dss.c
Get rid of make_err_msg() and log_err_msg().
[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                 return -E_LOCALTIME;
275         if (!localtime_r(end_seconds, &end_tm))
276                 return -E_LOCALTIME;
277         if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm))
278                 return -E_STRFTIME;
279         if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm))
280                 return -E_STRFTIME;
281         *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
282                 start_str, end_str);
283         return 1;
284 }
285
286 struct snapshot_list {
287         int64_t now;
288         unsigned num_snapshots;
289         unsigned array_size;
290         struct snapshot **snapshots;
291         /**
292          * Array of size num_intervals + 1
293          *
294          * It contains the number of snapshots in each interval. interval_count[num_intervals]
295          * is the number of snapshots which belong to any interval greater than num_intervals.
296          */
297         unsigned *interval_count;
298 };
299
300 #define FOR_EACH_SNAPSHOT(s, i, sl) \
301         for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
302
303 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
304
305 static int compare_snapshots(const void *a, const void *b)
306 {
307         struct snapshot *s1 = *(struct snapshot **)a;
308         struct snapshot *s2 = *(struct snapshot **)b;
309         return NUM_COMPARE(s2->creation_time, s1->creation_time);
310 }
311
312 /** Compute the minimum of \a a and \a b. */
313 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
314
315 int add_snapshot(const char *dirname, void *private)
316 {
317         struct snapshot_list *sl = private;
318         struct snapshot s;
319         int ret = is_snapshot(dirname, sl->now, &s);
320
321         if (!ret)
322                 return 1;
323         if (sl->num_snapshots >= sl->array_size) {
324                 sl->array_size = 2 * sl->array_size + 1;
325                 sl->snapshots = dss_realloc(sl->snapshots,
326                         sl->array_size * sizeof(struct snapshot *));
327         }
328         sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
329         *(sl->snapshots[sl->num_snapshots]) = s;
330         sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
331         sl->num_snapshots++;
332         return 1;
333 }
334
335 void get_snapshot_list(struct snapshot_list *sl)
336 {
337         sl->now = get_current_time();
338         sl->num_snapshots = 0;
339         sl->array_size = 0;
340         sl->snapshots = NULL;
341         sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
342         for_each_subdir(add_snapshot, sl);
343         qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
344                 compare_snapshots);
345 }
346
347 void free_snapshot_list(struct snapshot_list *sl)
348 {
349         int i;
350         struct snapshot *s;
351
352         FOR_EACH_SNAPSHOT(s, i, sl) {
353                 free(s->name);
354                 free(s);
355         }
356         free(sl->interval_count);
357         sl->interval_count = NULL;
358         free(sl->snapshots);
359         sl->snapshots = NULL;
360         sl->num_snapshots = 0;
361 }
362
363 void stop_rsync_process(void)
364 {
365         if (!rsync_pid || rsync_stopped)
366                 return;
367         kill(SIGSTOP, rsync_pid);
368         rsync_stopped = 1;
369 }
370
371 void restart_rsync_process(void)
372 {
373         if (!rsync_pid || !rsync_stopped)
374                 return;
375         kill (SIGCONT, rsync_pid);
376         rsync_stopped = 0;
377 }
378
379 /**
380  * Print a log message about the exit status of a child.
381  */
382 void log_termination_msg(pid_t pid, int status)
383 {
384         if (WIFEXITED(status))
385                 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
386                         WEXITSTATUS(status));
387         else if (WIFSIGNALED(status))
388                 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
389                         WTERMSIG(status));
390         else
391                 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
392 }
393
394 int wait_for_process(pid_t pid, int *status)
395 {
396         int ret;
397
398         DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
399         for (;;) {
400                 fd_set rfds;
401
402                 FD_ZERO(&rfds);
403                 FD_SET(signal_pipe, &rfds);
404                 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
405                 if (ret < 0)
406                         break;
407                 ret = next_signal();
408                 if (!ret)
409                         continue;
410                 if (ret == SIGCHLD) {
411                         ret = waitpid(pid, status, 0);
412                         if (ret >= 0)
413                                 break;
414                         if (errno != EINTR) { /* error */
415                                 ret = -ERRNO_TO_DSS_ERROR(errno);
416                                 break;
417                         }
418                 }
419                 /* SIGINT or SIGTERM */
420                 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
421                 kill(pid, SIGTERM);
422         }
423         if (ret < 0)
424                 DSS_ERROR_LOG("failed to wait for process %d\n", (int)pid);
425         else
426                 log_termination_msg(pid, *status);
427         return ret;
428 }
429
430 int remove_snapshot(struct snapshot *s)
431 {
432         int fds[3] = {0, 0, 0};
433         assert(!rm_pid);
434         char *new_name = being_deleted_name(s);
435         int ret = dss_rename(s->name, new_name);
436         char *argv[] = {"rm", "-rf", new_name, NULL};
437
438         if (ret < 0)
439                 goto out;
440         DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
441         stop_rsync_process();
442         ret = dss_exec(&rm_pid, argv[0], argv, fds);
443 out:
444         free(new_name);
445         return ret;
446 }
447
448 /*
449  * return: 0: no redundant snapshots, 1: rm process started, negative: error
450  */
451 int remove_redundant_snapshot(struct snapshot_list *sl)
452 {
453         int ret, i, interval;
454         struct snapshot *s;
455         unsigned missing = 0;
456
457         DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
458         for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
459                 unsigned keep = num_snapshots(interval);
460                 unsigned num = sl->interval_count[interval];
461                 struct snapshot *victim = NULL, *prev = NULL;
462                 int64_t score = LONG_MAX;
463
464                 if (keep >= num)
465                         missing += keep - num;
466 //              DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
467 //                      interval, keep, num, missing);
468                 if (keep + missing >= num)
469                         continue;
470                 /* redundant snapshot in this interval, pick snapshot with lowest score */
471                 FOR_EACH_SNAPSHOT(s, i, sl) {
472                         int64_t this_score;
473
474                         //DSS_DEBUG_LOG("checking %s\n", s->name);
475                         if (s->interval > interval) {
476                                 prev = s;
477                                 continue;
478                         }
479                         if (s->interval < interval)
480                                 break;
481                         if (!victim) {
482                                 victim = s;
483                                 prev = s;
484                                 continue;
485                         }
486                         assert(prev);
487                         /* check if s is a better victim */
488                         this_score = s->creation_time - prev->creation_time;
489                         assert(this_score >= 0);
490                         //DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
491                         if (this_score < score) {
492                                 score = this_score;
493                                 victim = s;
494                         }
495                         prev = s;
496                 }
497                 assert(victim);
498                 if (conf.dry_run_given) {
499                         dss_msg("%s would be removed (interval = %i)\n",
500                                 victim->name, victim->interval);
501                         continue;
502                 }
503                 ret = remove_snapshot(victim);
504                 return ret < 0? ret : 1;
505         }
506         return 0;
507 }
508
509 int remove_outdated_snapshot(struct snapshot_list *sl)
510 {
511         int i, ret;
512         struct snapshot *s;
513
514         DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
515                 conf.num_intervals_arg);
516         FOR_EACH_SNAPSHOT(s, i, sl) {
517                 if (s->interval <= conf.num_intervals_arg)
518                         continue;
519                 if (conf.dry_run_given) {
520                         dss_msg("%s would be removed (interval = %i)\n",
521                                 s->name, s->interval);
522                         continue;
523                 }
524                 ret = remove_snapshot(s);
525                 if (ret < 0)
526                         return ret;
527                 return 1;
528         }
529         return 0;
530 }
531
532 int handle_rm_exit(int status)
533 {
534         rm_pid = 0;
535         if (!WIFEXITED(status))
536                 return -E_INVOLUNTARY_EXIT;
537         if (WEXITSTATUS(status))
538                 return -E_BAD_EXIT_CODE;
539         return 1;
540 }
541
542 int wait_for_rm_process(void)
543 {
544         int status, ret = wait_for_process(rm_pid, &status);
545
546         if (ret < 0)
547                 return ret;
548         return handle_rm_exit(status);
549 }
550
551 void kill_process(pid_t pid)
552 {
553         if (!pid)
554                 return;
555         DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
556         kill(pid, SIGTERM);
557 }
558
559 int check_config(void)
560 {
561         if (conf.unit_interval_arg <= 0) {
562                 DSS_ERROR_LOG("bad unit interval: %i\n", conf.unit_interval_arg);
563                 return -E_INVALID_NUMBER;
564         }
565         DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
566         if (conf.num_intervals_arg <= 0) {
567                 DSS_ERROR_LOG("bad number of intervals  %i\n", conf.num_intervals_arg);
568                 return -E_INVALID_NUMBER;
569         }
570         DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
571         return 1;
572 }
573
574 /* exits on errors */
575 void parse_config_file(int override)
576 {
577         int ret;
578         char *config_file;
579         struct stat statbuf;
580         char *old_logfile_arg = NULL;
581         int old_daemon_given = 0;
582
583         if (conf.config_file_given)
584                 config_file = dss_strdup(conf.config_file_arg);
585         else {
586                 char *home = get_homedir();
587                 config_file = make_message("%s/.dssrc", home);
588                 free(home);
589         }
590         if (override) { /* SIGHUP */
591                 if (conf.logfile_given)
592                         old_logfile_arg = dss_strdup(conf.logfile_arg);
593                 old_daemon_given = conf.daemon_given;
594         }
595
596         ret = stat(config_file, &statbuf);
597         if (ret && conf.config_file_given) {
598                 ret = -ERRNO_TO_DSS_ERROR(errno);
599                 DSS_ERROR_LOG("failed to stat config file %s\n", config_file);
600                 goto out;
601         }
602         if (!ret) {
603                 struct cmdline_parser_params params = {
604                         .override = override,
605                         .initialize = 0,
606                         .check_required = 1,
607                         .check_ambiguity = 0
608                 };
609                 cmdline_parser_config_file(config_file, &conf, &params);
610         }
611         ret = check_config();
612         if (ret < 0)
613                 goto out;
614         if (override) {
615                 /* don't change daemon mode on SIGHUP */
616                 conf.daemon_given = old_daemon_given;
617                 close_log(logfile);
618                 logfile = NULL;
619                 if (conf.logfile_given)
620                         free(old_logfile_arg);
621                 else if (conf.daemon_given) { /* re-use old logfile */
622                         conf.logfile_arg = old_logfile_arg;
623                         conf.logfile_given = 1;
624                 }
625         }
626         if (conf.logfile_given) {
627                 logfile = open_log(conf.logfile_arg);
628                 log_welcome(conf.loglevel_arg);
629         }
630         DSS_EMERG_LOG("loglevel: %d\n", conf.loglevel_arg);
631 //      cmdline_parser_dump(logfile? logfile : stdout, &conf);
632         ret = dss_chdir(conf.dest_dir_arg);
633 out:
634         free(config_file);
635         if (ret >= 0)
636                 return;
637         DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
638         exit(EXIT_FAILURE);
639 }
640
641 void handle_sighup(void)
642 {
643         DSS_NOTICE_LOG("SIGHUP\n");
644         parse_config_file(1);
645 }
646
647 int rename_incomplete_snapshot(int64_t start)
648 {
649         char *old_name;
650         int ret;
651
652         free(path_to_last_complete_snapshot);
653         ret = complete_name(start, get_current_time(),
654                 &path_to_last_complete_snapshot);
655         if (ret < 0)
656                 return ret;
657         old_name = incomplete_name(start);
658         ret = dss_rename(old_name, path_to_last_complete_snapshot);
659         if (ret >= 0)
660                 DSS_NOTICE_LOG("%s -> %s\n", old_name,
661                         path_to_last_complete_snapshot);
662         free(old_name);
663         return ret;
664 }
665
666 void compute_next_snapshot_time(void)
667 {
668         struct timeval now, unit_interval = {.tv_sec = 24 * 3600 * conf.unit_interval_arg},
669                 tmp, diff;
670         int64_t x = 0;
671         unsigned wanted = num_snapshots(0), num_complete_snapshots = 0;
672         int i, ret;
673         struct snapshot *s = NULL;
674         struct snapshot_list sl;
675
676         assert(snapshot_creation_status == SCS_READY);
677         current_snapshot_creation_time = 0;
678         get_snapshot_list(&sl);
679         FOR_EACH_SNAPSHOT(s, i, &sl) {
680                 if (!(s->flags & SS_COMPLETE))
681                         continue;
682                 num_complete_snapshots++;
683                 x += s->completion_time - s->creation_time;
684         }
685         assert(x >= 0);
686         if (num_complete_snapshots)
687                 x /= num_complete_snapshots; /* avg time to create one snapshot */
688         x *= wanted; /* time to create all snapshots in interval 0 */
689         tmp.tv_sec = x;
690         tmp.tv_usec = 0;
691         ret = tv_diff(&unit_interval, &tmp, &diff); /* total sleep time per unit interval */
692         gettimeofday(&now, NULL);
693         if (ret < 0 || !s)
694                 goto min_sleep;
695         tv_divide(wanted, &diff, &tmp); /* sleep time betweeen two snapshots */
696         diff.tv_sec = s->completion_time;
697         diff.tv_usec = 0;
698         tv_add(&diff, &tmp, &next_snapshot_time);
699         if (tv_diff(&now, &next_snapshot_time, NULL) < 0)
700                 goto out;
701 min_sleep:
702         next_snapshot_time = now;
703         next_snapshot_time.tv_sec += 60;
704 out:
705         free_snapshot_list(&sl);
706 }
707
708 int handle_rsync_exit(int status)
709 {
710         int es, ret;
711
712         if (!WIFEXITED(status)) {
713                 DSS_ERROR_LOG("rsync process %d died involuntary\n", (int)rsync_pid);
714                 ret = -E_INVOLUNTARY_EXIT;
715                 snapshot_creation_status = SCS_READY;
716                 compute_next_snapshot_time();
717                 goto out;
718         }
719         es = WEXITSTATUS(status);
720         if (es != 0 && es != 23 && es != 24) {
721                 DSS_ERROR_LOG("rsync process %d returned %d\n", (int)rsync_pid, es);
722                 ret = -E_BAD_EXIT_CODE;
723                 snapshot_creation_status = SCS_READY;
724                 compute_next_snapshot_time();
725                 goto out;
726         }
727         ret = rename_incomplete_snapshot(current_snapshot_creation_time);
728         if (ret < 0)
729                 goto out;
730         snapshot_creation_status = SCS_RSYNC_SUCCESS;
731 out:
732         rsync_pid = 0;
733         rsync_stopped = 0;
734         return ret;
735 }
736
737 int get_newest_complete(const char *dirname, void *private)
738 {
739         struct edge_snapshot_data *esd = private;
740         struct snapshot s;
741         int ret = is_snapshot(dirname, esd->now, &s);
742
743         if (ret <= 0)
744                 return 1;
745         if (s.flags != SS_COMPLETE) /* incomplete or being deleted */
746                 return 1;
747         if (s.creation_time < esd->snap.creation_time)
748                 return 1;
749         free(esd->snap.name);
750         esd->snap = s;
751         return 1;
752 }
753
754 __malloc char *name_of_newest_complete_snapshot(void)
755 {
756         struct edge_snapshot_data esd = {
757                 .now = get_current_time(),
758                 .snap = {.creation_time = -1}
759         };
760         for_each_subdir(get_newest_complete, &esd);
761         return esd.snap.name;
762 }
763
764 void create_rsync_argv(char ***argv, int64_t *num)
765 {
766         char *logname, *newest = name_of_newest_complete_snapshot();
767         int i = 0, j;
768
769         *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
770         (*argv)[i++] = dss_strdup("rsync");
771         (*argv)[i++] = dss_strdup("-aq");
772         (*argv)[i++] = dss_strdup("--delete");
773         for (j = 0; j < conf.rsync_option_given; j++)
774                 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
775         if (newest) {
776                 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
777                 (*argv)[i++] = make_message("--link-dest=../%s", newest);
778                 free(newest);
779         } else
780                 DSS_INFO_LOG("no previous snapshot found\n");
781         if (conf.exclude_patterns_given) {
782                 (*argv)[i++] = dss_strdup("--exclude-from");
783                 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
784
785         }
786         logname = dss_logname();
787         if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
788                 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
789         else
790                 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
791                         conf.remote_user_arg : logname,
792                         conf.remote_host_arg, conf.source_dir_arg);
793         free(logname);
794         *num = get_current_time();
795         (*argv)[i++] = incomplete_name(*num);
796         (*argv)[i++] = NULL;
797         for (j = 0; j < i; j++)
798                 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
799 }
800
801 void free_rsync_argv(char **argv)
802 {
803         int i;
804         for (i = 0; argv[i]; i++)
805                 free(argv[i]);
806         free(argv);
807 }
808
809 int pre_create_hook(void)
810 {
811         int ret, fds[3] = {0, 0, 0};
812
813         if (!conf.pre_create_hook_given) {
814                 snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
815                 return 0;
816         }
817         DSS_NOTICE_LOG("executing %s\n", conf.pre_create_hook_arg);
818         ret = dss_exec_cmdline_pid(&pre_create_hook_pid,
819                 conf.pre_create_hook_arg, fds);
820         if (ret < 0)
821                 return ret;
822         snapshot_creation_status = SCS_PRE_HOOK_RUNNING;
823         return ret;
824 }
825
826 int post_create_hook(void)
827 {
828         int ret, fds[3] = {0, 0, 0};
829         char *cmd;
830
831         if (!conf.post_create_hook_given) {
832                 snapshot_creation_status = SCS_READY;
833                 compute_next_snapshot_time();
834                 return 0;
835         }
836         cmd = make_message("%s %s", conf.post_create_hook_arg,
837                 path_to_last_complete_snapshot);
838         DSS_NOTICE_LOG("executing %s\n", cmd);
839         ret = dss_exec_cmdline_pid(&post_create_hook_pid, cmd, fds);
840         free(cmd);
841         if (ret < 0)
842                 return ret;
843         snapshot_creation_status = SCS_POST_HOOK_RUNNING;
844         return ret;
845 }
846
847 int create_snapshot(char **argv)
848 {
849         int ret, fds[3] = {0, 0, 0};
850         char *name;
851
852         name = incomplete_name(current_snapshot_creation_time);
853         DSS_NOTICE_LOG("creating new snapshot %s\n", name);
854         free(name);
855         ret = dss_exec(&rsync_pid, argv[0], argv, fds);
856         if (ret < 0)
857                 return ret;
858         snapshot_creation_status = SCS_RSYNC_RUNNING;
859         return ret;
860 }
861
862 int handle_pre_create_hook_exit(int status)
863 {
864         int es, ret;
865
866         if (!WIFEXITED(status)) {
867                 snapshot_creation_status = SCS_READY;
868                 compute_next_snapshot_time();
869                 ret = -E_INVOLUNTARY_EXIT;
870                 goto out;
871         }
872         es = WEXITSTATUS(status);
873         if (es) {
874                 snapshot_creation_status = SCS_READY;
875                 compute_next_snapshot_time();
876                 ret = -E_BAD_EXIT_CODE;
877                 goto out;
878         }
879         snapshot_creation_status = SCS_PRE_HOOK_SUCCESS;
880         ret = 1;
881 out:
882         pre_create_hook_pid = 0;
883         return ret;
884 }
885
886 int handle_sigchld()
887 {
888         pid_t pid;
889         int status, ret = reap_child(&pid, &status);
890
891         if (ret <= 0)
892                 return ret;
893         if (pid == rsync_pid)
894                 return handle_rsync_exit(status);
895         if (pid == rm_pid)
896                 return handle_rm_exit(status);
897         if (pid == pre_create_hook_pid)
898                 return handle_pre_create_hook_exit(status);
899         if (pid == post_create_hook_pid) {
900                 snapshot_creation_status = SCS_READY;
901                 compute_next_snapshot_time();
902                 return 1;
903         }
904         DSS_EMERG_LOG("BUG: unknown process %d died\n", (int)pid);
905         exit(EXIT_FAILURE);
906 }
907
908 void handle_signal(void)
909 {
910         int sig, ret = next_signal();
911
912         if (ret <= 0)
913                 goto out;
914         sig = ret;
915         switch (sig) {
916         case SIGINT:
917         case SIGTERM:
918                 restart_rsync_process();
919                 kill_process(rsync_pid);
920                 kill_process(rm_pid);
921                 exit(EXIT_FAILURE);
922         case SIGHUP:
923                 handle_sighup();
924                 ret = 1;
925                 break;
926         case SIGCHLD:
927                 ret = handle_sigchld();
928                 break;
929         }
930 out:
931         if (ret < 0)
932                 DSS_ERROR_LOG("%s\n", dss_strerror(-ret));
933 }
934
935 int get_oldest(const char *dirname, void *private)
936 {
937         struct edge_snapshot_data *esd = private;
938         struct snapshot s;
939         int ret = is_snapshot(dirname, esd->now, &s);
940
941         if (ret <= 0)
942                 return 1;
943         if (s.creation_time > esd->snap.creation_time)
944                 return 1;
945         free(esd->snap.name);
946         esd->snap = s;
947         return 1;
948 }
949
950 int remove_oldest_snapshot()
951 {
952         int ret;
953         struct edge_snapshot_data esd = {
954                 .now = get_current_time(),
955                 .snap = {.creation_time = LLONG_MAX}
956         };
957         for_each_subdir(get_oldest, &esd);
958         if (!esd.snap.name) /* no snapshot found */
959                 return 0;
960         DSS_INFO_LOG("oldest snapshot: %s\n", esd.snap.name);
961         ret = 0;
962         if (esd.snap.creation_time == current_snapshot_creation_time)
963                 goto out; /* do not remove the snapshot currently being created */
964         ret = remove_snapshot(&esd.snap);
965 out:
966         free(esd.snap.name);
967         return ret;
968 }
969
970 /* TODO: Also consider number of inodes. */
971 int disk_space_low(void)
972 {
973         struct disk_space ds;
974         int ret = get_disk_space(".", &ds);
975
976         if (ret < 0)
977                 return ret;
978         if (conf.min_free_mb_arg)
979                 if (ds.free_mb < conf.min_free_mb_arg)
980                         return 1;
981         if (conf.min_free_percent_arg)
982                 if (ds.percent_free < conf.min_free_percent_arg)
983                         return 1;
984         return 0;
985 }
986
987 int try_to_free_disk_space(int low_disk_space)
988 {
989         int ret;
990         struct snapshot_list sl;
991
992         get_snapshot_list(&sl);
993         ret = remove_outdated_snapshot(&sl);
994         if (ret) /* error, or we are removing something */
995                 goto out;
996         /* no outdated snapshot */
997         ret = remove_redundant_snapshot(&sl);
998         if (ret)
999                 goto out;
1000         ret = 0;
1001         if (!low_disk_space)
1002                 goto out;
1003         DSS_WARNING_LOG("disk space low and nothing obvious to remove\n");
1004         ret = remove_oldest_snapshot();
1005         if (ret)
1006                 goto out;
1007         DSS_CRIT_LOG("uhuhu: not enough disk space for a single snapshot\n");
1008         ret= -ENOSPC;
1009 out:
1010         free_snapshot_list(&sl);
1011         return ret;
1012 }
1013
1014 int select_loop(void)
1015 {
1016         int ret;
1017         struct timeval tv = {.tv_sec = 0, .tv_usec = 0};
1018
1019         for (;;) {
1020                 fd_set rfds;
1021                 int low_disk_space;
1022                 char **rsync_argv;
1023                 struct timeval now, *tvp = &tv;
1024
1025                 if (rsync_pid)
1026                         tv.tv_sec = 60; /* check every 60 seconds for free disk space */
1027                 else if (rm_pid)
1028                         tvp = NULL; /* sleep until rm process dies */
1029                 FD_ZERO(&rfds);
1030                 FD_SET(signal_pipe, &rfds);
1031                 DSS_DEBUG_LOG("tvp: %p, tv_sec: %lu\n", tvp, (long unsigned) tv.tv_sec);
1032                 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1033                 if (ret < 0)
1034                         return ret;
1035                 if (FD_ISSET(signal_pipe, &rfds))
1036                         handle_signal();
1037                 if (rm_pid)
1038                         continue;
1039                 ret = disk_space_low();
1040                 if (ret < 0)
1041                         break;
1042                 low_disk_space = ret;
1043                 if (low_disk_space)
1044                         stop_rsync_process();
1045                 ret = try_to_free_disk_space(low_disk_space);
1046                 if (ret < 0)
1047                         break;
1048                 if (rm_pid)
1049                         continue;
1050                 restart_rsync_process();
1051                 gettimeofday(&now, NULL);
1052                 if (tv_diff(&next_snapshot_time, &now, &tv) > 0)
1053                         continue;
1054                 switch (snapshot_creation_status) {
1055                 case SCS_READY:
1056                         ret = pre_create_hook();
1057                         if (ret < 0)
1058                                 goto out;
1059                         continue;
1060                 case SCS_PRE_HOOK_RUNNING:
1061                         continue;
1062                 case SCS_PRE_HOOK_SUCCESS:
1063                         create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1064                         ret = create_snapshot(rsync_argv);
1065                         free_rsync_argv(rsync_argv);
1066                         if (ret < 0)
1067                                 goto out;
1068                         continue;
1069                 case SCS_RSYNC_RUNNING:
1070                         continue;
1071                 case SCS_RSYNC_SUCCESS:
1072                         ret = post_create_hook();
1073                         if (ret < 0)
1074                                 goto out;
1075                         continue;
1076                 case SCS_POST_HOOK_RUNNING:
1077                         continue;
1078                 }
1079         }
1080 out:
1081         return ret;
1082 }
1083
1084 int com_run(void)
1085 {
1086         int ret;
1087
1088         if (conf.dry_run_given) {
1089                 DSS_ERROR_LOG("dry_run not supported by this command\n");
1090                 return -E_SYNTAX;
1091         }
1092         ret = install_sighandler(SIGHUP);
1093         if (ret < 0)
1094                 return ret;
1095         compute_next_snapshot_time();
1096         return select_loop();
1097 }
1098
1099 void log_disk_space(struct disk_space *ds)
1100 {
1101         DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
1102                 ds->free_mb, ds->total_mb, ds->percent_free,
1103                 ds->percent_free_inodes);
1104 }
1105
1106 int com_prune(void)
1107 {
1108         int ret;
1109         struct snapshot_list sl;
1110         struct disk_space ds;
1111
1112         ret = get_disk_space(".", &ds);
1113         if (ret < 0)
1114                 return ret;
1115         log_disk_space(&ds);
1116         for (;;) {
1117                 get_snapshot_list(&sl);
1118                 ret = remove_outdated_snapshot(&sl);
1119                 free_snapshot_list(&sl);
1120                 if (ret < 0)
1121                         return ret;
1122                 if (!ret)
1123                         break;
1124                 ret = wait_for_rm_process();
1125                 if (ret < 0)
1126                         goto out;
1127         }
1128         for (;;) {
1129                 get_snapshot_list(&sl);
1130                 ret = remove_redundant_snapshot(&sl);
1131                 free_snapshot_list(&sl);
1132                 if (ret < 0)
1133                         return ret;
1134                 if (!ret)
1135                         break;
1136                 ret = wait_for_rm_process();
1137                 if (ret < 0)
1138                         goto out;
1139         }
1140         return 1;
1141 out:
1142         return ret;
1143 }
1144
1145 int com_create(void)
1146 {
1147         int ret, status;
1148         char **rsync_argv;
1149
1150         if (conf.dry_run_given) {
1151                 int i;
1152                 char *msg = NULL;
1153                 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1154                 for (i = 0; rsync_argv[i]; i++) {
1155                         char *tmp = msg;
1156                         msg = make_message("%s%s%s", tmp? tmp : "",
1157                                 tmp? " " : "", rsync_argv[i]);
1158                         free(tmp);
1159                 }
1160                 free_rsync_argv(rsync_argv);
1161                 dss_msg("%s\n", msg);
1162                 free(msg);
1163                 return 1;
1164         }
1165         ret = pre_create_hook();
1166         if (ret < 0)
1167                 return ret;
1168         if (pre_create_hook_pid) {
1169                 ret = wait_for_process(pre_create_hook_pid, &status);
1170                 if (ret < 0)
1171                         return ret;
1172                 ret = handle_pre_create_hook_exit(status);
1173                 if (ret < 0)
1174                         return ret;
1175         }
1176         create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1177         ret = create_snapshot(rsync_argv);
1178         if (ret < 0)
1179                 goto out;
1180         ret = wait_for_process(rsync_pid, &status);
1181         if (ret < 0)
1182                 goto out;
1183         ret = handle_rsync_exit(status);
1184         if (ret < 0)
1185                 goto out;
1186         post_create_hook();
1187         if (post_create_hook_pid)
1188                 ret = wait_for_process(post_create_hook_pid, &status);
1189 out:
1190         free_rsync_argv(rsync_argv);
1191         return ret;
1192 }
1193
1194 int com_ls(void)
1195 {
1196         int i;
1197         struct snapshot_list sl;
1198         struct snapshot *s;
1199         get_snapshot_list(&sl);
1200         FOR_EACH_SNAPSHOT(s, i, &sl)
1201                 dss_msg("%u\t%s\n", s->interval, s->name);
1202         free_snapshot_list(&sl);
1203         return 1;
1204 }
1205
1206 static void setup_signal_handling(void)
1207 {
1208         int ret;
1209
1210         DSS_INFO_LOG("setting up signal handlers\n");
1211         signal_pipe = signal_init(); /* always successful */
1212         ret = install_sighandler(SIGINT);
1213         if (ret < 0)
1214                 goto err;
1215         ret = install_sighandler(SIGTERM);
1216         if (ret < 0)
1217                 goto err;
1218         ret = install_sighandler(SIGCHLD);
1219         if (ret < 0)
1220                 goto err;
1221         return;
1222 err:
1223         DSS_EMERG_LOG("could not install signal handlers\n");
1224         exit(EXIT_FAILURE);
1225 }
1226
1227 int main(int argc, char **argv)
1228 {
1229         int ret;
1230         struct cmdline_parser_params params = {
1231                 .override = 0,
1232                 .initialize = 1,
1233                 .check_required = 0,
1234                 .check_ambiguity = 0
1235         };
1236
1237         cmdline_parser_ext(argc, argv, &conf, &params); /* aborts on errors */
1238         parse_config_file(0);
1239
1240         if (conf.daemon_given)
1241                 daemon_init();
1242         setup_signal_handling();
1243         ret = call_command_handler();
1244         if (ret < 0)
1245                 DSS_EMERG_LOG("%s\n", dss_strerror(-ret));
1246         exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
1247 }