3db30b38e64c2e1a739b2ceadd3abf7d639fa5f6
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / main.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 #include "injection.h"
18 #include "dotnet/dotnet_launcher.h"
19 #include "utils.h"
20 #include "log.h"
21
22 #include <cstdio>
23 #include <vector>
24 #include <memory>
25
26 #include <Ecore.h>
27 #include <Eina.h>
28 #include <aul.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #define CMD_LINE_SIZE   24      // sizeof("/usr/bin/dotnet-launcher")
33
34 static std::string StandaloneOption("--standalone");
35
36 extern "C" int realMain(int argc, char *argv[], const char* mode)
37 {
38         // checkInjection checks dotnet-launcher run mode,
39         // if it contains DOTNET_LAUNCHER_INJECT variable, it injects library.
40         // At the moment, this mechanism is used only when the Memory Profiler is started.
41         int res = checkInjection();
42         if (res != 0) {
43                 return 1;
44         }
45
46         int i;
47         bool standaloneMode = false;
48         char* standalonePath = nullptr;
49         bool corerunMode = false;
50
51         std::vector<char*> vargs;
52
53         // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
54         for (i = 1; i < argc; i++) {
55                 if (StandaloneOption.compare(argv[i]) == 0) {
56                         standaloneMode = true;
57
58                         if (i > argc-1) {
59                                 fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
60                                 return 1;
61                         }
62                         i++;
63                         standalonePath = argv[i];
64                 } else {
65                         vargs.push_back(argv[i]);
66                 }
67         }
68
69         if (isManagedAssembly(argv[1]) || isNativeImage(argv[1])) {
70                 corerunMode = true;
71         }
72
73         using tizen::runtime::Launchpad;
74         using tizen::runtime::AppInfo;
75         using tizen::runtime::dotnetcore::CoreRuntime;
76
77         std::unique_ptr<CoreRuntime> runtime(new CoreRuntime(mode));
78
79         if (corerunMode) {
80                 _INFO("##### Run it corerun Mode #########");
81                 char appId[1024] = {0,};
82                 std::string appRoot;
83                 snprintf(appId, 16, "%s", "dotnet-launcher");
84                 appRoot = baseName(argv[1]);
85
86                 if (runtime->initialize(true) != 0) {
87                         _ERR("Failed to initialize");
88                         return 1;
89                 }
90
91                 int argsLen = vargs.size() - 1;
92                 char** args = &vargs[1];
93                 if (runtime->launch(appId, appRoot.c_str(), argv[1], argsLen, args)) {
94                         _ERR("Failed to launch");
95                         return 1;
96                 }
97         } else if (standaloneMode) {
98                 _INFO("##### Run it standalone Mode #########");
99                 char appId[1024] = {0,};
100                 std::string appRoot;
101                 if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
102                         const char* appRootPath = aul_get_app_root_path();
103                         if (appRootPath != nullptr)
104                                 appRoot = std::string(appRootPath);
105                 } else {
106                         // If appId is not set, it is executed directly by cmdline.
107                         // In this case, appRoot is passed as an argument.
108                         snprintf(appId, 16, "%s", "dotnet-launcher");
109                         appRoot = baseName(baseName(standalonePath));
110                 }
111                 _INFO("AUL_APPID : %s", appId);
112
113                 if (runtime->initialize(true) != 0) {
114                         _ERR("Failed to initialize");
115                         return 1;
116                 }
117
118                 // change cmdline from dotnet-launcher to executable path
119                 memset(argv[0], '\0', CMD_LINE_SIZE);
120                 snprintf(argv[0], CMD_LINE_SIZE - 1, "%s", appId);
121
122                 int argsLen = vargs.size();
123                 char** args = &vargs[0];
124                 if (runtime->launch(appId, appRoot.c_str(), standalonePath, argsLen, args)) {
125                         _ERR("Failed to launch");
126                         return 1;
127                 }
128         } else {
129                 Launchpad.onCreate = [&runtime]() {
130                         if (runtime->initialize(false) != 0)
131                                 _ERR("Failed to initialized");
132                         else
133                                 _INFO("Success to initialized");
134                 };
135
136                 Launchpad.onTerminate = [&runtime](const AppInfo& appInfo, int argc, char** argv) {
137                         _INFO("launch request with app path : %s", appInfo.path.c_str());
138                         _INFO("appId : %s", appInfo.id.c_str());
139                         _INFO("pkg : %s", appInfo.pkg.c_str());
140
141                         // aul_get_app_root_path() can return NULL for error case.
142                         if (appInfo.root.empty()) {
143                                 _ERR("Failed to launch. root path is set to NULL");
144                         } else {
145                                 // The launchpad pass the name of exe file to the first argument.
146                                 // For the C# spec, we have to skip this first argument.
147                                 if (runtime->launch(appInfo.id.c_str(), appInfo.root.c_str(), appInfo.path.c_str(), argc-1, argv+1))
148                                         _ERR("Failed to launch");
149                         }
150                 };
151                 int ret = Launchpad.loaderMain(argc, argv);
152                 if (ret < 0) {
153                         _ERR("fail to start loaderMain. candidate process is not created.");
154                         return 1;
155                 }
156         }
157
158         return 0;
159 }
160
161 int main(int argc, char *argv[])
162 {
163         return realMain(argc, argv, "default");
164 }