Blame SOURCES/0011-libsemanage-clean-up-semanage_direct_commit-a-bit.patch

d477aa
From 129e121c9ab17f726a9a6294cfe04db97016e7ef Mon Sep 17 00:00:00 2001
d477aa
From: Ondrej Mosnacek <omosnace@redhat.com>
d477aa
Date: Thu, 3 Feb 2022 17:53:25 +0100
d477aa
Subject: [PATCH] libsemanage: clean up semanage_direct_commit() a bit
d477aa
d477aa
Do some minor cosmetic cleanup, mainly to eliminate the 'rebuilt' goto
d477aa
label.
d477aa
d477aa
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
d477aa
---
d477aa
 libsemanage/src/direct_api.c | 91 ++++++++++++++++++------------------
d477aa
 1 file changed, 45 insertions(+), 46 deletions(-)
d477aa
d477aa
diff --git a/libsemanage/src/direct_api.c b/libsemanage/src/direct_api.c
d477aa
index 63a18808..7e99a59f 100644
d477aa
--- a/libsemanage/src/direct_api.c
d477aa
+++ b/libsemanage/src/direct_api.c
d477aa
@@ -994,6 +994,16 @@ cleanup:
d477aa
 	return status;
d477aa
 }
d477aa
 
d477aa
+/* Files that must exist in order to skip policy rebuild. */
d477aa
+static const int semanage_computed_files[] = {
d477aa
+	SEMANAGE_STORE_KERNEL,
d477aa
+	SEMANAGE_STORE_FC,
d477aa
+	SEMANAGE_STORE_SEUSERS,
d477aa
+	SEMANAGE_LINKED,
d477aa
+	SEMANAGE_SEUSERS_LINKED,
d477aa
+	SEMANAGE_USERS_EXTRA_LINKED
d477aa
+};
d477aa
+
d477aa
 /* Copies a file from src to dst. If dst already exists then
d477aa
  * overwrite it. If source doesn't exist then return success.
d477aa
  * Returns 0 on success, -1 on error. */
d477aa
@@ -1053,6 +1063,14 @@ static int semanage_direct_commit(semanage_handle_t * sh)
d477aa
 	seusers_modified = seusers->dtable->is_modified(seusers->dbase);
d477aa
 	fcontexts_modified = fcontexts->dtable->is_modified(fcontexts->dbase);
d477aa
 
d477aa
+	/* Before we do anything else, flush the join to its component parts.
d477aa
+	 * This *does not* flush to disk automatically */
d477aa
+	if (users->dtable->is_modified(users->dbase)) {
d477aa
+		retval = users->dtable->flush(sh, users->dbase);
d477aa
+		if (retval < 0)
d477aa
+			goto cleanup;
d477aa
+	}
d477aa
+
d477aa
 	/* Rebuild if explicitly requested or any module changes occurred. */
d477aa
 	do_rebuild = sh->do_rebuild | sh->modules_modified;
d477aa
 
d477aa
@@ -1119,14 +1137,6 @@ static int semanage_direct_commit(semanage_handle_t * sh)
d477aa
 		}
d477aa
 	}
d477aa
 
d477aa
-	/* Before we do anything else, flush the join to its component parts.
d477aa
-	 * This *does not* flush to disk automatically */
d477aa
-	if (users->dtable->is_modified(users->dbase)) {
d477aa
-		retval = users->dtable->flush(sh, users->dbase);
d477aa
-		if (retval < 0)
d477aa
-			goto cleanup;
d477aa
-	}
d477aa
-
d477aa
 	/*
d477aa
 	 * This is for systems that have already migrated with an older version
d477aa
 	 * of semanage_migrate_store. The older version did not copy
d477aa
@@ -1135,48 +1145,20 @@ static int semanage_direct_commit(semanage_handle_t * sh)
d477aa
 	 * in order to skip re-linking are present; otherwise, we force
d477aa
 	 * a rebuild.
d477aa
 	 */
d477aa
-	if (!do_rebuild) {
d477aa
-		int files[] = {SEMANAGE_STORE_KERNEL,
d477aa
-					   SEMANAGE_STORE_FC,
d477aa
-					   SEMANAGE_STORE_SEUSERS,
d477aa
-					   SEMANAGE_LINKED,
d477aa
-					   SEMANAGE_SEUSERS_LINKED,
d477aa
-					   SEMANAGE_USERS_EXTRA_LINKED};
d477aa
-
d477aa
-		for (i = 0; i < (int) ARRAY_SIZE(files); i++) {
d477aa
-			path = semanage_path(SEMANAGE_TMP, files[i]);
d477aa
-			if (stat(path, &sb) != 0) {
d477aa
-				if (errno != ENOENT) {
d477aa
-					ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
d477aa
-					retval = -1;
d477aa
-					goto cleanup;
d477aa
-				}
d477aa
-
d477aa
-				do_rebuild = 1;
d477aa
-				goto rebuild;
d477aa
+	for (i = 0; !do_rebuild && i < (int)ARRAY_SIZE(semanage_computed_files); i++) {
d477aa
+		path = semanage_path(SEMANAGE_TMP, semanage_computed_files[i]);
d477aa
+		if (stat(path, &sb) != 0) {
d477aa
+			if (errno != ENOENT) {
d477aa
+				ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
d477aa
+				retval = -1;
d477aa
+				goto cleanup;
d477aa
 			}
d477aa
+
d477aa
+			do_rebuild = 1;
d477aa
+			break;
d477aa
 		}
d477aa
 	}
d477aa
 
d477aa
-rebuild:
d477aa
-	/*
d477aa
-	 * Now that we know whether or not a rebuild is required,
d477aa
-	 * we can determine what else needs to be done.
d477aa
-	 * We need to write the kernel policy if we are rebuilding
d477aa
-	 * or if any other policy component that lives in the kernel
d477aa
-	 * policy has been modified.
d477aa
-	 * We need to install the policy files if any of the managed files
d477aa
-	 * that live under /etc/selinux (kernel policy, seusers, file contexts)
d477aa
-	 * will be modified.
d477aa
-	 */
d477aa
-	do_write_kernel = do_rebuild | ports_modified | ibpkeys_modified |
d477aa
-		ibendports_modified |
d477aa
-		bools->dtable->is_modified(bools->dbase) |
d477aa
-		ifaces->dtable->is_modified(ifaces->dbase) |
d477aa
-		nodes->dtable->is_modified(nodes->dbase) |
d477aa
-		users->dtable->is_modified(users_base->dbase);
d477aa
-	do_install = do_write_kernel | seusers_modified | fcontexts_modified;
d477aa
-
d477aa
 	/*
d477aa
 	 * If there were policy changes, or explicitly requested, or
d477aa
 	 * any required files are missing, rebuild the policy.
d477aa
@@ -1323,6 +1305,23 @@ rebuild:
d477aa
 		}
d477aa
 	}
d477aa
 
d477aa
+	/*
d477aa
+	 * Determine what else needs to be done.
d477aa
+	 * We need to write the kernel policy if we are rebuilding
d477aa
+	 * or if any other policy component that lives in the kernel
d477aa
+	 * policy has been modified.
d477aa
+	 * We need to install the policy files if any of the managed files
d477aa
+	 * that live under /etc/selinux (kernel policy, seusers, file contexts)
d477aa
+	 * will be modified.
d477aa
+	 */
d477aa
+	do_write_kernel = do_rebuild | ports_modified | ibpkeys_modified |
d477aa
+		ibendports_modified |
d477aa
+		bools->dtable->is_modified(bools->dbase) |
d477aa
+		ifaces->dtable->is_modified(ifaces->dbase) |
d477aa
+		nodes->dtable->is_modified(nodes->dbase) |
d477aa
+		users->dtable->is_modified(users_base->dbase);
d477aa
+	do_install = do_write_kernel | seusers_modified | fcontexts_modified;
d477aa
+
d477aa
 	/* Attach our databases to the policydb we just created or loaded. */
d477aa
 	dbase_policydb_attach((dbase_policydb_t *) pusers_base->dbase, out);
d477aa
 	dbase_policydb_attach((dbase_policydb_t *) pports->dbase, out);
d477aa
-- 
d477aa
2.30.2
d477aa