Modify tizen coding style
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / dotnet / dotnet_launcher.cc
1 /*
2  * Copyright (c) 2016 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
18 #include <dlfcn.h>
19
20 #include <string>
21 #include <fstream>
22 #include <vector>
23
24 #include "utils.h"
25 #include "log.h"
26 #include "launcher.h"
27 #include "dotnet_launcher.h"
28
29 namespace tizen {
30 namespace runtime {
31 namespace dotnetcore {
32
33 CoreRuntime::CoreRuntime() :
34         initializeClr(nullptr),
35         executeAssembly(nullptr),
36         shutdown(nullptr),
37         createDelegate(nullptr),
38         __coreclrLib(nullptr),
39         __hostHandle(nullptr),
40         __domainId(-1),
41         preparedFunction(nullptr),
42         launchFunction(nullptr)
43 {
44 #define __XSTR(x) #x
45 #define __STR(x) __XSTR(x)
46
47 #ifdef DEVICE_API_DIR
48         __deviceAPIDirectory = __STR(DEVICE_API_DIR);
49 #endif
50 #ifdef RUNTIME_DIR
51         __runtimeDirectory = __STR(RUNTIME_DIR);
52 #endif
53 #ifdef NATIVE_LIB_DIR
54         __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
55 #endif
56
57 #ifdef USE_MANAGED_LAUNCHER
58 #ifdef CORECLR_LAUNCHER_ASSEMBLY_PATH
59         __launcherAssembly = __STR(CORECLR_LAUNCHER_ASSEMBLY_PATH);
60 #endif
61 #endif
62
63 #undef __STR
64 #undef __XSTR
65
66         _DBG("Constructor called!!");
67 }
68
69 CoreRuntime::~CoreRuntime()
70 {
71         dispose();
72 }
73
74 int CoreRuntime::initialize(bool standalone)
75 {
76         if (standalone) {
77                 const char *deviceApiDirectory = getenv("__deviceAPIDirectory");
78                 const char *runtimeDirectory = getenv("__runtimeDirectory");
79                 if (deviceApiDirectory != nullptr)
80                         __deviceAPIDirectory = deviceApiDirectory;
81                 if (runtimeDirectory != nullptr)
82                         __runtimeDirectory = runtimeDirectory;
83
84 #ifdef USE_MANAGED_LAUNCHER
85                 const char *launcherAssembly = getenv("__launcherAssembly");
86                 if (launcherAssembly != nullptr)
87                         __launcherAssembly = launcherAssembly;
88 #endif
89         }
90
91         if (__deviceAPIDirectory.empty()) {
92                 _ERR("Empty Device API Directory");
93                 return 1;
94         } else {
95                 __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
96         }
97
98         if (__runtimeDirectory.empty()) {
99                 _ERR("Empty Runtime Directory");
100                 return 1;
101         } else {
102                 __runtimeDirectory = absolutePath(__runtimeDirectory);
103         }
104
105 #ifdef USE_MANAGED_LAUNCHER
106         if (__launcherAssembly.empty()) {
107                 _ERR("Empty Launcher Assembly");
108                 return 1;
109         } else {
110                 __launcherAssembly = absolutePath(__launcherAssembly);
111         }
112 #endif
113
114         std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
115
116         _DBG("libcoreclr : %s", libCoreclr.c_str());
117
118         __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
119         if (__coreclrLib == nullptr) {
120                 char *err = dlerror();
121                 _ERR("dlopen failed to open libcoreclr.so with error %s", err);
122                 return 1;
123         }
124
125 #define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
126         do { \
127                 variable = (type)dlsym(__coreclrLib, name); \
128                 if (variable == nullptr) { \
129                         _ERR(name " is not found in the libcoreclr.so"); \
130                         return 1; \
131                 } \
132         } while (0)
133
134         CORELIB_RETURN_IF_NOSYM(coreclr_initialize_ptr, initializeClr, "coreclr_initialize");
135         CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
136         CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
137         CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
138
139 #undef CORELIB_RETURN_IF_NOSYM
140
141         _DBG("libcoreclr dlopen and dlsym success");
142         _DBG("this addr : %x", this);
143         _DBG("coreclr_initialize : %x", initializeClr);
144
145         return 0;
146 }
147
148 bool CoreRuntime::initializeCoreClr(const char* appId,
149                                                                          const char* assemblyProbePaths,
150                                                                          const char* pinvokeProbePaths,
151                                                                          const char* tpaList)
152 {
153         const char *propertyKeys[] = {
154                 "TRUSTED_PLATFORM_ASSEMBLIES",
155                 "APP_PATHS",
156                 "APP_NI_PATHS",
157                 "NATIVE_DLL_SEARCH_DIRECTORIES",
158                 "AppDomainCompatSwitch"
159         };
160
161         const char *propertyValues[] = {
162                 tpaList,
163                 assemblyProbePaths,
164                 assemblyProbePaths,
165                 pinvokeProbePaths,
166                 "UseLatestBehaviorWhenTFMNotSpecified"
167         };
168
169         std::string selfPath = readSelfPath();
170
171         int st = initializeClr(selfPath.c_str(),
172                                                         appId,
173                                                         sizeof(propertyKeys) / sizeof(propertyKeys[0]),
174                                                         propertyKeys,
175                                                         propertyValues,
176                                                         &__hostHandle,
177                                                         &__domainId);
178
179         if (st < 0) {
180                 _ERR("initialize core clr fail! (0x%08x)", st);
181                 return false;
182         }
183
184         _DBG("Initialize core clr success");
185         return true;
186 }
187
188 int CoreRuntime::runManagedLauncher(const char* appId, const char* appBase, const char* tpaList)
189 {
190         if (fileNotExist(__launcherAssembly)) {
191                 _ERR("Launcher assembly is not exist in %s", __launcherAssembly.c_str());
192                 return 1;
193         }
194
195         if (!initializeCoreClr(appId, appBase, appBase, tpaList)) {
196                 _ERR("Failed to initialize coreclr");
197                 return 1;
198         }
199
200 #ifdef USE_MANAGED_LAUNCHER
201         void *preparedFunctionDelegate;
202         int st = createDelegate(__hostHandle, __domainId,
203                                                         "Tizen.Runtime",
204                                                         "Tizen.Runtime.Coreclr.AssemblyManager",
205                                                         "Prepared", &preparedFunctionDelegate);
206         if (st < 0) {
207                 _ERR("Create delegate for Launch prepared function is fail (0x%08x)", st);
208                 return 1;
209         }
210         preparedFunction = reinterpret_cast<PreparedFunctionPtr>(preparedFunctionDelegate);
211
212         if (preparedFunction != nullptr)
213                 preparedFunction();
214
215         void *launchFunctionDelegate;
216         st = createDelegate(__hostHandle, __domainId,
217                                                 "Tizen.Runtime",
218                                                 "Tizen.Runtime.Coreclr.AssemblyManager",
219                                                 "Launch", &launchFunctionDelegate);
220         if (st < 0) {
221                 _ERR("Create delegate for Launch managed function is fail! (0x%08x)", st);
222                 return 1;
223         }
224         launchFunction = reinterpret_cast<LaunchFunctionPtr>(launchFunctionDelegate);
225 #endif
226         return 0;
227 }
228
229 void CoreRuntime::dispose()
230 {
231         if (__hostHandle != nullptr) {
232                 int st = shutdown(__hostHandle, __domainId);
233                 if (st < 0)
234                         _ERR("shutdown core clr fail! (0x%08x)", st);
235         }
236
237         if (dlclose(__coreclrLib) != 0)
238                 _ERR("libcoreclr.so close failed");
239
240         __coreclrLib = nullptr;
241
242         _DBG("Dotnet runtime disposed");
243 }
244
245 int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
246 {
247         if (path == nullptr) {
248                 _ERR("executable path is null");
249                 return 1;
250         }
251
252         if (fileNotExist(path)) {
253                 _ERR("File not exist : %s", path);
254                 return 1;
255         }
256
257         std::string tpa;
258         std::string appRoot = root;
259         std::string appBin = concatPath(appRoot, "bin");
260         std::string appLib = concatPath(appRoot, "lib");
261         std::string probePath = appBin + ":" + appLib + ":" + __nativeLibDirectory;
262
263         std::vector<std::string> searchDirectories;
264         searchDirectories.push_back(appBin);
265         searchDirectories.push_back(appLib);
266         searchDirectories.push_back(__runtimeDirectory);
267         searchDirectories.push_back(__deviceAPIDirectory);
268 #ifdef USE_MANAGED_LAUNCHER
269         searchDirectories.push_back(baseName(__launcherAssembly));
270 #endif
271
272         assembliesInDirectory(searchDirectories, tpa);
273
274 #ifdef USE_MANAGED_LAUNCHER
275         runManagedLauncher(appId, probePath.c_str(), tpa.c_str());
276
277         bool success = false;
278         if (launchFunction != nullptr) {
279                 std::string cppPath(path);
280
281                 if (isManagedAssembly(cppPath) && !isNativeImage(cppPath)) {
282                         size_t extindex = cppPath.size() - 4;
283                         cppPath = cppPath.substr(0, extindex) + ".ni" + cppPath.substr(extindex, 4);
284                         if (!fileNotExist(cppPath))
285                                 path = cppPath.c_str();
286                 }
287
288                 success = launchFunction(root, path, argc, argv);
289                 if (!success)
290                         _ERR("Failed to launch Application %s", path);
291                 return success ? 0 : 1;
292         } else {
293                 _ERR("Failed to find launch function");
294                 return 1;
295         }
296 #else
297         int st = initializeCoreClr(appId, probePath.c_str(), probePath.c_str(), tpa.c_str());
298         unsigned int ret = 0;
299         st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
300         if (st < 0)
301                 _ERR("Failed to Execute Assembly %s (0x%08x)", path, st);
302         return ret;
303 #endif
304 }
305
306 }  // namespace dotnetcore
307 }  // namespace runtime
308 }  // namespace tizen