2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-08-31 06:16:03 +00:00

parser: Move policy cache functionality into policy_cache.c

Signed-off-by: John Johansen <john.johansen@canonical.com>
[tyhicks: Fixed build failures]
[tyhicks: Fixed bug where a warning was being printed when it shouldn't]
[tyhicks: Forward ported to trunk]
Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
This commit is contained in:
John Johansen
2015-03-25 17:09:26 -05:00
committed by Tyler Hicks
parent 4970d40e0d
commit 2a082ee543
6 changed files with 119 additions and 80 deletions

View File

@@ -695,10 +695,10 @@ int test_for_dir_mode(const char *basename, const char *linkdir)
int process_profile(int option, const char *profilename)
{
struct stat stat_bin;
int retval = 0;
autofree char *cachename = NULL;
autofree char *cachetemp = NULL;
autofree const char *cachename = NULL;
autofree const char *cachetmpname = NULL;
autoclose int cachetmp = -1;
const char *basename = NULL;
/* per-profile states */
@@ -737,24 +737,8 @@ int process_profile(int option, const char *profilename)
/* setup cachename and tstamp */
if (!force_complain && !skip_cache) {
if (asprintf(&cachename, "%s/%s", cacheloc, basename)<0) {
PERROR(_("Memory allocation error."));
exit(1);
}
if (!skip_read_cache) {
if (stat(cachename, &stat_bin) == 0 &&
stat_bin.st_size > 0) {
if (valid_cached_file_version(cachename))
set_mru_tstamp(stat_bin.st_ctim);
else if (!cond_clear_cache)
write_cache = 0;
} else {
if (!cond_clear_cache)
write_cache = 0;
if (debug_cache)
pwarn("%s: Invalid or missing cache file '%s' (%s)\n", progname, cachename, strerror(errno));
}
}
cachename = cache_filename(cacheloc, basename);
valid_read_cache(cachename);
}
}
@@ -773,36 +757,20 @@ int process_profile(int option, const char *profilename)
* TODO: Add support for caching profiles in an alternate namespace
* TODO: Add support for embedded namespace defines if they aren't
* removed from the language.
* TODO: test profile->ns NOT profile_ns (must be after parse)
*/
if (profile_ns)
skip_cache = 1;
/* Do secondary test to see if cached binary profile is good,
* instead of checking against a presupplied list of files
* use the timestamps from the files that were parsed.
* Parsing the profile is slower that doing primary cache check
* its still faster than doing full compilation
*/
if (cachename) {
/* Load a binary cache if it exists and is newest */
if (!mru_skip_cache) {
if (show_cache)
PERROR("Cache hit: %s\n", cachename);
if (cache_hit(cachename)) {
retval = process_binary(option, cachename);
if (!retval || skip_bad_cache_rebuild)
return retval;
}
if (write_cache) {
/* Otherwise, set up to save a cached copy */
if (asprintf(&cachetemp, "%s-XXXXXX", cachename)<0) {
perror("asprintf");
return ENOMEM;
}
if ( (cache_fd = mkstemp(cachetemp)) < 0) {
perror("mkstemp");
return ENOMEM;
}
}
cachetmp = setup_cache_tmp(&cachetmpname, cachename);
}
if (show_cache)
@@ -838,32 +806,18 @@ int process_profile(int option, const char *profilename)
goto out;
}
retval = load_policy(option);
out:
if (cachetemp) {
/* Only install the generate cache file if it parsed correctly
and did not have write/close errors */
int useable_cache = (cache_fd != -1 && retval == 0);
if (cache_fd != -1) {
if (close(cache_fd))
useable_cache = 0;
cache_fd = -1;
}
if (useable_cache) {
if (rename(cachetemp, cachename) < 0) {
pwarn("Warning failed to write cache: %s\n", cachename);
unlink(cachetemp);
}
else if (show_cache)
PERROR("Wrote cache: %s\n", cachename);
}
else {
unlink(cachetemp);
PERROR("Warning failed to create cache: %s\n", basename);
/* cache file generated by load_policy */
retval = load_policy(option, cachetmp);
if (retval == 0 && write_cache) {
if (cachetmp == -1) {
unlink(cachetmpname);
PERROR("Warning failed to create cache: %s\n",
basename);
} else {
install_cache(cachetmpname, cachename);
}
}
out:
return retval;
}