CLOEXEC support in fdopen, freopen, and mkostemp/mkostemps.

Change-Id: I74ea88e0d4973d6ab3c57da7d8bb643c31592b14
This commit is contained in:
Elliott Hughes
2014-09-23 17:34:29 -07:00
parent 87b6906f6e
commit 31165edf57
11 changed files with 145 additions and 41 deletions

View File

@@ -17,24 +17,26 @@
#include <fcntl.h>
#include <unistd.h>
template<int (*mk_fn)(char*)>
#include "private/bionic_macros.h"
template <typename T = int (*)(char*)>
class GenericTemporaryFile {
public:
GenericTemporaryFile(const char* dirpath = NULL) {
if (dirpath != NULL) {
init(dirpath);
} else {
// Since we might be running on the host or the target, and if we're
// running on the host we might be running under bionic or glibc,
// let's just try both possible temporary directories and take the
// first one that works.
init("/data/local/tmp");
if (fd == -1) {
init("/tmp");
}
GenericTemporaryFile(T mk_fn = mkstemp) : mk_fn(mk_fn) {
// Since we might be running on the host or the target, and if we're
// running on the host we might be running under bionic or glibc,
// let's just try both possible temporary directories and take the
// first one that works.
init("/data/local/tmp");
if (fd == -1) {
init("/tmp");
}
}
GenericTemporaryFile(const char* dirpath, T mk_fn = mkstemp) : mk_fn(mk_fn) {
init(dirpath);
}
~GenericTemporaryFile() {
close(fd);
unlink(filename);
@@ -49,13 +51,17 @@ class GenericTemporaryFile {
char filename[1024];
private:
T mk_fn;
void init(const char* tmp_dir) {
snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir);
fd = mk_fn(filename);
}
DISALLOW_COPY_AND_ASSIGN(GenericTemporaryFile);
};
typedef GenericTemporaryFile<mkstemp> TemporaryFile;
typedef GenericTemporaryFile<> TemporaryFile;
class TemporaryDir {
public:
@@ -77,4 +83,5 @@ class TemporaryDir {
return (mkdtemp(dirname) != NULL);
}
DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
};