| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/service_manager/runner/host/native_library_runner.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "mojo/edk/embedder/entrypoints.h" | |
| 14 #include "mojo/public/c/system/thunks.h" | |
| 15 | |
| 16 namespace service_manager { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 template <typename Thunks> | |
| 21 bool SetThunks(Thunks (*make_thunks)(), | |
| 22 const char* function_name, | |
| 23 base::NativeLibrary library) { | |
| 24 typedef size_t (*SetThunksFn)(const Thunks* thunks); | |
| 25 SetThunksFn set_thunks = reinterpret_cast<SetThunksFn>( | |
| 26 base::GetFunctionPointerFromNativeLibrary(library, function_name)); | |
| 27 if (!set_thunks) | |
| 28 return false; | |
| 29 Thunks thunks = make_thunks(); | |
| 30 size_t expected_size = set_thunks(&thunks); | |
| 31 if (expected_size > sizeof(Thunks)) { | |
| 32 LOG(ERROR) << "Invalid library: expected " << function_name | |
| 33 << " to return thunks of size: " << expected_size; | |
| 34 return false; | |
| 35 } | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 base::NativeLibrary LoadNativeLibrary(const base::FilePath& path) { | |
| 42 DVLOG(2) << "Loading Service in process from library: " << path.value(); | |
| 43 | |
| 44 base::NativeLibraryLoadError error; | |
| 45 base::NativeLibrary library = base::LoadNativeLibrary(path, &error); | |
| 46 LOG_IF(ERROR, !library) | |
| 47 << "Failed to load library (path: " << path.value() | |
| 48 << " reason: " << error.ToString() << ")"; | |
| 49 return library; | |
| 50 } | |
| 51 | |
| 52 bool RunServiceInNativeLibrary(base::NativeLibrary library, | |
| 53 mojom::ServiceRequest request) { | |
| 54 // Tolerate |library| being null, to make life easier for callers. | |
| 55 if (!library) | |
| 56 return false; | |
| 57 | |
| 58 // Thunks aren't needed/used in component build, since the thunked methods | |
| 59 // just live in their own dynamically loaded library. | |
| 60 #if !defined(COMPONENT_BUILD) | |
| 61 if (!SetThunks(&mojo::edk::MakeSystemThunks, "MojoSetSystemThunks", | |
| 62 library)) { | |
| 63 LOG(ERROR) << "MojoSetSystemThunks not found"; | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 #if !defined(OS_WIN) | |
| 68 // On Windows, initializing base::CommandLine with null parameters gets the | |
| 69 // process's command line from the OS. Other platforms need it to be passed | |
| 70 // in. This needs to be passed in before the service initializes the command | |
| 71 // line, which is done as soon as it loads. | |
| 72 typedef void (*InitCommandLineArgs)(int, const char* const*); | |
| 73 InitCommandLineArgs init_command_line_args = | |
| 74 reinterpret_cast<InitCommandLineArgs>( | |
| 75 base::GetFunctionPointerFromNativeLibrary(library, | |
| 76 "InitCommandLineArgs")); | |
| 77 if (init_command_line_args) { | |
| 78 int argc = 0; | |
| 79 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
| 80 const char** argv = new const char*[cmd_line->argv().size()]; | |
| 81 for (auto& arg : cmd_line->argv()) | |
| 82 argv[argc++] = arg.c_str(); | |
| 83 init_command_line_args(argc, argv); | |
| 84 } | |
| 85 #endif | |
| 86 | |
| 87 #endif // !defined(COMPONENT_BUILD) | |
| 88 | |
| 89 typedef MojoResult (*ServiceMainFunction)(MojoHandle); | |
| 90 ServiceMainFunction main_function = reinterpret_cast<ServiceMainFunction>( | |
| 91 base::GetFunctionPointerFromNativeLibrary(library, "ServiceMain")); | |
| 92 if (!main_function) { | |
| 93 LOG(ERROR) << "ServiceMain not found"; | |
| 94 return false; | |
| 95 } | |
| 96 // |ServiceMain()| takes ownership of the service handle. | |
| 97 MojoHandle handle = request.PassMessagePipe().release().value(); | |
| 98 MojoResult result = main_function(handle); | |
| 99 if (result != MOJO_RESULT_OK) { | |
| 100 LOG(ERROR) << "ServiceMain returned error (result: " << result << ")"; | |
| 101 } | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 } // namespace service_manager | |
| OLD | NEW |