about summary refs log tree commit diff
path: root/src/NetworkManagerUtils.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2013-02-09 03:16:54 +0100
committerMichael Biebl <biebl@debian.org>2013-02-09 03:16:54 +0100
commite17c5736fc3722ce51cb33f3edb203960125a4c1 (patch)
tree2b41ee92b307915e9d97f0ef2c012fc793653599 /src/NetworkManagerUtils.c
parent9c202e3e860b3be9e1f8882630b69affbb130102 (diff)
Imported Upstream version 0.9.7.997 upstream/0.9.7.997
Diffstat (limited to 'src/NetworkManagerUtils.c')
-rw-r--r--src/NetworkManagerUtils.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c
index db627091..f2d72bd1 100644
--- a/src/NetworkManagerUtils.c
+++ b/src/NetworkManagerUtils.c
@@ -607,20 +607,30 @@ value_hash_add_object_property (GHashTable *hash,
 	value_hash_add (hash, key, value);
 }
 
+/**
+ * nm_utils_do_sysctl:
+ * @path: path to write @value to
+ * @value: value to write to @path
+ *
+ * Writes @value to the file at @path, trying 3 times on failure.
+ *
+ * Returns: %TRUE on success.  On failure, returns %FALSE and sets errno.
+ */
 gboolean
 nm_utils_do_sysctl (const char *path, const char *value)
 {
-	int fd, len, nwrote, tries;
+	int fd, len, nwrote, tries, saved_errno = 0;
 	char *actual;
 
 	g_return_val_if_fail (path != NULL, FALSE);
 	g_return_val_if_fail (value != NULL, FALSE);
-	g_return_val_if_fail (value[0], FALSE);
 
 	fd = open (path, O_WRONLY | O_TRUNC);
 	if (fd == -1) {
+		saved_errno = errno;
 		nm_log_warn (LOGD_CORE, "sysctl: failed to open '%s': (%d) %s",
-		             path, errno, strerror (errno));
+		             path, saved_errno, strerror (saved_errno));
+		errno = saved_errno;
 		return FALSE;
 	}
 
@@ -636,21 +646,24 @@ nm_utils_do_sysctl (const char *path, const char *value)
 	/* Try to write the entire value three times if a partial write occurs */
 	len = strlen (actual);
 	for (tries = 0, nwrote = 0; tries < 3 && nwrote != len; tries++) {
+		errno = 0;
 		nwrote = write (fd, actual, len);
 		if (nwrote == -1) {
 			if (errno == EINTR)
 				continue;
+			saved_errno = errno;
 			break;
 		}
 	}
 	g_free (actual);
+	close (fd);
 
-	if (nwrote != len) {
+	if (nwrote != len && saved_errno != EEXIST) {
 		nm_log_warn (LOGD_CORE, "sysctl: failed to set '%s' to '%s': (%d) %s",
-		             path, value, errno, strerror (errno));
+		             path, value, saved_errno, strerror (saved_errno));
 	}
 
-	close (fd);
+	errno = saved_errno;
 	return (nwrote == len);
 }