diff --git a/src/libexpr-tests/main.cc b/src/libexpr-tests/main.cc index 66afc2272..52cca53c4 100644 --- a/src/libexpr-tests/main.cc +++ b/src/libexpr-tests/main.cc @@ -14,7 +14,7 @@ int main (int argc, char **argv) { // Disable build hook. We won't be testing remote builds in these unit tests. If we do, fix the above build hook. settings.buildHook = {}; - #if __linux__ // should match the conditional around sandboxBuildDir declaration. + #ifdef __linux__ // should match the conditional around sandboxBuildDir declaration. // When building and testing nix within the host's Nix sandbox, our store dir will be located in the host's sandboxBuildDir, e.g.: // Host diff --git a/src/libstore/filetransfer.cc b/src/libstore/filetransfer.cc index b82fbb09c..6de458823 100644 --- a/src/libstore/filetransfer.cc +++ b/src/libstore/filetransfer.cc @@ -13,7 +13,7 @@ #include #endif -#if __linux__ +#ifdef __linux__ # include "nix/util/namespaces.hh" #endif @@ -622,7 +622,7 @@ struct curlFileTransfer : public FileTransfer }); #endif - #if __linux__ + #ifdef __linux__ try { tryUnshareFilesystem(); } catch (nix::Error & e) { diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index 5e56be7f3..db91e5213 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -335,7 +335,7 @@ static std::string quoteRegexChars(const std::string & raw) return std::regex_replace(raw, specialRegex, R"(\$&)"); } -#if __linux__ +#ifdef __linux__ static void readFileRoots(const std::filesystem::path & path, UncheckedRoots & roots) { try { @@ -427,7 +427,7 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor) } #endif -#if __linux__ +#ifdef __linux__ readFileRoots("/proc/sys/kernel/modprobe", unchecked); readFileRoots("/proc/sys/kernel/fbsplash", unchecked); readFileRoots("/proc/sys/kernel/poweroff_cmd", unchecked); diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 6b93e34bb..1df0a846e 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -181,11 +181,11 @@ StringSet Settings::getDefaultSystemFeatures() actually require anything special on the machines. */ StringSet features{"nixos-test", "benchmark", "big-parallel"}; - #if __linux__ + #ifdef __linux__ features.insert("uid-range"); #endif - #if __linux__ + #ifdef __linux__ if (access("/dev/kvm", R_OK | W_OK) == 0) features.insert("kvm"); #endif @@ -205,7 +205,7 @@ StringSet Settings::getDefaultExtraPlatforms() if (std::string{NIX_LOCAL_SYSTEM} == "x86_64-linux" && !isWSL1()) extraPlatforms.insert("i686-linux"); -#if __linux__ +#ifdef __linux__ StringSet levels = computeLevels(); for (auto iter = levels.begin(); iter != levels.end(); ++iter) extraPlatforms.insert(*iter + "-linux"); @@ -224,7 +224,7 @@ StringSet Settings::getDefaultExtraPlatforms() bool Settings::isWSL1() { -#if __linux__ +#ifdef __linux__ struct utsname utsbuf; uname(&utsbuf); // WSL1 uses -Microsoft suffix diff --git a/src/libstore/include/nix/store/globals.hh b/src/libstore/include/nix/store/globals.hh index 38757bcd4..82211d8dc 100644 --- a/src/libstore/include/nix/store/globals.hh +++ b/src/libstore/include/nix/store/globals.hh @@ -34,7 +34,7 @@ struct MaxBuildJobsSetting : public BaseSetting }; const uint32_t maxIdsPerBuild = - #if __linux__ + #ifdef __linux__ 1 << 16 #else 1 @@ -467,7 +467,7 @@ public: )", {}, true, Xp::AutoAllocateUids}; Setting startId{this, - #if __linux__ + #ifdef __linux__ 0x34000000, #else 56930, @@ -476,7 +476,7 @@ public: "The first UID and GID to use for dynamic ID allocation."}; Setting uidCount{this, - #if __linux__ + #ifdef __linux__ maxIdsPerBuild * 128, #else 128, @@ -484,7 +484,7 @@ public: "id-count", "The number of UIDs/GIDs to use for dynamic ID allocation."}; - #if __linux__ + #ifdef __linux__ Setting useCgroups{ this, false, "use-cgroups", R"( @@ -596,7 +596,7 @@ public: Setting sandboxMode{ this, - #if __linux__ + #ifdef __linux__ smEnabled #else smDisabled @@ -671,7 +671,7 @@ public: )"}; #endif -#if __linux__ +#ifdef __linux__ Setting sandboxShmSize{ this, "50%", "sandbox-dev-shm-size", R"( @@ -1066,7 +1066,7 @@ public: // Don't document the machine-specific default value false}; -#if __linux__ +#ifdef __linux__ Setting filterSyscalls{ this, true, "filter-syscalls", R"( diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index b26fdeed8..fff0b35bf 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -38,7 +38,7 @@ # include #endif -#if __linux__ +#ifdef __linux__ # include # include # include @@ -571,7 +571,7 @@ void LocalStore::upgradeDBSchema(State & state) bind mount. So make the Nix store writable for this process. */ void LocalStore::makeStoreWritable() { -#if __linux__ +#ifdef __linux__ if (!isRootUser()) return; /* Check if /nix/store is on a read-only mount. */ struct statvfs stat; diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 6721a6f2d..9e606d0ab 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -1328,7 +1328,7 @@ ref openStore(StoreReference && storeURI) return std::make_shared(params); else if (pathExists(settings.nixDaemonSocketFile)) return std::make_shared(params); - #if __linux__ + #ifdef __linux__ else if (!pathExists(stateDir) && params.empty() && !isRootUser() diff --git a/src/libstore/unix/build/local-derivation-goal.cc b/src/libstore/unix/build/local-derivation-goal.cc index 915b8b1d8..185c98bb6 100644 --- a/src/libstore/unix/build/local-derivation-goal.cc +++ b/src/libstore/unix/build/local-derivation-goal.cc @@ -42,7 +42,7 @@ #endif /* Includes required for chroot support. */ -#if __linux__ +#ifdef __linux__ # include "nix/store/fchmodat2-compat.hh" # include # include @@ -418,7 +418,7 @@ LocalDerivationGoal::~LocalDerivationGoal() inline bool LocalDerivationGoal::needsHashRewrite() { -#if __linux__ +#ifdef __linux__ return !useChroot; #else /* Darwin requires hash rewriting even when sandboxing is enabled. */ @@ -459,7 +459,7 @@ void LocalDerivationGoal::killChild() void LocalDerivationGoal::killSandbox(bool getStats) { if (cgroup) { - #if __linux__ + #ifdef __linux__ auto stats = destroyCgroup(*cgroup); if (getStats) { buildResult.cpuUser = stats.cpuUser; @@ -513,14 +513,14 @@ Goal::Co LocalDerivationGoal::tryLocalBuild() auto & localStore = getLocalStore(); if (localStore.storeDir != localStore.realStoreDir.get()) { - #if __linux__ + #ifdef __linux__ useChroot = true; #else throw Error("building using a diverted store is not supported on this platform"); #endif } - #if __linux__ + #ifdef __linux__ if (useChroot) { if (!mountAndPidNamespacesSupported()) { if (!settings.sandboxFallback) @@ -747,7 +747,7 @@ bool LocalDerivationGoal::cleanupDecideWhetherDiskFull() } -#if __linux__ +#ifdef __linux__ static void doBind(const Path & source, const Path & target, bool optional = false) { debug("bind mounting '%1%' to '%2%'", source, target); @@ -820,12 +820,12 @@ static void handleChildException(bool sendException) void LocalDerivationGoal::startBuilder() { if ((buildUser && buildUser->getUIDCount() != 1) - #if __linux__ + #ifdef __linux__ || settings.useCgroups #endif ) { - #if __linux__ + #ifdef __linux__ experimentalFeatureSettings.require(Xp::Cgroups); /* If we're running from the daemon, then this will return the @@ -1071,7 +1071,7 @@ void LocalDerivationGoal::startBuilder() pathsInChroot[i] = {i, true}; } -#if __linux__ +#ifdef __linux__ /* Create a temporary directory in which we set up the chroot environment using bind-mounts. We put it in the Nix store so that the build outputs can be moved efficiently from the @@ -1285,7 +1285,7 @@ void LocalDerivationGoal::startBuilder() /* Fork a child to build the package. */ -#if __linux__ +#ifdef __linux__ if (useChroot) { /* Set up private namespaces for the build: @@ -1482,7 +1482,7 @@ void LocalDerivationGoal::initTmpDir() { /* In a sandbox, for determinism, always use the same temporary directory. */ -#if __linux__ +#ifdef __linux__ tmpDirInSandbox = useChroot ? settings.sandboxBuildDir : tmpDir; #else tmpDirInSandbox = tmpDir; @@ -1728,7 +1728,7 @@ void LocalDerivationGoal::addDependency(const StorePath & path) debug("materialising '%s' in the sandbox", worker.store.printStorePath(path)); - #if __linux__ + #ifdef __linux__ Path source = worker.store.Store::toRealPath(path); Path target = chrootRootDir + worker.store.printStorePath(path); @@ -1778,7 +1778,7 @@ void LocalDerivationGoal::chownToBuilder(const Path & path) void setupSeccomp() { -#if __linux__ +#ifdef __linux__ if (!settings.filterSyscalls) return; #if HAVE_SECCOMP scmp_filter_ctx ctx; @@ -1898,7 +1898,7 @@ void LocalDerivationGoal::runChild() } catch (SystemError &) { } } -#if __linux__ +#ifdef __linux__ if (useChroot) { userNamespaceSync.writeSide = -1; @@ -2132,7 +2132,7 @@ void LocalDerivationGoal::runChild() /* Close all other file descriptors. */ unix::closeExtraFDs(); -#if __linux__ +#ifdef __linux__ linux::setPersonality(drv->platform); #endif diff --git a/src/libstore/unix/user-lock.cc b/src/libstore/unix/user-lock.cc index 770b00e2d..eb0bac887 100644 --- a/src/libstore/unix/user-lock.cc +++ b/src/libstore/unix/user-lock.cc @@ -10,7 +10,7 @@ namespace nix { -#if __linux__ +#ifdef __linux__ static std::vector get_group_list(const char *username, gid_t group_id) { @@ -94,7 +94,7 @@ struct SimpleUserLock : UserLock if (lock->uid == getuid() || lock->uid == geteuid()) throw Error("the Nix user should not be a member of '%s'", settings.buildUsersGroup); - #if __linux__ + #ifdef __linux__ /* Get the list of supplementary groups of this user. This is * usually either empty or contains a group such as "kvm". */ @@ -193,7 +193,7 @@ std::unique_ptr acquireUserLock(uid_t nrIds, bool useUserNamespace) bool useBuildUsers() { - #if __linux__ + #ifdef __linux__ static bool b = (settings.buildUsersGroup != "" || settings.autoAllocateUids) && isRootUser(); return b; #elif __APPLE__ diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index 4103c0515..8aef47146 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -13,7 +13,7 @@ # include #endif -#if __linux__ +#ifdef __linux__ # include # include "nix/util/cgroup.hh" # include "nix/util/namespaces.hh" @@ -23,7 +23,7 @@ namespace nix { unsigned int getMaxCPU() { - #if __linux__ + #ifdef __linux__ try { auto cgroupFS = getCgroupFS(); if (!cgroupFS) return 0; @@ -82,7 +82,7 @@ void restoreProcessContext(bool restoreMounts) unix::restoreSignals(); #endif if (restoreMounts) { - #if __linux__ + #ifdef __linux__ restoreMountNamespace(); #endif } @@ -106,7 +106,7 @@ std::optional getSelfExe() { static auto cached = []() -> std::optional { - #if __linux__ || __GNU__ + #if defined(__linux__) || defined(__GNU__) return readLink("/proc/self/exe"); #elif __APPLE__ char buf[1024]; diff --git a/src/libutil/file-descriptor.cc b/src/libutil/file-descriptor.cc index 4fc0f06cd..9e0827442 100644 --- a/src/libutil/file-descriptor.cc +++ b/src/libutil/file-descriptor.cc @@ -113,7 +113,7 @@ void AutoCloseFD::fsync() const void AutoCloseFD::startFsync() const { -#if __linux__ +#ifdef __linux__ if (fd != -1) { /* Ignore failure, since fsync must be run later anyway. This is just a performance optimization. */ ::sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE); diff --git a/src/libutil/unix/file-descriptor.cc b/src/libutil/unix/file-descriptor.cc index 6ce307252..73ee49982 100644 --- a/src/libutil/unix/file-descriptor.cc +++ b/src/libutil/unix/file-descriptor.cc @@ -163,7 +163,7 @@ void Pipe::create() ////////////////////////////////////////////////////////////////////// -#if __linux__ || __FreeBSD__ +#if defined(__linux__) || defined(__FreeBSD__) static int unix_close_range(unsigned int first, unsigned int last, int flags) { #if !HAVE_CLOSE_RANGE @@ -179,7 +179,7 @@ void unix::closeExtraFDs() constexpr int MAX_KEPT_FD = 2; static_assert(std::max({STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}) == MAX_KEPT_FD); -#if __linux__ || __FreeBSD__ +#if defined(__linux__) || defined(__FreeBSD__) // first try to close_range everything we don't care about. if this // returns an error with these parameters we're running on a kernel // that does not implement close_range (i.e. pre 5.9) and fall back @@ -189,7 +189,7 @@ void unix::closeExtraFDs() } #endif -#if __linux__ +#ifdef __linux__ try { for (auto & s : std::filesystem::directory_iterator{"/proc/self/fd"}) { checkInterrupt(); diff --git a/src/libutil/unix/processes.cc b/src/libutil/unix/processes.cc index 4df0a7777..198243c20 100644 --- a/src/libutil/unix/processes.cc +++ b/src/libutil/unix/processes.cc @@ -190,7 +190,7 @@ static pid_t doFork(bool allowVfork, ChildWrapperFunction & fun) } -#if __linux__ +#ifdef __linux__ static int childEntry(void * arg) { auto & fun = *reinterpret_cast(arg); @@ -213,7 +213,7 @@ pid_t startProcess(std::function fun, const ProcessOptions & options) logger = makeSimpleLogger(); } try { -#if __linux__ +#ifdef __linux__ if (options.dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1) throw SysError("setting death signal"); #endif diff --git a/src/libutil/unix/signals.cc b/src/libutil/unix/signals.cc index f1cb28527..665b9b096 100644 --- a/src/libutil/unix/signals.cc +++ b/src/libutil/unix/signals.cc @@ -105,7 +105,7 @@ void unix::setChildSignalMask(sigset_t * sigs) { assert(sigs); // C style function, but think of sigs as a reference -#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE +#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE) || (defined(_POSIX_SOURCE) && _POSIX_SOURCE) sigemptyset(&savedSignalMask); // There's no "assign" or "copy" function, so we rely on (math) idempotence // of the or operator: a or a = a. diff --git a/src/nix/main.cc b/src/nix/main.cc index 6470213a2..a2c9dcf68 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -36,7 +36,7 @@ # include #endif -#if __linux__ +#ifdef __linux__ # include "nix/util/namespaces.hh" #endif @@ -384,7 +384,7 @@ void mainWrapped(int argc, char * * argv) "__build-remote", }); - #if __linux__ + #ifdef __linux__ if (isRootUser()) { try { saveMountNamespace(); diff --git a/src/nix/run.cc b/src/nix/run.cc index 64eab3ff3..146ae9ec9 100644 --- a/src/nix/run.cc +++ b/src/nix/run.cc @@ -12,7 +12,7 @@ #include "nix/expr/eval.hh" #include -#if __linux__ +#ifdef __linux__ # include # include "nix/store/personality.hh" #endif @@ -59,7 +59,7 @@ void execProgramInStore(ref store, throw SysError("could not execute chroot helper"); } -#if __linux__ +#ifdef __linux__ if (system) linux::setPersonality(*system); #endif @@ -153,7 +153,7 @@ void chrootHelper(int argc, char * * argv) while (p < argc) args.push_back(argv[p++]); -#if __linux__ +#ifdef __linux__ uid_t uid = getuid(); uid_t gid = getgid(); @@ -212,7 +212,7 @@ void chrootHelper(int argc, char * * argv) writeFile(fs::path{"/proc/self/uid_map"}, fmt("%d %d %d", uid, uid, 1)); writeFile(fs::path{"/proc/self/gid_map"}, fmt("%d %d %d", gid, gid, 1)); -#if __linux__ +#ifdef __linux__ if (system != "") linux::setPersonality(system); #endif diff --git a/src/nix/unix/daemon.cc b/src/nix/unix/daemon.cc index 1acf2bd5b..607a7bb01 100644 --- a/src/nix/unix/daemon.cc +++ b/src/nix/unix/daemon.cc @@ -34,7 +34,7 @@ #include #include -#if __linux__ +#ifdef __linux__ #include "nix/util/cgroup.hh" #endif @@ -317,7 +317,7 @@ static void daemonLoop(std::optional forceTrustClientOpt) // Get rid of children automatically; don't let them become zombies. setSigChldAction(true); - #if __linux__ + #ifdef __linux__ if (settings.useCgroups) { experimentalFeatureSettings.require(Xp::Cgroups);