Skip to content

Commit

Permalink
Add wrapper for acct(2)
Browse files Browse the repository at this point in the history
This PR aims to add a nix wrapper for acct(2).
  • Loading branch information
jabedude committed Oct 12, 2018
1 parent d5aec34 commit 01e6a00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> {
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`
Expand Down
17 changes: 15 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,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();
Expand Down

0 comments on commit 01e6a00

Please sign in to comment.