2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief File-descriptor allocator implementation
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2010-01-21
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode includes */
|
2016-04-05 19:08:25 +02:00
|
|
|
#include <util/construct_at.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <base/env.h>
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
#include <base/log.h>
|
2017-02-22 13:26:33 +01:00
|
|
|
#include <libc/allocator.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* libc plugin interface */
|
|
|
|
#include <libc-plugin/fd_alloc.h>
|
|
|
|
|
2017-02-22 13:26:33 +01:00
|
|
|
using namespace Libc;
|
|
|
|
using namespace Genode;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2016-04-05 19:08:25 +02:00
|
|
|
|
2017-02-22 13:26:33 +01:00
|
|
|
File_descriptor_allocator *Libc::file_descriptor_allocator()
|
|
|
|
{
|
|
|
|
static bool constructed = false;
|
|
|
|
static char placeholder[sizeof(File_descriptor_allocator)];
|
|
|
|
static Libc::Allocator md_alloc;
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-02-22 13:26:33 +01:00
|
|
|
if (!constructed) {
|
|
|
|
Genode::construct_at<File_descriptor_allocator>(placeholder, md_alloc);
|
|
|
|
constructed = true;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2017-02-22 13:26:33 +01:00
|
|
|
return reinterpret_cast<File_descriptor_allocator *>(placeholder);
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2017-02-22 13:26:33 +01:00
|
|
|
File_descriptor_allocator::File_descriptor_allocator(Genode::Allocator &md_alloc)
|
|
|
|
: Allocator_avl_tpl<File_descriptor>(&md_alloc)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
add_range(0, MAX_NUM_FDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
File_descriptor *File_descriptor_allocator::alloc(Plugin *plugin,
|
|
|
|
Plugin_context *context,
|
|
|
|
int libc_fd)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
/* we use addresses returned by the allocator as file descriptors */
|
2014-09-09 14:32:31 +02:00
|
|
|
addr_t addr = (libc_fd <= ANY_FD ? ANY_FD : libc_fd);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* allocate fresh fd if the default value for 'libc_fd' was specified */
|
|
|
|
bool alloc_ok = false;
|
2014-09-09 14:32:31 +02:00
|
|
|
if (libc_fd <= ANY_FD)
|
2011-12-22 16:19:25 +01:00
|
|
|
alloc_ok = Allocator_avl_base::alloc(1, reinterpret_cast<void**>(&addr));
|
|
|
|
else
|
2016-05-11 18:21:47 +02:00
|
|
|
alloc_ok = (Allocator_avl_base::alloc_addr(1, addr).ok());
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
if (!alloc_ok) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
error("could not allocate libc_fd ", libc_fd,
|
|
|
|
libc_fd == ANY_FD ? " (any)" : "");
|
2011-12-22 16:19:25 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
File_descriptor *fdo = metadata((void*)addr);
|
|
|
|
fdo->libc_fd = (int)addr;
|
2012-10-08 14:44:31 +02:00
|
|
|
fdo->fd_path = 0;
|
2011-12-22 16:19:25 +01:00
|
|
|
fdo->plugin = plugin;
|
|
|
|
fdo->context = context;
|
2013-11-13 13:36:18 +01:00
|
|
|
fdo->lock = Lock(Lock::UNLOCKED);
|
2011-12-22 16:19:25 +01:00
|
|
|
return fdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void File_descriptor_allocator::free(File_descriptor *fdo)
|
|
|
|
{
|
2014-04-12 00:03:40 +02:00
|
|
|
::free((void *)fdo->fd_path);
|
2011-12-22 16:19:25 +01:00
|
|
|
Allocator_avl_base::free(reinterpret_cast<void*>(fdo->libc_fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
File_descriptor *File_descriptor_allocator::find_by_libc_fd(int libc_fd)
|
|
|
|
{
|
|
|
|
return metadata(reinterpret_cast<void*>(libc_fd));
|
|
|
|
}
|
2016-01-08 14:51:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/********************
|
|
|
|
** Libc functions **
|
|
|
|
********************/
|
|
|
|
|
2016-04-15 14:19:51 +02:00
|
|
|
extern "C" int __attribute__((weak)) getdtablesize(void) { return MAX_NUM_FDS; }
|