Init basesystem source codes.
[staging/basesystem.git] / systemservice / system_manager / server / src / processlauncher / ProcessLauncher_if.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_SystemManager
19 /// \brief    This file provides support for process launching and termination.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include <native_service/frameworkunified_framework_if.h>
23
24 #include "ProcessLauncher.h"
25 #include "ss_sm_process_launcher_protocol.h"
26 #include "ss_sm_systemmanagerlog.h"
27
28 template<typename C, eFrameworkunifiedStatus (C::*M)(HANDLE)> EFrameworkunifiedStatus ProcessLauncherCallback(HANDLE hThread) {
29   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
30
31   C * pObj = static_cast<C *>(FrameworkunifiedGetThreadSpecificData(hThread));
32
33   if (pObj) {  // LCOV_EXCL_BR_LINE 5:pObj must not be NULL
34     l_eStatus = (pObj->*M)(hThread);
35   }
36
37   return l_eStatus;
38 }
39
40 EFrameworkunifiedStatus ProcessLauncherOnStart(HANDLE hThread) {
41   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
42   EFrameworkunifiedStatus l_eStatus;
43   CProcessLauncher * pObj = new (std::nothrow) CProcessLauncher(NULL);  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
44
45   if (pObj) {  // LCOV_EXCL_BR_LINE 5:new error
46     // LCOV_EXCL_BR_START 200:l_eStatus always return eFrameworkunifiedStatusOK
47     if (eFrameworkunifiedStatusOK != (l_eStatus = pObj->PLOnCmdStart(hThread))) {
48     // LCOV_EXCL_BR_STOP
49       // LCOV_EXCL_START 200:l_eStatus always return eFrameworkunifiedStatusOK
50       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
51       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
52               " Error: pObj->PLOnCmdStart(hThread) errored: %d/'%s'",
53               l_eStatus, GetStr(l_eStatus).c_str());
54       // LCOV_EXCL_STOP
55     } else {
56       FrameworkunifiedSetThreadSpecificData(hThread, pObj);
57
58       FrameworkunifiedProtocolCallbackHandler aParentHandlers[] = { {
59               ePLThrdCmd_LAUNCH_MODULE_REQST, ProcessLauncherCallback<
60                       CProcessLauncher,
61                       &CProcessLauncher::PLOnCmdLaunchModule> }, {
62               ePLThrdCmd_TERMINATE_MODULE_REQST, ProcessLauncherCallback<
63                       CProcessLauncher,
64                       &CProcessLauncher::PLOnCmdTerminateModule> }, {
65               ePLThrdCmd_RELAUNCH_MODULE_REQST, ProcessLauncherCallback<
66                       CProcessLauncher,
67                       &CProcessLauncher::PLOnCmdRelaunchModule> }, {
68               ePLThrdCmd_MODULE_STATUS_REQST, ProcessLauncherCallback<
69                       CProcessLauncher,
70                       &CProcessLauncher::PLOnCmdModuleStatus> }, {
71               ePLThrdCmd_THREAD_STATUS_REQST, ProcessLauncherCallback<
72                       CProcessLauncher,
73                       &CProcessLauncher::PLOnCmdHeartbeatStatusReq> } };  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
74
75       // LCOV_EXCL_BR_START 4:NSFW error case
76       if (eFrameworkunifiedStatusOK
77           != (l_eStatus = FrameworkunifiedAttachParentCallbacksToDispatcher(hThread, aParentHandlers,
78                                                                static_cast<UI_32>(_countof(aParentHandlers))))) {
79       // LCOV_EXCL_BR_STOP
80         // LCOV_EXCL_START 4:NSFW error case
81         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
82         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
83                 " Error: FrameworkunifiedAttachParentCallbacksToDispatcher() errored: %d/'%s'",
84                 l_eStatus, GetStr(l_eStatus).c_str());
85         // LCOV_EXCL_STOP
86       } else {
87         // Add other Attaches here!!!
88       }
89     }
90   } else {  // LCOV_EXCL_BR_LINE 5::new operation failed
91     // LCOV_EXCL_START 5:new error
92     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
93     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: new CProcessLauncher( NULL ) returned NULL");
94     l_eStatus = eFrameworkunifiedStatusNullPointer;
95     // LCOV_EXCL_STOP
96   }
97   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
98   return l_eStatus;
99 }
100
101 // LCOV_EXCL_START 200:Thread which registered in init_process_launcher() is never stoppped.
102 EFrameworkunifiedStatus ProcessLauncherOnStop(HANDLE hThread) {
103   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
104   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
105   EFrameworkunifiedStatus l_eStatus;
106   CProcessLauncher * pObj = static_cast<CProcessLauncher *>(FrameworkunifiedGetThreadSpecificData(hThread));
107
108   if (pObj) {
109     if (eFrameworkunifiedStatusOK != (l_eStatus = pObj->PLOnCmdStop(hThread))) {
110       LOG_ERROR("PLOnCmdStop()");  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h  // NOLINT(whitespace/line_length)
111     }
112     delete pObj;
113     pObj = NULL;
114   }
115
116   FrameworkunifiedSetThreadSpecificData(hThread, NULL);
117
118   if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedDetachServiceFromDispatcher(hThread, FrameworkunifiedGetAppName(hThread)))) {
119     LOG_ERROR("FrameworkunifiedDetachServiceFromDispatcher()");  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h  // NOLINT(whitespace/line_length)
120   }
121
122   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
123   return l_eStatus;
124 }
125 // LCOV_EXCL_STOP
126