d26d7f9667babf2fa6b4058ec4bcacbc43ee340d
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / exec / launcher.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 "core_runtime.h"
18 #include "utils.h"
19 #include "log.h"
20
21 #include <cstdio>
22 #include <vector>
23 #include <memory>
24
25 #include <aul.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <sys/prctl.h>
30
31 using tizen::runtime::dotnetcore::CoreRuntime;
32
33 // By the specification, application id must be shorter than 50 characters.
34 // Current length of argv[0] is 25 with a space. ("/usr/bin/dotnet-launcher ")
35 // To be able to change argv[0] when standalone mode padding for executable path is added.
36 #define APPID_MAX_LENGTH        (25 + 105)
37 #define PRC_NAME_LENGTH         16
38
39 static const char* KEY_TIZEN_UIFW = "TIZEN_UIFW";
40 static std::string StandaloneOption("--standalone");
41 static std::string PaddingOption("--PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE");
42 static std::string AppTypeOption("--appType");
43 static std::string UIFWOption(KEY_TIZEN_UIFW);
44 static std::string ProfileOption("--profile");
45 static std::string GlobalizationInvariantOption("--invariant");
46
47 int main(int argc, char *argv[])
48 {
49         printHWClockLog("[dotnet-launcher] launcher main!");
50         _INFO("##### Run in standalone mode #####");
51
52         char* standalonePath = nullptr;
53         bool paddingExist = false;
54         bool profile = false;
55         const char* appType = "dotnet";
56         const char* UIFWType = "ElmSharp";
57         const char* appRootPath = NULL;
58         char appId[APPID_MAX_LENGTH] = {0,};
59
60         std::vector<char*> vargs;
61
62         // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
63         for (int i = 1; i < argc; i++) {
64
65                 if (GlobalizationInvariantOption.compare(argv[i]) == 0) {
66                         setenv("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "1", 1);
67                         setenv("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", "false", 1);
68                 } else if (StandaloneOption.compare(argv[i]) == 0) {
69                         if (i > argc - 1) {
70                                 _ERR("Assembly path must be after %s option", StandaloneOption.c_str());
71                                 return -1;
72                         }
73                         i++;
74                         standalonePath = argv[i];
75                 } else if (PaddingOption.compare(argv[i]) == 0) {
76                         paddingExist = true;
77                 } else if (ProfileOption.compare(argv[i]) == 0) {
78                         profile = true;
79                 } else if (AppTypeOption.compare(argv[i]) == 0) {
80                         if (i > argc - 1) {
81                                 _ERR("app type for launchpad must be after %s option", AppTypeOption.c_str());
82                                 return -1;
83                         }
84                         i++;
85                         appType = argv[i];
86                 } else if (UIFWOption.compare(argv[i]) == 0) {
87                         if (i > argc - 1) {
88                                 _ERR("UIFW type for launchpad must be after %s option", UIFWOption.c_str());
89                                 return -1;
90                         }
91                         i++;
92                         UIFWType = argv[i];
93                 } else {
94                         vargs.push_back(argv[i]);
95                 }
96         }
97
98         // get app ID and app root path
99         if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
100                 _INFO("AUL_APPID : %s", appId);
101                 // aul_get_app_root_path returns const char*, so there is no need to free after use.
102                 appRootPath = aul_get_app_root_path();
103                 if (appRootPath == nullptr) {
104                         _ERR("Fail to get application root path");
105                         return -1;
106                 }
107         } else {
108                 _ERR("Fail to get app_id");
109                 return -1;
110         }
111
112         // set command name to assembly file
113         setCmdName(getFileName(standalonePath));
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 - 1, "%s", standalonePath);
119
120         setenv(KEY_TIZEN_UIFW, UIFWType, 1);
121         _INFO("TIZEN_UIFW is set to %s", UIFWType);
122
123         // initialize CoreRuntime
124         int err = CoreRuntime::initialize(appType, LaunchMode::launcher);
125         if (err) {
126                 _ERR("Failed to initialize");
127         } else {
128                 // launch application
129                 err = CoreRuntime::launch(appId, appRootPath, standalonePath, vargs.size(), &vargs[0], profile);
130                 if (err) {
131                         _ERR("Failed to launch");
132                 }
133         }
134
135         // finalize CoreRuntime
136         CoreRuntime::finalize();
137
138         return err;
139 }