Respect umask when creating temporary file
[alexxy/gromacs.git] / src / gromacs / utility / futil.cpp
index 8a54e0d864093fe150f932176e41ee495ee03b9c..befd2d34bb701eb16e807e374e7863781fbae28b 100644 (file)
@@ -521,6 +521,8 @@ FilePtr openLibraryFile(const char* filename, bool bAddCWD, bool bFatal)
 /*! \brief Use mkstemp (or similar function to make a new temporary
  * file and (on non-Windows systems) return a file descriptor to it.
  *
+ * Note: not thread-safe on non-Windows systems
+ *
  * \todo Use std::string and std::vector<char>. */
 static int makeTemporaryFilename(char* buf)
 {
@@ -548,6 +550,11 @@ static int makeTemporaryFilename(char* buf)
 #else
     int fd = mkstemp(buf);
 
+    /* mkstemp creates 0600 files - respect umask instead */
+    mode_t currUmask = umask(0);
+    umask(currUmask);
+    fchmod(fd, 0666 & ~currUmask);
+
     if (fd < 0)
     {
         gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf, strerror(errno));