Merge in libtest updates.
[awesomized/libmemcached] / libtest / killpid.cc
index bc9a55941d7b8e5817395cc19a34cea9a0ef1c83..1d5992db175cd486b2897de1c7f10ce85155c6b1 100644 (file)
@@ -99,12 +99,13 @@ bool kill_pid(pid_t pid_arg)
 }
 
 
-void kill_file(const std::string &filename)
+pid_t kill_file(const std::string &filename)
 {
+  pid_t ret= -1;
   FILE *fp;
 
   if (filename.empty())
-    return;
+    return ret;
 
   if ((fp= fopen(filename.c_str(), "r")))
   {
@@ -123,4 +124,37 @@ void kill_file(const std::string &filename)
       }
     }
   }
+  
+  return ret;
+}
+
+pid_t get_pid_from_file(const std::string &filename)
+{
+  pid_t ret= -1;
+  FILE *fp;
+
+  if (filename.empty())
+  {
+    Error << "empty pid file";
+    return ret;
+  }
+
+  if ((fp= fopen(filename.c_str(), "r")))
+  {
+    char pid_buffer[1024];
+
+    char *ptr= fgets(pid_buffer, sizeof(pid_buffer), fp);
+    fclose(fp);
+
+    if (ptr)
+    {
+      ret= (pid_t)atoi(pid_buffer);
+      if (ret <= 0)
+      {
+        return ret;
+      }
+    }
+  }
+  
+  return ret;
 }