Skip to content

Commit

Permalink
Use fnctl on non-linux
Browse files Browse the repository at this point in the history
  • Loading branch information
jkarni committed Jun 18, 2024
1 parent d56ebf9 commit a4abf3d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
16 changes: 16 additions & 0 deletions cbits/cradle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifdef __linux__
#define _GNU_SOURCE
#endif
#include <fcntl.h>
#include <unistd.h>

int cloexec_pipe(int pipefd[2]) {
#ifdef __linux__
int r = pipe2(pipefd, O_CLOEXEC);
#else
int r = pipe(pipefd);
r = fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
r = fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
#endif
return r;
}
6 changes: 6 additions & 0 deletions cradle.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ library
hs-source-dirs:
src
ghc-options: -Wall -Wno-name-shadowing -threaded
c-sources:
cbits/cradle.c
build-depends:
base >=4.5 && <5
, bytestring
Expand All @@ -49,6 +51,8 @@ test-suite readme
other-modules:
Paths_cradle
ghc-options: -Wall -Wno-name-shadowing -threaded -pgmL markdown-unlit
c-sources:
cbits/cradle.c
build-depends:
base >=4.5 && <5
, bytestring
Expand Down Expand Up @@ -76,6 +80,8 @@ test-suite spec
test
src
ghc-options: -Wall -Wno-name-shadowing -threaded -fdefer-typed-holes
c-sources:
cbits/cradle.c
build-depends:
base >=4.5 && <5
, bytestring
Expand Down
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
setupEnvironment = haskellPackages: ''
export PYTHON_BIN_PATH=${pkgs.python3}/bin/python3
export NIX_GHC=${haskellPackages.ghc.withPackages (p: (mkCradle haskellPackages).buildInputs)}/bin/ghc
export NIX_GHCPKG=${haskellPackages.ghc.withPackages (p: (mkCradle haskellPackages).buildInputs)}/bin/ghc-pkg
export NIX_GHC_LIBDIR=$($NIX_GHC --print-libdir)
'';
src = ./.;
mkCradle = haskellPackages:
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test: hpack
just doctest

doctest:
doctest -isrc src/*.hs
cabal repl --with-ghc=doctest

watch *args="": hpack
ghcid --command "cabal repl test:spec" --test ':main {{ args }}' --warnings
Expand Down
3 changes: 3 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ ghc-options:
- -Wno-name-shadowing
- -threaded

c-sources:
cbits/cradle.c

tests:
spec:
main: Spec.hs
Expand Down
10 changes: 3 additions & 7 deletions src/Cradle/SafeUnix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import System.IO (Handle)
import System.Posix.IO (fdToHandle)
import System.Posix.Types (Fd (..))

-- O_CLOEXEC from fcntl.h. See `man 2 open` and `man 2 pipe`.
oCLOEXEC :: CInt
oCLOEXEC = 524288

safeCreatePipe :: IO (Handle, Handle)
safeCreatePipe = do
(readfd, writefd) <- safeCreatePipeFd
Expand All @@ -20,10 +16,10 @@ safeCreatePipe = do
safeCreatePipeFd :: IO (Fd, Fd)
safeCreatePipeFd =
allocaArray 2 $ \p_fd -> do
throwErrnoIfMinus1_ "safeCreatePipe" (c_pipe2 p_fd oCLOEXEC)
throwErrnoIfMinus1_ "safeCreatePipe" (c_cloexec_pipe p_fd)
rfd <- Fd <$> peekElemOff p_fd 0
wfd <- Fd <$> peekElemOff p_fd 1
return (rfd, wfd)

foreign import ccall unsafe "pipe2"
c_pipe2 :: Ptr CInt -> CInt -> IO CInt
foreign import ccall unsafe "cloexec_pipe"
c_cloexec_pipe :: Ptr CInt -> IO CInt

0 comments on commit a4abf3d

Please sign in to comment.