1 /* 2 * A pipe opener resource. 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 "pipe_opener_resource.h" 23 #include "pipe_opener_server.h" 24 #include "pipe_pager.h" 25 #include "resource_server.h" 26 27 28 29 /* Support for providing access to pipes. */ 30 31 PipeOpenerResource::PipeOpenerResource(Memory *memory) 32 : _memory(memory) 33 { 34 } 35 36 int PipeOpenerResource::expected_items() 37 { 38 return PipeOpener_expected_items; 39 } 40 41 ipc_server_handler_type PipeOpenerResource::handler() 42 { 43 return (ipc_server_handler_type) handle_PipeOpener; 44 } 45 46 47 48 /* Pipe opener interface methods. */ 49 50 long PipeOpenerResource::pipe(offset_t size, l4_cap_idx_t *reader, l4_cap_idx_t *writer) 51 { 52 /* Both endpoints will employ a common paging coordinator. */ 53 54 PipePaging *paging = new PipePaging(_memory, size); 55 56 /* Each endpoint will have its own pager. */ 57 58 /* NOTE: Failure to open an endpoint should invalidate both, plus the 59 paging object. Also, any active server thread would need to be 60 cancelled. */ 61 62 return open_endpoint(paging, false, reader) || open_endpoint(paging, true, writer); 63 } 64 65 long PipeOpenerResource::open_endpoint(PipePaging *paging, bool writing, l4_cap_idx_t *endpoint) 66 { 67 PipePager *pager = new PipePager(paging, writing); 68 69 /* Start the endpoint server in a new thread. 70 If the thread does not start, the resource should be finalised. */ 71 72 ResourceServer server(pager); 73 long err = server.start_thread(); 74 75 /* Return the server capability to the caller. */ 76 77 if (!err) 78 *endpoint = server.config()->server; 79 80 return err; 81 } 82 83 // vim: tabstop=4 expandtab shiftwidth=4