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