-
Notifications
You must be signed in to change notification settings - Fork 276
/
pid.cc
87 lines (69 loc) · 2.18 KB
/
pid.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
nsjail - CLONE_PID routines
-----------------------------------------
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "pid.h"
#include <linux/sched.h>
#include <sched.h>
#include <signal.h>
#include <stddef.h>
#include <sys/prctl.h>
#include <unistd.h>
#include "logs.h"
#include "subproc.h"
namespace pid {
bool initNs(nsjconf_t* nsjconf) {
if (nsjconf->mode != MODE_STANDALONE_EXECVE) {
return true;
}
if (!nsjconf->clone_newpid) {
return true;
}
LOG_D("Creating a dummy 'init' process");
/*
* If -Me is used then we need to create permanent init inside PID ns, otherwise only the
* first clone/fork will work, and the rest will fail with ENOMEM (see 'man pid_namespaces'
* for details on this behavior)
*/
pid_t pid = subproc::cloneProc(CLONE_FS, 0);
if (pid == -1) {
PLOG_E("Couldn't create a dummy init process");
return false;
}
if (pid > 0) {
LOG_D("Created a dummy 'init' process with PID=%d", pid);
return true;
}
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0UL, 0UL, 0UL) == -1) {
PLOG_W("(prctl(PR_SET_PDEATHSIG, SIGKILL) failed");
}
if (prctl(PR_SET_NAME, "ns-init", 0UL, 0UL, 0UL) == -1) {
PLOG_W("(prctl(PR_SET_NAME, 'init') failed");
}
if (prctl(PR_SET_DUMPABLE, 0UL, 0UL, 0UL, 0UL) == -1) {
PLOG_W("(prctl(PR_SET_DUMPABLE, 0) failed");
}
/* Act sort-a like a init by reaping zombie processes */
struct sigaction sa = {};
sa.sa_handler = SIG_DFL;
sa.sa_flags = SA_NOCLDWAIT | SA_NOCLDSTOP;
sa.sa_restorer = NULL;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
PLOG_W("Couldn't set sighandler for SIGCHLD");
}
for (;;) {
pause();
}
}
} // namespace pid