Skip to content
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

Add Libc.mkfifo #34587

Merged
merged 19 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ New library functions
provide one-argument method that returns a closure. The old methods of `merge` and
`merge!` are still available for backward compatibility ([#34296]).
* The new `isdisjoint` function indicates whether two collections are disjoint ([#34427]).
* The new `Libc.mkfifo` function wraps `mkfifo` C function on Unix platforms ([#34587]).
* Add function `ismutable` and deprecate `isimmutable` to check whether something is mutable.([#34652])
* `include` now accepts an optional `mapexpr` first argument to transform the parsed
expressions before they are evaluated ([#34595]).
Expand Down
26 changes: 26 additions & 0 deletions base/libc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import Core.Intrinsics: bitcast

export FILE, TmStruct, strftime, strptime, getpid, gethostname, free, malloc, calloc, realloc,
errno, strerror, flush_cstdio, systemsleep, time, transcode
if Sys.isunix()
tkf marked this conversation as resolved.
Show resolved Hide resolved
export mkfifo
end
if Sys.iswindows()
export GetLastError, FormatMessage
end
Expand Down Expand Up @@ -400,4 +403,27 @@ Interface to the C `srand(seed)` function.
"""
srand(seed=floor(Int, time()) % Cuint) = ccall(:srand, Cvoid, (Cuint,), seed)

"""
mkfifo(path::AbstractString, [mode::Integer]) -> path

Make a FIFO special file (a named pipe) at `path`. Return `path` as-is on success.
tkf marked this conversation as resolved.
Show resolved Hide resolved

!!! compat "Julia 1.5"
`mkfifo` requires at least Julia 1.5.
"""
function mkfifo end

if Sys.isunix()
tkf marked this conversation as resolved.
Show resolved Hide resolved
function mkfifo(
path::AbstractString,
mode::Integer = Base.S_IRUSR | Base.S_IWUSR | Base.S_IRGRP | Base.S_IWGRP |
Base.S_IROTH | Base.S_IWOTH,
)
# Default `mode` is compatible with `mkfifo` CLI in coreutils.
ret = ccall(:mkfifo, Cint, (Cstring, Base.Cmode_t), path, mode)
systemerror("mkfifo", ret == -1)
return path
end
end

end # module
1 change: 1 addition & 0 deletions doc/src/base/libc.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Base.Libc.strptime
Base.Libc.TmStruct
Base.Libc.flush_cstdio
Base.Libc.systemsleep
Base.Libc.mkfifo
```
17 changes: 17 additions & 0 deletions test/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1510,3 +1510,20 @@ end
end
end
end

if Sys.isunix()
@testset "mkfifo" begin
mktempdir() do dir
path = Libc.mkfifo(joinpath(dir, "fifo"))
@sync begin
@async write(path, "hello")
cat_exec = `$(Base.julia_cmd()) --startup-file=no -e "write(stdout, read(ARGS[1]))"`
@test read(`$cat_exec $path`, String) == "hello"
end

existing_file = joinpath(dir, "existing")
write(existing_file, "")
@test_throws SystemError Libc.mkfifo(existing_file)
end
end
end