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