1
0
Fork 0
mirror of https://github.com/containers/youki synced 2024-06-10 00:36:16 +02:00
Signed-off-by: utam0k <k0ma@utam0k.jp>
This commit is contained in:
utam0k 2022-07-19 21:06:25 +09:00
parent 4bafaabb42
commit dacc73773f
No known key found for this signature in database
GPG Key ID: 14ACDDE088DF059E
4 changed files with 49 additions and 5 deletions

View File

@ -15,7 +15,10 @@ use nix::unistd::setsid;
use nix::unistd::{self, Gid, Uid};
use oci_spec::runtime::{LinuxNamespaceType, Spec, User};
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::os::unix::io::AsRawFd;
use std::os::unix::prelude::FromRawFd;
use std::{
env, fs,
path::{Path, PathBuf},
@ -159,6 +162,7 @@ pub fn container_init_process(
args: &ContainerArgs,
main_sender: &mut channel::MainSender,
init_receiver: &mut channel::InitReceiver,
fifo_fd: i32,
) -> Result<()> {
let syscall = args.syscall;
let spec = args.spec;
@ -412,7 +416,13 @@ pub fn container_init_process(
}
if proc.args().is_some() {
ExecutorManager::exec(spec)
ExecutorManager::exec(spec)?;
if fifo_fd != 0 {
let f = &mut unsafe { File::from_raw_fd(fifo_fd) };
// TODO: impl
write!(f, "1")?;
}
Ok(())
} else {
bail!("on non-Windows, at least one process arg entry is required")
}

View File

@ -14,6 +14,7 @@ pub fn container_intermediate_process(
intermediate_chan: &mut (channel::IntermediateSender, channel::IntermediateReceiver),
init_chan: &mut (channel::InitSender, channel::InitReceiver),
main_sender: &mut channel::MainSender,
fifo_fd: i32,
) -> Result<()> {
let (inter_sender, inter_receiver) = intermediate_chan;
let (init_sender, init_receiver) = init_chan;
@ -95,7 +96,7 @@ pub fn container_intermediate_process(
inter_sender
.close()
.context("failed to close sender in the intermediate process")?;
container_init_process(args, main_sender, init_receiver)
container_init_process(args, main_sender, init_receiver, fifo_fd)
})?;
// Once we fork the container init process, the job for intermediate process
// is done. We notify the container main process about the pid we just

View File

@ -6,8 +6,11 @@ use crate::{
};
use anyhow::{Context, Result};
use nix::{
sys::socket::{self, UnixAddr},
unistd::{self, Pid},
sys::{
socket::{self, UnixAddr},
stat,
},
unistd::{self, mkfifo, Pid},
};
use oci_spec::runtime;
use std::{io::IoSlice, path::Path};
@ -22,12 +25,32 @@ pub fn container_main_process(container_args: &ContainerArgs) -> Result<Pid> {
let inter_chan = &mut channel::intermediate_channel()?;
let init_chan = &mut channel::init_channel()?;
// TODO: implement Option version
let mut fifo_fd = 0;
// let container_root = &container_args
// .container
// .as_ref()
// .context("container state is required")?
// .root;
let container_root = &std::path::Path::new("/run/youki/tutorial_container/");
let fifo_path = container_root.join("state.fifo");
if container_args.init {
mkfifo(&fifo_path, stat::Mode::S_IRWXU).context("failed to create the fifo file.")?;
}
let mut open_flags = nix::fcntl::OFlag::empty();
open_flags.insert(nix::fcntl::OFlag::O_PATH);
open_flags.insert(nix::fcntl::OFlag::O_CLOEXEC);
fifo_fd = nix::fcntl::open(&fifo_path, open_flags, stat::Mode::S_IRWXU)?;
log::debug!("fifo_fd: {}", fifo_fd);
let intermediate_pid = fork::container_fork(|| {
container_intermediate_process::container_intermediate_process(
container_args,
inter_chan,
init_chan,
main_sender,
fifo_fd,
)
})?;
// Close down unused fds. The corresponding fds are duplicated to the

View File

@ -3,12 +3,15 @@ use nix::{
libc,
poll::{PollFd, PollFlags},
};
use std::{os::unix::prelude::RawFd, path::PathBuf};
use std::{fs::OpenOptions, io::Read, os::unix::prelude::RawFd, path::PathBuf};
use libcontainer::{container::builder::ContainerBuilder, syscall::syscall::create_syscall};
use liboci_cli::Exec;
use super::load_container;
pub fn exec(args: Exec, root_path: PathBuf) -> Result<i32> {
let container = load_container(&root_path, &args.container_id)?;
let syscall = create_syscall();
let pid = ContainerBuilder::new(args.container_id.clone(), syscall.as_ref())
.with_root_path(root_path)?
@ -26,6 +29,13 @@ pub fn exec(args: Exec, root_path: PathBuf) -> Result<i32> {
let poll_fd = PollFd::new(pidfd, PollFlags::POLLIN);
nix::poll::poll(&mut [poll_fd], -1).context("failed to wait for the container id")?;
let fifo_path = &container.root.join("state.fifo");
println!("fifo_path: {:?}", fifo_path);
let mut f = OpenOptions::new().read(true).open(fifo_path)?;
let mut contents = String::new();
f.read_to_string(&mut contents)?;
println!("get the value: {:?}", contents);
// TODO
Ok(0)
}