aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/util.cc
AgeCommit message (Collapse)Author
2022-04-20Merge remote-tracking branch 'upstream/master' into fix-url-formatJohn Ericson
2022-04-19Merge pull request #6128 from ncfavier/fix-completionEelco Dolstra
Shell completion improvements
2022-04-19Avoid `fmt` when constructor already does itJohn Ericson
There is a correctnes issue here, but #3724 will fix that. This is just a cleanup for brevity's sake.
2022-04-07Merge pull request #6348 from cole-h/fix-restoring-mount-namespaceEelco Dolstra
libutil: Fix restoring mount namespace
2022-04-05Merge pull request #6366 from danpls/base64-reserveEelco Dolstra
libutil: Reserve memory when en/decoding base64
2022-04-05tokenizeString: Fix semantic mistakeDaniel Pauls
`string_view::find_first_not_of(...)` and `string_view::find_first_of(...)` return `string_view::npos` on error not `string::npos`.
2022-04-05libutil: Reserve memory when en/decoding base64Daniel Pauls
The size of the output when encoding to and decoding from base64 is (roughly) known so we can allocate it in advance to prevent reallocation.
2022-04-04libutil: don't save cwd fd, use path insteadCole Helbling
Saving the cwd fd didn't actually work well -- prior to this commit, the following would happen: : ~/w/vc/nix ; doas outputs/out/bin/nix --experimental-features 'nix-command flakes' run nixpkgs#coreutils -- --coreutils-prog=pwd pwd: couldn't find directory entry in ‘../../../..’ with matching i-node : ~/w/vc/nix ; doas outputs/out/bin/nix --experimental-features 'nix-command flakes' develop -c pwd pwd: couldn't find directory entry in ‘../../../..’ with matching i-node
2022-04-04libutil: save cwd fd in restoreMountNamespaceCole Helbling
This doesn't work very well (maybe I'm misunderstanding the desired implementation): : ~/w/vc/nix ; doas outputs/out/bin/nix --experimental-features 'nix-command flakes' develop -c pwd pwd: couldn't find directory entry in ‘../../../..’ with matching i-node
2022-04-04libutil: `try` restoring the cwd from fdSavedCwdCole Helbling
2022-04-04libutil: save fd to cwd instead of cwd itselfCole Helbling
2022-04-04libutil: cleanup savedCwd logicCole Helbling
Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
2022-04-01libutil: Properly guard self-allocating getcwd on GNUCole Helbling
It's a GNU extension, as pointed out by pennae.
2022-04-01libutil: Don't use std::filesystemCole Helbling
Just in case making libutil depend on std::filesystem is unacceptable, here is the non-filesystem approach.
2022-04-01libutil: Fix restoring mount namespaceaszlig
I regularly pass around simple scripts by using nix-shell as the script interpreter, eg. like this: #!/usr/bin/env nix-shell #!nix-shell -p dd_rescue coreutils bash -i bash While this works most of the time, I recently had one occasion where it would not and the above would result in the following: $ sudo ./myscript.sh bash: ./myscript.sh: No such file or directory Note the "sudo" here, because this error only occurs if we're root. The reason for the latter is because running Nix as root means that we can directly access the store, which makes sure we use a filesystem namespace to make the store writable. XXX - REWORD! So when stracing the process, I stumbled on the following sequence: openat(AT_FDCWD, "/proc/self/ns/mnt", O_RDONLY) = 3 unshare(CLONE_NEWNS) = 0 ... later ... getcwd("/the/real/cwd", 4096) = 14 setns(3, CLONE_NEWNS) = 0 getcwd("/", 4096) = 2 In the whole strace output there are no calls to chdir() whatsoever, so I decided to look into the kernel source to see what else could change directories and found this[1]: /* Update the pwd and root */ set_fs_pwd(fs, &root); set_fs_root(fs, &root); The set_fs_pwd() call is roughly equivalent to a chdir() syscall and this is called when the setns() syscall is invoked[2]. [1]: https://github.com/torvalds/linux/blob/b14ffae378aa1db993e62b01392e70d1e585fb23/fs/namespace.c#L4659 [2]: https://github.com/torvalds/linux/blob/b14ffae378aa1db993e62b01392e70d1e585fb23/kernel/nsproxy.c#L346
2022-03-31replaceEnv(): Pass newEnv by referenceEelco Dolstra
2022-03-13nix store gc: account for auto-optimised storeSergei Trofimovich
Before the change on a system with `auto-optimise-store = true`: $ nix store gc --verbose --max 1 deleted all the paths instead of one path (we requested 1 byte limit). It happens because every file in `auto-optimise-store = true` has at least 2 links: file itself and a link in /nix/store/.links/ directory. The change conservatively assumes that any file that has one (as before) or two links (assume auto-potimise mode) will free space. Co-authored-by: Sandro <sandro.jaeckel@gmail.com>
2022-03-09make Finally more localpennae
no need for function<> with c++17 deduction. this saves allocations and virtual calls, but has the same semantics otherwise. not going through function has the side effect of giving compilers more insight into the cleanup code, so we need a few local warning disables.
2022-03-07Perform tilde expansion when completing flake fragmentsNaïm Favier
Allows completing `nix build ~/flake#<Tab>`. We can implement expansion for `~user` later if needed. Not using wordexp(3) since that expands way too much.
2022-03-07Make completeDir follow symlinksNaïm Favier
Allows completing `nix why-depends /run/cur<Tab>` to /run/current-system
2022-03-03Fix segfault in headerCallback()Eelco Dolstra
https://hydra.nixos.org/build/168594664
2022-03-01filterANSIEscapes: Ignore BEL characterAnders Kaseorg
GCC is not as good at music as it seems to think it is. Fixes #4546. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2022-02-25Remove std::string alias (for real this time)Eelco Dolstra
Also use std::string_view in a few more places.
2022-02-21Remove std::vector aliasEelco Dolstra
2022-02-21Remove std::set aliasEelco Dolstra
2022-02-21Merge pull request #6052 from ↵Eelco Dolstra
hercules-ci/issue-3294-fix-interruptCallback-deadlock Fix deadlocked nix-daemon zombies on darwin #3294
2022-02-21triggerInterrupt: Refactor to use breakRobert Hensing
2022-02-07Make sure no exceptions leave ignoreException()Sergei Trofimovich
I noticed that occasional Ctrl-C leaves *.lock files around. `nix-daemon`'s journal logs contained crashes like: nix-daemon[30416]: terminate called after throwing an instance of 'nix::SysError' nix-daemon[30416]: what(): error: writing to file: Broken pipe And core dump backtraces pointed at `teriminate()` call from destructors: ... _Unwind_Resume () nix::ignoreException() () nix::LocalDerivationGoal::~LocalDerivationGoal() ... void ignoreException() { try { throw; } catch (std::exception & e) { printError("error (ignored): %1%", e.what()); } } The crashes happen when client side closes early and printError() throws an IO error. The change wraps `ignoreException()` into blanket `try { ... } catch (...) {}`. Closes: https://github.com/NixOS/nix/issues/6046
2022-02-06Don't hold interruptCallbacks lock during interrupt handlingRobert Hensing
This changes the representation of the interrupt callback list to be safe to use during interrupt handling. Holding a lock while executing arbitrary functions is something to avoid in general, because of the risk of deadlock. Such a deadlock occurs in https://github.com/NixOS/nix/issues/3294 where ~CurlDownloader tries to deregister its interrupt callback. This happens during what seems to be a triggerInterrupt() by the daemon connection's MonitorFdHup thread. This bit I can not confirm based on the stack trace though; it's based on reading the code, so no absolute certainty, but a smoking gun nonetheless.
2022-02-02Merge branch 'more-stringviews' of https://github.com/pennae/nixEelco Dolstra
2022-01-29canonPath: fix missing slash when resolving linksWill Dietz
Fixes #6017
2022-01-27convert a for more utilities to string_viewpennae
2022-01-18Merge pull request #5906 from pennae/primops-optimizationEelco Dolstra
optimize primops and utils by caching more and copying less
2022-01-18Merge pull request #5932 from edolstra/remove-shared-stringsEelco Dolstra
Remove shared strings
2022-01-18Get rid of std::shared_ptr<std::string> and ref<std::string>Eelco Dolstra
These were needed back in the pre-C++11 era because we didn't have move semantics. But now we do.
2022-01-17Merge pull request #5812 from pennae/small-perf-improvementsEelco Dolstra
improve parser performance a bit
2022-01-13avoid copies of parser input datapennae
when given a string yacc will copy the entire input to a newly allocated location so that it can add a second terminating NUL byte. since the parser is a very internal thing to EvalState we can ensure that having two terminating NUL bytes is always possible without copying, and have the parser itself merely check that the expected NULs are present. # before Benchmark 1: nix search --offline nixpkgs hello Time (mean ± σ): 572.4 ms ± 2.3 ms [User: 563.4 ms, System: 8.6 ms] Range (min … max): 566.9 ms … 579.1 ms 50 runs Benchmark 2: nix eval -f ../nixpkgs/pkgs/development/haskell-modules/hackage-packages.nix Time (mean ± σ): 381.7 ms ± 1.0 ms [User: 348.3 ms, System: 33.1 ms] Range (min … max): 380.2 ms … 387.7 ms 50 runs Benchmark 3: nix eval --raw --impure --expr 'with import <nixpkgs/nixos> {}; system' Time (mean ± σ): 2.936 s ± 0.005 s [User: 2.715 s, System: 0.221 s] Range (min … max): 2.923 s … 2.946 s 50 runs # after Benchmark 1: nix search --offline nixpkgs hello Time (mean ± σ): 571.7 ms ± 2.4 ms [User: 563.3 ms, System: 8.0 ms] Range (min … max): 566.7 ms … 579.7 ms 50 runs Benchmark 2: nix eval -f ../nixpkgs/pkgs/development/haskell-modules/hackage-packages.nix Time (mean ± σ): 376.6 ms ± 1.0 ms [User: 345.8 ms, System: 30.5 ms] Range (min … max): 374.5 ms … 379.1 ms 50 runs Benchmark 3: nix eval --raw --impure --expr 'with import <nixpkgs/nixos> {}; system' Time (mean ± σ): 2.922 s ± 0.006 s [User: 2.707 s, System: 0.215 s] Range (min … max): 2.906 s … 2.934 s 50 runs
2022-01-13use more string_view in utilspennae
there's a couple places that can be easily converted from using strings to using string_views instead. gives a slight (~1%) boost to system eval. # before nix eval --raw --impure --expr 'with import <nixpkgs/nixos> {}; system' Time (mean ± σ): 2.946 s ± 0.026 s [User: 2.655 s, System: 0.209 s] Range (min … max): 2.905 s … 2.995 s 20 runs # after Time (mean ± σ): 2.928 s ± 0.024 s [User: 2.638 s, System: 0.211 s] Range (min … max): 2.893 s … 2.970 s 20 runs
2022-01-07isInDir() / isDirOrInDir(): Use std::string_viewEelco Dolstra
2021-12-22Remove CPU lockingEelco Dolstra
This was already accidentally disabled in ba87b08. It also no longer appears to be beneficial, and in fact slow things down, e.g. when evaluating a NixOS system configuration: elapsed time: median = 3.8170 mean = 3.8202 stddev = 0.0195 min = 3.7894 max = 3.8600 [rejected, p=0.00000, Δ=0.36929±0.02513]
2021-12-16Ignore EPERM when unsharing FS stateEelco Dolstra
On Docker (but not podman), unshare(CLONE_FS) fails with EPERM. So let's ignore it and hope nothing bad happens. Attempted fix for #5777.
2021-11-29createTempFile(): Mark file as CLOEEXECEelco Dolstra
Fixes #5674.
2021-11-24Fix a minor data race with _isInterruptedAlex Shabalin
2021-11-17Fix XDG_CONFIG_DIRS fallbackJan Tojnar
According to XDG Base Directory Specification, it should fall back to /etc/xdg when the env var is not present.
2021-11-16Ignore errors unsharing/restoring the mount namespaceEelco Dolstra
This prevents Nix from barfing when run in a container where it doesn't have the appropriate privileges.
2021-11-08Merge branch 'fix-writable-shell' of https://github.com/yorickvP/nixEelco Dolstra
2021-10-29Merge branch 'master' of https://github.com/alekswn/nixEelco Dolstra
2021-10-17Fix error detection in 'base64Decode()'Alexey Novikov
Fixed a bug in initialization of 'base64DecodeChars' variable. Currently decoder do not fail on invalid Base64 strings. Added test-case to verify the fix. Also have made 'base64DecodeChars' to be computed at compile time. And added a test case to encode/decode string with non-printable charactes.
2021-10-15Restore parent mount namespace in restoreProcessContextYorick van Pelt
This ensures any started processes can't write to /nix/store (except during builds). This partially reverts 01d07b1e, which happened because of #2646. The problem was only happening after nix downloads anything, causing me to suspect the download thread. The problem turns out to be: "A process can't join a new mount namespace if it is sharing filesystem-related attributes with another process", in this case this process is the curl thread. Ideally, we might kill it before spawning the shell process, but it's inside a static variable in the getFileTransfer() function. So instead, stop it from sharing FS state using unshare(). A strategy such as the one from #5057 (single-threaded chroot helper binary) is also very much on the table. Fixes #4337.
2021-10-13strcpy -> memcpyEelco Dolstra
Co-authored-by: Jörg Thalheim <Mic92@users.noreply.github.com>