Skip to content

Commit

Permalink
Make some global variables constant
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Jan 28, 2022
1 parent ed87f07 commit b9384cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
11 changes: 5 additions & 6 deletions src/Sandbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ include("Docker.jl")
# Load the UserNamespace executor
include("UserNamespaces.jl")

all_executors = Type{<:SandboxExecutor}[
const all_executors = Type{<:SandboxExecutor}[
# We always prefer the UserNamespaces executor, if we can use it,
# and the unprivileged one most of all. Only after that do we try `docker`.
UnprivilegedUserNamespacesExecutor,
Expand Down Expand Up @@ -89,16 +89,15 @@ function select_executor(verbose::Bool)
error("Could not find any available executors for $(triplet(HostPlatform()))!")
end

_preferred_executor = nothing
const _preferred_executor = Ref{Union{Nothing, (x->Type{x}).(all_executors)...}}(nothing)
const _preferred_executor_lock = ReentrantLock()
function preferred_executor(;verbose::Bool = false)
lock(_preferred_executor_lock) do
# If we've already asked this question, return the old answer
global _preferred_executor
if _preferred_executor === nothing
_preferred_executor = select_executor(verbose)
if _preferred_executor[] === nothing
_preferred_executor[] = select_executor(verbose)
end
return _preferred_executor
return _preferred_executor[]
end
end

Expand Down
19 changes: 9 additions & 10 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,27 +198,26 @@ Wrapper around libc's `getuid()` function
"""
getgid() = ccall(:getgid, Cint, ())

_sudo_cmd = nothing
const _sudo_cmd = Ref{Union{Vector{String}, Nothing}}(nothing)
function sudo_cmd()
global _sudo_cmd

# Use cached value if we've already run this
if _sudo_cmd !== nothing
return _sudo_cmd
if _sudo_cmd[] !== nothing
return _sudo_cmd[]
end

if getuid() == 0
_sudo_cmd[] = if getuid() == 0
# If we're already root, don't use any kind of sudo program
_sudo_cmd = String[]
String[]
elseif Sys.which("sudo") !== nothing success(`sudo -V`)
# If `sudo` is available, use that
_sudo_cmd = ["sudo"]
["sudo"]
elseif Sys.which("su") !== nothing
# Fall back to `su` if all else fails
_sudo_cmd = ["su", "root", "-c"]
["su", "root", "-c"]
else
@warn("No known sudo-like wrappers!")
_sudo_cmd = String[]
String[]
end
return _sudo_cmd
return _sudo_cmd[]
end

0 comments on commit b9384cf

Please sign in to comment.