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