| 1 |
use libc::{_exit, mode_t, off_t}; |
| 2 |
use nix::errno::Errno; |
| 3 |
#[cfg(not(any(target_os = "redox", target_os = "haiku")))] |
| 4 |
use nix::fcntl::readlink; |
| 5 |
use nix::fcntl::OFlag; |
| 6 |
#[cfg(not(target_os = "redox"))] |
| 7 |
use nix::fcntl::{self, open}; |
| 8 |
#[cfg(not(any( |
| 9 |
target_os = "redox", |
| 10 |
target_os = "fuchsia", |
| 11 |
target_os = "haiku" |
| 12 |
)))] |
| 13 |
use nix::pty::{grantpt, posix_openpt, ptsname, unlockpt}; |
| 14 |
#[cfg(not(target_os = "redox"))] |
| 15 |
use nix::sys::signal::{ |
| 16 |
sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal, |
| 17 |
}; |
| 18 |
use nix::sys::stat::{self, Mode, SFlag}; |
| 19 |
use nix::sys::wait::*; |
| 20 |
use nix::unistd::ForkResult::*; |
| 21 |
use nix::unistd::*; |
| 22 |
use std::env; |
| 23 |
#[cfg(not(any(target_os = "fuchsia", target_os = "redox")))] |
| 24 |
use std::ffi::CString; |
| 25 |
#[cfg(not(target_os = "redox"))] |
| 26 |
use std::fs::DirBuilder; |
| 27 |
use std::fs::{self, File}; |
| 28 |
use std::io::Write; |
| 29 |
#[cfg(not(any( |
| 30 |
target_os = "fuchsia", |
| 31 |
target_os = "redox", |
| 32 |
target_os = "haiku" |
| 33 |
)))] |
| 34 |
use std::path::Path; |
| 35 |
use tempfile::{tempdir, tempfile}; |
| 36 |
|
| 37 |
use crate::*; |
| 38 |
|
| 39 |
#[test] |
| 40 |
#[cfg(not(any(target_os = "netbsd")))] |
| 41 |
fn test_fork_and_waitpid() { |
| 42 |
let _m = crate::FORK_MTX.lock(); |
| 43 |
|
| 44 |
// Safe: Child only calls `_exit`, which is signal-safe |
| 45 |
match unsafe { fork() }.expect("Error: Fork Failed") { |
| 46 |
Child => unsafe { _exit(0) }, |
| 47 |
Parent { child } => { |
| 48 |
// assert that child was created and pid > 0 |
| 49 |
let child_raw: ::libc::pid_t = child.into(); |
| 50 |
assert!(child_raw > 0); |
| 51 |
let wait_status = waitpid(child, None); |
| 52 |
match wait_status { |
| 53 |
// assert that waitpid returned correct status and the pid is the one of the child |
| 54 |
Ok(WaitStatus::Exited(pid_t, _)) => assert_eq!(pid_t, child), |
| 55 |
|
| 56 |
// panic, must never happen |
| 57 |
s @ Ok(_) => { |
| 58 |
panic!("Child exited {s:?}, should never happen") |
| 59 |
} |
| 60 |
|
| 61 |
// panic, waitpid should never fail |
| 62 |
Err(s) => panic!("Error: waitpid returned Err({s:?}"), |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
#[cfg(target_os = "freebsd")] |
| 70 |
fn test_rfork_and_waitpid() { |
| 71 |
let _m = crate::FORK_MTX.lock(); |
| 72 |
|
| 73 |
// Safe: Child only calls `_exit`, which is signal-safe |
| 74 |
match unsafe { rfork(RforkFlags::RFPROC | RforkFlags::RFTHREAD) } |
| 75 |
.expect("Error: Rfork Failed") |
| 76 |
{ |
| 77 |
Child => unsafe { _exit(0) }, |
| 78 |
Parent { child } => { |
| 79 |
// assert that child was created and pid > 0 |
| 80 |
let child_raw: ::libc::pid_t = child.into(); |
| 81 |
assert!(child_raw > 0); |
| 82 |
let wait_status = waitpid(child, None); |
| 83 |
match wait_status { |
| 84 |
// assert that waitpid returned correct status and the pid is the one of the child |
| 85 |
Ok(WaitStatus::Exited(pid_t, _)) => assert_eq!(pid_t, child), |
| 86 |
|
| 87 |
// panic, must never happen |
| 88 |
s @ Ok(_) => { |
| 89 |
panic!("Child exited {s:?}, should never happen") |
| 90 |
} |
| 91 |
|
| 92 |
// panic, waitpid should never fail |
| 93 |
Err(s) => panic!("Error: waitpid returned Err({s:?}"), |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn test_wait() { |
| 101 |
// Grab FORK_MTX so wait doesn't reap a different test's child process |
| 102 |
let _m = crate::FORK_MTX.lock(); |
| 103 |
|
| 104 |
// Safe: Child only calls `_exit`, which is signal-safe |
| 105 |
match unsafe { fork() }.expect("Error: Fork Failed") { |
| 106 |
Child => unsafe { _exit(0) }, |
| 107 |
Parent { child } => { |
| 108 |
let wait_status = wait(); |
| 109 |
|
| 110 |
// just assert that (any) one child returns with WaitStatus::Exited |
| 111 |
assert_eq!(wait_status, Ok(WaitStatus::Exited(child, 0))); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn test_mkstemp() { |
| 118 |
let mut path = env::temp_dir(); |
| 119 |
path.push("nix_tempfile.XXXXXX"); |
| 120 |
|
| 121 |
let result = mkstemp(&path); |
| 122 |
match result { |
| 123 |
Ok((_, path)) => { |
| 124 |
unlink(path.as_path()).unwrap(); |
| 125 |
} |
| 126 |
Err(e) => panic!("mkstemp failed: {e}"), |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn test_mkstemp_directory() { |
| 132 |
// mkstemp should fail if a directory is given |
| 133 |
mkstemp(&env::temp_dir()).expect_err("assertion failed"); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
#[cfg(not(target_os = "redox"))] |
| 138 |
fn test_mkfifo() { |
| 139 |
let tempdir = tempdir().unwrap(); |
| 140 |
let mkfifo_fifo = tempdir.path().join("mkfifo_fifo"); |
| 141 |
|
| 142 |
mkfifo(&mkfifo_fifo, Mode::S_IRUSR).unwrap(); |
| 143 |
|
| 144 |
let stats = stat::stat(&mkfifo_fifo).unwrap(); |
| 145 |
let typ = stat::SFlag::from_bits_truncate(stats.st_mode as mode_t); |
| 146 |
assert_eq!(typ, SFlag::S_IFIFO); |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
#[cfg(not(target_os = "redox"))] |
| 151 |
fn test_mkfifo_directory() { |
| 152 |
// mkfifo should fail if a directory is given |
| 153 |
mkfifo(&env::temp_dir(), Mode::S_IRUSR).expect_err("assertion failed"); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
#[cfg(not(any( |
| 158 |
apple_targets, |
| 159 |
target_os = "android", |
| 160 |
target_os = "redox", |
| 161 |
target_os = "haiku" |
| 162 |
)))] |
| 163 |
fn test_mkfifoat_none() { |
| 164 |
use nix::fcntl::AT_FDCWD; |
| 165 |
|
| 166 |
let _m = crate::CWD_LOCK.read(); |
| 167 |
|
| 168 |
let tempdir = tempdir().unwrap(); |
| 169 |
let mkfifoat_fifo = tempdir.path().join("mkfifoat_fifo"); |
| 170 |
|
| 171 |
mkfifoat(AT_FDCWD, &mkfifoat_fifo, Mode::S_IRUSR).unwrap(); |
| 172 |
|
| 173 |
let stats = stat::stat(&mkfifoat_fifo).unwrap(); |
| 174 |
let typ = stat::SFlag::from_bits_truncate(stats.st_mode); |
| 175 |
assert_eq!(typ, SFlag::S_IFIFO); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
#[cfg(not(any( |
| 180 |
apple_targets, |
| 181 |
target_os = "android", |
| 182 |
target_os = "redox", |
| 183 |
target_os = "haiku" |
| 184 |
)))] |
| 185 |
fn test_mkfifoat() { |
| 186 |
use nix::fcntl; |
| 187 |
|
| 188 |
let tempdir = tempdir().unwrap(); |
| 189 |
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); |
| 190 |
let mkfifoat_name = "mkfifoat_name"; |
| 191 |
|
| 192 |
mkfifoat(&dirfd, mkfifoat_name, Mode::S_IRUSR).unwrap(); |
| 193 |
|
| 194 |
let stats = |
| 195 |
stat::fstatat(&dirfd, mkfifoat_name, fcntl::AtFlags::empty()).unwrap(); |
| 196 |
let typ = stat::SFlag::from_bits_truncate(stats.st_mode); |
| 197 |
assert_eq!(typ, SFlag::S_IFIFO); |
| 198 |
} |
| 199 |
|
| 200 |
#[test] |
| 201 |
#[cfg(not(any( |
| 202 |
apple_targets, |
| 203 |
target_os = "android", |
| 204 |
target_os = "redox", |
| 205 |
target_os = "haiku" |
| 206 |
)))] |
| 207 |
fn test_mkfifoat_directory_none() { |
| 208 |
use nix::fcntl::AT_FDCWD; |
| 209 |
|
| 210 |
let _m = crate::CWD_LOCK.read(); |
| 211 |
|
| 212 |
// mkfifoat should fail if a directory is given |
| 213 |
mkfifoat(AT_FDCWD, &env::temp_dir(), Mode::S_IRUSR) |
| 214 |
.expect_err("assertion failed"); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
#[cfg(not(any( |
| 219 |
apple_targets, |
| 220 |
target_os = "android", |
| 221 |
target_os = "redox", |
| 222 |
target_os = "haiku" |
| 223 |
)))] |
| 224 |
fn test_mkfifoat_directory() { |
| 225 |
// mkfifoat should fail if a directory is given |
| 226 |
let tempdir = tempdir().unwrap(); |
| 227 |
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); |
| 228 |
let mkfifoat_dir = "mkfifoat_dir"; |
| 229 |
stat::mkdirat(&dirfd, mkfifoat_dir, Mode::S_IRUSR).unwrap(); |
| 230 |
|
| 231 |
mkfifoat(&dirfd, mkfifoat_dir, Mode::S_IRUSR) |
| 232 |
.expect_err("assertion failed"); |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn test_getpid() { |
| 237 |
let pid: ::libc::pid_t = getpid().into(); |
| 238 |
let ppid: ::libc::pid_t = getppid().into(); |
| 239 |
assert!(pid > 0); |
| 240 |
assert!(ppid > 0); |
| 241 |
} |
| 242 |
|
| 243 |
#[test] |
| 244 |
#[cfg(not(target_os = "redox"))] |
| 245 |
fn test_getsid() { |
| 246 |
let none_sid: ::libc::pid_t = getsid(None).unwrap().into(); |
| 247 |
let pid_sid: ::libc::pid_t = getsid(Some(getpid())).unwrap().into(); |
| 248 |
assert!(none_sid > 0); |
| 249 |
assert_eq!(none_sid, pid_sid); |
| 250 |
} |
| 251 |
|
| 252 |
#[cfg(linux_android)] |
| 253 |
mod linux_android { |
| 254 |
use nix::unistd::gettid; |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn test_gettid() { |
| 258 |
let tid: ::libc::pid_t = gettid().into(); |
| 259 |
assert!(tid > 0); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
#[cfg(target_os = "freebsd")] |
| 264 |
mod freebsd { |
| 265 |
use nix::unistd::pthread_getthreadid_np; |
| 266 |
|
| 267 |
#[test] |
| 268 |
fn test_pthread_getthreadid_np() { |
| 269 |
let tid: ::libc::pid_t = pthread_getthreadid_np().into(); |
| 270 |
// FreeBSD has thread id namespace shared with pids, the split |
| 271 |
// is at PID_MAX = 99999. |
| 272 |
assert!(tid >= 100000); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
#[test] |
| 277 |
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms |
| 278 |
#[cfg(not(any( |
| 279 |
apple_targets, |
| 280 |
target_os = "redox", |
| 281 |
target_os = "fuchsia", |
| 282 |
target_os = "haiku" |
| 283 |
)))] |
| 284 |
fn test_setgroups() { |
| 285 |
// Skip this test when not run as root as `setgroups()` requires root. |
| 286 |
skip_if_not_root!("test_setgroups"); |
| 287 |
|
| 288 |
let _m = crate::GROUPS_MTX.lock(); |
| 289 |
|
| 290 |
// Save the existing groups |
| 291 |
let old_groups = getgroups().unwrap(); |
| 292 |
|
| 293 |
// Set some new made up groups |
| 294 |
let groups = [Gid::from_raw(123), Gid::from_raw(456)]; |
| 295 |
setgroups(&groups).unwrap(); |
| 296 |
|
| 297 |
let new_groups = getgroups().unwrap(); |
| 298 |
assert_eq!(new_groups, groups); |
| 299 |
|
| 300 |
// Revert back to the old groups |
| 301 |
setgroups(&old_groups).unwrap(); |
| 302 |
} |
| 303 |
|
| 304 |
#[test] |
| 305 |
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms |
| 306 |
#[cfg(not(any( |
| 307 |
apple_targets, |
| 308 |
target_os = "redox", |
| 309 |
target_os = "fuchsia", |
| 310 |
target_os = "haiku", |
| 311 |
solarish |
| 312 |
)))] |
| 313 |
fn test_initgroups() { |
| 314 |
// Skip this test when not run as root as `initgroups()` and `setgroups()` |
| 315 |
// require root. |
| 316 |
skip_if_not_root!("test_initgroups"); |
| 317 |
|
| 318 |
let _m = crate::GROUPS_MTX.lock(); |
| 319 |
|
| 320 |
// Save the existing groups |
| 321 |
let old_groups = getgroups().unwrap(); |
| 322 |
|
| 323 |
// It doesn't matter if the root user is not called "root" or if a user |
| 324 |
// called "root" doesn't exist. We are just checking that the extra, |
| 325 |
// made-up group, `123`, is set. |
| 326 |
// FIXME: Test the other half of initgroups' functionality: whether the |
| 327 |
// groups that the user belongs to are also set. |
| 328 |
let user = CString::new("root").unwrap(); |
| 329 |
let group = Gid::from_raw(123); |
| 330 |
let mut group_list = getgrouplist(&user, group).unwrap(); |
| 331 |
assert!(group_list.contains(&group)); |
| 332 |
|
| 333 |
initgroups(&user, group).unwrap(); |
| 334 |
|
| 335 |
let mut new_groups = getgroups().unwrap(); |
| 336 |
|
| 337 |
new_groups.sort_by_key(|gid| gid.as_raw()); |
| 338 |
group_list.sort_by_key(|gid| gid.as_raw()); |
| 339 |
assert_eq!(new_groups, group_list); |
| 340 |
|
| 341 |
// Revert back to the old groups |
| 342 |
setgroups(&old_groups).unwrap(); |
| 343 |
} |
| 344 |
|
| 345 |
#[cfg(not(any(target_os = "fuchsia", target_os = "redox")))] |
| 346 |
macro_rules! execve_test_factory ( |
| 347 |
($test_name:ident, $syscall:ident, $exe: expr $(, $pathname:expr, $flags:expr)*) => ( |
| 348 |
|
| 349 |
#[cfg(test)] |
| 350 |
mod $test_name { |
| 351 |
use std::ffi::CStr; |
| 352 |
use super::*; |
| 353 |
|
| 354 |
const EMPTY: &'static [u8] = b"\0"; |
| 355 |
const DASH_C: &'static [u8] = b"-c\0"; |
| 356 |
const BIGARG: &'static [u8] = b"echo nix!!! && echo foo=$foo && echo baz=$baz\0"; |
| 357 |
const FOO: &'static [u8] = b"foo=bar\0"; |
| 358 |
const BAZ: &'static [u8] = b"baz=quux\0"; |
| 359 |
|
| 360 |
fn syscall_cstr_ref() -> Result<std::convert::Infallible, nix::Error> { |
| 361 |
$syscall( |
| 362 |
$exe, |
| 363 |
$(CString::new($pathname).unwrap().as_c_str(), )* |
| 364 |
&[CStr::from_bytes_with_nul(EMPTY).unwrap(), |
| 365 |
CStr::from_bytes_with_nul(DASH_C).unwrap(), |
| 366 |
CStr::from_bytes_with_nul(BIGARG).unwrap()], |
| 367 |
&[CStr::from_bytes_with_nul(FOO).unwrap(), |
| 368 |
CStr::from_bytes_with_nul(BAZ).unwrap()] |
| 369 |
$(, $flags)*) |
| 370 |
} |
| 371 |
|
| 372 |
fn syscall_cstring() -> Result<std::convert::Infallible, nix::Error> { |
| 373 |
$syscall( |
| 374 |
$exe, |
| 375 |
$(CString::new($pathname).unwrap().as_c_str(), )* |
| 376 |
&[CString::from(CStr::from_bytes_with_nul(EMPTY).unwrap()), |
| 377 |
CString::from(CStr::from_bytes_with_nul(DASH_C).unwrap()), |
| 378 |
CString::from(CStr::from_bytes_with_nul(BIGARG).unwrap())], |
| 379 |
&[CString::from(CStr::from_bytes_with_nul(FOO).unwrap()), |
| 380 |
CString::from(CStr::from_bytes_with_nul(BAZ).unwrap())] |
| 381 |
$(, $flags)*) |
| 382 |
} |
| 383 |
|
| 384 |
fn common_test(syscall: fn() -> Result<std::convert::Infallible, nix::Error>) { |
| 385 |
if "execveat" == stringify!($syscall) { |
| 386 |
// Though undocumented, Docker's default seccomp profile seems to |
| 387 |
// block this syscall. https://github.com/nix-rust/nix/issues/1122 |
| 388 |
skip_if_seccomp!($test_name); |
| 389 |
} |
| 390 |
|
| 391 |
let m = crate::FORK_MTX.lock(); |
| 392 |
// The `exec`d process will write to `writer`, and we'll read that |
| 393 |
// data from `reader`. |
| 394 |
let (reader, writer) = pipe().unwrap(); |
| 395 |
|
| 396 |
// Safe: Child calls `exit`, `dup`, `close` and the provided `exec*` family function. |
| 397 |
// NOTE: Technically, this makes the macro unsafe to use because you could pass anything. |
| 398 |
// The tests make sure not to do that, though. |
| 399 |
match unsafe{fork()}.unwrap() { |
| 400 |
Child => { |
| 401 |
// Make `writer` be the stdout of the new process. |
| 402 |
nix::unistd::dup2_stdout(&writer).unwrap(); |
| 403 |
let r = syscall(); |
| 404 |
let _ = std::io::stderr() |
| 405 |
.write_all(format!("{:?}", r).as_bytes()); |
| 406 |
// Should only get here in event of error |
| 407 |
unsafe{ _exit(1) }; |
| 408 |
}, |
| 409 |
Parent { child } => { |
| 410 |
// Wait for the child to exit. |
| 411 |
let ws = waitpid(child, None); |
| 412 |
drop(m); |
| 413 |
assert_eq!(ws, Ok(WaitStatus::Exited(child, 0))); |
| 414 |
// Read 1024 bytes. |
| 415 |
let mut buf = [0u8; 1024]; |
| 416 |
read(&reader, &mut buf).unwrap(); |
| 417 |
// It should contain the things we printed using `/bin/sh`. |
| 418 |
let string = String::from_utf8_lossy(&buf); |
| 419 |
assert!(string.contains("nix!!!")); |
| 420 |
assert!(string.contains("foo=bar")); |
| 421 |
assert!(string.contains("baz=quux")); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
// These tests frequently fail on musl, probably due to |
| 427 |
// https://github.com/nix-rust/nix/issues/555 |
| 428 |
#[cfg_attr(target_env = "musl", ignore)] |
| 429 |
#[test] |
| 430 |
fn test_cstr_ref() { |
| 431 |
common_test(syscall_cstr_ref); |
| 432 |
} |
| 433 |
|
| 434 |
// These tests frequently fail on musl, probably due to |
| 435 |
// https://github.com/nix-rust/nix/issues/555 |
| 436 |
#[cfg_attr(target_env = "musl", ignore)] |
| 437 |
#[test] |
| 438 |
fn test_cstring() { |
| 439 |
common_test(syscall_cstring); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
) |
| 444 |
); |
| 445 |
|
| 446 |
cfg_if! { |
| 447 |
if #[cfg(target_os = "android")] { |
| 448 |
execve_test_factory!(test_execve, execve, CString::new("/system/bin/sh").unwrap().as_c_str()); |
| 449 |
execve_test_factory!(test_fexecve, fexecve, &File::open("/system/bin/sh").unwrap()); |
| 450 |
} else if #[cfg(any(freebsdlike, target_os = "linux", target_os = "hurd"))] { |
| 451 |
// These tests frequently fail on musl, probably due to |
| 452 |
// https://github.com/nix-rust/nix/issues/555 |
| 453 |
execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str()); |
| 454 |
execve_test_factory!(test_fexecve, fexecve, &File::open("/bin/sh").unwrap()); |
| 455 |
} else if #[cfg(any(solarish, apple_targets, netbsdlike))] { |
| 456 |
execve_test_factory!(test_execve, execve, CString::new("/bin/sh").unwrap().as_c_str()); |
| 457 |
// No fexecve() on ios, macos, NetBSD, OpenBSD. |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
#[cfg(any( |
| 462 |
target_os = "haiku", |
| 463 |
target_os = "hurd", |
| 464 |
target_os = "linux", |
| 465 |
target_os = "openbsd" |
| 466 |
))] |
| 467 |
execve_test_factory!(test_execvpe, execvpe, &CString::new("sh").unwrap()); |
| 468 |
|
| 469 |
cfg_if! { |
| 470 |
if #[cfg(target_os = "android")] { |
| 471 |
use nix::fcntl::AtFlags; |
| 472 |
execve_test_factory!(test_execveat_empty, execveat, |
| 473 |
&File::open("/system/bin/sh").unwrap(), |
| 474 |
"", AtFlags::AT_EMPTY_PATH); |
| 475 |
execve_test_factory!(test_execveat_relative, execveat, |
| 476 |
&File::open("/system/bin/").unwrap(), |
| 477 |
"./sh", AtFlags::empty()); |
| 478 |
execve_test_factory!(test_execveat_absolute, execveat, |
| 479 |
&File::open("/").unwrap(), |
| 480 |
"/system/bin/sh", AtFlags::empty()); |
| 481 |
} else if #[cfg(all(target_os = "linux", any(target_arch ="x86_64", target_arch ="x86")))] { |
| 482 |
use nix::fcntl::AtFlags; |
| 483 |
execve_test_factory!(test_execveat_empty, execveat, &File::open("/bin/sh").unwrap(), |
| 484 |
"", AtFlags::AT_EMPTY_PATH); |
| 485 |
execve_test_factory!(test_execveat_relative, execveat, &File::open("/bin/").unwrap(), |
| 486 |
"./sh", AtFlags::empty()); |
| 487 |
execve_test_factory!(test_execveat_absolute, execveat, &File::open("/").unwrap(), |
| 488 |
"/bin/sh", AtFlags::empty()); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
#[test] |
| 493 |
#[cfg(not(target_os = "fuchsia"))] |
| 494 |
fn test_fchdir() { |
| 495 |
// fchdir changes the process's cwd |
| 496 |
let _dr = crate::DirRestore::new(); |
| 497 |
|
| 498 |
let tmpdir = tempdir().unwrap(); |
| 499 |
let tmpdir_path = tmpdir.path().canonicalize().unwrap(); |
| 500 |
let tmpdir_fd = File::open(&tmpdir_path).unwrap(); |
| 501 |
|
| 502 |
fchdir(&tmpdir_fd).expect("assertion failed"); |
| 503 |
assert_eq!(getcwd().unwrap(), tmpdir_path); |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn test_getcwd() { |
| 508 |
// chdir changes the process's cwd |
| 509 |
let _dr = crate::DirRestore::new(); |
| 510 |
|
| 511 |
let tmpdir = tempdir().unwrap(); |
| 512 |
let tmpdir_path = tmpdir.path().canonicalize().unwrap(); |
| 513 |
chdir(&tmpdir_path).expect("assertion failed"); |
| 514 |
assert_eq!(getcwd().unwrap(), tmpdir_path); |
| 515 |
|
| 516 |
// make path 500 chars longer so that buffer doubling in getcwd |
| 517 |
// kicks in. Note: One path cannot be longer than 255 bytes |
| 518 |
// (NAME_MAX) whole path cannot be longer than PATH_MAX (usually |
| 519 |
// 4096 on linux, 1024 on macos) |
| 520 |
let mut inner_tmp_dir = tmpdir_path; |
| 521 |
for _ in 0..5 { |
| 522 |
let newdir = "a".repeat(100); |
| 523 |
inner_tmp_dir.push(newdir); |
| 524 |
mkdir(inner_tmp_dir.as_path(), Mode::S_IRWXU) |
| 525 |
.expect("assertion failed"); |
| 526 |
} |
| 527 |
chdir(inner_tmp_dir.as_path()).expect("assertion failed"); |
| 528 |
assert_eq!(getcwd().unwrap(), inner_tmp_dir.as_path()); |
| 529 |
} |
| 530 |
|
| 531 |
#[test] |
| 532 |
fn test_chown() { |
| 533 |
// Testing for anything other than our own UID/GID is hard. |
| 534 |
let uid = Some(getuid()); |
| 535 |
let gid = Some(getgid()); |
| 536 |
|
| 537 |
let tempdir = tempdir().unwrap(); |
| 538 |
let path = tempdir.path().join("file"); |
| 539 |
{ |
| 540 |
File::create(&path).unwrap(); |
| 541 |
} |
| 542 |
|
| 543 |
chown(&path, uid, gid).unwrap(); |
| 544 |
chown(&path, uid, None).unwrap(); |
| 545 |
chown(&path, None, gid).unwrap(); |
| 546 |
|
| 547 |
fs::remove_file(&path).unwrap(); |
| 548 |
chown(&path, uid, gid).unwrap_err(); |
| 549 |
} |
| 550 |
|
| 551 |
#[test] |
| 552 |
fn test_fchown() { |
| 553 |
// Testing for anything other than our own UID/GID is hard. |
| 554 |
let uid = Some(getuid()); |
| 555 |
let gid = Some(getgid()); |
| 556 |
|
| 557 |
let file = tempfile().unwrap(); |
| 558 |
|
| 559 |
fchown(&file, uid, gid).unwrap(); |
| 560 |
fchown(&file, uid, None).unwrap(); |
| 561 |
fchown(&file, None, gid).unwrap(); |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
#[cfg(not(target_os = "redox"))] |
| 566 |
fn test_fchownat() { |
| 567 |
use nix::fcntl::AtFlags; |
| 568 |
use nix::fcntl::AT_FDCWD; |
| 569 |
|
| 570 |
let _dr = crate::DirRestore::new(); |
| 571 |
// Testing for anything other than our own UID/GID is hard. |
| 572 |
let uid = Some(getuid()); |
| 573 |
let gid = Some(getgid()); |
| 574 |
|
| 575 |
let tempdir = tempdir().unwrap(); |
| 576 |
let path = tempdir.path().join("file"); |
| 577 |
{ |
| 578 |
File::create(&path).unwrap(); |
| 579 |
} |
| 580 |
|
| 581 |
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); |
| 582 |
|
| 583 |
fchownat(&dirfd, "file", uid, gid, AtFlags::empty()).unwrap(); |
| 584 |
|
| 585 |
chdir(tempdir.path()).unwrap(); |
| 586 |
fchownat(AT_FDCWD, "file", uid, gid, AtFlags::empty()).unwrap(); |
| 587 |
|
| 588 |
fs::remove_file(&path).unwrap(); |
| 589 |
fchownat(AT_FDCWD, "file", uid, gid, AtFlags::empty()).unwrap_err(); |
| 590 |
} |
| 591 |
|
| 592 |
#[test] |
| 593 |
fn test_lseek() { |
| 594 |
const CONTENTS: &[u8] = b"abcdef123456"; |
| 595 |
let mut tmp = tempfile().unwrap(); |
| 596 |
tmp.write_all(CONTENTS).unwrap(); |
| 597 |
|
| 598 |
let offset: off_t = 5; |
| 599 |
lseek(&tmp, offset, Whence::SeekSet).unwrap(); |
| 600 |
|
| 601 |
let mut buf = [0u8; 7]; |
| 602 |
crate::read_exact(&tmp, &mut buf); |
| 603 |
assert_eq!(b"f123456", &buf); |
| 604 |
} |
| 605 |
|
| 606 |
#[cfg(linux_android)] |
| 607 |
#[test] |
| 608 |
fn test_lseek64() { |
| 609 |
const CONTENTS: &[u8] = b"abcdef123456"; |
| 610 |
let mut tmp = tempfile().unwrap(); |
| 611 |
tmp.write_all(CONTENTS).unwrap(); |
| 612 |
|
| 613 |
lseek64(&tmp, 5, Whence::SeekSet).unwrap(); |
| 614 |
|
| 615 |
let mut buf = [0u8; 7]; |
| 616 |
crate::read_exact(&tmp, &mut buf); |
| 617 |
assert_eq!(b"f123456", &buf); |
| 618 |
} |
| 619 |
|
| 620 |
cfg_if! { |
| 621 |
if #[cfg(linux_android)] { |
| 622 |
macro_rules! require_acct{ |
| 623 |
() => { |
| 624 |
require_capability!("test_acct", CAP_SYS_PACCT); |
| 625 |
} |
| 626 |
} |
| 627 |
} else if #[cfg(target_os = "freebsd")] { |
| 628 |
macro_rules! require_acct{ |
| 629 |
() => { |
| 630 |
skip_if_not_root!("test_acct"); |
| 631 |
skip_if_jailed!("test_acct"); |
| 632 |
} |
| 633 |
} |
| 634 |
} else if #[cfg(not(any(target_os = "redox", target_os = "fuchsia", target_os = "haiku")))] { |
| 635 |
macro_rules! require_acct{ |
| 636 |
() => { |
| 637 |
skip_if_not_root!("test_acct"); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
#[test] |
| 644 |
#[cfg(not(any( |
| 645 |
target_os = "redox", |
| 646 |
target_os = "fuchsia", |
| 647 |
target_os = "haiku", |
| 648 |
target_os = "cygwin" |
| 649 |
)))] |
| 650 |
fn test_acct() { |
| 651 |
use std::process::Command; |
| 652 |
use std::{thread, time}; |
| 653 |
use tempfile::NamedTempFile; |
| 654 |
|
| 655 |
let _m = crate::FORK_MTX.lock(); |
| 656 |
require_acct!(); |
| 657 |
|
| 658 |
let file = NamedTempFile::new().unwrap(); |
| 659 |
let path = file.path().to_str().unwrap(); |
| 660 |
|
| 661 |
acct::enable(path).unwrap(); |
| 662 |
|
| 663 |
loop { |
| 664 |
Command::new("echo").arg("Hello world").output().unwrap(); |
| 665 |
let len = fs::metadata(path).unwrap().len(); |
| 666 |
if len > 0 { |
| 667 |
break; |
| 668 |
} |
| 669 |
thread::sleep(time::Duration::from_millis(10)); |
| 670 |
} |
| 671 |
acct::disable().unwrap(); |
| 672 |
} |
| 673 |
|
| 674 |
#[cfg_attr(target_os = "hurd", ignore)] |
| 675 |
#[test] |
| 676 |
fn test_fpathconf_limited() { |
| 677 |
let f = tempfile().unwrap(); |
| 678 |
// PATH_MAX is limited on most platforms, so it makes a good test |
| 679 |
let path_max = fpathconf(f, PathconfVar::PATH_MAX); |
| 680 |
assert!( |
| 681 |
path_max |
| 682 |
.expect("fpathconf failed") |
| 683 |
.expect("PATH_MAX is unlimited") |
| 684 |
> 0 |
| 685 |
); |
| 686 |
} |
| 687 |
|
| 688 |
#[cfg_attr(target_os = "hurd", ignore)] |
| 689 |
#[test] |
| 690 |
fn test_pathconf_limited() { |
| 691 |
// PATH_MAX is limited on most platforms, so it makes a good test |
| 692 |
let path_max = pathconf("/", PathconfVar::PATH_MAX); |
| 693 |
assert!( |
| 694 |
path_max |
| 695 |
.expect("pathconf failed") |
| 696 |
.expect("PATH_MAX is unlimited") |
| 697 |
> 0 |
| 698 |
); |
| 699 |
} |
| 700 |
|
| 701 |
#[cfg_attr(target_os = "hurd", ignore)] |
| 702 |
#[test] |
| 703 |
fn test_sysconf_limited() { |
| 704 |
// OPEN_MAX is limited on most platforms, so it makes a good test |
| 705 |
let open_max = sysconf(SysconfVar::OPEN_MAX); |
| 706 |
assert!( |
| 707 |
open_max |
| 708 |
.expect("sysconf failed") |
| 709 |
.expect("OPEN_MAX is unlimited") |
| 710 |
> 0 |
| 711 |
); |
| 712 |
} |
| 713 |
|
| 714 |
#[cfg(target_os = "freebsd")] |
| 715 |
#[test] |
| 716 |
fn test_sysconf_unsupported() { |
| 717 |
// I know of no sysconf variables that are unsupported everywhere, but |
| 718 |
// _XOPEN_CRYPT is unsupported on FreeBSD 11.0, which is one of the platforms |
| 719 |
// we test. |
| 720 |
let open_max = sysconf(SysconfVar::_XOPEN_CRYPT); |
| 721 |
assert!(open_max.expect("sysconf failed").is_none()) |
| 722 |
} |
| 723 |
|
| 724 |
#[cfg(any(linux_android, freebsdlike, target_os = "openbsd"))] |
| 725 |
#[test] |
| 726 |
fn test_getresuid() { |
| 727 |
let resuids = getresuid().unwrap(); |
| 728 |
assert_ne!(resuids.real.as_raw(), libc::uid_t::MAX); |
| 729 |
assert_ne!(resuids.effective.as_raw(), libc::uid_t::MAX); |
| 730 |
assert_ne!(resuids.saved.as_raw(), libc::uid_t::MAX); |
| 731 |
} |
| 732 |
|
| 733 |
#[cfg(any(linux_android, freebsdlike, target_os = "openbsd"))] |
| 734 |
#[test] |
| 735 |
fn test_getresgid() { |
| 736 |
let resgids = getresgid().unwrap(); |
| 737 |
assert_ne!(resgids.real.as_raw(), libc::gid_t::MAX); |
| 738 |
assert_ne!(resgids.effective.as_raw(), libc::gid_t::MAX); |
| 739 |
assert_ne!(resgids.saved.as_raw(), libc::gid_t::MAX); |
| 740 |
} |
| 741 |
|
| 742 |
// Test that we can create a pair of pipes. No need to verify that they pass |
| 743 |
// data; that's the domain of the OS, not nix. |
| 744 |
#[test] |
| 745 |
fn test_pipe() { |
| 746 |
let (fd0, fd1) = pipe().unwrap(); |
| 747 |
let m0 = stat::SFlag::from_bits_truncate( |
| 748 |
stat::fstat(&fd0).unwrap().st_mode as mode_t, |
| 749 |
); |
| 750 |
// S_IFIFO means it's a pipe |
| 751 |
assert_eq!(m0, SFlag::S_IFIFO); |
| 752 |
let m1 = stat::SFlag::from_bits_truncate( |
| 753 |
stat::fstat(&fd1).unwrap().st_mode as mode_t, |
| 754 |
); |
| 755 |
assert_eq!(m1, SFlag::S_IFIFO); |
| 756 |
} |
| 757 |
|
| 758 |
// pipe2(2) is the same as pipe(2), except it allows setting some flags. Check |
| 759 |
// that we can set a flag. |
| 760 |
#[cfg(any( |
| 761 |
linux_android, |
| 762 |
freebsdlike, |
| 763 |
solarish, |
| 764 |
netbsdlike, |
| 765 |
target_os = "emscripten", |
| 766 |
target_os = "redox", |
| 767 |
))] |
| 768 |
#[test] |
| 769 |
fn test_pipe2() { |
| 770 |
use nix::fcntl::{fcntl, FcntlArg, FdFlag}; |
| 771 |
|
| 772 |
let (fd0, fd1) = pipe2(OFlag::O_CLOEXEC).unwrap(); |
| 773 |
let f0 = |
| 774 |
FdFlag::from_bits_truncate(fcntl(&fd0, FcntlArg::F_GETFD).unwrap()); |
| 775 |
assert!(f0.contains(FdFlag::FD_CLOEXEC)); |
| 776 |
let f1 = |
| 777 |
FdFlag::from_bits_truncate(fcntl(&fd1, FcntlArg::F_GETFD).unwrap()); |
| 778 |
assert!(f1.contains(FdFlag::FD_CLOEXEC)); |
| 779 |
} |
| 780 |
|
| 781 |
#[test] |
| 782 |
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))] |
| 783 |
fn test_truncate() { |
| 784 |
let tempdir = tempdir().unwrap(); |
| 785 |
let path = tempdir.path().join("file"); |
| 786 |
|
| 787 |
{ |
| 788 |
let mut tmp = File::create(&path).unwrap(); |
| 789 |
const CONTENTS: &[u8] = b"12345678"; |
| 790 |
tmp.write_all(CONTENTS).unwrap(); |
| 791 |
} |
| 792 |
|
| 793 |
truncate(&path, 4).unwrap(); |
| 794 |
|
| 795 |
let metadata = fs::metadata(&path).unwrap(); |
| 796 |
assert_eq!(4, metadata.len()); |
| 797 |
} |
| 798 |
|
| 799 |
#[test] |
| 800 |
fn test_ftruncate() { |
| 801 |
let tempdir = tempdir().unwrap(); |
| 802 |
let path = tempdir.path().join("file"); |
| 803 |
|
| 804 |
let mut file = File::create(&path).unwrap(); |
| 805 |
const CONTENTS: &[u8] = b"12345678"; |
| 806 |
file.write_all(CONTENTS).unwrap(); |
| 807 |
|
| 808 |
ftruncate(&file, 2).unwrap(); |
| 809 |
drop(file); |
| 810 |
|
| 811 |
let metadata = fs::metadata(&path).unwrap(); |
| 812 |
assert_eq!(2, metadata.len()); |
| 813 |
} |
| 814 |
|
| 815 |
// Used in `test_alarm`. |
| 816 |
#[cfg(not(target_os = "redox"))] |
| 817 |
static mut ALARM_CALLED: bool = false; |
| 818 |
|
| 819 |
// Used in `test_alarm`. |
| 820 |
#[cfg(not(target_os = "redox"))] |
| 821 |
pub extern "C" fn alarm_signal_handler(raw_signal: libc::c_int) { |
| 822 |
assert_eq!(raw_signal, libc::SIGALRM, "unexpected signal: {raw_signal}"); |
| 823 |
unsafe { ALARM_CALLED = true }; |
| 824 |
} |
| 825 |
|
| 826 |
#[test] |
| 827 |
#[cfg(not(target_os = "redox"))] |
| 828 |
fn test_alarm() { |
| 829 |
use std::{ |
| 830 |
thread, |
| 831 |
time::{Duration, Instant}, |
| 832 |
}; |
| 833 |
|
| 834 |
// Maybe other tests that fork interfere with this one? |
| 835 |
let _m = crate::SIGNAL_MTX.lock(); |
| 836 |
|
| 837 |
let handler = SigHandler::Handler(alarm_signal_handler); |
| 838 |
let signal_action = |
| 839 |
SigAction::new(handler, SaFlags::SA_RESTART, SigSet::empty()); |
| 840 |
let old_handler = unsafe { |
| 841 |
sigaction(Signal::SIGALRM, &signal_action) |
| 842 |
.expect("unable to set signal handler for alarm") |
| 843 |
}; |
| 844 |
|
| 845 |
// Set an alarm. |
| 846 |
assert_eq!(alarm::set(60), None); |
| 847 |
|
| 848 |
// Overwriting an alarm should return the old alarm. |
| 849 |
assert_eq!(alarm::set(1), Some(60)); |
| 850 |
|
| 851 |
// We should be woken up after 1 second by the alarm, so we'll sleep for 3 |
| 852 |
// seconds to be sure. |
| 853 |
let starttime = Instant::now(); |
| 854 |
loop { |
| 855 |
thread::sleep(Duration::from_millis(100)); |
| 856 |
if unsafe { ALARM_CALLED } { |
| 857 |
break; |
| 858 |
} |
| 859 |
if starttime.elapsed() > Duration::from_secs(3) { |
| 860 |
panic!("Timeout waiting for SIGALRM"); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
// Reset the signal. |
| 865 |
unsafe { |
| 866 |
sigaction(Signal::SIGALRM, &old_handler) |
| 867 |
.expect("unable to set signal handler for alarm"); |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
#[test] |
| 872 |
#[cfg(not(target_os = "redox"))] |
| 873 |
fn test_canceling_alarm() { |
| 874 |
let _m = crate::SIGNAL_MTX.lock(); |
| 875 |
|
| 876 |
assert_eq!(alarm::cancel(), None); |
| 877 |
|
| 878 |
assert_eq!(alarm::set(60), None); |
| 879 |
assert_eq!(alarm::cancel(), Some(60)); |
| 880 |
} |
| 881 |
|
| 882 |
#[test] |
| 883 |
#[cfg(not(any(target_os = "redox", target_os = "haiku")))] |
| 884 |
fn test_symlinkat() { |
| 885 |
use nix::fcntl::AT_FDCWD; |
| 886 |
|
| 887 |
let _m = crate::CWD_LOCK.read(); |
| 888 |
|
| 889 |
let tempdir = tempdir().unwrap(); |
| 890 |
|
| 891 |
let target = tempdir.path().join("a"); |
| 892 |
let linkpath = tempdir.path().join("b"); |
| 893 |
symlinkat(&target, AT_FDCWD, &linkpath).unwrap(); |
| 894 |
assert_eq!( |
| 895 |
readlink(&linkpath).unwrap().to_str().unwrap(), |
| 896 |
target.to_str().unwrap() |
| 897 |
); |
| 898 |
|
| 899 |
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); |
| 900 |
let target = "c"; |
| 901 |
let linkpath = "d"; |
| 902 |
symlinkat(target, &dirfd, linkpath).unwrap(); |
| 903 |
assert_eq!( |
| 904 |
readlink(&tempdir.path().join(linkpath)) |
| 905 |
.unwrap() |
| 906 |
.to_str() |
| 907 |
.unwrap(), |
| 908 |
target |
| 909 |
); |
| 910 |
} |
| 911 |
|
| 912 |
#[test] |
| 913 |
#[cfg(not(any(target_os = "redox", target_os = "haiku")))] |
| 914 |
fn test_linkat_file() { |
| 915 |
use nix::fcntl::AtFlags; |
| 916 |
|
| 917 |
let tempdir = tempdir().unwrap(); |
| 918 |
let oldfilename = "foo.txt"; |
| 919 |
let oldfilepath = tempdir.path().join(oldfilename); |
| 920 |
|
| 921 |
let newfilename = "bar.txt"; |
| 922 |
let newfilepath = tempdir.path().join(newfilename); |
| 923 |
|
| 924 |
// Create file |
| 925 |
File::create(oldfilepath).unwrap(); |
| 926 |
|
| 927 |
// Get file descriptor for base directory |
| 928 |
let dirfd = |
| 929 |
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) |
| 930 |
.unwrap(); |
| 931 |
|
| 932 |
// Attempt hard link file at relative path |
| 933 |
linkat( |
| 934 |
&dirfd, |
| 935 |
oldfilename, |
| 936 |
&dirfd, |
| 937 |
newfilename, |
| 938 |
AtFlags::AT_SYMLINK_FOLLOW, |
| 939 |
) |
| 940 |
.unwrap(); |
| 941 |
assert!(newfilepath.exists()); |
| 942 |
} |
| 943 |
|
| 944 |
#[test] |
| 945 |
#[cfg(not(any(target_os = "redox", target_os = "haiku")))] |
| 946 |
/// This test is the same as [test_linkat_file], but ensures that two different types can be used |
| 947 |
/// as the path arguments. |
| 948 |
fn test_linkat_pathtypes() { |
| 949 |
use nix::fcntl::AtFlags; |
| 950 |
|
| 951 |
let tempdir = tempdir().unwrap(); |
| 952 |
let oldfilename = "foo.txt"; |
| 953 |
let oldfilepath = tempdir.path().join(oldfilename); |
| 954 |
|
| 955 |
let newfilename = "bar.txt"; |
| 956 |
let newfilepath = tempdir.path().join(newfilename); |
| 957 |
|
| 958 |
// Create file |
| 959 |
File::create(oldfilepath).unwrap(); |
| 960 |
|
| 961 |
// Get file descriptor for base directory |
| 962 |
let dirfd = |
| 963 |
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) |
| 964 |
.unwrap(); |
| 965 |
|
| 966 |
// Attempt hard link file at relative path |
| 967 |
linkat( |
| 968 |
&dirfd, |
| 969 |
PathBuf::from(oldfilename).as_path(), |
| 970 |
&dirfd, |
| 971 |
newfilename, |
| 972 |
AtFlags::AT_SYMLINK_FOLLOW, |
| 973 |
) |
| 974 |
.unwrap(); |
| 975 |
assert!(newfilepath.exists()); |
| 976 |
} |
| 977 |
|
| 978 |
#[test] |
| 979 |
#[cfg(not(any(target_os = "redox", target_os = "haiku")))] |
| 980 |
fn test_linkat_olddirfd_none() { |
| 981 |
use nix::fcntl::AtFlags; |
| 982 |
use nix::fcntl::AT_FDCWD; |
| 983 |
|
| 984 |
let _dr = crate::DirRestore::new(); |
| 985 |
|
| 986 |
let tempdir_oldfile = tempdir().unwrap(); |
| 987 |
let oldfilename = "foo.txt"; |
| 988 |
let oldfilepath = tempdir_oldfile.path().join(oldfilename); |
| 989 |
|
| 990 |
let tempdir_newfile = tempdir().unwrap(); |
| 991 |
let newfilename = "bar.txt"; |
| 992 |
let newfilepath = tempdir_newfile.path().join(newfilename); |
| 993 |
|
| 994 |
// Create file |
| 995 |
File::create(oldfilepath).unwrap(); |
| 996 |
|
| 997 |
// Get file descriptor for base directory of new file |
| 998 |
let dirfd = fcntl::open( |
| 999 |
tempdir_newfile.path(), |
| 1000 |
fcntl::OFlag::empty(), |
| 1001 |
stat::Mode::empty(), |
| 1002 |
) |
| 1003 |
.unwrap(); |
| 1004 |
|
| 1005 |
// Attempt hard link file using curent working directory as relative path for old file path |
| 1006 |
chdir(tempdir_oldfile.path()).unwrap(); |
| 1007 |
linkat( |
| 1008 |
AT_FDCWD, |
| 1009 |
oldfilename, |
| 1010 |
&dirfd, |
| 1011 |
newfilename, |
| 1012 |
AtFlags::AT_SYMLINK_FOLLOW, |
| 1013 |
) |
| 1014 |
.unwrap(); |
| 1015 |
assert!(newfilepath.exists()); |
| 1016 |
} |
| 1017 |
|
| 1018 |
#[test] |
| 1019 |
#[cfg(not(any(target_os = "redox", target_os = "haiku")))] |
| 1020 |
fn test_linkat_newdirfd_none() { |
| 1021 |
use nix::fcntl::AtFlags; |
| 1022 |
use nix::fcntl::AT_FDCWD; |
| 1023 |
|
| 1024 |
let _dr = crate::DirRestore::new(); |
| 1025 |
|
| 1026 |
let tempdir_oldfile = tempdir().unwrap(); |
| 1027 |
let oldfilename = "foo.txt"; |
| 1028 |
let oldfilepath = tempdir_oldfile.path().join(oldfilename); |
| 1029 |
|
| 1030 |
let tempdir_newfile = tempdir().unwrap(); |
| 1031 |
let newfilename = "bar.txt"; |
| 1032 |
let newfilepath = tempdir_newfile.path().join(newfilename); |
| 1033 |
|
| 1034 |
// Create file |
| 1035 |
File::create(oldfilepath).unwrap(); |
| 1036 |
|
| 1037 |
// Get file descriptor for base directory of old file |
| 1038 |
let dirfd = fcntl::open( |
| 1039 |
tempdir_oldfile.path(), |
| 1040 |
fcntl::OFlag::empty(), |
| 1041 |
stat::Mode::empty(), |
| 1042 |
) |
| 1043 |
.unwrap(); |
| 1044 |
|
| 1045 |
// Attempt hard link file using current working directory as relative path for new file path |
| 1046 |
chdir(tempdir_newfile.path()).unwrap(); |
| 1047 |
linkat( |
| 1048 |
&dirfd, |
| 1049 |
oldfilename, |
| 1050 |
AT_FDCWD, |
| 1051 |
newfilename, |
| 1052 |
AtFlags::AT_SYMLINK_FOLLOW, |
| 1053 |
) |
| 1054 |
.unwrap(); |
| 1055 |
assert!(newfilepath.exists()); |
| 1056 |
} |
| 1057 |
|
| 1058 |
#[test] |
| 1059 |
#[cfg(not(any(apple_targets, target_os = "redox", target_os = "haiku")))] |
| 1060 |
fn test_linkat_no_follow_symlink() { |
| 1061 |
use nix::fcntl::AtFlags; |
| 1062 |
use nix::fcntl::AT_FDCWD; |
| 1063 |
|
| 1064 |
let _m = crate::CWD_LOCK.read(); |
| 1065 |
|
| 1066 |
let tempdir = tempdir().unwrap(); |
| 1067 |
let oldfilename = "foo.txt"; |
| 1068 |
let oldfilepath = tempdir.path().join(oldfilename); |
| 1069 |
|
| 1070 |
let symoldfilename = "symfoo.txt"; |
| 1071 |
let symoldfilepath = tempdir.path().join(symoldfilename); |
| 1072 |
|
| 1073 |
let newfilename = "nofollowsymbar.txt"; |
| 1074 |
let newfilepath = tempdir.path().join(newfilename); |
| 1075 |
|
| 1076 |
// Create file |
| 1077 |
File::create(&oldfilepath).unwrap(); |
| 1078 |
|
| 1079 |
// Create symlink to file |
| 1080 |
symlinkat(&oldfilepath, AT_FDCWD, &symoldfilepath).unwrap(); |
| 1081 |
|
| 1082 |
// Get file descriptor for base directory |
| 1083 |
let dirfd = |
| 1084 |
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) |
| 1085 |
.unwrap(); |
| 1086 |
|
| 1087 |
// Attempt link symlink of file at relative path |
| 1088 |
linkat( |
| 1089 |
&dirfd, |
| 1090 |
symoldfilename, |
| 1091 |
&dirfd, |
| 1092 |
newfilename, |
| 1093 |
AtFlags::empty(), |
| 1094 |
) |
| 1095 |
.unwrap(); |
| 1096 |
|
| 1097 |
// Assert newfile is actually a symlink to oldfile. |
| 1098 |
assert_eq!( |
| 1099 |
readlink(&newfilepath).unwrap().to_str().unwrap(), |
| 1100 |
oldfilepath.to_str().unwrap() |
| 1101 |
); |
| 1102 |
} |
| 1103 |
|
| 1104 |
#[test] |
| 1105 |
#[cfg(not(any(target_os = "redox", target_os = "haiku")))] |
| 1106 |
fn test_linkat_follow_symlink() { |
| 1107 |
use nix::fcntl::AtFlags; |
| 1108 |
use nix::fcntl::AT_FDCWD; |
| 1109 |
|
| 1110 |
let _m = crate::CWD_LOCK.read(); |
| 1111 |
|
| 1112 |
let tempdir = tempdir().unwrap(); |
| 1113 |
let oldfilename = "foo.txt"; |
| 1114 |
let oldfilepath = tempdir.path().join(oldfilename); |
| 1115 |
|
| 1116 |
let symoldfilename = "symfoo.txt"; |
| 1117 |
let symoldfilepath = tempdir.path().join(symoldfilename); |
| 1118 |
|
| 1119 |
let newfilename = "nofollowsymbar.txt"; |
| 1120 |
let newfilepath = tempdir.path().join(newfilename); |
| 1121 |
|
| 1122 |
// Create file |
| 1123 |
File::create(&oldfilepath).unwrap(); |
| 1124 |
|
| 1125 |
// Create symlink to file |
| 1126 |
symlinkat(&oldfilepath, AT_FDCWD, &symoldfilepath).unwrap(); |
| 1127 |
|
| 1128 |
// Get file descriptor for base directory |
| 1129 |
let dirfd = |
| 1130 |
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) |
| 1131 |
.unwrap(); |
| 1132 |
|
| 1133 |
// Attempt link target of symlink of file at relative path |
| 1134 |
linkat( |
| 1135 |
&dirfd, |
| 1136 |
symoldfilename, |
| 1137 |
&dirfd, |
| 1138 |
newfilename, |
| 1139 |
AtFlags::AT_SYMLINK_FOLLOW, |
| 1140 |
) |
| 1141 |
.unwrap(); |
| 1142 |
|
| 1143 |
let newfilestat = stat::stat(&newfilepath).unwrap(); |
| 1144 |
|
| 1145 |
// Check the file type of the new link |
| 1146 |
assert_eq!( |
| 1147 |
stat::SFlag::from_bits_truncate(newfilestat.st_mode as mode_t) |
| 1148 |
& SFlag::S_IFMT, |
| 1149 |
SFlag::S_IFREG |
| 1150 |
); |
| 1151 |
|
| 1152 |
// Check the number of hard links to the original file |
| 1153 |
assert_eq!(newfilestat.st_nlink, 2); |
| 1154 |
} |
| 1155 |
|
| 1156 |
#[test] |
| 1157 |
#[cfg(not(target_os = "redox"))] |
| 1158 |
fn test_unlinkat_dir_noremovedir() { |
| 1159 |
let tempdir = tempdir().unwrap(); |
| 1160 |
let dirname = "foo_dir"; |
| 1161 |
let dirpath = tempdir.path().join(dirname); |
| 1162 |
|
| 1163 |
// Create dir |
| 1164 |
DirBuilder::new().recursive(true).create(dirpath).unwrap(); |
| 1165 |
|
| 1166 |
// Get file descriptor for base directory |
| 1167 |
let dirfd = |
| 1168 |
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) |
| 1169 |
.unwrap(); |
| 1170 |
|
| 1171 |
// Attempt unlink dir at relative path without proper flag |
| 1172 |
let err_result = |
| 1173 |
unlinkat(&dirfd, dirname, UnlinkatFlags::NoRemoveDir).unwrap_err(); |
| 1174 |
assert!(err_result == Errno::EISDIR || err_result == Errno::EPERM); |
| 1175 |
} |
| 1176 |
|
| 1177 |
#[test] |
| 1178 |
#[cfg(not(target_os = "redox"))] |
| 1179 |
fn test_unlinkat_dir_removedir() { |
| 1180 |
let tempdir = tempdir().unwrap(); |
| 1181 |
let dirname = "foo_dir"; |
| 1182 |
let dirpath = tempdir.path().join(dirname); |
| 1183 |
|
| 1184 |
// Create dir |
| 1185 |
DirBuilder::new().recursive(true).create(&dirpath).unwrap(); |
| 1186 |
|
| 1187 |
// Get file descriptor for base directory |
| 1188 |
let dirfd = |
| 1189 |
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) |
| 1190 |
.unwrap(); |
| 1191 |
|
| 1192 |
// Attempt unlink dir at relative path with proper flag |
| 1193 |
unlinkat(&dirfd, dirname, UnlinkatFlags::RemoveDir).unwrap(); |
| 1194 |
assert!(!dirpath.exists()); |
| 1195 |
} |
| 1196 |
|
| 1197 |
#[test] |
| 1198 |
#[cfg(not(target_os = "redox"))] |
| 1199 |
fn test_unlinkat_file() { |
| 1200 |
let tempdir = tempdir().unwrap(); |
| 1201 |
let filename = "foo.txt"; |
| 1202 |
let filepath = tempdir.path().join(filename); |
| 1203 |
|
| 1204 |
// Create file |
| 1205 |
File::create(&filepath).unwrap(); |
| 1206 |
|
| 1207 |
// Get file descriptor for base directory |
| 1208 |
let dirfd = |
| 1209 |
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty()) |
| 1210 |
.unwrap(); |
| 1211 |
|
| 1212 |
// Attempt unlink file at relative path |
| 1213 |
unlinkat(&dirfd, filename, UnlinkatFlags::NoRemoveDir).unwrap(); |
| 1214 |
assert!(!filepath.exists()); |
| 1215 |
} |
| 1216 |
|
| 1217 |
#[test] |
| 1218 |
fn test_access_not_existing() { |
| 1219 |
let tempdir = tempdir().unwrap(); |
| 1220 |
let dir = tempdir.path().join("does_not_exist.txt"); |
| 1221 |
assert_eq!( |
| 1222 |
access(&dir, AccessFlags::F_OK).err().unwrap(), |
| 1223 |
Errno::ENOENT |
| 1224 |
); |
| 1225 |
} |
| 1226 |
|
| 1227 |
#[test] |
| 1228 |
fn test_access_file_exists() { |
| 1229 |
let tempdir = tempdir().unwrap(); |
| 1230 |
let path = tempdir.path().join("does_exist.txt"); |
| 1231 |
let _file = File::create(path.clone()).unwrap(); |
| 1232 |
access(&path, AccessFlags::R_OK | AccessFlags::W_OK) |
| 1233 |
.expect("assertion failed"); |
| 1234 |
} |
| 1235 |
|
| 1236 |
#[cfg(not(target_os = "redox"))] |
| 1237 |
#[test] |
| 1238 |
fn test_user_into_passwd() { |
| 1239 |
let test_username = if cfg!(target_os = "haiku") { |
| 1240 |
// "nobody" unavailable on haiku |
| 1241 |
"user" |
| 1242 |
} else if cfg!(target_os = "cygwin") { |
| 1243 |
// the Windows admin user |
| 1244 |
"Administrator" |
| 1245 |
} else { |
| 1246 |
// get the UID of the "nobody" user |
| 1247 |
"nobody" |
| 1248 |
}; |
| 1249 |
|
| 1250 |
let nobody = User::from_name(test_username).unwrap().unwrap(); |
| 1251 |
let pwd: libc::passwd = nobody.into(); |
| 1252 |
let _: User = (&pwd).into(); |
| 1253 |
} |
| 1254 |
|
| 1255 |
/// Tests setting the filesystem UID with `setfsuid`. |
| 1256 |
#[cfg(linux_android)] |
| 1257 |
#[test] |
| 1258 |
fn test_setfsuid() { |
| 1259 |
use std::os::unix::fs::PermissionsExt; |
| 1260 |
use std::{fs, io, thread}; |
| 1261 |
require_capability!("test_setfsuid", CAP_SETUID); |
| 1262 |
|
| 1263 |
// get the UID of the "nobody" user |
| 1264 |
let nobody = User::from_name("nobody").unwrap().unwrap(); |
| 1265 |
|
| 1266 |
// create a temporary file with permissions '-rw-r-----' |
| 1267 |
let file = tempfile::NamedTempFile::new_in("/var/tmp").unwrap(); |
| 1268 |
let temp_path = file.into_temp_path(); |
| 1269 |
let temp_path_2 = temp_path.to_path_buf(); |
| 1270 |
let mut permissions = fs::metadata(&temp_path).unwrap().permissions(); |
| 1271 |
permissions.set_mode(0o640); |
| 1272 |
|
| 1273 |
// spawn a new thread where to test setfsuid |
| 1274 |
thread::spawn(move || { |
| 1275 |
// set filesystem UID |
| 1276 |
let fuid = setfsuid(nobody.uid); |
| 1277 |
// trying to open the temporary file should fail with EACCES |
| 1278 |
let res = fs::File::open(&temp_path); |
| 1279 |
let err = res.expect_err("assertion failed"); |
| 1280 |
assert_eq!(err.kind(), io::ErrorKind::PermissionDenied); |
| 1281 |
|
| 1282 |
// assert fuid actually changes |
| 1283 |
let prev_fuid = setfsuid(Uid::from_raw(-1i32 as u32)); |
| 1284 |
assert_ne!(prev_fuid, fuid); |
| 1285 |
}) |
| 1286 |
.join() |
| 1287 |
.unwrap(); |
| 1288 |
|
| 1289 |
// open the temporary file with the current thread filesystem UID |
| 1290 |
fs::File::open(temp_path_2).unwrap(); |
| 1291 |
} |
| 1292 |
|
| 1293 |
#[test] |
| 1294 |
#[cfg(not(any( |
| 1295 |
target_os = "redox", |
| 1296 |
target_os = "fuchsia", |
| 1297 |
target_os = "haiku" |
| 1298 |
)))] |
| 1299 |
fn test_ttyname() { |
| 1300 |
use std::os::fd::AsRawFd; |
| 1301 |
|
| 1302 |
let fd = posix_openpt(OFlag::O_RDWR).expect("posix_openpt failed"); |
| 1303 |
assert!(fd.as_raw_fd() > 0); |
| 1304 |
|
| 1305 |
// on linux, we can just call ttyname on the pty master directly, but |
| 1306 |
// apparently osx requires that ttyname is called on a slave pty (can't |
| 1307 |
// find this documented anywhere, but it seems to empirically be the case) |
| 1308 |
grantpt(&fd).expect("grantpt failed"); |
| 1309 |
unlockpt(&fd).expect("unlockpt failed"); |
| 1310 |
let sname = unsafe { ptsname(&fd) }.expect("ptsname failed"); |
| 1311 |
let fds = fs::OpenOptions::new() |
| 1312 |
.read(true) |
| 1313 |
.write(true) |
| 1314 |
.open(Path::new(&sname)) |
| 1315 |
.expect("open failed"); |
| 1316 |
|
| 1317 |
let name = ttyname(fds).expect("ttyname failed"); |
| 1318 |
assert!(name.starts_with("/dev")); |
| 1319 |
} |
| 1320 |
|
| 1321 |
#[test] |
| 1322 |
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))] |
| 1323 |
fn test_ttyname_not_pty() { |
| 1324 |
let fd = File::open("/dev/zero").unwrap(); |
| 1325 |
assert_eq!(ttyname(fd), Err(Errno::ENOTTY)); |
| 1326 |
} |
| 1327 |
|
| 1328 |
#[test] |
| 1329 |
#[cfg(bsd)] |
| 1330 |
fn test_getpeereid() { |
| 1331 |
use std::os::unix::net::UnixStream; |
| 1332 |
let (sock_a, sock_b) = UnixStream::pair().unwrap(); |
| 1333 |
|
| 1334 |
let (uid_a, gid_a) = getpeereid(sock_a).unwrap(); |
| 1335 |
let (uid_b, gid_b) = getpeereid(sock_b).unwrap(); |
| 1336 |
|
| 1337 |
let uid = geteuid(); |
| 1338 |
let gid = getegid(); |
| 1339 |
|
| 1340 |
assert_eq!(uid, uid_a); |
| 1341 |
assert_eq!(gid, gid_a); |
| 1342 |
assert_eq!(uid_a, uid_b); |
| 1343 |
assert_eq!(gid_a, gid_b); |
| 1344 |
} |
| 1345 |
|
| 1346 |
#[test] |
| 1347 |
#[cfg(not(target_os = "redox"))] |
| 1348 |
fn test_faccessat_none_not_existing() { |
| 1349 |
use nix::fcntl::AtFlags; |
| 1350 |
use nix::fcntl::AT_FDCWD; |
| 1351 |
|
| 1352 |
let tempdir = tempfile::tempdir().unwrap(); |
| 1353 |
let dir = tempdir.path().join("does_not_exist.txt"); |
| 1354 |
assert_eq!( |
| 1355 |
faccessat(AT_FDCWD, &dir, AccessFlags::F_OK, AtFlags::empty()) |
| 1356 |
.err() |
| 1357 |
.unwrap(), |
| 1358 |
Errno::ENOENT |
| 1359 |
); |
| 1360 |
} |
| 1361 |
|
| 1362 |
#[test] |
| 1363 |
#[cfg(not(target_os = "redox"))] |
| 1364 |
fn test_faccessat_not_existing() { |
| 1365 |
use nix::fcntl::AtFlags; |
| 1366 |
|
| 1367 |
let tempdir = tempfile::tempdir().unwrap(); |
| 1368 |
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); |
| 1369 |
let not_exist_file = "does_not_exist.txt"; |
| 1370 |
assert_eq!( |
| 1371 |
faccessat(&dirfd, not_exist_file, AccessFlags::F_OK, AtFlags::empty(),) |
| 1372 |
.err() |
| 1373 |
.unwrap(), |
| 1374 |
Errno::ENOENT |
| 1375 |
); |
| 1376 |
} |
| 1377 |
|
| 1378 |
#[test] |
| 1379 |
#[cfg(not(target_os = "redox"))] |
| 1380 |
fn test_faccessat_none_file_exists() { |
| 1381 |
use nix::fcntl::AtFlags; |
| 1382 |
use nix::fcntl::AT_FDCWD; |
| 1383 |
|
| 1384 |
let tempdir = tempfile::tempdir().unwrap(); |
| 1385 |
let path = tempdir.path().join("does_exist.txt"); |
| 1386 |
let _file = File::create(path.clone()).unwrap(); |
| 1387 |
assert!(faccessat( |
| 1388 |
AT_FDCWD, |
| 1389 |
&path, |
| 1390 |
AccessFlags::R_OK | AccessFlags::W_OK, |
| 1391 |
AtFlags::empty(), |
| 1392 |
) |
| 1393 |
.is_ok()); |
| 1394 |
} |
| 1395 |
|
| 1396 |
#[test] |
| 1397 |
#[cfg(not(target_os = "redox"))] |
| 1398 |
fn test_faccessat_file_exists() { |
| 1399 |
use nix::fcntl::AtFlags; |
| 1400 |
|
| 1401 |
let tempdir = tempfile::tempdir().unwrap(); |
| 1402 |
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); |
| 1403 |
let exist_file = "does_exist.txt"; |
| 1404 |
let path = tempdir.path().join(exist_file); |
| 1405 |
let _file = File::create(path.clone()).unwrap(); |
| 1406 |
assert!(faccessat( |
| 1407 |
&dirfd, |
| 1408 |
&path, |
| 1409 |
AccessFlags::R_OK | AccessFlags::W_OK, |
| 1410 |
AtFlags::empty(), |
| 1411 |
) |
| 1412 |
.is_ok()); |
| 1413 |
} |
| 1414 |
|
| 1415 |
#[test] |
| 1416 |
#[cfg(any(all(target_os = "linux", not(target_env = "uclibc")), freebsdlike))] |
| 1417 |
fn test_eaccess_not_existing() { |
| 1418 |
let tempdir = tempdir().unwrap(); |
| 1419 |
let dir = tempdir.path().join("does_not_exist.txt"); |
| 1420 |
assert_eq!( |
| 1421 |
eaccess(&dir, AccessFlags::F_OK).err().unwrap(), |
| 1422 |
Errno::ENOENT |
| 1423 |
); |
| 1424 |
} |
| 1425 |
|
| 1426 |
#[test] |
| 1427 |
#[cfg(any(all(target_os = "linux", not(target_env = "uclibc")), freebsdlike))] |
| 1428 |
fn test_eaccess_file_exists() { |
| 1429 |
let tempdir = tempdir().unwrap(); |
| 1430 |
let path = tempdir.path().join("does_exist.txt"); |
| 1431 |
let _file = File::create(path.clone()).unwrap(); |
| 1432 |
eaccess(&path, AccessFlags::R_OK | AccessFlags::W_OK) |
| 1433 |
.expect("assertion failed"); |
| 1434 |
} |
| 1435 |
|
| 1436 |
#[test] |
| 1437 |
#[cfg(bsd)] |
| 1438 |
fn test_group_from() { |
| 1439 |
let group = Group::from_name("wheel").unwrap().unwrap(); |
| 1440 |
assert!(group.name == "wheel"); |
| 1441 |
let group_id = group.gid; |
| 1442 |
let group = Group::from_gid(group_id).unwrap().unwrap(); |
| 1443 |
assert_eq!(group.gid, group_id); |
| 1444 |
assert_eq!(group.name, "wheel"); |
| 1445 |
} |