Skip to content

Commit

Permalink
Add acct(2) wrapper API
Browse files Browse the repository at this point in the history
This patch adds a wrapper for the acct(2) syscall, with two functions
for enabling and disabling process accounting.
  • Loading branch information
jabedude committed Oct 13, 2018
1 parent d5aec34 commit 2ef9333
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
26 changes: 26 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,32 @@ pub fn sleep(seconds: c_uint) -> c_uint {
unsafe { libc::sleep(seconds) }
}

#[cfg(target_family = "unix")]
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<P: ?Sized + NixPath>(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`
Expand Down
20 changes: 18 additions & 2 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -335,6 +336,21 @@ fn test_lseek64() {
close(tmpfd).unwrap();
}

#[cfg(target_family = "unix")]
#[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();
Expand Down

0 comments on commit 2ef9333

Please sign in to comment.