Hi,

The following diff adds pledge(2) on ripgrep:
- "stdio rpath" in common use-cases
- "stdio rpath proc exec" when -z is used in order to call external
  decompressors

The purpose was mostly educational, in order to see how pledge(2) fit
inside Rust code. From syscall point of vue it is good (no extra
syscalls...)

It was done using direct FFI.

Tested on amd64.
$ make test
...
test result: ok. 156 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Thanks.
-- 
Sebastien Marie

Index: Makefile
===================================================================
RCS file: /cvs/ports/textproc/ripgrep/Makefile,v
retrieving revision 1.8
diff -u -p -r1.8 Makefile
--- Makefile    20 Mar 2018 10:40:23 -0000      1.8
+++ Makefile    26 Apr 2018 12:42:54 -0000
@@ -5,12 +5,14 @@ COMMENT =             line oriented search tool usi
 GH_ACCOUNT =           BurntSushi
 GH_PROJECT =           ripgrep
 GH_TAGNAME =           0.8.1
+REVISION =             0
 
 CATEGORIES =           textproc sysutils
 
 # Unlicense/MIT
 PERMIT_PACKAGE_CDROM = Yes
 
+# uses pledge()
 WANTLIB += c pthread
 
 # as devel/cargo MODULES adds DISTFILES, GH_* didn't
@@ -68,6 +70,7 @@ MODCARGO_CRATES +=    winapi-x86_64-pc-wind
 MODCARGO_CRATES +=     wincolor-0.1.6  # Unlicense/MIT
 CONFIGURE_STYLE =      cargo
 
+PATCHORIG =            .openbsd.orig
 SEPARATE_BUILD =       Yes
 
 MODCARGO_RUSTFLAGS =   -C debuginfo=0
Index: patches/patch-src_args_rs
===================================================================
RCS file: patches/patch-src_args_rs
diff -N patches/patch-src_args_rs
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_args_rs   26 Apr 2018 12:42:54 -0000
@@ -0,0 +1,14 @@
+$OpenBSD$
+Declare search_zip_files to be public for pledge(2)
+Index: src/args.rs
+--- src/args.rs.orig
++++ src/args.rs
+@@ -77,7 +77,7 @@ pub struct Args {
+     type_list: bool,
+     types: Types,
+     with_filename: bool,
+-    search_zip_files: bool
++    pub search_zip_files: bool
+ }
+ 
+ impl Args {
Index: patches/patch-src_main_rs
===================================================================
RCS file: patches/patch-src_main_rs
diff -N patches/patch-src_main_rs
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-src_main_rs   26 Apr 2018 12:42:54 -0000
@@ -0,0 +1,38 @@
+$OpenBSD$
+Add pledge(2) call:
+- "stdio rpath" for common usage
+- "stdio rpath proc exec" for -z (--search-zip) as it is spawning external 
commands for decompress files.
+Index: src/main.rs
+--- src/main.rs.orig
++++ src/main.rs
+@@ -28,6 +28,13 @@ use std::sync::atomic::{AtomicUsize, Ordering};
+ use std::sync::mpsc;
+ use std::thread;
+ 
++use std::ffi::CString;
++use std::io;
++use std::ptr;
++extern "C" {
++    fn pledge(promises: *const libc::c_char, execpromises: *const 
libc::c_char) -> libc::c_int;
++}
++
+ use args::Args;
+ use worker::Work;
+ 
+@@ -65,6 +72,16 @@ fn main() {
+ }
+ 
+ fn run(args: Arc<Args>) -> Result<u64> {
++    let promises = if args.search_zip_files {
++        CString::new("stdio rpath proc exec").unwrap()
++    } else {
++        CString::new("stdio rpath").unwrap()
++    };
++    let execpromises = ptr::null() as *const libc::c_char;
++    if unsafe { pledge(promises.as_ptr(), execpromises) } == -1 {
++        eprintln!("error: pledge: {:?}", io::Error::last_os_error());
++        process::exit(1);
++    }
+     if args.never_match() {
+         return Ok(0);
+     }

Reply via email to