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