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