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
This commit is contained in:
Ingo Weinhold
2005-01-13 22:06:51 +00:00
parent 49057561f5
commit b06942c604
5 changed files with 71 additions and 9 deletions
+24 -1
View File
@@ -7,12 +7,13 @@
BDirectory implementation.
*/
#include <fs_info.h>
#include <fcntl.h>
#include <string.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <fs_info.h>
#include <Path.h>
#include <SymLink.h>
@@ -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;
}
+12 -2
View File
@@ -7,14 +7,14 @@
BEntry and entry_ref implementations.
*/
#include <Entry.h>
#include <fcntl.h>
#include <new>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <Directory.h>
#include <Entry.h>
#include <Path.h>
#include <SymLink.h>
#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)
+22 -1
View File
@@ -7,6 +7,7 @@
BFile implementation.
*/
#include <fcntl.h>
#include <Directory.h>
#include <Entry.h>
@@ -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;
}
+8 -4
View File
@@ -8,6 +8,7 @@
*/
#include <errno.h>
#include <fcntl.h>
#include <fs_attr.h> // for struct attr_info
#include <new>
#include <string.h>
@@ -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;
}
+5 -1
View File
@@ -7,12 +7,13 @@
BQuery implementation.
*/
#include <fs_query.h>
#include <fcntl.h>
#include <new>
#include <parsedate.h>
#include <time.h>
#include <Entry.h>
#include <fs_query.h>
#include <Query.h>
#include <Volume.h>
@@ -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;
}