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 #include <string.h>
16 #include <sys/ioctl.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include "bootstage.h"
20 #include "init_log.h"
21 #include "init_module_engine.h"
22 
23 #define SA_MAIN_PATH ("/system/bin/sa_main")
24 
25 #define OH_ENCAPS_PROC_TYPE_BASE 0x18
26 #define OH_ENCAPS_MAGIC 'E'
27 #define OH_PROC_SYS 3
28 #define SET_PROC_TYPE_CMD _IOW(OH_ENCAPS_MAGIC, OH_ENCAPS_PROC_TYPE_BASE, uint32_t)
29 
SetEncapsFlag(uint32_t flag)30 static void SetEncapsFlag(uint32_t flag)
31 {
32     int fd = 0;
33     int ret = 0;
34 
35     fd = open("/dev/encaps", O_RDWR);
36     if (fd < 0) {
37         INIT_LOGI("SetEncapsFlag open failed, maybe this device is not supported");
38         return;
39     }
40 
41     ret = ioctl(fd, SET_PROC_TYPE_CMD, &flag);
42     if (ret != 0) {
43         close(fd);
44         INIT_LOGE("SetEncapsFlag ioctl failed");
45         return;
46     }
47 
48     close(fd);
49 }
50 
SetEncapsProcType(SERVICE_INFO_CTX * serviceCtx)51 static void SetEncapsProcType(SERVICE_INFO_CTX *serviceCtx)
52 {
53     if (serviceCtx->reserved == NULL) {
54         return;
55     }
56     if (strncmp(SA_MAIN_PATH, serviceCtx->reserved, strlen(SA_MAIN_PATH)) == 0) {
57         SetEncapsFlag(OH_PROC_SYS);
58     }
59 }
60 
MODULE_CONSTRUCTOR(void)61 MODULE_CONSTRUCTOR(void)
62 {
63     // Add hook to set encaps flag
64     InitAddServiceHook(SetEncapsProcType, INIT_SERVICE_SET_PERMS_BEFORE);
65 }
66