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