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