Skip to content

make tests ignore root more robustly #42533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions base/libc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,36 +404,36 @@ srand(seed=floor(Int, time()) % Cuint) = ccall(:srand, Cvoid, (Cuint,), seed)

struct Cpasswd
username::Cstring
uid::Clong
gid::Clong
uid::Culong
gid::Culong
shell::Cstring
homedir::Cstring
gecos::Cstring
Cpasswd() = new(C_NULL, -1, -1, C_NULL, C_NULL, C_NULL)
Cpasswd() = new(C_NULL, typemax(Culong), typemax(Culong), C_NULL, C_NULL, C_NULL)
end
mutable struct Cgroup
groupname::Cstring # group name
gid::Clong # group ID
mem::Ptr{Cstring} # group members
Cgroup() = new(C_NULL, -1, C_NULL)
groupname::Cstring # group name
gid::Culong # group ID
mem::Ptr{Cstring} # group members
Cgroup() = new(C_NULL, typemax(Culong), C_NULL)
end
struct Passwd
username::String
uid::Int
gid::Int
uid::UInt
gid::UInt
shell::String
homedir::String
gecos::String
end
struct Group
groupname::String
gid::Int
gid::UInt
mem::Vector{String}
end

function getpwuid(uid::Unsigned, throw_error::Bool=true)
ref_pd = Ref(Cpasswd())
ret = ccall(:jl_os_get_passwd, Cint, (Ref{Cpasswd}, UInt), ref_pd, uid)
ret = ccall(:jl_os_get_passwd, Cint, (Ref{Cpasswd}, Culong), ref_pd, uid)
if ret != 0
throw_error && Base.uv_error("getpwuid", ret)
return
Expand All @@ -452,7 +452,7 @@ function getpwuid(uid::Unsigned, throw_error::Bool=true)
end
function getgrgid(gid::Unsigned, throw_error::Bool=true)
ref_gp = Ref(Cgroup())
ret = ccall(:jl_os_get_group, Cint, (Ref{Cgroup}, UInt), ref_gp, gid)
ret = ccall(:jl_os_get_group, Cint, (Ref{Cgroup}, Culong), ref_gp, gid)
if ret != 0
throw_error && Base.uv_error("getgrgid", ret)
return
Expand All @@ -475,6 +475,9 @@ function getgrgid(gid::Unsigned, throw_error::Bool=true)
return gp
end

getuid() = ccall(:jl_getuid, Culong, ())
geteuid() = ccall(:jl_geteuid, Culong, ())

# Include dlopen()/dlpath() code
include("libdl.jl")
using .Libdl
Expand Down
24 changes: 21 additions & 3 deletions src/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,25 @@ JL_DLLEXPORT double jl_stat_ctime(char *statbuf)
return (double)s->st_ctim.tv_sec + (double)s->st_ctim.tv_nsec * 1e-9;
}

JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, size_t uid)
JL_DLLEXPORT unsigned long jl_getuid(void)
{
#ifdef _OS_WINDOWS_
return -1;
#else
return getuid();
#endif
}

JL_DLLEXPORT unsigned long jl_geteuid(void)
{
#ifdef _OS_WINDOWS_
return -1;
#else
return geteuid();
#endif
}

JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, unsigned long uid)
{
#ifdef _OS_WINDOWS_
return UV_ENOTSUP;
Expand Down Expand Up @@ -342,11 +360,11 @@ JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, size_t uid)

typedef struct jl_group_s {
char* groupname;
long gid;
unsigned long gid;
char** members;
} jl_group_t;

JL_DLLEXPORT int jl_os_get_group(jl_group_t *grp, size_t gid)
JL_DLLEXPORT int jl_os_get_group(jl_group_t *grp, unsigned long gid)
{
#ifdef _OS_WINDOWS_
return UV_ENOTSUP;
Expand Down
5 changes: 4 additions & 1 deletion test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,10 @@ end

if !Sys.iswindows()
# chown will give an error if the user does not have permissions to change files
if get(ENV, "USER", "") == "root" || get(ENV, "HOME", "") == "/root"
uid = Libc.geteuid()
@test stat(file).uid == uid
@test uid == Libc.getuid()
if uid == 0 # root user
chown(file, -2, -1) # Change the file owner to nobody
@test stat(file).uid != 0
chown(file, 0, -2) # Change the file group to nogroup (and owner back to root)
Expand Down
4 changes: 2 additions & 2 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ rm(f)
io = Base.Filesystem.open(f, Base.Filesystem.JL_O_WRONLY | Base.Filesystem.JL_O_CREAT | Base.Filesystem.JL_O_EXCL, 0o000)
@test write(io, "abc") == 3
close(io)
if !Sys.iswindows() && get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
if !Sys.iswindows() && Libc.geteuid() != 0 # root user
# msvcrt _wchmod documentation states that all files are readable,
# so we don't test that it correctly set the umask on windows
@test_throws SystemError open(f)
Expand Down Expand Up @@ -511,7 +511,7 @@ close(f1)
close(f2)
@test eof(f1)
@test_throws Base.IOError eof(f2)
if get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root"
if Libc.geteuid() != 0 # root user
@test_throws SystemError open(f, "r+")
@test_throws Base.IOError Base.Filesystem.open(f, Base.Filesystem.JL_O_RDWR)
else
Expand Down