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