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_adapter.h"
16 
17 #include <errno.h>
18 #include <semaphore.h>
19 #include <sys/prctl.h>
20 #include <unistd.h>
21 #if defined OHOS_LITE && !defined __LINUX__
22 #include <sys/capability.h>
23 #else
24 #include <linux/capability.h>
25 #endif
26 
27 #if ((defined __LINUX__) || (!defined OHOS_LITE))
28 #include <linux/securebits.h>
29 #endif
30 #include "init_log.h"
31 
KeepCapability(const Service * service)32 int KeepCapability(const Service *service)
33 {
34 #ifdef __LINUX__
35     if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP | SECBIT_NO_SETUID_FIXUP_LOCKED)) {
36             INIT_LOGE("prctl PR_SET_SECUREBITS failed: %d", errno);
37             return -1;
38         }
39         return 0;
40 #else
41 #ifndef OHOS_LITE
42     if (service->attribute & SERVICE_ATTR_SETUID) {
43         if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP | SECBIT_NO_SETUID_FIXUP_LOCKED)) {
44             INIT_LOGE("prctl PR_SET_SECUREBITS failed: %d", errno);
45             return -1;
46         }
47         return 0;
48     }
49     if (prctl(PR_SET_SECUREBITS, SECBIT_KEEP_CAPS | SECBIT_KEEP_CAPS_LOCKED)) {
50         INIT_LOGE("prctl PR_SET_SECUREBITS failed: %d", errno);
51         return -1;
52     }
53 #endif
54 #endif
55     return 0;
56 }
57 
SetAmbientCapability(int cap)58 int SetAmbientCapability(int cap)
59 {
60 #if ((defined __LINUX__) || (!defined OHOS_LITE))
61     if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0)) {
62         INIT_LOGE("prctl PR_CAP_AMBIENT failed: %d", errno);
63         return -1;
64     }
65 #endif
66     return 0;
67 }
68 
69 #if (defined __LINUX__) && (defined NEED_EXEC_RCS_LINUX)
70 static pid_t g_waitPid = -1;
71 static sem_t *g_waitSem = NULL;
SignalRegWaitSem(pid_t waitPid,sem_t * waitSem)72 static void SignalRegWaitSem(pid_t waitPid, sem_t *waitSem)
73 {
74     g_waitPid = waitPid;
75     g_waitSem = waitSem;
76 }
77 
CheckWaitPid(pid_t sigPID)78 void CheckWaitPid(pid_t sigPID)
79 {
80     if (g_waitPid == sigPID && g_waitSem != NULL) {
81         if (sem_post(g_waitSem) != 0) {
82             INIT_LOGE("CheckWaitPid, sem_post failed, errno %d.", errno);
83         }
84         g_waitPid = -1;
85         g_waitSem = NULL;
86     }
87 }
88 #else
CheckWaitPid(pid_t sigPID)89 void CheckWaitPid(pid_t sigPID)
90 {
91     (void)(sigPID);
92 }
93 #endif
94 
SystemExecuteRcs(void)95 void SystemExecuteRcs(void)
96 {
97 #if (defined __LINUX__) && (defined NEED_EXEC_RCS_LINUX)
98     pid_t retPid = fork();
99     if (retPid < 0) {
100         INIT_LOGE("ExecuteRcs, fork failed! err %d.", errno);
101         return;
102     }
103 
104     // child process
105     if (retPid == 0) {
106         INIT_LOGI("ExecuteRcs, child process id %d.", getpid());
107         if (execle("/bin/sh", "sh", "/etc/init.d/rcS", NULL, NULL) != 0) {
108             INIT_LOGE("ExecuteRcs, execle failed! err %d.", errno);
109         }
110         _exit(0x7f); // 0x7f: user specified
111     }
112 
113     // init process
114     sem_t sem;
115     if (sem_init(&sem, 0, 0) != 0) {
116         INIT_LOGE("ExecuteRcs, sem_init failed, err %d.", errno);
117         return;
118     }
119     SignalRegWaitSem(retPid, &sem);
120 
121     // wait until rcs process exited
122     if (sem_wait(&sem) != 0) {
123         INIT_LOGE("ExecuteRcs, sem_wait failed, err %d.", errno);
124     }
125 #endif
126 }
127