#include <errno.h>
#include <getopt.h>
-#include <string.h>
#include <sys/prctl.h>
#include <unistd.h>
#include "hashmap.h"
#include "locale-util.h"
#include "log.h"
+#include "main-func.h"
+#include "nulstr-util.h"
#include "pager.h"
#include "parse-util.h"
#include "path-util.h"
+#include "pretty-print.h"
#include "process-util.h"
#include "signal-util.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
-#include "util.h"
static const char prefixes[] =
"/etc\0"
_cleanup_free_ char *x = NULL, *y = NULL;
int r;
- r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x);
+ r = chase_symlinks(a, NULL, CHASE_TRAIL_SLASH, &x, NULL);
if (r < 0)
return r;
- r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y);
+ r = chase_symlinks(b, NULL, CHASE_TRAIL_SLASH, &y, NULL);
if (r < 0)
return r;
printf("%s%s%s %s %s %s\n",
ansi_highlight_red(), "[MASKED]", ansi_normal(),
- top, special_glyph(ARROW), bottom);
+ top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
return 1;
}
printf("%s%s%s %s %s %s\n",
ansi_highlight_green(), "[EQUIVALENT]", ansi_normal(),
- top, special_glyph(ARROW), bottom);
+ top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
return 1;
}
printf("%s%s%s %s %s %s\n",
ansi_highlight(), "[REDIRECTED]", ansi_normal(),
- top, special_glyph(ARROW), bottom);
+ top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
return 1;
}
printf("%s%s%s %s %s %s\n",
ansi_highlight(), "[OVERRIDDEN]", ansi_normal(),
- top, special_glyph(ARROW), bottom);
+ top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
return 1;
}
printf("%s%s%s %s %s %s\n",
ansi_highlight(), "[EXTENDED]", ansi_normal(),
- top, special_glyph(ARROW), bottom);
+ top, special_glyph(SPECIAL_GLYPH_ARROW), bottom);
return 1;
}
fflush(stdout);
- r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_LOG, &pid);
+ r = safe_fork("(diff)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
if (r < 0)
return r;
if (r == 0) {
assert(!endswith(drop, "/"));
- path = strjoin(toppath, "/", drop);
+ path = path_join(toppath, drop);
if (!path)
return -ENOMEM;
if (!endswith(*file, ".conf"))
continue;
- p = strjoin(path, "/", *file);
+ p = path_join(path, *file);
if (!p)
return -ENOMEM;
d = p + strlen(toppath) + 1;
- log_debug("Adding at top: %s %s %s", d, special_glyph(ARROW), p);
+ log_debug("Adding at top: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW), p);
k = ordered_hashmap_put(top, d, p);
if (k >= 0) {
p = strdup(p);
return k;
}
- log_debug("Adding at bottom: %s %s %s", d, special_glyph(ARROW), p);
+ log_debug("Adding at bottom: %s %s %s", d, special_glyph(SPECIAL_GLYPH_ARROW), p);
free(ordered_hashmap_remove(bottom, d));
k = ordered_hashmap_put(bottom, d, p);
if (k < 0) {
return -ENOMEM;
log_debug("Adding to drops: %s %s %s %s %s",
- unit, special_glyph(ARROW), basename(p), special_glyph(ARROW), p);
+ unit, special_glyph(SPECIAL_GLYPH_ARROW), basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
k = ordered_hashmap_put(h, basename(p), p);
if (k < 0) {
free(p);
STRV_FOREACH(t, files) {
_cleanup_free_ char *p = NULL;
- p = strjoin(path, "/", *t);
+ p = path_join(path, *t);
if (!p)
return -ENOMEM;
- log_debug("Adding at top: %s %s %s", basename(p), special_glyph(ARROW), p);
+ log_debug("Adding at top: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
r = ordered_hashmap_put(top, basename(p), p);
if (r >= 0) {
p = strdup(p);
} else if (r != -EEXIST)
return r;
- log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(ARROW), p);
+ log_debug("Adding at bottom: %s %s %s", basename(p), special_glyph(SPECIAL_GLYPH_ARROW), p);
free(ordered_hashmap_remove(bottom, basename(p)));
r = ordered_hashmap_put(bottom, basename(p), p);
if (r < 0)
return 0;
}
-static bool should_skip_path(const char *prefix, const char *suffix) {
+static int should_skip_path(const char *prefix, const char *suffix) {
#if HAVE_SPLIT_USR
_cleanup_free_ char *target = NULL;
- const char *p;
- char *dirname;
+ const char *dirname, *p;
- dirname = strjoina(prefix, "/", suffix);
+ dirname = prefix_roota(prefix, suffix);
- if (chase_symlinks(dirname, NULL, 0, &target) < 0)
+ if (chase_symlinks(dirname, NULL, 0, &target, NULL) < 0)
return false;
NULSTR_FOREACH(p, prefixes) {
+ _cleanup_free_ char *tmp = NULL;
+
if (path_startswith(dirname, p))
continue;
- if (path_equal(target, strjoina(p, "/", suffix))) {
+ tmp = path_join(p, suffix);
+ if (!tmp)
+ return -ENOMEM;
+
+ if (path_equal(target, tmp)) {
log_debug("%s redirects to %s, skipping.", dirname, target);
return true;
}
static int process_suffix(const char *suffix, const char *onlyprefix) {
const char *p;
- char *f;
- OrderedHashmap *top, *bottom, *drops;
- OrderedHashmap *h;
- char *key;
- int r = 0, k;
- Iterator i, j;
- int n_found = 0;
+ char *f, *key;
+ OrderedHashmap *top, *bottom, *drops, *h;
+ int r = 0, k, n_found = 0;
bool dropins;
assert(suffix);
NULSTR_FOREACH(p, prefixes) {
_cleanup_free_ char *t = NULL;
- if (should_skip_path(p, suffix))
+ if (should_skip_path(p, suffix) > 0)
continue;
- t = strjoin(p, "/", suffix);
+ t = path_join(p, suffix);
if (!t) {
r = -ENOMEM;
goto finish;
r = k;
}
- ORDERED_HASHMAP_FOREACH_KEY(f, key, top, i) {
+ ORDERED_HASHMAP_FOREACH_KEY(f, key, top) {
char *o;
o = ordered_hashmap_get(bottom, key);
h = ordered_hashmap_get(drops, key);
if (h)
- ORDERED_HASHMAP_FOREACH(o, h, j)
+ ORDERED_HASHMAP_FOREACH(o, h)
if (!onlyprefix || startswith(o, onlyprefix))
n_found += notify_override_extended(f, o);
}
ordered_hashmap_free_free(top);
ordered_hashmap_free_free(bottom);
- ORDERED_HASHMAP_FOREACH_KEY(h, key, drops, i) {
+ ORDERED_HASHMAP_FOREACH_KEY(h, key, drops) {
ordered_hashmap_free_free(ordered_hashmap_remove(drops, key));
ordered_hashmap_remove(drops, key);
free(key);
}
}
- log_error("Invalid suffix specification %s.", arg);
- return -EINVAL;
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "Invalid suffix specification %s.", arg);
}
static int help(void) {
case 't': {
int f;
f = parse_flags(optarg, arg_flags);
- if (f < 0) {
- log_error("Failed to parse flags field.");
- return -EINVAL;
- }
+ if (f < 0)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "Failed to parse flags field.");
arg_flags = f;
break;
}
int b;
b = parse_boolean(optarg);
- if (b < 0) {
- log_error("Failed to parse diff boolean.");
- return -EINVAL;
- }
+ if (b < 0)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+ "Failed to parse diff boolean.");
arg_diff = b;
}
return 1;
}
-int main(int argc, char *argv[]) {
+static int run(int argc, char *argv[]) {
int r, k, n_found = 0;
- log_parse_environment();
- log_open();
+ log_setup_cli();
r = parse_argv(argc, argv);
if (r <= 0)
- goto finish;
+ return r;
if (arg_flags == 0)
arg_flags = SHOW_DEFAULTS;
if (r >= 0)
printf("%s%i overridden configuration files found.\n", n_found ? "\n" : "", n_found);
-
-finish:
- pager_close();
-
- return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+ return r;
}
+
+DEFINE_MAIN_FUNCTION(run);