• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <errno.h>
17 #include <limits.h>
18 #include <stdlib.h>
19 #include <signal.h>
20 #include <sys/prctl.h>
21 #include "hdf_base.h"
22 #include "devhost_service.h"
23 #include "devhost_service_full.h"
24 #include "hdf_cstring.h"
25 #include "hdf_log.h"
26 #include "hdf_power_manager.h"
27 #include "securec.h"
28 
29 #define HDF_LOG_TAG hdf_device_host
30 #define DEVHOST_INPUT_PARAM_NUM 3
31 #define DEVHOST_INPUT_PARAM_HOSTID_POS 1
32 
HdfStringToInt(const char * str,int * value)33 bool HdfStringToInt(const char *str, int *value)
34 {
35     if (str == NULL || value == NULL) {
36         return false;
37     }
38 
39     char *end = NULL;
40     errno = 0;
41     const int base = 10;
42     long result = strtol(str, &end, base);
43     if (end == str || end[0] != '\0' || errno == ERANGE || result > INT_MAX || result < INT_MIN) {
44         return false;
45     }
46 
47     *value = (int)result;
48     return true;
49 }
50 
SetProcTitle(char ** argv,const char * newTitle)51 static void SetProcTitle(char **argv, const char *newTitle)
52 {
53     size_t len = strlen(argv[0]);
54     if (strlen(newTitle) > len) {
55         return;
56     }
57     (void)memset_s(argv[0], len, 0, len);
58     if (strcpy_s(argv[0], len + 1, newTitle) != EOK) {
59         return;
60     }
61     prctl(PR_SET_NAME, newTitle);
62 }
63 
main(int argc,char ** argv)64 int main(int argc, char **argv)
65 {
66     if (argc != DEVHOST_INPUT_PARAM_NUM) {
67         HDF_LOGE("Devhost main parameter error, argc: %{public}d", argc);
68         return HDF_ERR_INVALID_PARAM;
69     }
70 
71     prctl(PR_SET_PDEATHSIG, SIGKILL); // host process should exit with devmgr process
72 
73     int hostId = 0;
74     if (!HdfStringToInt(argv[DEVHOST_INPUT_PARAM_HOSTID_POS], &hostId)) {
75         HDF_LOGE("Devhost main parameter error, argv[1]: %{public}s", argv[DEVHOST_INPUT_PARAM_HOSTID_POS]);
76         return HDF_ERR_INVALID_PARAM;
77     }
78     const char *hostName = argv[argc - 1];
79     HDF_LOGD("hdf device host %{public}s %{public}d start", hostName, hostId);
80     SetProcTitle(argv, hostName);
81 
82     struct IDevHostService *instance = DevHostServiceNewInstance(hostId, hostName);
83     if (instance == NULL || instance->StartService == NULL) {
84         HDF_LOGE("DevHostServiceGetInstance fail");
85         return HDF_ERR_INVALID_OBJECT;
86     }
87     int status = instance->StartService(instance);
88     if (status != HDF_SUCCESS) {
89         HDF_LOGE("Devhost StartService fail, return: %{public}d", status);
90         DevHostServiceFreeInstance(instance);
91         return status;
92     }
93     HdfPowerManagerInit();
94     struct DevHostServiceFull *fullService = (struct DevHostServiceFull*)instance;
95     struct HdfMessageLooper *looper = &fullService->looper;
96     if ((looper != NULL) && (looper->Start != NULL)) {
97         looper->Start(looper);
98     }
99 
100     DevHostServiceFreeInstance(instance);
101     HdfPowerManagerExit();
102 
103     return status;
104 }
105 
106