]> git.proxmox.com Git - pve-lxc-syscalld.git/commitdiff
working on epoll
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 29 Oct 2019 13:49:59 +0000 (14:49 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 29 Oct 2019 13:49:59 +0000 (14:49 +0100)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/epoll.rs [new file with mode: 0644]
src/main.rs
src/rw_traits.rs [new file with mode: 0644]

diff --git a/src/epoll.rs b/src/epoll.rs
new file mode 100644 (file)
index 0000000..08c593b
--- /dev/null
@@ -0,0 +1,57 @@
+use std::io;
+use std::os::raw::c_int;
+use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
+
+use crate::tools::Fd;
+
+pub struct Epoll {
+    fd: Fd,
+}
+
+impl Epoll {
+    pub fn new() -> io::Result<Self> {
+        let fd = unsafe { Fd::from_raw_fd(c_try!(libc::epoll_create1(libc::EPOLL_CLOEXEC))) };
+        Ok(Self { fd })
+    }
+
+    pub fn add_file<T: AsRawFd>(&self, fd: &T, events: u32, data: u64) -> io::Result<()> {
+        self.add_fd(fd.as_raw_fd(), events, data)
+    }
+
+    pub fn modify_file<T: AsRawFd>(&self, fd: &T, events: u32, data: u64) -> io::Result<()> {
+        self.modify_fd(fd.as_raw_fd(), events, data)
+    }
+
+    pub fn remove_file<T: AsRawFd>(&self, fd: &T) -> io::Result<()> {
+        self.remove_fd(fd.as_raw_fd())
+    }
+
+    fn addmod_fd(&self, op: c_int, fd: RawFd, events: u32, data: u64) -> io::Result<()> {
+        let mut events = libc::epoll_event {
+            events,
+            r#u64: data,
+        };
+        c_try!(unsafe { libc::epoll_ctl(self.fd.as_raw_fd(), op, fd, &mut events) });
+        Ok(())
+    }
+
+    fn add_fd(&self, fd: RawFd, events: u32, data: u64) -> io::Result<()> {
+        self.addmod_fd(libc::EPOLL_CTL_ADD, fd, events, data)
+    }
+
+    fn modify_fd(&self, fd: RawFd, events: u32, data: u64) -> io::Result<()> {
+        self.addmod_fd(libc::EPOLL_CTL_MOD, fd, events, data)
+    }
+
+    fn remove_fd(&self, fd: RawFd) -> io::Result<()> {
+        c_try!(unsafe {
+            libc::epoll_ctl(
+                self.fd.as_raw_fd(),
+                libc::EPOLL_CTL_DEL,
+                fd,
+                std::ptr::null_mut(),
+            )
+        });
+        Ok(())
+    }
+}
index a49ef6ebf3699700e79b5cc04ea47442b74c3133..e87077199d897c3a4538c799a8865cd9ad146344 100644 (file)
@@ -10,11 +10,13 @@ mod macros;
 pub mod apparmor;
 pub mod capability;
 pub mod client;
+pub mod epoll;
 pub mod executor;
 pub mod fork;
 pub mod lxcseccomp;
 pub mod nsfd;
 pub mod process;
+pub mod rw_traits;
 pub mod seccomp;
 pub mod sys_mknod;
 pub mod sys_quotactl;
diff --git a/src/rw_traits.rs b/src/rw_traits.rs
new file mode 100644 (file)
index 0000000..96d39d1
--- /dev/null
@@ -0,0 +1,19 @@
+pub struct Write;
+pub struct Read;
+pub struct RW;
+
+mod private {
+    pub trait Sealed {}
+
+    impl Sealed for super::Read {}
+    impl Sealed for super::Write {}
+    impl Sealed for super::RW {}
+}
+
+pub trait HasRead: private::Sealed {}
+impl HasRead for Read {}
+impl HasRead for RW {}
+
+pub trait HasWrite: private::Sealed {}
+impl HasWrite for Write {}
+impl HasWrite for RW {}