genode/repos/libports/include/libc-plugin/fd_alloc.h

117 lines
2.4 KiB
C
Raw Normal View History

2011-12-22 15:19:25 +00:00
/*
* \brief file descriptor allocator interface
* \author Christian Prochaska
* \date 2010-01-21
*
*/
/*
* Copyright (C) 2010-2017 Genode Labs GmbH
2011-12-22 15:19:25 +00:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 15:19:25 +00:00
*/
#ifndef _LIBC_PLUGIN__FD_ALLOC_H_
#define _LIBC_PLUGIN__FD_ALLOC_H_
#include <base/lock.h>
#include <base/log.h>
#include <os/path.h>
#include <base/allocator.h>
#include <base/id_space.h>
#include <util/xml_generator.h>
/* libc includes */
#include <stdlib.h>
#include <string.h>
2011-12-22 15:19:25 +00:00
#include <libc-plugin/plugin.h>
enum { MAX_NUM_FDS = 1024 };
namespace Libc {
/**
* Plugin-specific file-descriptor context
*/
struct Plugin_context { virtual ~Plugin_context() { } };
2011-12-22 15:19:25 +00:00
enum { ANY_FD = -1 };
2011-12-22 15:19:25 +00:00
struct File_descriptor
{
Genode::Lock lock { };
typedef Genode::Id_space<File_descriptor> Id_space;
Id_space::Element _elem;
int const libc_fd = _elem.id().value;
char const *fd_path = nullptr; /* for 'fchdir', 'fstat' */
Plugin *plugin;
Plugin_context *context;
int flags = 0; /* for 'fcntl' */
bool cloexec = 0; /* for 'fcntl' */
File_descriptor(Id_space &id_space, Plugin &plugin, Plugin_context &context)
: _elem(*this, id_space), plugin(&plugin), context(&context) { }
File_descriptor(Id_space &id_space, Plugin &plugin, Plugin_context &context,
Id_space::Id id)
: _elem(*this, id_space, id), plugin(&plugin), context(&context) { }
void path(char const *newpath);
2011-12-22 15:19:25 +00:00
};
class File_descriptor_allocator
2011-12-22 15:19:25 +00:00
{
private:
Genode::Lock _lock;
Genode::Allocator &_alloc;
typedef File_descriptor::Id_space Id_space;
Id_space _id_space;
2011-12-22 15:19:25 +00:00
public:
/**
* Constructor
*/
File_descriptor_allocator(Genode::Allocator &_alloc);
2011-12-22 15:19:25 +00:00
/**
* Allocate file descriptor
*/
File_descriptor *alloc(Plugin *plugin, Plugin_context *context, int libc_fd = -1);
/**
* Release file descriptor
*/
void free(File_descriptor *fdo);
/**
* Prevent the use of the specified file descriptor
*/
void preserve(int libc_fd);
2011-12-22 15:19:25 +00:00
File_descriptor *find_by_libc_fd(int libc_fd);
void generate_info(Genode::Xml_generator &);
2011-12-22 15:19:25 +00:00
};
/**
* Return singleton instance of file-descriptor allocator
*/
extern File_descriptor_allocator *file_descriptor_allocator();
}
#endif /* _LIBC_PLUGIN__FD_ALLOC_H_ */