1 /* 2 * A resource offering support for creating contexts and 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 <string.h> 23 24 #include "opener_server.h" 25 #include "opener_resource.h" 26 #include "resource_server.h" 27 28 /* Support for providing access to files. */ 29 30 OpenerResource::OpenerResource(ResourceRegistry *registry) 31 : _registry(registry) 32 { 33 } 34 35 OpenerResource::~OpenerResource() 36 { 37 } 38 39 int OpenerResource::expected_items() 40 { 41 return Opener_expected_items; 42 } 43 44 ipc_server_handler_type OpenerResource::handler() 45 { 46 return (ipc_server_handler_type) handle_Opener; 47 } 48 49 50 51 /* Obtain the identifier for any active parent directory for a path. */ 52 53 long OpenerResource::get_parent(const char *path, fileid_t *fileid) 54 { 55 char *sep = strrchr(path, (int) '/'); 56 long err; 57 58 /* For top-level paths, use the empty string to get the root directory. 59 Otherwise, obtain the parent directory path to obtain the file 60 identifier. */ 61 62 if (sep == NULL) 63 err = get_fileid("", 0, fileid); 64 else 65 { 66 char *parent = strndup(path, sep - path + 1); 67 68 parent[sep - path] = '\0'; 69 err = get_fileid(parent, 0, fileid); 70 free(parent); 71 } 72 73 return err; 74 } 75 76 /* Obtain any active parent directory for a path, notifying its subscribers of 77 the file opening event. 78 NOTE: This might need to happen in the registry due to concurrency issues. */ 79 80 long OpenerResource::notify_parent(const char *path) 81 { 82 Provider *provider; 83 fileid_t fileid; 84 long err; 85 86 err = get_parent(path, &fileid); 87 88 if (err) 89 return err; 90 91 /* Obtain the provider of the parent directory. */ 92 93 err = _registry->find_provider(fileid, &provider); 94 95 if (err) 96 return err; 97 98 /* With any active provider, send a notification. 99 NOTE: This should also communicate which file was involved, probably 100 NOTE: using the file identifier of the opened file. */ 101 102 provider->notify_all(NOTIFY_FILE_OPENED); 103 return L4_EOK; 104 } 105 106 107 108 /* Return an object for the given path and flags. */ 109 110 long OpenerResource::open(const char *path, flags_t flags, offset_t *size, 111 l4_cap_idx_t *cap, object_flags_t *object_flags) 112 { 113 Resource *resource; 114 long err = _registry->get_resource(this, path, flags, size, object_flags, &resource); 115 116 if (err) 117 return err; 118 119 /* Notify listeners subscribed to the parent of the opened object. */ 120 121 notify_parent(path); 122 123 /* Start a server for the resource. */ 124 125 return ResourceServer(resource).start_thread(cap); 126 } 127 128 /* Request the removal of the named filesystem object. */ 129 130 long OpenerResource::remove(const char *path) 131 { 132 return _registry->remove_provider(this, path); 133 } 134 135 136 137 /* Opener interface methods. */ 138 139 long OpenerResource::context(l4_cap_idx_t *context) 140 { 141 OpenerContextResource *resource = new OpenerContextResource(this); 142 143 /* Complete the initialisation and start a server in a new thread. 144 If the thread does not start, the resource will be finalised. */ 145 146 return ResourceServer(resource).start_thread(context); 147 } 148 149 // vim: tabstop=4 expandtab shiftwidth=4