apply launchpad-hydra refactoring
[platform/core/dotnet/launcher.git] / NativeLauncher / hydra / hydra_main.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <launchpad_hydra.h>
18
19 #include <stdio.h>
20 #include <dlfcn.h>
21 #include <cstring>
22 #include <fstream>
23 #include <limits.h>
24 #include <stdlib.h>
25
26 #include "log.h"
27 #include "launcher_env.h"
28
29 const char* __coreclr_lib = "/usr/share/dotnet.tizen/netcoreapp/libcoreclr.so";
30 const char* __dotnet_launcher = "/usr/bin/dotnet-launcher";
31
32 typedef int (*coreclr_preload_assembly_ptr)(const char* assemblyPath);
33 typedef int (*launcher_real_main_ptr)(int argc, char *argv[], const char* mode);
34
35 static std::string absolutePath(const std::string& path)
36 {
37         std::string absPath;
38         char realPath[PATH_MAX];
39         if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
40                 absPath.assign(realPath);
41
42         return absPath;
43 }
44
45 static void preloadAssemblies()
46 {
47 #ifdef USE_DEFAULT_BASE_ADDR
48         putenv(const_cast<char *>("COMPlus_UseDefaultBaseAddr=1"));
49 #endif // USE_DEFAULT_BASE_ADDR
50
51         void* coreclrLib = dlopen(__coreclr_lib, RTLD_NOW | RTLD_GLOBAL);
52         if (coreclrLib == nullptr) {
53                 _DBG("dlopen failed to open libcoreclr.so");
54                 return;
55         }
56
57         coreclr_preload_assembly_ptr preloadAssembly;
58         preloadAssembly = (coreclr_preload_assembly_ptr)dlsym(coreclrLib, "coreclr_preload_assembly");
59         if (preloadAssembly == nullptr) {
60                 _DBG("coreclr_preload_assembly is not found in the libcoreclr.so");
61                 return;
62         }
63
64         std::ifstream preloadList(AOT_PRELOAD_PATH);
65         if (preloadList) {
66                 std::string path;
67                 while (getline(preloadList, path)) {
68                         int st = preloadAssembly(absolutePath(path).c_str());
69                         if (st != 0) {
70                                 _DBG("preload of %s failed! (0x%08x)", path.c_str(), st);
71                         } else {
72                                 _DBG("preload of %s succeded", path.c_str());
73                         } 
74                 }
75         }
76 }
77
78 int main(int argc, char** argv) 
79 {
80         hydra_lifecycle_callback_s hydra_callback;
81
82         hydra_callback.precreate = [](void* user_data) {
83                 _INFO("hydra : precreate");
84                 preloadAssemblies();
85         };
86
87         hydra_callback.create = [](void* user_data) {
88                 _INFO("hydra : create");
89         };
90         
91         hydra_callback.fork = [](int argc, char **argv, void* user_data) -> int {
92                 _INFO("hydra : fork");
93                 void* launcher_h = dlopen(__dotnet_launcher, RTLD_NOW | RTLD_GLOBAL);
94                 if (launcher_h == nullptr) {
95                         _DBG("dlopen failed to open dotnet-launcher");
96                         return -1;
97                 }
98
99                 launcher_real_main_ptr realMain = (launcher_real_main_ptr)dlsym(launcher_h, "realMain");
100                 if (realMain == nullptr) {
101                         _DBG("realMain is not found in the dotnet-launcher");
102                         return -1;
103                 }
104
105                 return realMain(argc, argv, "default");                 
106         };
107
108         hydra_callback.terminate = [](void* user_data)-> int {
109                 _INFO("hydra : terminate");
110                 return 0;
111         };
112
113         return launchpad_hydra_main(argc, argv, &hydra_callback, nullptr);
114 }
115