From d2e3f806c036f0519ebc17d0ea227e620113aaa1 Mon Sep 17 00:00:00 2001 From: Tyler Hicks Date: Wed, 25 Mar 2015 17:09:26 -0500 Subject: [PATCH] parser: Don't use the basedir global in setup_cache() Require the caller of setup_cache() to pass in a valid cache location string. This removes the use of the basedir global from the policy_cache.c file. Additionally, it is no longer necessary to return the "cache dir" path from setup_cache() since it will always be the same as the input path. The return value is changed to an int so an error code can be returned instead of using exit(). Signed-off-by: Tyler Hicks Acked-by: John Johansen --- parser/parser_main.c | 17 +++++++++++++---- parser/policy_cache.c | 30 +++++++++++------------------- parser/policy_cache.h | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/parser/parser_main.c b/parser/parser_main.c index 1548fc27c..8e68f5b26 100644 --- a/parser/parser_main.c +++ b/parser/parser_main.c @@ -877,7 +877,6 @@ static void setup_flags(void) int main(int argc, char *argv[]) { - autofree char *cachedir = NULL; int retval, last_error; int i; int optind; @@ -910,7 +909,17 @@ int main(int argc, char *argv[]) setup_flags(); - cachedir = setup_cache(cacheloc); + if (!cacheloc && asprintf(&cacheloc, "%s/cache", basedir) == -1) { + PERROR(_("Memory allocation error.")); + return 1; + } + + retval = setup_cache(cacheloc); + if (retval) { + PERROR(_("Failed setting up policy cache (%s): %s\n"), + cacheloc, strerror(errno)); + return 1; + } retval = last_error = 0; for (i = optind; i <= argc; i++) { @@ -938,7 +947,7 @@ int main(int argc, char *argv[]) struct dir_cb_data cb_data; cb_data.dirname = profilename; - cb_data.cachedir = cachedir; + cb_data.cachedir = cacheloc; cb = binary_input ? binary_dir_cb : profile_dir_cb; if ((retval = dirat_for_each(NULL, profilename, &cb_data, cb))) { @@ -948,7 +957,7 @@ int main(int argc, char *argv[]) } else if (binary_input) { retval = process_binary(option, profilename); } else { - retval = process_profile(option, profilename, cachedir); + retval = process_profile(option, profilename, cacheloc); } if (profilename) free(profilename); diff --git a/parser/policy_cache.c b/parser/policy_cache.c index b9aa795f8..e7c3c609e 100644 --- a/parser/policy_cache.c +++ b/parser/policy_cache.c @@ -228,27 +228,18 @@ void install_cache(const char *cachetmpname, const char *cachename) } } -char *setup_cache(const char *cacheloc) +int setup_cache(const char *cacheloc) { autofree char *cache_features_path = NULL; autofree char *cache_flags = NULL; - char *cachedir; - if (cacheloc) { - cachedir = strdup(cacheloc); - if (!cachedir) { - PERROR(_("Memory allocation error.")); - exit(1); - } - } else { - if (asprintf(&cachedir, "%s/cache", basedir) == -1) { - PERROR(_("Memory allocation error.")); - exit(1); - } + if (!cacheloc) { + errno = EINVAL; + return -1; } if (force_clear_cache) - exit(clear_cache_files(cachedir)); + exit(clear_cache_files(cacheloc)); /* * Deal with cache directory versioning: @@ -256,16 +247,17 @@ char *setup_cache(const char *cacheloc) * - If cache/.features exists, and does not match features_string, * force cache reading/writing off. */ - if (asprintf(&cache_features_path, "%s/.features", cachedir) == -1) { + if (asprintf(&cache_features_path, "%s/.features", cacheloc) == -1) { PERROR(_("Memory allocation error.")); - exit(1); + errno = ENOMEM; + return -1; } cache_flags = load_features_file(cache_features_path); if (cache_flags) { if (strcmp(features_string, cache_flags) != 0) { if (write_cache && cond_clear_cache) { - if (create_cache(cachedir, cache_features_path, + if (create_cache(cacheloc, cache_features_path, features_string)) skip_read_cache = 1; } else { @@ -276,8 +268,8 @@ char *setup_cache(const char *cacheloc) } } } else if (write_cache) { - create_cache(cachedir, cache_features_path, features_string); + create_cache(cacheloc, cache_features_path, features_string); } - return cachedir; + return 0; } diff --git a/parser/policy_cache.h b/parser/policy_cache.h index 76d2f16d7..68d45a45d 100644 --- a/parser/policy_cache.h +++ b/parser/policy_cache.h @@ -46,6 +46,6 @@ void valid_read_cache(const char *cachename); int cache_hit(const char *cachename); int setup_cache_tmp(const char **cachetmpname, const char *cachename); void install_cache(const char *cachetmpname, const char *cachename); -char *setup_cache(const char *cacheloc); +int setup_cache(const char *cacheloc); #endif /* __AA_POLICY_CACHE_H */