2016-05-13 23:07:44 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2016-07-21 21:48:47 +08:00
|
|
|
#include <linux/sched.h>
|
2016-05-13 23:07:44 +08:00
|
|
|
#include <sched.h>
|
2016-05-14 09:38:15 +08:00
|
|
|
#include <signal.h>
|
2017-10-18 20:46:17 +08:00
|
|
|
#include <stddef.h>
|
2016-05-13 23:07:44 +08:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2016-10-15 08:42:01 +08:00
|
|
|
#include "subproc.h"
|
2016-05-13 23:07:44 +08:00
|
|
|
|
2017-10-09 05:00:45 +08:00
|
|
|
bool pidInitNs(struct nsjconf_t* nsjconf)
|
2016-05-13 23:07:44 +08:00
|
|
|
{
|
|
|
|
if (nsjconf->mode != MODE_STANDALONE_EXECVE) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-10-25 07:34:10 +08:00
|
|
|
if (!nsjconf->clone_newpid) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-13 23:07:44 +08:00
|
|
|
|
2016-06-06 01:09:14 +08:00
|
|
|
LOG_D("Creating a dummy 'init' process");
|
|
|
|
|
2017-10-07 01:42:58 +08:00
|
|
|
/*
|
2017-10-17 21:22:23 +08:00
|
|
|
* 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)
|
2017-10-07 01:42:58 +08:00
|
|
|
*/
|
2016-10-15 08:42:01 +08:00
|
|
|
pid_t pid = subprocClone(CLONE_FS);
|
2016-05-13 23:07:44 +08:00
|
|
|
if (pid == -1) {
|
2016-05-15 08:32:50 +08:00
|
|
|
PLOG_E("Couldn't create a dummy init process");
|
2016-05-13 23:07:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pid > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-14 08:21:53 +08:00
|
|
|
|
2017-06-14 08:19:03 +08:00
|
|
|
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0UL, 0UL, 0UL) == -1) {
|
2016-06-06 01:09:14 +08:00
|
|
|
PLOG_W("(prctl(PR_SET_PDEATHSIG, SIGKILL) failed");
|
2016-05-13 23:07:44 +08:00
|
|
|
}
|
2017-10-07 01:42:58 +08:00
|
|
|
if (prctl(PR_SET_NAME, "ns-init", 0UL, 0UL, 0UL) == -1) {
|
2017-06-14 08:19:03 +08:00
|
|
|
PLOG_W("(prctl(PR_SET_NAME, 'init') failed");
|
|
|
|
}
|
2017-06-14 08:21:53 +08:00
|
|
|
if (prctl(PR_SET_DUMPABLE, 0UL, 0UL, 0UL, 0UL) == -1) {
|
|
|
|
PLOG_W("(prctl(PR_SET_DUMPABLE, 0) failed");
|
|
|
|
}
|
2017-06-14 08:15:11 +08:00
|
|
|
|
|
|
|
/* Act sort-a like a init by reaping zombie processes */
|
|
|
|
struct sigaction sa = {
|
|
|
|
.sa_handler = SIG_DFL,
|
|
|
|
.sa_flags = SA_NOCLDWAIT | SA_NOCLDSTOP,
|
|
|
|
.sa_restorer = NULL,
|
|
|
|
};
|
|
|
|
sigemptyset(&sa.sa_mask);
|
2017-06-14 08:19:03 +08:00
|
|
|
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
|
|
|
|
PLOG_W("Couldn't set sighandler for SIGCHLD");
|
2017-06-14 08:15:11 +08:00
|
|
|
}
|
|
|
|
|
2016-05-13 23:07:44 +08:00
|
|
|
for (;;) {
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
}
|