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(void)32 int KeepCapability(void)
33 {
34 #if ((defined __LINUX__) || (!defined OHOS_LITE))
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 #endif
40 return 0;
41 }
42
SetAmbientCapability(int cap)43 int SetAmbientCapability(int cap)
44 {
45 #if ((defined __LINUX__) || (!defined OHOS_LITE))
46 if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0)) {
47 INIT_LOGE("prctl PR_CAP_AMBIENT failed: %d", errno);
48 return -1;
49 }
50 #endif
51 return 0;
52 }
53
54 #if (defined __LINUX__) && (defined NEED_EXEC_RCS_LINUX)
55 static pid_t g_waitPid = -1;
56 static sem_t *g_waitSem = NULL;
SignalRegWaitSem(pid_t waitPid,sem_t * waitSem)57 static void SignalRegWaitSem(pid_t waitPid, sem_t *waitSem)
58 {
59 g_waitPid = waitPid;
60 g_waitSem = waitSem;
61 }
62
CheckWaitPid(pid_t sigPID)63 void CheckWaitPid(pid_t sigPID)
64 {
65 if (g_waitPid == sigPID && g_waitSem != NULL) {
66 if (sem_post(g_waitSem) != 0) {
67 INIT_LOGE("CheckWaitPid, sem_post failed, errno %d.", errno);
68 }
69 g_waitPid = -1;
70 g_waitSem = NULL;
71 }
72 }
73 #else
CheckWaitPid(pid_t sigPID)74 void CheckWaitPid(pid_t sigPID)
75 {
76 (void)(sigPID);
77 }
78 #endif
79
SystemExecuteRcs(void)80 void SystemExecuteRcs(void)
81 {
82 #if (defined __LINUX__) && (defined NEED_EXEC_RCS_LINUX)
83 pid_t retPid = fork();
84 if (retPid < 0) {
85 INIT_LOGE("ExecuteRcs, fork failed! err %d.", errno);
86 return;
87 }
88
89 // child process
90 if (retPid == 0) {
91 INIT_LOGI("ExecuteRcs, child process id %d.", getpid());
92 if (execle("/bin/sh", "sh", "/etc/init.d/rcS", NULL, NULL) != 0) {
93 INIT_LOGE("ExecuteRcs, execle failed! err %d.", errno);
94 }
95 _exit(0x7f); // 0x7f: user specified
96 }
97
98 // init process
99 sem_t sem;
100 if (sem_init(&sem, 0, 0) != 0) {
101 INIT_LOGE("ExecuteRcs, sem_init failed, err %d.", errno);
102 return;
103 }
104 SignalRegWaitSem(retPid, &sem);
105
106 // wait until rcs process exited
107 if (sem_wait(&sem) != 0) {
108 INIT_LOGE("ExecuteRcs, sem_wait failed, err %d.", errno);
109 }
110 #endif
111 }
112