[PATCH 3/4] virpidfile: replace fopen/fwrite/fscanf with more portable version

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Replace calls to fwrite() and fscanf() with more portable-friendly
version, such as snprintf() and virStrToLong().
---
 src/util/virpidfile.c |   42 +++++++++++++++++++++++++-----------------
 1 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/src/util/virpidfile.c b/src/util/virpidfile.c
index f2532d4..bbcdd53 100644
--- a/src/util/virpidfile.c
+++ b/src/util/virpidfile.c
@@ -54,7 +54,7 @@ int virPidFileWritePath(const char *pidfile,
 {
     int rc;
     int fd;
-    FILE *file = NULL;
+    char pidstr[INT_BUFSIZE_BOUND(pid)];
 
     if ((fd = open(pidfile,
                    O_WRONLY | O_CREAT | O_TRUNC,
@@ -63,21 +63,18 @@ int virPidFileWritePath(const char *pidfile,
         goto cleanup;
     }
 
-    if (!(file = VIR_FDOPEN(fd, "w"))) {
-        rc = -errno;
-        VIR_FORCE_CLOSE(fd);
-        goto cleanup;
-    }
+    snprintf(pidstr, sizeof(pidstr), "%" PID_FORMAT, pid);
 
-    if (fprintf(file, "%" PID_FORMAT, pid) < 0) {
+    if (safewrite(fd, pidstr, strlen(pidstr)) < 0) {
         rc = -errno;
+        VIR_FORCE_CLOSE(fd);
         goto cleanup;
     }
 
     rc = 0;
 
 cleanup:
-    if (VIR_FCLOSE(file) < 0)
+    if (VIR_CLOSE(fd) < 0)
         rc = -errno;
 
     return rc;
@@ -117,30 +114,41 @@ cleanup:
 int virPidFileReadPath(const char *path,
                        pid_t *pid)
 {
-    FILE *file;
+    int fd;
     int rc;
+    ssize_t bytes;
+    char pidstr[INT_BUFSIZE_BOUND(*pid)];
 
     *pid = 0;
 
-    if (!(file = fopen(path, "r"))) {
+    if ((fd = open(path, O_RDONLY)) < 0) {
         rc = -errno;
         goto cleanup;
     }
 
-    if (fscanf(file, "%" PID_FORMAT, pid) != 1) {
-        rc = -EINVAL;
-        VIR_FORCE_FCLOSE(file);
+    bytes = saferead(fd, pidstr, sizeof(pidstr));
+    if (bytes < 0) {
+        rc = -errno;
+        VIR_FORCE_CLOSE(fd);
         goto cleanup;
     }
+    pidstr[bytes] = '\0';
 
-    if (VIR_FCLOSE(file) < 0) {
-        rc = -errno;
+#if SIZEOF_PID_T == 8
+    if (virStrToLong_ll(pidstr, NULL, 10, pid) < 0) {
+#elif SIZEOF_PID_T == 4
+    if (virStrToLong_i(pidstr, NULL, 10, pid) < 0) {
+#endif
+        rc = -1;
         goto cleanup;
     }
 
     rc = 0;
 
- cleanup:
+cleanup:
+    if (VIR_CLOSE(fd) < 0)
+        rc = -errno;
+
     return rc;
 }
 
@@ -367,7 +375,7 @@ int virPidFileAcquirePath(const char *path,
         /* Someone else must be racing with us, so try agin */
     }
 
-    snprintf(pidstr, sizeof(pidstr), "%u", (unsigned int)pid);
+    snprintf(pidstr, sizeof(pidstr), "%" PID_FORMAT, pid);
 
     if (safewrite(fd, pidstr, strlen(pidstr)) < 0) {
         virReportSystemError(errno,
-- 
1.7.7.6

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]