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