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(..))
59 import GHC.IO.Device -- yes, I know, but its portable, really!
61 import Control.Exception
66 {-# CFILES cbits/PrelIOUtils.c cbits/dirUtils.c cbits/consUtils.c #-}
69 -- ---------------------------------------------------------------------------
89 -- ---------------------------------------------------------------------------
90 -- stat()-related stuff
92 fdFileSize :: FD -> IO Integer
94 allocaBytes sizeof_stat $ \ p_stat -> do
95 throwErrnoIfMinus1Retry "fileSize" $
97 c_mode <- st_mode p_stat :: IO CMode
98 if not (s_isreg c_mode)
101 c_size <- st_size p_stat
102 return (fromIntegral c_size)
104 fileType :: FilePath -> IO IODeviceType
106 allocaBytes sizeof_stat $ \ p_stat -> do
107 withFilePath file $ \p_file -> do
108 throwErrnoIfMinus1Retry "fileType" $
112 -- NOTE: On Win32 platforms, this will only work with file descriptors
113 -- referring to file handles. i.e., it'll fail for socket FDs.
114 fdStat :: FD -> IO (IODeviceType, CDev, CIno)
116 allocaBytes sizeof_stat $ \ p_stat -> do
117 throwErrnoIfMinus1Retry "fdType" $
119 ty <- statGetType p_stat
124 fdType :: FD -> IO IODeviceType
125 fdType fd = do (ty,_,_) <- fdStat fd; return ty
127 statGetType :: Ptr CStat -> IO IODeviceType
128 statGetType p_stat = do
129 c_mode <- st_mode p_stat :: IO CMode
131 _ | s_isdir c_mode -> return Directory
132 | s_isfifo c_mode || s_issock c_mode || s_ischr c_mode
134 | s_isreg c_mode -> return RegularFile
135 -- Q: map char devices to RawDevice too?
136 | s_isblk c_mode -> return RawDevice
137 | otherwise -> ioError ioe_unknownfiletype
139 ioe_unknownfiletype :: IOException
141 ioe_unknownfiletype = IOError Nothing UnsupportedOperation "fdType"
143 # if __GLASGOW_HASKELL__
148 ioe_unknownfiletype = UserError "fdType" "unknown file type"
151 fdGetMode :: FD -> IO IOMode
152 #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
154 -- We don't have a way of finding out which flags are set on FDs
155 -- on Windows, so make a handle that thinks that anything goes.
159 flags <- throwErrnoIfMinus1Retry "fdGetMode"
160 (c_fcntl_read fd const_f_getfl)
163 wH = (flags .&. o_WRONLY) /= 0
164 aH = (flags .&. o_APPEND) /= 0
165 rwH = (flags .&. o_RDWR) /= 0
168 | wH && aH = AppendMode
170 | rwH = ReadWriteMode
171 | otherwise = ReadMode
175 #ifdef mingw32_HOST_OS
176 withFilePath :: FilePath -> (CWString -> IO a) -> IO a
177 withFilePath = withCWString
179 withFilePath :: FilePath -> (CString -> IO a) -> IO a
180 withFilePath = withCString
183 -- ---------------------------------------------------------------------------
184 -- Terminal-related stuff
186 #if defined(HTYPE_TCFLAG_T)
188 setEcho :: FD -> Bool -> IO ()
190 tcSetAttr fd $ \ p_tios -> do
191 lflag <- c_lflag p_tios :: IO CTcflag
193 | on = lflag .|. fromIntegral const_echo
194 | otherwise = lflag .&. complement (fromIntegral const_echo)
195 poke_c_lflag p_tios (new_lflag :: CTcflag)
197 getEcho :: FD -> IO Bool
199 tcSetAttr fd $ \ p_tios -> do
200 lflag <- c_lflag p_tios :: IO CTcflag
201 return ((lflag .&. fromIntegral const_echo) /= 0)
203 setCooked :: FD -> Bool -> IO ()
204 setCooked fd cooked =
205 tcSetAttr fd $ \ p_tios -> do
207 -- turn on/off ICANON
208 lflag <- c_lflag p_tios :: IO CTcflag
209 let new_lflag | cooked = lflag .|. (fromIntegral const_icanon)
210 | otherwise = lflag .&. complement (fromIntegral const_icanon)
211 poke_c_lflag p_tios (new_lflag :: CTcflag)
213 -- set VMIN & VTIME to 1/0 respectively
214 when (not cooked) $ do
215 c_cc <- ptr_c_cc p_tios
216 let vmin = (c_cc `plusPtr` (fromIntegral const_vmin)) :: Ptr Word8
217 vtime = (c_cc `plusPtr` (fromIntegral const_vtime)) :: Ptr Word8
221 tcSetAttr :: FD -> (Ptr CTermios -> IO a) -> IO a
222 tcSetAttr fd fun = do
223 allocaBytes sizeof_termios $ \p_tios -> do
224 throwErrnoIfMinus1Retry "tcSetAttr"
225 (c_tcgetattr fd p_tios)
227 #ifdef __GLASGOW_HASKELL__
228 -- Save a copy of termios, if this is a standard file descriptor.
229 -- These terminal settings are restored in hs_exit().
231 p <- get_saved_termios fd
232 when (p == nullPtr) $ do
233 saved_tios <- mallocBytes sizeof_termios
234 copyBytes saved_tios p_tios sizeof_termios
235 set_saved_termios fd saved_tios
238 -- tcsetattr() when invoked by a background process causes the process
239 -- to be sent SIGTTOU regardless of whether the process has TOSTOP set
240 -- in its terminal flags (try it...). This function provides a
241 -- wrapper which temporarily blocks SIGTTOU around the call, making it
243 allocaBytes sizeof_sigset_t $ \ p_sigset -> do
244 allocaBytes sizeof_sigset_t $ \ p_old_sigset -> do
245 c_sigemptyset p_sigset
246 c_sigaddset p_sigset const_sigttou
247 c_sigprocmask const_sig_block p_sigset p_old_sigset
248 r <- fun p_tios -- do the business
249 throwErrnoIfMinus1Retry_ "tcSetAttr" $
250 c_tcsetattr fd const_tcsanow p_tios
251 c_sigprocmask const_sig_setmask p_old_sigset nullPtr
254 #ifdef __GLASGOW_HASKELL__
255 foreign import ccall unsafe "HsBase.h __hscore_get_saved_termios"
256 get_saved_termios :: CInt -> IO (Ptr CTermios)
258 foreign import ccall unsafe "HsBase.h __hscore_set_saved_termios"
259 set_saved_termios :: CInt -> (Ptr CTermios) -> IO ()
264 -- 'raw' mode for Win32 means turn off 'line input' (=> buffering and
265 -- character translation for the console.) The Win32 API for doing
266 -- this is GetConsoleMode(), which also requires echoing to be disabled
267 -- when turning off 'line input' processing. Notice that turning off
268 -- 'line input' implies enter/return is reported as '\r' (and it won't
269 -- report that character until another character is input..odd.) This
270 -- latter feature doesn't sit too well with IO actions like IO.hGetLine..
271 -- consider yourself warned.
272 setCooked :: FD -> Bool -> IO ()
273 setCooked fd cooked = do
274 x <- set_console_buffering fd (if cooked then 1 else 0)
276 then ioError (ioe_unk_error "setCooked" "failed to set buffering")
279 ioe_unk_error :: String -> String -> IOException
280 ioe_unk_error loc msg
282 = ioeSetErrorString (mkIOError OtherError loc Nothing Nothing) msg
287 -- Note: echoing goes hand in hand with enabling 'line input' / raw-ness
288 -- for Win32 consoles, hence setEcho ends up being the inverse of setCooked.
289 setEcho :: FD -> Bool -> IO ()
291 x <- set_console_echo fd (if on then 1 else 0)
293 then ioError (ioe_unk_error "setEcho" "failed to set echoing")
296 getEcho :: FD -> IO Bool
298 r <- get_console_echo fd
300 then ioError (ioe_unk_error "getEcho" "failed to get echoing")
303 foreign import ccall unsafe "consUtils.h set_console_buffering__"
304 set_console_buffering :: CInt -> CInt -> IO CInt
306 foreign import ccall unsafe "consUtils.h set_console_echo__"
307 set_console_echo :: CInt -> CInt -> IO CInt
309 foreign import ccall unsafe "consUtils.h get_console_echo__"
310 get_console_echo :: CInt -> IO CInt
314 -- ---------------------------------------------------------------------------
315 -- Turning on non-blocking for a file descriptor
317 setNonBlockingFD :: FD -> IO ()
318 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
319 setNonBlockingFD fd = do
320 flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
321 (c_fcntl_read fd const_f_getfl)
322 -- An error when setting O_NONBLOCK isn't fatal: on some systems
323 -- there are certain file handles on which this will fail (eg. /dev/null
324 -- on FreeBSD) so we throw away the return code from fcntl_write.
325 unless (testBit flags (fromIntegral o_NONBLOCK)) $ do
326 c_fcntl_write fd const_f_setfl (fromIntegral (flags .|. o_NONBLOCK))
330 -- bogus defns for win32
331 setNonBlockingFD _ = return ()
335 -- -----------------------------------------------------------------------------
336 -- Set close-on-exec for a file descriptor
338 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
339 setCloseOnExec :: FD -> IO ()
340 setCloseOnExec fd = do
341 throwErrnoIfMinus1 "setCloseOnExec" $
342 c_fcntl_write fd const_f_setfd const_fd_cloexec
346 -- -----------------------------------------------------------------------------
349 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
350 type CFilePath = CString
352 type CFilePath = CWString
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 :: CFilePath -> Ptr CStat -> IO CInt
393 foreign import ccall unsafe "__hscore_open"
394 c_open :: CFilePath -> 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 Word8 -> CSize -> IO CSsize
405 foreign import ccall safe "read"
406 c_safe_read :: CInt -> Ptr Word8 -> CSize -> IO CSsize
408 foreign import ccall unsafe "HsBase.h rewinddir"
409 c_rewinddir :: Ptr CDir -> IO ()
411 foreign import ccall unsafe "__hscore_stat"
412 c_stat :: CFilePath -> Ptr CStat -> IO CInt
414 foreign import ccall unsafe "HsBase.h umask"
415 c_umask :: CMode -> IO CMode
417 foreign import ccall unsafe "HsBase.h write"
418 c_write :: CInt -> Ptr Word8 -> CSize -> IO CSsize
420 foreign import ccall safe "write"
421 c_safe_write :: CInt -> Ptr Word8 -> CSize -> IO CSsize
423 foreign import ccall unsafe "HsBase.h __hscore_ftruncate"
424 c_ftruncate :: CInt -> COff -> IO CInt
426 foreign import ccall unsafe "HsBase.h unlink"
427 c_unlink :: CString -> IO CInt
429 foreign import ccall unsafe "HsBase.h getpid"
432 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
433 foreign import ccall unsafe "HsBase.h fcntl_read"
434 c_fcntl_read :: CInt -> CInt -> IO CInt
436 foreign import ccall unsafe "HsBase.h fcntl_write"
437 c_fcntl_write :: CInt -> CInt -> CLong -> IO CInt
439 foreign import ccall unsafe "HsBase.h fcntl_lock"
440 c_fcntl_lock :: CInt -> CInt -> Ptr CFLock -> IO CInt
442 foreign import ccall unsafe "HsBase.h fork"
445 foreign import ccall unsafe "HsBase.h link"
446 c_link :: CString -> CString -> IO CInt
448 foreign import ccall unsafe "HsBase.h mkfifo"
449 c_mkfifo :: CString -> CMode -> IO CInt
451 foreign import ccall unsafe "HsBase.h pipe"
452 c_pipe :: Ptr CInt -> IO CInt
454 foreign import ccall unsafe "HsBase.h __hscore_sigemptyset"
455 c_sigemptyset :: Ptr CSigset -> IO CInt
457 foreign import ccall unsafe "HsBase.h __hscore_sigaddset"
458 c_sigaddset :: Ptr CSigset -> CInt -> IO CInt
460 foreign import ccall unsafe "HsBase.h sigprocmask"
461 c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO CInt
463 foreign import ccall unsafe "HsBase.h tcgetattr"
464 c_tcgetattr :: CInt -> Ptr CTermios -> IO CInt
466 foreign import ccall unsafe "HsBase.h tcsetattr"
467 c_tcsetattr :: CInt -> CInt -> Ptr CTermios -> IO CInt
469 foreign import ccall unsafe "HsBase.h utime"
470 c_utime :: CString -> Ptr CUtimbuf -> IO CInt
472 foreign import ccall unsafe "HsBase.h waitpid"
473 c_waitpid :: CPid -> Ptr CInt -> CInt -> IO CPid
476 -- traversing directories
477 foreign import ccall unsafe "dirUtils.h __hscore_readdir"
478 readdir :: Ptr CDir -> Ptr (Ptr CDirent) -> IO CInt
480 foreign import ccall unsafe "HsBase.h __hscore_free_dirent"
481 freeDirEnt :: Ptr CDirent -> IO ()
483 foreign import ccall unsafe "HsBase.h __hscore_end_of_dir"
486 foreign import ccall unsafe "HsBase.h __hscore_d_name"
487 d_name :: Ptr CDirent -> IO CString
490 foreign import ccall unsafe "HsBase.h __hscore_o_rdonly" o_RDONLY :: CInt
491 foreign import ccall unsafe "HsBase.h __hscore_o_wronly" o_WRONLY :: CInt
492 foreign import ccall unsafe "HsBase.h __hscore_o_rdwr" o_RDWR :: CInt
493 foreign import ccall unsafe "HsBase.h __hscore_o_append" o_APPEND :: CInt
494 foreign import ccall unsafe "HsBase.h __hscore_o_creat" o_CREAT :: CInt
495 foreign import ccall unsafe "HsBase.h __hscore_o_excl" o_EXCL :: CInt
496 foreign import ccall unsafe "HsBase.h __hscore_o_trunc" o_TRUNC :: CInt
499 foreign import ccall unsafe "HsBase.h __hscore_o_noctty" o_NOCTTY :: CInt
500 foreign import ccall unsafe "HsBase.h __hscore_o_nonblock" o_NONBLOCK :: CInt
501 foreign import ccall unsafe "HsBase.h __hscore_o_binary" o_BINARY :: CInt
503 foreign import ccall unsafe "HsBase.h __hscore_s_isreg" c_s_isreg :: CMode -> CInt
504 foreign import ccall unsafe "HsBase.h __hscore_s_ischr" c_s_ischr :: CMode -> CInt
505 foreign import ccall unsafe "HsBase.h __hscore_s_isblk" c_s_isblk :: CMode -> CInt
506 foreign import ccall unsafe "HsBase.h __hscore_s_isdir" c_s_isdir :: CMode -> CInt
507 foreign import ccall unsafe "HsBase.h __hscore_s_isfifo" c_s_isfifo :: CMode -> CInt
509 s_isreg :: CMode -> Bool
510 s_isreg cm = c_s_isreg cm /= 0
511 s_ischr :: CMode -> Bool
512 s_ischr cm = c_s_ischr cm /= 0
513 s_isblk :: CMode -> Bool
514 s_isblk cm = c_s_isblk cm /= 0
515 s_isdir :: CMode -> Bool
516 s_isdir cm = c_s_isdir cm /= 0
517 s_isfifo :: CMode -> Bool
518 s_isfifo cm = c_s_isfifo cm /= 0
520 foreign import ccall unsafe "HsBase.h __hscore_sizeof_stat" sizeof_stat :: Int
521 foreign import ccall unsafe "HsBase.h __hscore_st_mtime" st_mtime :: Ptr CStat -> IO CTime
522 #ifdef mingw32_HOST_OS
523 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO Int64
525 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO COff
527 foreign import ccall unsafe "HsBase.h __hscore_st_mode" st_mode :: Ptr CStat -> IO CMode
528 foreign import ccall unsafe "HsBase.h __hscore_st_dev" st_dev :: Ptr CStat -> IO CDev
529 foreign import ccall unsafe "HsBase.h __hscore_st_ino" st_ino :: Ptr CStat -> IO CIno
531 foreign import ccall unsafe "HsBase.h __hscore_echo" const_echo :: CInt
532 foreign import ccall unsafe "HsBase.h __hscore_tcsanow" const_tcsanow :: CInt
533 foreign import ccall unsafe "HsBase.h __hscore_icanon" const_icanon :: CInt
534 foreign import ccall unsafe "HsBase.h __hscore_vmin" const_vmin :: CInt
535 foreign import ccall unsafe "HsBase.h __hscore_vtime" const_vtime :: CInt
536 foreign import ccall unsafe "HsBase.h __hscore_sigttou" const_sigttou :: CInt
537 foreign import ccall unsafe "HsBase.h __hscore_sig_block" const_sig_block :: CInt
538 foreign import ccall unsafe "HsBase.h __hscore_sig_setmask" const_sig_setmask :: CInt
539 foreign import ccall unsafe "HsBase.h __hscore_f_getfl" const_f_getfl :: CInt
540 foreign import ccall unsafe "HsBase.h __hscore_f_setfl" const_f_setfl :: CInt
541 foreign import ccall unsafe "HsBase.h __hscore_f_setfd" const_f_setfd :: CInt
542 foreign import ccall unsafe "HsBase.h __hscore_fd_cloexec" const_fd_cloexec :: CLong
544 #if defined(HTYPE_TCFLAG_T)
545 foreign import ccall unsafe "HsBase.h __hscore_sizeof_termios" sizeof_termios :: Int
546 foreign import ccall unsafe "HsBase.h __hscore_sizeof_sigset_t" sizeof_sigset_t :: Int
548 foreign import ccall unsafe "HsBase.h __hscore_lflag" c_lflag :: Ptr CTermios -> IO CTcflag
549 foreign import ccall unsafe "HsBase.h __hscore_poke_lflag" poke_c_lflag :: Ptr CTermios -> CTcflag -> IO ()
550 foreign import ccall unsafe "HsBase.h __hscore_ptr_c_cc" ptr_c_cc :: Ptr CTermios -> IO (Ptr Word8)
553 s_issock :: CMode -> Bool
554 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
555 s_issock cmode = c_s_issock cmode /= 0
556 foreign import ccall unsafe "HsBase.h __hscore_s_issock" c_s_issock :: CMode -> CInt
561 foreign import ccall unsafe "__hscore_bufsiz" dEFAULT_BUFFER_SIZE :: Int
562 foreign import ccall unsafe "__hscore_seek_cur" sEEK_CUR :: CInt
563 foreign import ccall unsafe "__hscore_seek_set" sEEK_SET :: CInt
564 foreign import ccall unsafe "__hscore_seek_end" sEEK_END :: CInt