Fixed svace.(HANDLE_LEAK.EX)
[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 = realpath(path.c_str(), NULL);
39         if (realPath) {
40                 absPath.assign(realPath);
41                 free(realPath);
42         }
43
44         return absPath;
45 }
46
47 static void preloadAssemblies()
48 {
49 #ifdef USE_DEFAULT_BASE_ADDR
50         putenv(const_cast<char *>("COMPlus_UseDefaultBaseAddr=1"));
51 #endif // USE_DEFAULT_BASE_ADDR
52
53         void* coreclrLib = dlopen(__coreclr_lib, RTLD_NOW | RTLD_GLOBAL);
54         if (coreclrLib == nullptr) {
55                 _DBG("dlopen failed to open libcoreclr.so");
56                 return;
57         }
58
59         coreclr_preload_assembly_ptr preloadAssembly;
60         preloadAssembly = (coreclr_preload_assembly_ptr)dlsym(coreclrLib, "coreclr_preload_assembly");
61         if (preloadAssembly == nullptr) {
62                 _DBG("coreclr_preload_assembly is not found in the libcoreclr.so");
63                 return;
64         }
65
66         std::ifstream preloadList(AOT_PRELOAD_PATH);
67         if (preloadList) {
68                 std::string path;
69                 while (getline(preloadList, path)) {
70                         int st = preloadAssembly(absolutePath(path).c_str());
71                         if (st != 0) {
72                                 _DBG("preload of %s failed! (0x%08x)", path.c_str(), st);
73                         } else {
74                                 _DBG("preload of %s succeded", path.c_str());
75                         }
76                 }
77         }
78 }
79
80 int main(int argc, char** argv)
81 {
82         hydra_lifecycle_callback_s hydra_callback;
83
84         hydra_callback.precreate = [](void* user_data) {
85                 _INFO("hydra : precreate");
86                 preloadAssemblies();
87         };
88
89         hydra_callback.create = [](void* user_data) {
90                 _INFO("hydra : create");
91         };
92
93         hydra_callback.fork = [](int argc, char **argv, void* user_data) -> int {
94                 _INFO("hydra : fork");
95                 void* launcher_h = dlopen(__dotnet_launcher, RTLD_NOW | RTLD_GLOBAL);
96                 if (launcher_h == nullptr) {
97                         _DBG("dlopen failed to open dotnet-launcher");
98                         return -1;
99                 }
100
101                 launcher_real_main_ptr realMain = (launcher_real_main_ptr)dlsym(launcher_h, "realMain");
102                 if (realMain == nullptr) {
103                         _DBG("realMain is not found in the dotnet-launcher");
104                         dlclose(launcher_h);
105                         return -1;
106                 }
107
108                 return realMain(argc, argv, "default");
109         };
110
111         hydra_callback.terminate = [](void* user_data)-> int {
112                 _INFO("hydra : terminate");
113                 return 0;
114         };
115
116         return launchpad_hydra_main(argc, argv, &hydra_callback, nullptr);
117 }