Skip to content

Commit 01e6a00

Browse files
committed
Add wrapper for acct(2)
This PR aims to add a nix wrapper for acct(2).
1 parent d5aec34 commit 01e6a00

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/unistd.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,18 @@ pub fn sleep(seconds: c_uint) -> c_uint {
14391439
unsafe { libc::sleep(seconds) }
14401440
}
14411441

1442+
/// Switch process accounting on or off
1443+
///
1444+
/// See also [acct(2)](https://linux.die.net/man/2/acct)
1445+
#[cfg(any(target_os = "linux", target_os = "android"))]
1446+
pub fn acct(filename: &CString) -> Result<Void> {
1447+
unsafe {
1448+
libc::acct(filename.as_ptr())
1449+
};
1450+
1451+
Err(Error::Sys(Errno::last()))
1452+
}
1453+
14421454
/// Creates a regular file which persists even after process termination
14431455
///
14441456
/// * `template`: a path whose 6 rightmost characters must be X, e.g. `/tmp/tmpfile_XXXXXX`

test/test_unistd.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use nix::sys::wait::*;
66
use nix::sys::stat::{self, Mode, SFlag};
77
use std::{env, iter};
88
use std::ffi::CString;
9-
use std::fs::File;
9+
use std::fs::{File, metadata};
1010
use std::io::Write;
1111
use std::os::unix::prelude::*;
12-
use tempfile::{self, tempfile};
12+
use std::process::Command;
13+
use tempfile::{self, tempfile, NamedTempFile};
1314
use libc::{self, _exit, off_t};
1415

1516
#[test]
@@ -335,6 +336,18 @@ fn test_lseek64() {
335336
close(tmpfd).unwrap();
336337
}
337338

339+
#[cfg(any(target_os = "linux", target_os = "android"))]
340+
#[test]
341+
fn test_acct() {
342+
skip_if_not_root!("test_acct");
343+
let file = NamedTempFile::new().unwrap();
344+
let path = file.path().to_str().unwrap();
345+
acct(&CString::new(path).unwrap()).unwrap();
346+
Command::new("echo").arg("Hello world");
347+
let md = metadata(path).unwrap();
348+
assert!(md.len() > 0);
349+
}
350+
338351
#[test]
339352
fn test_fpathconf_limited() {
340353
let f = tempfile().unwrap();

0 commit comments

Comments
 (0)