# HG changeset patch # User Paul Boddie # Date 1676414170 -3600 # Node ID 269fcdd3a413f29cacfaff4e6f1e4f50078cc7de # Parent 981e2c4ac7985f8ab0f93e22dac574786b521259 Introduced a reopen operation to open new instances of already-opened files, employing this operation when obtaining memory segments of executable programs to avoid passing filenames around. diff -r 981e2c4ac798 -r 269fcdd3a413 libexec/include/exec/memory.h --- a/libexec/include/exec/memory.h Tue Feb 14 21:05:14 2023 +0100 +++ b/libexec/include/exec/memory.h Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,7 @@ /* * Program memory initialisation support. * - * Copyright (C) 2022 Paul Boddie + * Copyright (C) 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -30,5 +30,7 @@ long exec_get_payload(const char *filename, Payload **payload, bool attach); +long exec_get_payload_file(file_t *file, Payload **payload, bool attach); + /* vim: tabstop=2 expandtab shiftwidth=2 */ diff -r 981e2c4ac798 -r 269fcdd3a413 libexec/lib/src/memory.cc --- a/libexec/lib/src/memory.cc Tue Feb 14 21:05:14 2023 +0100 +++ b/libexec/lib/src/memory.cc Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,7 @@ /* * Support for initialising program memory regions in new tasks. * - * Copyright (C) 2022 Paul Boddie + * Copyright (C) 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -31,12 +31,19 @@ -/* Obtain the payload as a dataspace. */ +/* Obtain the payload as a dataspace for a named file. */ long exec_get_payload(const char *filename, Payload **payload, bool attach) { file_t *file = client_open(filename, O_RDONLY); + return exec_get_payload_file(file, payload, attach); +} + +/* Obtain the payload as a dataspace for an existing file. */ + +long exec_get_payload_file(file_t *file, Payload **payload, bool attach) +{ if ((file == NULL) || !client_opened(file)) return -L4_EIO; @@ -77,7 +84,7 @@ if (segment->file_contents()) { - file_t *rfile = client_open(filename, file_opening_flags(segment->region_flags())); + file_t *rfile = client_reopen(file, file_opening_flags(segment->region_flags())); if ((rfile == NULL) || !client_opened(rfile)) { diff -r 981e2c4ac798 -r 269fcdd3a413 libfsclient/include/fsclient/client.h --- a/libfsclient/include/fsclient/client.h Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsclient/include/fsclient/client.h Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,8 @@ /* * Filesystem client functions. * - * Copyright (C) 2018, 2019, 2020, 2021, 2022 Paul Boddie + * Copyright (C) 2018, 2019, 2020, 2021, 2022, + * 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -43,6 +44,7 @@ file_t *client_opendir_using(const char *name, l4_cap_idx_t server); file_t *client_opendir_at(file_t *file); +file_t *client_reopen(file_t *file, flags_t flags); long client_pipe(file_t **reader, file_t **writer, flags_t flags); long client_pipe_using(file_t **reader, file_t **writer, flags_t flags, l4_cap_idx_t server); diff -r 981e2c4ac798 -r 269fcdd3a413 libfsclient/include/fsclient/file.h --- a/libfsclient/include/fsclient/file.h Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsclient/include/fsclient/file.h Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,7 @@ /* * File access convenience functions and types. * - * Copyright (C) 2021, 2022 Paul Boddie + * Copyright (C) 2021, 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -97,6 +97,7 @@ long file_mkdir(const char *filename, mode_t mode, l4_cap_idx_t server); long file_open(file_t *file, const char *filename, flags_t flags, l4_cap_idx_t server); long file_remove(const char *filename, l4_cap_idx_t server); +long file_reopen(file_t *file, file_t *new_file, flags_t flags); long file_rename(const char *source, const char *target, l4_cap_idx_t server); long file_stat(const char *filename, struct stat *st, l4_cap_idx_t server); diff -r 981e2c4ac798 -r 269fcdd3a413 libfsclient/lib/src/client.cc --- a/libfsclient/lib/src/client.cc Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsclient/lib/src/client.cc Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,8 @@ /* * Filesystem client functions. * - * Copyright (C) 2018, 2019, 2020, 2021, 2022 Paul Boddie + * Copyright (C) 2018, 2019, 2020, 2021, 2022, + * 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -333,6 +334,26 @@ +/* Open another instance of an opened filesystem object. */ + +file_t *client_reopen(file_t *file, flags_t flags) +{ + if (file == NULL) + return NULL; + + file_t *new_file = (file_t *) malloc(sizeof(file_t)); + + if (new_file == NULL) + return NULL; + + /* Return any allocated structure even if an error occurs. */ + + new_file->error = file_reopen(file, new_file, flags); + return new_file; +} + + + /* Open a pipe object, returning any error condition. */ long client_pipe(file_t **reader, file_t **writer, flags_t flags) diff -r 981e2c4ac798 -r 269fcdd3a413 libfsclient/lib/src/file.cc --- a/libfsclient/lib/src/file.cc Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsclient/lib/src/file.cc Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,7 @@ /* * File access convenience functions. * - * Copyright (C) 2021, 2022 Paul Boddie + * Copyright (C) 2021, 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -190,6 +190,16 @@ return err; } +/* Open a new instance of a file using the given structure. */ + +long file_reopen(file_t *file, file_t *new_file, flags_t flags) +{ + client_File _file(file->ref); + file_init(new_file); + new_file->flags = flags; + return _file.reopen(flags, &new_file->size, &new_file->ref, &new_file->object_flags); +} + /* Rename an object in the filesystem. This is a convenience function invoking file_context and file_context_rename. */ diff -r 981e2c4ac798 -r 269fcdd3a413 libfsserver/include/fsserver/file_pager.h --- a/libfsserver/include/fsserver/file_pager.h Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsserver/include/fsserver/file_pager.h Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,7 @@ /* * File-specific pager functionality. * - * Copyright (C) 2021, 2022 Paul Boddie + * Copyright (C) 2021, 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -65,6 +65,9 @@ virtual long flush(offset_t populated_size, offset_t *size); + virtual long reopen(flags_t flags, offset_t *size, l4_cap_idx_t *file, + object_flags_t *object_flags); + virtual long resize(offset_t *size); /* Pager and mapped file methods. */ diff -r 981e2c4ac798 -r 269fcdd3a413 libfsserver/include/fsserver/opener_resource.h --- a/libfsserver/include/fsserver/opener_resource.h Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsserver/include/fsserver/opener_resource.h Tue Feb 14 23:36:10 2023 +0100 @@ -57,7 +57,7 @@ long mkdir(const char *path, sys_mode_t mode); long open(const char *path, flags_t flags, offset_t *size, - l4_cap_idx_t *cap, object_flags_t *object_flags); + l4_cap_idx_t *file, object_flags_t *object_flags); long remove(const char *path); diff -r 981e2c4ac798 -r 269fcdd3a413 libfsserver/lib/files/file_pager.cc --- a/libfsserver/lib/files/file_pager.cc Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsserver/lib/files/file_pager.cc Tue Feb 14 23:36:10 2023 +0100 @@ -1,7 +1,7 @@ /* * File-specific pager functionality. * - * Copyright (C) 2021, 2022 Paul Boddie + * Copyright (C) 2021, 2022, 2023 Paul Boddie * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -25,6 +25,7 @@ #include "copied_page_mapper.h" #include "file_pager.h" #include "mapped_file_object_server.h" +#include "resource_server.h" @@ -76,6 +77,20 @@ return err; } +long FilePager::reopen(flags_t flags, offset_t *size, l4_cap_idx_t *file, + object_flags_t *object_flags) +{ + Resource *resource; + long err = _provider->make_resource(flags, size, object_flags, &resource); + + if (err) + return err; + + _provider->notify_all(NOTIFY_FILE_OPENED); + + return ResourceServer(resource).start_thread(file); +} + long FilePager::resize(offset_t *size) { offset_t old_size = get_data_size(); diff -r 981e2c4ac798 -r 269fcdd3a413 libfsserver/lib/files/opener_resource.cc --- a/libfsserver/lib/files/opener_resource.cc Tue Feb 14 21:05:14 2023 +0100 +++ b/libfsserver/lib/files/opener_resource.cc Tue Feb 14 23:36:10 2023 +0100 @@ -83,7 +83,7 @@ /* Return an object for the given path and flags. */ long OpenerResource::open(const char *path, flags_t flags, offset_t *size, - l4_cap_idx_t *cap, object_flags_t *object_flags) + l4_cap_idx_t *file, object_flags_t *object_flags) { Resource *resource; long err = _registry->get_resource(this, path, flags, size, object_flags, &resource); @@ -97,7 +97,7 @@ /* Start a server for the resource. */ - return ResourceServer(resource).start_thread(cap); + return ResourceServer(resource).start_thread(file); } /* Request the removal of the named filesystem object. */ diff -r 981e2c4ac798 -r 269fcdd3a413 libsystypes/idl/file.idl --- a/libsystypes/idl/file.idl Tue Feb 14 21:05:14 2023 +0100 +++ b/libsystypes/idl/file.idl Tue Feb 14 23:36:10 2023 +0100 @@ -8,4 +8,9 @@ /* Resize the file. */ [opcode(21)] void resize(inout offset_t size); + + /* Obtain a new reference to the given file. */ + + [opcode(27)] void reopen(in flags_t flags, out offset_t size, out cap file, + out object_flags_t object_flags); };