1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 {-# OPTIONS_GHC -fno-warn-unused-binds #-}
3 {-# OPTIONS_HADDOCK hide #-}
5 -----------------------------------------------------------------------------
7 -- Module : System.Posix.Internals
8 -- Copyright : (c) The University of Glasgow, 1992-2002
9 -- License : see libraries/base/LICENSE
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (requires POSIX)
15 -- POSIX support layer for the standard libraries.
16 -- This library is built on *every* platform, including Win32.
18 -- Non-posix compliant in order to support the following features:
19 -- * S_ISSOCK (no sockets in POSIX)
21 -----------------------------------------------------------------------------
24 module System.Posix.Internals where
27 #define HTYPE_TCFLAG_T
29 # include "HsBaseConfig.h"
32 #if ! (defined(mingw32_HOST_OS) || defined(__MINGW32__))
35 import System.Posix.Types
43 #if !defined(HTYPE_TCFLAG_T)
44 import System.IO.Error
47 #if __GLASGOW_HASKELL__
53 import Hugs.Prelude (IOException(..), IOErrorType(..))
54 import Hugs.IO (IOMode(..))
57 import Control.Exception
62 {-# CFILES cbits/PrelIOUtils.c cbits/dirUtils.c cbits/consUtils.c #-}
65 -- ---------------------------------------------------------------------------
83 #ifndef __GLASGOW_HASKELL__
87 -- ---------------------------------------------------------------------------
88 -- stat()-related stuff
90 fdFileSize :: FD -> IO Integer
92 allocaBytes sizeof_stat $ \ p_stat -> do
93 throwErrnoIfMinus1Retry "fileSize" $
95 c_mode <- st_mode p_stat :: IO CMode
96 if not (s_isreg c_mode)
99 c_size <- st_size p_stat
100 return (fromIntegral c_size)
102 data FDType = Directory | Stream | RegularFile | RawDevice
105 fileType :: FilePath -> IO FDType
107 allocaBytes sizeof_stat $ \ p_stat -> do
108 withCString file $ \p_file -> do
109 throwErrnoIfMinus1Retry "fileType" $
113 -- NOTE: On Win32 platforms, this will only work with file descriptors
114 -- referring to file handles. i.e., it'll fail for socket FDs.
115 fdStat :: FD -> IO (FDType, CDev, CIno)
117 allocaBytes sizeof_stat $ \ p_stat -> do
118 throwErrnoIfMinus1Retry "fdType" $
120 ty <- statGetType p_stat
125 fdType :: FD -> IO FDType
126 fdType fd = do (ty,_,_) <- fdStat fd; return ty
128 statGetType :: Ptr CStat -> IO FDType
129 statGetType p_stat = do
130 c_mode <- st_mode p_stat :: IO CMode
132 _ | s_isdir c_mode -> return Directory
133 | s_isfifo c_mode || s_issock c_mode || s_ischr c_mode
135 | s_isreg c_mode -> return RegularFile
136 -- Q: map char devices to RawDevice too?
137 | s_isblk c_mode -> return RawDevice
138 | otherwise -> ioError ioe_unknownfiletype
140 ioe_unknownfiletype :: IOException
142 ioe_unknownfiletype = IOError Nothing UnsupportedOperation "fdType"
144 # if __GLASGOW_HASKELL__
149 ioe_unknownfiletype = UserError "fdType" "unknown file type"
152 #if __GLASGOW_HASKELL__ && (defined(mingw32_HOST_OS) || defined(__MINGW32__))
153 closeFd :: Bool -> CInt -> IO CInt
155 | isStream = c_closesocket fd
156 | otherwise = c_close fd
158 foreign import stdcall unsafe "HsBase.h closesocket"
159 c_closesocket :: CInt -> IO CInt
162 fdGetMode :: FD -> IO IOMode
163 #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
165 -- We don't have a way of finding out which flags are set on FDs
166 -- on Windows, so make a handle that thinks that anything goes.
170 flags <- throwErrnoIfMinus1Retry "fdGetMode"
171 (c_fcntl_read fd const_f_getfl)
174 wH = (flags .&. o_WRONLY) /= 0
175 aH = (flags .&. o_APPEND) /= 0
176 rwH = (flags .&. o_RDWR) /= 0
179 | wH && aH = AppendMode
181 | rwH = ReadWriteMode
182 | otherwise = ReadMode
186 -- ---------------------------------------------------------------------------
187 -- Terminal-related stuff
189 fdIsTTY :: FD -> IO Bool
190 fdIsTTY fd = c_isatty fd >>= return.toBool
192 #if defined(HTYPE_TCFLAG_T)
194 setEcho :: FD -> Bool -> IO ()
196 tcSetAttr fd $ \ p_tios -> do
197 lflag <- c_lflag p_tios :: IO CTcflag
199 | on = lflag .|. fromIntegral const_echo
200 | otherwise = lflag .&. complement (fromIntegral const_echo)
201 poke_c_lflag p_tios (new_lflag :: CTcflag)
203 getEcho :: FD -> IO Bool
205 tcSetAttr fd $ \ p_tios -> do
206 lflag <- c_lflag p_tios :: IO CTcflag
207 return ((lflag .&. fromIntegral const_echo) /= 0)
209 setCooked :: FD -> Bool -> IO ()
210 setCooked fd cooked =
211 tcSetAttr fd $ \ p_tios -> do
213 -- turn on/off ICANON
214 lflag <- c_lflag p_tios :: IO CTcflag
215 let new_lflag | cooked = lflag .|. (fromIntegral const_icanon)
216 | otherwise = lflag .&. complement (fromIntegral const_icanon)
217 poke_c_lflag p_tios (new_lflag :: CTcflag)
219 -- set VMIN & VTIME to 1/0 respectively
220 when (not cooked) $ do
221 c_cc <- ptr_c_cc p_tios
222 let vmin = (c_cc `plusPtr` (fromIntegral const_vmin)) :: Ptr Word8
223 vtime = (c_cc `plusPtr` (fromIntegral const_vtime)) :: Ptr Word8
227 tcSetAttr :: FD -> (Ptr CTermios -> IO a) -> IO a
228 tcSetAttr fd fun = do
229 allocaBytes sizeof_termios $ \p_tios -> do
230 throwErrnoIfMinus1Retry "tcSetAttr"
231 (c_tcgetattr fd p_tios)
233 #ifdef __GLASGOW_HASKELL__
234 -- Save a copy of termios, if this is a standard file descriptor.
235 -- These terminal settings are restored in hs_exit().
237 p <- get_saved_termios fd
238 when (p == nullPtr) $ do
239 saved_tios <- mallocBytes sizeof_termios
240 copyBytes saved_tios p_tios sizeof_termios
241 set_saved_termios fd saved_tios
244 -- tcsetattr() when invoked by a background process causes the process
245 -- to be sent SIGTTOU regardless of whether the process has TOSTOP set
246 -- in its terminal flags (try it...). This function provides a
247 -- wrapper which temporarily blocks SIGTTOU around the call, making it
249 allocaBytes sizeof_sigset_t $ \ p_sigset -> do
250 allocaBytes sizeof_sigset_t $ \ p_old_sigset -> do
251 c_sigemptyset p_sigset
252 c_sigaddset p_sigset const_sigttou
253 c_sigprocmask const_sig_block p_sigset p_old_sigset
254 r <- fun p_tios -- do the business
255 throwErrnoIfMinus1Retry_ "tcSetAttr" $
256 c_tcsetattr fd const_tcsanow p_tios
257 c_sigprocmask const_sig_setmask p_old_sigset nullPtr
260 #ifdef __GLASGOW_HASKELL__
261 foreign import ccall unsafe "HsBase.h __hscore_get_saved_termios"
262 get_saved_termios :: CInt -> IO (Ptr CTermios)
264 foreign import ccall unsafe "HsBase.h __hscore_set_saved_termios"
265 set_saved_termios :: CInt -> (Ptr CTermios) -> IO ()
270 -- 'raw' mode for Win32 means turn off 'line input' (=> buffering and
271 -- character translation for the console.) The Win32 API for doing
272 -- this is GetConsoleMode(), which also requires echoing to be disabled
273 -- when turning off 'line input' processing. Notice that turning off
274 -- 'line input' implies enter/return is reported as '\r' (and it won't
275 -- report that character until another character is input..odd.) This
276 -- latter feature doesn't sit too well with IO actions like IO.hGetLine..
277 -- consider yourself warned.
278 setCooked :: FD -> Bool -> IO ()
279 setCooked fd cooked = do
280 x <- set_console_buffering fd (if cooked then 1 else 0)
282 then ioError (ioe_unk_error "setCooked" "failed to set buffering")
285 ioe_unk_error :: String -> String -> IOException
286 ioe_unk_error loc msg
288 = ioeSetErrorString (mkIOError OtherError loc Nothing Nothing) msg
293 -- Note: echoing goes hand in hand with enabling 'line input' / raw-ness
294 -- for Win32 consoles, hence setEcho ends up being the inverse of setCooked.
295 setEcho :: FD -> Bool -> IO ()
297 x <- set_console_echo fd (if on then 1 else 0)
299 then ioError (ioe_unk_error "setEcho" "failed to set echoing")
302 getEcho :: FD -> IO Bool
304 r <- get_console_echo fd
306 then ioError (ioe_unk_error "getEcho" "failed to get echoing")
309 foreign import ccall unsafe "consUtils.h set_console_buffering__"
310 set_console_buffering :: CInt -> CInt -> IO CInt
312 foreign import ccall unsafe "consUtils.h set_console_echo__"
313 set_console_echo :: CInt -> CInt -> IO CInt
315 foreign import ccall unsafe "consUtils.h get_console_echo__"
316 get_console_echo :: CInt -> IO CInt
320 -- ---------------------------------------------------------------------------
321 -- Turning on non-blocking for a file descriptor
323 setNonBlockingFD :: FD -> IO ()
324 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
325 setNonBlockingFD fd = do
326 flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
327 (c_fcntl_read fd const_f_getfl)
328 -- An error when setting O_NONBLOCK isn't fatal: on some systems
329 -- there are certain file handles on which this will fail (eg. /dev/null
330 -- on FreeBSD) so we throw away the return code from fcntl_write.
331 unless (testBit flags (fromIntegral o_NONBLOCK)) $ do
332 c_fcntl_write fd const_f_setfl (fromIntegral (flags .|. o_NONBLOCK))
336 -- bogus defns for win32
337 setNonBlockingFD _ = return ()
341 -- -----------------------------------------------------------------------------
342 -- Set close-on-exec for a file descriptor
344 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
345 setCloseOnExec :: FD -> IO ()
346 setCloseOnExec fd = do
347 throwErrnoIfMinus1 "setCloseOnExec" $
348 c_fcntl_write fd const_f_setfd const_fd_cloexec
352 -- -----------------------------------------------------------------------------
355 foreign import ccall unsafe "HsBase.h access"
356 c_access :: CString -> CInt -> IO CInt
358 foreign import ccall unsafe "HsBase.h chmod"
359 c_chmod :: CString -> CMode -> IO CInt
361 foreign import ccall unsafe "HsBase.h close"
362 c_close :: CInt -> IO CInt
364 foreign import ccall unsafe "HsBase.h closedir"
365 c_closedir :: Ptr CDir -> IO CInt
367 foreign import ccall unsafe "HsBase.h creat"
368 c_creat :: CString -> CMode -> IO CInt
370 foreign import ccall unsafe "HsBase.h dup"
371 c_dup :: CInt -> IO CInt
373 foreign import ccall unsafe "HsBase.h dup2"
374 c_dup2 :: CInt -> CInt -> IO CInt
376 foreign import ccall unsafe "HsBase.h __hscore_fstat"
377 c_fstat :: CInt -> Ptr CStat -> IO CInt
379 foreign import ccall unsafe "HsBase.h isatty"
380 c_isatty :: CInt -> IO CInt
382 #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
383 foreign import ccall unsafe "HsBase.h __hscore_lseek"
384 c_lseek :: CInt -> Int64 -> CInt -> IO Int64
386 foreign import ccall unsafe "HsBase.h __hscore_lseek"
387 c_lseek :: CInt -> COff -> CInt -> IO COff
390 foreign import ccall unsafe "HsBase.h __hscore_lstat"
391 lstat :: CString -> Ptr CStat -> IO CInt
393 foreign import ccall unsafe "HsBase.h __hscore_open"
394 c_open :: CString -> CInt -> CMode -> IO CInt
396 foreign import ccall unsafe "HsBase.h opendir"
397 c_opendir :: CString -> IO (Ptr CDir)
399 foreign import ccall unsafe "HsBase.h __hscore_mkdir"
400 mkdir :: CString -> CInt -> IO CInt
402 foreign import ccall unsafe "HsBase.h read"
403 c_read :: CInt -> Ptr CChar -> CSize -> IO CSsize
405 foreign import ccall unsafe "HsBase.h rewinddir"
406 c_rewinddir :: Ptr CDir -> IO ()
408 foreign import ccall unsafe "HsBase.h __hscore_stat"
409 c_stat :: CString -> Ptr CStat -> IO CInt
411 foreign import ccall unsafe "HsBase.h umask"
412 c_umask :: CMode -> IO CMode
414 foreign import ccall unsafe "HsBase.h write"
415 c_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
417 foreign import ccall unsafe "HsBase.h __hscore_ftruncate"
418 c_ftruncate :: CInt -> COff -> IO CInt
420 foreign import ccall unsafe "HsBase.h unlink"
421 c_unlink :: CString -> IO CInt
423 foreign import ccall unsafe "HsBase.h getpid"
426 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
427 foreign import ccall unsafe "HsBase.h fcntl_read"
428 c_fcntl_read :: CInt -> CInt -> IO CInt
430 foreign import ccall unsafe "HsBase.h fcntl_write"
431 c_fcntl_write :: CInt -> CInt -> CLong -> IO CInt
433 foreign import ccall unsafe "HsBase.h fcntl_lock"
434 c_fcntl_lock :: CInt -> CInt -> Ptr CFLock -> IO CInt
436 foreign import ccall unsafe "HsBase.h fork"
439 foreign import ccall unsafe "HsBase.h link"
440 c_link :: CString -> CString -> IO CInt
442 foreign import ccall unsafe "HsBase.h mkfifo"
443 c_mkfifo :: CString -> CMode -> IO CInt
445 foreign import ccall unsafe "HsBase.h pipe"
446 c_pipe :: Ptr CInt -> IO CInt
448 foreign import ccall unsafe "HsBase.h __hscore_sigemptyset"
449 c_sigemptyset :: Ptr CSigset -> IO CInt
451 foreign import ccall unsafe "HsBase.h __hscore_sigaddset"
452 c_sigaddset :: Ptr CSigset -> CInt -> IO CInt
454 foreign import ccall unsafe "HsBase.h sigprocmask"
455 c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO CInt
457 foreign import ccall unsafe "HsBase.h tcgetattr"
458 c_tcgetattr :: CInt -> Ptr CTermios -> IO CInt
460 foreign import ccall unsafe "HsBase.h tcsetattr"
461 c_tcsetattr :: CInt -> CInt -> Ptr CTermios -> IO CInt
463 foreign import ccall unsafe "HsBase.h utime"
464 c_utime :: CString -> Ptr CUtimbuf -> IO CInt
466 foreign import ccall unsafe "HsBase.h waitpid"
467 c_waitpid :: CPid -> Ptr CInt -> CInt -> IO CPid
470 -- traversing directories
471 foreign import ccall unsafe "dirUtils.h __hscore_readdir"
472 readdir :: Ptr CDir -> Ptr (Ptr CDirent) -> IO CInt
474 foreign import ccall unsafe "HsBase.h __hscore_free_dirent"
475 freeDirEnt :: Ptr CDirent -> IO ()
477 foreign import ccall unsafe "HsBase.h __hscore_end_of_dir"
480 foreign import ccall unsafe "HsBase.h __hscore_d_name"
481 d_name :: Ptr CDirent -> IO CString
484 foreign import ccall unsafe "HsBase.h __hscore_o_rdonly" o_RDONLY :: CInt
485 foreign import ccall unsafe "HsBase.h __hscore_o_wronly" o_WRONLY :: CInt
486 foreign import ccall unsafe "HsBase.h __hscore_o_rdwr" o_RDWR :: CInt
487 foreign import ccall unsafe "HsBase.h __hscore_o_append" o_APPEND :: CInt
488 foreign import ccall unsafe "HsBase.h __hscore_o_creat" o_CREAT :: CInt
489 foreign import ccall unsafe "HsBase.h __hscore_o_excl" o_EXCL :: CInt
490 foreign import ccall unsafe "HsBase.h __hscore_o_trunc" o_TRUNC :: CInt
493 foreign import ccall unsafe "HsBase.h __hscore_o_noctty" o_NOCTTY :: CInt
494 foreign import ccall unsafe "HsBase.h __hscore_o_nonblock" o_NONBLOCK :: CInt
495 foreign import ccall unsafe "HsBase.h __hscore_o_binary" o_BINARY :: CInt
497 foreign import ccall unsafe "HsBase.h __hscore_s_isreg" c_s_isreg :: CMode -> CInt
498 foreign import ccall unsafe "HsBase.h __hscore_s_ischr" c_s_ischr :: CMode -> CInt
499 foreign import ccall unsafe "HsBase.h __hscore_s_isblk" c_s_isblk :: CMode -> CInt
500 foreign import ccall unsafe "HsBase.h __hscore_s_isdir" c_s_isdir :: CMode -> CInt
501 foreign import ccall unsafe "HsBase.h __hscore_s_isfifo" c_s_isfifo :: CMode -> CInt
503 s_isreg :: CMode -> Bool
504 s_isreg cm = c_s_isreg cm /= 0
505 s_ischr :: CMode -> Bool
506 s_ischr cm = c_s_ischr cm /= 0
507 s_isblk :: CMode -> Bool
508 s_isblk cm = c_s_isblk cm /= 0
509 s_isdir :: CMode -> Bool
510 s_isdir cm = c_s_isdir cm /= 0
511 s_isfifo :: CMode -> Bool
512 s_isfifo cm = c_s_isfifo cm /= 0
514 foreign import ccall unsafe "HsBase.h __hscore_sizeof_stat" sizeof_stat :: Int
515 foreign import ccall unsafe "HsBase.h __hscore_st_mtime" st_mtime :: Ptr CStat -> IO CTime
516 #ifdef mingw32_HOST_OS
517 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO Int64
519 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO COff
521 foreign import ccall unsafe "HsBase.h __hscore_st_mode" st_mode :: Ptr CStat -> IO CMode
522 foreign import ccall unsafe "HsBase.h __hscore_st_dev" st_dev :: Ptr CStat -> IO CDev
523 foreign import ccall unsafe "HsBase.h __hscore_st_ino" st_ino :: Ptr CStat -> IO CIno
525 foreign import ccall unsafe "HsBase.h __hscore_echo" const_echo :: CInt
526 foreign import ccall unsafe "HsBase.h __hscore_tcsanow" const_tcsanow :: CInt
527 foreign import ccall unsafe "HsBase.h __hscore_icanon" const_icanon :: CInt
528 foreign import ccall unsafe "HsBase.h __hscore_vmin" const_vmin :: CInt
529 foreign import ccall unsafe "HsBase.h __hscore_vtime" const_vtime :: CInt
530 foreign import ccall unsafe "HsBase.h __hscore_sigttou" const_sigttou :: CInt
531 foreign import ccall unsafe "HsBase.h __hscore_sig_block" const_sig_block :: CInt
532 foreign import ccall unsafe "HsBase.h __hscore_sig_setmask" const_sig_setmask :: CInt
533 foreign import ccall unsafe "HsBase.h __hscore_f_getfl" const_f_getfl :: CInt
534 foreign import ccall unsafe "HsBase.h __hscore_f_setfl" const_f_setfl :: CInt
535 foreign import ccall unsafe "HsBase.h __hscore_f_setfd" const_f_setfd :: CInt
536 foreign import ccall unsafe "HsBase.h __hscore_fd_cloexec" const_fd_cloexec :: CLong
538 #if defined(HTYPE_TCFLAG_T)
539 foreign import ccall unsafe "HsBase.h __hscore_sizeof_termios" sizeof_termios :: Int
540 foreign import ccall unsafe "HsBase.h __hscore_sizeof_sigset_t" sizeof_sigset_t :: Int
542 foreign import ccall unsafe "HsBase.h __hscore_lflag" c_lflag :: Ptr CTermios -> IO CTcflag
543 foreign import ccall unsafe "HsBase.h __hscore_poke_lflag" poke_c_lflag :: Ptr CTermios -> CTcflag -> IO ()
544 foreign import ccall unsafe "HsBase.h __hscore_ptr_c_cc" ptr_c_cc :: Ptr CTermios -> IO (Ptr Word8)
547 s_issock :: CMode -> Bool
548 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
549 s_issock cmode = c_s_issock cmode /= 0
550 foreign import ccall unsafe "HsBase.h __hscore_s_issock" c_s_issock :: CMode -> CInt