1 /*
2 * Copyright (c) 2020-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 #include "init_service.h"
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/param.h>
20
21 #include "init.h"
22 #include "init_log.h"
23 #include "init_service_manager.h"
24 #include "securec.h"
25
NotifyServiceChange(Service * service,int status)26 void NotifyServiceChange(Service *service, int status)
27 {
28 UNUSED(service);
29 UNUSED(status);
30 }
31
IsForbidden(const char * fieldStr)32 int IsForbidden(const char *fieldStr)
33 {
34 size_t fieldLen = strlen(fieldStr);
35 size_t forbidStrLen = strlen(BIN_SH_NOT_ALLOWED);
36 if (fieldLen == forbidStrLen) {
37 if (strncmp(fieldStr, BIN_SH_NOT_ALLOWED, fieldLen) == 0) {
38 return 1;
39 }
40 return 0;
41 } else if (fieldLen > forbidStrLen) {
42 // "/bin/shxxxx" is valid but "/bin/sh xxxx" is invalid
43 if (strncmp(fieldStr, BIN_SH_NOT_ALLOWED, forbidStrLen) == 0) {
44 if (fieldStr[forbidStrLen] == ' ') {
45 return 1;
46 }
47 }
48 return 0;
49 } else {
50 return 0;
51 }
52 }
53
SetImportantValue(Service * service,const char * attrName,int value,int flag)54 int SetImportantValue(Service *service, const char *attrName, int value, int flag)
55 {
56 UNUSED(attrName);
57 UNUSED(flag);
58 INIT_ERROR_CHECK(service != NULL, return SERVICE_FAILURE, "Set service attr failed! null ptr.");
59 if (value != 0) {
60 service->attribute |= SERVICE_ATTR_IMPORTANT;
61 }
62 return SERVICE_SUCCESS;
63 }
64
ServiceExec(const Service * service)65 int ServiceExec(const Service *service)
66 {
67 INIT_ERROR_CHECK(service != NULL && service->pathArgs.count > 0,
68 return SERVICE_FAILURE, "Exec service failed! null ptr.");
69 INIT_LOGI("service->name is %s ", service->name);
70 char *env[] = { "LD_LIBRARY_PATH=/storage/app/libs", NULL, NULL };
71 char sockEnv[MAX_ENV_NAME] = {0};
72 if (service->socketCfg != NULL) {
73 INIT_ERROR_CHECK(snprintf_s(sockEnv, sizeof(sockEnv), sizeof(sockEnv) - 1, "OHOS_SOCKET_%s=%d",
74 service->socketCfg->name, service->socketCfg->sockFd) != -1,
75 return SERVICE_FAILURE, "format socket env failed!");
76 env[1] = sockEnv;
77 }
78 if (execve(service->pathArgs.argv[0], service->pathArgs.argv, env) != 0) {
79 INIT_LOGE("service %s execve failed! err %d.", service->name, errno);
80 return errno;
81 }
82 return SERVICE_SUCCESS;
83 }
84
SetAccessToken(const Service * service)85 int SetAccessToken(const Service *service)
86 {
87 return SERVICE_SUCCESS;
88 }
89
GetAccessToken(void)90 void GetAccessToken(void)
91 {
92 return;
93 }
94