]> git.tuebingen.mpg.de Git - dss.git/blob - dss.c
Implement signal handling.
[dss.git] / dss.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <signal.h>
8 #include <ctype.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12 #include <time.h>
13 #include <sys/wait.h>
14 #include <fnmatch.h>
15 #include <limits.h>
16
17
18 #include "gcc-compat.h"
19 #include "cmdline.h"
20 #include "log.h"
21 #include "string.h"
22 #include "error.h"
23 #include "fd.h"
24 #include "exec.h"
25 #include "daemon.h"
26 #include "signal.h"
27
28
29 struct gengetopt_args_info conf;
30 char *dss_error_txt = NULL;
31 static FILE *logfile;
32 int signal_pipe;
33
34 DEFINE_DSS_ERRLIST;
35
36
37 /* a litte cpp magic helps to DRY */
38 #define COMMANDS \
39         COMMAND(ls) \
40         COMMAND(create) \
41         COMMAND(prune) \
42         COMMAND(run)
43 #define COMMAND(x) int com_ ##x(void);
44 COMMANDS
45 #undef COMMAND
46 #define COMMAND(x) if (conf.x ##_given) return com_ ##x();
47 int call_command_handler(void)
48 {
49         COMMANDS
50         DSS_EMERG_LOG("BUG: did not find command handler\n");
51         exit(EXIT_FAILURE);
52 }
53 #undef COMMAND
54 #undef COMMANDS
55
56 /*
57  * complete, not being deleted: 1204565370-1204565371.Sun_Mar_02_2008_14_33-Sun_Mar_02_2008_14_43
58  * complete, being deleted: 1204565370-1204565371.being_deleted
59  * incomplete, not being deleted: 1204565370-incomplete
60  * incomplete, being deleted: 1204565370-incomplete.being_deleted
61  */
62 enum snapshot_status_flags {
63         SS_COMPLETE = 1,
64         SS_BEING_DELETED = 2,
65 };
66
67 struct snapshot {
68         char *name;
69         int64_t creation_time;
70         int64_t completion_time;
71         enum snapshot_status_flags flags;
72         unsigned interval;
73 };
74
75 __printf_2_3 void dss_log(int ll, const char* fmt,...)
76 {
77         va_list argp;
78         FILE *outfd;
79         struct tm *tm;
80         time_t t1;
81         char str[255] = "";
82
83         if (ll < conf.loglevel_arg)
84                 return;
85         outfd = logfile? logfile : stderr;
86         time(&t1);
87         tm = localtime(&t1);
88         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
89         fprintf(outfd, "%s ", str);
90         if (conf.loglevel_arg <= INFO)
91                 fprintf(outfd, "%i: ", ll);
92         va_start(argp, fmt);
93         vfprintf(outfd, fmt, argp);
94         va_end(argp);
95 }
96
97 /**
98  * Print a message either to stdout or to the log file.
99  */
100 __printf_1_2 void dss_msg(const char* fmt,...)
101 {
102         FILE *outfd = conf.daemon_given? logfile : stdout;
103         va_list argp;
104         va_start(argp, fmt);
105         vfprintf(outfd, fmt, argp);
106         va_end(argp);
107 }
108
109 int is_snapshot(const char *dirname, int64_t now, struct snapshot *s)
110 {
111         int i, ret;
112         char *dash, *dot, *tmp;
113         int64_t num;
114
115         assert(dirname);
116         dash = strchr(dirname, '-');
117         if (!dash || !dash[1] || dash == dirname)
118                 return 0;
119         for (i = 0; dirname[i] != '-'; i++)
120                 if (!isdigit(dirname[i]))
121                         return 0;
122         tmp = dss_strdup(dirname);
123         tmp[i] = '\0';
124         ret = dss_atoi64(tmp, &num);
125         free(tmp);
126         if (ret < 0) {
127                 free(dss_error_txt);
128                 return 0;
129         }
130         assert(num >= 0);
131         if (num > now)
132                 return 0;
133         s->creation_time = num;
134         //DSS_DEBUG_LOG("%s start time: %lli\n", dirname, (long long)s->creation_time);
135         s->interval = (long long) ((now - s->creation_time)
136                 / conf.unit_interval_arg / 24 / 3600);
137         if (!strcmp(dash + 1, "incomplete")) {
138                 s->completion_time = -1;
139                 s->flags = 0; /* neither complete, nor being deleted */
140                 goto success;
141         }
142         if (!strcmp(dash + 1, "incomplete.being_deleted")) {
143                 s->completion_time = -1;
144                 s->flags = SS_BEING_DELETED; /* mot cpmplete, being deleted */
145                 goto success;
146         }
147         tmp = dash + 1;
148         dot = strchr(tmp, '.');
149         if (!dot || !dot[1] || dot == tmp)
150                 return 0;
151         for (i = 0; tmp[i] != '.'; i++)
152                 if (!isdigit(tmp[i]))
153                         return 0;
154         tmp = dss_strdup(dash + 1);
155         tmp[i] = '\0';
156         ret = dss_atoi64(tmp, &num);
157         free(tmp);
158         if (ret < 0) {
159                 free(dss_error_txt);
160                 return 0;
161         }
162         if (num > now)
163                 return 0;
164         s->completion_time = num;
165         s->flags = SS_COMPLETE;
166         if (strcmp(dot + 1, "being_deleted"))
167                 s->flags |= SS_BEING_DELETED;
168 success:
169         s->name = dss_strdup(dirname);
170         return 1;
171 }
172
173 int64_t get_current_time(void)
174 {
175         time_t now;
176         time(&now);
177         DSS_DEBUG_LOG("now: %lli\n", (long long) now);
178         return (int64_t)now;
179 }
180
181 char *incomplete_name(int64_t start)
182 {
183         return make_message("%lli-incomplete", (long long)start);
184 }
185
186 char *being_deleted_name(struct snapshot *s)
187 {
188         if (s->flags & SS_COMPLETE)
189                 return make_message("%lli-%lli.being_deleted",
190                         (long long)s->creation_time,
191                         (long long)s->completion_time);
192         return make_message("%lli-incomplete.being_deleted",
193                 (long long)s->creation_time);
194 }
195
196 int complete_name(int64_t start, int64_t end, char **result)
197 {
198         struct tm start_tm, end_tm;
199         time_t *start_seconds = (time_t *) (uint64_t *)&start; /* STFU, gcc */
200         time_t *end_seconds = (time_t *) (uint64_t *)&end; /* STFU, gcc */
201         char start_str[200], end_str[200];
202
203         if (!localtime_r(start_seconds, &start_tm)) {
204                 make_err_msg("%lli", (long long)start);
205                 return -E_LOCALTIME;
206         }
207         if (!localtime_r(end_seconds, &end_tm)) {
208                 make_err_msg("%lli", (long long)end);
209                 return -E_LOCALTIME;
210         }
211         if (!strftime(start_str, sizeof(start_str), "%a_%b_%d_%Y_%H_%M_%S", &start_tm)) {
212                 make_err_msg("%lli", (long long)start);
213                 return -E_STRFTIME;
214         }
215         if (!strftime(end_str, sizeof(end_str), "%a_%b_%d_%Y_%H_%M_%S", &end_tm)) {
216                 make_err_msg("%lli", (long long)end);
217                 return -E_STRFTIME;
218         }
219         *result = make_message("%lli-%lli.%s-%s", (long long) start, (long long) end,
220                 start_str, end_str);
221         return 1;
222 }
223
224 struct snapshot_list {
225         int64_t now;
226         unsigned num_snapshots;
227         unsigned array_size;
228         struct snapshot **snapshots;
229         /**
230          * Array of size num_intervals + 1
231          *
232          * It contains the number of snapshots in each interval. interval_count[num_intervals]
233          * is the number of snapshots which belong to any interval greater than num_intervals.
234          */
235         unsigned *interval_count;
236 };
237
238 #define FOR_EACH_SNAPSHOT(s, i, sl) \
239         for ((i) = 0; (i) < (sl)->num_snapshots && ((s) = (sl)->snapshots[(i)]); (i)++)
240
241
242
243 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
244
245 static int compare_snapshots(const void *a, const void *b)
246 {
247         struct snapshot *s1 = *(struct snapshot **)a;
248         struct snapshot *s2 = *(struct snapshot **)b;
249         return NUM_COMPARE(s2->creation_time, s1->creation_time);
250 }
251
252 /** Compute the minimum of \a a and \a b. */
253 #define DSS_MIN(a,b) ((a) < (b) ? (a) : (b))
254
255 int add_snapshot(const char *dirname, void *private)
256 {
257         struct snapshot_list *sl = private;
258         struct snapshot s;
259         int ret = is_snapshot(dirname, sl->now, &s);
260
261         if (!ret)
262                 return 1;
263         if (sl->num_snapshots >= sl->array_size) {
264                 sl->array_size = 2 * sl->array_size + 1;
265                 sl->snapshots = dss_realloc(sl->snapshots,
266                         sl->array_size * sizeof(struct snapshot *));
267         }
268         sl->snapshots[sl->num_snapshots] = dss_malloc(sizeof(struct snapshot));
269         *(sl->snapshots[sl->num_snapshots]) = s;
270         sl->interval_count[DSS_MIN(s.interval, conf.num_intervals_arg)]++;
271         sl->num_snapshots++;
272         return 1;
273 }
274
275 void get_snapshot_list(struct snapshot_list *sl)
276 {
277         sl->now = get_current_time();
278         sl->num_snapshots = 0;
279         sl->array_size = 0;
280         sl->snapshots = NULL;
281         sl->interval_count = dss_calloc((conf.num_intervals_arg + 1) * sizeof(unsigned));
282         for_each_subdir(add_snapshot, sl);
283         qsort(sl->snapshots, sl->num_snapshots, sizeof(struct snapshot *),
284                 compare_snapshots);
285 }
286
287 void free_snapshot_list(struct snapshot_list *sl)
288 {
289         int i;
290         struct snapshot *s;
291
292         FOR_EACH_SNAPSHOT(s, i, sl) {
293                 free(s->name);
294                 free(s);
295         }
296         free(sl->interval_count);
297         free(sl->snapshots);
298 }
299
300 /**
301  * Print a log message about the exit status of a child.
302  */
303 void log_termination_msg(pid_t pid, int status)
304 {
305         if (WIFEXITED(status))
306                 DSS_INFO_LOG("child %i exited. Exit status: %i\n", (int)pid,
307                         WEXITSTATUS(status));
308         else if (WIFSIGNALED(status))
309                 DSS_NOTICE_LOG("child %i was killed by signal %i\n", (int)pid,
310                         WTERMSIG(status));
311         else
312                 DSS_WARNING_LOG("child %i terminated abormally\n", (int)pid);
313 }
314
315 int wait_for_process(pid_t pid, int *status)
316 {
317         int ret;
318
319         DSS_DEBUG_LOG("Waiting for process %d to terminate\n", (int)pid);
320         for (;;) {
321                 pause();
322                 ret = next_signal();
323                 if  (ret < 0)
324                         break;
325                 if (!ret)
326                         continue;
327                 if (ret == SIGCHLD) {
328                         ret = waitpid(pid, status, 0);
329                         if (ret >= 0)
330                                 break;
331                         if (errno != EINTR) /* error */
332                                 break;
333                 }
334                 DSS_WARNING_LOG("sending SIGTERM to pid %d\n", (int)pid);
335                 kill(pid, SIGTERM);
336         }
337         if (ret < 0) {
338                 ret = -ERRNO_TO_DSS_ERROR(errno);
339                 make_err_msg("failed to wait for process %d", (int)pid);
340         } else
341                 log_termination_msg(pid, *status);
342         return ret;
343 }
344
345 int remove_snapshot(struct snapshot *s, pid_t *pid)
346 {
347         int fds[3] = {0, 0, 0};
348         char *new_name = being_deleted_name(s);
349         int ret = dss_rename(s->name, new_name);
350         char *argv[] = {"rm", "-rf", new_name, NULL};
351
352         if (ret < 0)
353                 goto out;
354         DSS_NOTICE_LOG("removing %s (interval = %i)\n", s->name, s->interval);
355         ret = dss_exec(pid, argv[0], argv, fds);
356 out:
357         free(new_name);
358         return ret;
359 }
360
361 int remove_redundant_snapshot(struct snapshot_list *sl, pid_t *pid)
362 {
363         int ret, i, interval;
364         struct snapshot *s;
365         unsigned missing = 0;
366
367         DSS_INFO_LOG("looking for intervals containing too many snapshots\n");
368         for (interval = conf.num_intervals_arg - 1; interval >= 0; interval--) {
369                 unsigned keep = 1<<(conf.num_intervals_arg - interval - 1);
370                 unsigned num = sl->interval_count[interval];
371                 struct snapshot *victim = NULL, *prev = NULL;
372                 int64_t score = LONG_MAX;
373
374                 if (keep >= num)
375                         missing += keep - num;
376                 DSS_DEBUG_LOG("interval %i: keep: %u, have: %u, missing: %u\n",
377                         interval, keep, num, missing);
378                 if (keep + missing >= num)
379                         continue;
380                 /* redundant snapshot in this interval, pick snapshot with lowest score */
381                 FOR_EACH_SNAPSHOT(s, i, sl) {
382                         int64_t this_score;
383
384                         DSS_DEBUG_LOG("checking %s\n", s->name);
385                         if (s->interval > interval) {
386                                 prev = s;
387                                 continue;
388                         }
389                         if (s->interval < interval)
390                                 break;
391                         if (!victim) {
392                                 victim = s;
393                                 prev = s;
394                                 continue;
395                         }
396                         assert(prev);
397                         /* check if s is a better victim */
398                         this_score = s->creation_time - prev->creation_time;
399                         assert(this_score >= 0);
400                         DSS_DEBUG_LOG("%s: score %lli\n", s->name, (long long)score);
401                         if (this_score < score) {
402                                 score = this_score;
403                                 victim = s;
404                         }
405                         prev = s;
406                 }
407                 assert(victim);
408                 if (conf.dry_run_given) {
409                         dss_msg("%s would be removed (interval = %i)\n",
410                                 victim->name, victim->interval);
411                         continue;
412                 }
413                 ret = remove_snapshot(victim, pid);
414                 return ret < 0? ret : 1;
415         }
416         return 0;
417 }
418
419 int remove_old_snapshot(struct snapshot_list *sl, pid_t *pid)
420 {
421         int i, ret;
422         struct snapshot *s;
423
424         DSS_INFO_LOG("looking for snapshots belonging to intervals greater than %d\n",
425                 conf.num_intervals_arg);
426         FOR_EACH_SNAPSHOT(s, i, sl) {
427                 if (s->interval <= conf.num_intervals_arg)
428                         continue;
429                 if (conf.dry_run_given) {
430                         dss_msg("%s would be removed (interval = %i)\n",
431                                 s->name, s->interval);
432                         continue;
433                 }
434                 ret = remove_snapshot(s, pid);
435                 if (ret < 0)
436                         return ret;
437                 return 1;
438         }
439         return 0;
440 }
441
442 int wait_for_rm_process(pid_t pid)
443 {
444         int status, es, ret = wait_for_process(pid, &status);
445         if (ret < 0)
446                 return ret;
447         if (!WIFEXITED(status)) {
448                 ret = E_INVOLUNTARY_EXIT;
449                 make_err_msg("rm process %d died involuntary", (int)pid);
450                 return ret;
451         }
452         es = WEXITSTATUS(status);
453         if (es) {
454                 ret = -E_BAD_EXIT_CODE;
455                 make_err_msg("rm process %d returned %d", (int)pid, es);
456                 return ret;
457         }
458         return 1;
459 }
460
461 int com_run(void)
462 {
463         int ret;
464
465         if (conf.dry_run_given) {
466                 make_err_msg("dry_run not supported by this command");
467                 return -E_SYNTAX;
468         }
469         ret = install_sighandler(SIGHUP);
470         if (ret < 0)
471                 return ret;
472         return 42;
473 }
474
475 int com_prune(void)
476 {
477         int ret;
478         struct snapshot_list sl;
479         pid_t pid;
480
481         for (;;) {
482                 get_snapshot_list(&sl);
483                 ret = remove_old_snapshot(&sl, &pid);
484                 free_snapshot_list(&sl);
485                 if (ret < 0)
486                         return ret;
487                 if (!ret)
488                         break;
489                 ret = wait_for_rm_process(pid);
490                 if (ret < 0)
491                         goto out;
492         }
493         for (;;) {
494                 get_snapshot_list(&sl);
495                 ret = remove_redundant_snapshot(&sl, &pid);
496                 free_snapshot_list(&sl);
497                 if (ret < 0)
498                         return ret;
499                 if (!ret)
500                         break;
501                 ret = wait_for_rm_process(pid);
502                 if (ret < 0)
503                         goto out;
504         }
505         return 1;
506 out:
507         return ret;
508 }
509
510 struct newest_snapshot_data {
511         char * newest_name;
512         int64_t newest_creation_time;
513         int64_t now;
514 };
515
516 int get_newest_complete(const char *dirname, void *private)
517 {
518         struct newest_snapshot_data *nsd = private;
519         struct snapshot s;
520         int ret = is_snapshot(dirname, nsd->now, &s);
521
522         if (ret <= 0)
523                 return 1;
524         if (s.creation_time < nsd->newest_creation_time)
525                 return 1;
526         nsd->newest_creation_time = s.creation_time;
527         free(nsd->newest_name);
528         nsd->newest_name = s.name;
529         return 1;
530 }
531
532 __malloc char *name_of_newest_complete_snapshot(void)
533 {
534         struct newest_snapshot_data nsd = {
535                 .now = get_current_time(),
536                 .newest_creation_time = -1
537         };
538         for_each_subdir(get_newest_complete, &nsd);
539         return nsd.newest_name;
540 }
541
542 void create_rsync_argv(char ***argv, int64_t *num)
543 {
544         char *logname, *newest = name_of_newest_complete_snapshot();
545         int i = 0, j;
546
547         *argv = dss_malloc((15 + conf.rsync_option_given) * sizeof(char *));
548         (*argv)[i++] = dss_strdup("rsync");
549         (*argv)[i++] = dss_strdup("-aq");
550         (*argv)[i++] = dss_strdup("--delete");
551         for (j = 0; j < conf.rsync_option_given; j++)
552                 (*argv)[i++] = dss_strdup(conf.rsync_option_arg[j]);
553         if (newest) {
554                 DSS_INFO_LOG("using %s as reference snapshot\n", newest);
555                 (*argv)[i++] = make_message("--link-dest=../%s", newest);
556                 free(newest);
557         } else
558                 DSS_INFO_LOG("no previous snapshot found");
559         if (conf.exclude_patterns_given) {
560                 (*argv)[i++] = dss_strdup("--exclude-from");
561                 (*argv)[i++] = dss_strdup(conf.exclude_patterns_arg);
562
563         }
564         logname = dss_logname();
565         if (conf.remote_user_given && !strcmp(conf.remote_user_arg, logname))
566                 (*argv)[i++] = dss_strdup(conf.source_dir_arg);
567         else
568                 (*argv)[i++] = make_message("%s@%s:%s/", conf.remote_user_given?
569                         conf.remote_user_arg : logname,
570                         conf.remote_host_arg, conf.source_dir_arg);
571         free(logname);
572         *num = get_current_time();
573         (*argv)[i++] = incomplete_name(*num);
574         (*argv)[i++] = NULL;
575         for (j = 0; j < i; j++)
576                 DSS_DEBUG_LOG("argv[%d] = %s\n", j, (*argv)[j]);
577 }
578
579 void free_rsync_argv(char **argv)
580 {
581         int i;
582         for (i = 0; argv[i]; i++)
583                 free(argv[i]);
584         free(argv);
585 }
586
587 int create_snapshot(char **argv, pid_t *pid)
588 {
589         int fds[3] = {0, 0, 0};
590
591         return dss_exec(pid, argv[0], argv, fds);
592 }
593
594 int rename_incomplete_snapshot(int64_t start)
595 {
596         char *old_name, *new_name;
597         int ret;
598
599         ret = complete_name(start, get_current_time(), &new_name);
600         if (ret < 0)
601                 return ret;
602         old_name = incomplete_name(start);
603         ret = dss_rename(old_name, new_name);
604         if (ret >= 0)
605                 DSS_NOTICE_LOG("%s -> %s\n", old_name, new_name);
606         free(old_name);
607         free(new_name);
608         return ret;
609 }
610
611 int com_create(void)
612 {
613         int ret, status, es;
614         char **rsync_argv;
615         int64_t snapshot_num;
616         pid_t pid;
617
618         create_rsync_argv(&rsync_argv, &snapshot_num);
619         if (conf.dry_run_given) {
620                 int i;
621                 char *msg = NULL;
622                 for (i = 0; rsync_argv[i]; i++) {
623                         char *tmp = msg;
624                         msg = make_message("%s%s%s", tmp? tmp : "",
625                                 tmp? " " : "", rsync_argv[i]);
626                         free(tmp);
627                 }
628                 dss_msg("%s\n", msg);
629                 free(msg);
630                 return 1;
631         }
632         DSS_NOTICE_LOG("creating snapshot %lli\n", (long long)snapshot_num);
633         ret = create_snapshot(rsync_argv, &pid);
634         if (ret < 0)
635                 goto out;
636         ret = wait_for_process(pid, &status);
637         if (ret < 0)
638                 goto out;
639         if (!WIFEXITED(status)) {
640                 ret = E_INVOLUNTARY_EXIT;
641                 make_err_msg("rsync process %d died involuntary", (int)pid);
642                 goto out;
643         }
644         es = WEXITSTATUS(status);
645         if (es != 0 && es != 23 && es != 24) {
646                 ret = -E_BAD_EXIT_CODE;
647                 make_err_msg("rsync process %d returned %d", (int)pid, es);
648                 goto out;
649         }
650         ret = rename_incomplete_snapshot(snapshot_num);
651 out:
652         free_rsync_argv(rsync_argv);
653         return ret;
654 }
655
656 int com_ls(void)
657 {
658         int i;
659         struct snapshot_list sl;
660         struct snapshot *s;
661         get_snapshot_list(&sl);
662         FOR_EACH_SNAPSHOT(s, i, &sl)
663                 dss_msg("%u\t%s\n", s->interval, s->name);
664         free_snapshot_list(&sl);
665         return 1;
666 }
667
668 /* TODO: Unlink pid file */
669 __noreturn void clean_exit(int status)
670 {
671         //kill(0, SIGTERM);
672         free(dss_error_txt);
673         exit(status);
674 }
675
676 int read_config_file(void)
677 {
678         int ret;
679         char *config_file;
680         struct stat statbuf;
681
682         if (conf.config_file_given)
683                 config_file = dss_strdup(conf.config_file_arg);
684         else {
685                 char *home = get_homedir();
686                 config_file = make_message("%s/.dssrc", home);
687                 free(home);
688         }
689         ret = stat(config_file, &statbuf);
690         if (ret && conf.config_file_given) {
691                 ret = -ERRNO_TO_DSS_ERROR(errno);
692                 make_err_msg("failed to stat config file %s", config_file);
693                 goto out;
694         }
695         if (!ret) {
696                 struct cmdline_parser_params params = {
697                         .override = 0,
698                         .initialize = 0,
699                         .check_required = 0,
700                         .check_ambiguity = 0
701                 };
702                 cmdline_parser_config_file(config_file, &conf, &params);
703         }
704         if (!conf.source_dir_given || !conf.dest_dir_given) {
705                 ret = -E_SYNTAX;
706                 make_err_msg("you need to specify both source_dir and dest_dir");
707                 goto out;
708         }
709         ret = 1;
710 out:
711         free(config_file);
712         return ret;
713 }
714
715 int check_config(void)
716 {
717         if (conf.unit_interval_arg <= 0) {
718                 make_err_msg("bad unit interval: %i", conf.unit_interval_arg);
719                 return -E_INVALID_NUMBER;
720         }
721         DSS_DEBUG_LOG("unit interval: %i day(s)\n", conf.unit_interval_arg);
722         if (conf.num_intervals_arg <= 0) {
723                 make_err_msg("bad number of intervals  %i", conf.num_intervals_arg);
724                 return -E_INVALID_NUMBER;
725         }
726         DSS_DEBUG_LOG("number of intervals: %i\n", conf.num_intervals_arg);
727         return 1;
728 }
729
730 static void setup_signal_handling(void)
731 {
732         int ret;
733
734         DSS_NOTICE_LOG("setting up signal handlers\n");
735         signal_pipe = signal_init(); /* always successful */
736         ret = install_sighandler(SIGINT);
737         if (ret < 0)
738                 goto err;
739         ret = install_sighandler(SIGTERM);
740         if (ret < 0)
741                 goto err;
742         ret = install_sighandler(SIGCHLD);
743         if (ret < 0)
744                 goto err;
745         return;
746 err:
747         DSS_EMERG_LOG("could not install signal handlers\n");
748         exit(EXIT_FAILURE);
749 }
750
751
752 int main(int argc, char **argv)
753 {
754         int ret;
755
756         cmdline_parser(argc, argv, &conf); /* aborts on errors */
757         if (conf.inputs_num) {
758                 ret = -E_SYNTAX;
759                 make_err_msg("additional non-options given");
760                 goto out;
761         }
762         ret = read_config_file();
763         if (ret < 0)
764                 goto out;
765         ret = check_config();
766         if (ret < 0)
767                 goto out;
768         if (conf.logfile_given) {
769                 logfile = open_log(conf.logfile_arg);
770                 log_welcome(conf.loglevel_arg);
771         }
772         ret = dss_chdir(conf.dest_dir_arg);
773         if (ret < 0)
774                 goto out;
775         if (conf.daemon_given)
776                 daemon_init();
777         setup_signal_handling();
778         ret = call_command_handler();
779 out:
780         if (ret < 0)
781                 log_err_msg(EMERG, -ret);
782         clean_exit(ret >= 0? EXIT_SUCCESS : EXIT_FAILURE);
783 }