2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "dotnet/dotnet_launcher.h"
28 #include <sys/types.h>
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
35 static std::string StandaloneOption("--standalone");
36 static std::string PaddingOption("--PADDING_TO_CHANGE_CMDLINE");
38 extern "C" int realMain(int argc, char *argv[], const char* mode)
41 bool standaloneMode = false;
42 char* standalonePath = nullptr;
43 bool corerunMode = false;
44 bool paddingExist = false;
46 std::vector<char*> vargs;
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;
54 fprintf(stderr, "Assembly path must be after \"--standalone\" option\n");
58 standalonePath = argv[i];
59 } else if (PaddingOption.compare(argv[i]) == 0) {
62 vargs.push_back(argv[i]);
66 if (isManagedAssembly(argv[1]) || isNativeImage(argv[1])) {
70 using tizen::runtime::Launchpad;
71 using tizen::runtime::AppInfo;
72 using tizen::runtime::dotnetcore::CoreRuntime;
74 std::unique_ptr<CoreRuntime> runtime(new CoreRuntime(mode));
76 // Intiailize ecore first (signal handlers, etc.) before runtime init.
80 _INFO("##### Run in corerun mode #####");
81 char appId[APPID_MAX_LENGTH] = {0,};
83 snprintf(appId, 16, "%s", "dotnet-launcher");
84 appRoot = baseName(argv[1]);
86 if (runtime->preinitialize(true) != 0) {
87 _ERR("Failed to preinitialized");
91 if (runtime->initialize(true) != 0) {
92 _ERR("Failed to initialize");
96 int argsLen = vargs.size() - 1;
97 char** args = &vargs[1];
98 if (runtime->launch(appId, appRoot.c_str(), argv[1], argsLen, args)) {
99 _ERR("Failed to launch");
102 } else if (standaloneMode) {
103 _INFO("##### Run in standalone mode #####");
104 char appId[APPID_MAX_LENGTH] = {0,};
106 if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
107 const char* appRootPath = aul_get_app_root_path();
108 if (appRootPath != nullptr)
109 appRoot = std::string(appRootPath);
111 // If appId is not set, it is executed directly by cmdline.
112 // In this case, appRoot is passed as an argument.
113 snprintf(appId, 16, "%s", "dotnet-launcher");
114 appRoot = baseName(baseName(standalonePath));
116 _INFO("AUL_APPID : %s", appId);
118 if (runtime->preinitialize(true) != 0) {
119 _ERR("Failed to preinitialized");
123 if (runtime->initialize(true) != 0) {
124 _ERR("Failed to initialize");
128 // change cmdline from dotnet-launcher to executable path
129 int cmdlineSize = paddingExist ? APPID_MAX_LENGTH : APPID_MAX_LENGTH - PaddingOption.length();
130 memset(argv[0], '\0', cmdlineSize);
131 snprintf(argv[0], cmdlineSize, "%s", appId);
133 int argsLen = vargs.size();
134 char** args = &vargs[0];
135 if (runtime->launch(appId, appRoot.c_str(), standalonePath, argsLen, args)) {
136 _ERR("Failed to launch");
140 Launchpad.onPreCreate = [&runtime]() {
141 if (runtime->preinitialize(false) != 0)
142 _ERR("Failed to preinitialized");
144 _INFO("Success to preinitialized");
147 Launchpad.onCreate = [&runtime]() {
148 if (runtime->initialize(false) != 0)
149 _ERR("Failed to initialized");
151 _INFO("Success to initialized");
154 Launchpad.onTerminate = [&runtime](const AppInfo& appInfo, int argc, char** argv) {
155 _INFO("launch request with app path : %s", appInfo.path.c_str());
156 _INFO("appId : %s", appInfo.id.c_str());
157 _INFO("pkg : %s", appInfo.pkg.c_str());
159 // aul_get_app_root_path() can return NULL for error case.
160 if (appInfo.root.empty()) {
161 _ERR("Failed to launch. root path is set to NULL");
163 // The launchpad pass the name of exe file to the first argument.
164 // For the C# spec, we have to skip this first argument.
165 if (runtime->launch(appInfo.id.c_str(), appInfo.root.c_str(), appInfo.path.c_str(), argc-1, argv+1))
166 _ERR("Failed to launch");
169 int ret = Launchpad.loaderMain(argc, argv);
171 _ERR("fail to start loaderMain. candidate process is not created.");
179 int main(int argc, char *argv[])
181 return realMain(argc, argv, "default");