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