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