From b06942c6049d167350529b36a6f7f559383a764f Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Thu, 13 Jan 2005 22:06:51 +0000 Subject: [PATCH] All internally used FDs are now set to O_CLOEXEC. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10715 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/storage/Directory.cpp | 25 ++++++++++++++++++++++++- src/kits/storage/Entry.cpp | 14 ++++++++++++-- src/kits/storage/File.cpp | 23 ++++++++++++++++++++++- src/kits/storage/Node.cpp | 12 ++++++++---- src/kits/storage/Query.cpp | 6 +++++- 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/src/kits/storage/Directory.cpp b/src/kits/storage/Directory.cpp index 34c7f52ee2..6f521a5a30 100644 --- a/src/kits/storage/Directory.cpp +++ b/src/kits/storage/Directory.cpp @@ -7,12 +7,13 @@ BDirectory implementation. */ -#include +#include #include #include #include #include +#include #include #include @@ -149,6 +150,7 @@ BDirectory::SetTo(const entry_ref *ref) status_t error = _SetTo(ref, true); if (error != B_OK) return error; + // open dir fDirFd = _kern_open_dir_entry_ref(ref->device, ref->directory, ref->name); if (fDirFd < 0) { @@ -156,6 +158,10 @@ BDirectory::SetTo(const entry_ref *ref) Unset(); return (fCStatus = error); } + + // set close on exec flag on dir FD + fcntl(fDirFd, F_SETFD, FD_CLOEXEC); + return B_OK; } @@ -209,10 +215,12 @@ BDirectory::SetTo(const BEntry *entry) Unset(); return (fCStatus = B_BAD_VALUE); } + // open node status_t error = _SetTo(entry->fDirFd, entry->fName, true); if (error != B_OK) return error; + // open dir fDirFd = _kern_open_dir(entry->fDirFd, entry->fName); if (fDirFd < 0) { @@ -220,6 +228,10 @@ BDirectory::SetTo(const BEntry *entry) Unset(); return (fCStatus = error); } + + // set close on exec flag on dir FD + fcntl(fDirFd, F_SETFD, FD_CLOEXEC); + return B_OK; } @@ -247,6 +259,7 @@ BDirectory::SetTo(const char *path) status_t error = _SetTo(-1, path, true); if (error != B_OK) return error; + // open dir fDirFd = _kern_open_dir(-1, path); if (fDirFd < 0) { @@ -254,6 +267,10 @@ BDirectory::SetTo(const char *path) Unset(); return (fCStatus = error); } + + // set close on exec flag on dir FD + fcntl(fDirFd, F_SETFD, FD_CLOEXEC); + return B_OK; } @@ -283,10 +300,12 @@ BDirectory::SetTo(const BDirectory *dir, const char *path) Unset(); return (fCStatus = B_BAD_VALUE); } + // open node status_t error = _SetTo(dir->fDirFd, path, true); if (error != B_OK) return error; + // open dir fDirFd = _kern_open_dir(dir->fDirFd, path); if (fDirFd < 0) { @@ -294,6 +313,10 @@ BDirectory::SetTo(const BDirectory *dir, const char *path) Unset(); return (fCStatus = error); } + + // set close on exec flag on dir FD + fcntl(fDirFd, F_SETFD, FD_CLOEXEC); + return B_OK; } diff --git a/src/kits/storage/Entry.cpp b/src/kits/storage/Entry.cpp index c6d3089024..a6b5f7cc2e 100644 --- a/src/kits/storage/Entry.cpp +++ b/src/kits/storage/Entry.cpp @@ -7,14 +7,14 @@ BEntry and entry_ref implementations. */ -#include - +#include #include #include #include #include #include +#include #include #include #include "storage_support.h" @@ -523,15 +523,21 @@ status_t BEntry::GetParent(BEntry *entry) const return B_NO_INIT; if (entry == NULL) return B_BAD_VALUE; + // check whether we are the root directory // It is sufficient to check whether our leaf name is ".". if (strcmp(fName, ".") == 0) return B_ENTRY_NOT_FOUND; + // open the parent directory char leafName[B_FILE_NAME_LENGTH]; int parentFD = _kern_open_parent_dir(fDirFd, leafName, B_FILE_NAME_LENGTH); if (parentFD < 0) return parentFD; + + // set close on exec flag on dir FD + fcntl(parentFD, F_SETFD, FD_CLOEXEC); + // init the entry entry->Unset(); entry->fDirFd = parentFD; @@ -927,6 +933,10 @@ BEntry::set(int dirFD, const char *path, bool traverse) // next round... } } + + // set close on exec flag on dir FD + fcntl(dirFD, F_SETFD, FD_CLOEXEC); + // set the result status_t error = set_name(leafName); if (error != B_OK) diff --git a/src/kits/storage/File.cpp b/src/kits/storage/File.cpp index 59fa910f85..be55c4a8d2 100644 --- a/src/kits/storage/File.cpp +++ b/src/kits/storage/File.cpp @@ -7,6 +7,7 @@ BFile implementation. */ +#include #include #include @@ -150,8 +151,12 @@ status_t BFile::SetTo(const entry_ref *ref, uint32 openMode) { Unset(); + if (!ref) return (fCStatus = B_BAD_VALUE); + + openMode |= O_CLOEXEC; + int fd = _kern_open_entry_ref(ref->device, ref->directory, ref->name, openMode); if (fd >= 0) { @@ -160,6 +165,7 @@ BFile::SetTo(const entry_ref *ref, uint32 openMode) fCStatus = B_OK; } else fCStatus = fd; + return fCStatus; } @@ -186,17 +192,22 @@ status_t BFile::SetTo(const BEntry *entry, uint32 openMode) { Unset(); + if (!entry) return (fCStatus = B_BAD_VALUE); if (entry->InitCheck() != B_OK) return (fCStatus = entry->InitCheck()); - int fd = _kern_open(entry->fDirFd, entry->fName, openMode); + + openMode |= O_CLOEXEC; + + int fd = _kern_open(entry->fDirFd, entry->fName, openMode | O_CLOEXEC); if (fd >= 0) { set_fd(fd); fMode = openMode; fCStatus = B_OK; } else fCStatus = fd; + return fCStatus; } @@ -221,8 +232,12 @@ status_t BFile::SetTo(const char *path, uint32 openMode) { Unset(); + if (!path) return (fCStatus = B_BAD_VALUE); + + openMode |= O_CLOEXEC; + int fd = _kern_open(-1, path, openMode); if (fd >= 0) { set_fd(fd); @@ -230,6 +245,7 @@ BFile::SetTo(const char *path, uint32 openMode) fCStatus = B_OK; } else fCStatus = fd; + return fCStatus; } @@ -258,8 +274,12 @@ status_t BFile::SetTo(const BDirectory *dir, const char *path, uint32 openMode) { Unset(); + if (!dir) return (fCStatus = B_BAD_VALUE); + + openMode |= O_CLOEXEC; + int fd = _kern_open(dir->fDirFd, path, openMode); if (fd >= 0) { set_fd(fd); @@ -267,6 +287,7 @@ BFile::SetTo(const BDirectory *dir, const char *path, uint32 openMode) fCStatus = B_OK; } else fCStatus = fd; + return fCStatus; } diff --git a/src/kits/storage/Node.cpp b/src/kits/storage/Node.cpp index c740acaf26..939c382928 100644 --- a/src/kits/storage/Node.cpp +++ b/src/kits/storage/Node.cpp @@ -8,6 +8,7 @@ */ #include +#include #include // for struct attr_info #include #include @@ -699,10 +700,10 @@ BNode::_SetTo(int fd, const char *path, bool traverse) status_t error = (fd >= 0 || path ? B_OK : B_BAD_VALUE); if (error == B_OK) { int traverseFlag = (traverse ? 0 : O_NOTRAVERSE); - fFd = _kern_open(fd, path, O_RDWR | traverseFlag); + fFd = _kern_open(fd, path, O_RDWR | O_CLOEXEC | traverseFlag); if (fFd < B_OK && fFd != B_ENTRY_NOT_FOUND) { // opening read-write failed, re-try read-only - fFd = _kern_open(fd, path, O_RDONLY | traverseFlag); + fFd = _kern_open(fd, path, O_RDONLY | O_CLOEXEC | traverseFlag); } if (fFd < 0) error = fFd; @@ -733,11 +734,11 @@ BNode::_SetTo(const entry_ref *ref, bool traverse) if (error == B_OK) { int traverseFlag = (traverse ? 0 : O_NOTRAVERSE); fFd = _kern_open_entry_ref(ref->device, ref->directory, ref->name, - O_RDWR | traverseFlag); + O_RDWR | O_CLOEXEC | traverseFlag); if (fFd < B_OK && fFd != B_ENTRY_NOT_FOUND) { // opening read-write failed, re-try read-only fFd = _kern_open_entry_ref(ref->device, ref->directory, ref->name, - O_RDONLY | traverseFlag); + O_RDONLY | O_CLOEXEC | traverseFlag); } if (fFd < 0) error = fFd; @@ -773,6 +774,9 @@ BNode::InitAttrDir() fAttrFd = _kern_open_attr_dir(fFd, NULL); if (fAttrFd < 0) return fAttrFd; + + // set close on exec flag + fcntl(fAttrFd, F_SETFD, FD_CLOEXEC); } return fCStatus; } diff --git a/src/kits/storage/Query.cpp b/src/kits/storage/Query.cpp index bc9286ef7e..13411bd37f 100644 --- a/src/kits/storage/Query.cpp +++ b/src/kits/storage/Query.cpp @@ -7,12 +7,13 @@ BQuery implementation. */ -#include +#include #include #include #include #include +#include #include #include @@ -527,6 +528,9 @@ BQuery::Fetch() if (fQueryFd < 0) return fQueryFd; + // set close on exec flag + fcntl(fQueryFd, F_SETFD, FD_CLOEXEC); + return B_OK; }