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 GHC.IO.Exception
56 import Hugs.Prelude (IOException(..), IOErrorType(..))
57 import Hugs.IO (IOMode(..))
60 import Control.Exception
65 {-# CFILES cbits/PrelIOUtils.c cbits/dirUtils.c cbits/consUtils.c #-}
68 -- ---------------------------------------------------------------------------
88 -- ---------------------------------------------------------------------------
89 -- stat()-related stuff
91 fdFileSize :: FD -> IO Integer
93 allocaBytes sizeof_stat $ \ p_stat -> do
94 throwErrnoIfMinus1Retry "fileSize" $
96 c_mode <- st_mode p_stat :: IO CMode
97 if not (s_isreg c_mode)
100 c_size <- st_size p_stat
101 return (fromIntegral c_size)
103 fileType :: FilePath -> IO IODeviceType
105 allocaBytes sizeof_stat $ \ p_stat -> do
106 withCString file $ \p_file -> do
107 throwErrnoIfMinus1Retry "fileType" $
111 -- NOTE: On Win32 platforms, this will only work with file descriptors
112 -- referring to file handles. i.e., it'll fail for socket FDs.
113 fdStat :: FD -> IO (IODeviceType, CDev, CIno)
115 allocaBytes sizeof_stat $ \ p_stat -> do
116 throwErrnoIfMinus1Retry "fdType" $
118 ty <- statGetType p_stat
123 fdType :: FD -> IO IODeviceType
124 fdType fd = do (ty,_,_) <- fdStat fd; return ty
126 statGetType :: Ptr CStat -> IO IODeviceType
127 statGetType p_stat = do
128 c_mode <- st_mode p_stat :: IO CMode
130 _ | s_isdir c_mode -> return Directory
131 | s_isfifo c_mode || s_issock c_mode || s_ischr c_mode
133 | s_isreg c_mode -> return RegularFile
134 -- Q: map char devices to RawDevice too?
135 | s_isblk c_mode -> return RawDevice
136 | otherwise -> ioError ioe_unknownfiletype
138 ioe_unknownfiletype :: IOException
140 ioe_unknownfiletype = IOError Nothing UnsupportedOperation "fdType"
142 # if __GLASGOW_HASKELL__
147 ioe_unknownfiletype = UserError "fdType" "unknown file type"
150 fdGetMode :: FD -> IO IOMode
151 #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
153 -- We don't have a way of finding out which flags are set on FDs
154 -- on Windows, so make a handle that thinks that anything goes.
158 flags <- throwErrnoIfMinus1Retry "fdGetMode"
159 (c_fcntl_read fd const_f_getfl)
162 wH = (flags .&. o_WRONLY) /= 0
163 aH = (flags .&. o_APPEND) /= 0
164 rwH = (flags .&. o_RDWR) /= 0
167 | wH && aH = AppendMode
169 | rwH = ReadWriteMode
170 | otherwise = ReadMode
174 -- ---------------------------------------------------------------------------
175 -- Terminal-related stuff
177 #if defined(HTYPE_TCFLAG_T)
179 setEcho :: FD -> Bool -> IO ()
181 tcSetAttr fd $ \ p_tios -> do
182 lflag <- c_lflag p_tios :: IO CTcflag
184 | on = lflag .|. fromIntegral const_echo
185 | otherwise = lflag .&. complement (fromIntegral const_echo)
186 poke_c_lflag p_tios (new_lflag :: CTcflag)
188 getEcho :: FD -> IO Bool
190 tcSetAttr fd $ \ p_tios -> do
191 lflag <- c_lflag p_tios :: IO CTcflag
192 return ((lflag .&. fromIntegral const_echo) /= 0)
194 setCooked :: FD -> Bool -> IO ()
195 setCooked fd cooked =
196 tcSetAttr fd $ \ p_tios -> do
198 -- turn on/off ICANON
199 lflag <- c_lflag p_tios :: IO CTcflag
200 let new_lflag | cooked = lflag .|. (fromIntegral const_icanon)
201 | otherwise = lflag .&. complement (fromIntegral const_icanon)
202 poke_c_lflag p_tios (new_lflag :: CTcflag)
204 -- set VMIN & VTIME to 1/0 respectively
205 when (not cooked) $ do
206 c_cc <- ptr_c_cc p_tios
207 let vmin = (c_cc `plusPtr` (fromIntegral const_vmin)) :: Ptr Word8
208 vtime = (c_cc `plusPtr` (fromIntegral const_vtime)) :: Ptr Word8
212 tcSetAttr :: FD -> (Ptr CTermios -> IO a) -> IO a
213 tcSetAttr fd fun = do
214 allocaBytes sizeof_termios $ \p_tios -> do
215 throwErrnoIfMinus1Retry "tcSetAttr"
216 (c_tcgetattr fd p_tios)
218 #ifdef __GLASGOW_HASKELL__
219 -- Save a copy of termios, if this is a standard file descriptor.
220 -- These terminal settings are restored in hs_exit().
222 p <- get_saved_termios fd
223 when (p == nullPtr) $ do
224 saved_tios <- mallocBytes sizeof_termios
225 copyBytes saved_tios p_tios sizeof_termios
226 set_saved_termios fd saved_tios
229 -- tcsetattr() when invoked by a background process causes the process
230 -- to be sent SIGTTOU regardless of whether the process has TOSTOP set
231 -- in its terminal flags (try it...). This function provides a
232 -- wrapper which temporarily blocks SIGTTOU around the call, making it
234 allocaBytes sizeof_sigset_t $ \ p_sigset -> do
235 allocaBytes sizeof_sigset_t $ \ p_old_sigset -> do
236 c_sigemptyset p_sigset
237 c_sigaddset p_sigset const_sigttou
238 c_sigprocmask const_sig_block p_sigset p_old_sigset
239 r <- fun p_tios -- do the business
240 throwErrnoIfMinus1Retry_ "tcSetAttr" $
241 c_tcsetattr fd const_tcsanow p_tios
242 c_sigprocmask const_sig_setmask p_old_sigset nullPtr
245 #ifdef __GLASGOW_HASKELL__
246 foreign import ccall unsafe "HsBase.h __hscore_get_saved_termios"
247 get_saved_termios :: CInt -> IO (Ptr CTermios)
249 foreign import ccall unsafe "HsBase.h __hscore_set_saved_termios"
250 set_saved_termios :: CInt -> (Ptr CTermios) -> IO ()
255 -- 'raw' mode for Win32 means turn off 'line input' (=> buffering and
256 -- character translation for the console.) The Win32 API for doing
257 -- this is GetConsoleMode(), which also requires echoing to be disabled
258 -- when turning off 'line input' processing. Notice that turning off
259 -- 'line input' implies enter/return is reported as '\r' (and it won't
260 -- report that character until another character is input..odd.) This
261 -- latter feature doesn't sit too well with IO actions like IO.hGetLine..
262 -- consider yourself warned.
263 setCooked :: FD -> Bool -> IO ()
264 setCooked fd cooked = do
265 x <- set_console_buffering fd (if cooked then 1 else 0)
267 then ioError (ioe_unk_error "setCooked" "failed to set buffering")
270 ioe_unk_error :: String -> String -> IOException
271 ioe_unk_error loc msg
273 = ioeSetErrorString (mkIOError OtherError loc Nothing Nothing) msg
278 -- Note: echoing goes hand in hand with enabling 'line input' / raw-ness
279 -- for Win32 consoles, hence setEcho ends up being the inverse of setCooked.
280 setEcho :: FD -> Bool -> IO ()
282 x <- set_console_echo fd (if on then 1 else 0)
284 then ioError (ioe_unk_error "setEcho" "failed to set echoing")
287 getEcho :: FD -> IO Bool
289 r <- get_console_echo fd
291 then ioError (ioe_unk_error "getEcho" "failed to get echoing")
294 foreign import ccall unsafe "consUtils.h set_console_buffering__"
295 set_console_buffering :: CInt -> CInt -> IO CInt
297 foreign import ccall unsafe "consUtils.h set_console_echo__"
298 set_console_echo :: CInt -> CInt -> IO CInt
300 foreign import ccall unsafe "consUtils.h get_console_echo__"
301 get_console_echo :: CInt -> IO CInt
305 -- ---------------------------------------------------------------------------
306 -- Turning on non-blocking for a file descriptor
308 setNonBlockingFD :: FD -> IO ()
309 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
310 setNonBlockingFD fd = do
311 flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
312 (c_fcntl_read fd const_f_getfl)
313 -- An error when setting O_NONBLOCK isn't fatal: on some systems
314 -- there are certain file handles on which this will fail (eg. /dev/null
315 -- on FreeBSD) so we throw away the return code from fcntl_write.
316 unless (testBit flags (fromIntegral o_NONBLOCK)) $ do
317 c_fcntl_write fd const_f_setfl (fromIntegral (flags .|. o_NONBLOCK))
321 -- bogus defns for win32
322 setNonBlockingFD _ = return ()
326 -- -----------------------------------------------------------------------------
327 -- Set close-on-exec for a file descriptor
329 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
330 setCloseOnExec :: FD -> IO ()
331 setCloseOnExec fd = do
332 throwErrnoIfMinus1 "setCloseOnExec" $
333 c_fcntl_write fd const_f_setfd const_fd_cloexec
337 -- -----------------------------------------------------------------------------
340 foreign import ccall unsafe "HsBase.h access"
341 c_access :: CString -> CInt -> IO CInt
343 foreign import ccall unsafe "HsBase.h chmod"
344 c_chmod :: CString -> CMode -> IO CInt
346 foreign import ccall unsafe "HsBase.h close"
347 c_close :: CInt -> IO CInt
349 foreign import ccall unsafe "HsBase.h closedir"
350 c_closedir :: Ptr CDir -> IO CInt
352 foreign import ccall unsafe "HsBase.h creat"
353 c_creat :: CString -> CMode -> IO CInt
355 foreign import ccall unsafe "HsBase.h dup"
356 c_dup :: CInt -> IO CInt
358 foreign import ccall unsafe "HsBase.h dup2"
359 c_dup2 :: CInt -> CInt -> IO CInt
361 foreign import ccall unsafe "HsBase.h __hscore_fstat"
362 c_fstat :: CInt -> Ptr CStat -> IO CInt
364 foreign import ccall unsafe "HsBase.h isatty"
365 c_isatty :: CInt -> IO CInt
367 #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
368 foreign import ccall unsafe "HsBase.h __hscore_lseek"
369 c_lseek :: CInt -> Int64 -> CInt -> IO Int64
371 foreign import ccall unsafe "HsBase.h __hscore_lseek"
372 c_lseek :: CInt -> COff -> CInt -> IO COff
375 foreign import ccall unsafe "HsBase.h __hscore_lstat"
376 lstat :: CString -> Ptr CStat -> IO CInt
378 foreign import ccall unsafe "HsBase.h __hscore_open"
379 c_open :: CString -> CInt -> CMode -> IO CInt
381 foreign import ccall unsafe "HsBase.h opendir"
382 c_opendir :: CString -> IO (Ptr CDir)
384 foreign import ccall unsafe "HsBase.h __hscore_mkdir"
385 mkdir :: CString -> CInt -> IO CInt
387 foreign import ccall unsafe "HsBase.h read"
388 c_read :: CInt -> Ptr CChar -> CSize -> IO CSsize
390 foreign import ccall unsafe "HsBase.h rewinddir"
391 c_rewinddir :: Ptr CDir -> IO ()
393 foreign import ccall unsafe "HsBase.h __hscore_stat"
394 c_stat :: CString -> Ptr CStat -> IO CInt
396 foreign import ccall unsafe "HsBase.h umask"
397 c_umask :: CMode -> IO CMode
399 foreign import ccall unsafe "HsBase.h write"
400 c_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
402 foreign import ccall unsafe "HsBase.h __hscore_ftruncate"
403 c_ftruncate :: CInt -> COff -> IO CInt
405 foreign import ccall unsafe "HsBase.h unlink"
406 c_unlink :: CString -> IO CInt
408 foreign import ccall unsafe "HsBase.h getpid"
411 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
412 foreign import ccall unsafe "HsBase.h fcntl_read"
413 c_fcntl_read :: CInt -> CInt -> IO CInt
415 foreign import ccall unsafe "HsBase.h fcntl_write"
416 c_fcntl_write :: CInt -> CInt -> CLong -> IO CInt
418 foreign import ccall unsafe "HsBase.h fcntl_lock"
419 c_fcntl_lock :: CInt -> CInt -> Ptr CFLock -> IO CInt
421 foreign import ccall unsafe "HsBase.h fork"
424 foreign import ccall unsafe "HsBase.h link"
425 c_link :: CString -> CString -> IO CInt
427 foreign import ccall unsafe "HsBase.h mkfifo"
428 c_mkfifo :: CString -> CMode -> IO CInt
430 foreign import ccall unsafe "HsBase.h pipe"
431 c_pipe :: Ptr CInt -> IO CInt
433 foreign import ccall unsafe "HsBase.h __hscore_sigemptyset"
434 c_sigemptyset :: Ptr CSigset -> IO CInt
436 foreign import ccall unsafe "HsBase.h __hscore_sigaddset"
437 c_sigaddset :: Ptr CSigset -> CInt -> IO CInt
439 foreign import ccall unsafe "HsBase.h sigprocmask"
440 c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO CInt
442 foreign import ccall unsafe "HsBase.h tcgetattr"
443 c_tcgetattr :: CInt -> Ptr CTermios -> IO CInt
445 foreign import ccall unsafe "HsBase.h tcsetattr"
446 c_tcsetattr :: CInt -> CInt -> Ptr CTermios -> IO CInt
448 foreign import ccall unsafe "HsBase.h utime"
449 c_utime :: CString -> Ptr CUtimbuf -> IO CInt
451 foreign import ccall unsafe "HsBase.h waitpid"
452 c_waitpid :: CPid -> Ptr CInt -> CInt -> IO CPid
455 -- traversing directories
456 foreign import ccall unsafe "dirUtils.h __hscore_readdir"
457 readdir :: Ptr CDir -> Ptr (Ptr CDirent) -> IO CInt
459 foreign import ccall unsafe "HsBase.h __hscore_free_dirent"
460 freeDirEnt :: Ptr CDirent -> IO ()
462 foreign import ccall unsafe "HsBase.h __hscore_end_of_dir"
465 foreign import ccall unsafe "HsBase.h __hscore_d_name"
466 d_name :: Ptr CDirent -> IO CString
469 foreign import ccall unsafe "HsBase.h __hscore_o_rdonly" o_RDONLY :: CInt
470 foreign import ccall unsafe "HsBase.h __hscore_o_wronly" o_WRONLY :: CInt
471 foreign import ccall unsafe "HsBase.h __hscore_o_rdwr" o_RDWR :: CInt
472 foreign import ccall unsafe "HsBase.h __hscore_o_append" o_APPEND :: CInt
473 foreign import ccall unsafe "HsBase.h __hscore_o_creat" o_CREAT :: CInt
474 foreign import ccall unsafe "HsBase.h __hscore_o_excl" o_EXCL :: CInt
475 foreign import ccall unsafe "HsBase.h __hscore_o_trunc" o_TRUNC :: CInt
478 foreign import ccall unsafe "HsBase.h __hscore_o_noctty" o_NOCTTY :: CInt
479 foreign import ccall unsafe "HsBase.h __hscore_o_nonblock" o_NONBLOCK :: CInt
480 foreign import ccall unsafe "HsBase.h __hscore_o_binary" o_BINARY :: CInt
482 foreign import ccall unsafe "HsBase.h __hscore_s_isreg" c_s_isreg :: CMode -> CInt
483 foreign import ccall unsafe "HsBase.h __hscore_s_ischr" c_s_ischr :: CMode -> CInt
484 foreign import ccall unsafe "HsBase.h __hscore_s_isblk" c_s_isblk :: CMode -> CInt
485 foreign import ccall unsafe "HsBase.h __hscore_s_isdir" c_s_isdir :: CMode -> CInt
486 foreign import ccall unsafe "HsBase.h __hscore_s_isfifo" c_s_isfifo :: CMode -> CInt
488 s_isreg :: CMode -> Bool
489 s_isreg cm = c_s_isreg cm /= 0
490 s_ischr :: CMode -> Bool
491 s_ischr cm = c_s_ischr cm /= 0
492 s_isblk :: CMode -> Bool
493 s_isblk cm = c_s_isblk cm /= 0
494 s_isdir :: CMode -> Bool
495 s_isdir cm = c_s_isdir cm /= 0
496 s_isfifo :: CMode -> Bool
497 s_isfifo cm = c_s_isfifo cm /= 0
499 foreign import ccall unsafe "HsBase.h __hscore_sizeof_stat" sizeof_stat :: Int
500 foreign import ccall unsafe "HsBase.h __hscore_st_mtime" st_mtime :: Ptr CStat -> IO CTime
501 #ifdef mingw32_HOST_OS
502 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO Int64
504 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO COff
506 foreign import ccall unsafe "HsBase.h __hscore_st_mode" st_mode :: Ptr CStat -> IO CMode
507 foreign import ccall unsafe "HsBase.h __hscore_st_dev" st_dev :: Ptr CStat -> IO CDev
508 foreign import ccall unsafe "HsBase.h __hscore_st_ino" st_ino :: Ptr CStat -> IO CIno
510 foreign import ccall unsafe "HsBase.h __hscore_echo" const_echo :: CInt
511 foreign import ccall unsafe "HsBase.h __hscore_tcsanow" const_tcsanow :: CInt
512 foreign import ccall unsafe "HsBase.h __hscore_icanon" const_icanon :: CInt
513 foreign import ccall unsafe "HsBase.h __hscore_vmin" const_vmin :: CInt
514 foreign import ccall unsafe "HsBase.h __hscore_vtime" const_vtime :: CInt
515 foreign import ccall unsafe "HsBase.h __hscore_sigttou" const_sigttou :: CInt
516 foreign import ccall unsafe "HsBase.h __hscore_sig_block" const_sig_block :: CInt
517 foreign import ccall unsafe "HsBase.h __hscore_sig_setmask" const_sig_setmask :: CInt
518 foreign import ccall unsafe "HsBase.h __hscore_f_getfl" const_f_getfl :: CInt
519 foreign import ccall unsafe "HsBase.h __hscore_f_setfl" const_f_setfl :: CInt
520 foreign import ccall unsafe "HsBase.h __hscore_f_setfd" const_f_setfd :: CInt
521 foreign import ccall unsafe "HsBase.h __hscore_fd_cloexec" const_fd_cloexec :: CLong
523 #if defined(HTYPE_TCFLAG_T)
524 foreign import ccall unsafe "HsBase.h __hscore_sizeof_termios" sizeof_termios :: Int
525 foreign import ccall unsafe "HsBase.h __hscore_sizeof_sigset_t" sizeof_sigset_t :: Int
527 foreign import ccall unsafe "HsBase.h __hscore_lflag" c_lflag :: Ptr CTermios -> IO CTcflag
528 foreign import ccall unsafe "HsBase.h __hscore_poke_lflag" poke_c_lflag :: Ptr CTermios -> CTcflag -> IO ()
529 foreign import ccall unsafe "HsBase.h __hscore_ptr_c_cc" ptr_c_cc :: Ptr CTermios -> IO (Ptr Word8)
532 s_issock :: CMode -> Bool
533 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
534 s_issock cmode = c_s_issock cmode /= 0
535 foreign import ccall unsafe "HsBase.h __hscore_s_issock" c_s_issock :: CMode -> CInt
540 foreign import ccall unsafe "__hscore_bufsiz" dEFAULT_BUFFER_SIZE :: Int
541 foreign import ccall unsafe "__hscore_seek_cur" sEEK_CUR :: CInt
542 foreign import ccall unsafe "__hscore_seek_set" sEEK_SET :: CInt
543 foreign import ccall unsafe "__hscore_seek_end" sEEK_END :: CInt