Skip to content

Commit

Permalink
Split up util.{hh,cc}
Browse files Browse the repository at this point in the history
All OS and IO operations should be moved out, leaving only some misc
portable pure functions.

This is useful to avoid copious CPP when doing things like Windows and
Emscripten ports.

Newly exposed functions to break cycles:

 - `restoreSignals`
 - `updateWindowSize`
  • Loading branch information
Ericson2314 committed Oct 19, 2023
1 parent 9bc7b4f commit f1fc3e2
Show file tree
Hide file tree
Showing 111 changed files with 3,425 additions and 2,630 deletions.
2 changes: 1 addition & 1 deletion src/libcmd/editor-for.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "util.hh"
#include "editor-for.hh"
#include "environment-variables.hh"

namespace nix {

Expand Down
1 change: 0 additions & 1 deletion src/libcmd/installable-attr-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "globals.hh"
#include "installable-value.hh"
#include "outputs-spec.hh"
#include "util.hh"
#include "command.hh"
#include "attr-path.hh"
#include "common-eval-args.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "installable-attr-path.hh"
#include "installable-flake.hh"
#include "outputs-spec.hh"
#include "users.hh"
#include "util.hh"
#include "command.hh"
#include "attr-path.hh"
Expand Down
1 change: 0 additions & 1 deletion src/libcmd/installables.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
///@file

#include "util.hh"
#include "path.hh"
#include "outputs-spec.hh"
#include "derived-path.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libcmd/markdown.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "markdown.hh"
#include "util.hh"
#include "finally.hh"
#include "terminal.hh"

#include <sys/queue.h>
#include <lowdown.h>
Expand Down
3 changes: 3 additions & 0 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" {
#include "repl.hh"

#include "ansicolor.hh"
#include "signals.hh"
#include "shared.hh"
#include "eval.hh"
#include "eval-cache.hh"
Expand All @@ -36,6 +37,8 @@ extern "C" {
#include "globals.hh"
#include "flake/flake.hh"
#include "flake/lockfile.hh"
#include "users.hh"
#include "terminal.hh"
#include "editor-for.hh"
#include "finally.hh"
#include "markdown.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/eval-cache.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "users.hh"
#include "eval-cache.hh"
#include "sqlite.hh"
#include "eval.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/eval-settings.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "users.hh"
#include "globals.hh"
#include "profiles.hh"
#include "eval.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "function-trace.hh"
#include "profiles.hh"
#include "print.hh"
#include "signals.hh"

#include <algorithm>
#include <chrono>
Expand Down
3 changes: 2 additions & 1 deletion src/libexpr/flake/config.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "flake.hh"
#include "users.hh"
#include "globals.hh"
#include "fetch-settings.hh"
#include "flake.hh"

#include <nlohmann/json.hpp>

Expand Down
1 change: 1 addition & 0 deletions src/libexpr/flake/flake.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "terminal.hh"
#include "flake.hh"
#include "eval.hh"
#include "eval-settings.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <variant>

#include "util.hh"
#include "users.hh"

#include "nixexpr.hh"
#include "eval.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "path-references.hh"
#include "store-api.hh"
#include "util.hh"
#include "processes.hh"
#include "value-to-json.hh"
#include "value-to-xml.hh"
#include "primops.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/value-to-json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "eval-inline.hh"
#include "util.hh"
#include "store-api.hh"
#include "signals.hh"

#include <cstdlib>
#include <iomanip>
Expand Down
1 change: 1 addition & 0 deletions src/libexpr/value-to-xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "xml-writer.hh"
#include "eval-inline.hh"
#include "util.hh"
#include "signals.hh"

#include <cstdlib>

Expand Down
1 change: 1 addition & 0 deletions src/libexpr/value/context.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "util.hh"
#include "value/context.hh"

#include <optional>
Expand Down
1 change: 0 additions & 1 deletion src/libexpr/value/context.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
///@file

#include "util.hh"
#include "comparator.hh"
#include "derived-path.hh"
#include "variant-wrapper.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libfetchers/cache.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cache.hh"
#include "users.hh"
#include "sqlite.hh"
#include "sync.hh"
#include "store-api.hh"
Expand Down
1 change: 0 additions & 1 deletion src/libfetchers/fetch-settings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "types.hh"
#include "config.hh"
#include "util.hh"

#include <map>
#include <limits>
Expand Down
2 changes: 2 additions & 0 deletions src/libfetchers/git.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "fetchers.hh"
#include "users.hh"
#include "cache.hh"
#include "globals.hh"
#include "tarfile.hh"
#include "store-api.hh"
#include "url-parts.hh"
#include "pathlocks.hh"
#include "processes.hh"
#include "util.hh"
#include "git.hh"

Expand Down
1 change: 1 addition & 0 deletions src/libfetchers/input-accessor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "types.hh"
#include "archive.hh"
#include "canon-path.hh"
#include "file-system.hh"
#include "repair-flag.hh"

namespace nix {
Expand Down
2 changes: 2 additions & 0 deletions src/libfetchers/mercurial.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "fetchers.hh"
#include "processes.hh"
#include "users.hh"
#include "cache.hh"
#include "globals.hh"
#include "tarfile.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libfetchers/registry.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "registry.hh"
#include "fetchers.hh"
#include "users.hh"
#include "util.hh"
#include "globals.hh"
#include "store-api.hh"
Expand Down
2 changes: 2 additions & 0 deletions src/libmain/common-args.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "common-args.hh"
#include "globals.hh"
#include "logging.hh"
#include "loggers.hh"
#include "util.hh"

namespace nix {

Expand Down
2 changes: 2 additions & 0 deletions src/libmain/loggers.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "loggers.hh"
#include "environment-variables.hh"
#include "progress-bar.hh"
#include "util.hh"
#include "util.hh"

namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/libmain/progress-bar.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "progress-bar.hh"
#include "terminal.hh"
#include "util.hh"
#include "sync.hh"
#include "store-api.hh"
Expand Down
2 changes: 2 additions & 0 deletions src/libmain/shared.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "globals.hh"
#include "ambient-authority.hh"
#include "shared.hh"
#include "store-api.hh"
#include "gc-store.hh"
#include "util.hh"
#include "loggers.hh"
#include "progress-bar.hh"
#include "signals.hh"

#include <algorithm>
#include <cctype>
Expand Down
2 changes: 1 addition & 1 deletion src/libmain/shared.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
///@file

#include "util.hh"
#include "processes.hh"
#include "args.hh"
#include "common-args.hh"
#include "path.hh"
Expand Down
1 change: 1 addition & 0 deletions src/libstore/binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "nar-accessor.hh"
#include "thread-pool.hh"
#include "callback.hh"
#include "signals.hh"

#include <chrono>
#include <future>
Expand Down
81 changes: 81 additions & 0 deletions src/libstore/build/child.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "util.hh"
#include "users.hh"
#include "ambient-authority.hh"
#include "environment-variables.hh"
#include "file-system.hh"
#include "signals.hh"
#include "processes.hh"
#include "unix-domain-socket.hh"
#include "sync.hh"
#include "finally.hh"
#include "serialise.hh"
#include "cgroup.hh"
#include "child.hh"

#include <array>
#include <cctype>
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <future>
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>

#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>

#ifdef __APPLE__
#include <sys/syscall.h>
#include <mach-o/dyld.h>
#endif

#ifdef __linux__
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/mman.h>

#include <cmath>
#endif

namespace nix {

void commonChildInit()
{
logger = makeSimpleLogger();

const static std::string pathNullDevice = "/dev/null";
restoreProcessContext(false);

/* Put the child in a separate session (and thus a separate
process group) so that it has no controlling terminal (meaning
that e.g. ssh cannot open /dev/tty) and it doesn't receive
terminal signals. */
if (setsid() == -1)
throw SysError("creating a new session");

/* Dup stderr to stdout. */
if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
throw SysError("cannot dup stderr into stdout");

/* Reroute stdin to /dev/null. */
int fdDevNull = open(pathNullDevice.c_str(), O_RDWR);
if (fdDevNull == -1)
throw SysError("cannot open '%1%'", pathNullDevice);
if (dup2(fdDevNull, STDIN_FILENO) == -1)
throw SysError("cannot dup null device into stdin");
close(fdDevNull);
}

}
11 changes: 11 additions & 0 deletions src/libstore/build/child.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
///@file

namespace nix {

/**
* Common initialisation performed in child processes.
*/
void commonChildInit();

}
2 changes: 2 additions & 0 deletions src/libstore/build/hook-instance.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "globals.hh"
#include "hook-instance.hh"
#include "file-system.hh"
#include "child.hh"

namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/libstore/build/hook-instance.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "logging.hh"
#include "serialise.hh"
#include "processes.hh"

namespace nix {

Expand Down
5 changes: 4 additions & 1 deletion src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
#include "json-utils.hh"
#include "cgroup.hh"
#include "personality.hh"
#include "namespaces.hh"
#include "ambient-authority.hh"
#include "linux-namespace.hh"
#include "child.hh"
#include "unix-domain-socket.hh"

#include <regex>
#include <queue>
Expand Down
1 change: 1 addition & 0 deletions src/libstore/build/local-derivation-goal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "derivation-goal.hh"
#include "local-store.hh"
#include "processes.hh"

namespace nix {

Expand Down
1 change: 1 addition & 0 deletions src/libstore/build/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "drv-output-substitution-goal.hh"
#include "local-derivation-goal.hh"
#include "hook-instance.hh"
#include "signals.hh"

#include <poll.h>

Expand Down
1 change: 1 addition & 0 deletions src/libstore/crypto.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "crypto.hh"
#include "file-system.hh"
#include "util.hh"
#include "globals.hh"

Expand Down
1 change: 1 addition & 0 deletions src/libstore/derived-path-map.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "derived-path-map.hh"
#include "util.hh"

namespace nix {

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/derived-path.hh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#pragma once
///@file

#include "util.hh"
#include "path.hh"
#include "outputs-spec.hh"
#include "comparator.hh"
#include "config.hh"

#include <variant>

Expand Down
Loading

0 comments on commit f1fc3e2

Please sign in to comment.