1 %************************************************************************
3 <sect1>The Posix library
4 <label id="Posix-library">
6 <nidx>Posix library</nidx>
7 <nidx>libraries, Posix</nidx>
9 %************************************************************************
11 The @Posix@ interface gives you access to the set of OS services
12 standardised by POSIX 1003.1b (or the <em>IEEE Portable Operating System
13 Interface for Computing Environments</em> - IEEE Std. 1003.1). The
14 interface is accessed by @import Posix@ and adding
15 @-syslib posix@ on your command-line.
17 <sect2>Posix data types
18 <label id="Posix data types">
20 <nidx>Posix, data types</nidx>
24 data ByteCount -- instances of : Eq Ord Num Real Integral Ix Enum Show
27 A @ByteCount@ is a primitive of type @unsigned@. At a minimum,
28 an conforming implementation must support values in the range
32 data ClockTick -- instances of : Eq Ord Num Real Integral Ix Enum Show
35 A @ClockTick@ is a primitive of type @clock_t@, which
36 is used to measure intervals of time in fractions of a second. The
37 resolution is determined by @getSysVar ClockTick@.
40 data DeviceID -- instances of : Eq Ord Num Real Integral Ix Enum Show
43 A @DeviceID@ is a primitive of type @dev_t@. It must
44 be an arithmetic type.
47 data EpochTime -- instances of : Eq Ord Num Real Integral Ix Enum Show
50 A @EpochTime@ is a primitive of type @time_t@, which is
51 used to measure seconds since the Epoch. At a minimum, the implementation
52 must support values in the range @[0, INT_MAX]@.
55 data FileID -- instances of : Eq Ord Num Real Integral Ix Enum Show
58 A @FileID@ is a primitive of type @ino_t@. It must
59 be an arithmetic type.
62 data FileMode -- instances of : Eq Ord Num Real Integral Ix Enum Show
65 A @FileMode@ is a primitive of type @mode_t@.
66 It must be an arithmetic type.
69 data FileOffset -- instances of : Eq Ord Num Real Integral Ix Enum Show
72 A @FileOffset@ is a primitive of type @off_t@. It must
73 be an arithmetic type.
76 data GroupID -- instances of : Eq Ord Num Real Integral Ix Enum Show
79 A @GroupID@ is a primitive of type @gid_t@. It must
80 be an arithmetic type.
82 data Limit -- instances of : Eq Ord Num Real Integral Ix Enum Show
85 A @Limit@ is a primitive of type @long@.
86 At a minimum, the implementation must support values in the range
87 @[LONG_MIN, LONG_MAX]@.
90 data LinkCount -- instances of : Eq Ord Num Real Integral Ix Enum Show
93 A @LinkCount@ is a primitive of type @nlink_t@. It must
94 be an arithmetic type.
97 data ProcessID -- instances of : Eq Ord Num Real Integral Ix Enum Show
98 type ProcessGroupID = ProcessID
101 A @ProcessID@ is a primitive of type @pid_t@. It
102 must be a signed arithmetic type.
104 data UserID -- instances of : Eq Ord Num Real Integral Ix Enum Show
107 A @UserID@ is a primitive of type @uid_t@. It
108 must be an arithmetic type.
113 A @DirStream@ is a primitive of type @DIR *@.
118 A @FileStatus@ is a primitive of type @struct stat@.
124 A @GroupEntry@ is a primitive of type @struct group@.
129 @ProcessTimes@ is a primitive structure containing a
130 @clock_t@ and a @struct tms@.
136 An @SignalSet@ is a primitive of type @sigset_t@.
142 A @SystemID@ is a primitive of type @struct utsname@.
145 data TerminalAttributes
147 @TerminalAttributes@ is a primitive of type @struct termios@.
153 A @UserEntry@ is a primitive of type @struct passwd@.
156 data BaudRate = B0 | B50 | B75 | B110 | B134 | B150 | B200 | B300 | B600
157 | B1200 | B1800 | B2400 | B4800 | B9600 | B19200 | B38400
162 intToFd :: Int -> Fd -- use with care.
164 data FdOption = AppendOnWrite
168 data ControlCharacter = EndOfFile
180 type FileLock = (LockRequest, SeekMode, FileOffset, FileOffset)
181 -- whence start length
183 data FlowAction = SuspendOutput | RestartOutput | TransmitStop | TransmitStart
185 data Handler = Default | Ignore | Catch (IO ())
187 data LockRequest = ReadLock | WriteLock | Unlock
190 data OpenMode = ReadOnly | WriteOnly | ReadWrite
192 data PathVar = LinkLimit
198 | SetOwnerAndGroupIsRestricted
199 | FileNamesAreNotTruncated
201 data QueueSelector = InputQueue | OutputQueue | BothQueues
205 data SysVar = ArgumentLimit
214 data TerminalMode = InterruptOnBreak -- BRKINT
216 | IgnoreBreak -- IGNBRK
218 | IgnoreParityErrors -- IGNPAR
220 | CheckParity -- INPCK
221 | StripHighBit -- ISTRIP
222 | StartStopInput -- IXOFF
223 | StartStopOutput -- IXON
224 | MarkParityErrors -- PARMRK
225 | ProcessOutput -- OPOST
226 | LocalMode -- CLOCAL
227 | ReadEnable -- CREAD
228 | TwoStopBits -- CSTOPB
229 | HangupOnClose -- HUPCL
230 | EnableParity -- PARENB
231 | OddParity -- PARODD
236 | ProcessInput -- ICANON
237 | ExtendedFunctions -- IEXTEN
238 | KeyboardInterrupts -- ISIG
239 | NoFlushOnInterrupt -- NOFLSH
240 | BackgroundWriteInterrupt -- TOSTOP
242 data TerminalState = Immediately | WhenDrained | WhenFlushed
244 data ProcessStatus = Exited ExitCode
250 <sect2>Posix Process Primitives
251 <label id="Process Primitives">
255 forkProcess :: IO (Maybe ProcessID)
258 @forkProcess@ calls @fork@, returning
259 @Just pid@ to the parent, where @pid@ is the
260 ProcessID of the child, and returning @Nothing@ to the
264 executeFile :: FilePath -- Command
265 -> Bool -- Search PATH?
266 -> [String] -- Arguments
267 -> Maybe [(String, String)] -- Environment
271 @executeFile cmd args env@ calls one of the
272 @execv*@ family, depending on whether or not the current
273 PATH is to be searched for the command, and whether or not an
274 environment is provided to supersede the process's current
275 environment. The basename (leading directory names suppressed) of
276 the command is passed to @execv*@ as @arg[0]@;
277 the argument list passed to @executeFile@ therefore begins
281 Search PATH? Supersede environ? Call
282 ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~ ~~~~~~~
289 Note that @execvpe@ is not provided by the POSIX standard, and must
290 be written by hand. Care must be taken to ensure that the search path
291 is extracted from the original environment, and not from the
292 environment to be passed on to the new image.
294 NOTE: In general, sharing open files between parent and child
295 processes is potential bug farm, and should be avoided unless you
296 really depend on this `feature' of POSIX' @fork()@ semantics. Using
297 Haskell, there's the extra complication that arguments to
298 @executeFile@ might come from files that are read lazily (using
299 @hGetContents@, or some such.) If this is the case, then for your own
300 sanity, please ensure that the arguments to @executeFile@ have been
301 fully evaluated before calling @forkProcess@ (followed by
302 @executeFile@.) Consider yourself warned :-)
304 A successful @executeFile@ overlays the current process image with
305 a new one, so it only returns on failure.
308 runProcess :: FilePath -- Command
309 -> [String] -- Arguments
310 -> Maybe [(String, String)] -- Environment (Nothing -> Inherited)
311 -> Maybe FilePath -- Working directory (Nothing -> inherited)
312 -> Maybe Handle -- stdin (Nothing -> inherited)
313 -> Maybe Handle -- stdout (Nothing -> inherited)
314 -> Maybe Handle -- stderr (Nothing -> inherited)
318 @runProcess@ is our candidate for the high-level OS-independent
321 @runProcess cmd args env wd inhdl outhdl errhdl@ runs @cmd@
322 (searching the current @PATH@) with arguments @args@. If
323 @env@ is @Just pairs@, the command is executed with the
324 environment specified by @pairs@ of variables and values;
325 otherwise, the command is executed with the current environment. If
326 @wd@ is @Just dir@, the command is executed with working
327 directory @dir@; otherwise, the command is executed in the current
328 working directory. If @{in,out,err@hdl} is @Just handle@, the
329 command is executed with the @Fd@ for @std{in,out,err@}
330 attached to the specified @handle@; otherwise, the @Fd@ for
331 @std{in,out,err@} is left unchanged.
334 getProcessStatus :: Bool -- Block?
335 -> Bool -- Stopped processes?
337 -> IO (Maybe ProcessStatus)
340 @getProcessStatus blk stopped pid@ calls @waitpid@, returning
341 @Just tc@, the @ProcessStatus@ for process @pid@ if it is
342 available, @Nothing@ otherwise. If @blk@ is @False@, then
343 @WNOHANG@ is set in the options for @waitpid@, otherwise not.
344 If @stopped@ is @True@, then @WUNTRACED@ is set in the
345 options for @waitpid@, otherwise not.
348 getGroupProcessStatus :: Bool -- Block?
349 -> Bool -- Stopped processes?
351 -> IO (Maybe (ProcessID, ProcessStatus))
354 @getGroupProcessStatus blk stopped pgid@ calls @waitpid@,
355 returning @Just (pid, tc)@, the @ProcessID@ and
356 @ProcessStatus@ for any process in group @pgid@ if one is
357 available, @Nothing@ otherwise. If @blk@ is @False@, then
358 @WNOHANG@ is set in the options for @waitpid@, otherwise not.
359 If @stopped@ is @True@, then @WUNTRACED@ is set in the
360 options for @waitpid@, otherwise not.
363 getAnyProcessStatus :: Bool -- Block?
364 -> Bool -- Stopped processes?
365 -> IO (Maybe (ProcessID, ProcessStatus))
368 @getAnyProcessStatus blk stopped@ calls @waitpid@, returning
369 @Just (pid, tc)@, the @ProcessID@ and @ProcessStatus@ for any
370 child process if one is available, @Nothing@ otherwise. If
371 @blk@ is @False@, then @WNOHANG@ is set in the options for
372 @waitpid@, otherwise not. If @stopped@ is @True@, then
373 @WUNTRACED@ is set in the options for @waitpid@, otherwise not.
376 exitImmediately :: ExitCode -> IO ()
379 @exitImmediately status@ calls @_exit@ to terminate the process
380 with the indicated exit @status@.
381 The operation never returns.
384 getEnvironment :: IO [(String, String)]
387 @getEnvironment@ parses the environment variable mapping provided by
388 @environ@, returning @(variable, value)@ pairs.
389 The operation never fails.
392 setEnvironment :: [(String, String)] -> IO ()
395 @setEnvironment@ replaces the process environment with the provided
396 mapping of @(variable, value)@ pairs.
399 getEnvVar :: String -> IO String
402 @getEnvVar var@ returns the value associated with variable @var@
403 in the current environment (identical functionality provided through
404 standard Haskell library function @System.getEnv@).
406 The operation may fail with:
409 <tag>@NoSuchThing@</tag>
410 The variable has no mapping in the current environment.
414 setEnvVar :: String -> String -> IO ()
417 @setEnvVar var val@ sets the value associated with variable @var@
418 in the current environment to be @val@. Any previous mapping is
422 removeEnvVar :: String -> IO ()
425 @removeEnvVar var@ removes any value associated with variable @var@
426 in the current environment. Deleting a variable for which there is no mapping
427 does not generate an error.
433 backgroundRead, sigTTIN :: Signal
434 backgroundWrite, sigTTOU :: Signal
435 continueProcess, sigCONT :: Signal
436 floatingPointException, sigFPE :: Signal
437 illegalInstruction, sigILL :: Signal
438 internalAbort, sigABRT :: Signal
439 keyboardSignal, sigINT :: Signal
440 keyboardStop, sigTSTP :: Signal
441 keyboardTermination, sigQUIT :: Signal
442 killProcess, sigKILL :: Signal
443 lostConnection, sigHUP :: Signal
444 openEndedPipe, sigPIPE :: Signal
445 processStatusChanged, sigCHLD :: Signal
446 realTimeAlarm, sigALRM :: Signal
447 segmentationViolation, sigSEGV :: Signal
448 softwareStop, sigSTOP :: Signal
449 softwareTermination, sigTERM :: Signal
450 userDefinedSignal1, sigUSR1 :: Signal
451 userDefinedSignal2, sigUSR2 :: Signal
453 signalProcess :: Signal -> ProcessID -> IO ()
456 @signalProcess int pid@ calls @kill@ to signal
457 process @pid@ with interrupt signal @int@.
460 raiseSignal :: Signal -> IO ()
463 @raiseSignal int@ calls @kill@ to signal the current process
464 with interrupt signal @int@.
467 signalProcessGroup :: Signal -> ProcessGroupID -> IO ()
470 @signalProcessGroup int pgid@ calls @kill@ to signal
471 all processes in group @pgid@ with interrupt signal @int@.
474 setStoppedChildFlag :: Bool -> IO Bool
477 @setStoppedChildFlag bool@ sets a flag which controls whether or
478 not the @NOCLDSTOP@ option will be used the next time a signal
479 handler is installed for @SIGCHLD@. If @bool@ is @True@ (the
480 default), @NOCLDSTOP@ will not be used; otherwise it will be. The
481 operation never fails.
484 queryStoppedChildFlag :: IO Bool
487 @queryStoppedChildFlag@ queries the flag which
488 controls whether or not the @NOCLDSTOP@ option will be used
489 the next time a signal handler is installed for @SIGCHLD@.
490 If @NOCLDSTOP@ will be used, it returns @False@;
491 otherwise (the default) it returns @True@.
492 The operation never fails.
495 emptySignalSet :: SignalSet
496 fullSignalSet :: SignalSet
497 addSignal :: Signal -> SignalSet -> SignalSet
498 deleteSignal :: Signal -> SignalSet -> SignalSet
499 inSignalSet :: Signal -> SignalSet -> Bool
501 installHandler :: Signal
503 -> Maybe SignalSet -- other signals to block
504 -> IO Handler -- old handler
507 @installHandler int handler iset@ calls @sigaction@ to install an
508 interrupt handler for signal @int@. If @handler@ is @Default@,
509 @SIG_DFL@ is installed; if @handler@ is @Ignore@, @SIG_IGN@ is
510 installed; if @handler@ is @Catch action@, a handler is installed
511 which will invoke @action@ in a new thread when (or shortly after) the
512 signal is received. See Section <ref name="Concurrent Haskell"
513 id="concurrent-haskell"> for details on how to communicate between
516 If @iset@ is @Just s@, then the @sa_mask@ of the @sigaction@ structure
517 is set to @s@; otherwise it is cleared. The previously installed
518 signal handler for @int@ is returned.
521 getSignalMask :: IO SignalSet
524 @getSignalMask@ calls @sigprocmask@ to determine the
525 set of interrupts which are currently being blocked.
528 setSignalMask :: SignalSet -> IO SignalSet
531 @setSignalMask mask@ calls @sigprocmask@ with
532 @SIG_SETMASK@ to block all interrupts in @mask@. The
533 previous set of blocked interrupts is returned.
536 blockSignals :: SignalSet -> IO SignalSet
539 @setSignalMask mask@ calls @sigprocmask@ with
540 @SIG_BLOCK@ to add all interrupts in @mask@ to the
541 set of blocked interrupts. The previous set of blocked interrupts is returned.
544 unBlockSignals :: SignalSet -> IO SignalSet
547 @setSignalMask mask@ calls @sigprocmask@ with
548 @SIG_UNBLOCK@ to remove all interrupts in @mask@ from the
549 set of blocked interrupts. The previous set of blocked interrupts is returned.
552 getPendingSignals :: IO SignalSet
555 @getPendingSignals@ calls @sigpending@ to obtain
556 the set of interrupts which have been received but are currently blocked.
559 awaitSignal :: Maybe SignalSet -> IO ()
562 @awaitSignal iset@ suspends execution until an interrupt is received.
563 If @iset@ is @Just s@, @awaitSignal@ calls @sigsuspend@, installing
564 @s@ as the new signal mask before suspending execution; otherwise, it
565 calls @pause@. @awaitSignal@ returns on receipt of a signal. If you
566 have installed any signal handlers with @installHandler@, it may be
567 wise to call @yield@ directly after @awaitSignal@ to ensure that the
568 signal handler runs as promptly.
571 scheduleAlarm :: Int -> IO Int
574 @scheduleAlarm i@ calls @alarm@ to schedule a real time
575 alarm at least @i@ seconds in the future.
578 sleep :: Int -> IO ()
581 @sleep i@ calls @sleep@ to suspend execution of the
582 program until at least @i@ seconds have elapsed or a signal is
585 <sect2>Posix Process Environment
586 <label id="Process Environment">
588 <nidx>Posix, process environment</nidx>
591 getProcessID :: IO ProcessID
594 @getProcessID@ calls @getpid@ to obtain the @ProcessID@ for
598 getParentProcessID :: IO ProcessID
601 @getProcessID@ calls @getppid@ to obtain the @ProcessID@ for
602 the parent of the current process.
605 getRealUserID :: IO UserID
608 @getRealUserID@ calls @getuid@ to obtain the real @UserID@
609 associated with the current process.
612 getEffectiveUserID :: IO UserID
615 @getRealUserID@ calls @geteuid@ to obtain the effective
616 @UserID@ associated with the current process.
619 setUserID :: UserID -> IO ()
622 @setUserID uid@ calls @setuid@ to set the real, effective, and
623 saved set-user-id associated with the current process to @uid@.
626 getLoginName :: IO String
629 @getLoginName@ calls @getlogin@ to obtain the login name
630 associated with the current process.
633 getRealGroupID :: IO GroupID
636 @getRealGroupID@ calls @getgid@ to obtain the real @GroupID@
637 associated with the current process.
640 getEffectiveGroupID :: IO GroupID
643 @getEffectiveGroupID@ calls @getegid@ to obtain the effective
644 @GroupID@ associated with the current process.
647 setGroupID :: GroupID -> IO ()
650 @setGroupID gid@ calls @setgid@ to set the real, effective, and
651 saved set-group-id associated with the current process to @gid@.
654 getGroups :: IO [GroupID]
657 @getGroups@ calls @getgroups@ to obtain the list of
658 supplementary @GroupID@s associated with the current process.
661 getEffectiveUserName :: IO String
664 @getEffectiveUserName@ calls @cuserid@ to obtain a name
665 associated with the effective @UserID@ of the process.
668 getProcessGroupID :: IO ProcessGroupID
671 @getProcessGroupID@ calls @getpgrp@ to obtain the
672 @ProcessGroupID@ for the current process.
675 createProcessGroup :: ProcessID -> IO ProcessGroupID
678 @createProcessGroup pid@ calls @setpgid@ to make
679 process @pid@ a new process group leader.
682 joinProcessGroup :: ProcessGroupID -> IO ProcessGroupID
685 @joinProcessGroup pgid@ calls @setpgid@ to set the
686 @ProcessGroupID@ of the current process to @pgid@.
689 setProcessGroupID :: ProcessID -> ProcessGroupID -> IO ()
692 @setProcessGroupID pid pgid@ calls @setpgid@ to set the
693 @ProcessGroupID@ for process @pid@ to @pgid@.
696 createSession :: IO ProcessGroupID
699 @createSession@ calls @setsid@ to create a new session
700 with the current process as session leader.
703 systemName :: SystemID -> String
704 nodeName :: SystemID -> String
705 release :: SystemID -> String
706 version :: SystemID -> String
707 machine :: SystemID -> String
709 getSystemID :: IO SystemID
712 @getSystemID@ calls @uname@ to obtain information
713 about the current operating system.
716 > epochTime :: IO EpochTime
719 @epochTime@ calls @time@ to obtain the number of
720 seconds that have elapsed since the epoch (Jan 01 00:00:00 GMT 1970).
723 elapsedTime :: ProcessTimes -> ClockTick
724 userTime :: ProcessTimes -> ClockTick
725 systemTime :: ProcessTimes -> ClockTick
726 childUserTime :: ProcessTimes -> ClockTick
727 childSystemTime :: ProcessTimes -> ClockTick
729 getProcessTimes :: IO ProcessTimes
732 @getProcessTimes@ calls @times@ to obtain time-accounting
733 information for the current process and its children.
736 getControllingTerminalName :: IO FilePath
739 @getControllingTerminalName@ calls @ctermid@ to obtain
740 a name associated with the controlling terminal for the process. If a
741 controlling terminal exists,
742 @getControllingTerminalName@ returns the name of the
743 controlling terminal.
745 The operation may fail with:
748 <tag>@NoSuchThing@</tag>
749 There is no controlling terminal, or its name cannot be determined.
750 <tag>@SystemError@</tag>
751 Various other causes.
755 getTerminalName :: Fd -> IO FilePath
758 @getTerminalName fd@ calls @ttyname@ to obtain a name associated
759 with the terminal for @Fd@ @fd@. If @fd@ is associated
760 with a terminal, @getTerminalName@ returns the name of the
763 The operation may fail with:
766 <tag>@InappropriateType@</tag>
767 The channel is not associated with a terminal.
768 <tag>@NoSuchThing@</tag>
769 The channel is associated with a terminal, but it has no name.
770 <tag>@SystemError@</tag>
771 Various other causes.
775 queryTerminal :: Fd -> IO Bool
778 @queryTerminal fd@ calls @isatty@ to determine whether or
779 not @Fd@ @fd@ is associated with a terminal.
782 getSysVar :: SysVar -> IO Limit
785 @getSysVar var@ calls @sysconf@ to obtain the
786 dynamic value of the requested configurable system limit or option.
787 For defined system limits, @getSysVar@ returns the associated
788 value. For defined system options, the result of @getSysVar@
789 is undefined, but not failure.
791 The operation may fail with:
794 <tag>@NoSuchThing@</tag>
795 The requested system limit or option is undefined.
798 <sect2>Posix operations on files and directories
799 <label id="Files and Directories">
801 <nidx>Posix, files and directories</nidx>
804 openDirStream :: FilePath -> IO DirStream
807 @openDirStream dir@ calls @opendir@ to obtain a
808 directory stream for @dir@.
811 readDirStream :: DirStream -> IO String
814 @readDirStream dp@ calls @readdir@ to obtain the
815 next directory entry (@struct dirent@) for the open directory
816 stream @dp@, and returns the @d_name@ member of that
819 The operation may fail with:
823 End of file has been reached.
824 <tag>@SystemError@</tag>
825 Various other causes.
829 rewindDirStream :: DirStream -> IO ()
832 @rewindDirStream dp@ calls @rewinddir@ to reposition
833 the directory stream @dp@ at the beginning of the directory.
836 closeDirStream :: DirStream -> IO ()
839 @closeDirStream dp@ calls @closedir@ to close
840 the directory stream @dp@.
843 getWorkingDirectory :: IO FilePath
846 @getWorkingDirectory@ calls @getcwd@ to obtain the name
847 of the current working directory.
850 changeWorkingDirectory :: FilePath -> IO ()
853 @changeWorkingDirectory dir@ calls @chdir@ to change
854 the current working directory to @dir@.
857 nullFileMode :: FileMode -- ---------
858 ownerReadMode :: FileMode -- r--------
859 ownerWriteMode :: FileMode -- -w-------
860 ownerExecuteMode :: FileMode -- --x------
861 groupReadMode :: FileMode -- ---r-----
862 groupWriteMode :: FileMode -- ----w----
863 groupExecuteMode :: FileMode -- -----x---
864 otherReadMode :: FileMode -- ------r--
865 otherWriteMode :: FileMode -- -------w-
866 otherExecuteMode :: FileMode -- --------x
867 setUserIDMode :: FileMode -- --S------
868 setGroupIDMode :: FileMode -- -----S---
870 stdFileMode :: FileMode -- rw-rw-rw-
872 ownerModes :: FileMode -- rwx------
873 groupModes :: FileMode -- ---rwx---
874 otherModes :: FileMode -- ------rwx
875 accessModes :: FileMode -- rwxrwxrwx
877 unionFileModes :: FileMode -> FileMode -> FileMode
878 intersectFileModes :: FileMode -> FileMode -> FileMode
884 stdOutput = intToFd 1
900 -> Maybe FileMode -- Just x => O_CREAT, Nothing => must exist
905 @openFd path acc mode (OpenFileFlags app excl noctty nonblock trunc)@ calls
906 @open@ to obtain a @Fd@ for the file @path@ with access
907 mode @acc@. If @mode@ is @Just m@, the @O_CREAT@ flag is
908 set and the file's permissions will be based on @m@ if it does not
909 already exist; otherwise, the @O_CREAT@ flag is not set. The
910 arguments @app@, @excl@, @noctty@, @nonblock@, and
911 @trunc@ control whether or not the flags @O_APPEND@,
912 @O_EXCL@, @O_NOCTTY@, @O_NONBLOCK@, and @O_TRUNC@ are set,
916 createFile :: FilePath -> FileMode -> IO Fd
919 @createFile path mode@ calls @creat@ to obtain a @Fd@
920 for file @path@, which will be created with permissions based on
921 @mode@ if it does not already exist.
924 setFileCreationMask :: FileMode -> IO FileMode
927 @setFileCreationMask mode@ calls @umask@ to set
928 the process's file creation mask to @mode@. The previous file
929 creation mask is returned.
932 createLink :: FilePath -> FilePath -> IO ()
935 @createLink old new@ calls @link@ to create a
936 new path, @new@, linked to an existing file, @old@.
938 createDirectory :: FilePath -> FileMode -> IO ()
941 @createDirectory dir mode@ calls @mkdir@ to
942 create a new directory, @dir@, with permissions based on
946 createNamedPipe :: FilePath -> FileMode -> IO ()
949 @createNamedPipe fifo mode@ calls @mkfifo@ to
950 create a new named pipe, @fifo@, with permissions based on
954 removeLink :: FilePath -> IO ()
957 @removeLink path@ calls @unlink@ to remove the link
961 removeDirectory :: FilePath -> IO ()
964 @removeDirectory dir@ calls @rmdir@ to remove the
965 directory named @dir@.
968 rename :: FilePath -> FilePath -> IO ()
971 @rename old new@ calls @rename@ to rename a
972 file or directory from @old@ to @new@.
975 fileMode :: FileStatus -> FileMode
977 fileID :: FileStatus -> FileID
978 deviceID :: FileStatus -> DeviceID
980 linkCount :: FileStatus -> LinkCount
982 fileOwner :: FileStatus -> UserID
983 fileGroup :: FileStatus -> GroupID
984 fileSize :: FileStatus -> FileOffset
986 accessTime :: FileStatus -> EpochTime
987 modificationTime :: FileStatus -> EpochTime
988 statusChangeTime :: FileStatus -> EpochTime
990 isDirectory :: FileStatus -> Bool
991 isCharacterDevice :: FileStatus -> Bool
992 isBlockDevice :: FileStatus -> Bool
993 isRegularFile :: FileStatus -> Bool
994 isNamedPipe :: FileStatus -> Bool
996 getFileStatus :: FilePath -> IO FileStatus
999 @getFileStatus path@ calls @stat@ to get the
1000 @FileStatus@ information for the file @path@.
1003 getFdStatus :: Fd -> IO FileStatus
1006 @getFdStatus fd@ calls @fstat@ to get the
1007 @FileStatus@ information for the file associated with
1011 queryAccess :: FilePath -> Bool -> Bool -> Bool -> IO Bool
1014 @queryAccess path r w x@ calls @access@ to test the access
1015 permissions for file @path@. The three arguments, @r@, @w@,
1016 and @x@ control whether or not @access@ is called with
1017 @R_OK@, @W_OK@, and @X_OK@ respectively.
1020 queryFile :: FilePath -> IO Bool
1023 @queryFile path@ calls @access@ with @F_OK@ to test for the
1024 existence for file @path@.
1027 setFileMode :: FilePath -> FileMode -> IO ()
1030 @setFileMode path mode@ calls @chmod@ to set the
1031 permission bits associated with file @path@ to @mode@.
1034 setOwnerAndGroup :: FilePath -> UserID -> GroupID -> IO ()
1037 @setOwnerAndGroup path uid gid@ calls @chown@ to
1038 set the @UserID@ and @GroupID@ associated with file
1039 @path@ to @uid@ and @gid@, respectively.
1042 setFileTimes :: FilePath -> EpochTime -> EpochTime -> IO ()
1045 @setFileTimes path atime mtime@ calls @utime@ to
1046 set the access and modification times associated with file
1047 @path@ to @atime@ and @mtime@, respectively.
1050 touchFile :: FilePath -> IO ()
1053 @touchFile path@ calls @utime@ to
1054 set the access and modification times associated with file
1055 @path@ to the current time.
1058 getPathVar :: PathVar -> FilePath -> IO Limit
1061 @getPathVar var path@ calls @pathconf@ to obtain the
1062 dynamic value of the requested configurable file limit or option associated
1063 with file or directory @path@. For
1064 defined file limits, @getPathVar@ returns the associated
1065 value. For defined file options, the result of @getPathVar@
1066 is undefined, but not failure.
1067 The operation may fail with:
1069 <tag>@NoSuchThing@</tag>
1070 The requested file limit or option is undefined.
1071 <tag>@SystemError@</tag>
1072 Various other causes.
1077 getFdVar :: PathVar -> Fd -> IO Limit
1080 @getFdVar var fd@ calls @fpathconf@ to obtain the
1081 dynamic value of the requested configurable file limit or option associated
1082 with the file or directory attached to the open channel @fd@.
1083 For defined file limits, @getFdVar@ returns the associated
1084 value. For defined file options, the result of @getFdVar@
1085 is undefined, but not failure.
1087 The operation may fail with:
1090 <tag>@NoSuchThing@</tag>
1091 The requested file limit or option is undefined.
1092 <tag>@SystemError@</tag>
1093 Various other causes.
1096 <sect2>Posix Input and Output Primitives
1097 <label id="Input/Output">
1099 <nidx>Posix, input/output</nidx>
1102 createPipe :: IO (Fd, Fd)
1105 @createPipe@ calls @pipe@ to create a pipe and returns a pair of
1106 @Fd@s, the first for reading and the second for writing.
1112 @dup fd@ calls @dup@ to duplicate @Fd@ @fd@ to
1116 dupTo :: Fd -> Fd -> IO ()
1119 @dupTo src dst@ calls @dup2@ to duplicate @Fd@
1120 @src@ to @Fd@ @dst@.
1123 fdClose :: Fd -> IO ()
1126 @fdClose fd@ calls @close@ to close @Fd@ @fd@.
1129 fdRead :: Fd -> ByteCount -> IO (String, ByteCount)
1132 @fdRead fd nbytes@ calls @read@ to read at most @nbytes@
1133 bytes from @Fd@ @fd@, and returns the result as a string
1134 paired with the number of bytes actually read.
1136 The operation may fail with:
1140 End of file has been reached.
1141 <tag>@SystemError@</tag>
1142 Various other causes.
1146 fdWrite :: Fd -> String -> IO ByteCount
1149 @fdWrite fd s@ calls @write@ to write
1150 the string @s@ to @Fd@ @fd@ as a
1151 contiguous sequence of bytes. It returns the number of bytes successfully
1155 queryFdOption :: FdOption -> Fd -> IO Bool
1158 @getFdOption opt fd@ calls @fcntl@ to determine whether or
1159 not the flag associated with @FdOption@ @opt@ is set for
1163 setFdOption :: Fd -> FdOption -> Bool -> IO ()
1166 @setFdOption fd opt val@ calls @fcntl@ to set the flag
1167 associated with @FdOption@ @opt@ on @Fd@ @fd@ to
1171 getLock :: Fd -> FileLock -> IO (Maybe (ProcessID, FileLock))
1174 @getLock fd lock@ calls @fcntl@ to get the first @FileLock@
1175 for @Fd@ @fd@ which blocks the @FileLock@ @lock@. If
1176 no such @FileLock@ exists, @getLock@ returns @Nothing@.
1177 Otherwise, it returns @Just (pid, block)@, where @block@ is the
1178 blocking @FileLock@ and @pid@ is the @ProcessID@ of the
1179 process holding the blocking @FileLock@.
1183 setLock :: Fd -> FileLock -> IO ()
1186 @setLock fd lock@ calls @fcntl@ with @F_SETLK@ to set or
1187 clear a lock segment for @Fd@ @fd@ as indicated by the
1188 @FileLock@ @lock@. @setLock@ does not block, but fails with
1189 @SystemError@ if the request cannot be satisfied immediately.
1192 waitToSetLock :: Fd -> FileLock -> IO ()
1195 @waitToSetLock fd lock@ calls @fcntl@ with @F_SETLKW@ to set
1196 or clear a lock segment for @Fd@ @fd@ as indicated by the
1197 @FileLock@ @lock@. If the request cannot be satisfied
1198 immediately, @waitToSetLock@ blocks until the request can be
1203 fdSeek :: Fd -> SeekMode -> FileOffset -> IO FileOffset
1206 @fdSeek fd whence offset@ calls @lseek@ to position the
1207 @Fd@ @fd@ at the given @offset@ from the starting location
1208 indicated by @whence@. It returns the resulting offset from the
1209 start of the file in bytes.
1211 <sect2>Posix, Device- and Class-Specific Functions
1212 <label id="Device Specific Functions">
1214 <nidx>Posix, device and class-specific functions</nidx>
1217 terminalMode :: TerminalMode -> TerminalAttributes -> Bool
1218 withMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
1219 withoutMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
1221 bitsPerByte :: TerminalAttributes -> Int
1222 withBits :: TerminalAttributes -> Int -> TerminalAttributes
1224 controlChar :: TerminalAttributes -> ControlCharacter -> Maybe Char
1225 withCC :: TerminalAttributes
1226 -> (ControlCharacter, Char)
1227 -> TerminalAttributes
1228 withoutCC :: TerminalAttributes
1230 -> TerminalAttributes
1232 inputTime :: TerminalAttributes -> Int
1233 withTime :: TerminalAttributes -> Int -> TerminalAttributes
1235 minInput :: TerminalAttributes -> Int
1236 withMinInput :: TerminalAttributes -> Int -> TerminalAttributes
1238 inputSpeed :: TerminalAttributes -> BaudRate
1239 withInputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
1241 outputSpeed :: TerminalAttributes -> BaudRate
1242 withOutputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
1244 getTerminalAttributes :: Fd -> IO TerminalAttributes
1247 @getTerminalAttributes fd@ calls @tcgetattr@ to obtain
1248 the @TerminalAttributes@ associated with @Fd@ @fd@.
1251 setTerminalAttributes :: Fd
1252 -> TerminalAttributes
1257 @setTerminalAttributes fd attr ts@ calls @tcsetattr@ to change
1258 the @TerminalAttributes@ associated with @Fd@ @fd@ to
1259 @attr@, when the terminal is in the state indicated by @ts@.
1262 sendBreak :: Fd -> Int -> IO ()
1265 @sendBreak fd duration@ calls @tcsendbreak@ to transmit a
1266 continuous stream of zero-valued bits on @Fd@ @fd@ for the
1267 specified implementation-dependent @duration@.
1270 drainOutput :: Fd -> IO ()
1273 @drainOutput fd@ calls @tcdrain@ to block until all output
1274 written to @Fd@ @fd@ has been transmitted.
1277 discardData :: Fd -> QueueSelector -> IO ()
1280 @discardData fd queues@ calls @tcflush@ to discard
1281 pending input and/or output for @Fd@ @fd@,
1282 as indicated by the @QueueSelector@ @queues@.
1285 controlFlow :: Fd -> FlowAction -> IO ()
1288 @controlFlow fd action@ calls @tcflow@ to control the
1289 flow of data on @Fd@ @fd@, as indicated by
1293 getTerminalProcessGroupID :: Fd -> IO ProcessGroupID
1296 @getTerminalProcessGroupID fd@ calls @tcgetpgrp@ to
1297 obtain the @ProcessGroupID@ of the foreground process group
1298 associated with the terminal attached to @Fd@ @fd@.
1301 setTerminalProcessGroupID :: Fd -> ProcessGroupID -> IO ()
1304 @setTerminalProcessGroupID fd pgid@ calls @tcsetpgrp@ to
1305 set the @ProcessGroupID@ of the foreground process group
1306 associated with the terminal attached to @Fd@
1309 <sect2>Posix System Databases
1310 <label id="System Database">
1312 <nidx>Posix, system databases</nidx>
1315 groupName :: GroupEntry -> String
1316 groupID :: GroupEntry -> GroupID
1317 groupMembers :: GroupEntry -> [String]
1319 getGroupEntryForID :: GroupID -> IO GroupEntry
1322 @getGroupEntryForID gid@ calls @getgrgid@ to obtain
1323 the @GroupEntry@ information associated with @GroupID@
1326 The operation may fail with:
1329 <tag>@NoSuchThing@</tag>
1330 There is no group entry for the GroupID.
1334 getGroupEntryForName :: String -> IO GroupEntry
1337 @getGroupEntryForName name@ calls @getgrnam@ to obtain
1338 the @GroupEntry@ information associated with the group called
1341 The operation may fail with:
1344 <tag>@NoSuchThing@</tag>
1345 There is no group entry for the name.
1349 userName :: UserEntry -> String
1350 userID :: UserEntry -> UserID
1351 userGroupID :: UserEntry -> GroupID
1352 homeDirectory :: UserEntry -> String
1353 userShell :: UserEntry -> String
1355 getUserEntryForID :: UserID -> IO UserEntry
1358 @getUserEntryForID gid@ calls @getpwuid@ to obtain
1359 the @UserEntry@ information associated with @UserID@
1361 The operation may fail with:
1364 <tag>@NoSuchThing@</tag>
1365 There is no user entry for the UserID.
1369 getUserEntryForName :: String -> IO UserEntry
1372 @getUserEntryForName name@ calls @getpwnam@ to obtain
1373 the @UserEntry@ information associated with the user login
1376 The operation may fail with:
1379 <tag>@NoSuchThing@</tag>
1380 There is no user entry for the name.
1384 <label id="Error reporting and handling">
1386 <nidx>Posix, errors</nidx>
1389 getErrorCode :: IO ErrorCode
1392 @getErrorCode@ returns the current value of the external
1393 variable @errno@. It never fails.
1396 setErrorCode :: ErrorCode -> IO ()
1399 @setErrorCode err@ sets the external
1400 variable @errno@ to @err@. It never fails.
1403 noError :: ErrorCode
1406 argumentListTooLong, e2BIG :: ErrorCode
1407 badFd, eBADF :: ErrorCode
1408 brokenPipe, ePIPE :: ErrorCode
1409 directoryNotEmpty, eNOTEMPTY :: ErrorCode
1410 execFormatError, eNOEXEC :: ErrorCode
1411 fileAlreadyExists, eEXIST :: ErrorCode
1412 fileTooLarge, eFBIG :: ErrorCode
1413 filenameTooLong, eNAMETOOLONG :: ErrorCode
1414 improperLink, eXDEV :: ErrorCode
1415 inappropriateIOControlOperation, eNOTTY :: ErrorCode
1416 inputOutputError, eIO :: ErrorCode
1417 interruptedOperation, eINTR :: ErrorCode
1418 invalidArgument, eINVAL :: ErrorCode
1419 invalidSeek, eSPIPE :: ErrorCode
1420 isADirectory, eISDIR :: ErrorCode
1421 noChildProcess, eCHILD :: ErrorCode
1422 noLocksAvailable, eNOLCK :: ErrorCode
1423 noSpaceLeftOnDevice, eNOSPC :: ErrorCode
1424 noSuchOperationOnDevice, eNODEV :: ErrorCode
1425 noSuchDeviceOrAddress, eNXIO :: ErrorCode
1426 noSuchFileOrDirectory, eNOENT :: ErrorCode
1427 noSuchProcess, eSRCH :: ErrorCode
1428 notADirectory, eNOTDIR :: ErrorCode
1429 notEnoughMemory, eNOMEM :: ErrorCode
1430 operationNotImplemented, eNOSYS :: ErrorCode
1431 operationNotPermitted, ePERM :: ErrorCode
1432 permissionDenied, eACCES :: ErrorCode
1433 readOnlyFileSystem, eROFS :: ErrorCode
1434 resourceBusy, eBUSY :: ErrorCode
1435 resourceDeadlockAvoided, eDEADLK :: ErrorCode
1436 resourceTemporarilyUnavailable, eAGAIN :: ErrorCode
1437 tooManyLinks, eMLINK :: ErrorCode
1438 tooManyOpenFiles, eMFILE :: ErrorCode
1439 tooManyOpenFilesInSystem, eNFILE :: ErrorCode