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/consUtils.c #-}
69 -- ---------------------------------------------------------------------------
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 fileType :: FilePath -> IO IODeviceType
104 allocaBytes sizeof_stat $ \ p_stat -> do
105 withFilePath file $ \p_file -> do
106 throwErrnoIfMinus1Retry_ "fileType" $
110 -- NOTE: On Win32 platforms, this will only work with file descriptors
111 -- referring to file handles. i.e., it'll fail for socket FDs.
112 fdStat :: FD -> IO (IODeviceType, CDev, CIno)
114 allocaBytes sizeof_stat $ \ p_stat -> do
115 throwErrnoIfMinus1Retry_ "fdType" $
117 ty <- statGetType p_stat
122 fdType :: FD -> IO IODeviceType
123 fdType fd = do (ty,_,_) <- fdStat fd; return ty
125 statGetType :: Ptr CStat -> IO IODeviceType
126 statGetType p_stat = do
127 c_mode <- st_mode p_stat :: IO CMode
129 _ | s_isdir c_mode -> return Directory
130 | s_isfifo c_mode || s_issock c_mode || s_ischr c_mode
132 | s_isreg c_mode -> return RegularFile
133 -- Q: map char devices to RawDevice too?
134 | s_isblk c_mode -> return RawDevice
135 | otherwise -> ioError ioe_unknownfiletype
137 ioe_unknownfiletype :: IOException
139 ioe_unknownfiletype = IOError Nothing UnsupportedOperation "fdType"
141 # if __GLASGOW_HASKELL__
146 ioe_unknownfiletype = UserError "fdType" "unknown file type"
149 fdGetMode :: FD -> IO IOMode
150 #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
152 -- We don't have a way of finding out which flags are set on FDs
153 -- on Windows, so make a handle that thinks that anything goes.
157 flags <- throwErrnoIfMinus1Retry "fdGetMode"
158 (c_fcntl_read fd const_f_getfl)
161 wH = (flags .&. o_WRONLY) /= 0
162 aH = (flags .&. o_APPEND) /= 0
163 rwH = (flags .&. o_RDWR) /= 0
166 | wH && aH = AppendMode
168 | rwH = ReadWriteMode
169 | otherwise = ReadMode
173 #ifdef mingw32_HOST_OS
174 withFilePath :: FilePath -> (CWString -> IO a) -> IO a
175 withFilePath = withCWString
177 withFilePath :: FilePath -> (CString -> IO a) -> IO a
178 withFilePath = withCString
181 -- ---------------------------------------------------------------------------
182 -- Terminal-related stuff
184 #if defined(HTYPE_TCFLAG_T)
186 setEcho :: FD -> Bool -> IO ()
188 tcSetAttr fd $ \ p_tios -> do
189 lflag <- c_lflag p_tios :: IO CTcflag
191 | on = lflag .|. fromIntegral const_echo
192 | otherwise = lflag .&. complement (fromIntegral const_echo)
193 poke_c_lflag p_tios (new_lflag :: CTcflag)
195 getEcho :: FD -> IO Bool
197 tcSetAttr fd $ \ p_tios -> do
198 lflag <- c_lflag p_tios :: IO CTcflag
199 return ((lflag .&. fromIntegral const_echo) /= 0)
201 setCooked :: FD -> Bool -> IO ()
202 setCooked fd cooked =
203 tcSetAttr fd $ \ p_tios -> do
205 -- turn on/off ICANON
206 lflag <- c_lflag p_tios :: IO CTcflag
207 let new_lflag | cooked = lflag .|. (fromIntegral const_icanon)
208 | otherwise = lflag .&. complement (fromIntegral const_icanon)
209 poke_c_lflag p_tios (new_lflag :: CTcflag)
211 -- set VMIN & VTIME to 1/0 respectively
212 when (not cooked) $ do
213 c_cc <- ptr_c_cc p_tios
214 let vmin = (c_cc `plusPtr` (fromIntegral const_vmin)) :: Ptr Word8
215 vtime = (c_cc `plusPtr` (fromIntegral const_vtime)) :: Ptr Word8
219 tcSetAttr :: FD -> (Ptr CTermios -> IO a) -> IO a
220 tcSetAttr fd fun = do
221 allocaBytes sizeof_termios $ \p_tios -> do
222 throwErrnoIfMinus1Retry_ "tcSetAttr"
223 (c_tcgetattr fd p_tios)
225 #ifdef __GLASGOW_HASKELL__
226 -- Save a copy of termios, if this is a standard file descriptor.
227 -- These terminal settings are restored in hs_exit().
229 p <- get_saved_termios fd
230 when (p == nullPtr) $ do
231 saved_tios <- mallocBytes sizeof_termios
232 copyBytes saved_tios p_tios sizeof_termios
233 set_saved_termios fd saved_tios
236 -- tcsetattr() when invoked by a background process causes the process
237 -- to be sent SIGTTOU regardless of whether the process has TOSTOP set
238 -- in its terminal flags (try it...). This function provides a
239 -- wrapper which temporarily blocks SIGTTOU around the call, making it
241 allocaBytes sizeof_sigset_t $ \ p_sigset -> do
242 allocaBytes sizeof_sigset_t $ \ p_old_sigset -> do
243 throwErrnoIfMinus1_ "sigemptyset" $
244 c_sigemptyset p_sigset
245 throwErrnoIfMinus1_ "sigaddset" $
246 c_sigaddset p_sigset const_sigttou
247 throwErrnoIfMinus1_ "sigprocmask" $
248 c_sigprocmask const_sig_block p_sigset p_old_sigset
249 r <- fun p_tios -- do the business
250 throwErrnoIfMinus1Retry_ "tcSetAttr" $
251 c_tcsetattr fd const_tcsanow p_tios
252 throwErrnoIfMinus1_ "sigprocmask" $
253 c_sigprocmask const_sig_setmask p_old_sigset nullPtr
256 #ifdef __GLASGOW_HASKELL__
257 foreign import ccall unsafe "HsBase.h __hscore_get_saved_termios"
258 get_saved_termios :: CInt -> IO (Ptr CTermios)
260 foreign import ccall unsafe "HsBase.h __hscore_set_saved_termios"
261 set_saved_termios :: CInt -> (Ptr CTermios) -> IO ()
266 -- 'raw' mode for Win32 means turn off 'line input' (=> buffering and
267 -- character translation for the console.) The Win32 API for doing
268 -- this is GetConsoleMode(), which also requires echoing to be disabled
269 -- when turning off 'line input' processing. Notice that turning off
270 -- 'line input' implies enter/return is reported as '\r' (and it won't
271 -- report that character until another character is input..odd.) This
272 -- latter feature doesn't sit too well with IO actions like IO.hGetLine..
273 -- consider yourself warned.
274 setCooked :: FD -> Bool -> IO ()
275 setCooked fd cooked = do
276 x <- set_console_buffering fd (if cooked then 1 else 0)
278 then ioError (ioe_unk_error "setCooked" "failed to set buffering")
281 ioe_unk_error :: String -> String -> IOException
282 ioe_unk_error loc msg
284 = ioeSetErrorString (mkIOError OtherError loc Nothing Nothing) msg
289 -- Note: echoing goes hand in hand with enabling 'line input' / raw-ness
290 -- for Win32 consoles, hence setEcho ends up being the inverse of setCooked.
291 setEcho :: FD -> Bool -> IO ()
293 x <- set_console_echo fd (if on then 1 else 0)
295 then ioError (ioe_unk_error "setEcho" "failed to set echoing")
298 getEcho :: FD -> IO Bool
300 r <- get_console_echo fd
302 then ioError (ioe_unk_error "getEcho" "failed to get echoing")
305 foreign import ccall unsafe "consUtils.h set_console_buffering__"
306 set_console_buffering :: CInt -> CInt -> IO CInt
308 foreign import ccall unsafe "consUtils.h set_console_echo__"
309 set_console_echo :: CInt -> CInt -> IO CInt
311 foreign import ccall unsafe "consUtils.h get_console_echo__"
312 get_console_echo :: CInt -> IO CInt
314 foreign import ccall unsafe "consUtils.h is_console__"
315 is_console :: CInt -> IO CInt
319 -- ---------------------------------------------------------------------------
320 -- Turning on non-blocking for a file descriptor
322 setNonBlockingFD :: FD -> Bool -> IO ()
323 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
324 setNonBlockingFD fd set = do
325 flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
326 (c_fcntl_read fd const_f_getfl)
327 let flags' | set = flags .|. o_NONBLOCK
328 | otherwise = flags .&. complement o_NONBLOCK
329 unless (flags == flags') $ do
330 -- An error when setting O_NONBLOCK isn't fatal: on some systems
331 -- there are certain file handles on which this will fail (eg. /dev/null
332 -- on FreeBSD) so we throw away the return code from fcntl_write.
333 _ <- c_fcntl_write fd const_f_setfl (fromIntegral flags')
337 -- bogus defns for win32
338 setNonBlockingFD _ _ = return ()
342 -- -----------------------------------------------------------------------------
343 -- Set close-on-exec for a file descriptor
345 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
346 setCloseOnExec :: FD -> IO ()
347 setCloseOnExec fd = do
348 throwErrnoIfMinus1_ "setCloseOnExec" $
349 c_fcntl_write fd const_f_setfd const_fd_cloexec
352 -- -----------------------------------------------------------------------------
355 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
356 type CFilePath = CString
358 type CFilePath = CWString
361 foreign import ccall unsafe "HsBase.h access"
362 c_access :: CString -> CInt -> IO CInt
364 foreign import ccall unsafe "HsBase.h chmod"
365 c_chmod :: CString -> CMode -> IO CInt
367 foreign import ccall unsafe "HsBase.h close"
368 c_close :: CInt -> IO CInt
370 foreign import ccall unsafe "HsBase.h creat"
371 c_creat :: CString -> CMode -> IO CInt
373 foreign import ccall unsafe "HsBase.h dup"
374 c_dup :: CInt -> IO CInt
376 foreign import ccall unsafe "HsBase.h dup2"
377 c_dup2 :: CInt -> CInt -> IO CInt
379 foreign import ccall unsafe "HsBase.h __hscore_fstat"
380 c_fstat :: CInt -> Ptr CStat -> IO CInt
382 foreign import ccall unsafe "HsBase.h isatty"
383 c_isatty :: CInt -> IO CInt
385 #if defined(mingw32_HOST_OS) || defined(__MINGW32__)
386 foreign import ccall unsafe "HsBase.h __hscore_lseek"
387 c_lseek :: CInt -> Int64 -> CInt -> IO Int64
389 foreign import ccall unsafe "HsBase.h __hscore_lseek"
390 c_lseek :: CInt -> COff -> CInt -> IO COff
393 foreign import ccall unsafe "HsBase.h __hscore_lstat"
394 lstat :: CFilePath -> Ptr CStat -> IO CInt
396 foreign import ccall unsafe "HsBase.h __hscore_open"
397 c_open :: CFilePath -> CInt -> CMode -> IO CInt
399 foreign import ccall unsafe "HsBase.h read"
400 c_read :: CInt -> Ptr Word8 -> CSize -> IO CSsize
402 foreign import ccall safe "HsBase.h read"
403 c_safe_read :: CInt -> Ptr Word8 -> CSize -> IO CSsize
405 foreign import ccall unsafe "HsBase.h __hscore_stat"
406 c_stat :: CFilePath -> Ptr CStat -> IO CInt
408 foreign import ccall unsafe "HsBase.h umask"
409 c_umask :: CMode -> IO CMode
411 foreign import ccall unsafe "HsBase.h write"
412 c_write :: CInt -> Ptr Word8 -> CSize -> IO CSsize
414 foreign import ccall safe "HsBase.h write"
415 c_safe_write :: CInt -> Ptr Word8 -> 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 __hscore_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
471 foreign import ccall unsafe "HsBase.h __hscore_o_rdonly" o_RDONLY :: CInt
472 foreign import ccall unsafe "HsBase.h __hscore_o_wronly" o_WRONLY :: CInt
473 foreign import ccall unsafe "HsBase.h __hscore_o_rdwr" o_RDWR :: CInt
474 foreign import ccall unsafe "HsBase.h __hscore_o_append" o_APPEND :: CInt
475 foreign import ccall unsafe "HsBase.h __hscore_o_creat" o_CREAT :: CInt
476 foreign import ccall unsafe "HsBase.h __hscore_o_excl" o_EXCL :: CInt
477 foreign import ccall unsafe "HsBase.h __hscore_o_trunc" o_TRUNC :: CInt
480 foreign import ccall unsafe "HsBase.h __hscore_o_noctty" o_NOCTTY :: CInt
481 foreign import ccall unsafe "HsBase.h __hscore_o_nonblock" o_NONBLOCK :: CInt
482 foreign import ccall unsafe "HsBase.h __hscore_o_binary" o_BINARY :: CInt
484 foreign import ccall unsafe "HsBase.h __hscore_s_isreg" c_s_isreg :: CMode -> CInt
485 foreign import ccall unsafe "HsBase.h __hscore_s_ischr" c_s_ischr :: CMode -> CInt
486 foreign import ccall unsafe "HsBase.h __hscore_s_isblk" c_s_isblk :: CMode -> CInt
487 foreign import ccall unsafe "HsBase.h __hscore_s_isdir" c_s_isdir :: CMode -> CInt
488 foreign import ccall unsafe "HsBase.h __hscore_s_isfifo" c_s_isfifo :: CMode -> CInt
490 s_isreg :: CMode -> Bool
491 s_isreg cm = c_s_isreg cm /= 0
492 s_ischr :: CMode -> Bool
493 s_ischr cm = c_s_ischr cm /= 0
494 s_isblk :: CMode -> Bool
495 s_isblk cm = c_s_isblk cm /= 0
496 s_isdir :: CMode -> Bool
497 s_isdir cm = c_s_isdir cm /= 0
498 s_isfifo :: CMode -> Bool
499 s_isfifo cm = c_s_isfifo cm /= 0
501 foreign import ccall unsafe "HsBase.h __hscore_sizeof_stat" sizeof_stat :: Int
502 foreign import ccall unsafe "HsBase.h __hscore_st_mtime" st_mtime :: Ptr CStat -> IO CTime
503 #ifdef mingw32_HOST_OS
504 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO Int64
506 foreign import ccall unsafe "HsBase.h __hscore_st_size" st_size :: Ptr CStat -> IO COff
508 foreign import ccall unsafe "HsBase.h __hscore_st_mode" st_mode :: Ptr CStat -> IO CMode
509 foreign import ccall unsafe "HsBase.h __hscore_st_dev" st_dev :: Ptr CStat -> IO CDev
510 foreign import ccall unsafe "HsBase.h __hscore_st_ino" st_ino :: Ptr CStat -> IO CIno
512 foreign import ccall unsafe "HsBase.h __hscore_echo" const_echo :: CInt
513 foreign import ccall unsafe "HsBase.h __hscore_tcsanow" const_tcsanow :: CInt
514 foreign import ccall unsafe "HsBase.h __hscore_icanon" const_icanon :: CInt
515 foreign import ccall unsafe "HsBase.h __hscore_vmin" const_vmin :: CInt
516 foreign import ccall unsafe "HsBase.h __hscore_vtime" const_vtime :: CInt
517 foreign import ccall unsafe "HsBase.h __hscore_sigttou" const_sigttou :: CInt
518 foreign import ccall unsafe "HsBase.h __hscore_sig_block" const_sig_block :: CInt
519 foreign import ccall unsafe "HsBase.h __hscore_sig_setmask" const_sig_setmask :: CInt
520 foreign import ccall unsafe "HsBase.h __hscore_f_getfl" const_f_getfl :: CInt
521 foreign import ccall unsafe "HsBase.h __hscore_f_setfl" const_f_setfl :: CInt
522 foreign import ccall unsafe "HsBase.h __hscore_f_setfd" const_f_setfd :: CInt
523 foreign import ccall unsafe "HsBase.h __hscore_fd_cloexec" const_fd_cloexec :: CLong
525 #if defined(HTYPE_TCFLAG_T)
526 foreign import ccall unsafe "HsBase.h __hscore_sizeof_termios" sizeof_termios :: Int
527 foreign import ccall unsafe "HsBase.h __hscore_sizeof_sigset_t" sizeof_sigset_t :: Int
529 foreign import ccall unsafe "HsBase.h __hscore_lflag" c_lflag :: Ptr CTermios -> IO CTcflag
530 foreign import ccall unsafe "HsBase.h __hscore_poke_lflag" poke_c_lflag :: Ptr CTermios -> CTcflag -> IO ()
531 foreign import ccall unsafe "HsBase.h __hscore_ptr_c_cc" ptr_c_cc :: Ptr CTermios -> IO (Ptr Word8)
534 s_issock :: CMode -> Bool
535 #if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
536 s_issock cmode = c_s_issock cmode /= 0
537 foreign import ccall unsafe "HsBase.h __hscore_s_issock" c_s_issock :: CMode -> CInt
542 foreign import ccall unsafe "__hscore_bufsiz" dEFAULT_BUFFER_SIZE :: Int
543 foreign import ccall unsafe "__hscore_seek_cur" sEEK_CUR :: CInt
544 foreign import ccall unsafe "__hscore_seek_set" sEEK_SET :: CInt
545 foreign import ccall unsafe "__hscore_seek_end" sEEK_END :: CInt