forked from Mirrors/openclonk
linux: Add O_CLOEXEC to various open calls
This should prevent some file descriptor leakage from forks that some libraries we use do.
parent
4bc309122d
commit
b4fd8fcd15
|
@ -69,34 +69,27 @@ bool CStdFile::Create(const char *szFilename, bool fCompressed, bool fExecutable
|
|||
MemoryPtr = 0;
|
||||
}
|
||||
// Open standard file
|
||||
else if (fCompressed)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
int mode = _S_IREAD|_S_IWRITE;
|
||||
int flags = _O_BINARY|_O_CREAT|_O_WRONLY|_O_TRUNC;
|
||||
int fd = _wopen(GetWideChar(Name), flags, mode);
|
||||
#else
|
||||
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
|
||||
int flags = O_CREAT|O_WRONLY|O_TRUNC;
|
||||
int fd = open(Name, flags, mode);
|
||||
#endif
|
||||
if (!(hgzFile = c4_gzdopen(fd,"wb1"))) return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
int flags = _O_BINARY|O_CLOEXEC|O_CREAT|O_WRONLY|O_TRUNC;
|
||||
#ifdef _WIN32
|
||||
int mode = _S_IREAD|_S_IWRITE;
|
||||
int flags = _O_BINARY|_O_CREAT|_O_WRONLY|_O_TRUNC;
|
||||
int fd = _wopen(GetWideChar(Name), flags, mode);
|
||||
#else
|
||||
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
|
||||
if (fExecutable)
|
||||
mode |= S_IXUSR|S_IXGRP|S_IXOTH;
|
||||
int flags = O_CREAT|O_WRONLY|O_TRUNC;
|
||||
int fd = open(Name, flags, mode);
|
||||
#endif
|
||||
if (fd == -1) return false;
|
||||
if (!(hFile = fdopen(fd,"wb"))) return false;
|
||||
if (fCompressed)
|
||||
{
|
||||
if (!(hgzFile = c4_gzdopen(fd,"wb1"))) return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(hFile = fdopen(fd,"wb"))) return false;
|
||||
}
|
||||
}
|
||||
// Reset buffer
|
||||
ClearBuffer();
|
||||
|
@ -111,22 +104,18 @@ bool CStdFile::Open(const char *szFilename, bool fCompressed)
|
|||
thread_check.Set();
|
||||
// Set modes
|
||||
ModeWrite=false;
|
||||
// Open standard file
|
||||
int flags = _O_BINARY|O_CLOEXEC|O_RDONLY;
|
||||
#ifdef _WIN32
|
||||
int mode = _S_IREAD|_S_IWRITE;
|
||||
int fd = _wopen(GetWideChar(Name), flags, mode);
|
||||
#else
|
||||
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
|
||||
int fd = open(Name, flags, mode);
|
||||
#endif
|
||||
if(fd == -1) return false;
|
||||
if (fCompressed)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
int mode = _S_IREAD|_S_IWRITE;
|
||||
int flags = _O_BINARY|_O_RDONLY;
|
||||
int fd = _wopen(GetWideChar(Name), flags, mode);
|
||||
#else
|
||||
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
|
||||
int flags = O_RDONLY;
|
||||
int fd = open(Name, flags, mode);
|
||||
#endif
|
||||
|
||||
if(fd == -1) return false;
|
||||
if (!(hgzFile = c4_gzdopen(fd,"rb"))) { close(fd); return false; }
|
||||
|
||||
/* Reject uncompressed files */
|
||||
if(c4_gzdirect(hgzFile))
|
||||
{
|
||||
|
@ -137,11 +126,7 @@ bool CStdFile::Open(const char *szFilename, bool fCompressed)
|
|||
}
|
||||
else
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if (!(hFile=_wfopen(GetWideChar(Name),L"rb"))) return false;
|
||||
#else
|
||||
if (!(hFile=fopen(Name,"rb"))) return false;
|
||||
#endif
|
||||
if (!(hFile = fdopen(fd,"rb"))) return false;
|
||||
}
|
||||
// Reset buffer
|
||||
ClearBuffer();
|
||||
|
@ -330,13 +315,12 @@ int UncompressedFileSize(const char *szFilename)
|
|||
{
|
||||
int rd,rval=0;
|
||||
BYTE buf[1024];
|
||||
int flags = _O_BINARY|O_CLOEXEC|O_RDONLY;
|
||||
#ifdef _WIN32
|
||||
int mode = _S_IREAD|_S_IWRITE;
|
||||
int flags = _O_BINARY|_O_RDONLY;
|
||||
int fd = _wopen(GetWideChar(szFilename), flags, mode);
|
||||
#else
|
||||
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
|
||||
int flags = O_RDONLY;
|
||||
int fd = open(szFilename, flags, mode);
|
||||
#endif
|
||||
gzFile hFile;
|
||||
|
|
|
@ -49,7 +49,7 @@ bool StdBuf::LoadFromFile(const char *szFile)
|
|||
#ifdef _WIN32
|
||||
int fh = _wopen(::GetWideChar(szFile), O_BINARY | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
||||
#else
|
||||
int fh = open(szFile, O_BINARY | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
||||
int fh = open(szFile, O_BINARY | O_CLOEXEC | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
||||
#endif
|
||||
if (fh < 0) return false;
|
||||
// Create buf
|
||||
|
@ -70,7 +70,7 @@ bool StdBuf::SaveToFile(const char *szFile) const
|
|||
#ifdef _WIN32
|
||||
int fh = _wopen(::GetWideChar(szFile), O_BINARY | O_CREAT | O_WRONLY | O_SEQUENTIAL | O_TRUNC, S_IREAD | S_IWRITE);
|
||||
#else
|
||||
int fh = open(szFile, O_BINARY | O_CREAT | O_WRONLY | O_SEQUENTIAL | O_TRUNC, S_IREAD | S_IWRITE);
|
||||
int fh = open(szFile, O_BINARY | O_CLOEXEC | O_CREAT | O_WRONLY | O_SEQUENTIAL | O_TRUNC, S_IREAD | S_IWRITE);
|
||||
#endif
|
||||
if (fh < 0) return false;
|
||||
// Write data
|
||||
|
@ -90,7 +90,7 @@ bool StdStrBuf::LoadFromFile(const char *szFile)
|
|||
#ifdef _WIN32
|
||||
int fh = _wopen(::GetWideChar(szFile), O_BINARY | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
||||
#else
|
||||
int fh = open(szFile, O_BINARY | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
||||
int fh = open(szFile, O_BINARY | O_CLOEXEC | O_RDONLY | O_SEQUENTIAL, S_IREAD | S_IWRITE);
|
||||
#endif
|
||||
if (fh < 0) return false;
|
||||
// Create buf
|
||||
|
@ -111,7 +111,7 @@ bool StdStrBuf::SaveToFile(const char *szFile) const
|
|||
#ifdef _WIN32
|
||||
int fh = _wopen(::GetWideChar(szFile), O_BINARY | O_CREAT | O_WRONLY | O_SEQUENTIAL | O_TRUNC, S_IREAD | S_IWRITE);
|
||||
#else
|
||||
int fh = open(szFile, O_BINARY | O_CREAT | O_WRONLY | O_SEQUENTIAL | O_TRUNC, S_IREAD | S_IWRITE);
|
||||
int fh = open(szFile, O_BINARY | O_CLOEXEC | O_CREAT | O_WRONLY | O_SEQUENTIAL | O_TRUNC, S_IREAD | S_IWRITE);
|
||||
#endif
|
||||
if (fh < 0) return false;
|
||||
// Write data
|
||||
|
|
|
@ -1008,7 +1008,7 @@ int32_t C4Network2Res::OpenFileRead()
|
|||
#ifdef _WIN32
|
||||
return _wopen(GetWideChar(szStandalone), _O_BINARY | O_RDONLY);
|
||||
#else
|
||||
return open(szStandalone, _O_BINARY | O_RDONLY);
|
||||
return open(szStandalone, _O_BINARY | O_CLOEXEC | O_RDONLY);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -1019,7 +1019,7 @@ int32_t C4Network2Res::OpenFileWrite()
|
|||
#ifdef _WIN32
|
||||
return _wopen(GetWideChar(szStandalone), _O_BINARY | O_CREAT | O_WRONLY, S_IREAD | S_IWRITE);
|
||||
#else
|
||||
return open(szStandalone, _O_BINARY | O_CREAT | O_WRONLY, S_IREAD | S_IWRITE);
|
||||
return open(szStandalone, _O_BINARY | O_CLOEXEC | O_CREAT | O_WRONLY, S_IREAD | S_IWRITE);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -248,4 +248,32 @@ inline int swprintf(wchar_t* buffer, size_t n, const wchar_t* format, ...)
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#define F_OK 0
|
||||
#else
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
#define _O_BINARY 0
|
||||
#define _MAX_PATH PATH_MAX
|
||||
#define _MAX_FNAME NAME_MAX
|
||||
|
||||
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists);
|
||||
#endif
|
||||
|
||||
#include <fcntl.h>
|
||||
#ifndef O_CLOEXEC
|
||||
#define O_CLOEXEC 0
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#define DirSep "\\"
|
||||
#define DirectorySeparator '\\'
|
||||
#define AltDirectorySeparator '/'
|
||||
#else
|
||||
#define DirSep "/"
|
||||
#define DirectorySeparator '/'
|
||||
#define AltDirectorySeparator '\\'
|
||||
#endif
|
||||
|
||||
#endif // INC_PLATFORMABSTRACTION
|
||||
|
|
|
@ -547,10 +547,10 @@ bool EraseFile(const char *szFilename)
|
|||
#ifndef _WIN32
|
||||
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
|
||||
{
|
||||
int fds = open (szSource, O_RDONLY);
|
||||
int fds = open (szSource, O_RDONLY | O_CLOEXEC);
|
||||
if (!fds) return false;
|
||||
struct stat info; fstat(fds, &info);
|
||||
int fdt = open (szTarget, O_WRONLY | O_CREAT | (FailIfExists? O_EXCL : O_TRUNC), info.st_mode);
|
||||
int fdt = open (szTarget, O_CLOEXEC | O_WRONLY | O_CREAT | (FailIfExists? O_EXCL : O_TRUNC), info.st_mode);
|
||||
if (!fdt)
|
||||
{
|
||||
close (fds);
|
||||
|
|
|
@ -25,34 +25,6 @@
|
|||
#ifndef STDFILE_INCLUDED
|
||||
#define STDFILE_INCLUDED
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#define F_OK 0
|
||||
#else
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
#define _O_BINARY 0
|
||||
#define _MAX_PATH PATH_MAX
|
||||
#define _MAX_FNAME NAME_MAX
|
||||
|
||||
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists);
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#define DirSep "\\"
|
||||
#define DirectorySeparator '\\'
|
||||
#define AltDirectorySeparator '/'
|
||||
#else
|
||||
#define DirSep "/"
|
||||
#define DirectorySeparator '/'
|
||||
#define AltDirectorySeparator '\\'
|
||||
#endif
|
||||
|
||||
/** Create a directory and all of its parents.
|
||||
* \p[in] path Directory to create
|
||||
* \returns true on success, false otherwise.
|
||||
|
|
Loading…
Reference in New Issue