• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #ifdef ENABLE_PROCESS_PRIORITY
27 #include <sys/resource.h>
28 #define MIN_IMPORTANT_LEVEL (-20)
29 #define MAX_IMPORTANT_LEVEL (19)
30 #endif
31 
NotifyServiceChange(Service * service,int status)32 void NotifyServiceChange(Service *service, int status)
33 {
34     UNUSED(service);
35     UNUSED(status);
36 }
37 
IsForbidden(const char * fieldStr)38 int IsForbidden(const char *fieldStr)
39 {
40     size_t fieldLen = strlen(fieldStr);
41     size_t forbidStrLen = strlen(BIN_SH_NOT_ALLOWED);
42     if (fieldLen == forbidStrLen) {
43         if (strncmp(fieldStr, BIN_SH_NOT_ALLOWED, fieldLen) == 0) {
44             return 1;
45         }
46         return 0;
47     } else if (fieldLen > forbidStrLen) {
48         // "/bin/shxxxx" is valid but "/bin/sh xxxx" is invalid
49         if (strncmp(fieldStr, BIN_SH_NOT_ALLOWED, forbidStrLen) == 0) {
50             if (fieldStr[forbidStrLen] == ' ') {
51                 return 1;
52             }
53         }
54         return 0;
55     } else {
56         return 0;
57     }
58 }
59 
SetImportantValue(Service * service,const char * attrName,int value,int flag)60 int SetImportantValue(Service *service, const char *attrName, int value, int flag)
61 {
62     UNUSED(attrName);
63     UNUSED(flag);
64     INIT_ERROR_CHECK(service != NULL, return SERVICE_FAILURE, "Set service attr failed! null ptr.");
65 #ifdef ENABLE_PROCESS_PRIORITY
66     if (value >= MIN_IMPORTANT_LEVEL && value <= MAX_IMPORTANT_LEVEL) { // -20~19
67         service->attribute |= SERVICE_ATTR_IMPORTANT;
68         service->importance = value;
69     } else {
70         INIT_LOGE("Importance level = %d, is not between -20 and 19, error", value);
71         return SERVICE_FAILURE;
72     }
73 #else
74     if (value != 0) {
75         service->attribute |= SERVICE_ATTR_IMPORTANT;
76     }
77 #endif
78     return SERVICE_SUCCESS;
79 }
80 
ServiceExec(Service * service,const ServiceArgs * pathArgs)81 int ServiceExec(Service *service, const ServiceArgs *pathArgs)
82 {
83     INIT_ERROR_CHECK(service != NULL, return SERVICE_FAILURE, "Exec service failed! null ptr.");
84     INIT_LOGI("ServiceExec %s", service->name);
85     INIT_ERROR_CHECK(pathArgs != NULL && pathArgs->count > 0,
86         return SERVICE_FAILURE, "Exec service failed! null ptr.");
87 
88 #ifdef ENABLE_PROCESS_PRIORITY
89     if (service->importance != 0) {
90         INIT_ERROR_CHECK(setpriority(PRIO_PROCESS, 0, service->importance) == 0,
91             service->lastErrno = INIT_EPRIORITY;
92             return SERVICE_FAILURE,
93             "Service error %d %s, failed to set priority %d.", errno, service->name, service->importance);
94     }
95 #endif
96     int isCritical = (service->attribute & SERVICE_ATTR_CRITICAL);
97     INIT_ERROR_CHECK(execv(pathArgs->argv[0], pathArgs->argv) == 0,
98         service->lastErrno = INIT_EEXEC;
99         return errno, "[startup_failed]failed to execv %d %d %s", isCritical, errno, service->name);
100     return SERVICE_SUCCESS;
101 }
102 
SetAccessToken(const Service * service)103 int SetAccessToken(const Service *service)
104 {
105     return SERVICE_SUCCESS;
106 }
107 
GetAccessToken(void)108 void GetAccessToken(void)
109 {
110     return;
111 }
112 
IsEnableSandbox(void)113 void IsEnableSandbox(void)
114 {
115     return;
116 }
117 
SetServiceEnterSandbox(const Service * service,const char * path)118 int SetServiceEnterSandbox(const Service *service, const char *path)
119 {
120     UNUSED(path);
121     UNUSED(service);
122     return 0;
123 }
124