All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joshua Brindle <jbrindle@tresys.com>
To: SELinux <SELinux@tycho.nsa.gov>
Subject: [PATCH] transaction rollback in lock
Date: Wed, 19 Oct 2005 09:13:57 -0400	[thread overview]
Message-ID: <43564695.1050900@tresys.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 65 bytes --]

This patch moves the transaction rollback into the commit lock.


[-- Attachment #2: 2-rollback-in-lock.diff --]
[-- Type: text/x-patch, Size: 7066 bytes --]

diff -purN -x .svn libsemanage/src/semanage_store.c libsemanage/src/semanage_store.c
--- libsemanage/src/semanage_store.c	2005-10-18 14:56:43.000000000 -0400
+++ libsemanage/src/semanage_store.c	2005-10-18 16:47:29.000000000 -0400
@@ -480,78 +480,6 @@ int semanage_get_modules_names(semanage_
 }
 
 
-/* Prepare the sandbox to be installed by making a backup of the
- * current active directory.  Then copy the sandbox to the active
- * directory.  Return the new commit number on success, negative
- * values on error. */
-static int semanage_commit_sandbox(semanage_handle_t *sh) {
-	int commit_number, fd, retval;
-	char write_buf[32];
-	const char *commit_filename = semanage_path(SEMANAGE_TMP, SEMANAGE_COMMIT_NUM_FILE);
-	ssize_t amount_written;
-	const char *active = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
-	const char *backup = semanage_path(SEMANAGE_PREVIOUS, SEMANAGE_TOPLEVEL);
-	const char *sandbox = semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL);
-	struct stat buf;
-
-	/* update the commit number */
-	if ((commit_number = semanage_get_commit_number(sh)) < 0) {
-		return -1;
-	}
-	commit_number++;
-	memset(write_buf, 0, sizeof(write_buf));
-	snprintf(write_buf, sizeof(write_buf), "%d", commit_number);
-	if ((fd = open(commit_filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
-		ERR(sh, "Could not open commit number file %s for writing.", commit_filename);
-		return -1;
-	}
-	amount_written = write(fd, write_buf, sizeof(write_buf));
-	if (amount_written == -1) {
-		ERR(sh, "Error while writing commit number to %s.", commit_filename);
-		close(fd);
-		return -1;
-	}
-	close(fd);
-
-        retval = commit_number;
-
-        if (semanage_get_active_lock(sh) < 0) {
-                return -1;
-        }
-	/* make the backup of the current active directory */
-	if (stat(backup, &buf) == 0) {
-		if (S_ISDIR(buf.st_mode) && 
-		    semanage_remove_directory(backup) != 0) {
-			ERR(sh, "Could not remove previous backup %s.", backup);
-                        retval = -1;
-                        goto cleanup;
-		}
-	}
-	else if (errno != ENOENT) {
-		ERR(sh, "Could not stat directory %s.", backup);
-		retval = -1;
-                goto cleanup;
-	}
-
-	if (rename(active, backup) == -1) {
-		ERR(sh, "Error while renaming %s to %s.", active, backup);
-		retval = -1;
-                goto cleanup;
-	}
-	if (rename(sandbox, active) == -1) {
-		ERR(sh, "Error while renaming %s to %s.", sandbox, active);
-		/* note that if an error occurs during the next
-		 * function then the store will be left in an
-		 * inconsistent state */
-		rename(backup, active);
-		retval = -1;
-                goto cleanup;
-	}
- cleanup:
-        semanage_release_active_lock(sh);
-	return retval;
-}
-
 /******************* routines that run external programs *******************/
 
 /* Appends a single character to a string.  Returns a pointer to the
@@ -863,6 +791,89 @@ static int semanage_install_active(seman
 	return retval;
 }
 
+/* Prepare the sandbox to be installed by making a backup of the
+ * current active directory.  Then copy the sandbox to the active
+ * directory.  Return the new commit number on success, negative
+ * values on error. */
+static int semanage_commit_sandbox(semanage_handle_t *sh) {
+	int commit_number, fd, retval;
+	char write_buf[32];
+	const char *commit_filename = semanage_path(SEMANAGE_TMP, SEMANAGE_COMMIT_NUM_FILE);
+	ssize_t amount_written;
+	const char *active = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
+	const char *backup = semanage_path(SEMANAGE_PREVIOUS, SEMANAGE_TOPLEVEL);
+	const char *sandbox = semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL);
+	struct stat buf;
+
+	/* update the commit number */
+	if ((commit_number = semanage_get_commit_number(sh)) < 0) {
+		return -1;
+	}
+	commit_number++;
+	memset(write_buf, 0, sizeof(write_buf));
+	snprintf(write_buf, sizeof(write_buf), "%d", commit_number);
+	if ((fd = open(commit_filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
+		ERR(sh, "Could not open commit number file %s for writing.", commit_filename);
+		return -1;
+	}
+	amount_written = write(fd, write_buf, sizeof(write_buf));
+	if (amount_written == -1) {
+		ERR(sh, "Error while writing commit number to %s.", commit_filename);
+		close(fd);
+		return -1;
+	}
+	close(fd);
+
+        retval = commit_number;
+
+        if (semanage_get_active_lock(sh) < 0) {
+                return -1;
+        }
+	/* make the backup of the current active directory */
+	if (stat(backup, &buf) == 0) {
+		if (S_ISDIR(buf.st_mode) && 
+		    semanage_remove_directory(backup) != 0) {
+			ERR(sh, "Could not remove previous backup %s.", backup);
+                        retval = -1;
+                        goto cleanup;
+		}
+	}
+	else if (errno != ENOENT) {
+		ERR(sh, "Could not stat directory %s.", backup);
+		retval = -1;
+                goto cleanup;
+	}
+
+	if (rename(active, backup) == -1) {
+		ERR(sh, "Error while renaming %s to %s.", active, backup);
+		retval = -1;
+                goto cleanup;
+	}
+	if (rename(sandbox, active) == -1) {
+		ERR(sh, "Error while renaming %s to %s.", sandbox, active);
+		/* note that if an error occurs during the next
+		 * function then the store will be left in an
+		 * inconsistent state */
+		rename(backup, active);
+		retval = -1;
+                goto cleanup;
+	}
+	if (semanage_install_active(sh) != 0) {
+		/* note that if an error occurs during the next three
+		 * function then the store will be left in an
+		 * inconsistent state */
+		rename(active, sandbox);
+		rename(backup, active);
+		semanage_install_active(sh);
+		retval = -1;
+		goto cleanup;
+	}
+
+ cleanup:
+        semanage_release_active_lock(sh);
+	return retval;
+}
+
 /* Takes the kernel policy in a sandbox, move it to the active
  * directory, copy it to the binary policy path, then load it.	Upon
  * error move the active directory back to the sandbox.	 This function
@@ -870,7 +881,7 @@ static int semanage_install_active(seman
  * atomically.	Returns 0 on success, -1 on error.
  */
 int semanage_install_sandbox(semanage_handle_t *sh) {
-	int retval = -1, new_commit_number;
+	int retval = -1;
 
 	if (sh->conf->load_policy == NULL) {
 		ERR(sh, "No load_policy program specified in configuration file.");
@@ -881,25 +892,13 @@ int semanage_install_sandbox(semanage_ha
 		goto cleanup;
 	}
 
-	if ((new_commit_number = semanage_commit_sandbox(sh)) < 0) {
+	if ((retval = semanage_commit_sandbox(sh)) < 0) {
 		goto cleanup;
 	}
-	if (semanage_install_active(sh) != 0) {
-		const char *active = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_TOPLEVEL);
-		const char *backup = semanage_path(SEMANAGE_PREVIOUS, SEMANAGE_TOPLEVEL);
-		const char *sandbox = semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL);
-		/* note that if an error occurs during the next three
-		 * function then the store will be left in an
-		 * inconsistent state */
-		rename(active, sandbox);
-		rename(backup, active);
-		semanage_install_active(sh);
-		goto cleanup;
-	}
-	retval = new_commit_number;
 
- cleanup:
+cleanup:
 	return retval;
+
 }
 
 


             reply	other threads:[~2005-10-19 13:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-19 13:13 Joshua Brindle [this message]
2005-10-19 16:12 ` [PATCH] transaction rollback in lock Stephen Smalley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43564695.1050900@tresys.com \
    --to=jbrindle@tresys.com \
    --cc=SELinux@tycho.nsa.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.