]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mon/MonitorStore.cc: use safe_read
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 10 Feb 2011 16:24:43 +0000 (08:24 -0800)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 10 Feb 2011 17:16:22 +0000 (09:16 -0800)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/mon/MonitorStore.cc

index 2d8753ad65e7c6d99f19f09f6f2dd61060f8fc1c..4f8ed6b06f6dffa0da747a2f1148878374537f35 100644 (file)
@@ -141,13 +141,27 @@ version_t MonitorStore::get_int(const char *a, const char *b)
     snprintf(fn, sizeof(fn), "%s/%s", dir.c_str(), a);
   
   int fd = ::open(fn, O_RDONLY);
-  if (fd < 0)
+  if (fd < 0) {
+    int err = errno;
+    if (err == EEXIST) {
+      // Non-existent files are treated as containing 0.
+      return 0;
+    }
+    derr << "MonitorStore::get_int: failed to open '" << fn << "': "
+        << cpp_strerror(err) << dendl;
     return 0;
+  }
   
   char buf[20];
-  int r = ::read(fd, buf, sizeof(buf));
-  assert(r >= 0);
-  ::close(fd);
+  memset(buf, 0, sizeof(buf));
+  int r = safe_read(fd, buf, sizeof(buf) - 1);
+  if (r < 0) {
+    derr << "MonitorStore::get_int: failed to read '" << fn << "': "
+        << cpp_strerror(r) << dendl;
+    TEMP_FAILURE_RETRY(::close(fd));
+    return 0;
+  }
+  TEMP_FAILURE_RETRY(::close(fd));
   
   version_t val = atoi(buf);