1 /* 2 * Resource server functionality. 3 * 4 * Copyright (C) 2018, 2019, 2020, 2021, 2022 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 <l4/re/env.h> 23 #include <l4/sys/types.h> 24 25 #include <pthread-l4.h> 26 #include <pthread.h> 27 28 #include "resource_server.h" 29 30 31 32 /* Convenience server methods. */ 33 34 /* Bind to a named IPC gate capability. */ 35 36 long ResourceServer::bind(const char *name) 37 { 38 return ipc_server_bind(name, (l4_umword_t) _config, &_config->server); 39 } 40 41 /* Start in the same thread indicating whether deletion notifications and 42 finalisation are to be used. */ 43 44 long ResourceServer::start(bool finalisation) 45 { 46 resource_init_config(_config, _resource); 47 _config->thread = pthread_l4_cap(pthread_self()); 48 49 /* NOTE: On MIPS32, at least, in a payload started by libexec, the main thread 50 is not necessarily returned correctly. */ 51 52 if (l4_is_invalid_cap(_config->thread)) 53 _config->thread = l4re_env()->main_thread; 54 55 if (finalisation) 56 { 57 _config->finaliser = resource_same_thread_finaliser; 58 _config->notifications = 1; 59 } 60 61 return resource_start_config(_config, _resource); 62 } 63 64 /* Start serving a resource in an existing thread. */ 65 66 long ResourceServer::start_in_thread(l4_cap_idx_t thread, bool finalisation) 67 { 68 resource_init_config(_config, _resource); 69 resource_set_config_threaded(_config, thread, 1, finalisation); 70 71 return resource_start_config(_config, _resource); 72 } 73 74 /* Start serving a resource in a new thread. */ 75 76 long ResourceServer::start_thread(bool finalisation) 77 { 78 pthread_t thread; 79 pthread_attr_t attr; 80 long err; 81 82 pthread_attr_init(&attr); 83 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 84 85 resource_init_config(_config, _resource); 86 87 err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config); 88 if (err) 89 return err; 90 91 resource_set_config_threaded(_config, pthread_l4_cap(thread), 1, finalisation); 92 93 return resource_start_config(_config, _resource); 94 } 95 96 /* A convenience method starting a thread and returning the server capability 97 employed via the given parameter. */ 98 99 long ResourceServer::start_thread(l4_cap_idx_t *server, bool finalisation) 100 { 101 long err = start_thread(finalisation); 102 103 if (!err) 104 *server = _config->server; 105 106 return err; 107 } 108 109 110 111 /* Initialise a server configuration for a resource. */ 112 113 void resource_init_config(ipc_server_config_type *config, Resource *resource) 114 { 115 ipc_server_default_config_type default_config = resource->config(); 116 117 config->handler_obj = resource->interface(); 118 config->finaliser_obj = resource; 119 config->expected_items = default_config.expected_items; 120 config->handler = default_config.handler; 121 } 122 123 /* Set a configuration to be threaded. */ 124 125 void resource_set_config_threaded(ipc_server_config_type *config, 126 l4_cap_idx_t thread, int new_thread, 127 int finalisation) 128 { 129 config->finaliser = resource_thread_finaliser; 130 config->config_thread = new_thread; 131 config->thread = thread; 132 config->notifications = finalisation; 133 } 134 135 /* Activate a resource and start a server for it. */ 136 137 long resource_start_config(ipc_server_config_type *config, Resource *resource) 138 { 139 resource->activate(); 140 long err = ipc_server_start_config(config); 141 142 /* Discard any server resources if starting it failed. */ 143 144 if (err) 145 { 146 ipc_server_finalise_config(config); 147 ipc_server_discard_thread(config); 148 } 149 150 return err; 151 } 152 153 154 155 /* A finaliser for exposed resources in the same thread. */ 156 157 void resource_same_thread_finaliser(ipc_server_config_type *config) 158 { 159 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 160 161 /* Close but do not delete the resource since it is assumed that it is being 162 managed by the thread. */ 163 164 resource->close(); 165 166 /* Release the capabilities. */ 167 168 ipc_server_finalise_config(config); 169 delete config; 170 } 171 172 /* A finaliser for exposed resources in a different thread. */ 173 174 void resource_thread_finaliser(ipc_server_config_type *config) 175 { 176 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 177 178 /* Close and delete the resource. */ 179 180 resource->close(); 181 delete resource; 182 183 /* Release the capabilities. */ 184 185 ipc_server_finalise_config(config); 186 delete config; 187 } 188 189 // vim: tabstop=2 expandtab shiftwidth=2