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