agent: fix agent panic running as init

We should mount procfs before trying to parse kernel command lines.

Fixes: #771
Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
Peng Tao 2020-09-21 20:53:30 +08:00 committed by Eric Ernst
parent 38eb1df4bd
commit 407cb9a3c3

View File

@ -126,15 +126,6 @@ fn main() -> Result<()> {
let writer = unsafe { File::from_raw_fd(wfd) }; let writer = unsafe { File::from_raw_fd(wfd) };
let agentConfig = AGENT_CONFIG.clone(); let agentConfig = AGENT_CONFIG.clone();
// once parsed cmdline and set the config, release the write lock
// as soon as possible in case other thread would get read lock on
// it.
{
let mut config = agentConfig.write().unwrap();
config.parse_cmdline(KERNEL_CMDLINE_FILE)?;
}
let config = agentConfig.read().unwrap();
let init_mode = unistd::getpid() == Pid::from_raw(1); let init_mode = unistd::getpid() == Pid::from_raw(1);
if init_mode { if init_mode {
@ -148,8 +139,25 @@ fn main() -> Result<()> {
// since before do the base mount, it wouldn't access "/proc/cmdline" // since before do the base mount, it wouldn't access "/proc/cmdline"
// to get the customzied debug level. // to get the customzied debug level.
let logger = logging::create_logger(NAME, "agent", slog::Level::Debug, writer); let logger = logging::create_logger(NAME, "agent", slog::Level::Debug, writer);
// Must mount proc fs before parsing kernel command line
general_mount(&logger).map_err(|e| {
error!(logger, "fail general mount: {}", e);
e
})?;
let mut config = agentConfig.write().unwrap();
config.parse_cmdline(KERNEL_CMDLINE_FILE)?;
init_agent_as_init(&logger, config.unified_cgroup_hierarchy)?; init_agent_as_init(&logger, config.unified_cgroup_hierarchy)?;
} else {
// once parsed cmdline and set the config, release the write lock
// as soon as possible in case other thread would get read lock on
// it.
let mut config = agentConfig.write().unwrap();
config.parse_cmdline(KERNEL_CMDLINE_FILE)?;
} }
let config = agentConfig.read().unwrap();
let log_vport = config.log_vport as u32; let log_vport = config.log_vport as u32;
let log_handle = thread::spawn(move || -> Result<()> { let log_handle = thread::spawn(move || -> Result<()> {
@ -335,8 +343,13 @@ fn setup_signal_handler(logger: &Logger, sandbox: Arc<Mutex<Sandbox>>) -> Result
// init_agent_as_init will do the initializations such as setting up the rootfs // init_agent_as_init will do the initializations such as setting up the rootfs
// when this agent has been run as the init process. // when this agent has been run as the init process.
fn init_agent_as_init(logger: &Logger, unified_cgroup_hierarchy: bool) -> Result<()> { fn init_agent_as_init(logger: &Logger, unified_cgroup_hierarchy: bool) -> Result<()> {
general_mount(logger)?; cgroups_mount(logger, unified_cgroup_hierarchy).map_err(|e| {
cgroups_mount(logger, unified_cgroup_hierarchy)?; error!(
logger,
"fail cgroups mount, unified_cgroup_hierarchy {}: {}", unified_cgroup_hierarchy, e
);
e
})?;
fs::remove_file(Path::new("/dev/ptmx"))?; fs::remove_file(Path::new("/dev/ptmx"))?;
unixfs::symlink(Path::new("/dev/pts/ptmx"), Path::new("/dev/ptmx"))?; unixfs::symlink(Path::new("/dev/pts/ptmx"), Path::new("/dev/ptmx"))?;