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