Re-factor the code from networkEnableIpForwarding() into a
utility function in preparation for code which writes to
sysfs files.
Signed-off-by: Mark McLoughlin <markmc(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/network_driver.c | 26 ++------------------------
src/util.c | 24 ++++++++++++++++++++++++
src/util.h | 2 ++
4 files changed, 29 insertions(+), 24 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4338da7..9e9b3e5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -279,6 +279,7 @@ virCondBroadcast;
# util.h
virFileReadAll;
+virFileWriteStr;
virStrToLong_i;
virStrToLong_ll;
virStrToLong_ull;
diff --git a/src/network_driver.c b/src/network_driver.c
index 4138939..b256e3d 100644
--- a/src/network_driver.c
+++ b/src/network_driver.c
@@ -794,33 +794,11 @@ networkRemoveIptablesRules(struct network_driver *driver,
iptablesSaveRules(driver->iptables);
}
-/* Enable IP Forwarding.
- Return 0 for success, nonzero for failure.
- Be careful to preserve any errno value upon failure. */
+/* Enable IP Forwarding. Return 0 for success, nonzero for failure. */
static int
networkEnableIpForwarding(void)
{
-#define PROC_IP_FORWARD "/proc/sys/net/ipv4/ip_forward"
-
- int fd;
-
- if ((fd = open(PROC_IP_FORWARD, O_WRONLY|O_TRUNC)) == -1)
- return 0;
-
- if (safewrite(fd, "1\n", 2) < 0) {
- int saved_errno = errno;
- close (fd);
- errno = saved_errno;
- return 0;
- }
-
- /* Use errno from failed close only if there was no write error. */
- if (close (fd) != 0)
- return 0;
-
- return 1;
-
-#undef PROC_IP_FORWARD
+ return virFileWriteStr("/proc/sys/net/ipv4/ip_forward", "1\n");
}
static int networkStartNetworkDaemon(virConnectPtr conn,
diff --git a/src/util.c b/src/util.c
index 01fe37a..990433a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -774,6 +774,30 @@ int virFileReadAll(const char *path, int maxlen, char **buf)
return len;
}
+/* Truncate @path and write @str to it.
+ Return 0 for success, nonzero for failure.
+ Be careful to preserve any errno value upon failure. */
+int virFileWriteStr(const char *path, const char *str)
+{
+ int fd;
+
+ if ((fd = open(path, O_WRONLY|O_TRUNC)) == -1)
+ return -1;
+
+ if (safewrite(fd, str, strlen(str)) < 0) {
+ int saved_errno = errno;
+ close (fd);
+ errno = saved_errno;
+ return -1;
+ }
+
+ /* Use errno from failed close only if there was no write error. */
+ if (close (fd) != 0)
+ return -1;
+
+ return 0;
+}
+
int virFileMatchesNameSuffix(const char *file,
const char *name,
const char *suffix)
diff --git a/src/util.h b/src/util.h
index 4667b92..a79cfa7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -56,6 +56,8 @@ int virFileReadLimFD(int fd, int maxlen, char **buf);
int virFileReadAll(const char *path, int maxlen, char **buf);
+int virFileWriteStr(const char *path, const char *str);
+
int virFileMatchesNameSuffix(const char *file,
const char *name,
const char *suffix);
--
1.6.0.6