summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Henningsson <diwic@ubuntu.com>2015-08-23 07:41:33 +0200
committerCarl Lerche <me@carllerche.com>2015-08-25 15:56:04 -0700
commit4f69bc358d7ab644ae766fde8bb5bdfd631f789b (patch)
treebad01ac6570ac623577f6a940dfe298e7c27fb27 /src
parent1020c6417a47e5abcc2ddce8f0c0c014f52ad60d (diff)
downloadnix-4f69bc358d7ab644ae766fde8bb5bdfd631f789b.zip
Add memfd functionality
Diffstat (limited to 'src')
-rw-r--r--src/fcntl.rs28
-rw-r--r--src/sys/memfd.rs18
-rw-r--r--src/sys/mod.rs3
-rw-r--r--src/sys/syscall.rs3
4 files changed, 51 insertions, 1 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs
index 1bc11c74..6a0fdf96 100644
--- a/src/fcntl.rs
+++ b/src/fcntl.rs
@@ -40,6 +40,14 @@ mod ffi {
pub const F_SETLK: c_int = 6;
pub const F_SETLKW: c_int = 7;
pub const F_GETLK: c_int = 5;
+
+ pub const F_ADD_SEALS: c_int = 1033;
+ pub const F_GET_SEALS: c_int = 1034;
+
+ pub const F_SEAL_SEAL: c_int = 1;
+ pub const F_SEAL_SHRINK: c_int = 2;
+ pub const F_SEAL_GROW: c_int = 4;
+ pub const F_SEAL_WRITE: c_int = 8;
}
#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "ios", target_os = "openbsd"))]
@@ -98,7 +106,11 @@ pub enum FcntlArg<'a> {
#[cfg(any(target_os = "linux", target_os = "android"))]
F_OFD_SETLKW(&'a flock),
#[cfg(any(target_os = "linux", target_os = "android"))]
- F_OFD_GETLK(&'a mut flock)
+ F_OFD_GETLK(&'a mut flock),
+ #[cfg(target_os = "linux")]
+ F_ADD_SEALS(SealFlag),
+ #[cfg(target_os = "linux")]
+ F_GET_SEALS,
// TODO: Rest of flags
}
@@ -118,6 +130,10 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
F_SETLK(flock) => ffi::fcntl(fd, ffi::F_SETLK, flock),
F_SETLKW(flock) => ffi::fcntl(fd, ffi::F_SETLKW, flock),
F_GETLK(flock) => ffi::fcntl(fd, ffi::F_GETLK, flock),
+ #[cfg(target_os = "linux")]
+ F_ADD_SEALS(flag) => ffi::fcntl(fd, ffi::F_ADD_SEALS, flag.bits()),
+ #[cfg(target_os = "linux")]
+ F_GET_SEALS => ffi::fcntl(fd, ffi::F_GET_SEALS),
#[cfg(any(target_os = "linux", target_os = "android"))]
_ => unimplemented!()
}
@@ -195,6 +211,16 @@ mod consts {
const FD_CLOEXEC = 1
}
);
+
+ bitflags!(
+ flags SealFlag: c_int {
+ const F_SEAL_SEAL = 1,
+ const F_SEAL_SHRINK = 2,
+ const F_SEAL_GROW = 4,
+ const F_SEAL_WRITE = 8,
+ }
+ );
+
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
diff --git a/src/sys/memfd.rs b/src/sys/memfd.rs
new file mode 100644
index 00000000..e142218f
--- /dev/null
+++ b/src/sys/memfd.rs
@@ -0,0 +1,18 @@
+use libc;
+use std::os::unix::io::RawFd;
+use {Error, Result};
+use std::ffi::CStr;
+
+bitflags!(
+ flags MemFdCreateFlag: libc::c_uint {
+ const MFD_CLOEXEC = 0x0001,
+ const MFD_ALLOW_SEALING = 0x0002,
+ }
+);
+
+pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
+ use sys::syscall::{syscall, MEMFD_CREATE};
+ let res = unsafe { syscall(MEMFD_CREATE, name.as_ptr(), flags.bits()) };
+ if res == -1 { Err(Error::last()) }
+ else { Ok(res as RawFd) }
+}
diff --git a/src/sys/mod.rs b/src/sys/mod.rs
index c124359d..3df06c44 100644
--- a/src/sys/mod.rs
+++ b/src/sys/mod.rs
@@ -9,6 +9,9 @@ pub mod event;
#[cfg(feature = "eventfd")]
pub mod eventfd;
+#[cfg(target_os = "linux")]
+pub mod memfd;
+
#[cfg(not(any(target_os = "ios", target_os = "freebsd")))]
pub mod ioctl;
diff --git a/src/sys/syscall.rs b/src/sys/syscall.rs
index 0f6a5e0b..c80defa2 100644
--- a/src/sys/syscall.rs
+++ b/src/sys/syscall.rs
@@ -11,6 +11,7 @@ mod arch {
pub type Syscall = c_long;
pub static SYSPIVOTROOT: Syscall = 155;
+ pub static MEMFD_CREATE: Syscall = 319;
}
#[cfg(target_arch = "x86")]
@@ -20,6 +21,7 @@ mod arch {
pub type Syscall = c_long;
pub static SYSPIVOTROOT: Syscall = 217;
+ pub static MEMFD_CREATE: Syscall = 356;
}
#[cfg(target_arch = "arm")]
@@ -29,6 +31,7 @@ mod arch {
pub type Syscall = c_long;
pub static SYSPIVOTROOT: Syscall = 218;
+ pub static MEMFD_CREATE: Syscall = 385;
}