Blame SOURCES/0171-tests-secontext-add-secontext-field-getters.patch

742536
From 4951286eb634c00c11883b851c91f3a21975eabd Mon Sep 17 00:00:00 2001
742536
From: Eugene Syromyatnikov <evgsyr@gmail.com>
742536
Date: Tue, 18 Jan 2022 18:03:57 +0100
742536
Subject: [PATCH 171/174] tests/secontext: add secontext field getters
742536
742536
* tests/secontext.h (get_secontext_field, get_secontext_field_file): New
742536
declarations.
742536
* tests/secontext.c (get_type_from_context): Rename to...
742536
(get_secontext_field): ...this;  remove "static" qualifier;  add "field"
742536
argument, use it.
742536
(raw_expected_secontext_short_file, raw_secontext_short_pid): Replace
742536
get_type_from_context call with get_secontext_field.
742536
(get_secontext_field_file): New function.
742536
(raw_secontext_short_file): Replace body with get_secontext_field_file
742536
call.
742536
---
742536
 tests/secontext.c | 27 +++++++++++++++------------
742536
 tests/secontext.h | 20 ++++++++++++++++++++
742536
 2 files changed, 35 insertions(+), 12 deletions(-)
742536
742536
diff --git a/tests/secontext.c b/tests/secontext.c
742536
index 848eea9..52211ed 100644
742536
--- a/tests/secontext.c
742536
+++ b/tests/secontext.c
742536
@@ -56,8 +56,8 @@ strip_trailing_newlines(char *context)
742536
 	return context;
742536
 }
742536
 
742536
-static char *
742536
-get_type_from_context(const char *full_context)
742536
+char *
742536
+get_secontext_field(const char *full_context, enum secontext_field field)
742536
 {
742536
 	int saved_errno = errno;
742536
 
742536
@@ -72,7 +72,7 @@ get_type_from_context(const char *full_context)
742536
 	char *context = NULL;
742536
 	for (token = strtok_r(ctx_copy, ":", &saveptr), i = 0;
742536
 	     token; token = strtok_r(NULL, ":", &saveptr), i++) {
742536
-		if (i == 2) {
742536
+		if (i == field) {
742536
 			context = xstrdup(token);
742536
 			break;
742536
 		}
742536
@@ -122,7 +122,7 @@ raw_expected_secontext_short_file(const char *filename)
742536
 	int saved_errno = errno;
742536
 
742536
 	char *ctx = raw_expected_secontext_full_file(filename);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
742536
 	free(ctx);
742536
 
742536
 	errno = saved_errno;
742536
@@ -144,20 +144,23 @@ raw_secontext_full_file(const char *filename)
742536
 	return full_secontext;
742536
 }
742536
 
742536
-static char *
742536
-raw_secontext_short_file(const char *filename)
742536
+char *
742536
+get_secontext_field_file(const char *file, enum secontext_field field)
742536
 {
742536
-	int saved_errno = errno;
742536
-
742536
-	char *ctx = raw_secontext_full_file(filename);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *ctx = raw_secontext_full_file(file);
742536
+	char *type =  get_secontext_field(ctx, field);
742536
 	free(ctx);
742536
 
742536
-	errno = saved_errno;
742536
 	return type;
742536
 }
742536
 
742536
 static char *
742536
+raw_secontext_short_file(const char *filename)
742536
+{
742536
+	return get_secontext_field_file(filename, SECONTEXT_TYPE);
742536
+}
742536
+
742536
+static char *
742536
 raw_secontext_full_pid(pid_t pid)
742536
 {
742536
 	int saved_errno = errno;
742536
@@ -178,7 +181,7 @@ raw_secontext_short_pid(pid_t pid)
742536
 	int saved_errno = errno;
742536
 
742536
 	char *ctx = raw_secontext_full_pid(pid);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
742536
 	free(ctx);
742536
 
742536
 	errno = saved_errno;
742536
diff --git a/tests/secontext.h b/tests/secontext.h
742536
index 1d0251a..e5571d5 100644
742536
--- a/tests/secontext.h
742536
+++ b/tests/secontext.h
742536
@@ -23,6 +23,15 @@ enum secontext_field {
742536
 
742536
 #if defined TEST_SECONTEXT && defined HAVE_SELINUX_RUNTIME
742536
 
742536
+/**
742536
+ * Parse a SELinux context string and return a specified field, duplicated
742536
+ * in a separate string.  The caller is responsible for freeing the memory
742536
+ * pointed by the returned value.
742536
+ */
742536
+char *get_secontext_field(const char *full_context, enum secontext_field field);
742536
+
742536
+char *get_secontext_field_file(const char *file, enum secontext_field field);
742536
+
742536
 void update_secontext_field(const char *file, enum secontext_field field,
742536
 			    const char *newvalue);
742536
 
742536
@@ -48,6 +57,17 @@ void update_secontext_field(const char *file, enum secontext_field field,
742536
 
742536
 #else
742536
 
742536
+static inline char *
742536
+get_secontext_field(const char *ctx, enum secontext_field field)
742536
+{
742536
+	return NULL;
742536
+}
742536
+static inline char *
742536
+get_secontext_field_file(const char *file, enum secontext_field field)
742536
+{
742536
+	return NULL;
742536
+}
742536
+
742536
 static inline void
742536
 update_secontext_field(const char *file, enum secontext_field field,
742536
 		       const char *newvalue)
742536
diff --git a/tests-m32/secontext.c b/tests-m32/secontext.c
742536
index 848eea9..52211ed 100644
742536
--- a/tests-m32/secontext.c
742536
+++ b/tests-m32/secontext.c
742536
@@ -56,8 +56,8 @@ strip_trailing_newlines(char *context)
742536
 	return context;
742536
 }
742536
 
742536
-static char *
742536
-get_type_from_context(const char *full_context)
742536
+char *
742536
+get_secontext_field(const char *full_context, enum secontext_field field)
742536
 {
742536
 	int saved_errno = errno;
742536
 
742536
@@ -72,7 +72,7 @@ get_type_from_context(const char *full_context)
742536
 	char *context = NULL;
742536
 	for (token = strtok_r(ctx_copy, ":", &saveptr), i = 0;
742536
 	     token; token = strtok_r(NULL, ":", &saveptr), i++) {
742536
-		if (i == 2) {
742536
+		if (i == field) {
742536
 			context = xstrdup(token);
742536
 			break;
742536
 		}
742536
@@ -122,7 +122,7 @@ raw_expected_secontext_short_file(const char *filename)
742536
 	int saved_errno = errno;
742536
 
742536
 	char *ctx = raw_expected_secontext_full_file(filename);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
742536
 	free(ctx);
742536
 
742536
 	errno = saved_errno;
742536
@@ -144,20 +144,23 @@ raw_secontext_full_file(const char *filename)
742536
 	return full_secontext;
742536
 }
742536
 
742536
-static char *
742536
-raw_secontext_short_file(const char *filename)
742536
+char *
742536
+get_secontext_field_file(const char *file, enum secontext_field field)
742536
 {
742536
-	int saved_errno = errno;
742536
-
742536
-	char *ctx = raw_secontext_full_file(filename);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *ctx = raw_secontext_full_file(file);
742536
+	char *type =  get_secontext_field(ctx, field);
742536
 	free(ctx);
742536
 
742536
-	errno = saved_errno;
742536
 	return type;
742536
 }
742536
 
742536
 static char *
742536
+raw_secontext_short_file(const char *filename)
742536
+{
742536
+	return get_secontext_field_file(filename, SECONTEXT_TYPE);
742536
+}
742536
+
742536
+static char *
742536
 raw_secontext_full_pid(pid_t pid)
742536
 {
742536
 	int saved_errno = errno;
742536
@@ -178,7 +181,7 @@ raw_secontext_short_pid(pid_t pid)
742536
 	int saved_errno = errno;
742536
 
742536
 	char *ctx = raw_secontext_full_pid(pid);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
742536
 	free(ctx);
742536
 
742536
 	errno = saved_errno;
742536
diff --git a/tests-m32/secontext.h b/tests-m32/secontext.h
742536
index 1d0251a..e5571d5 100644
742536
--- a/tests-m32/secontext.h
742536
+++ b/tests-m32/secontext.h
742536
@@ -23,6 +23,15 @@ enum secontext_field {
742536
 
742536
 #if defined TEST_SECONTEXT && defined HAVE_SELINUX_RUNTIME
742536
 
742536
+/**
742536
+ * Parse a SELinux context string and return a specified field, duplicated
742536
+ * in a separate string.  The caller is responsible for freeing the memory
742536
+ * pointed by the returned value.
742536
+ */
742536
+char *get_secontext_field(const char *full_context, enum secontext_field field);
742536
+
742536
+char *get_secontext_field_file(const char *file, enum secontext_field field);
742536
+
742536
 void update_secontext_field(const char *file, enum secontext_field field,
742536
 			    const char *newvalue);
742536
 
742536
@@ -48,6 +57,17 @@ void update_secontext_field(const char *file, enum secontext_field field,
742536
 
742536
 #else
742536
 
742536
+static inline char *
742536
+get_secontext_field(const char *ctx, enum secontext_field field)
742536
+{
742536
+	return NULL;
742536
+}
742536
+static inline char *
742536
+get_secontext_field_file(const char *file, enum secontext_field field)
742536
+{
742536
+	return NULL;
742536
+}
742536
+
742536
 static inline void
742536
 update_secontext_field(const char *file, enum secontext_field field,
742536
 		       const char *newvalue)
742536
diff --git a/tests-mx32/secontext.c b/tests-mx32/secontext.c
742536
index 848eea9..52211ed 100644
742536
--- a/tests-mx32/secontext.c
742536
+++ b/tests-mx32/secontext.c
742536
@@ -56,8 +56,8 @@ strip_trailing_newlines(char *context)
742536
 	return context;
742536
 }
742536
 
742536
-static char *
742536
-get_type_from_context(const char *full_context)
742536
+char *
742536
+get_secontext_field(const char *full_context, enum secontext_field field)
742536
 {
742536
 	int saved_errno = errno;
742536
 
742536
@@ -72,7 +72,7 @@ get_type_from_context(const char *full_context)
742536
 	char *context = NULL;
742536
 	for (token = strtok_r(ctx_copy, ":", &saveptr), i = 0;
742536
 	     token; token = strtok_r(NULL, ":", &saveptr), i++) {
742536
-		if (i == 2) {
742536
+		if (i == field) {
742536
 			context = xstrdup(token);
742536
 			break;
742536
 		}
742536
@@ -122,7 +122,7 @@ raw_expected_secontext_short_file(const char *filename)
742536
 	int saved_errno = errno;
742536
 
742536
 	char *ctx = raw_expected_secontext_full_file(filename);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
742536
 	free(ctx);
742536
 
742536
 	errno = saved_errno;
742536
@@ -144,20 +144,23 @@ raw_secontext_full_file(const char *filename)
742536
 	return full_secontext;
742536
 }
742536
 
742536
-static char *
742536
-raw_secontext_short_file(const char *filename)
742536
+char *
742536
+get_secontext_field_file(const char *file, enum secontext_field field)
742536
 {
742536
-	int saved_errno = errno;
742536
-
742536
-	char *ctx = raw_secontext_full_file(filename);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *ctx = raw_secontext_full_file(file);
742536
+	char *type =  get_secontext_field(ctx, field);
742536
 	free(ctx);
742536
 
742536
-	errno = saved_errno;
742536
 	return type;
742536
 }
742536
 
742536
 static char *
742536
+raw_secontext_short_file(const char *filename)
742536
+{
742536
+	return get_secontext_field_file(filename, SECONTEXT_TYPE);
742536
+}
742536
+
742536
+static char *
742536
 raw_secontext_full_pid(pid_t pid)
742536
 {
742536
 	int saved_errno = errno;
742536
@@ -178,7 +181,7 @@ raw_secontext_short_pid(pid_t pid)
742536
 	int saved_errno = errno;
742536
 
742536
 	char *ctx = raw_secontext_full_pid(pid);
742536
-	char *type = get_type_from_context(ctx);
742536
+	char *type = get_secontext_field(ctx, SECONTEXT_TYPE);
742536
 	free(ctx);
742536
 
742536
 	errno = saved_errno;
742536
diff --git a/tests-mx32/secontext.h b/tests-mx32/secontext.h
742536
index 1d0251a..e5571d5 100644
742536
--- a/tests-mx32/secontext.h
742536
+++ b/tests-mx32/secontext.h
742536
@@ -23,6 +23,15 @@ enum secontext_field {
742536
 
742536
 #if defined TEST_SECONTEXT && defined HAVE_SELINUX_RUNTIME
742536
 
742536
+/**
742536
+ * Parse a SELinux context string and return a specified field, duplicated
742536
+ * in a separate string.  The caller is responsible for freeing the memory
742536
+ * pointed by the returned value.
742536
+ */
742536
+char *get_secontext_field(const char *full_context, enum secontext_field field);
742536
+
742536
+char *get_secontext_field_file(const char *file, enum secontext_field field);
742536
+
742536
 void update_secontext_field(const char *file, enum secontext_field field,
742536
 			    const char *newvalue);
742536
 
742536
@@ -48,6 +57,17 @@ void update_secontext_field(const char *file, enum secontext_field field,
742536
 
742536
 #else
742536
 
742536
+static inline char *
742536
+get_secontext_field(const char *ctx, enum secontext_field field)
742536
+{
742536
+	return NULL;
742536
+}
742536
+static inline char *
742536
+get_secontext_field_file(const char *file, enum secontext_field field)
742536
+{
742536
+	return NULL;
742536
+}
742536
+
742536
 static inline void
742536
 update_secontext_field(const char *file, enum secontext_field field,
742536
 		       const char *newvalue)
742536
-- 
742536
2.1.4
742536