Update util and fix a few cppcheck warnings.
[m6w6/libmemcached] / util / pidfile.cc
index c44b90817023101fd88e45adca25f2bc99a7d75b..c6c5b4401b64698c0b24e0269811b343cffff23c 100644 (file)
@@ -48,6 +48,7 @@
 #include <sstream>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <unistd.h>
 
 extern "C" {
 
@@ -117,8 +118,13 @@ bool Pidfile::create()
     }
   }
 
+  int oflags= O_CREAT|O_WRONLY|O_TRUNC;
+#ifdef HAVE_O_CLOEXEC
+  oflags= oflags | O_CLOEXEC;
+#endif
+
   int file;
-  if ((file = open(_filename.c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) < 0)
+  if ((file = open(_filename.c_str(), oflags, S_IRWXU|S_IRGRP|S_IROTH)) < 0)
   {
     std::stringstream error_stream;
     error_stream << "Could not open pid file for writing: " << _filename << "(" << strerror(errno) << ")";