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