1 /* 2 * A context resource offering support for opening files. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include "opener_context_resource.h" 23 #include "opener_context_object_server.h" 24 #include "opener_resource.h" 25 #include "resource_server.h" 26 27 #include <string.h> 28 29 30 31 /* Support for providing access to files. */ 32 33 OpenerContextResource::OpenerContextResource(OpenerResource *opener, Memory *memory) 34 : SimplePager(memory), _opener(opener) 35 { 36 } 37 38 int OpenerContextResource::expected_items() 39 { 40 return OpenerContextObject_expected_items; 41 } 42 43 ipc_server_handler_type OpenerContextResource::handler() 44 { 45 return (ipc_server_handler_type) handle_OpenerContextObject; 46 } 47 48 49 50 /* Data access methods. */ 51 52 char *OpenerContextResource::get_path() 53 { 54 char *buffer = _region->read(); 55 offset_t size = _region->size(); 56 57 /* Confine the path to the limit of the buffer. */ 58 59 if ((buffer != NULL) && (strnlen(buffer, size) < size)) 60 return buffer; 61 else 62 return NULL; 63 } 64 65 66 67 /* Opener context interface methods. */ 68 69 long OpenerContextResource::open(flags_t flags, offset_t *size, l4_cap_idx_t *file) 70 { 71 char *path = get_path(); 72 73 if (path == NULL) 74 return -L4_EINVAL; 75 76 Pager *pager = _opener->open(path, flags); 77 78 /* Complete the initialisation and start a server in a new thread. 79 If the thread does not start, the resource should be finalised. */ 80 81 ResourceServer server(pager); 82 long err = server.start_thread(); 83 84 if (!err) 85 { 86 /* Return the file size. */ 87 /* Return the server capability to the caller. */ 88 89 *size = pager->get_data_size(); 90 *file = server.config()->server; 91 } 92 93 return err; 94 } 95 96 // vim: tabstop=4 expandtab shiftwidth=4