d0dab0dc3f688254163d9d138069a48dd77ccd5b
[dss.git] / dss.c
1 /*
2  * Copyright (C) 2008-2011 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <assert.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <signal.h>
14 #include <ctype.h>
15 #include <stdbool.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <inttypes.h>
19 #include <sys/time.h>
20 #include <time.h>
21 #include <sys/wait.h>
22 #include <fnmatch.h>
23 #include <limits.h>
24 #include <fcntl.h>
25 #include <lopsub.h>
26 #include <sys/mman.h>
27
28 #include "gcc-compat.h"
29 #include "log.h"
30 #include "str.h"
31 #include "err.h"
32 #include "file.h"
33 #include "exec.h"
34 #include "daemon.h"
35 #include "sig.h"
36 #include "df.h"
37 #include "tv.h"
38 #include "snap.h"
39 #include "ipc.h"
40 #include "dss.lsg.h"
41
42 #define CMD_PTR(_cname) lls_cmd(LSG_DSS_CMD_ ## _cname, dss_suite)
43 #define OPT_RESULT(_cname, _oname) (lls_opt_result(\
44         LSG_DSS_ ## _cname ## _OPT_ ## _oname, (CMD_PTR(_cname) == CMD_PTR(DSS))? lpr : sublpr))
45 #define OPT_GIVEN(_cname, _oname) (lls_opt_given(OPT_RESULT(_cname, _oname)))
46 #define OPT_STRING_VAL(_cname, _oname) (lls_string_val(0, \
47         OPT_RESULT(_cname, _oname)))
48 #define OPT_UINT32_VAL(_cname, _oname) (lls_uint32_val(0, \
49                 OPT_RESULT(_cname, _oname)))
50
51 struct dss_user_data {int (*handler)(void);};
52 #define EXPORT_CMD_HANDLER(_cmd) const struct dss_user_data \
53         lsg_dss_com_ ## _cmd ## _user_data = { \
54                 .handler = com_ ## _cmd \
55         };
56
57 /*
58  * Command line and active options. We need to keep a copy of the parsed
59  * command line options for the SIGHUP case where we merge the command line
60  * options and the new config file options.
61  */
62 static struct lls_parse_result *cmdline_lpr, *lpr;
63
64 /** Parsed subcommand options. */
65 static struct lls_parse_result *cmdline_sublpr, *sublpr;
66 /** Wether daemon_init() was called. */
67 static bool daemonized;
68 /** Non-NULL if we log to a file. */
69 static FILE *logfile;
70 /** The read end of the signal pipe */
71 static int signal_pipe;
72 /** Process id of current pre-create-hook/rsync/post-create-hook process. */
73 static pid_t create_pid;
74 /** Whether the pre-create-hook/rsync/post-create-hook is currently stopped. */
75 static int create_process_stopped;
76 /** How many times in a row the rsync command failed. */
77 static int num_consecutive_rsync_errors;
78 /** Process id of current pre-remove/rm/post-remove process. */
79 static pid_t remove_pid;
80 /** When the next snapshot is due. */
81 static int64_t next_snapshot_time;
82 /** When to try to remove something. */
83 static struct timeval next_removal_check;
84 /** Creation time of the snapshot currently being created. */
85 static int64_t current_snapshot_creation_time;
86 /** The snapshot currently being removed. */
87 struct snapshot *snapshot_currently_being_removed;
88 /** Needed by the post-create hook. */
89 static char *path_to_last_complete_snapshot;
90 static char *name_of_reference_snapshot;
91 /** \sa \ref snap.h for details. */
92 enum hook_status snapshot_creation_status;
93 /** \sa \ref snap.h for details. */
94 enum hook_status snapshot_removal_status;
95
96
97 DEFINE_DSS_ERRLIST;
98 static const char *hook_status_description[] = {HOOK_STATUS_ARRAY};
99
100 /* may be called with ds == NULL. */
101 static int disk_space_low(struct disk_space *ds)
102 {
103         struct disk_space ds_struct;
104         uint32_t val;
105
106         if (!ds) {
107                 int ret = get_disk_space(".", &ds_struct);
108                 if (ret < 0)
109                         return ret;
110                 ds = &ds_struct;
111         }
112         val = OPT_UINT32_VAL(DSS, MIN_FREE_MB);
113         if (val != 0)
114                 if (ds->free_mb < val)
115                         return 1;
116         val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT);
117         if (val != 0)
118                 if (ds->percent_free < val)
119                         return 1;
120         val = OPT_UINT32_VAL(DSS, MIN_FREE_PERCENT_INODES);
121         if (val != 0)
122                 if (ds->percent_free_inodes < val)
123                         return 1;
124         return 0;
125 }
126
127 static void dump_dss_config(const char *msg)
128 {
129         const char dash[] = "-----------------------------";
130         char *lopsub_dump;
131         int ret;
132         FILE *log = logfile? logfile : stderr;
133         struct disk_space ds;
134         int64_t now = get_current_time();
135
136         if (OPT_UINT32_VAL(DSS, LOGLEVEL) > INFO)
137                 return;
138
139         fprintf(log, "%s <%s config> %s\n", dash, msg, dash);
140         fprintf(log, "\n*** disk space ***\n\n");
141         ret = get_disk_space(".", &ds);
142         if (ret >= 0) {
143                 DSS_INFO_LOG(("disk space low: %s\n", disk_space_low(&ds)?
144                         "yes" : "no"));
145                 log_disk_space(&ds);
146         } else
147                 DSS_ERROR_LOG(("can not get free disk space: %s\n",
148                         dss_strerror(-ret)));
149
150         /* we continue on errors from get_disk_space */
151
152         fprintf(log, "\n*** non-default options ***\n\n");
153         lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(DSS), true);
154         fprintf(log, "%s", lopsub_dump);
155         free(lopsub_dump);
156         fprintf(log, "\n*** non-default options for \"run\" ***\n\n");
157         lopsub_dump = lls_dump_parse_result(lpr, CMD_PTR(RUN), true);
158         fprintf(log, "%s", lopsub_dump);
159         free(lopsub_dump);
160         fprintf(log, "\n*** internal state ***\n\n");
161         fprintf(log,
162                 "pid: %d\n"
163                 "logile: %s\n"
164                 "snapshot_currently_being_removed: %s\n"
165                 "path_to_last_complete_snapshot: %s\n"
166                 "reference_snapshot: %s\n"
167                 "snapshot_creation_status: %s\n"
168                 "snapshot_removal_status: %s\n"
169                 "num_consecutive_rsync_errors: %d\n"
170                 ,
171                 (int) getpid(),
172                 logfile? OPT_STRING_VAL(RUN, LOGFILE) : "stderr",
173                 snapshot_currently_being_removed?
174                         snapshot_currently_being_removed->name : "(none)",
175                 path_to_last_complete_snapshot?
176                         path_to_last_complete_snapshot : "(none)",
177                 name_of_reference_snapshot?
178                         name_of_reference_snapshot : "(none)",
179                 hook_status_description[snapshot_creation_status],
180                 hook_status_description[snapshot_removal_status],
181                 num_consecutive_rsync_errors
182         );
183         if (create_pid != 0)
184                 fprintf(log,
185                         "create_pid: %" PRId32 "\n"
186                         "create process is %sstopped\n"
187                         ,
188                         create_pid,
189                         create_process_stopped? "" : "not "
190                 );
191         if (remove_pid != 0)
192                 fprintf(log, "remove_pid: %" PRId32 "\n", remove_pid);
193         if (next_snapshot_time != 0)
194                 fprintf(log, "next snapshot due in %" PRId64 " seconds\n",
195                         next_snapshot_time - now);
196         if (current_snapshot_creation_time != 0)
197                 fprintf(log, "current_snapshot_creation_time: %"
198                         PRId64 " (%" PRId64 " seconds ago)\n",
199                         current_snapshot_creation_time,
200                         now - current_snapshot_creation_time
201                 );
202         if (next_removal_check.tv_sec != 0) {
203                 fprintf(log, "next removal check: %llu (%llu seconds ago)\n",
204                         (long long unsigned)next_removal_check.tv_sec,
205                         now - (long long unsigned)next_removal_check.tv_sec
206                 );
207
208         }
209         fprintf(log, "%s </%s config> %s\n", dash, msg, dash);
210 }
211
212 static int loglevel = -1;
213 static const char *location_file = NULL;
214 static int         location_line = -1;
215 static const char *location_func = NULL;
216
217 void dss_log_set_params(int ll, const char *file, int line, const char *func)
218 {
219         loglevel = ll;
220         location_file = file;
221         location_line = line;
222         location_func = func;
223 }
224
225 /**
226  * The log function of dss.
227  *
228  * \param ll Loglevel.
229  * \param fml Usual format string.
230  *
231  * All DSS_XXX_LOG() macros use this function.
232  */
233 __printf_1_2 void dss_log(const char* fmt,...)
234 {
235         va_list argp;
236         FILE *outfd;
237         struct tm *tm;
238         time_t t1;
239         char str[255] = "";
240         int lpr_ll = lpr? OPT_UINT32_VAL(DSS, LOGLEVEL) : WARNING;
241
242         if (loglevel < lpr_ll)
243                 return;
244         outfd = logfile? logfile : stderr;
245         time(&t1);
246         tm = localtime(&t1);
247         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
248         fprintf(outfd, "%s ", str);
249         if (lpr_ll <= INFO)
250                 fprintf(outfd, "%i: ", loglevel);
251 #ifdef DSS_NO_FUNC_NAMES
252         fprintf(outfd, "%s:%d: ", location_file, location_line);
253 #else
254         fprintf(outfd, "%s: ", location_func);
255 #endif
256         va_start(argp, fmt);
257         vfprintf(outfd, fmt, argp);
258         va_end(argp);
259 }
260
261 /**
262  * Print a message either to stdout or to the log file.
263  */
264 static __printf_1_2 void dss_msg(const char* fmt,...)
265 {
266         FILE *outfd = logfile? logfile : stdout;
267         va_list argp;
268         va_start(argp, fmt);
269         vfprintf(outfd, fmt, argp);
270         va_end(argp);
271 }
272
273 static char *get_config_file_name(void)
274 {
275         char *home, *config_file;
276
277         if (OPT_GIVEN(DSS, CONFIG_FILE))
278                 return dss_strdup(OPT_STRING_VAL(DSS, CONFIG_FILE));
279         home = get_homedir();
280         config_file = make_message("%s/.dssrc", home);
281         free(home);
282         return config_file;
283 }
284
285 static int send_signal(int sig)
286 {
287         pid_t pid;
288         char *config_file = get_config_file_name();
289         int ret = get_dss_pid(config_file, &pid);
290
291         free(config_file);
292         if (ret < 0)
293                 return ret;
294         if (OPT_GIVEN(DSS, DRY_RUN)) {
295                 dss_msg("%d\n", (int)pid);
296                 return 0;
297         }
298         ret = kill(pid, sig);
299         if (ret < 0)
300                 return -ERRNO_TO_DSS_ERROR(errno);
301         return 1;
302 }
303
304 struct signal_info {
305         const char * const name;
306         int num;
307 };
308
309 /*
310  * The table below was taken 2016 from proc/sig.c of procps-3.2.8. Copyright
311  * 1998-2003 by Albert Cahalan, GPLv2.
312  */
313 static const struct signal_info signal_table[] = {
314         {"ABRT",   SIGABRT},  /* IOT */
315         {"ALRM",   SIGALRM},
316         {"BUS",    SIGBUS},
317         {"CHLD",   SIGCHLD},  /* CLD */
318         {"CONT",   SIGCONT},
319         {"FPE",    SIGFPE},
320         {"HUP",    SIGHUP},
321         {"ILL",    SIGILL},
322         {"INT",    SIGINT},
323         {"KILL",   SIGKILL},
324         {"PIPE",   SIGPIPE},
325 #ifdef SIGPOLL
326         {"POLL",   SIGPOLL},  /* IO */
327 #endif
328         {"PROF",   SIGPROF},
329 #ifdef SIGPWR
330         {"PWR",    SIGPWR},
331 #endif
332         {"QUIT",   SIGQUIT},
333         {"SEGV",   SIGSEGV},
334 #ifdef SIGSTKFLT
335         {"STKFLT", SIGSTKFLT},
336 #endif
337         {"STOP",   SIGSTOP},
338         {"SYS",    SIGSYS},   /* UNUSED */
339         {"TERM",   SIGTERM},
340         {"TRAP",   SIGTRAP},
341         {"TSTP",   SIGTSTP},
342         {"TTIN",   SIGTTIN},
343         {"TTOU",   SIGTTOU},
344         {"URG",    SIGURG},
345         {"USR1",   SIGUSR1},
346         {"USR2",   SIGUSR2},
347         {"VTALRM", SIGVTALRM},
348         {"WINCH",  SIGWINCH},
349         {"XCPU",   SIGXCPU},
350         {"XFSZ",   SIGXFSZ}
351 };
352
353 #define SIGNAL_TABLE_SIZE (sizeof(signal_table) / sizeof(signal_table[0]))
354 #ifndef SIGRTMAX
355 #define SIGRTMAX 64
356 #endif
357
358 static int com_kill(void)
359 {
360         const char *arg = OPT_STRING_VAL(KILL, SIGNAL);
361         int ret, i;
362
363         if (*arg >= '0' && *arg <= '9') {
364                 int64_t val;
365                 ret = dss_atoi64(arg, &val);
366                 if (ret < 0)
367                         return ret;
368                 if (val < 0 || val > SIGRTMAX)
369                         return -ERRNO_TO_DSS_ERROR(EINVAL);
370                 return send_signal(val);
371         }
372         if (strncasecmp(arg, "sig", 3) == 0)
373                 arg += 3;
374         if (strcasecmp(arg, "CLD") == 0)
375                 return send_signal(SIGCHLD);
376         if (strcasecmp(arg, "IOT") == 0)
377                 return send_signal(SIGABRT);
378         for (i = 0; i < SIGNAL_TABLE_SIZE; i++)
379                 if (strcasecmp(arg, signal_table[i].name) == 0)
380                         return send_signal(signal_table[i].num);
381         DSS_ERROR_LOG(("invalid sigspec: %s\n", arg));
382         return -ERRNO_TO_DSS_ERROR(EINVAL);
383 }
384 EXPORT_CMD_HANDLER(kill);
385
386 static void dss_get_snapshot_list(struct snapshot_list *sl)
387 {
388         get_snapshot_list(sl, OPT_UINT32_VAL(DSS, UNIT_INTERVAL),
389                 OPT_UINT32_VAL(DSS, NUM_INTERVALS));
390 }
391
392 static int64_t compute_next_snapshot_time(void)
393 {
394         int64_t x = 0, now = get_current_time(), unit_interval
395                 = 24 * 3600 * OPT_UINT32_VAL(DSS, UNIT_INTERVAL), ret;
396         unsigned wanted = desired_number_of_snapshots(0,
397                 OPT_UINT32_VAL(DSS, NUM_INTERVALS)),
398                 num_complete = 0;
399         int i;
400         struct snapshot *s = NULL;
401         struct snapshot_list sl;
402
403         dss_get_snapshot_list(&sl);
404         FOR_EACH_SNAPSHOT(s, i, &sl) {
405                 if (!(s->flags & SS_COMPLETE))
406                         continue;
407                 num_complete++;
408                 x += s->completion_time - s->creation_time;
409         }
410         assert(x >= 0);
411
412         ret = now;
413         if (num_complete == 0)
414                 goto out;
415         x /= num_complete; /* avg time to create one snapshot */
416         if (unit_interval < x * wanted) /* oops, no sleep at all */
417                 goto out;
418         ret = s->completion_time + unit_interval / wanted - x;
419 out:
420         free_snapshot_list(&sl);
421         return ret;
422 }
423
424 static inline void invalidate_next_snapshot_time(void)
425 {
426         next_snapshot_time = 0;
427 }
428
429 static inline int next_snapshot_time_is_valid(void)
430 {
431         return next_snapshot_time != 0;
432 }
433
434 static int next_snapshot_is_due(void)
435 {
436         int64_t now = get_current_time();
437
438         if (!next_snapshot_time_is_valid())
439                 next_snapshot_time = compute_next_snapshot_time();
440         if (next_snapshot_time <= now) {
441                 DSS_DEBUG_LOG(("next snapshot: now\n"));
442                 return 1;
443         }
444         DSS_DEBUG_LOG(("next snapshot due in %" PRId64 " seconds\n",
445                 next_snapshot_time - now));
446         return 0;
447 }
448
449 static void pre_create_hook(void)
450 {
451         assert(snapshot_creation_status == HS_READY);
452         /* make sure that the next snapshot time will be recomputed */
453         invalidate_next_snapshot_time();
454         DSS_DEBUG_LOG(("executing %s\n", OPT_STRING_VAL(DSS, PRE_CREATE_HOOK)));
455         dss_exec_cmdline_pid(&create_pid, OPT_STRING_VAL(DSS, PRE_CREATE_HOOK));
456         snapshot_creation_status = HS_PRE_RUNNING;
457 }
458
459 static void pre_remove_hook(struct snapshot *s, const char *why)
460 {
461         char *cmd;
462
463         if (!s)
464                 return;
465         DSS_DEBUG_LOG(("%s snapshot %s\n", why, s->name));
466         assert(snapshot_removal_status == HS_READY);
467         assert(remove_pid == 0);
468         assert(!snapshot_currently_being_removed);
469
470         snapshot_currently_being_removed = dss_malloc(sizeof(struct snapshot));
471         *snapshot_currently_being_removed = *s;
472         snapshot_currently_being_removed->name = dss_strdup(s->name);
473
474         cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, PRE_REMOVE_HOOK),
475                 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
476         DSS_DEBUG_LOG(("executing %s\n", cmd));
477         dss_exec_cmdline_pid(&remove_pid, cmd);
478         free(cmd);
479         snapshot_removal_status = HS_PRE_RUNNING;
480 }
481
482 static int exec_rm(void)
483 {
484         struct snapshot *s = snapshot_currently_being_removed;
485         char *new_name = being_deleted_name(s);
486         char *argv[4];
487         int ret;
488
489         argv[0] = "rm";
490         argv[1] = "-rf";
491         argv[2] = new_name;
492         argv[3] = NULL;
493
494         assert(snapshot_removal_status == HS_PRE_SUCCESS);
495         assert(remove_pid == 0);
496
497         DSS_NOTICE_LOG(("removing %s (interval = %i)\n", s->name, s->interval));
498         ret = dss_rename(s->name, new_name);
499         if (ret < 0)
500                 goto out;
501         dss_exec(&remove_pid, argv[0], argv);
502         snapshot_removal_status = HS_RUNNING;
503 out:
504         free(new_name);
505         return ret;
506 }
507
508 static int snapshot_is_being_created(struct snapshot *s)
509 {
510         return s->creation_time == current_snapshot_creation_time;
511 }
512
513 static struct snapshot *find_orphaned_snapshot(struct snapshot_list *sl)
514 {
515         struct snapshot *s;
516         int i;
517
518         DSS_DEBUG_LOG(("looking for orphaned snapshots\n"));
519         FOR_EACH_SNAPSHOT(s, i, sl) {
520                 if (snapshot_is_being_created(s))
521                         continue;
522                 /*
523                  * We know that no rm is currently running, so if s is marked
524                  * as being deleted, a previously started rm must have failed.
525                  */
526                 if (s->flags & SS_BEING_DELETED)
527                         return s;
528
529                 if (s->flags & SS_COMPLETE) /* good snapshot */
530                         continue;
531                 /*
532                  * This snapshot is incomplete and it is not the snapshot
533                  * currently being created. However, we must not remove it if
534                  * rsync is about to be restarted. As only the newest snapshot
535                  * can be restarted, this snapshot is orphaned if it is not the
536                  * newest snapshot or if we are not about to restart rsync.
537                  */
538                 if (get_newest_snapshot(sl) != s)
539                         return s;
540                 if (snapshot_creation_status != HS_NEEDS_RESTART)
541                         return s;
542         }
543         /* no orphaned snapshots */
544         return NULL;
545 }
546
547 static int is_reference_snapshot(struct snapshot *s)
548 {
549         if (!name_of_reference_snapshot)
550                 return 0;
551         return strcmp(s->name, name_of_reference_snapshot)? 0 : 1;
552 }
553
554 /*
555  * return: 0: no redundant snapshots, 1: rm process started, negative: error
556  */
557 static struct snapshot *find_redundant_snapshot(struct snapshot_list *sl)
558 {
559         int i, interval;
560         struct snapshot *s;
561         unsigned missing = 0;
562         uint32_t N = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
563
564         DSS_DEBUG_LOG(("looking for intervals containing too many snapshots\n"));
565         for (interval = N - 1; interval >= 0; interval--) {
566                 unsigned keep = desired_number_of_snapshots(interval, N);
567                 unsigned num = sl->interval_count[interval];
568                 struct snapshot *victim = NULL, *prev = NULL;
569                 int64_t score = LONG_MAX;
570
571                 if (keep >= num)
572                         missing += keep - num;
573                 if (keep + missing >= num)
574                         continue;
575                 /* redundant snapshot in this interval, pick snapshot with lowest score */
576                 FOR_EACH_SNAPSHOT(s, i, sl) {
577                         int64_t this_score;
578
579                         if (snapshot_is_being_created(s))
580                                 continue;
581                         if (is_reference_snapshot(s))
582                                 continue;
583                         if (s->interval > interval) {
584                                 prev = s;
585                                 continue;
586                         }
587                         if (s->interval < interval)
588                                 break;
589                         if (!victim) {
590                                 victim = s;
591                                 prev = s;
592                                 continue;
593                         }
594                         assert(prev);
595                         /* check if s is a better victim */
596                         this_score = s->creation_time - prev->creation_time;
597                         assert(this_score >= 0);
598                         if (this_score < score) {
599                                 score = this_score;
600                                 victim = s;
601                         }
602                         prev = s;
603                 }
604                 assert(victim);
605                 return victim;
606         }
607         return NULL;
608 }
609
610 static struct snapshot *find_outdated_snapshot(struct snapshot_list *sl)
611 {
612         int i;
613         struct snapshot *s;
614
615         DSS_DEBUG_LOG(("looking for snapshots belonging to intervals >= %d\n",
616                 OPT_UINT32_VAL(DSS, NUM_INTERVALS)));
617         FOR_EACH_SNAPSHOT(s, i, sl) {
618                 if (snapshot_is_being_created(s))
619                         continue;
620                 if (is_reference_snapshot(s))
621                         continue;
622                 if (s->interval < OPT_UINT32_VAL(DSS, NUM_INTERVALS))
623                         continue;
624                 return s;
625         }
626         return NULL;
627 }
628
629 static struct snapshot *find_oldest_removable_snapshot(struct snapshot_list *sl)
630 {
631         int i, num_complete;
632         struct snapshot *s, *ref = NULL;
633
634         num_complete = num_complete_snapshots(sl);
635         if (num_complete <= OPT_UINT32_VAL(DSS, MIN_COMPLETE))
636                 return NULL;
637         FOR_EACH_SNAPSHOT(s, i, sl) {
638                 if (snapshot_is_being_created(s))
639                         continue;
640                 if (is_reference_snapshot(s)) { /* avoid this one */
641                         ref = s;
642                         continue;
643                 }
644                 DSS_INFO_LOG(("oldest removable snapshot: %s\n", s->name));
645                 return s;
646         }
647         assert(ref);
648         DSS_WARNING_LOG(("removing reference snapshot %s\n", ref->name));
649         return ref;
650 }
651
652 static int rename_incomplete_snapshot(int64_t start)
653 {
654         char *old_name;
655         int ret;
656         int64_t now;
657
658         /*
659          * We don't want the dss_rename() below to fail with EEXIST because the
660          * last complete snapshot was created (and completed) in the same
661          * second as this one.
662          */
663         while ((now = get_current_time()) == start)
664                 sleep(1);
665         free(path_to_last_complete_snapshot);
666         ret = complete_name(start, now, &path_to_last_complete_snapshot);
667         if (ret < 0)
668                 return ret;
669         old_name = incomplete_name(start);
670         ret = dss_rename(old_name, path_to_last_complete_snapshot);
671         if (ret >= 0)
672                 DSS_NOTICE_LOG(("%s -> %s\n", old_name,
673                         path_to_last_complete_snapshot));
674         free(old_name);
675         return ret;
676 }
677
678 static int try_to_free_disk_space(void)
679 {
680         int ret;
681         struct snapshot_list sl;
682         struct snapshot *victim;
683         struct timeval now;
684         const char *why;
685         int low_disk_space;
686
687         ret = disk_space_low(NULL);
688         if (ret < 0)
689                 return ret;
690         low_disk_space = ret;
691         gettimeofday(&now, NULL);
692         if (tv_diff(&next_removal_check, &now, NULL) > 0)
693                 return 0;
694         if (!low_disk_space) {
695                 if (OPT_GIVEN(DSS, KEEP_REDUNDANT))
696                         return 0;
697                 if (snapshot_creation_status != HS_READY)
698                         return 0;
699                 if (next_snapshot_is_due())
700                         return 0;
701         }
702         /*
703          * Idle and --keep_redundant not given, or low disk space. Look at
704          * existing snapshots.
705          */
706         dss_get_snapshot_list(&sl);
707         ret = 0;
708         /*
709          * Don't remove anything if there is free space and we have fewer
710          * snapshots than configured, plus one. This way there is always one
711          * snapshot that can be recycled.
712          */
713         if (!low_disk_space && sl.num_snapshots <=
714                         1 << OPT_UINT32_VAL(DSS, NUM_INTERVALS))
715                 goto out;
716         why = "outdated";
717         victim = find_outdated_snapshot(&sl);
718         if (victim)
719                 goto remove;
720         why = "redundant";
721         victim = find_redundant_snapshot(&sl);
722         if (victim)
723                 goto remove;
724         why = "orphaned";
725         victim = find_orphaned_snapshot(&sl);
726         if (victim)
727                 goto remove;
728         /* try harder only if disk space is low */
729         if (!low_disk_space)
730                 goto out;
731         DSS_WARNING_LOG(("disk space low and nothing obvious to remove\n"));
732         victim = find_oldest_removable_snapshot(&sl);
733         if (victim)
734                 goto remove;
735         DSS_CRIT_LOG(("uhuhu: disk space low and nothing to remove\n"));
736         ret = -ERRNO_TO_DSS_ERROR(ENOSPC);
737         goto out;
738 remove:
739         pre_remove_hook(victim, why);
740 out:
741         free_snapshot_list(&sl);
742         return ret;
743 }
744
745 static void post_create_hook(void)
746 {
747         char *cmd = make_message("%s %s/%s",
748                 OPT_STRING_VAL(DSS, POST_CREATE_HOOK),
749                 OPT_STRING_VAL(DSS, DEST_DIR), path_to_last_complete_snapshot);
750         DSS_NOTICE_LOG(("executing %s\n", cmd));
751         dss_exec_cmdline_pid(&create_pid, cmd);
752         free(cmd);
753         snapshot_creation_status = HS_POST_RUNNING;
754 }
755
756 static void post_remove_hook(void)
757 {
758         char *cmd;
759         struct snapshot *s = snapshot_currently_being_removed;
760
761         assert(s);
762
763         cmd = make_message("%s %s/%s", OPT_STRING_VAL(DSS, POST_REMOVE_HOOK),
764                 OPT_STRING_VAL(DSS, DEST_DIR), s->name);
765         DSS_NOTICE_LOG(("executing %s\n", cmd));
766         dss_exec_cmdline_pid(&remove_pid, cmd);
767         free(cmd);
768         snapshot_removal_status = HS_POST_RUNNING;
769 }
770
771 static void dss_kill(pid_t pid, int sig, const char *msg)
772 {
773         const char *signame, *process_name;
774
775         if (pid == 0)
776                 return;
777         switch (sig) {
778         case SIGTERM: signame = "TERM"; break;
779         case SIGSTOP: signame = "STOP"; break;
780         case SIGCONT: signame = "CONT"; break;
781         default: signame = "????";
782         }
783
784         if (pid == create_pid)
785                 process_name = "create";
786         else if (pid == remove_pid)
787                 process_name = "remove";
788         else process_name = "??????";
789
790         if (msg)
791                 DSS_INFO_LOG(("%s\n", msg));
792         DSS_DEBUG_LOG(("sending signal %d (%s) to pid %d (%s process)\n",
793                 sig, signame, (int)pid, process_name));
794         if (kill(pid, sig) >= 0)
795                 return;
796         DSS_INFO_LOG(("failed to send signal %d (%s) to pid %d (%s process)\n",
797                 sig, signame, (int)pid, process_name));
798 }
799
800 static void stop_create_process(void)
801 {
802         if (create_process_stopped)
803                 return;
804         dss_kill(create_pid, SIGSTOP, "suspending create process");
805         create_process_stopped = 1;
806 }
807
808 static void restart_create_process(void)
809 {
810         if (!create_process_stopped)
811                 return;
812         dss_kill(create_pid, SIGCONT, "resuming create process");
813         create_process_stopped = 0;
814 }
815
816 /**
817  * Print a log message about the exit status of a child.
818  */
819 static void log_termination_msg(pid_t pid, int status)
820 {
821         if (WIFEXITED(status))
822                 DSS_INFO_LOG(("child %i exited. Exit status: %i\n", (int)pid,
823                         WEXITSTATUS(status)));
824         else if (WIFSIGNALED(status))
825                 DSS_NOTICE_LOG(("child %i was killed by signal %i\n", (int)pid,
826                         WTERMSIG(status)));
827         else
828                 DSS_WARNING_LOG(("child %i terminated abormally\n", (int)pid));
829 }
830
831 static int wait_for_process(pid_t pid, int *status)
832 {
833         int ret;
834
835         DSS_DEBUG_LOG(("Waiting for process %d to terminate\n", (int)pid));
836         for (;;) {
837                 fd_set rfds;
838
839                 FD_ZERO(&rfds);
840                 FD_SET(signal_pipe, &rfds);
841                 ret = dss_select(signal_pipe + 1, &rfds, NULL, NULL);
842                 if (ret < 0)
843                         break;
844                 ret = next_signal();
845                 if (!ret)
846                         continue;
847                 if (ret == SIGCHLD) {
848                         ret = waitpid(pid, status, 0);
849                         if (ret >= 0)
850                                 break;
851                         if (errno != EINTR) { /* error */
852                                 ret = -ERRNO_TO_DSS_ERROR(errno);
853                                 break;
854                         }
855                 }
856                 /* SIGINT or SIGTERM */
857                 dss_kill(pid, SIGTERM, "killing child process");
858         }
859         if (ret < 0)
860                 DSS_ERROR_LOG(("failed to wait for process %d\n", (int)pid));
861         else
862                 log_termination_msg(pid, *status);
863         return ret;
864 }
865
866 static void handle_pre_remove_exit(int status)
867 {
868         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
869                 snapshot_removal_status = HS_READY;
870                 gettimeofday(&next_removal_check, NULL);
871                 next_removal_check.tv_sec += 60;
872                 return;
873         }
874         snapshot_removal_status = HS_PRE_SUCCESS;
875 }
876
877 static int handle_rm_exit(int status)
878 {
879         if (!WIFEXITED(status)) {
880                 snapshot_removal_status = HS_READY;
881                 return -E_INVOLUNTARY_EXIT;
882         }
883         if (WEXITSTATUS(status)) {
884                 snapshot_removal_status = HS_READY;
885                 return -E_BAD_EXIT_CODE;
886         }
887         snapshot_removal_status = HS_SUCCESS;
888         return 1;
889 }
890
891 static void handle_post_remove_exit(void)
892 {
893         snapshot_removal_status = HS_READY;
894 }
895
896 static int handle_remove_exit(int status)
897 {
898         int ret;
899         struct snapshot *s = snapshot_currently_being_removed;
900
901         assert(s);
902         switch (snapshot_removal_status) {
903         case HS_PRE_RUNNING:
904                 handle_pre_remove_exit(status);
905                 ret = 1;
906                 break;
907         case HS_RUNNING:
908                 ret = handle_rm_exit(status);
909                 break;
910         case HS_POST_RUNNING:
911                 handle_post_remove_exit();
912                 ret = 1;
913                 break;
914         default:
915                 ret = -E_BUG;
916         }
917         if (snapshot_removal_status == HS_READY) {
918                 free(s->name);
919                 free(s);
920                 snapshot_currently_being_removed = NULL;
921         }
922         remove_pid = 0;
923         return ret;
924 }
925
926 static int wait_for_remove_process(void)
927 {
928         int status, ret;
929
930         assert(remove_pid);
931         assert(
932                 snapshot_removal_status == HS_PRE_RUNNING ||
933                 snapshot_removal_status == HS_RUNNING ||
934                 snapshot_removal_status == HS_POST_RUNNING
935         );
936         ret = wait_for_process(remove_pid, &status);
937         if (ret < 0)
938                 return ret;
939         return handle_remove_exit(status);
940 }
941
942 static int handle_rsync_exit(int status)
943 {
944         int es, ret;
945
946         if (!WIFEXITED(status)) {
947                 DSS_ERROR_LOG(("rsync process %d died involuntary\n", (int)create_pid));
948                 ret = -E_INVOLUNTARY_EXIT;
949                 snapshot_creation_status = HS_READY;
950                 goto out;
951         }
952         es = WEXITSTATUS(status);
953         /*
954          * Restart rsync on non-fatal errors:
955          * 24: Partial transfer due to vanished source files
956          */
957         if (es != 0 && es != 24) {
958                 DSS_WARNING_LOG(("rsync exit code %d, error count %d\n",
959                         es, ++num_consecutive_rsync_errors));
960                 if (!logfile) { /* called by com_run() */
961                         ret = -E_BAD_EXIT_CODE;
962                         goto out;
963                 }
964                 if (num_consecutive_rsync_errors >
965                                 OPT_UINT32_VAL(RUN, MAX_RSYNC_ERRORS)) {
966                         ret = -E_TOO_MANY_RSYNC_ERRORS;
967                         snapshot_creation_status = HS_READY;
968                         goto out;
969                 }
970                 DSS_WARNING_LOG(("restarting rsync process\n"));
971                 snapshot_creation_status = HS_NEEDS_RESTART;
972                 next_snapshot_time = get_current_time() + 60;
973                 ret = 1;
974                 goto out;
975         }
976         num_consecutive_rsync_errors = 0;
977         ret = rename_incomplete_snapshot(current_snapshot_creation_time);
978         if (ret < 0)
979                 goto out;
980         snapshot_creation_status = HS_SUCCESS;
981         free(name_of_reference_snapshot);
982         name_of_reference_snapshot = NULL;
983 out:
984         create_process_stopped = 0;
985         return ret;
986 }
987
988 static int handle_pre_create_hook_exit(int status)
989 {
990         int es, ret;
991         static int warn_count;
992
993         if (!WIFEXITED(status)) {
994                 snapshot_creation_status = HS_READY;
995                 ret = -E_INVOLUNTARY_EXIT;
996                 goto out;
997         }
998         es = WEXITSTATUS(status);
999         if (es) {
1000                 if (!warn_count--) {
1001                         DSS_NOTICE_LOG(("pre_create_hook %s returned %d\n",
1002                                 OPT_STRING_VAL(DSS, PRE_CREATE_HOOK), es));
1003                         DSS_NOTICE_LOG(("deferring snapshot creation...\n"));
1004                         warn_count = 60; /* warn only once per hour */
1005                 }
1006                 next_snapshot_time = get_current_time() + 60;
1007                 snapshot_creation_status = HS_READY;
1008                 ret = 0;
1009                 goto out;
1010         }
1011         warn_count = 0;
1012         snapshot_creation_status = HS_PRE_SUCCESS;
1013         ret = 1;
1014 out:
1015         return ret;
1016 }
1017
1018 static int handle_sigchld(void)
1019 {
1020         pid_t pid;
1021         int status, ret = reap_child(&pid, &status);
1022
1023         if (ret <= 0)
1024                 return ret;
1025
1026         if (pid == create_pid) {
1027                 switch (snapshot_creation_status) {
1028                 case HS_PRE_RUNNING:
1029                         ret = handle_pre_create_hook_exit(status);
1030                         break;
1031                 case HS_RUNNING:
1032                         ret = handle_rsync_exit(status);
1033                         break;
1034                 case HS_POST_RUNNING:
1035                         snapshot_creation_status = HS_READY;
1036                         ret = 1;
1037                         break;
1038                 default:
1039                         DSS_EMERG_LOG(("BUG: create can't die in status %d\n",
1040                                 snapshot_creation_status));
1041                         return -E_BUG;
1042                 }
1043                 create_pid = 0;
1044                 return ret;
1045         }
1046         if (pid == remove_pid) {
1047                 ret = handle_remove_exit(status);
1048                 if (ret < 0)
1049                         return ret;
1050                 return ret;
1051         }
1052         DSS_EMERG_LOG(("BUG: unknown process %d died\n", (int)pid));
1053         return -E_BUG;
1054 }
1055
1056 static int change_to_dest_dir(void)
1057 {
1058         const char *dd = OPT_STRING_VAL(DSS, DEST_DIR);
1059         DSS_INFO_LOG(("changing cwd to %s\n", dd));
1060         return dss_chdir(dd);
1061 }
1062
1063 static int check_config(const struct lls_command *cmd)
1064 {
1065         int ret;
1066         uint32_t unit_interval = OPT_UINT32_VAL(DSS, UNIT_INTERVAL);
1067         uint32_t num_intervals = OPT_UINT32_VAL(DSS, NUM_INTERVALS);
1068
1069         if (unit_interval == 0) {
1070                 DSS_ERROR_LOG(("bad unit interval: %i\n", unit_interval));
1071                 return -E_INVALID_NUMBER;
1072         }
1073         DSS_DEBUG_LOG(("unit interval: %i day(s)\n", unit_interval));
1074
1075         if (num_intervals == 0 || num_intervals > 30) {
1076                 DSS_ERROR_LOG(("bad number of intervals: %i\n", num_intervals));
1077                 return -E_INVALID_NUMBER;
1078         }
1079         if (cmd == CMD_PTR(RUN) || cmd == CMD_PTR(CREATE))
1080                 if (!OPT_GIVEN(DSS, SOURCE_DIR)) {
1081                         DSS_ERROR_LOG(("--source-dir required\n"));
1082                         return -E_SYNTAX;
1083                 }
1084         if (cmd == CMD_PTR(RUN) || cmd == CMD_PTR(CREATE)
1085                         || cmd == CMD_PTR(LS) || cmd == CMD_PTR(PRUNE)) {
1086                 if (!OPT_GIVEN(DSS, DEST_DIR)) {
1087                         DSS_ERROR_LOG(("--dest-dir required\n"));
1088                         return -E_SYNTAX;
1089                 }
1090                 ret = change_to_dest_dir();
1091                 if (ret < 0)
1092                         return ret;
1093         }
1094         DSS_DEBUG_LOG(("number of intervals: %i\n", num_intervals));
1095         return 1;
1096 }
1097
1098 static int lopsub_error(int lopsub_ret, char **errctx)
1099 {
1100         const char *msg = lls_strerror(-lopsub_ret);
1101         if (*errctx)
1102                 DSS_ERROR_LOG(("%s: %s\n", *errctx, msg));
1103         else
1104                 DSS_ERROR_LOG(("%s\n", msg));
1105         free(*errctx);
1106         *errctx = NULL;
1107         return -E_LOPSUB;
1108 }
1109
1110 static int parse_config_file(bool sighup, const struct lls_command *cmd)
1111 {
1112         int ret, fd = -1;
1113         char *config_file = get_config_file_name();
1114         struct stat statbuf;
1115         void *map;
1116         size_t sz;
1117         int cf_argc;
1118         char **cf_argv, *errctx = NULL;
1119         struct lls_parse_result *cf_lpr, *merged_lpr, *clpr;
1120         const char *subcmd_name;
1121
1122         ret = open(config_file, O_RDONLY);
1123         if (ret < 0) {
1124                 if (errno != ENOENT || OPT_GIVEN(DSS, CONFIG_FILE)) {
1125                         ret = -ERRNO_TO_DSS_ERROR(errno);
1126                         DSS_ERROR_LOG(("config file %s can not be opened\n",
1127                                 config_file));
1128                         goto out;
1129                 }
1130                 /* no config file -- nothing to do */
1131                 ret = 0;
1132                 goto success;
1133         }
1134         fd = ret;
1135         ret = fstat(fd, &statbuf);
1136         if (ret < 0) {
1137                 ret = -ERRNO_TO_DSS_ERROR(errno);
1138                 DSS_ERROR_LOG(("failed to stat config file %s\n", config_file));
1139                 goto close_fd;
1140         }
1141         sz = statbuf.st_size;
1142         if (sz == 0) { /* config file is empty -- nothing to do */
1143                 ret = 0;
1144                 goto success;
1145         }
1146         map = mmap(NULL, sz, PROT_READ, MAP_PRIVATE, fd, 0);
1147         if (map == MAP_FAILED) {
1148                 ret = -ERRNO_TO_DSS_ERROR(errno);
1149                 DSS_ERROR_LOG(("failed to mmap config file %s\n",
1150                         config_file));
1151                 goto close_fd;
1152         }
1153         if (cmd == CMD_PTR(DSS))
1154                 subcmd_name = NULL;
1155         else
1156                 subcmd_name = lls_command_name(cmd);
1157         ret = lls_convert_config(map, sz, subcmd_name, &cf_argv, &errctx);
1158         munmap(map, sz);
1159         if (ret < 0) {
1160                 DSS_ERROR_LOG(("failed to convert config file %s\n",
1161                         config_file));
1162                 ret = lopsub_error(ret, &errctx);
1163                 goto close_fd;
1164         }
1165         cf_argc = ret;
1166         ret = lls_parse(cf_argc, cf_argv, cmd, &cf_lpr, &errctx);
1167         lls_free_argv(cf_argv);
1168         if (ret < 0) {
1169                 ret = lopsub_error(ret, &errctx);
1170                 goto close_fd;
1171         }
1172         clpr = cmd == CMD_PTR(DSS)? cmdline_lpr : cmdline_sublpr;
1173         if (sighup) /* config file overrides command line */
1174                 ret = lls_merge(cf_lpr, clpr, cmd, &merged_lpr, &errctx);
1175         else /* command line options overrride config file options */
1176                 ret = lls_merge(clpr, cf_lpr, cmd, &merged_lpr, &errctx);
1177         lls_free_parse_result(cf_lpr, cmd);
1178         if (ret < 0) {
1179                 ret = lopsub_error(ret, &errctx);
1180                 goto close_fd;
1181         }
1182         ret = 1;
1183 success:
1184         assert(ret >= 0);
1185         DSS_DEBUG_LOG(("loglevel: %d\n", OPT_UINT32_VAL(DSS, LOGLEVEL)));
1186         if (cmd != CMD_PTR(DSS)) {
1187                 if (ret > 0) {
1188                         if (sublpr != cmdline_sublpr)
1189                                 lls_free_parse_result(sublpr, cmd);
1190                         sublpr = merged_lpr;
1191                 } else
1192                         sublpr = cmdline_sublpr;
1193         } else {
1194                 if (ret > 0) {
1195                         if (lpr != cmdline_lpr)
1196                                 lls_free_parse_result(lpr, cmd);
1197                         lpr = merged_lpr;
1198                 } else
1199                         lpr = cmdline_lpr;
1200         }
1201 close_fd:
1202         if (fd >= 0)
1203                 close(fd);
1204 out:
1205         free(config_file);
1206         if (ret < 0)
1207                 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1208         return ret;
1209 }
1210
1211 static int handle_sighup(void)
1212 {
1213         int ret;
1214
1215         DSS_NOTICE_LOG(("SIGHUP, re-reading config\n"));
1216         dump_dss_config("old");
1217         ret = parse_config_file(true /* SIGHUP */, CMD_PTR(DSS));
1218         if (ret < 0)
1219                 return ret;
1220         ret = parse_config_file(true /* SIGHUP */, CMD_PTR(RUN));
1221         if (ret < 0)
1222                 return ret;
1223         ret = check_config(CMD_PTR(RUN));
1224         if (ret < 0)
1225                 return ret;
1226         close_log(logfile);
1227         logfile = NULL;
1228         if (OPT_GIVEN(RUN, DAEMON) || daemonized) {
1229                 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1230                 log_welcome(OPT_UINT32_VAL(DSS, LOGLEVEL));
1231                 daemonized = true;
1232         }
1233         dump_dss_config("reloaded");
1234         invalidate_next_snapshot_time();
1235         return 1;
1236 }
1237
1238 static void kill_children(void)
1239 {
1240         restart_create_process();
1241         dss_kill(create_pid, SIGTERM, NULL);
1242         dss_kill(remove_pid, SIGTERM, NULL);
1243 }
1244
1245 static int handle_signal(void)
1246 {
1247         int sig, ret = next_signal();
1248
1249         if (ret <= 0)
1250                 goto out;
1251         sig = ret;
1252         switch (sig) {
1253         case SIGINT:
1254         case SIGTERM:
1255                 kill_children();
1256                 ret = -E_SIGNAL;
1257                 break;
1258         case SIGHUP:
1259                 ret = handle_sighup();
1260                 break;
1261         case SIGCHLD:
1262                 ret = handle_sigchld();
1263                 break;
1264         }
1265 out:
1266         if (ret < 0)
1267                 DSS_ERROR_LOG(("%s\n", dss_strerror(-ret)));
1268         return ret;
1269 }
1270
1271 /*
1272  * We can not use rsync locally if the local user is different from the remote
1273  * user or if the src dir is not on the local host (or both).
1274  */
1275 static int use_rsync_locally(char *logname)
1276 {
1277         const char *h = OPT_STRING_VAL(DSS, REMOTE_HOST);
1278
1279         if (strcmp(h, "localhost") && strcmp(h, "127.0.0.1"))
1280                 return 0;
1281         if (OPT_GIVEN(DSS, REMOTE_USER) &&
1282                         strcmp(OPT_STRING_VAL(DSS, REMOTE_USER), logname))
1283                 return 0;
1284         return 1;
1285 }
1286
1287 static int rename_resume_snap(int64_t creation_time)
1288 {
1289         struct snapshot_list sl;
1290         struct snapshot *s = NULL;
1291         char *new_name = incomplete_name(creation_time);
1292         int ret;
1293         const char *why;
1294
1295         sl.num_snapshots = 0;
1296
1297         ret = 0;
1298         dss_get_snapshot_list(&sl);
1299         /*
1300          * Snapshot recycling: We first look at the newest snapshot. If this
1301          * snapshot happens to be incomplete, the last rsync process was
1302          * aborted and we reuse this one. Otherwise we look at snapshots which
1303          * could be removed (outdated and redundant snapshots) as candidates
1304          * for recycling. If no outdated/redundant snapshot exists, we check if
1305          * there is an orphaned snapshot, which likely is useless anyway.
1306          *
1307          * Only if no existing snapshot is suitable for recycling, we bite the
1308          * bullet and create a new one.
1309          */
1310         s = get_newest_snapshot(&sl);
1311         if (!s) /* no snapshots at all */
1312                 goto out;
1313         /* re-use last snapshot if it is incomplete */
1314         why = "aborted";
1315         if ((s->flags & SS_COMPLETE) == 0)
1316                 goto out;
1317         why = "outdated";
1318         s = find_outdated_snapshot(&sl);
1319         if (s)
1320                 goto out;
1321         why = "redundant";
1322         s = find_redundant_snapshot(&sl);
1323         if (s)
1324                 goto out;
1325         why = "orphaned";
1326         s = find_orphaned_snapshot(&sl);
1327 out:
1328         if (s) {
1329                 DSS_NOTICE_LOG(("recycling %s snapshot %s\n", why, s->name));
1330                 ret = dss_rename(s->name, new_name);
1331         }
1332         if (ret >= 0)
1333                 DSS_NOTICE_LOG(("creating %s\n", new_name));
1334         free(new_name);
1335         free_snapshot_list(&sl);
1336         return ret;
1337 }
1338
1339 static void create_rsync_argv(char ***argv, int64_t *num)
1340 {
1341         char *logname;
1342         int i = 0, j, N = OPT_GIVEN(DSS, RSYNC_OPTION);
1343         struct snapshot_list sl;
1344
1345         dss_get_snapshot_list(&sl);
1346         assert(!name_of_reference_snapshot);
1347         name_of_reference_snapshot = name_of_newest_complete_snapshot(&sl);
1348         free_snapshot_list(&sl);
1349
1350         *argv = dss_malloc((15 + N) * sizeof(char *));
1351         (*argv)[i++] = dss_strdup("rsync");
1352         (*argv)[i++] = dss_strdup("-a");
1353         (*argv)[i++] = dss_strdup("--delete");
1354         for (j = 0; j < N; j++)
1355                 (*argv)[i++] = dss_strdup(lls_string_val(j,
1356                         OPT_RESULT(DSS, RSYNC_OPTION)));
1357         if (name_of_reference_snapshot) {
1358                 DSS_INFO_LOG(("using %s as reference\n", name_of_reference_snapshot));
1359                 (*argv)[i++] = make_message("--link-dest=../%s",
1360                         name_of_reference_snapshot);
1361         } else
1362                 DSS_INFO_LOG(("no suitable reference snapshot found\n"));
1363         logname = dss_logname();
1364         if (use_rsync_locally(logname))
1365                 (*argv)[i++] = dss_strdup(OPT_STRING_VAL(DSS, SOURCE_DIR));
1366         else
1367                 (*argv)[i++] = make_message("%s@%s:%s/",
1368                         OPT_GIVEN(DSS, REMOTE_USER)?
1369                                 OPT_STRING_VAL(DSS, REMOTE_USER) : logname,
1370                         OPT_STRING_VAL(DSS, REMOTE_HOST),
1371                         OPT_STRING_VAL(DSS, SOURCE_DIR));
1372         free(logname);
1373         *num = get_current_time();
1374         (*argv)[i++] = incomplete_name(*num);
1375         (*argv)[i++] = NULL;
1376         for (j = 0; j < i; j++)
1377                 DSS_DEBUG_LOG(("argv[%d] = %s\n", j, (*argv)[j]));
1378 }
1379
1380 static void free_rsync_argv(char **argv)
1381 {
1382         int i;
1383
1384         if (!argv)
1385                 return;
1386         for (i = 0; argv[i]; i++)
1387                 free(argv[i]);
1388         free(argv);
1389 }
1390
1391 static int create_snapshot(char **argv)
1392 {
1393         int ret;
1394
1395         ret = rename_resume_snap(current_snapshot_creation_time);
1396         if (ret < 0)
1397                 return ret;
1398         dss_exec(&create_pid, argv[0], argv);
1399         snapshot_creation_status = HS_RUNNING;
1400         return ret;
1401 }
1402
1403 static int select_loop(void)
1404 {
1405         int ret;
1406         /* check every 60 seconds for free disk space */
1407         struct timeval tv;
1408         char **rsync_argv = NULL;
1409
1410         for (;;) {
1411                 fd_set rfds;
1412                 struct timeval *tvp;
1413
1414                 if (remove_pid)
1415                         tvp = NULL; /* sleep until rm hook/process dies */
1416                 else { /* sleep one minute */
1417                         tv.tv_sec = 60;
1418                         tv.tv_usec = 0;
1419                         tvp = &tv;
1420                 }
1421                 FD_ZERO(&rfds);
1422                 FD_SET(signal_pipe, &rfds);
1423                 ret = dss_select(signal_pipe + 1, &rfds, NULL, tvp);
1424                 if (ret < 0)
1425                         goto out;
1426                 if (FD_ISSET(signal_pipe, &rfds)) {
1427                         ret = handle_signal();
1428                         if (ret < 0)
1429                                 goto out;
1430                 }
1431                 if (remove_pid)
1432                         continue;
1433                 if (snapshot_removal_status == HS_PRE_SUCCESS) {
1434                         ret = exec_rm();
1435                         if (ret < 0)
1436                                 goto out;
1437                         continue;
1438                 }
1439                 if (snapshot_removal_status == HS_SUCCESS) {
1440                         post_remove_hook();
1441                         continue;
1442                 }
1443                 ret = try_to_free_disk_space();
1444                 if (ret < 0)
1445                         goto out;
1446                 if (snapshot_removal_status != HS_READY) {
1447                         stop_create_process();
1448                         continue;
1449                 }
1450                 restart_create_process();
1451                 switch (snapshot_creation_status) {
1452                 case HS_READY:
1453                         if (!next_snapshot_is_due())
1454                                 continue;
1455                         pre_create_hook();
1456                         continue;
1457                 case HS_PRE_RUNNING:
1458                 case HS_RUNNING:
1459                 case HS_POST_RUNNING:
1460                         continue;
1461                 case HS_PRE_SUCCESS:
1462                         if (!name_of_reference_snapshot) {
1463                                 free_rsync_argv(rsync_argv);
1464                                 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1465                         }
1466                         ret = create_snapshot(rsync_argv);
1467                         if (ret < 0)
1468                                 goto out;
1469                         continue;
1470                 case HS_NEEDS_RESTART:
1471                         if (!next_snapshot_is_due())
1472                                 continue;
1473                         ret = create_snapshot(rsync_argv);
1474                         if (ret < 0)
1475                                 goto out;
1476                         continue;
1477                 case HS_SUCCESS:
1478                         post_create_hook();
1479                         continue;
1480                 }
1481         }
1482 out:
1483         return ret;
1484 }
1485
1486 static void exit_hook(int exit_code)
1487 {
1488         const char *argv[3];
1489         pid_t pid;
1490
1491         argv[0] = OPT_STRING_VAL(DSS, EXIT_HOOK);
1492         argv[1] = dss_strerror(-exit_code);
1493         argv[2] = NULL;
1494
1495         DSS_NOTICE_LOG(("executing %s %s\n", argv[0], argv[1]));
1496         dss_exec(&pid, argv[0], (char **)argv);
1497 }
1498
1499 static void lock_dss_or_die(void)
1500 {
1501         char *config_file = get_config_file_name();
1502         int ret = lock_dss(config_file);
1503
1504         free(config_file);
1505         if (ret < 0) {
1506                 DSS_EMERG_LOG(("failed to lock: %s\n", dss_strerror(-ret)));
1507                 exit(EXIT_FAILURE);
1508         }
1509 }
1510
1511 static int com_run(void)
1512 {
1513         int ret;
1514
1515         if (OPT_GIVEN(DSS, DRY_RUN)) {
1516                 DSS_ERROR_LOG(("dry run not supported by this command\n"));
1517                 return -E_SYNTAX;
1518         }
1519         if (OPT_GIVEN(RUN, DAEMON)) {
1520                 daemon_init();
1521                 daemonized = true;
1522                 logfile = open_log(OPT_STRING_VAL(RUN, LOGFILE));
1523         }
1524         lock_dss_or_die();
1525         dump_dss_config("startup");
1526         ret = install_sighandler(SIGHUP);
1527         if (ret < 0)
1528                 return ret;
1529         ret = select_loop();
1530         if (ret >= 0) /* impossible */
1531                 ret = -E_BUG;
1532         kill_children();
1533         exit_hook(ret);
1534         return ret;
1535 }
1536 EXPORT_CMD_HANDLER(run);
1537
1538 static int com_prune(void)
1539 {
1540         int ret;
1541         struct snapshot_list sl;
1542         struct snapshot *victim;
1543         struct disk_space ds;
1544         const char *why;
1545
1546         lock_dss_or_die();
1547         ret = get_disk_space(".", &ds);
1548         if (ret < 0)
1549                 return ret;
1550         log_disk_space(&ds);
1551         dss_get_snapshot_list(&sl);
1552         why = "outdated";
1553         victim = find_outdated_snapshot(&sl);
1554         if (victim)
1555                 goto rm;
1556         why = "redundant";
1557         victim = find_redundant_snapshot(&sl);
1558         if (victim)
1559                 goto rm;
1560         ret = 0;
1561         goto out;
1562 rm:
1563         if (OPT_GIVEN(DSS, DRY_RUN)) {
1564                 dss_msg("%s snapshot %s (interval = %i)\n",
1565                         why, victim->name, victim->interval);
1566                 ret = 0;
1567                 goto out;
1568         }
1569         pre_remove_hook(victim, why);
1570         if (snapshot_removal_status == HS_PRE_RUNNING) {
1571                 ret = wait_for_remove_process();
1572                 if (ret < 0)
1573                         goto out;
1574                 if (snapshot_removal_status != HS_PRE_SUCCESS)
1575                         goto out;
1576         }
1577         ret = exec_rm();
1578         if (ret < 0)
1579                 goto out;
1580         ret = wait_for_remove_process();
1581         if (ret < 0)
1582                 goto out;
1583         if (snapshot_removal_status != HS_SUCCESS)
1584                 goto out;
1585         post_remove_hook();
1586         if (snapshot_removal_status != HS_POST_RUNNING)
1587                 goto out;
1588         ret = wait_for_remove_process();
1589         if (ret < 0)
1590                 goto out;
1591         ret = 1;
1592 out:
1593         free_snapshot_list(&sl);
1594         return ret;
1595 }
1596 EXPORT_CMD_HANDLER(prune);
1597
1598 static int com_create(void)
1599 {
1600         int ret, status;
1601         char **rsync_argv;
1602
1603         lock_dss_or_die();
1604         if (OPT_GIVEN(DSS, DRY_RUN)) {
1605                 int i;
1606                 char *msg = NULL;
1607                 create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1608                 for (i = 0; rsync_argv[i]; i++) {
1609                         char *tmp = msg;
1610                         msg = make_message("%s%s%s", tmp? tmp : "",
1611                                 tmp? " " : "", rsync_argv[i]);
1612                         free(tmp);
1613                 }
1614                 free_rsync_argv(rsync_argv);
1615                 dss_msg("%s\n", msg);
1616                 free(msg);
1617                 return 1;
1618         }
1619         pre_create_hook();
1620         if (create_pid) {
1621                 ret = wait_for_process(create_pid, &status);
1622                 if (ret < 0)
1623                         return ret;
1624                 ret = handle_pre_create_hook_exit(status);
1625                 if (ret <= 0) /* error, or pre-create failed */
1626                         return ret;
1627         }
1628         create_rsync_argv(&rsync_argv, &current_snapshot_creation_time);
1629         ret = create_snapshot(rsync_argv);
1630         if (ret < 0)
1631                 goto out;
1632         ret = wait_for_process(create_pid, &status);
1633         if (ret < 0)
1634                 goto out;
1635         ret = handle_rsync_exit(status);
1636         if (ret < 0)
1637                 goto out;
1638         post_create_hook();
1639         if (create_pid)
1640                 ret = wait_for_process(create_pid, &status);
1641 out:
1642         free_rsync_argv(rsync_argv);
1643         return ret;
1644 }
1645 EXPORT_CMD_HANDLER(create);
1646
1647 static int com_ls(void)
1648 {
1649         int i;
1650         struct snapshot_list sl;
1651         struct snapshot *s;
1652
1653         dss_get_snapshot_list(&sl);
1654         FOR_EACH_SNAPSHOT(s, i, &sl) {
1655                 int64_t d = 0;
1656                 if (s->flags & SS_COMPLETE)
1657                         d = (s->completion_time - s->creation_time) / 60;
1658                 dss_msg("%u\t%s\t%3" PRId64 ":%02" PRId64 "\n", s->interval, s->name, d/60, d%60);
1659         }
1660         free_snapshot_list(&sl);
1661         return 1;
1662 }
1663 EXPORT_CMD_HANDLER(ls);
1664
1665 static int setup_signal_handling(void)
1666 {
1667         int ret;
1668
1669         DSS_INFO_LOG(("setting up signal handlers\n"));
1670         signal_pipe = signal_init(); /* always successful */
1671         ret = install_sighandler(SIGINT);
1672         if (ret < 0)
1673                 return ret;
1674         ret = install_sighandler(SIGTERM);
1675         if (ret < 0)
1676                 return ret;
1677         return install_sighandler(SIGCHLD);
1678 }
1679
1680 static void handle_version_and_help(void)
1681 {
1682         char *txt;
1683
1684         if (OPT_GIVEN(DSS, DETAILED_HELP))
1685                 txt = lls_long_help(CMD_PTR(DSS));
1686         else if (OPT_GIVEN(DSS, HELP))
1687                 txt = lls_short_help(CMD_PTR(DSS));
1688         else if (OPT_GIVEN(DSS, VERSION))
1689                 txt = dss_strdup(VERSION_STRING);
1690         else
1691                 return;
1692         printf("%s", txt);
1693         free(txt);
1694         exit(EXIT_SUCCESS);
1695 }
1696
1697 static void show_subcommand_summary(void)
1698 {
1699         const struct lls_command *cmd;
1700         int i;
1701
1702         printf("Available subcommands:\n");
1703         for (i = 1; (cmd = lls_cmd(i, dss_suite)); i++) {
1704                 const char *name = lls_command_name(cmd);
1705                 const char *purpose = lls_purpose(cmd);
1706                 printf("%-10s%s\n", name, purpose);
1707         }
1708         exit(EXIT_SUCCESS);
1709 }
1710
1711 int main(int argc, char **argv)
1712 {
1713         int ret;
1714         const struct lls_command *cmd = CMD_PTR(DSS);
1715         char *errctx = NULL;
1716         unsigned num_inputs;
1717         const struct dss_user_data *ud = NULL;
1718
1719         ret = lls_parse(argc, argv, cmd, &cmdline_lpr, &errctx);
1720         if (ret < 0) {
1721                 ret = lopsub_error(ret, &errctx);
1722                 goto out;
1723         }
1724         lpr = cmdline_lpr;
1725         ret = parse_config_file(false /* no SIGHUP */, cmd);
1726         if (ret < 0)
1727                 goto out;
1728         handle_version_and_help();
1729         num_inputs = lls_num_inputs(lpr);
1730         if (num_inputs == 0)
1731                 show_subcommand_summary();
1732         ret = lls_lookup_subcmd(argv[argc - num_inputs], dss_suite, &errctx);
1733         if (ret < 0) {
1734                 ret = lopsub_error(ret, &errctx);
1735                 goto out;
1736         }
1737         cmd = lls_cmd(ret, dss_suite);
1738         ret = lls_parse(num_inputs, argv + argc - num_inputs, cmd,
1739                 &cmdline_sublpr, &errctx);
1740         if (ret < 0) {
1741                 ret = lopsub_error(ret, &errctx);
1742                 goto out;
1743         }
1744         sublpr = cmdline_sublpr;
1745         ret = parse_config_file(false /* no SIGHUP */, cmd);
1746         if (ret < 0)
1747                 goto out;
1748         ret = check_config(cmd);
1749         if (ret < 0)
1750                 goto out;
1751         ret = setup_signal_handling();
1752         if (ret < 0)
1753                 goto out;
1754         ud = lls_user_data(cmd);
1755         ret = ud->handler();
1756         signal_shutdown();
1757 out:
1758         if (ret < 0) {
1759                 if (errctx)
1760                         DSS_ERROR_LOG(("%s\n", errctx));
1761                 DSS_EMERG_LOG(("%s\n", dss_strerror(-ret)));
1762         }
1763         free(errctx);
1764         lls_free_parse_result(lpr, CMD_PTR(DSS));
1765         if (lpr != cmdline_lpr)
1766                 lls_free_parse_result(cmdline_lpr, CMD_PTR(DSS));
1767         lls_free_parse_result(sublpr, cmd);
1768         if (sublpr != cmdline_sublpr)
1769                 lls_free_parse_result(cmdline_sublpr, cmd);
1770         exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
1771 }