Skip to contentSkip to conversation

liblvm: Save off and restore umask values

PATCH New Tony Asleson 1 participant 1 message Dec 17, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 liblvm: Save off and restore umask values

lvm has a default umask value in the config file that defaults to 0077 which lvm changes to during normal operation. This causes a problem when the code is used as a library with liblvm as it is changing the umask for the process. This patch saves off the current umask, sets to what is specified in the config file and restores it what it was on library function call exit.

The user is now free to change the umask in their application at anytime including between library calls.

This fix address BZ:
https://bugzilla.redhat.com/show_bug.cgi?id=1012113

Tested by setting umask to 0777 and running the python unit test and verifying that umask is still same value as expected at the test completion and with a successful run.

---
 liblvm/lvm_base.c |  68 +++++++++++--
 liblvm/lvm_lv.c   | 246 ++++++++++++++++++++++++++++++++++++++-------
 liblvm/lvm_misc.c |  25 +++++
 liblvm/lvm_misc.h |  12 +++
 liblvm/lvm_pv.c   | 150 +++++++++++++++++++++++-----
 liblvm/lvm_vg.c   | 292 ++++++++++++++++++++++++++++++++++++++++--------------
 6 files changed, 647 insertions(+), 146 deletions(-)
liblvm/lvm_base.c +57 -11
index 80a4894..2a373ba 100644
@@ -18,13 +18,14 @@
 #include "lvm-version.h"
 #include "metadata-exported.h"
 #include "lvm2app.h"
+#include "lvm_misc.h"
 
 const char *lvm_library_get_version(void)
 {
 	return LVM_VERSION;
 }
 
-lvm_t lvm_init(const char *system_dir)
+static lvm_t _lvm_init(const char *system_dir)
 {
 	struct cmd_context *cmd;
 
@@ -75,18 +76,34 @@ lvm_t lvm_init(const char *system_dir)
 	return (lvm_t) cmd;
 }
 
+
+lvm_t lvm_init(const char *system_dir)
+{
+	lvm_t h = NULL;
+	struct saved_env e = store_user_env(NULL);
+	h = _lvm_init(system_dir);
+	restore_user_env(&e);
+	return h;
+}
+
 void lvm_quit(lvm_t libh)
 {
+	struct saved_env e = store_user_env((struct cmd_context *)libh);
 	destroy_toolcontext((struct cmd_context *)libh);
 	udev_fin_library_context();
+	restore_user_env(&e);
 }
 
 int lvm_config_reload(lvm_t libh)
 {
+	int rc = 0;
+
 	/* FIXME: re-init locking needed here? */
+	struct saved_env e = store_user_env((struct cmd_context *)libh);
 	if (!refresh_toolcontext((struct cmd_context *)libh))
-		return -1;
-	return 0;
+		rc = -1;
+	restore_user_env(&e);
+	return rc;
 }
 
 /*
@@ -94,54 +111,83 @@ int lvm_config_reload(lvm_t libh)
  */
 int lvm_config_override(lvm_t libh, const char *config_settings)
 {
+	int rc = 0;
 	struct cmd_context *cmd = (struct cmd_context *)libh;
+	struct saved_env e = store_user_env((struct cmd_context *)libh);
+
 	if (!override_config_tree_from_string(cmd, config_settings))
-		return -1;
-	return 0;
+		rc = -1;
+	restore_user_env(&e);
+	return rc;
 }
 
 int lvm_config_find_bool(lvm_t libh, const char *config_path, int fail)
 {
+	int rc = 0;
 	struct cmd_context *cmd = (struct cmd_context *)libh;
+	struct saved_env e = store_user_env((struct cmd_context *)libh);
 
-	return dm_config_tree_find_bool(cmd->cft, config_path, fail);
+	rc = dm_config_tree_find_bool(cmd->cft, config_path, fail);
+	restore_user_env(&e);
+	return rc;
 }
 
 int lvm_errno(lvm_t libh)
 {
-	return stored_errno();
+	int rc;
+	struct saved_env e = store_user_env((struct cmd_context *)libh);
+	rc = stored_errno();
+	restore_user_env(&e);
+	return rc;
 }
 
 const char *lvm_errmsg(lvm_t libh)
 {
 	const char *rc = NULL;
 	struct cmd_context *cmd = (struct cmd_context *)libh;
+	struct saved_env e = store_user_env((struct cmd_context *)libh);
+
 	const char *msg = stored_errmsg_with_clear();
 	if (msg) {
 		rc = dm_pool_strdup(cmd->mem, msg);
 		free((void *)msg);
 	}
+
+	restore_user_env(&e);
 	return rc;
 }
 
 const char *lvm_vgname_from_pvid(lvm_t libh, const char *pvid)
 {
+	const char *rc = NULL;
 	struct cmd_context *cmd = (struct cmd_context *)libh;
 	struct id id;
+	struct saved_env e = store_user_env((struct cmd_context *)libh);
 
-	if (!id_read_format(&id, pvid)) {
+	if (id_read_format(&id, pvid)) {
+		rc = find_vgname_from_pvid(cmd, (char *)id.uuid);
+	} else {
 		log_error(INTERNAL_ERROR "Unable to convert uuid");
-		return NULL;
 	}
-	return find_vgname_from_pvid(cmd, (char *)id.uuid);
+
+	restore_user_env(&e);
+	return rc;
 }
 
 const char *lvm_vgname_from_device(lvm_t libh, const char *device)
 {
+	const char *rc = NULL;
 	struct cmd_context *cmd = (struct cmd_context *)libh;
-	return find_vgname_from_pvname(cmd, device);
+	struct saved_env e = store_user_env(cmd);
+	rc = find_vgname_from_pvname(cmd, device);
+	restore_user_env(&e);
+	return rc;
 }
 
+/*
+ * No context to work with, so no ability to save off and restore env is not
+ * available and is not needed.
+ */
 float lvm_percent_to_float(percent_t v)
 {
 	return percent_to_float(v);
liblvm/lvm_lv.c +211 -35

458 lines not on this page · raw message

liblvm/lvm_misc.c +25 -0
index ba77010..d9b4323 100644
@@ -140,3 +140,28 @@ int set_property(const pv_t pv, const vg_t vg, const lv_t lv,
 	}
 	return 0;
 }
+
+/*
+ * Store anything that may need to be restored back to the user on library
+ * call exit.  Currently the only thing we are preserving is the users umask.
+ */
+struct saved_env store_user_env(struct cmd_context *cmd)
+{
+	struct saved_env env = {0};
+
+	if (cmd) {
+		env.user_umask = umask(cmd->default_settings.umask);
+	} else {
+		env.user_umask = umask(0);
+		umask(env.user_umask);
+	}
+
+	return env;
+}
+
+void restore_user_env(const struct saved_env *env)
+{
+	if (env) {
+		umask(env->user_umask);
+	}
+}
liblvm/lvm_misc.h +12 -0
index b187021..4678338 100644
@@ -17,6 +17,18 @@
 #include "libdevmapper.h"
 #include "lvm2app.h"
 #include "metadata-exported.h"
+#include "toolcontext.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+struct saved_env
+{
+	mode_t user_umask;
+};
+
+struct saved_env store_user_env(struct cmd_context *cmd);
+void restore_user_env(const struct saved_env *env);
 
 struct dm_list *tag_list_copy(struct dm_pool *p, struct dm_list *tag_list);
 struct lvm_property_value get_property(const pv_t pv, const vg_t vg,
liblvm/lvm_pv.c +126 -24

299 lines not on this page · raw message

liblvm/lvm_vg.c +216 -76

489 lines not on this page · raw message

Signature · 1 line
1.8.2.1