[apparmor] [PATCH v2 22/42] parser: Create aa_features function to write features to a file

Tyler Hicks tyhicks at canonical.com
Fri Mar 6 21:48:38 UTC 2015


This patch removes the final dependency on callers needing access to the
features string so aa_features_get_string() can go away.

Signed-off-by: Tyler Hicks <tyhicks at canonical.com>
Acked-by: John Johansen <john.johansen at canonical.com>
---
 parser/features.c     | 30 ++++++++++++++++++++++++++----
 parser/features.h     |  2 +-
 parser/policy_cache.c | 18 ++++++------------
 3 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/parser/features.c b/parser/features.c
index 50e0024..d0153ad 100644
--- a/parser/features.c
+++ b/parser/features.c
@@ -358,14 +358,36 @@ void aa_features_unref(aa_features *features)
 }
 
 /**
- * aa_features_get_string - provides immutable string representation of features
+ * aa_features_write_to_file - write a string representation to a file
  * @features: the features
+ * @path: the path to write to
  *
- * Returns: an immutable string representation of features
+ * Returns: 0 on success, -1 on error with errno set
  */
-const char *aa_features_get_string(aa_features *features)
+int aa_features_write_to_file(aa_features *features, const char *path)
 {
-	return features->string;
+	autoclose int fd = -1;
+	size_t size;
+	ssize_t retval;
+	char *string;
+
+	fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC | O_CLOEXEC,
+		  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+	if (fd == -1)
+		return -1;
+
+	string = features->string;
+	size = strlen(string);
+	do {
+		retval = write(fd, string, size);
+		if (retval == -1)
+			return -1;
+
+		size -= retval;
+		string += retval;
+	} while (size);
+
+	return 0;
 }
 
 /**
diff --git a/parser/features.h b/parser/features.h
index 96d0ee9..1d3cf3f 100644
--- a/parser/features.h
+++ b/parser/features.h
@@ -27,7 +27,7 @@ int aa_features_new_from_string(aa_features **features,
 int aa_features_new_from_kernel(aa_features **features);
 aa_features *aa_features_ref(aa_features *features);
 void aa_features_unref(aa_features *features);
-const char *aa_features_get_string(aa_features *features);
+int aa_features_write_to_file(aa_features *features, const char *path);
 bool aa_features_is_equal(aa_features *features1, aa_features *features2);
 bool aa_features_supports(aa_features *features, char *str);
 
diff --git a/parser/policy_cache.c b/parser/policy_cache.c
index 27c83c1..dea7e21 100644
--- a/parser/policy_cache.c
+++ b/parser/policy_cache.c
@@ -107,7 +107,7 @@ int clear_cache_files(const char *path)
 	return dirat_for_each(NULL, path, NULL, clear_cache_cb);
 }
 
-int create_cache(const char *cachedir, const char *path, const char *features)
+int create_cache(const char *cachedir, const char *path, aa_features *features)
 {
 	struct stat stat_file;
 	autofclose FILE * f = NULL;
@@ -116,13 +116,10 @@ int create_cache(const char *cachedir, const char *path, const char *features)
 		goto error;
 
 create_file:
-	f = fopen(path, "w");
-	if (f) {
-		if (fwrite(features, strlen(features), 1, f) != 1 )
-			goto error;
+	if (aa_features_write_to_file(features, path) == -1)
+		goto error;
 
-		return 0;
-	}
+	return 0;
 
 error:
 	/* does the dir exist? */
@@ -231,7 +228,6 @@ int setup_cache(aa_features *kernel_features, const char *cacheloc)
 {
 	autofree char *cache_features_path = NULL;
 	aa_features *cache_features;
-	const char *kernel_features_string;
 
 	if (!cacheloc) {
 		errno = EINVAL;
@@ -250,12 +246,11 @@ int setup_cache(aa_features *kernel_features, const char *cacheloc)
 		return -1;
 	}
 
-	kernel_features_string = aa_features_get_string(kernel_features);
 	if (!aa_features_new(&cache_features, cache_features_path)) {
 		if (!aa_features_is_equal(kernel_features, cache_features)) {
 			if (write_cache && cond_clear_cache) {
 				if (create_cache(cacheloc, cache_features_path,
-						 kernel_features_string))
+						 kernel_features))
 					skip_read_cache = 1;
 			} else {
 				if (show_cache)
@@ -266,8 +261,7 @@ int setup_cache(aa_features *kernel_features, const char *cacheloc)
 		}
 		aa_features_unref(cache_features);
 	} else if (write_cache) {
-		create_cache(cacheloc, cache_features_path,
-			     kernel_features_string);
+		create_cache(cacheloc, cache_features_path, kernel_features);
 	}
 
 	return 0;
-- 
2.1.4




More information about the AppArmor mailing list