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>
|
|
|
|
|
2019-08-14 17:34:09 +02:00
|
|
|
/* libc includes */
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.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
|
|
|
|
|
|
|
|
2019-08-16 11:42:27 +02:00
|
|
|
File_descriptor_allocator::File_descriptor_allocator(Genode::Allocator &alloc)
|
|
|
|
: _alloc(alloc)
|
|
|
|
{ }
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-08-15 18:32:40 +02:00
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
|
2019-08-16 11:42:27 +02:00
|
|
|
bool const any_fd = (libc_fd < 0);
|
|
|
|
if (any_fd)
|
|
|
|
return new (_alloc) File_descriptor(_id_space, *plugin, *context);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2019-08-16 11:42:27 +02:00
|
|
|
Id_space::Id const id {(unsigned)libc_fd};
|
|
|
|
return new (_alloc) File_descriptor(_id_space, *plugin, *context, id);
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void File_descriptor_allocator::free(File_descriptor *fdo)
|
|
|
|
{
|
2017-08-15 18:32:40 +02:00
|
|
|
Lock::Guard guard(_lock);
|
2019-08-16 11:42:27 +02:00
|
|
|
|
2014-04-12 00:03:40 +02:00
|
|
|
::free((void *)fdo->fd_path);
|
2019-08-16 11:42:27 +02:00
|
|
|
|
|
|
|
Genode::destroy(_alloc, fdo);
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-16 19:04:04 +02:00
|
|
|
void File_descriptor_allocator::preserve(int fd)
|
|
|
|
{
|
|
|
|
if (!find_by_libc_fd(fd))
|
|
|
|
alloc(nullptr, nullptr, fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
File_descriptor *File_descriptor_allocator::find_by_libc_fd(int libc_fd)
|
|
|
|
{
|
2017-08-15 18:32:40 +02:00
|
|
|
Lock::Guard guard(_lock);
|
2019-08-16 11:42:27 +02:00
|
|
|
|
|
|
|
if (libc_fd < 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
File_descriptor *result = nullptr;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Id_space::Id const id {(unsigned)libc_fd};
|
|
|
|
_id_space.apply<File_descriptor>(id, [&] (File_descriptor &fd) {
|
|
|
|
result = &fd; });
|
|
|
|
} catch (Id_space::Unknown_id) { }
|
|
|
|
|
|
|
|
return result;
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
2016-01-08 14:51:07 +01:00
|
|
|
|
|
|
|
|
2019-08-14 17:34:09 +02:00
|
|
|
void File_descriptor_allocator::generate_info(Xml_generator &xml)
|
|
|
|
{
|
|
|
|
Lock::Guard guard(_lock);
|
|
|
|
|
|
|
|
_id_space.for_each<File_descriptor>([&] (File_descriptor &fd) {
|
|
|
|
xml.node("fd", [&] () {
|
|
|
|
|
|
|
|
xml.attribute("id", fd.libc_fd);
|
|
|
|
|
|
|
|
if (fd.fd_path)
|
|
|
|
xml.attribute("path", fd.fd_path);
|
|
|
|
|
|
|
|
if (fd.cloexec)
|
|
|
|
xml.attribute("cloexec", "yes");
|
|
|
|
|
|
|
|
if (((fd.flags & O_ACCMODE) != O_WRONLY))
|
|
|
|
xml.attribute("readable", "yes");
|
|
|
|
|
|
|
|
if (((fd.flags & O_ACCMODE) != O_RDONLY))
|
|
|
|
xml.attribute("writeable", "yes");
|
|
|
|
|
|
|
|
if (fd.plugin) {
|
|
|
|
::off_t const seek = fd.plugin->lseek(&fd, 0, SEEK_CUR);
|
|
|
|
if (seek)
|
|
|
|
xml.attribute("seek", seek);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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; }
|