call ecore_init() in the CoreRuntime constructor.
[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 <Eina.h>
26 #include <aul.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 // By the specification, application id must be shorter than 50 characters.
31 // Current length of argv[0] is 25 with a space. ("/usr/bin/dotnet-launcher ")
32 // To be able to change argv[0] when standalone mode padding for executable path is added.
33 #define APPID_MAX_LENGTH        (25 + 105)
34
35 static std::string StandaloneOption("--standalone");
36 static std::string PaddingOption("--PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_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 in 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->initialize(true, false) != 0) {
84                         _ERR("Failed to initialize");
85                         return 1;
86                 }
87
88                 int argsLen = vargs.size() - 1;
89                 char** args = &vargs[1];
90                 if (runtime->launch(appId, appRoot.c_str(), argv[1], argsLen, args)) {
91                         _ERR("Failed to launch");
92                         return 1;
93                 }
94         } else if (standaloneMode) {
95                 _INFO("##### Run in standalone mode #####");
96                 char appId[APPID_MAX_LENGTH] = {0,};
97                 std::string appRoot;
98                 if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
99                         const char* appRootPath = aul_get_app_root_path();
100                         if (appRootPath != nullptr)
101                                 appRoot = std::string(appRootPath);
102                 } else {
103                         // If appId is not set, it is executed directly by cmdline.
104                         // In this case, appRoot is passed as an argument.
105                         snprintf(appId, 16, "%s", "dotnet-launcher");
106                         appRoot = baseName(baseName(standalonePath));
107                 }
108                 _INFO("AUL_APPID : %s", appId);
109
110                 if (runtime->initialize(true, true) != 0) {
111                         _ERR("Failed to initialize");
112                         return 1;
113                 }
114
115                 // change cmdline from dotnet-launcher to executable path
116                 int cmdlineSize = paddingExist ? APPID_MAX_LENGTH : APPID_MAX_LENGTH - PaddingOption.length();
117                 memset(argv[0], '\0', cmdlineSize);
118                 snprintf(argv[0], cmdlineSize, "%s", standalonePath);
119
120                 int argsLen = vargs.size();
121                 char** args = &vargs[0];
122                 if (runtime->launch(appId, appRoot.c_str(), standalonePath, argsLen, args)) {
123                         _ERR("Failed to launch");
124                         return 1;
125                 }
126         } else {
127                 // change cmdline from dotnet-hydra-launcher to dotnet-launcher
128                 if (strcmp(argv[0], "/usr/bin/dotnet-hydra-launcher") == 0) {
129                         memset(argv[0], '\0', strlen("/usr/bin/dotnet-hydra-launcher"));
130                         snprintf(argv[0], strlen("/usr/bin/dotnet-launcher") + 1, "/usr/bin/dotnet-launcher");
131                 }
132
133                 Launchpad.onCreate = [&runtime]() {
134                         if (runtime->initialize(false, true) != 0) {
135                                 _ERR("Failed to initialized");
136                         } else {
137                                 _INFO("Success to initialized");
138                         }
139                 };
140
141                 Launchpad.onTerminate = [&runtime](const AppInfo& appInfo, int argc, char** argv) {
142                         _INFO("launch request with app path : %s", appInfo.path.c_str());
143                         _INFO("appId : %s", appInfo.id.c_str());
144                         _INFO("pkg : %s", appInfo.pkg.c_str());
145
146                         // aul_get_app_root_path() can return NULL for error case.
147                         if (appInfo.root.empty()) {
148                                 _ERR("Failed to launch. root path is set to NULL");
149                         } else {
150                                 // The launchpad pass the name of exe file to the first argument.
151                                 // For the C# spec, we have to skip this first argument.
152                                 if (runtime->launch(appInfo.id.c_str(), appInfo.root.c_str(), appInfo.path.c_str(), argc-1, argv+1))
153                                         _ERR("Failed to launch");
154                         }
155                 };
156                 int ret = Launchpad.loaderMain(argc, argv);
157                 if (ret < 0) {
158                         _ERR("fail to start loaderMain. candidate process is not created.");
159                         return 1;
160                 }
161         }
162
163         return 0;
164 }
165
166 int main(int argc, char *argv[])
167 {
168         return realMain(argc, argv, "default");
169 }