Renamed VFS syscalls to the new style.
Fixed read/write calls to use and update the descriptor position. Removed the CHECK_USER_ADDR() macro; it's now using the usual (more readable) check. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@7967 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+95
-59
@@ -1,6 +1,6 @@
|
|||||||
/* Operations on file descriptors
|
/* Operations on file descriptors
|
||||||
**
|
**
|
||||||
** Copyright 2002, Axel Dörfler, [email protected]. All rights reserved.
|
** Copyright 2002-2004, Axel Dörfler, [email protected]. All rights reserved.
|
||||||
** Distributed under the terms of the OpenBeOS License.
|
** Distributed under the terms of the OpenBeOS License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -16,12 +16,9 @@
|
|||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define CHECK_USER_ADDR(x) \
|
|
||||||
if ((addr)(x) >= KERNEL_BASE && (addr)(x) <= KERNEL_TOP) \
|
|
||||||
return B_BAD_ADDRESS;
|
|
||||||
|
|
||||||
#define TRACE_FD 0
|
//#define TRACE_FD
|
||||||
#if TRACE_FD
|
#ifdef TRACE_FD
|
||||||
# define TRACE(x) dprintf x
|
# define TRACE(x) dprintf x
|
||||||
# define PRINT(x) dprintf x
|
# define PRINT(x) dprintf x
|
||||||
#else
|
#else
|
||||||
@@ -305,10 +302,10 @@ fd_is_valid(int fd, bool kernel)
|
|||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
user_read(int fd, off_t pos, void *buffer, size_t length)
|
_user_read(int fd, off_t pos, void *buffer, size_t length)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
ssize_t retval;
|
ssize_t bytesRead;
|
||||||
|
|
||||||
/* This is a user_function, so abort if we have a kernel address */
|
/* This is a user_function, so abort if we have a kernel address */
|
||||||
if (!IS_USER_ADDRESS(buffer))
|
if (!IS_USER_ADDRESS(buffer))
|
||||||
@@ -318,44 +315,63 @@ user_read(int fd, off_t pos, void *buffer, size_t length)
|
|||||||
if (!descriptor)
|
if (!descriptor)
|
||||||
return B_FILE_ERROR;
|
return B_FILE_ERROR;
|
||||||
|
|
||||||
|
if (pos == -1)
|
||||||
|
pos = descriptor->pos;
|
||||||
|
|
||||||
if (descriptor->ops->fd_read) {
|
if (descriptor->ops->fd_read) {
|
||||||
retval = descriptor->ops->fd_read(descriptor, pos, buffer, &length);
|
bytesRead = descriptor->ops->fd_read(descriptor, pos, buffer, &length);
|
||||||
if (retval >= 0)
|
if (bytesRead >= B_OK) {
|
||||||
retval = (ssize_t)length;
|
if (length > 0x7fffffff)
|
||||||
|
bytesRead = 0x7fffffff;
|
||||||
|
else
|
||||||
|
bytesRead = (ssize_t)length;
|
||||||
|
|
||||||
|
descriptor->pos = pos + length;
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
retval = EINVAL;
|
bytesRead = B_BAD_VALUE;
|
||||||
|
|
||||||
put_fd(descriptor);
|
put_fd(descriptor);
|
||||||
return retval;
|
return bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
user_write(int fd, off_t pos, const void *buffer, size_t length)
|
_user_write(int fd, off_t pos, const void *buffer, size_t length)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
ssize_t retval = 0;
|
ssize_t bytesWritten = 0;
|
||||||
|
|
||||||
CHECK_USER_ADDR(buffer)
|
if (IS_KERNEL_ADDRESS(buffer))
|
||||||
|
return B_BAD_ADDRESS;
|
||||||
|
|
||||||
descriptor = get_fd(get_current_io_context(false), fd);
|
descriptor = get_fd(get_current_io_context(false), fd);
|
||||||
if (!descriptor)
|
if (!descriptor)
|
||||||
return B_FILE_ERROR;
|
return B_FILE_ERROR;
|
||||||
|
|
||||||
|
if (pos == -1)
|
||||||
|
pos = descriptor->pos;
|
||||||
|
|
||||||
if (descriptor->ops->fd_write) {
|
if (descriptor->ops->fd_write) {
|
||||||
retval = descriptor->ops->fd_write(descriptor, pos, buffer, &length);
|
bytesWritten = descriptor->ops->fd_write(descriptor, pos, buffer, &length);
|
||||||
if (retval >= 0)
|
if (bytesWritten >= B_OK) {
|
||||||
retval = (ssize_t)length;
|
if (length > 0x7fffffff)
|
||||||
|
bytesWritten = 0x7fffffff;
|
||||||
|
else
|
||||||
|
bytesWritten = (ssize_t)length;
|
||||||
|
|
||||||
|
descriptor->pos = pos + length;
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
retval = EINVAL;
|
bytesWritten = B_BAD_VALUE;
|
||||||
|
|
||||||
put_fd(descriptor);
|
put_fd(descriptor);
|
||||||
return retval;
|
return bytesWritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
off_t
|
off_t
|
||||||
user_seek(int fd, off_t pos, int seekType)
|
_user_seek(int fd, off_t pos, int seekType)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
|
|
||||||
@@ -375,13 +391,14 @@ user_seek(int fd, off_t pos, int seekType)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
status_t
|
||||||
user_ioctl(int fd, ulong op, void *buffer, size_t length)
|
_user_ioctl(int fd, ulong op, void *buffer, size_t length)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
CHECK_USER_ADDR(buffer)
|
if (IS_KERNEL_ADDRESS(buffer))
|
||||||
|
return B_BAD_ADDRESS;
|
||||||
|
|
||||||
PRINT(("user_ioctl: fd %d\n", fd));
|
PRINT(("user_ioctl: fd %d\n", fd));
|
||||||
|
|
||||||
@@ -400,12 +417,13 @@ 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;
|
||||||
|
|
||||||
CHECK_USER_ADDR(buffer)
|
if (IS_KERNEL_ADDRESS(buffer))
|
||||||
|
return B_BAD_ADDRESS;
|
||||||
|
|
||||||
PRINT(("user_read_dir(fd = %d, buffer = %p, bufferSize = %ld, count = %lu)\n", fd, buffer, bufferSize, maxCount));
|
PRINT(("user_read_dir(fd = %d, buffer = %p, bufferSize = %ld, count = %lu)\n", fd, buffer, bufferSize, maxCount));
|
||||||
|
|
||||||
@@ -427,7 +445,7 @@ user_read_dir(int fd, struct dirent *buffer, size_t bufferSize, uint32 maxCount)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
user_rewind_dir(int fd)
|
_user_rewind_dir(int fd)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
status_t status;
|
status_t status;
|
||||||
@@ -458,7 +476,8 @@ _user_read_stat(int fd, struct stat *userStat, size_t statSize)
|
|||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
/* This is a user_function, so abort if we have a kernel address */
|
/* This is a user_function, so abort if we have a kernel address */
|
||||||
CHECK_USER_ADDR(userStat)
|
if (IS_KERNEL_ADDRESS(userStat))
|
||||||
|
return B_BAD_ADDRESS;
|
||||||
|
|
||||||
descriptor = get_fd(get_current_io_context(false), fd);
|
descriptor = get_fd(get_current_io_context(false), fd);
|
||||||
if (descriptor == NULL)
|
if (descriptor == NULL)
|
||||||
@@ -491,7 +510,8 @@ _user_write_stat(int fd, const struct stat *userStat, size_t statSize, int statM
|
|||||||
if (statSize > sizeof(struct stat))
|
if (statSize > sizeof(struct stat))
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
CHECK_USER_ADDR(userStat)
|
if (IS_KERNEL_ADDRESS(userStat))
|
||||||
|
return B_BAD_ADDRESS;
|
||||||
|
|
||||||
descriptor = get_fd(get_current_io_context(false), fd);
|
descriptor = get_fd(get_current_io_context(false), fd);
|
||||||
if (descriptor == NULL)
|
if (descriptor == NULL)
|
||||||
@@ -517,8 +537,8 @@ _user_write_stat(int fd, const struct stat *userStat, size_t statSize, int statM
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
status_t
|
||||||
user_close(int fd)
|
_user_close(int fd)
|
||||||
{
|
{
|
||||||
struct io_context *io = get_current_io_context(false);
|
struct io_context *io = get_current_io_context(false);
|
||||||
struct file_descriptor *descriptor = get_fd(io, fd);
|
struct file_descriptor *descriptor = get_fd(io, fd);
|
||||||
@@ -536,14 +556,14 @@ user_close(int fd)
|
|||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
user_dup(int fd)
|
_user_dup(int fd)
|
||||||
{
|
{
|
||||||
return dup_fd(fd, false);
|
return dup_fd(fd, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
user_dup2(int ofd, int nfd)
|
_user_dup2(int ofd, int nfd)
|
||||||
{
|
{
|
||||||
return dup2_fd(ofd, nfd, false);
|
return dup2_fd(ofd, nfd, false);
|
||||||
}
|
}
|
||||||
@@ -554,53 +574,69 @@ user_dup2(int ofd, int nfd)
|
|||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sys_read(int fd, off_t pos, void *buffer, size_t length)
|
_kern_read(int fd, off_t pos, void *buffer, size_t length)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
ssize_t retval;
|
ssize_t bytesRead;
|
||||||
|
|
||||||
descriptor = get_fd(get_current_io_context(true), fd);
|
descriptor = get_fd(get_current_io_context(true), fd);
|
||||||
if (!descriptor)
|
if (!descriptor)
|
||||||
return B_FILE_ERROR;
|
return B_FILE_ERROR;
|
||||||
|
|
||||||
|
if (pos == -1)
|
||||||
|
pos = descriptor->pos;
|
||||||
|
|
||||||
if (descriptor->ops->fd_read) {
|
if (descriptor->ops->fd_read) {
|
||||||
retval = descriptor->ops->fd_read(descriptor, pos, buffer, &length);
|
bytesRead = descriptor->ops->fd_read(descriptor, pos, buffer, &length);
|
||||||
if (retval >= 0)
|
if (bytesRead >= B_OK) {
|
||||||
retval = (ssize_t)length;
|
if (length > 0x7fffffff)
|
||||||
|
bytesRead = 0x7fffffff;
|
||||||
|
else
|
||||||
|
bytesRead = (ssize_t)length;
|
||||||
|
|
||||||
|
descriptor->pos = pos + length;
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
retval = EINVAL;
|
bytesRead = B_BAD_VALUE;
|
||||||
|
|
||||||
put_fd(descriptor);
|
put_fd(descriptor);
|
||||||
return retval;
|
return bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sys_write(int fd, off_t pos, const void *buffer, size_t length)
|
_kern_write(int fd, off_t pos, const void *buffer, size_t length)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
ssize_t retval;
|
ssize_t bytesWritten;
|
||||||
|
|
||||||
descriptor = get_fd(get_current_io_context(true), fd);
|
descriptor = get_fd(get_current_io_context(true), fd);
|
||||||
if (descriptor == NULL)
|
if (descriptor == NULL)
|
||||||
return B_FILE_ERROR;
|
return B_FILE_ERROR;
|
||||||
|
|
||||||
if (descriptor->ops->fd_write) {
|
if (pos == -1)
|
||||||
retval = descriptor->ops->fd_write(descriptor, pos, buffer, &length);
|
pos = descriptor->pos;
|
||||||
if (retval >= 0)
|
|
||||||
retval = (ssize_t)length;
|
|
||||||
|
|
||||||
PRINT(("sys_write(%d) = %ld (rlen = %ld)\n", fd, retval, length));
|
if (descriptor->ops->fd_write) {
|
||||||
|
bytesWritten = descriptor->ops->fd_write(descriptor, pos, buffer, &length);
|
||||||
|
if (bytesWritten >= B_OK) {
|
||||||
|
if (length > 0x7fffffff)
|
||||||
|
bytesWritten = 0x7fffffff;
|
||||||
|
else
|
||||||
|
bytesWritten = (ssize_t)length;
|
||||||
|
|
||||||
|
descriptor->pos = pos + length;
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
retval = EINVAL;
|
bytesWritten = B_BAD_VALUE;
|
||||||
|
|
||||||
put_fd(descriptor);
|
put_fd(descriptor);
|
||||||
return retval;
|
return bytesWritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
off_t
|
off_t
|
||||||
sys_seek(int fd, off_t pos, int seekType)
|
_kern_seek(int fd, off_t pos, int seekType)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
|
|
||||||
@@ -618,8 +654,8 @@ sys_seek(int fd, off_t pos, int seekType)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
status_t
|
||||||
sys_ioctl(int fd, ulong op, void *buffer, size_t length)
|
_kern_ioctl(int fd, ulong op, void *buffer, size_t length)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
int status;
|
int status;
|
||||||
@@ -641,7 +677,7 @@ sys_ioctl(int fd, ulong op, void *buffer, size_t length)
|
|||||||
|
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
sys_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;
|
||||||
@@ -666,7 +702,7 @@ sys_read_dir(int fd, struct dirent *buffer, size_t bufferSize, uint32 maxCount)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
sys_rewind_dir(int fd)
|
_kern_rewind_dir(int fd)
|
||||||
{
|
{
|
||||||
struct file_descriptor *descriptor;
|
struct file_descriptor *descriptor;
|
||||||
status_t status;
|
status_t status;
|
||||||
@@ -756,8 +792,8 @@ _kern_write_stat(int fd, const struct stat *stat, size_t statSize, int statMask)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
status_t
|
||||||
sys_close(int fd)
|
_kern_close(int fd)
|
||||||
{
|
{
|
||||||
struct io_context *io = get_current_io_context(true);
|
struct io_context *io = get_current_io_context(true);
|
||||||
struct file_descriptor *descriptor = get_fd(io, fd);
|
struct file_descriptor *descriptor = get_fd(io, fd);
|
||||||
@@ -773,14 +809,14 @@ sys_close(int fd)
|
|||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
sys_dup(int fd)
|
_kern_dup(int fd)
|
||||||
{
|
{
|
||||||
return dup_fd(fd, true);
|
return dup_fd(fd, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
sys_dup2(int ofd, int nfd)
|
_kern_dup2(int ofd, int nfd)
|
||||||
{
|
{
|
||||||
return dup2_fd(ofd, nfd, true);
|
return dup2_fd(ofd, nfd, true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user