2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief libc file operations
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \author Norman Feske
|
2017-02-01 11:28:15 +01:00
|
|
|
* \author Emery Hemingway
|
|
|
|
* \author Christian Helmuth
|
2011-12-22 16:19:25 +01:00
|
|
|
* \date 2010-01-21
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2019-04-26 12:47:33 +02:00
|
|
|
* Copyright (C) 2010-2019 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
|
|
|
*/
|
|
|
|
|
2012-05-18 17:04:52 +02:00
|
|
|
/* Genode includes */
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <base/env.h>
|
2012-10-08 14:44:31 +02:00
|
|
|
#include <os/path.h>
|
|
|
|
#include <util/token.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2012-05-18 17:04:52 +02:00
|
|
|
/* Genode-specific libc interfaces */
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <libc-plugin/plugin_registry.h>
|
|
|
|
#include <libc-plugin/plugin.h>
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" {
|
2012-05-18 17:04:52 +02:00
|
|
|
/* libc includes */
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <dirent.h>
|
2012-10-08 14:44:31 +02:00
|
|
|
#include <errno.h>
|
2012-01-09 21:01:53 +01:00
|
|
|
#include <fcntl.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <stdlib.h>
|
2012-10-08 14:44:31 +02:00
|
|
|
#include <string.h>
|
2017-10-18 14:20:49 +02:00
|
|
|
#include <sys/mman.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
#include <sys/types.h>
|
2019-04-26 12:47:33 +02:00
|
|
|
#include <sys/wait.h>
|
2012-01-09 21:01:53 +01:00
|
|
|
#include <unistd.h>
|
2019-04-26 12:47:33 +02:00
|
|
|
#include <libc_private.h>
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2012-05-18 17:04:52 +02:00
|
|
|
/* libc-internal includes */
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
#include <internal/file.h>
|
|
|
|
#include <internal/mem_alloc.h>
|
|
|
|
#include <internal/mmap_registry.h>
|
|
|
|
#include <internal/errno.h>
|
2012-05-18 17:04:52 +02:00
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
using namespace Libc;
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
#define __SYS_(ret_type, name, args, body) \
|
|
|
|
extern "C" {\
|
|
|
|
ret_type __sys_##name args body \
|
|
|
|
ret_type __libc_##name args __attribute__((alias("__sys_" #name))); \
|
|
|
|
ret_type _##name args __attribute__((alias("__sys_" #name))); \
|
|
|
|
ret_type name args __attribute__((alias("__sys_" #name))); \
|
|
|
|
} \
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2012-08-17 08:27:15 +02:00
|
|
|
Libc::Mmap_registry *Libc::mmap_registry()
|
|
|
|
{
|
|
|
|
static Libc::Mmap_registry registry;
|
|
|
|
return ®istry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
/***************
|
|
|
|
** Utilities **
|
|
|
|
***************/
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
/**
|
|
|
|
* Current working directory
|
|
|
|
*/
|
|
|
|
static Absolute_path &cwd()
|
|
|
|
{
|
|
|
|
static Absolute_path _cwd("/");
|
|
|
|
return _cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* path element token
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct Scanner_policy_path_element
|
|
|
|
{
|
|
|
|
static bool identifier_char(char c, unsigned /* i */)
|
|
|
|
{
|
|
|
|
return (c != '/') && (c != 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef Genode::Token<Scanner_policy_path_element> Path_element_token;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve symbolic links in a given absolute path
|
|
|
|
*/
|
2019-08-20 12:18:54 +02:00
|
|
|
void Libc::resolve_symlinks(char const *path, Absolute_path &resolved_path)
|
2012-10-08 14:44:31 +02:00
|
|
|
{
|
|
|
|
char path_element[PATH_MAX];
|
|
|
|
char symlink_target[PATH_MAX];
|
|
|
|
|
|
|
|
Absolute_path current_iteration_working_path;
|
|
|
|
Absolute_path next_iteration_working_path(path, cwd().base());
|
|
|
|
|
|
|
|
enum { FOLLOW_LIMIT = 10 };
|
|
|
|
int follow_count = 0;
|
|
|
|
bool symlink_resolved_in_this_iteration;
|
|
|
|
do {
|
|
|
|
if (follow_count++ == FOLLOW_LIMIT) {
|
|
|
|
errno = ELOOP;
|
|
|
|
throw Symlink_resolve_error();
|
|
|
|
}
|
|
|
|
|
2016-05-31 12:40:35 +02:00
|
|
|
current_iteration_working_path = next_iteration_working_path;
|
2012-10-08 14:44:31 +02:00
|
|
|
|
|
|
|
next_iteration_working_path.import("");
|
|
|
|
symlink_resolved_in_this_iteration = false;
|
|
|
|
|
|
|
|
Path_element_token t(current_iteration_working_path.base());
|
|
|
|
|
|
|
|
while (t) {
|
|
|
|
if (t.type() != Path_element_token::IDENT) {
|
|
|
|
t = t.next();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
t.string(path_element, sizeof(path_element));
|
|
|
|
|
|
|
|
try {
|
2016-05-12 12:50:16 +02:00
|
|
|
next_iteration_working_path.append_element(path_element);
|
2012-10-08 14:44:31 +02:00
|
|
|
} catch (Genode::Path_base::Path_too_long) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
throw Symlink_resolve_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a symlink has been resolved in this iteration, the remaining
|
|
|
|
* path elements get added and a new iteration starts.
|
|
|
|
*/
|
|
|
|
if (!symlink_resolved_in_this_iteration) {
|
|
|
|
struct stat stat_buf;
|
|
|
|
int res;
|
|
|
|
FNAME_FUNC_WRAPPER_GENERIC(res = , stat, next_iteration_working_path.base(), &stat_buf);
|
|
|
|
if (res == -1) {
|
|
|
|
throw Symlink_resolve_error();
|
|
|
|
}
|
|
|
|
if (S_ISLNK(stat_buf.st_mode)) {
|
|
|
|
FNAME_FUNC_WRAPPER_GENERIC(res = , readlink,
|
|
|
|
next_iteration_working_path.base(),
|
2014-01-20 17:17:58 +01:00
|
|
|
symlink_target, sizeof(symlink_target) - 1);
|
2012-10-08 14:44:31 +02:00
|
|
|
if (res < 1)
|
|
|
|
throw Symlink_resolve_error();
|
|
|
|
|
2014-01-20 17:17:58 +01:00
|
|
|
/* zero terminate target */
|
|
|
|
symlink_target[res] = 0;
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
if (symlink_target[0] == '/')
|
|
|
|
/* absolute target */
|
|
|
|
next_iteration_working_path.import(symlink_target, cwd().base());
|
|
|
|
else {
|
|
|
|
/* relative target */
|
|
|
|
next_iteration_working_path.strip_last_element();
|
|
|
|
try {
|
2016-05-12 12:50:16 +02:00
|
|
|
next_iteration_working_path.append_element(symlink_target);
|
2012-10-08 14:44:31 +02:00
|
|
|
} catch (Genode::Path_base::Path_too_long) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
throw Symlink_resolve_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
symlink_resolved_in_this_iteration = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t = t.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (symlink_resolved_in_this_iteration);
|
|
|
|
|
2016-05-31 12:40:35 +02:00
|
|
|
resolved_path = next_iteration_working_path;
|
2016-03-11 16:39:22 +01:00
|
|
|
resolved_path.remove_trailing('/');
|
2012-10-08 14:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void resolve_symlinks_except_last_element(char const *path, Absolute_path &resolved_path)
|
|
|
|
{
|
|
|
|
Absolute_path absolute_path_without_last_element(path, cwd().base());
|
|
|
|
absolute_path_without_last_element.strip_last_element();
|
|
|
|
|
|
|
|
resolve_symlinks(absolute_path_without_last_element.base(), resolved_path);
|
|
|
|
|
|
|
|
/* append last element to resolved path */
|
|
|
|
Absolute_path absolute_path_last_element(path, cwd().base());
|
|
|
|
absolute_path_last_element.keep_only_last_element();
|
|
|
|
try {
|
2016-05-12 12:50:16 +02:00
|
|
|
resolved_path.append_element(absolute_path_last_element.base());
|
2012-10-08 14:44:31 +02:00
|
|
|
} catch (Genode::Path_base::Path_too_long) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
throw Symlink_resolve_error();
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/********************
|
|
|
|
** Libc functions **
|
|
|
|
********************/
|
|
|
|
|
2015-09-18 11:10:29 +02:00
|
|
|
extern "C" int access(const char *path, int amode)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks(path, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(access, resolved_path.base(), amode);
|
|
|
|
} catch (Symlink_resolve_error) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
extern "C" int chdir(const char *path)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
if ((stat(path, &stat_buf) == -1) ||
|
|
|
|
(!S_ISDIR(stat_buf.st_mode))) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cwd().import(path, cwd().base());
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2017-07-26 18:52:46 +02:00
|
|
|
/**
|
|
|
|
* Close is called incorrectly enough to justify a silent failure
|
|
|
|
*/
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, close, (int libc_fd),
|
2017-02-01 11:28:15 +01:00
|
|
|
{
|
2017-07-26 18:52:46 +02:00
|
|
|
Libc::File_descriptor *fd =
|
|
|
|
Libc::file_descriptor_allocator()->find_by_libc_fd(libc_fd);
|
|
|
|
return (!fd || !fd->plugin)
|
|
|
|
? Libc::Errno(EBADF)
|
|
|
|
: fd->plugin->close(fd);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int dup(int libc_fd)
|
2012-11-09 17:39:04 +01:00
|
|
|
{
|
2013-09-04 21:44:01 +02:00
|
|
|
File_descriptor *ret_fd;
|
|
|
|
FD_FUNC_WRAPPER_GENERIC(ret_fd =, 0, dup, libc_fd);
|
2012-11-09 17:39:04 +01:00
|
|
|
return ret_fd ? ret_fd->libc_fd : INVALID_FD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" int dup2(int libc_fd, int new_libc_fd)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
File_descriptor *fd = libc_fd_to_fd(libc_fd, "dup2");
|
2013-09-04 21:44:01 +02:00
|
|
|
if (!fd || !fd->plugin) {
|
|
|
|
errno = EBADF;
|
2011-12-22 16:19:25 +01:00
|
|
|
return INVALID_FD;
|
2013-09-04 21:44:01 +02:00
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2013-02-21 16:39:46 +01:00
|
|
|
if (libc_fd == new_libc_fd)
|
|
|
|
return libc_fd;
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* Check if 'new_libc_fd' is already in use. If so, close it before
|
|
|
|
* allocating it again.
|
|
|
|
*/
|
|
|
|
File_descriptor *new_fd = file_descriptor_allocator()->find_by_libc_fd(new_libc_fd);
|
|
|
|
if (new_fd)
|
|
|
|
close(new_libc_fd);
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
new_fd = file_descriptor_allocator()->alloc(fd->plugin, 0, new_libc_fd);
|
|
|
|
new_fd->path(fd->fd_path);
|
2011-12-22 16:19:25 +01:00
|
|
|
/* new_fd->context must be assigned by the plugin implementing 'dup2' */
|
|
|
|
return fd->plugin->dup2(fd, new_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
extern "C" __attribute__((alias("dup2")))
|
|
|
|
int _dup2(int libc_fd, int new_libc_fd);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
extern "C" int fchdir(int libc_fd)
|
|
|
|
{
|
|
|
|
File_descriptor *fd = libc_fd_to_fd(libc_fd, "fchdir");
|
|
|
|
|
2013-09-04 21:44:01 +02:00
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
2012-10-08 14:44:31 +02:00
|
|
|
return INVALID_FD;
|
2013-09-04 21:44:01 +02:00
|
|
|
}
|
2012-10-08 14:44:31 +02:00
|
|
|
|
|
|
|
return chdir(fd->fd_path);
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, fcntl, (int libc_fd, int cmd, ...),
|
2012-01-09 21:01:53 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int res;
|
|
|
|
va_start(ap, cmd);
|
2013-09-04 21:44:01 +02:00
|
|
|
FD_FUNC_WRAPPER_GENERIC(res =, INVALID_FD, fcntl, libc_fd, cmd, va_arg(ap, long));
|
2012-01-09 21:01:53 +01:00
|
|
|
va_end(ap);
|
|
|
|
return res;
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, fstat, (int libc_fd, struct stat *buf),
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2019-04-26 12:47:33 +02:00
|
|
|
FD_FUNC_WRAPPER(fstat, libc_fd, buf);
|
|
|
|
})
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, fstatat, (int libc_fd, char const *path, struct stat *buf, int flags),
|
2015-11-28 18:44:46 +01:00
|
|
|
{
|
|
|
|
if (*path == '/') {
|
|
|
|
if (flags & AT_SYMLINK_NOFOLLOW)
|
|
|
|
return lstat(path, buf);
|
|
|
|
return stat(path, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
Libc::Absolute_path abs_path;
|
|
|
|
|
|
|
|
if (libc_fd == AT_FDCWD) {
|
2016-05-31 12:40:35 +02:00
|
|
|
abs_path = cwd();
|
2016-05-12 12:50:16 +02:00
|
|
|
abs_path.append_element(path);
|
2015-11-28 18:44:46 +01:00
|
|
|
} else {
|
|
|
|
Libc::File_descriptor *fd =
|
|
|
|
Libc::file_descriptor_allocator()->find_by_libc_fd(libc_fd);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
abs_path.import(path, fd->fd_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (flags & AT_SYMLINK_NOFOLLOW)
|
|
|
|
? lstat(abs_path.base(), buf)
|
|
|
|
: stat(abs_path.base(), buf);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2015-11-28 18:44:46 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, fstatfs, (int libc_fd, struct statfs *buf), {
|
|
|
|
FD_FUNC_WRAPPER(fstatfs, libc_fd, buf); })
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, fsync, (int libc_fd), {
|
|
|
|
FD_FUNC_WRAPPER(fsync, libc_fd); })
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, fdatasync, (int libc_fd), {
|
|
|
|
FD_FUNC_WRAPPER(fsync, libc_fd); })
|
2012-08-02 15:17:24 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, ftruncate, (int libc_fd, ::off_t length), {
|
|
|
|
FD_FUNC_WRAPPER(ftruncate, libc_fd, length); })
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(ssize_t, getdirentries, (int libc_fd, char *buf, ::size_t nbytes, ::off_t *basep), {
|
|
|
|
FD_FUNC_WRAPPER(getdirentries, libc_fd, buf, nbytes, basep); })
|
2012-05-24 15:26:59 +02:00
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, ioctl, (int libc_fd, int request, char *argp), {
|
|
|
|
FD_FUNC_WRAPPER(ioctl, libc_fd, request, argp); })
|
2011-12-22 16:19:25 +01:00
|
|
|
|
2011-12-26 20:08:58 +01:00
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(::off_t, lseek, (int libc_fd, ::off_t offset, int whence), {
|
|
|
|
FD_FUNC_WRAPPER(lseek, libc_fd, offset, whence); })
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
extern "C" int lstat(const char *path, struct stat *buf)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks_except_last_element(path, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(stat, resolved_path.base(), buf);
|
|
|
|
} catch (Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" int mkdir(const char *path, mode_t mode)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks_except_last_element(path, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(mkdir, resolved_path.base(), mode);
|
|
|
|
} catch(Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(void *, mmap, (void *addr, ::size_t length,
|
|
|
|
int prot, int flags,
|
|
|
|
int libc_fd, ::off_t offset),
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2012-08-20 13:52:27 +02:00
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
/* handle requests for anonymous memory */
|
2012-08-17 08:27:15 +02:00
|
|
|
if (!addr && libc_fd == -1) {
|
2017-10-18 14:20:49 +02:00
|
|
|
bool const executable = prot & PROT_EXEC;
|
|
|
|
void *start = Libc::mem_alloc(executable)->alloc(length, PAGE_SHIFT);
|
2018-07-23 14:53:03 +02:00
|
|
|
if (!start) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
2012-08-17 08:27:15 +02:00
|
|
|
mmap_registry()->insert(start, length, 0);
|
|
|
|
return start;
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* lookup plugin responsible for file descriptor */
|
|
|
|
File_descriptor *fd = libc_fd_to_fd(libc_fd, "mmap");
|
2012-08-17 08:27:15 +02:00
|
|
|
if (!fd || !fd->plugin || !fd->plugin->supports_mmap()) {
|
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
|
|
|
Genode::warning("mmap not supported for file descriptor ", libc_fd);
|
2013-09-04 21:44:01 +02:00
|
|
|
errno = EBADF;
|
2018-07-23 14:53:03 +02:00
|
|
|
return MAP_FAILED;
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
2012-08-17 08:27:15 +02:00
|
|
|
void *start = fd->plugin->mmap(addr, length, prot, flags, fd, offset);
|
|
|
|
mmap_registry()->insert(start, length, fd->plugin);
|
|
|
|
return start;
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2012-08-17 08:27:15 +02:00
|
|
|
|
|
|
|
|
2012-08-20 13:52:27 +02:00
|
|
|
extern "C" int munmap(void *start, ::size_t length)
|
2012-08-17 08:27:15 +02:00
|
|
|
{
|
2016-05-11 18:21:47 +02:00
|
|
|
if (!mmap_registry()->registered(start)) {
|
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
|
|
|
Genode::warning("munmap: could not lookup plugin for address ", start);
|
2012-08-17 08:27:15 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup plugin that was used for mmap
|
|
|
|
*
|
|
|
|
* If the pointer is NULL, 'start' refers to an anonymous mmap.
|
|
|
|
*/
|
|
|
|
Plugin *plugin = mmap_registry()->lookup_plugin_by_addr(start);
|
|
|
|
|
2012-08-20 13:52:27 +02:00
|
|
|
int ret = 0;
|
|
|
|
if (plugin)
|
|
|
|
ret = plugin->munmap(start, length);
|
2017-10-18 14:20:49 +02:00
|
|
|
else {
|
|
|
|
bool const executable = true;
|
|
|
|
/* XXX another metadata handling required to track anonymous memory */
|
|
|
|
Libc::mem_alloc(!executable)->free(start);
|
|
|
|
Libc::mem_alloc(executable)->free(start);
|
|
|
|
}
|
2012-08-17 08:27:15 +02:00
|
|
|
|
2012-08-20 13:52:27 +02:00
|
|
|
mmap_registry()->remove(start);
|
|
|
|
return ret;
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, msync, (void *start, ::size_t len, int flags),
|
2017-07-26 01:53:01 +02:00
|
|
|
{
|
|
|
|
if (!mmap_registry()->registered(start)) {
|
|
|
|
Genode::warning("munmap: could not lookup plugin for address ", start);
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup plugin that was used for mmap
|
|
|
|
*
|
|
|
|
* If the pointer is NULL, 'start' refers to an anonymous mmap.
|
|
|
|
*/
|
|
|
|
Plugin *plugin = mmap_registry()->lookup_plugin_by_addr(start);
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
if (plugin)
|
|
|
|
ret = plugin->msync(start, len, flags);
|
|
|
|
|
|
|
|
return ret;
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2017-07-26 01:53:01 +02:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, open, (const char *pathname, int flags, ...),
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2012-10-08 14:44:31 +02:00
|
|
|
Absolute_path resolved_path;
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
Plugin *plugin;
|
|
|
|
File_descriptor *new_fdo;
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
try {
|
|
|
|
resolve_symlinks_except_last_element(pathname, resolved_path);
|
|
|
|
} catch (Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & O_NOFOLLOW)) {
|
|
|
|
/* resolve last element */
|
|
|
|
try {
|
|
|
|
resolve_symlinks(resolved_path.base(), resolved_path);
|
|
|
|
} catch (Symlink_resolve_error) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
if (!(flags & O_CREAT))
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin = plugin_registry()->get_plugin_for_open(resolved_path.base(), flags);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
if (!plugin) {
|
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
|
|
|
Genode::error("no plugin found for open(\"", pathname, "\", ", flags, ")");
|
2011-12-22 16:19:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
new_fdo = plugin->open(resolved_path.base(), flags);
|
2011-12-22 16:19:25 +01:00
|
|
|
if (!new_fdo) {
|
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
|
|
|
Genode::error("plugin()->open(\"", pathname, "\") failed");
|
2011-12-22 16:19:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-08 14:44:31 +02:00
|
|
|
new_fdo->path(resolved_path.base());
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
return new_fdo->libc_fd;
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(int, openat, (int libc_fd, const char *path, int flags, ...),
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
2012-01-09 21:01:53 +01:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, flags);
|
2019-04-26 12:47:33 +02:00
|
|
|
mode_t mode = va_arg(ap, unsigned);
|
2012-01-09 21:01:53 +01:00
|
|
|
va_end(ap);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
if (*path == '/') {
|
|
|
|
return open(path, flags, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Libc::Absolute_path abs_path;
|
|
|
|
|
|
|
|
if (libc_fd == AT_FDCWD) {
|
|
|
|
abs_path = cwd();
|
|
|
|
abs_path.append_element(path);
|
|
|
|
} else {
|
|
|
|
Libc::File_descriptor *fd =
|
|
|
|
Libc::file_descriptor_allocator()->find_by_libc_fd(libc_fd);
|
|
|
|
if (!fd) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
abs_path.import(path, fd->fd_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return open(abs_path.base(), flags, mode);
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" int pipe(int pipefd[2]) {
|
|
|
|
return pipe2(pipefd, 0); }
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" int pipe2(int pipefd[2], int flags)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
Plugin *plugin;
|
|
|
|
File_descriptor *pipefdo[2];
|
|
|
|
|
|
|
|
plugin = plugin_registry()->get_plugin_for_pipe();
|
|
|
|
|
|
|
|
if (!plugin) {
|
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
|
|
|
Genode::error("no plugin found for pipe()");
|
2011-12-22 16:19:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (plugin->pipe(pipefdo) == -1) {
|
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
|
|
|
Genode::error("plugin()->pipe() failed");
|
2011-12-22 16:19:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
if (flags & O_NONBLOCK) {
|
|
|
|
int err = plugin->fcntl(pipefdo[0], F_SETFL, O_NONBLOCK)
|
|
|
|
| plugin->fcntl(pipefdo[1], F_SETFL, O_NONBLOCK);
|
|
|
|
if (err != 0)
|
|
|
|
Genode::warning("pipe plugin does not support O_NONBLOCK");
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
pipefd[0] = pipefdo[0]->libc_fd;
|
|
|
|
pipefd[1] = pipefdo[1]->libc_fd;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(ssize_t, read, (int libc_fd, void *buf, ::size_t count), {
|
|
|
|
FD_FUNC_WRAPPER(read, libc_fd, buf, count); })
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2016-09-15 14:40:37 +02:00
|
|
|
extern "C" ssize_t readlink(const char *path, char *buf, ::size_t bufsiz)
|
2012-10-08 14:44:31 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks_except_last_element(path, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(readlink, resolved_path.base(), buf, bufsiz);
|
|
|
|
} catch(Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" int rename(const char *oldpath, const char *newpath)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_oldpath, resolved_newpath;
|
|
|
|
resolve_symlinks_except_last_element(oldpath, resolved_oldpath);
|
|
|
|
resolve_symlinks_except_last_element(newpath, resolved_newpath);
|
|
|
|
FNAME_FUNC_WRAPPER(rename, resolved_oldpath.base(), resolved_newpath.base());
|
|
|
|
} catch(Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2014-04-18 22:58:04 +02:00
|
|
|
extern "C" int rmdir(const char *path)
|
|
|
|
{
|
2018-02-23 17:40:30 +01:00
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
if (stat(path, &stat_buf) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!S_ISDIR(stat_buf.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:58:04 +02:00
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks_except_last_element(path, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(rmdir, resolved_path.base());
|
|
|
|
} catch(Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
extern "C" int stat(const char *path, struct stat *buf)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks(path, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(stat, resolved_path.base(), buf);
|
|
|
|
} catch(Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" int symlink(const char *oldpath, const char *newpath)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks_except_last_element(newpath, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(symlink, oldpath, resolved_path.base());
|
|
|
|
} catch(Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
extern "C" int unlink(const char *path)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Absolute_path resolved_path;
|
|
|
|
resolve_symlinks_except_last_element(path, resolved_path);
|
|
|
|
FNAME_FUNC_WRAPPER(unlink, resolved_path.base());
|
|
|
|
} catch(Symlink_resolve_error) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
|
2019-04-26 12:47:33 +02:00
|
|
|
__SYS_(ssize_t, write, (int libc_fd, const void *buf, ::size_t count),
|
2012-10-08 15:38:35 +02:00
|
|
|
{
|
|
|
|
int flags = fcntl(libc_fd, F_GETFL);
|
|
|
|
|
|
|
|
if ((flags != -1) && (flags & O_APPEND))
|
|
|
|
lseek(libc_fd, 0, SEEK_END);
|
|
|
|
|
|
|
|
FD_FUNC_WRAPPER(write, libc_fd, buf, count);
|
2019-04-26 12:47:33 +02:00
|
|
|
})
|
2012-10-08 14:44:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
extern "C" int __getcwd(char *dst, ::size_t dst_size)
|
|
|
|
{
|
|
|
|
Genode::strncpy(dst, cwd().base(), dst_size);
|
|
|
|
return 0;
|
|
|
|
}
|