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