Use O_CLOEXEC when possible

This avoids having to do a fcntl(fd, F_SETFD, FD_CLOEXEC) to set the “close on exec” flag.
This commit is contained in:
Allan Odgaard
2012-08-23 20:25:17 +02:00
parent 16b3fcecd7
commit f5c7301ba7
2 changed files with 3 additions and 6 deletions

View File

@@ -209,13 +209,10 @@ namespace document
void watch_server_t::observe (watch_info_t& info, size_t client_id)
{
info.path_watched = existing_parent(info.path);
info.fd = open(info.path_watched.c_str(), O_EVTONLY, 0);
if(info.fd == -1)
info.fd = open(info.path_watched.c_str(), O_EVTONLY|O_CLOEXEC, 0);
if(info.fd == -1) // TODO we need to actually handle this error @allan
fprintf(stderr, "error observing path, open(\"%s\"): %s\n", info.path_watched.c_str(), strerror(errno));
// TODO we need to actually handle this error @allan
fcntl(info.fd, F_SETFD, 1);
struct kevent changeList;
struct timespec timeout = { };
EV_SET(&changeList, info.fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, NOTE_DELETE | NOTE_WRITE | NOTE_RENAME | NOTE_ATTRIB, 0, (void*)client_id);

View File

@@ -161,7 +161,7 @@ struct track_paths_t
private:
static int open_file (std::string const& path, bool* exists)
{
int fd = open(path.c_str(), O_EVTONLY/*|O_CLOEXEC*/, 0);
int fd = open(path.c_str(), O_EVTONLY|O_CLOEXEC, 0);
return fd == -1 && errno == ENOENT ? (*exists = false), open_file(path::parent(path), exists) : fd;
}