| /* |
| * |
| * Copyright (c) 2018 Google Inc. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| * |
| */ |
| |
| #include "dlopen_fuchsia.h" |
| |
| #include <fcntl.h> |
| #include <lib/fdio/directory.h> |
| #include <lib/fdio/io.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <threads.h> |
| #include <zircon/dlfcn.h> |
| #include <zircon/syscalls.h> |
| |
| #include "loader_service.h" |
| |
| static char g_error[128] = {}; |
| |
| const char *dlerror_fuchsia(void) { return g_error; } |
| |
| void *dlopen_fuchsia(const char *name, int mode, bool driver) { |
| // First try to just dlopen() from our own namespace. This will succeed for |
| // any layers that are packaged with the application, but will fail for |
| // client drivers loaded from the system. |
| void *result; |
| if (!driver) { |
| result = dlopen(name, mode); |
| if (result != NULL) return result; |
| } |
| |
| // If we couldn't find the library in our own namespace, connect to the |
| // loader service to request this library. |
| |
| auto &loader_svc = get_vulkan_loader_service(); |
| if (!loader_svc.client_end()) { |
| snprintf(g_error, sizeof(g_error), "libvulkan.so:dlopen_fuchsia: no connection to loader svc\n"); |
| return NULL; |
| } |
| |
| auto get_result = loader_svc.Get(fidl::StringView::FromExternal(name)); |
| if (!get_result.ok()) { |
| snprintf(g_error, sizeof(g_error), "libvulkan.so:dlopen_fuchsia: Get() failed: %d\n", get_result.status()); |
| return NULL; |
| } |
| |
| if (!get_result->lib) { |
| snprintf(g_error, sizeof(g_error), "libvulkan.so:dlopen_fuchsia: Get() returned invalid vmo\n"); |
| return NULL; |
| } |
| |
| result = dlopen_vmo(get_result->lib.get(), mode); |
| if (!result) { |
| snprintf(g_error, sizeof(g_error), "%s", dlerror()); |
| } |
| return result; |
| } |