* Replaced all "status < B_OK" with != B_OK - this should make the VFS layer
more robust against broken (userland) file systems. * 80 character column cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32184 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+44
-40
@@ -1,10 +1,12 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2008, Axel Dörfler, [email protected].
|
* Copyright 2002-2009, Axel Dörfler, [email protected].
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
//! Operations on file descriptors
|
//! Operations on file descriptors
|
||||||
|
|
||||||
|
|
||||||
#include <fd.h>
|
#include <fd.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -83,7 +85,7 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*** General fd routines ***/
|
// #pragma mark - General fd routines
|
||||||
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
@@ -92,15 +94,17 @@ void dump_fd(int fd, struct file_descriptor *descriptor);
|
|||||||
void
|
void
|
||||||
dump_fd(int fd,struct file_descriptor* descriptor)
|
dump_fd(int fd,struct file_descriptor* descriptor)
|
||||||
{
|
{
|
||||||
dprintf("fd[%d] = %p: type = %ld, ref_count = %ld, ops = %p, u.vnode = %p, u.mount = %p, cookie = %p, open_mode = %lx, pos = %Ld\n",
|
dprintf("fd[%d] = %p: type = %ld, ref_count = %ld, ops = %p, u.vnode = %p, "
|
||||||
fd, descriptor, descriptor->type, descriptor->ref_count, descriptor->ops,
|
"u.mount = %p, cookie = %p, open_mode = %lx, pos = %Ld\n",
|
||||||
descriptor->u.vnode, descriptor->u.mount, descriptor->cookie, descriptor->open_mode, descriptor->pos);
|
fd, descriptor, descriptor->type, descriptor->ref_count,
|
||||||
|
descriptor->ops, descriptor->u.vnode, descriptor->u.mount,
|
||||||
|
descriptor->cookie, descriptor->open_mode, descriptor->pos);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/** Allocates and initializes a new file_descriptor */
|
/*! Allocates and initializes a new file_descriptor.
|
||||||
|
*/
|
||||||
struct file_descriptor*
|
struct file_descriptor*
|
||||||
alloc_fd(void)
|
alloc_fd(void)
|
||||||
{
|
{
|
||||||
@@ -137,12 +141,12 @@ fd_set_close_on_exec(struct io_context *context, int fd, bool closeFD)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Searches a free slot in the FD table of the provided I/O context, and inserts
|
/*! Searches a free slot in the FD table of the provided I/O context, and
|
||||||
* the specified descriptor into it.
|
inserts the specified descriptor into it.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
new_fd_etc(struct io_context *context, struct file_descriptor *descriptor, int firstIndex)
|
new_fd_etc(struct io_context* context, struct file_descriptor* descriptor,
|
||||||
|
int firstIndex)
|
||||||
{
|
{
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
uint32 i;
|
uint32 i;
|
||||||
@@ -178,10 +182,9 @@ new_fd(struct io_context *context, struct file_descriptor *descriptor)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Reduces the descriptor's reference counter, and frees all resources
|
/*! Reduces the descriptor's reference counter, and frees all resources
|
||||||
* when it's no longer used.
|
when it's no longer used.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void
|
||||||
put_fd(struct file_descriptor* descriptor)
|
put_fd(struct file_descriptor* descriptor)
|
||||||
{
|
{
|
||||||
@@ -221,10 +224,9 @@ put_fd(struct file_descriptor *descriptor)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Decrements the open counter of the file descriptor and invokes
|
/*! Decrements the open counter of the file descriptor and invokes
|
||||||
* its close hook when appropriate.
|
its close hook when appropriate.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void
|
||||||
close_fd(struct file_descriptor* descriptor)
|
close_fd(struct file_descriptor* descriptor)
|
||||||
{
|
{
|
||||||
@@ -253,13 +255,12 @@ close_fd_index(struct io_context *context, int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** This descriptor's underlying object will be closed and freed
|
/*! This descriptor's underlying object will be closed and freed as soon as
|
||||||
* as soon as possible (in one of the next calls to put_fd() -
|
possible (in one of the next calls to put_fd() - get_fd() will no longer
|
||||||
* get_fd() will no longer succeed on this descriptor).
|
succeed on this descriptor).
|
||||||
* This is useful if the underlying object is gone, for instance
|
This is useful if the underlying object is gone, for instance when a
|
||||||
* when a (mounted) volume got removed unexpectedly.
|
(mounted) volume got removed unexpectedly.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void
|
||||||
disconnect_fd(struct file_descriptor* descriptor)
|
disconnect_fd(struct file_descriptor* descriptor)
|
||||||
{
|
{
|
||||||
@@ -318,9 +319,8 @@ get_open_fd(struct io_context *context, int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Removes the file descriptor from the specified slot.
|
/*! Removes the file descriptor from the specified slot.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static struct file_descriptor*
|
static struct file_descriptor*
|
||||||
remove_fd(struct io_context* context, int fd)
|
remove_fd(struct io_context* context, int fd)
|
||||||
{
|
{
|
||||||
@@ -663,15 +663,15 @@ deselect_fd(int32 fd, struct select_info* info, bool kernel)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** This function checks if the specified fd is valid in the current
|
/*! This function checks if the specified fd is valid in the current
|
||||||
* context. It can be used for a quick check; the fd is not locked
|
context. It can be used for a quick check; the fd is not locked
|
||||||
* so it could become invalid immediately after this check.
|
so it could become invalid immediately after this check.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool
|
bool
|
||||||
fd_is_valid(int fd, bool kernel)
|
fd_is_valid(int fd, bool kernel)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor = get_fd(get_current_io_context(kernel), fd);
|
struct file_descriptor* descriptor
|
||||||
|
= get_fd(get_current_io_context(kernel), fd);
|
||||||
if (descriptor == NULL)
|
if (descriptor == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -739,7 +739,7 @@ common_user_io(int fd, off_t pos, void *buffer, size_t length, bool write)
|
|||||||
else
|
else
|
||||||
status = descriptor->ops->fd_read(descriptor, pos, buffer, &length);
|
status = descriptor->ops->fd_read(descriptor, pos, buffer, &length);
|
||||||
|
|
||||||
if (status < B_OK)
|
if (status != B_OK)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
if (movePosition)
|
if (movePosition)
|
||||||
@@ -759,7 +759,7 @@ common_user_vector_io(int fd, off_t pos, const iovec *userVecs, size_t count,
|
|||||||
if (pos < -1)
|
if (pos < -1)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
/* prevent integer overflow exploit in malloc() */
|
// prevent integer overflow exploit in malloc()
|
||||||
if (count > IOV_MAX)
|
if (count > IOV_MAX)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
@@ -778,7 +778,7 @@ common_user_vector_io(int fd, off_t pos, const iovec *userVecs, size_t count,
|
|||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
MemoryDeleter _(vecs);
|
MemoryDeleter _(vecs);
|
||||||
|
|
||||||
if (user_memcpy(vecs, userVecs, sizeof(iovec) * count) < B_OK)
|
if (user_memcpy(vecs, userVecs, sizeof(iovec) * count) != B_OK)
|
||||||
return B_BAD_ADDRESS;
|
return B_BAD_ADDRESS;
|
||||||
|
|
||||||
bool movePosition = false;
|
bool movePosition = false;
|
||||||
@@ -805,7 +805,7 @@ common_user_vector_io(int fd, off_t pos, const iovec *userVecs, size_t count,
|
|||||||
&length);
|
&length);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status < B_OK) {
|
if (status != B_OK) {
|
||||||
if (bytesTransferred == 0)
|
if (bytesTransferred == 0)
|
||||||
return status;
|
return status;
|
||||||
status = B_OK;
|
status = B_OK;
|
||||||
@@ -908,7 +908,8 @@ _user_ioctl(int fd, ulong op, void *buffer, size_t length)
|
|||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
_user_read_dir(int fd, struct dirent *buffer, size_t bufferSize, uint32 maxCount)
|
_user_read_dir(int fd, struct dirent* buffer, size_t bufferSize,
|
||||||
|
uint32 maxCount)
|
||||||
{
|
{
|
||||||
struct file_descriptor* descriptor;
|
struct file_descriptor* descriptor;
|
||||||
ssize_t retval;
|
ssize_t retval;
|
||||||
@@ -916,7 +917,8 @@ _user_read_dir(int fd, struct dirent *buffer, size_t bufferSize, uint32 maxCount
|
|||||||
if (!IS_USER_ADDRESS(buffer))
|
if (!IS_USER_ADDRESS(buffer))
|
||||||
return B_BAD_ADDRESS;
|
return B_BAD_ADDRESS;
|
||||||
|
|
||||||
TRACE(("user_read_dir(fd = %d, buffer = %p, bufferSize = %ld, count = %lu)\n", fd, buffer, bufferSize, maxCount));
|
TRACE(("user_read_dir(fd = %d, buffer = %p, bufferSize = %ld, count = "
|
||||||
|
"%lu)\n", fd, buffer, bufferSize, maxCount));
|
||||||
|
|
||||||
struct io_context* ioContext = get_current_io_context(false);
|
struct io_context* ioContext = get_current_io_context(false);
|
||||||
descriptor = get_fd(ioContext, fd);
|
descriptor = get_fd(ioContext, fd);
|
||||||
@@ -1058,7 +1060,7 @@ _kern_readv(int fd, off_t pos, const iovec *vecs, size_t count)
|
|||||||
size_t length = vecs[i].iov_len;
|
size_t length = vecs[i].iov_len;
|
||||||
status = descriptor->ops->fd_read(descriptor, pos, vecs[i].iov_base,
|
status = descriptor->ops->fd_read(descriptor, pos, vecs[i].iov_base,
|
||||||
&length);
|
&length);
|
||||||
if (status < B_OK) {
|
if (status != B_OK) {
|
||||||
bytesRead = status;
|
bytesRead = status;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1153,7 +1155,7 @@ _kern_writev(int fd, off_t pos, const iovec *vecs, size_t count)
|
|||||||
size_t length = vecs[i].iov_len;
|
size_t length = vecs[i].iov_len;
|
||||||
status = descriptor->ops->fd_write(descriptor, pos,
|
status = descriptor->ops->fd_write(descriptor, pos,
|
||||||
vecs[i].iov_base, &length);
|
vecs[i].iov_base, &length);
|
||||||
if (status < B_OK) {
|
if (status != B_OK) {
|
||||||
bytesWritten = status;
|
bytesWritten = status;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1204,12 +1206,14 @@ _kern_ioctl(int fd, ulong op, void *buffer, size_t length)
|
|||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
_kern_read_dir(int fd, struct dirent *buffer, size_t bufferSize, uint32 maxCount)
|
_kern_read_dir(int fd, struct dirent* buffer, size_t bufferSize,
|
||||||
|
uint32 maxCount)
|
||||||
{
|
{
|
||||||
struct file_descriptor* descriptor;
|
struct file_descriptor* descriptor;
|
||||||
ssize_t retval;
|
ssize_t retval;
|
||||||
|
|
||||||
TRACE(("sys_read_dir(fd = %d, buffer = %p, bufferSize = %ld, count = %lu)\n",fd, buffer, bufferSize, maxCount));
|
TRACE(("sys_read_dir(fd = %d, buffer = %p, bufferSize = %ld, count = "
|
||||||
|
"%lu)\n",fd, buffer, bufferSize, maxCount));
|
||||||
|
|
||||||
struct io_context* ioContext = get_current_io_context(true);
|
struct io_context* ioContext = get_current_io_context(true);
|
||||||
descriptor = get_fd(ioContext, fd);
|
descriptor = get_fd(ioContext, fd);
|
||||||
|
|||||||
+237
-186
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user