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