2016-12-22 14:01:19 +00:00
|
|
|
/*
|
|
|
|
* \brief Hook functions for bootstrapping a libc-using Genode component
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2016-12-23
|
|
|
|
*
|
|
|
|
* This interface is implemented by components that use both Genode's API and
|
|
|
|
* the libc. For such components, the libc provides the 'Component::construct'
|
|
|
|
* function that takes the precautions needed for letting the application use
|
|
|
|
* blocking I/O via POSIX functions like 'read' or 'select'. The libc's
|
|
|
|
* 'Component::construct' function finally passes control to the application by
|
|
|
|
* calling the application-provided 'Libc::Component::construct' function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2016-2017 Genode Labs GmbH
|
2016-12-22 14:01:19 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2016-12-22 14:01:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__LIBC__COMPONENT_H_
|
|
|
|
#define _INCLUDE__LIBC__COMPONENT_H_
|
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
#include <util/meta.h>
|
2016-12-22 14:01:19 +00:00
|
|
|
#include <base/env.h>
|
|
|
|
#include <base/stdint.h>
|
2018-06-15 11:37:00 +00:00
|
|
|
#include <util/xml_node.h>
|
2016-12-22 14:01:19 +00:00
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
namespace Libc { class Env; }
|
|
|
|
|
2022-12-03 17:50:25 +00:00
|
|
|
namespace Vfs { struct Env; }
|
2016-12-22 14:01:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface to be provided by the component implementation
|
|
|
|
*/
|
2017-02-01 11:43:30 +00:00
|
|
|
class Libc::Env : public Genode::Env
|
|
|
|
{
|
|
|
|
private:
|
2017-01-03 13:33:55 +00:00
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
virtual Genode::Xml_node _config_xml() const = 0;
|
2017-01-03 13:33:55 +00:00
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component configuration
|
|
|
|
*/
|
|
|
|
template <typename FUNC>
|
|
|
|
void config(FUNC const &func) const {
|
|
|
|
func(_config_xml()); }
|
|
|
|
|
|
|
|
/**
|
2022-12-03 17:50:25 +00:00
|
|
|
* Virtual file system configured for this component
|
2017-02-01 11:43:30 +00:00
|
|
|
*/
|
2022-12-03 17:50:25 +00:00
|
|
|
virtual Vfs::Env &vfs_env() = 0;
|
2017-02-22 13:42:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Libc configuration for this component
|
|
|
|
*/
|
|
|
|
virtual Genode::Xml_node libc_config() = 0;
|
2017-02-01 11:43:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace Libc { namespace Component {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return stack size of the component's initial entrypoint
|
|
|
|
*/
|
|
|
|
Genode::size_t stack_size();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct component
|
|
|
|
*
|
|
|
|
* \param env extended interface to the component's execution environment
|
|
|
|
*/
|
|
|
|
void construct(Libc::Env &env);
|
|
|
|
} }
|
2017-01-03 13:33:55 +00:00
|
|
|
|
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
namespace Libc {
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
class Application_code : Genode::Interface
|
|
|
|
{
|
2017-02-01 11:43:30 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper to distinguish void from non-void return type
|
2017-01-03 13:33:55 +00:00
|
|
|
*/
|
2017-02-01 11:43:30 +00:00
|
|
|
template <typename FUNC, typename RET>
|
|
|
|
void _execute(RET &ret_out, FUNC const &func) { ret_out = func(); }
|
|
|
|
|
2017-01-03 13:33:55 +00:00
|
|
|
template <typename FUNC>
|
2017-02-01 11:43:30 +00:00
|
|
|
void _execute(Genode::Meta::Empty &, FUNC const &func) { func(); }
|
2017-01-03 13:33:55 +00:00
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute() = 0;
|
2017-01-03 13:33:55 +00:00
|
|
|
};
|
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
void execute_in_application_context(Application_code &);
|
2017-01-03 13:33:55 +00:00
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
/**
|
|
|
|
* Execute 'FUNC' lambda in the libc application context
|
|
|
|
*
|
|
|
|
* In order to invoke the libc's I/O functions (in particular 'select',
|
|
|
|
* 'read', 'write', or other functions like 'socket' that indirectly call
|
|
|
|
* them), the libc-calling application code must be executed under the
|
|
|
|
* supervision of the libc runtime. This is not the case for signal
|
|
|
|
* handlers or RPC functions that are executed in the context of the
|
|
|
|
* 'Genode::Entrypoint'. The 'with_libc' function allows such handlers to
|
|
|
|
* interact with the libc by deliberately subjecting the specified function
|
|
|
|
* ('func') to the libc runtime.
|
|
|
|
*/
|
|
|
|
template <typename FUNC>
|
|
|
|
auto with_libc(FUNC const &func)
|
|
|
|
-> typename Genode::Trait::Call_return
|
|
|
|
<typename Genode::Trait::Functor
|
|
|
|
<decltype(&FUNC::operator())>::Return_type>::Type
|
|
|
|
{
|
|
|
|
using Functor = Genode::Trait::Functor<decltype(&FUNC::operator())>;
|
|
|
|
using Return_type = typename Genode::Trait::Call_return<typename Functor::Return_type>::Type;
|
2017-01-03 13:33:55 +00:00
|
|
|
|
2017-02-01 11:43:30 +00:00
|
|
|
/*
|
|
|
|
* Implementation of the 'Application_code' interface that executes
|
|
|
|
* the code in the 'func' lambda.
|
2017-01-03 13:33:55 +00:00
|
|
|
*/
|
2017-02-01 11:43:30 +00:00
|
|
|
struct Application_code_func : Application_code
|
|
|
|
{
|
|
|
|
FUNC const &func;
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 14:42:15 +00:00
|
|
|
Return_type retval { };
|
2017-02-01 11:43:30 +00:00
|
|
|
void execute() override { _execute(retval, func); }
|
|
|
|
Application_code_func(FUNC const &func) : func(func) { }
|
|
|
|
} application_code_func { func };
|
|
|
|
|
|
|
|
execute_in_application_context(application_code_func);
|
|
|
|
|
|
|
|
return application_code_func.retval;
|
2017-01-03 13:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-22 14:01:19 +00:00
|
|
|
|
|
|
|
#endif /* _INCLUDE__LIBC__COMPONENT_H_ */
|