change candidate process name to dotnet-launcher
[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         // Intiailize ecore first (signal handlers, etc.) before runtime init.
77         ecore_init();
78
79         if (corerunMode) {
80                 _INFO("##### Run in corerun mode #####");
81                 char appId[APPID_MAX_LENGTH] = {0,};
82                 std::string appRoot;
83                 snprintf(appId, 16, "%s", "dotnet-launcher");
84                 appRoot = baseName(argv[1]);
85
86                 if (runtime->initialize(true) != 0) {
87                         _ERR("Failed to initialize");
88                         return 1;
89                 }
90
91                 int argsLen = vargs.size() - 1;
92                 char** args = &vargs[1];
93                 if (runtime->launch(appId, appRoot.c_str(), argv[1], argsLen, args)) {
94                         _ERR("Failed to launch");
95                         return 1;
96                 }
97         } else if (standaloneMode) {
98                 _INFO("##### Run in standalone mode #####");
99                 char appId[APPID_MAX_LENGTH] = {0,};
100                 std::string appRoot;
101                 if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
102                         const char* appRootPath = aul_get_app_root_path();
103                         if (appRootPath != nullptr)
104                                 appRoot = std::string(appRootPath);
105                 } else {
106                         // If appId is not set, it is executed directly by cmdline.
107                         // In this case, appRoot is passed as an argument.
108                         snprintf(appId, 16, "%s", "dotnet-launcher");
109                         appRoot = baseName(baseName(standalonePath));
110                 }
111                 _INFO("AUL_APPID : %s", appId);
112
113                 if (runtime->initialize(true) != 0) {
114                         _ERR("Failed to initialize");
115                         return 1;
116                 }
117
118                 // change cmdline from dotnet-launcher to executable path
119                 int cmdlineSize = paddingExist ? APPID_MAX_LENGTH : APPID_MAX_LENGTH - PaddingOption.length();
120                 memset(argv[0], '\0', cmdlineSize);
121                 snprintf(argv[0], cmdlineSize, "%s", appId);
122
123                 int argsLen = vargs.size();
124                 char** args = &vargs[0];
125                 if (runtime->launch(appId, appRoot.c_str(), standalonePath, argsLen, args)) {
126                         _ERR("Failed to launch");
127                         return 1;
128                 }
129         } else {
130                 // change cmdline from dotnet-hydra-launcher to dotnet-launcher
131                 if (strcmp(argv[0], "/usr/bin/dotnet-hydra-launcher") == 0) {
132                         memset(argv[0], '\0', strlen("/usr/bin/dotnet-hydra-launcher"));
133                         snprintf(argv[0], strlen("/usr/bin/dotnet-launcher") + 1, "/usr/bin/dotnet-launcher");
134                 }
135
136                 Launchpad.onCreate = [&runtime]() {
137                         if (runtime->initialize(false) != 0)
138                                 _ERR("Failed to initialized");
139                         else
140                                 _INFO("Success to initialized");
141                 };
142
143                 Launchpad.onTerminate = [&runtime](const AppInfo& appInfo, int argc, char** argv) {
144                         _INFO("launch request with app path : %s", appInfo.path.c_str());
145                         _INFO("appId : %s", appInfo.id.c_str());
146                         _INFO("pkg : %s", appInfo.pkg.c_str());
147
148                         // aul_get_app_root_path() can return NULL for error case.
149                         if (appInfo.root.empty()) {
150                                 _ERR("Failed to launch. root path is set to NULL");
151                         } else {
152                                 // The launchpad pass the name of exe file to the first argument.
153                                 // For the C# spec, we have to skip this first argument.
154                                 if (runtime->launch(appInfo.id.c_str(), appInfo.root.c_str(), appInfo.path.c_str(), argc-1, argv+1))
155                                         _ERR("Failed to launch");
156                         }
157                 };
158                 int ret = Launchpad.loaderMain(argc, argv);
159                 if (ret < 0) {
160                         _ERR("fail to start loaderMain. candidate process is not created.");
161                         return 1;
162                 }
163         }
164
165         return 0;
166 }
167
168 int main(int argc, char *argv[])
169 {
170         return realMain(argc, argv, "default");
171 }