diff --git a/CHANGELOG.md b/CHANGELOG.md index 4de6ade247..55ea52833d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#930](https://github.com/nix-rust/nix/pull/930)) - Added `futimens` and `utimesat` wrappers ([#944](https://github.com/nix-rust/nix/pull/944)) and a `utimes` wrapper ([#946](https://github.com/nix-rust/nix/pull/946)). +- Added a `acct` wrapper module for enabling and disabling process accounting ([#952](https://github.com/nix-rust/nix/pull/952)) ### Changed - Increased required Rust version to 1.22.1/ diff --git a/src/unistd.rs b/src/unistd.rs index ad06a3c0eb..aa8fb69732 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -1439,6 +1439,31 @@ pub fn sleep(seconds: c_uint) -> c_uint { unsafe { libc::sleep(seconds) } } +pub mod acct { + use libc; + use {Result, NixPath}; + use errno::Errno; + use std::ptr; + + /// Enable process accounting + /// + /// See also [acct(2)](https://linux.die.net/man/2/acct) + pub fn enable(filename: &P) -> Result<()> { + let res = try!(filename.with_nix_path(|cstr| { + unsafe { libc::acct(cstr.as_ptr()) } + })); + + Errno::result(res).map(drop) + } + + /// Disable process accounting + pub fn disable() -> Result<()> { + let res = unsafe { libc::acct(ptr::null()) }; + + Errno::result(res).map(drop) + } +} + /// 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..3a64742821 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,21 @@ fn test_lseek64() { close(tmpfd).unwrap(); } +#[cfg(not(target_os = "freebsd"))] +#[test] +fn test_acct() { + skip_if_not_root!("test_acct"); + let file = NamedTempFile::new().unwrap(); + let path = file.path().to_str().unwrap(); + + acct::enable(path).unwrap(); + Command::new("echo").arg("Hello world"); + acct::disable().unwrap(); + + let md = metadata(path).unwrap(); + assert!(md.len() > 0); +} + #[test] fn test_fpathconf_limited() { let f = tempfile().unwrap();