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