]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virFileWriteStr()
authorMark McLoughlin <markmc@redhat.com>
Fri, 13 Feb 2009 19:13:06 +0000 (19:13 +0000)
committerMark McLoughlin <markmc@redhat.com>
Fri, 13 Feb 2009 19:13:06 +0000 (19:13 +0000)
Re-factor the code from networkEnableIpForwarding() into a
utility function in preparation for code which writes to
sysfs files.

ChangeLog
src/libvirt_private.syms
src/network_driver.c
src/util.c
src/util.h

index 6845ec172ff507bb4f45bf390a8c7c96ea0fbb18..6aae1c153408bf7d5cd96874708203afa525f750 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Fri Feb 13 19:04:57 IST 2009 Mark McLoughlin <markmc@redhat.com>
+
+       * src/libvirt_private.syms, src/util.[ch]: add
+       virFileWriteStr()
+
+       * src/network_driver.c: use it here
+
 Fri Feb 13 19:04:45 IST 2009 Mark McLoughlin <markmc@redhat.com>
 
        * src/xml.c: fix some error strings
index 4338da7e4b6990978bdd0db0685aa637687501c4..9e9b3e518751b62c612820e6315a95b166e47179 100644 (file)
@@ -279,6 +279,7 @@ virCondBroadcast;
 
 # util.h
 virFileReadAll;
+virFileWriteStr;
 virStrToLong_i;
 virStrToLong_ll;
 virStrToLong_ull;
index 4138939112e58d6986f866eb70da1413ea051566..b256e3d0256852c4711c18c56a1a8408f4acc7f5 100644 (file)
@@ -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,
index 01fe37a6c007e493bb6c40b78f71a8180538872b..990433a27573957aafdf5b3739feeccda960071d 100644 (file)
@@ -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)
index 4667b92e2fd6ffbdc72d37aa2fea7d35b238f46c..a79cfa7c772233950d19e7fb2b3776a0565876bf 100644 (file)
@@ -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);