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(FileObjectRegistry *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 79 long OpenerResource::notify_parent(const char *path) 80 { 81 Provider *provider; 82 fileid_t fileid; 83 long err; 84 85 err = get_parent(path, &fileid); 86 87 if (err) 88 return err; 89 90 /* Obtain the provider of the parent directory. */ 91 92 err = _registry->find_provider(fileid, &provider); 93 94 if (err) 95 return err; 96 97 /* With any active provider, send a notification. 98 NOTE: This should also communicate which file was involved, probably 99 NOTE: using the file identifier of the opened file. */ 100 101 provider->notify_all(NOTIFY_FILE_OPENED); 102 return L4_EOK; 103 } 104 105 106 107 /* Return an object for the given path and flags. */ 108 109 long OpenerResource::open(const char *path, flags_t flags, offset_t *size, 110 l4_cap_idx_t *cap, object_flags_t *object_flags) 111 { 112 Resource *resource; 113 long err = _registry->get_resource(this, path, flags, size, object_flags, &resource); 114 115 if (err) 116 return err; 117 118 /* Notify listeners subscribed to the parent of the opened object. */ 119 120 notify_parent(path); 121 122 /* Start a server for the resource. */ 123 124 return ResourceServer(resource).start_thread(cap); 125 } 126 127 128 129 /* Opener interface methods. */ 130 131 long OpenerResource::context(l4_cap_idx_t *context) 132 { 133 OpenerContextResource *resource = new OpenerContextResource(this); 134 135 /* Complete the initialisation and start a server in a new thread. 136 If the thread does not start, the resource will be finalised. */ 137 138 return ResourceServer(resource).start_thread(context); 139 } 140 141 // vim: tabstop=4 expandtab shiftwidth=4