Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete the original FileDescriptor. #195

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/container/builder_impl.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use anyhow::{Context, Result};
use oci_spec::Spec;
use std::{fs, path::PathBuf};
use std::{fs, os::unix::prelude::RawFd, path::PathBuf};

use crate::{
cgroups,
namespaces::Namespaces,
process::{child, fork, init, parent},
rootless::Rootless,
stdio::FileDescriptor,
syscall::linux::LinuxSyscall,
utils,
};
Expand All @@ -31,7 +30,7 @@ pub(super) struct ContainerBuilderImpl<'a> {
/// container process to the higher level runtime
pub pid_file: Option<PathBuf>,
/// Socket to communicate the file descriptor of the ptty
pub console_socket: Option<FileDescriptor>,
pub console_socket: Option<RawFd>,
/// Options for rootless containers
pub rootless: Option<Rootless<'a>>,
/// Path to the Unix Domain Socket to communicate container start
Expand Down Expand Up @@ -69,7 +68,7 @@ impl<'a> ContainerBuilderImpl<'a> {
syscall: self.syscall.clone(),
spec: self.spec.clone(),
rootfs: self.rootfs.clone(),
console_socket: self.console_socket.clone(),
console_socket: self.console_socket,
is_rootless: self.rootless.is_some(),
notify_path: self.notify_path.clone(),
preserve_fds: self.preserve_fds,
Expand Down
8 changes: 3 additions & 5 deletions src/container/tenant_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ use std::{
convert::TryFrom,
ffi::{CString, OsString},
fs,
os::unix::prelude::OsStrExt,
os::unix::prelude::{OsStrExt, RawFd},
path::{Path, PathBuf},
str::FromStr,
};

use crate::{
notify_socket::NotifySocket, rootless::detect_rootless, stdio::FileDescriptor, tty, utils,
};
use crate::{notify_socket::NotifySocket, rootless::detect_rootless, tty, utils};

use super::{builder::ContainerBuilder, builder_impl::ContainerBuilderImpl, Container};

Expand Down Expand Up @@ -308,7 +306,7 @@ impl TenantContainerBuilder {
Ok(socket_path)
}

fn setup_tty_socket(&self, container_dir: &Path) -> Result<Option<FileDescriptor>> {
fn setup_tty_socket(&self, container_dir: &Path) -> Result<Option<RawFd>> {
let tty_name = Self::generate_name(container_dir, TENANT_TTY);
let csocketfd = if let Some(console_socket) = &self.base.console_socket {
Some(tty::setup_console_socket(
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub mod process;
pub mod rootfs;
pub mod rootless;
pub mod signal;
pub mod stdio;
pub mod syscall;
pub mod tty;
pub mod utils;
8 changes: 5 additions & 3 deletions src/process/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use nix::{
unistd::{Gid, Uid},
};
use oci_spec::Spec;
use std::{env, os::unix::io::AsRawFd};
use std::{
env,
os::unix::{io::AsRawFd, prelude::RawFd},
};
use std::{fs, io::Write, path::Path, path::PathBuf};

use crate::{
Expand All @@ -13,7 +16,6 @@ use crate::{
notify_socket::NotifyListener,
process::child,
rootfs,
stdio::FileDescriptor,
syscall::{linux::LinuxSyscall, Syscall},
tty, utils,
};
Expand Down Expand Up @@ -93,7 +95,7 @@ pub struct ContainerInitArgs {
/// Root filesystem of the container
pub rootfs: PathBuf,
/// Socket to communicate the file descriptor of the ptty
pub console_socket: Option<FileDescriptor>,
pub console_socket: Option<RawFd>,
/// Options for rootless containers
pub is_rootless: bool,
/// Path to the Unix Domain Socket to communicate container start
Expand Down
48 changes: 0 additions & 48 deletions src/stdio.rs

This file was deleted.

27 changes: 19 additions & 8 deletions src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@

use std::os::unix::fs::symlink;
use std::os::unix::io::AsRawFd;
use std::os::unix::prelude::RawFd;
use std::path::Path;

use anyhow::Context;
use anyhow::{bail, Result};
use nix::errno::Errno;
use nix::sys::socket;
use nix::sys::uio;
use nix::unistd::dup2;
use nix::unistd::{close, setsid};

use crate::stdio;
use crate::stdio::FileDescriptor;
const STDIN: i32 = 0;
const STDOUT: i32 = 1;
const STDERR: i32 = 2;

// TODO: Handling when there isn't console-socket.

pub fn setup_console_socket(
container_dir: &Path,
console_socket_path: &Path,
socket_name: &str,
) -> Result<FileDescriptor> {
) -> Result<RawFd> {
let linked = container_dir.join(socket_name);
symlink(console_socket_path, &linked)?;

Expand All @@ -42,10 +44,10 @@ pub fn setup_console_socket(
}
Ok(()) => csocketfd,
};
Ok(csocketfd.into())
Ok(csocketfd)
}

pub fn setup_console(console_fd: &FileDescriptor) -> Result<()> {
pub fn setup_console(console_fd: &RawFd) -> Result<()> {
// You can also access pty master, but it is better to use the API.
// ref. https://github.com/containerd/containerd/blob/261c107ffc4ff681bc73988f64e3f60c32233b37/vendor/github.com/containerd/go-runc/console.go#L139-L154
let openpty_result =
Expand All @@ -67,12 +69,21 @@ pub fn setup_console(console_fd: &FileDescriptor) -> Result<()> {
if unsafe { libc::ioctl(openpty_result.slave, libc::TIOCSCTTY) } < 0 {
log::warn!("could not TIOCSCTTY");
};
let slave = FileDescriptor::from(openpty_result.slave);
stdio::connect_stdio(&slave, &slave, &slave).expect("could not dup tty to stderr");
let slave = openpty_result.slave;
connect_stdio(&slave, &slave, &slave).context("could not dup tty to stderr")?;
close(console_fd.as_raw_fd()).context("could not close console socket")?;
Ok(())
}

fn connect_stdio(stdin: &RawFd, stdout: &RawFd, stderr: &RawFd) -> Result<()> {
dup2(stdin.as_raw_fd(), STDIN)?;
dup2(stdout.as_raw_fd(), STDOUT)?;
// FIXME: Rarely does it fail.
// error message: `Error: Resource temporarily unavailable (os error 11)`
dup2(stderr.as_raw_fd(), STDERR)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down