Skip to main content

libcontainer/syscall/
syscall.rs

1//! An interface trait so that rest of Youki can call
2//! necessary functions without having to worry about their
3//! implementation details
4use std::any::Any;
5use std::ffi::OsStr;
6use std::os::fd::{BorrowedFd, RawFd};
7use std::os::unix::io::OwnedFd;
8use std::path::Path;
9use std::sync::Arc;
10
11use caps::{CapSet, CapsHashSet};
12use libc;
13use nix::mount::{MntFlags, MsFlags};
14use nix::sched::CloneFlags;
15use nix::sys::stat::{Mode, SFlag};
16use nix::unistd::{Gid, Uid};
17use oci_spec::runtime::PosixRlimit;
18
19use crate::config::PersonalityDomain;
20use crate::syscall::Result;
21use crate::syscall::linux::{LinuxSyscall, MountAttr};
22use crate::syscall::test::TestHelperSyscall;
23
24/// This specifies various kernel/other functionalities required for
25/// container management
26pub trait Syscall {
27    fn as_any(&self) -> &dyn Any;
28    fn pivot_rootfs(&self, path: &Path) -> Result<()>;
29    fn chroot(&self, path: &Path) -> Result<()>;
30    fn set_ns(&self, rawfd: i32, nstype: CloneFlags) -> Result<()>;
31    fn set_id(&self, uid: Uid, gid: Gid) -> Result<()>;
32    fn unshare(&self, flags: CloneFlags) -> Result<()>;
33    fn set_capability(&self, cset: CapSet, value: &CapsHashSet) -> Result<()>;
34    fn set_hostname(&self, hostname: &str) -> Result<()>;
35    fn set_domainname(&self, domainname: &str) -> Result<()>;
36    fn set_rlimit(&self, rlimit: &PosixRlimit) -> Result<()>;
37    fn get_pwuid(&self, uid: u32) -> Option<Arc<OsStr>>;
38    fn mount(
39        &self,
40        source: Option<&Path>,
41        target: &Path,
42        fstype: Option<&str>,
43        flags: MsFlags,
44        data: Option<&str>,
45    ) -> Result<()>;
46    // mount_from_fd mounts a filesystem specified by source_fd to target path.
47    // NOTE: mount_from_fd only supports BIND_MOUNT.
48    fn mount_from_fd(&self, source_fd: &OwnedFd, target: &Path) -> Result<()>;
49    fn move_mount(
50        &self,
51        from_dirfd: BorrowedFd<'_>,
52        from_path: Option<&str>,
53        to_dirfd: BorrowedFd<'_>,
54        to_path: Option<&str>,
55        flags: u32,
56    ) -> Result<()>;
57    fn fsopen(&self, fstype: Option<&str>, flags: u32) -> Result<OwnedFd>;
58    fn fsconfig(
59        &self,
60        fsfd: BorrowedFd<'_>,
61        cmd: u32,
62        key: Option<&str>,
63        val: Option<&str>,
64        aux: libc::c_int,
65    ) -> Result<()>;
66    fn fsmount(&self, fsfd: BorrowedFd<'_>, flags: u32, attr_flags: Option<u64>)
67    -> Result<OwnedFd>;
68    fn open_tree(&self, dirfd: RawFd, path: Option<&str>, flags: u32) -> Result<OwnedFd>;
69    fn symlink(&self, original: &Path, link: &Path) -> Result<()>;
70    fn mknod(&self, path: &Path, kind: SFlag, perm: Mode, dev: u64) -> Result<()>;
71    fn chown(&self, path: &Path, owner: Option<Uid>, group: Option<Gid>) -> Result<()>;
72    fn set_groups(&self, groups: &[Gid]) -> Result<()>;
73    fn close_range(&self, preserve_fds: i32) -> Result<()>;
74    fn mount_setattr(
75        &self,
76        dirfd: BorrowedFd<'_>,
77        pathname: &Path,
78        flags: u32,
79        mount_attr: &MountAttr,
80        size: libc::size_t,
81    ) -> Result<()>;
82    fn set_io_priority(&self, class: i64, priority: i64) -> Result<()>;
83    fn set_mempolicy(&self, mode: i32, nodemask: &[libc::c_ulong], maxnode: u64) -> Result<()>;
84    fn umount2(&self, target: &Path, flags: MntFlags) -> Result<()>;
85    fn get_uid(&self) -> Uid;
86    fn get_gid(&self) -> Gid;
87    fn get_euid(&self) -> Uid;
88    fn get_egid(&self) -> Gid;
89    fn personality(&self, domain: PersonalityDomain) -> Result<()>;
90}
91
92#[derive(Clone, Copy)]
93pub enum SyscallType {
94    Linux,
95    Test,
96}
97
98impl Default for SyscallType {
99    fn default() -> Self {
100        if cfg!(test) {
101            SyscallType::Test
102        } else {
103            SyscallType::Linux
104        }
105    }
106}
107
108impl SyscallType {
109    pub fn create_syscall(&self) -> Box<dyn Syscall> {
110        match self {
111            SyscallType::Linux => Box::new(LinuxSyscall),
112            SyscallType::Test => Box::<TestHelperSyscall>::default(),
113        }
114    }
115}
116
117pub fn create_syscall() -> Box<dyn Syscall> {
118    SyscallType::default().create_syscall()
119}