Skip to content

Commit 19affae

Browse files
bors[bot]jabedude
andcommitted
Merge #952
952: Add wrapper for acct(2) r=asomers a=jabedude This PR aims to add a nix wrapper for acct(2). Co-authored-by: Josh Abraham <[email protected]>
2 parents 9ed9a1d + 0ce57d9 commit 19affae

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2828
- Added support for `ptrace` on BSD operating systems ([#949](https://github.com/nix-rust/nix/pull/949))
2929
- Added `ptrace` functions for reads and writes to tracee memory and ptrace kill
3030
([#949](https://github.com/nix-rust/nix/pull/949))
31+
- Added a `acct` wrapper module for enabling and disabling process accounting
32+
([#952](https://github.com/nix-rust/nix/pull/952))
3133

3234
### Changed
3335
- Increased required Rust version to 1.22.1/

src/unistd.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,31 @@ pub fn sleep(seconds: c_uint) -> c_uint {
15081508
unsafe { libc::sleep(seconds) }
15091509
}
15101510

1511+
pub mod acct {
1512+
use libc;
1513+
use {Result, NixPath};
1514+
use errno::Errno;
1515+
use std::ptr;
1516+
1517+
/// Enable process accounting
1518+
///
1519+
/// See also [acct(2)](https://linux.die.net/man/2/acct)
1520+
pub fn enable<P: ?Sized + NixPath>(filename: &P) -> Result<()> {
1521+
let res = try!(filename.with_nix_path(|cstr| {
1522+
unsafe { libc::acct(cstr.as_ptr()) }
1523+
}));
1524+
1525+
Errno::result(res).map(drop)
1526+
}
1527+
1528+
/// Disable process accounting
1529+
pub fn disable() -> Result<()> {
1530+
let res = unsafe { libc::acct(ptr::null()) };
1531+
1532+
Errno::result(res).map(drop)
1533+
}
1534+
}
1535+
15111536
/// Creates a regular file which persists even after process termination
15121537
///
15131538
/// * `template`: a path whose 6 rightmost characters must be X, e.g. `/tmp/tmpfile_XXXXXX`

test/test_unistd.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ use nix::unistd::ForkResult::*;
44
use nix::sys::signal::{SaFlags, SigAction, SigHandler, SigSet, Signal, sigaction};
55
use nix::sys::wait::*;
66
use nix::sys::stat::{self, Mode, SFlag};
7-
use std::{env, iter};
7+
use std::{env, iter, thread, time};
88
use std::ffi::CString;
9-
use std::fs::{self, File};
9+
use std::fs::{self, 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]
@@ -378,6 +379,26 @@ fn test_lseek64() {
378379
close(tmpfd).unwrap();
379380
}
380381

382+
// Skip on FreeBSD because FreeBSD's CI environment is jailed, and jails
383+
// aren't allowed to use acct(2)
384+
#[cfg(not(target_os = "freebsd"))]
385+
#[test]
386+
fn test_acct() {
387+
skip_if_not_root!("test_acct");
388+
let file = NamedTempFile::new().unwrap();
389+
let path = file.path().to_str().unwrap();
390+
391+
acct::enable(path).unwrap();
392+
Command::new("echo").arg("Hello world");
393+
acct::disable().unwrap();
394+
395+
loop {
396+
let len = metadata(path).unwrap().len();
397+
if len > 0 { break; }
398+
thread::sleep(time::Duration::from_millis(10));
399+
}
400+
}
401+
381402
#[test]
382403
fn test_fpathconf_limited() {
383404
let f = tempfile().unwrap();

0 commit comments

Comments
 (0)