print HW clock log (#385)
[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 std::string StandaloneOption("--standalone");
40 static std::string PaddingOption("--PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_CMDLINE");
41 static std::string AppTypeOption("--appType");
42 static std::string ProfileOption("--profile");
43
44 int main(int argc, char *argv[])
45 {
46         printHWClockLog("[dotnet-launcher] launcher main!");
47         _INFO("##### Run in standalone mode #####");
48
49         char* standalonePath = nullptr;
50         bool paddingExist = false;
51         bool profile = false;
52         const char* appType = NULL;
53         const char* appRootPath = NULL;
54         char appId[APPID_MAX_LENGTH] = {0,};
55
56         std::vector<char*> vargs;
57
58         // start index 1 to avoid passing executable name "dotnet-launcher" as a parameter
59         for (int i = 1; i < argc; i++) {
60                 if (StandaloneOption.compare(argv[i]) == 0) {
61                         if (i > argc - 1) {
62                                 _ERR("Assembly path must be after \"--standalone\" option");
63                                 return -1;
64                         }
65                         i++;
66                         standalonePath = argv[i];
67                 } else if (PaddingOption.compare(argv[i]) == 0) {
68                         paddingExist = true;
69                 } else if (ProfileOption.compare(argv[i]) == 0) {
70                         profile = true;
71                 } else if (AppTypeOption.compare(argv[i]) == 0) {
72                         if (i > argc - 1) {
73                                 _ERR("app type for launchpad must be after \"--appType\" option");
74                                 return -1;
75                         }
76                         i++;
77                         appType = argv[i];
78                 } else {
79                         vargs.push_back(argv[i]);
80                 }
81         }
82
83         if (appType == NULL) {
84                 appType = "dotnet";
85         }
86
87         // get app ID and app root path
88         if (AUL_R_OK == aul_app_get_appid_bypid(getpid(), appId, sizeof(appId))) {
89                 _INFO("AUL_APPID : %s", appId);
90                 // aul_get_app_root_path returns const char*, so there is no need to free after use.
91                 appRootPath = aul_get_app_root_path();
92                 if (appRootPath == nullptr) {
93                         _ERR("Fail to get application root path");
94                         return -1;
95                 }
96         } else {
97                 _ERR("Fail to get app_id");
98                 return -1;
99         }
100
101         // set command name to assembly file
102         setCmdName(getFileName(standalonePath));
103
104         // change cmdline from dotnet-launcher to executable path
105         int cmdlineSize = paddingExist ? APPID_MAX_LENGTH : APPID_MAX_LENGTH - PaddingOption.length();
106         memset(argv[0], '\0', cmdlineSize);
107         snprintf(argv[0], cmdlineSize - 1, "%s", standalonePath);
108
109         // initialize CoreRuntime
110         int err = CoreRuntime::initialize(appType, LaunchMode::launcher);
111         if (err) {
112                 _ERR("Failed to initialize");
113         } else {
114                 // launch application
115                 err = CoreRuntime::launch(appId, appRootPath, standalonePath, vargs.size(), &vargs[0], profile);
116                 if (err) {
117                         _ERR("Failed to launch");
118                 }
119         }
120
121         // finalize CoreRuntime
122         CoreRuntime::finalize();
123
124         return err;
125 }