]> xenbits.xensource.com Git - libvirt.git/commitdiff
virpidfile: replace fopen/fwrite/fscanf with more portable version
authorMarc-André Lureau <marcandre.lureau@gmail.com>
Thu, 2 Feb 2012 00:28:30 +0000 (01:28 +0100)
committerEric Blake <eblake@redhat.com>
Fri, 10 Feb 2012 23:34:46 +0000 (16:34 -0700)
Replace calls to fwrite() and fscanf() with more portable-friendly
version, such as snprintf() and virStrToLong().

src/util/virpidfile.c

index 1fd6318ede71b1629402c56814884d013b0a413a..34d125000c1493ba3993bb3ed4ef1277ca3e84a8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * virpidfile.c: manipulation of pidfiles
  *
- * Copyright (C) 2010-2011 Red Hat, Inc.
+ * Copyright (C) 2010-2012 Red Hat, Inc.
  * Copyright (C) 2006, 2007 Binary Karma
  * Copyright (C) 2006 Shuveb Hussain
  *
@@ -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), "%lld", (long long) pid);
 
-    if (fprintf(file, "%d", 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,40 @@ cleanup:
 int virPidFileReadPath(const char *path,
                        pid_t *pid)
 {
-    FILE *file;
+    int fd;
     int rc;
+    ssize_t bytes;
+    long long pid_value = 0;
+    char pidstr[INT_BUFSIZE_BOUND(pid_value)];
 
     *pid = 0;
 
-    if (!(file = fopen(path, "r"))) {
+    if ((fd = open(path, O_RDONLY)) < 0) {
         rc = -errno;
         goto cleanup;
     }
 
-    if (fscanf(file, "%d", 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 (virStrToLong_ll(pidstr, NULL, 10, &pid_value) < 0 ||
+        (pid_t) pid_value != pid_value) {
+        rc = -1;
         goto cleanup;
     }
 
+    *pid = pid_value;
     rc = 0;
 
- cleanup:
+cleanup:
+    if (VIR_CLOSE(fd) < 0)
+        rc = -errno;
+
     return rc;
 }
 
@@ -367,7 +374,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), "%lld", (long long) pid);
 
     if (safewrite(fd, pidstr, strlen(pidstr)) < 0) {
         virReportSystemError(errno,