1 /*
2 * Copyright (c) 2024 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 <unistd.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <sys/wait.h>
20
21 #include "hilog/log.h"
22 #include "appspawn_utils.h"
23
24 #include "hnp_api.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #define HNPAPI_LOG(fmt, ...) \
31 HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__)
32 #define MAX_ARGV_NUM 256
33 #define MAX_ENV_NUM (128 + 2)
34 #define IS_OPTION_SET(x, option) ((x) & (1 << (option)))
35
36 /* 数字索引 */
37 enum {
38 HNP_INDEX_0 = 0,
39 HNP_INDEX_1,
40 HNP_INDEX_2,
41 HNP_INDEX_3,
42 HNP_INDEX_4,
43 HNP_INDEX_5,
44 HNP_INDEX_6,
45 HNP_INDEX_7
46 };
47
StartHnpProcess(char * const argv[],char * const apcEnv[])48 static int StartHnpProcess(char *const argv[], char *const apcEnv[])
49 {
50 pid_t pid;
51 int ret;
52 int status;
53 int exitVal = -1;
54
55 pid = vfork();
56 if (pid < 0) {
57 HNPAPI_LOG("\r\n [HNP API] fork unsuccess!\r\n");
58 return HNP_API_ERRNO_FORK_FAILED;
59 } else if (pid == 0) {
60 HNPAPI_LOG("\r\n [HNP API] this is fork children!\r\n");
61 ret = execve("/system/bin/hnp", argv, apcEnv);
62 if (ret < 0) {
63 HNPAPI_LOG("\r\n [HNP API] execve unsuccess!\r\n");
64 _exit(-1);
65 }
66 _exit(0);
67 }
68
69 HNPAPI_LOG("\r\n [HNP API] this is fork father! chid=%d\r\n", pid);
70 if (waitpid(pid, &status, 0) == -1) {
71 HNPAPI_LOG("\r\n [HNP API] wait pid unsuccess!\r\n");
72 return HNP_API_WAIT_PID_FAILED;
73 }
74 if (WIFEXITED(status)) {
75 exitVal = WEXITSTATUS(status);
76 }
77
78 if (WIFSIGNALED(status)) {
79 exitVal = WTERMSIG(status);
80 }
81 HNPAPI_LOG("\r\n [HNP API] Child process exited with exitval=%d\r\n", exitVal);
82
83 return exitVal;
84 }
85
NativeInstallHnpEx(const char * userId,const char * packages[],int count,const char * installPath,int installOptions)86 int NativeInstallHnpEx(const char *userId, const char *packages[], int count, const char *installPath, int installOptions)
87 {
88 char *argv[MAX_ARGV_NUM] = {0};
89 char *apcEnv[MAX_ENV_NUM] = {0};
90 int index = 0;
91
92 if (!IsDeveloperModeOpen()) {
93 HNPAPI_LOG("\r\n [HNP API] native package install not in developer mode");
94 return HNP_API_NOT_IN_DEVELOPER_MODE;
95 }
96
97 if ((userId == NULL) || (packages == NULL) || (count == 0)) {
98 return HNP_API_ERRNO_PARAM_INVALID;
99 }
100
101 HNPAPI_LOG("\r\n [HNP API] native package install! userId=%s, hnpPath count=%d, install path=%s "
102 "install options=%d\r\n", userId, count, installPath, installOptions);
103
104 argv[index++] = "hnp";
105 argv[index++] = "install";
106 argv[index++] = "-u";
107 argv[index++] = (char*)userId;
108
109 for (int i = 0; i < count; i++) {
110 argv[index++] = "-p";
111 argv[index++] = (char*)packages[i];
112 }
113
114 if (installPath != NULL) {
115 argv[index++] = "-i";
116 argv[index++] = (char*)installPath;
117 }
118
119 if (IS_OPTION_SET(installOptions, OPTION_INDEX_FORCE)) {
120 argv[index++] = "-f";
121 }
122
123 return StartHnpProcess(argv, apcEnv);
124 }
125
NativeUnInstallHnpEx(const char * userId,const char * hnpName,const char * hnpVersion,const char * installPath)126 int NativeUnInstallHnpEx(const char *userId, const char *hnpName, const char *hnpVersion, const char *installPath)
127 {
128 char *argv[MAX_ARGV_NUM] = {0};
129 char *apcEnv[MAX_ENV_NUM] = {0};
130 int index = 0;
131
132 if (!IsDeveloperModeOpen()) {
133 HNPAPI_LOG("\r\n [HNP API] native package uninstall not in developer mode");
134 return HNP_API_NOT_IN_DEVELOPER_MODE;
135 }
136
137 if ((userId == NULL) || (hnpName == NULL) || (hnpVersion == NULL)) {
138 return HNP_API_ERRNO_PARAM_INVALID;
139 }
140
141 HNPAPI_LOG("\r\n [HNP API] native package uninstall! userId=%s, hnpName=%s, hnpVersion=%s\r\n",
142 userId, hnpName, hnpVersion);
143
144 argv[index++] = "hnp";
145 argv[index++] = "uninstall";
146 argv[index++] = "-u";
147 argv[index++] = (char*)userId;
148 argv[index++] = "-n";
149 argv[index++] = (char*)hnpName;
150 argv[index++] = "-v";
151 argv[index++] = (char*)hnpVersion;
152
153 if (installPath != NULL) {
154 argv[index++] = "-i";
155 argv[index++] = (char*)installPath;
156 }
157
158 return StartHnpProcess(argv, apcEnv);
159 }
160
NativeInstallHnp(const char * userId,const char * hnpRootPath,const char * packageName,int installOptions)161 int NativeInstallHnp(const char *userId, const char *hnpRootPath, const char *packageName, int installOptions)
162 {
163 (void)userId;
164 (void)hnpRootPath;
165 (void)packageName;
166 (void)installOptions;
167
168 return 0;
169 }
170
NativeUnInstallHnp(const char * userId,const char * packageName)171 int NativeUnInstallHnp(const char *userId, const char *packageName)
172 {
173 (void)userId;
174 (void)packageName;
175
176 return 0;
177 }
178
179 #ifdef __cplusplus
180 }
181 #endif