summaryrefslogtreecommitdiff
path: root/src/lib.rs
AgeCommit message (Collapse)Author
2022-12-02Enable ucontext module on aarch64-unknown-linux-gnuRyan Zoeller
2022-10-23Define `MntFlags` and `unmount` on all of the BSDs.Alan Somers
2022-07-23Fix SockaddrLike::from_raw with unaligned inputsAlan Somers
The major users of this function are functions like gethostname, which will always properly align their buffers. But out-of-crate consumers could manually construct an unaligned buffer. Handle that correctly. Enable Clippy's cast_ptr_alignment lint. It's disabled by default as it reports many false positives, but it would've caught this problem. Reported-by: Miri Fixes: 1769
2022-07-10More docs for the dir moduleAlan Somers
2022-07-10More docs for mqueue.Alan Somers
Also, delete some dead code. It's always been dead.
2022-06-24Fix all formating problems to pass CI formating testCostin-Robert Sin
Signed-off-by: Costin-Robert Sin <sin.costinrobert@gmail.com>
2022-06-06Cleanup cfg blocksRyan Zoeller
Remove obsolete references to target_env = wasi, target_os = nacl, target_os = osx, and a typo'd target_os = fushsia that didn't compile when fixed. - target_env = wasi is dead: https://github.com/rust-lang/rust/pull/60117 - target_os = nacl is dead: https://github.com/rust-lang/rust/pull/45041 - target_os = osx is dead, but I can't find a link.
2022-04-09Use singular number for all feature namesAlan Somers
features => feature users => user Neither of these features have yet been included in a release, so it's ok to rename them.
2022-03-21Replace the Sockaddr enum with a unionAlan Somers
The SockAddr enum is quite large, and the user must allocate space for the whole thing even though he usually knows what type he needs. Furthermore, thanks to the sa_family field, the sockaddr types are basically an enum even in C. So replace the ungainly enum with a SockaddrLike trait implemented by all sockaddr types and a SockaddrStorage union that has safe accessors. Also, deprecate InetAddr, which only existed to support SockAddr. Supplants #1504 Fixes #1544
2022-02-27Remove `PATH_MAX` restriction from `with_nix_path`Alex Saveau
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-02-14Merge #1655bors[bot]
1655: 30x performance improvement in `with_nix_path` r=rtzoeller a=SUPERCILEX I've been digging into CPU instructions counts and found that `nix` accounted for an eye-watering 85% of my program's instruction counts (yes, I do a lot of I/O, I know). The fix is simple: don't initialize the stack memory since we're just going to overwrite it anyway. > Note: I also ran rustfmt in a separate commit, not sure if that's ok. ### Before ``` 650,398,225 (85.05%) 5,451,056 (17.31%) 627,714,969 (97.28%) 60 ( 0.54%) 22 ( 0.07%) 3,997 (20.98%) 10 ( 0.18%) 0 752 ( 5.98%) 627,716,244 (97.30%) 267,333 (34.62%) 914,814 (35.11%) 105 ( 0.73%) => /home/asaveau/Desktop/nix/src/lib.rs:<[u8] as nix::NixPath>::with_nix_path (152,469x) ``` ``` 1,677,159 ( 0.22%) 0 762,345 ( 0.12%) 14 ( 0.13%) 0 0 2 ( 0.04%) . . . . . . fn with_nix_path<T, F>(&self, f: F) -> Result<T> 2,287,035 ( 0.30%) 304,938 ( 0.97%) 152,469 ( 0.02%) 5 ( 0.04%) 0 0 0 0 0 304,938 ( 0.05%) . . . => ???:__rust_probestack (152,469x) . . . . . . . . . . . . . where . . . . . . . . . . . . . F: FnOnce(&CStr) -> T, . . . . . . . . . . . . . { 457,407 ( 0.06%) 152,469 ( 0.48%) 152,469 ( 0.02%) 0 0 93 ( 0.49%) 0 0 16 ( 0.13%) 0 0 152,469 ( 5.85%) 97 ( 0.67%) let mut buf = [0u8; PATH_MAX as usize]; 627,104,997 (82.00%) 304,938 ( 0.97%) 624,513,024 (96.78%) 1 ( 0.01%) 0 3,814 (20.02%) 1 ( 0.02%) 0 720 ( 5.72%) 625,122,900 (96.90%) 152,495 (19.75%) . . => ./string/../sysdeps/x86_64/multiarch/memset-vec-unaligned-erms.S:__memset_avx2_unaligned_erms (152,469x) . . . . . . . . . . . . . 304,938 ( 0.04%) 0 0 0 0 0 0 0 0 152,469 ( 0.02%) 2 ( 0.00%) . . if self.len() >= PATH_MAX as usize { . . . . . . . . . . . . . return Err(Errno::ENAMETOOLONG); . . . . . . . . . . . . . } . . . . . . . . . . . . . 1,067,283 ( 0.14%) 152,469 ( 0.48%) 152,469 ( 0.02%) 1 ( 0.01%) 4 ( 0.01%) 0 1 ( 0.02%) 0 0 0 0 152,469 ( 5.85%) 1 ( 0.01%) buf[..self.len()].copy_from_slice(self); 3,202,541 ( 0.42%) 914,814 ( 2.91%) 609,876 ( 0.09%) 1 ( 0.01%) 2 ( 0.01%) 0 0 0 0 458,005 ( 0.07%) 114,562 (14.84%) 152,469 ( 5.85%) . => /rustc/21b4a9cfdcbb1e76f4b36b5c3cfd64d627285093/library/core/src/slice/mod.rs:core::slice::<impl [T]>::copy_from_slice (152,469x) 762,345 ( 0.10%) 304,938 ( 0.97%) 152,469 ( 0.02%) 0 6 ( 0.02%) 0 0 0 0 152,469 ( 0.02%) 6 ( 0.00%) 152,469 ( 5.85%) 1 ( 0.01%) match CStr::from_bytes_with_nul(&buf[..=self.len()]) { 8,350,190 ( 1.09%) 1,181,828 ( 3.75%) 1,067,283 ( 0.17%) 19 ( 0.17%) 2 ( 0.01%) 87 ( 0.46%) 3 ( 0.05%) 0 16 ( 0.13%) 1,220,525 ( 0.19%) 167 ( 0.02%) 152,469 ( 5.85%) 5 ( 0.03%) => /rustc/21b4a9cfdcbb1e76f4b36b5c3cfd64d627285093//library/std/src/ffi/c_str.rs:std::ffi::c_str::CStr::from_bytes_with_nul (152,469x) 609,876 ( 0.08%) 609,876 ( 1.94%) 0 0 1 ( 0.00%) . . . . . . . . Ok(s) => Ok(f(s)), . . . . . . . . . . . . . Err(_) => Err(Errno::EINVAL), . . . . . . . . . . . . . } 914,814 ( 0.12%) 762,345 ( 2.42%) . . . . . . . . . . . } . . . . . . . . . . . . . } ``` ### After ``` 21,462,416 (15.81%) 4,688,455 (15.27%) 2,896,847 (14.17%) 74 ( 0.64%) 11 ( 0.04%) 249 ( 1.58%) 8 ( 0.15%) 0 48 ( 0.38%) 2,593,200 (12.98%) 1,128 ( 0.22%) 762,305 (31.08%) 158 ( 1.32%) => /home/asaveau/Desktop/nix/src/lib.rs:<[u8] as nix::NixPath>::with_nix_path (152,461x) ``` ``` 1,067,227 ( 0.79%) 0 609,844 ( 2.98%) 1 ( 0.01%) 0 0 1 ( 0.02%) . . . . . . fn with_nix_path<T, F>(&self, f: F) -> Result<T> 2,286,915 ( 1.68%) 304,922 ( 0.99%) 152,461 ( 0.75%) 19 ( 0.16%) 0 0 0 0 0 304,922 ( 1.53%) . . . => ???:__rust_probestack (152,461x) . . . . . . . . . . . . . where . . . . . . . . . . . . . F: FnOnce(&CStr) -> T, . . . . . . . . . . . . . { 304,922 ( 0.22%) 0 0 0 0 0 0 0 0 152,461 ( 0.76%) 6 ( 0.00%) . . if self.len() >= PATH_MAX as usize { . . . . . . . . . . . . . return Err(Errno::ENAMETOOLONG); . . . . . . . . . . . . . } . . . . . . . . . . . . . . . . . . . . . . . . . . let mut buf = MaybeUninit::<[u8; PATH_MAX as usize]>::uninit(); . . . . . . . . . . . . . let buf_ptr = buf.as_mut_ptr() as *mut u8; . . . . . . . . . . . . . . . . . . . . . . . . . . unsafe { . . . . . . . . . . . . . ptr::copy_nonoverlapping(self.as_ptr(), buf_ptr, self.len()); . . . . . . . . . . . . . buf_ptr.add(self.len()).write(0); . . . . . . . . . . . . . } . . . . . . . . . . . . . 1,067,227 ( 0.79%) 304,922 ( 0.99%) 152,461 ( 0.75%) 1 ( 0.01%) 3 ( 0.01%) 0 1 ( 0.02%) 0 0 304,922 ( 1.53%) 4 ( 0.00%) 152,461 ( 6.22%) 1 ( 0.01%) match CStr::from_bytes_with_nul(unsafe { slice::from_raw_parts(buf_ptr, self.len() + 1) }) { 8,349,726 ( 6.15%) 1,181,764 ( 3.85%) 1,067,227 ( 5.22%) 18 ( 0.15%) 1 ( 0.00%) 83 ( 0.53%) 2 ( 0.04%) 0 16 ( 0.13%) 1,220,453 ( 6.11%) 158 ( 0.03%) 152,461 ( 6.22%) 4 ( 0.03%) => /rustc/21b4a9cfdcbb1e76f4b36b5c3cfd64d627285093//library/std/src/ffi/c_str.rs:std::ffi::c_str::CStr::from_bytes_with_nul (152,461x) 609,844 ( 0.45%) 609,844 ( 1.99%) . . . . . . . . . . . Ok(s) => Ok(f(s)), . . . . . . . . . . . . . Err(_) => Err(Errno::EINVAL), . . . . . . . . . . . . . } 762,305 ( 0.56%) 609,844 ( 1.99%) 0 1 ( 0.01%) 0 0 1 ( 0.02%) . . . . . . } . . . . . . . . . . . . . } ``` Co-authored-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-02-1330X performance improvement in with_nix_path by not initializing memoryAlex Saveau
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-02-13Enable uconxtext module for s390xRyan Zoeller
Added to libc by https://github.com/rust-lang/libc/commit/38569c719befeb5b5051caeb6b0ff628ccd0ff90
2021-12-21Merge #1583bors[bot]
1583: Remove unsafe in with_nix_path() for [u8] r=asomers a=rtzoeller Remove use of `unsafe` in the implementation of `with_nix_path()` for `[u8]`. This also comes with a nice determinism win across input sizes, and is fairly performance neutral (slightly slower for small strings, much faster for large strings). I suspect the performance degradation in the existing implementation is related to the following note in the `CStr::from_ptr()` documentation: > Note: This operation is intended to be a 0-cost cast but it is currently implemented with an up-front calculation of the length of the string. This is not guaranteed to always be the case. --- Tested with `cargo 1.57.0-nightly (7fbbf4e8f 2021-10-19)`, with variations of the following benchmarking code: ```rs #[bench] fn bench_with_nix_path_1024(b: &mut test::Bencher) { let bytes = std::hint::black_box([70u8; 1024]); b.iter(|| { bytes.with_nix_path(|cstr| { std::hint::black_box(&cstr); }).unwrap(); }) } ``` | Length | Before Change | After Change | |--------|-----------------------|-----------------------| | 16 | 37 ns/iter (+/- 0) | 44 ns/iter (+/- 0) | | 64 | 39 ns/iter (+/- 0) | 44 ns/iter (+/- 0) | | 256 | 84 ns/iter (+/- 0) | 48 ns/iter (+/- 0) | | 1024 | 232 ns/iter (+/- 1) | 50 ns/iter (+/- 1) | | 4095 | 796 ns/iter (+/- 8) | 62 ns/iter (+/- 2) | Co-authored-by: Ryan Zoeller <rtzoeller@rtzoeller.com>
2021-12-20feature-gate most Nix functionsVincent Dagonneau
Using features reduces build time and size for consumer crates. By default all features are enabled.
2021-10-22Remove unsafe in with_nix_path() for [u8]Ryan Zoeller
2021-09-19Clippy cleanupAlan Somers
And this time, start running Clippy in CI
2021-09-04Document more thingsAlan Somers
Also, test rustdoc in CI, and demote missing_docs from a deny to a warning (but still deny it in CI).
2021-08-28Merge #1507 #1508bors[bot]
1507: Forbid dead code again r=asomers a=asomers The old problems with bitflags no longer apply 1508: More rust docs r=asomers a=asomers Switch from allow(missing_docs) to deny(missing_docs), which should gradually help us moving forward. Also, add a few missing docs, such as for sched and aio. Co-authored-by: Alan Somers <asomers@gmail.com>
2021-08-28More rust docsAlan Somers
Switch from allow(missing_docs) to deny(missing_docs), which should gradually help us moving forward. Also, add a few missing docs, such as for sched and aio.
2021-08-10Forbid dead code againAlan Somers
The old problems with bitflags no longer apply
2021-07-07Collapse Error into ErrnoAlan Somers
Now that Nix's weird error types are eliminated, there's no reason not to simply use Errno as the Error type.
2021-07-07Overhaul Nix's error typesAlan Somers
For many of Nix's consumers it be convenient to easily convert a Nix error into a std::io::Error. That's currently not possible because of the InvalidPath, InvalidUtf8, and UnsupportedOperation types that have no equivalent in std::io::Error. However, very few of Nix's public APIs actually return those unusual errors. So a more useful API would be for Nix's standard error type to implement Into<std::io::Error>. This commit makes Error a simple NewType around Errno. For most functions it's a drop-in replacement. There are only three exceptions: * clearenv now returns a bespoke error type. It was the only Nix function whose error couldn't be cleanly mapped onto an Errno. * sys::signal::signal now returns Error(Errno::ENOTSUP) instead of Error::UnsupportedOperation when the user passes an incompatible argument to `handler`. * When a NixPath exceeds PATH_MAX, it will now return Error(Errno::ENAMETOOLONG) instead of Error::InvalidPath. In the latter two cases there is now some abiguity about whether the error code was generated by Nix or by the OS. But I think the ambiguity is worth it for the sake of being able to implement Into<io::Error>. This commit also introduces Error::Sys() as a migration aid. Previously that as an enum variant. Now it's a function, but it will work in many of the same contexts as the original. Fixes #1155
2021-06-13Add nmount for FreeBSD.Alan Somers
2021-03-21illumos and Solaris supportJason King
Co-authored-by: Dominik Hassler <hadfl@omnios.org> Co-authored-by: Joshua M. Clulow <josh@sysmgr.org>
2020-12-19Add fuchsia supportAmanda Tait
Allow nix to compile on Fuchsia by conditionally avoiding libc functionality that does not exist for Fuchsia.
2020-09-28Added clock_gettime, clock_getres, clock_settime, clock_getcpuclockidDaniil Bondarev
2020-05-31Convert the crate to edition 2018Alan Somers
2020-05-17Simply disable the dir module, since only the entry struct is availableXavier L'Heureux
2020-05-17Add pollXavier L'Heureux
2020-05-17Add Redox support for most of the modulesXavier L'Heureux
Some things are not implemented yet in redox, so a lot of annotations were added to remove functions when compiling for redox. Those functions will hopefully be added in time, but for now it's better to have partial support than none. Blocked by https://github.com/rust-lang/libc/pull/1438
2020-05-17Replace void crate with Rust standard lib Infallible typeOssi Herrala
std::convert::Infallible has been available since Rust 1.34 and nix currently targets Rust 1.36 or later so this should not cause problems. Fixes #1238
2020-05-16Don't implement `NixPath` for `Option<&P> where P: NixPath`Alan Somers
Most Nix functions that accept `NixPath` arguments can't do anything useful with `None`. The exceptions (`mount` and `quotactl_sync`) already take explicitly optional arguments. Also, this changes the behavior of `mount` with `None` arguments. Previously, it would call mount(2) with empty strings for those arguments. Now, it will use null pointers.
2020-02-11Provide clearenv()John Gallagher
2019-12-29remove deprecated Error::descriptionRadyk Andrii
2019-12-01Remove the last use of mem::uninitializedAlan Somers
Replace it with mem::zeroed. It isn't perfect, but it's better than it was. Issue #1115
2019-08-29Clippy cleanupAlan Somers
2019-07-13Temporarily allow deprecated itemsAlan Somers
This fixes the tests on Rust 1.38.0. We'll fix them for real after release 0.15.0. Issue #1096
2019-07-01Move cross compilation cfg for sched into sched modJakub Konka
2019-06-09Add extra traits for all typesBryant Mairs
Derive Clone, Copy, Eq, Hash, and PartialEq for all types. Not all traits are supported by all types, which is why many are missing some.
2019-06-07ifaddrs: add ifaddrs support for androidPaul Osborne
Support under bionic/android is the same as under Linux for what is exposed by this code. Signed-off-by: Paul Osborne <osbpau@gmail.com>
2018-11-23Add Error::as_errnoAlan Somers
This method is useful when it can be statically determined that a nix::Error be an errno, which I find to be very common.
2018-09-05Add wrapper for linux kernel module loadingPascal Bach
- init_module and finit_module to load kernel modules - delete_module to unload kernel modules Signed-off-by: Pascal Bach <pascal.bach@nextrem.ch>
2018-09-03new dir moduleScott Lamb
This is a lower-level interface than `std::fs::ReadDir`. Notable differences: * can be opened from a file descriptor (as returned by `openat`, perhaps before knowing if the path represents a file or directory). Uses `fdopendir` for this, available on all Unix platforms as of rust-lang/libc#1018. * implements `AsRawFd`, so it can be passed to `fstat`, `openat`, etc. * can be iterated through multiple times without closing and reopening the file descriptor. Each iteration rewinds when finished. * returns entries for `.` (current directory) and `..` (parent directory). * returns entries' names as a `CStr` (no allocation or conversion beyond whatever libc does).
2018-03-22Replace AioCb::from_bytes with more generic from_boxed_sliceAlan Somers
Supporting the bytes crate was unnecessarily specific. This change replaces from_bytes and from_bytes_mut with from_boxed_slice and from_boxed_mut_slice, which can work with anything that implements Borrow<[u8]> and BorrowMut<[u8]>, respectively.
2018-02-02Re-add ifaddrs moduleBryant Mairs
Mistakingly removed in d1be45d8405
2018-01-28Fail to compile for any unused codeBryant Mairs
2018-01-28Clean up imports and uses in lib.rsBryant Mairs
2018-01-28Require docs for features and ifaddrs moduleBryant Mairs
2018-01-28Require docs for the net/if_ moduleBryant Mairs