New call new_fd_etc() which accepts a starting index for where to search for free FDs.

Implemented F_DUPFD support for fcntl() - we no longer see any redirection errors when
booting into Haiku.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10221 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2004-11-25 02:56:35 +00:00
parent e3e06dd847
commit 1e32802a52
2 changed files with 21 additions and 8 deletions

View File

@ -1,8 +1,8 @@
/* Operations on file descriptors /* Operations on file descriptors
** *
** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. * Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de.
** Distributed under the terms of the Haiku License. * Distributed under the terms of the MIT License.
*/ */
#include <OS.h> #include <OS.h>
@ -62,14 +62,14 @@ alloc_fd(void)
int int
new_fd(struct io_context *context, struct file_descriptor *descriptor) new_fd_etc(struct io_context *context, struct file_descriptor *descriptor, int firstIndex)
{ {
int fd = -1; int fd = -1;
uint32 i; uint32 i;
mutex_lock(&context->io_mutex); mutex_lock(&context->io_mutex);
for (i = 0; i < context->table_size; i++) { for (i = firstIndex; i < context->table_size; i++) {
if (!context->fds[i]) { if (!context->fds[i]) {
fd = i; fd = i;
break; break;
@ -90,6 +90,13 @@ err:
} }
int
new_fd(struct io_context *context, struct file_descriptor *descriptor)
{
return new_fd_etc(context, descriptor, 0);
}
/** Reduces the descriptor's reference counter, and frees all resources /** Reduces the descriptor's reference counter, and frees all resources
* if necessary. * if necessary.
*/ */
@ -124,7 +131,7 @@ get_fd(struct io_context *context, int fd)
if ((uint32)fd < context->table_size) if ((uint32)fd < context->table_size)
descriptor = context->fds[fd]; descriptor = context->fds[fd];
if (descriptor != NULL) // fd is valid if (descriptor != NULL) // fd is valid
atomic_add(&descriptor->ref_count, 1); atomic_add(&descriptor->ref_count, 1);
@ -289,7 +296,7 @@ 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;
put_fd(descriptor); put_fd(descriptor);
return true; return true;
} }

View File

@ -2938,6 +2938,12 @@ common_fcntl(int fd, int op, uint32 argument, bool kernel)
status = descriptor->open_mode; status = descriptor->open_mode;
break; break;
case F_DUPFD:
status = new_fd_etc(get_current_io_context(kernel), descriptor, (int)argument);
if (status >= 0)
atomic_add(&descriptor->ref_count, 1);
break;
// ToDo: add support for more ops // ToDo: add support for more ops
default: default: