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