diff --git a/rules/fcntl/tgt_refcount.rs b/rules/fcntl/tgt_refcount.rs index a347dbb3..51ff7206 100644 --- a/rules/fcntl/tgt_refcount.rs +++ b/rules/fcntl/tgt_refcount.rs @@ -4,12 +4,34 @@ use libcc2rs::*; fn f1(a0: i32, a1: i32, va: &[VaArg]) -> i32 { - panic!( - "fcntl is not supported in the refcount model (fd={}, cmd={}, varargs={})", - a0, - a1, - va.len() - ) + let __res = match a1 { + ::libc::F_GETFL => FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&va[0])); + FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_GETFD => FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&va[0])); + FdRegistry::with_fd(a0, |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } } fn f2(a0: Ptr, a1: i32, va: &[VaArg]) -> i32 { diff --git a/tests/unit/fcntl.c b/tests/unit/fcntl.c new file mode 100644 index 00000000..a9869b49 --- /dev/null +++ b/tests/unit/fcntl.c @@ -0,0 +1,22 @@ +#include +#include +#include + +int main(void) { + int fds[2]; + assert(pipe(fds) == 0); + int flags = fcntl(fds[0], F_GETFL, 0); + assert(flags >= 0); + assert((flags & O_NONBLOCK) == 0); + assert(fcntl(fds[0], F_SETFL, flags | O_NONBLOCK) == 0); + flags = fcntl(fds[0], F_GETFL, 0); + assert((flags & O_NONBLOCK) != 0); + char b; + assert(read(fds[0], &b, 1) == -1); + assert((fcntl(fds[0], F_GETFD, 0) & FD_CLOEXEC) == 0); + assert(fcntl(fds[0], F_SETFD, FD_CLOEXEC) == 0); + assert((fcntl(fds[0], F_GETFD, 0) & FD_CLOEXEC) != 0); + assert(close(fds[0]) == 0); + assert(close(fds[1]) == 0); + return 0; +} diff --git a/tests/unit/out/refcount/fcntl.rs b/tests/unit/out/refcount/fcntl.rs new file mode 100644 index 00000000..7b8bf8ad --- /dev/null +++ b/tests/unit/out/refcount/fcntl.rs @@ -0,0 +1,251 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let fds: Value> = Rc::new(RefCell::new( + (0..2).map(|_| ::default()).collect::>(), + )); + assert!( + (((match nix::unistd::pipe() { + Ok((__r, __w)) => { + let __fds = (fds.as_pointer() as Ptr).clone(); + __fds.write(FdRegistry::register(__r)); + __fds.offset(1).write(FdRegistry::register(__w)); + 0 + } + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) as i32) + != 0) + ); + let flags: Value = Rc::new(RefCell::new({ + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + })); + assert!(((((*flags.borrow()) >= 0) as i32) != 0)); + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) == 0) as i32) != 0)); + assert!( + ((({ + let __res = match 4 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get( + &&[((*flags.borrow()) | ::libc::O_NONBLOCK).into()][0], + )); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + (*flags.borrow_mut()) = { + let __res = match 3 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + }; + assert!((((((*flags.borrow()) & ::libc::O_NONBLOCK) != 0) as i32) != 0)); + let b: Value = >::default(); + assert!( + (((match FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + ((b.as_pointer()) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(1_usize, |__buf| nix::unistd::read(__fd, __buf)) + }) { + Ok(__n) => __n as isize, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + (((({ + let __res = match 1 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } & 1) + == 0) as i32) + != 0) + ); + assert!( + ((({ + let __res = match 2 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(1).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } == 0) as i32) + != 0) + ); + assert!( + (((({ + let __res = match 1 { + ::libc::F_GETFL => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFL) + }), + ::libc::F_SETFL => { + let __flags = nix::fcntl::OFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFL(__flags)) + }) + } + ::libc::F_GETFD => FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_GETFD) + }), + ::libc::F_SETFD => { + let __flags = nix::fcntl::FdFlag::from_bits_retain(i32::get(&&[(0).into()][0])); + FdRegistry::with_fd((*fds.borrow())[(0) as usize], |__fd| { + nix::fcntl::fcntl(__fd, nix::fcntl::FcntlArg::F_SETFD(__flags)) + }) + } + __cmd => panic!("fcntl: unsupported cmd {}", __cmd), + }; + match __res { + Ok(__r) => __r, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } + } & 1) + != 0) as i32) + != 0) + ); + assert!((((FdRegistry::close((*fds.borrow())[(0) as usize]) == 0) as i32) != 0)); + assert!((((FdRegistry::close((*fds.borrow())[(1) as usize]) == 0) as i32) != 0)); + return 0; +} diff --git a/tests/unit/out/unsafe/fcntl.rs b/tests/unit/out/unsafe/fcntl.rs new file mode 100644 index 00000000..d40f4c94 --- /dev/null +++ b/tests/unit/out/unsafe/fcntl.rs @@ -0,0 +1,57 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut fds: [i32; 2] = [0_i32; 2]; + assert!(((((libc::pipe(fds.as_mut_ptr())) == (0)) as i32) != 0)); + let mut flags: i32 = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!(((((flags) >= (0)) as i32) != 0)); + assert!((((((flags) & (::libc::O_NONBLOCK)) == (0)) as i32) != 0)); + assert!( + ((((unsafe { + libc::fcntl( + fds[(0) as usize] as i32, + 4 as i32, + ((flags) | (::libc::O_NONBLOCK)), + ) + }) == (0)) as i32) + != 0) + ); + flags = (unsafe { libc::fcntl(fds[(0) as usize] as i32, 3 as i32, (0)) }); + assert!((((((flags) & (::libc::O_NONBLOCK)) != (0)) as i32) != 0)); + let mut b: libc::c_char = (0 as libc::c_char); + assert!( + ((((libc::read( + fds[(0) as usize], + ((&mut b as *mut libc::c_char) as *mut libc::c_char as *mut ::libc::c_void), + 1_usize + )) == (-1_i32 as isize)) as i32) + != 0) + ); + assert!( + (((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 1 as i32, (0),) }) & (1)) == (0)) + as i32) + != 0) + ); + assert!( + ((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 2 as i32, (1),) }) == (0)) as i32) != 0) + ); + assert!( + (((((unsafe { libc::fcntl(fds[(0) as usize] as i32, 1 as i32, (0),) }) & (1)) != (0)) + as i32) + != 0) + ); + assert!(((((libc::close(fds[(0) as usize])) == (0)) as i32) != 0)); + assert!(((((libc::close(fds[(1) as usize])) == (0)) as i32) != 0)); + return 0; +}