From 01e6a00c3ddd419d7419a771d2a1edab838e850f Mon Sep 17 00:00:00 2001 From: Josh Abraham Date: Thu, 11 Oct 2018 21:37:35 -0400 Subject: [PATCH] Add wrapper for acct(2) This PR aims to add a nix wrapper for acct(2). --- src/unistd.rs | 12 ++++++++++++ test/test_unistd.rs | 17 +++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/unistd.rs b/src/unistd.rs index ad06a3c0eb..a22a160d12 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -1439,6 +1439,18 @@ pub fn sleep(seconds: c_uint) -> c_uint { unsafe { libc::sleep(seconds) } } +/// Switch process accounting on or off +/// +/// See also [acct(2)](https://linux.die.net/man/2/acct) +#[cfg(any(target_os = "linux", target_os = "android"))] +pub fn acct(filename: &CString) -> Result { + unsafe { + libc::acct(filename.as_ptr()) + }; + + Err(Error::Sys(Errno::last())) +} + /// Creates a regular file which persists even after process termination /// /// * `template`: a path whose 6 rightmost characters must be X, e.g. `/tmp/tmpfile_XXXXXX` diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 54cbff8dcf..934da77862 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -6,10 +6,11 @@ use nix::sys::wait::*; use nix::sys::stat::{self, Mode, SFlag}; use std::{env, iter}; use std::ffi::CString; -use std::fs::File; +use std::fs::{File, metadata}; use std::io::Write; use std::os::unix::prelude::*; -use tempfile::{self, tempfile}; +use std::process::Command; +use tempfile::{self, tempfile, NamedTempFile}; use libc::{self, _exit, off_t}; #[test] @@ -335,6 +336,18 @@ fn test_lseek64() { close(tmpfd).unwrap(); } +#[cfg(any(target_os = "linux", target_os = "android"))] +#[test] +fn test_acct() { + skip_if_not_root!("test_acct"); + let file = NamedTempFile::new().unwrap(); + let path = file.path().to_str().unwrap(); + acct(&CString::new(path).unwrap()).unwrap(); + Command::new("echo").arg("Hello world"); + let md = metadata(path).unwrap(); + assert!(md.len() > 0); +} + #[test] fn test_fpathconf_limited() { let f = tempfile().unwrap();