From 9453287a6b41b27ae0c97b79fafa0a7850ea4240 Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Thu, 30 Jul 2020 16:34:16 +0200 Subject: [PATCH] libc: move current working directory into kernel --- .../libports/src/lib/libc/file_operations.cc | 17 +++++++-- repos/libports/src/lib/libc/internal/cwd.h | 35 +++++++++++++++++++ repos/libports/src/lib/libc/internal/init.h | 2 ++ repos/libports/src/lib/libc/internal/kernel.h | 11 +++++- repos/libports/src/lib/libc/kernel.cc | 3 +- 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 repos/libports/src/lib/libc/internal/cwd.h diff --git a/repos/libports/src/lib/libc/file_operations.cc b/repos/libports/src/lib/libc/file_operations.cc index 697c5b8e0a..07b5fd51d5 100644 --- a/repos/libports/src/lib/libc/file_operations.cc +++ b/repos/libports/src/lib/libc/file_operations.cc @@ -43,6 +43,8 @@ extern "C" { #include #include #include +#include +#include using namespace Libc; @@ -61,6 +63,14 @@ Libc::Mmap_registry *Libc::mmap_registry() } +static Cwd *_cwd_ptr; + +void Libc::init_file_operations(Cwd &cwd) +{ + _cwd_ptr = &cwd; +} + + /*************** ** Utilities ** ***************/ @@ -70,8 +80,11 @@ Libc::Mmap_registry *Libc::mmap_registry() */ static Absolute_path &cwd() { - static Absolute_path _cwd("/"); - return _cwd; + struct Missing_call_of_init_file_operations : Exception { }; + if (!_cwd_ptr) + throw Missing_call_of_init_file_operations(); + + return _cwd_ptr->cwd(); } /** diff --git a/repos/libports/src/lib/libc/internal/cwd.h b/repos/libports/src/lib/libc/internal/cwd.h new file mode 100644 index 0000000000..928fb22d90 --- /dev/null +++ b/repos/libports/src/lib/libc/internal/cwd.h @@ -0,0 +1,35 @@ +/* + * \brief Interface for access to current working directory + * \author Christian Helmuth + * \date 2020-07-20 + */ + +/* + * Copyright (C) 2020 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#ifndef _LIBC__INTERNAL__CWD_H_ +#define _LIBC__INTERNAL__CWD_H_ + +/* libc-internal includes */ +#include +#include + +namespace Libc { + + /** + * Interface to resume all user contexts + */ + struct Cwd : Interface + { + /** + * Provide access to current working directory (modifiable) + */ + virtual Absolute_path &cwd() = 0; + }; +} + +#endif /* _LIBC__INTERNAL__CWD_H_ */ diff --git a/repos/libports/src/lib/libc/internal/init.h b/repos/libports/src/lib/libc/internal/init.h index 5e50f0c466..856ce4cdf2 100644 --- a/repos/libports/src/lib/libc/internal/init.h +++ b/repos/libports/src/lib/libc/internal/init.h @@ -39,6 +39,7 @@ namespace Libc { struct Signal; struct File_descriptor_allocator; struct Timer_accessor; + struct Cwd; /** * Support for shared libraries @@ -64,6 +65,7 @@ namespace Libc { * Virtual file system */ void init_vfs_plugin(Suspend &); + void init_file_operations(Cwd &); /** * Select support diff --git a/repos/libports/src/lib/libc/internal/kernel.h b/repos/libports/src/lib/libc/internal/kernel.h index 30b428523e..ec2a399ce6 100644 --- a/repos/libports/src/lib/libc/internal/kernel.h +++ b/repos/libports/src/lib/libc/internal/kernel.h @@ -41,6 +41,7 @@ #include #include #include +#include namespace Libc { class Kernel; @@ -107,7 +108,8 @@ struct Libc::Kernel final : Vfs::Io_response_handler, Select, Kernel_routine_scheduler, Current_time, - Watch + Watch, + Cwd { private: @@ -286,6 +288,8 @@ struct Libc::Kernel final : Vfs::Io_response_handler, Constructible _clone_connection { }; + Absolute_path _cwd { "/" }; + struct Resumer { GENODE_RPC(Rpc_resume, void, resume); @@ -658,6 +662,11 @@ struct Libc::Kernel final : Vfs::Io_response_handler, ? watch_handle : nullptr; } + /** + * Cwd interface + */ + Absolute_path &cwd() { return _cwd; } + /**************************************** ** Vfs::Io_response_handler interface ** diff --git a/repos/libports/src/lib/libc/kernel.cc b/repos/libports/src/lib/libc/kernel.cc index 600d206592..3a8c2ca250 100644 --- a/repos/libports/src/lib/libc/kernel.cc +++ b/repos/libports/src/lib/libc/kernel.cc @@ -168,7 +168,7 @@ void Libc::Kernel::_init_file_descriptors() typedef String Path; if (node.has_attribute("cwd")) - chdir(node.attribute_value("cwd", Path()).string()); + _cwd.import(node.attribute_value("cwd", Path()).string(), _cwd.base()); init_fd(node, "stdin", 0, O_RDONLY); init_fd(node, "stdout", 1, O_WRONLY); @@ -418,6 +418,7 @@ Libc::Kernel::Kernel(Genode::Env &env, Genode::Allocator &heap) init_plugin(*this); init_sleep(*this); init_vfs_plugin(*this); + init_file_operations(*this); init_time(*this, _rtc_path, *this); init_select(*this, *this, *this, _signal); init_socket_fs(*this);