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
508 an interrupt handler for signal @int@. If @handler@ is
509 @Default@, @SIG_DFL@ is installed; if @handler@ is
510 @Ignore@, @SIG_IGN@ is installed; if @handler@ is
511 @Catch action@, a handler is installed which will invoke @action@ as a
512 replacement for @main@. If @iset@ is @Just s@, then the
513 @sa_mask@ of the @sigaction@ structure is set to @s@;
514 otherwise it is cleared. The previously installed signal handler for
518 getSignalMask :: IO SignalSet
521 @getSignalMask@ calls @sigprocmask@ to determine the
522 set of interrupts which are currently being blocked.
525 setSignalMask :: SignalSet -> IO SignalSet
528 @setSignalMask mask@ calls @sigprocmask@ with
529 @SIG_SETMASK@ to block all interrupts in @mask@. The
530 previous set of blocked interrupts is returned.
533 blockSignals :: SignalSet -> IO SignalSet
536 @setSignalMask mask@ calls @sigprocmask@ with
537 @SIG_BLOCK@ to add all interrupts in @mask@ to the
538 set of blocked interrupts. The previous set of blocked interrupts is returned.
541 unBlockSignals :: SignalSet -> IO SignalSet
544 @setSignalMask mask@ calls @sigprocmask@ with
545 @SIG_UNBLOCK@ to remove all interrupts in @mask@ from the
546 set of blocked interrupts. The previous set of blocked interrupts is returned.
549 getPendingSignals :: IO SignalSet
552 @getPendingSignals@ calls @sigpending@ to obtain
553 the set of interrupts which have been received but are currently blocked.
556 awaitSignal :: Maybe SignalSet -> IO ()
559 @awaitSignal iset@ suspends execution until an interrupt is received.
560 If @iset@ is @Just s@, @awaitSignal@ calls
561 @sigsuspend@, installing @s@ as the new signal mask before
562 suspending execution; otherwise, it calls @pause@. If successful,
563 @awaitSignal@ does not return.
566 scheduleAlarm :: Int -> IO Int
569 @scheduleAlarm i@ calls @alarm@ to schedule a real time
570 alarm at least @i@ seconds in the future.
573 sleep :: Int -> IO ()
576 @sleep i@ calls @sleep@ to suspend execution of the
577 program until at least @i@ seconds have elapsed or a signal is
580 <sect2>Posix Process Environment
581 <label id="Process Environment">
583 <nidx>Posix, process environment</nidx>
586 getProcessID :: IO ProcessID
589 @getProcessID@ calls @getpid@ to obtain the @ProcessID@ for
593 getParentProcessID :: IO ProcessID
596 @getProcessID@ calls @getppid@ to obtain the @ProcessID@ for
597 the parent of the current process.
600 getRealUserID :: IO UserID
603 @getRealUserID@ calls @getuid@ to obtain the real @UserID@
604 associated with the current process.
607 getEffectiveUserID :: IO UserID
610 @getRealUserID@ calls @geteuid@ to obtain the effective
611 @UserID@ associated with the current process.
614 setUserID :: UserID -> IO ()
617 @setUserID uid@ calls @setuid@ to set the real, effective, and
618 saved set-user-id associated with the current process to @uid@.
621 getLoginName :: IO String
624 @getLoginName@ calls @getlogin@ to obtain the login name
625 associated with the current process.
628 getRealGroupID :: IO GroupID
631 @getRealGroupID@ calls @getgid@ to obtain the real @GroupID@
632 associated with the current process.
635 getEffectiveGroupID :: IO GroupID
638 @getEffectiveGroupID@ calls @getegid@ to obtain the effective
639 @GroupID@ associated with the current process.
642 setGroupID :: GroupID -> IO ()
645 @setGroupID gid@ calls @setgid@ to set the real, effective, and
646 saved set-group-id associated with the current process to @gid@.
649 getGroups :: IO [GroupID]
652 @getGroups@ calls @getgroups@ to obtain the list of
653 supplementary @GroupID@s associated with the current process.
656 getEffectiveUserName :: IO String
659 @getEffectiveUserName@ calls @cuserid@ to obtain a name
660 associated with the effective @UserID@ of the process.
663 getProcessGroupID :: IO ProcessGroupID
666 @getProcessGroupID@ calls @getpgrp@ to obtain the
667 @ProcessGroupID@ for the current process.
670 createProcessGroup :: ProcessID -> IO ProcessGroupID
673 @createProcessGroup pid@ calls @setpgid@ to make
674 process @pid@ a new process group leader.
677 joinProcessGroup :: ProcessGroupID -> IO ProcessGroupID
680 @joinProcessGroup pgid@ calls @setpgid@ to set the
681 @ProcessGroupID@ of the current process to @pgid@.
684 setProcessGroupID :: ProcessID -> ProcessGroupID -> IO ()
687 @setProcessGroupID pid pgid@ calls @setpgid@ to set the
688 @ProcessGroupID@ for process @pid@ to @pgid@.
691 createSession :: IO ProcessGroupID
694 @createSession@ calls @setsid@ to create a new session
695 with the current process as session leader.
698 systemName :: SystemID -> String
699 nodeName :: SystemID -> String
700 release :: SystemID -> String
701 version :: SystemID -> String
702 machine :: SystemID -> String
704 getSystemID :: IO SystemID
707 @getSystemID@ calls @uname@ to obtain information
708 about the current operating system.
711 > epochTime :: IO EpochTime
714 @epochTime@ calls @time@ to obtain the number of
715 seconds that have elapsed since the epoch (Jan 01 00:00:00 GMT 1970).
718 elapsedTime :: ProcessTimes -> ClockTick
719 userTime :: ProcessTimes -> ClockTick
720 systemTime :: ProcessTimes -> ClockTick
721 childUserTime :: ProcessTimes -> ClockTick
722 childSystemTime :: ProcessTimes -> ClockTick
724 getProcessTimes :: IO ProcessTimes
727 @getProcessTimes@ calls @times@ to obtain time-accounting
728 information for the current process and its children.
731 getControllingTerminalName :: IO FilePath
734 @getControllingTerminalName@ calls @ctermid@ to obtain
735 a name associated with the controlling terminal for the process. If a
736 controlling terminal exists,
737 @getControllingTerminalName@ returns the name of the
738 controlling terminal.
740 The operation may fail with:
743 <tag>@NoSuchThing@</tag>
744 There is no controlling terminal, or its name cannot be determined.
745 <tag>@SystemError@</tag>
746 Various other causes.
750 getTerminalName :: Fd -> IO FilePath
753 @getTerminalName fd@ calls @ttyname@ to obtain a name associated
754 with the terminal for @Fd@ @fd@. If @fd@ is associated
755 with a terminal, @getTerminalName@ returns the name of the
758 The operation may fail with:
761 <tag>@InappropriateType@</tag>
762 The channel is not associated with a terminal.
763 <tag>@NoSuchThing@</tag>
764 The channel is associated with a terminal, but it has no name.
765 <tag>@SystemError@</tag>
766 Various other causes.
770 queryTerminal :: Fd -> IO Bool
773 @queryTerminal fd@ calls @isatty@ to determine whether or
774 not @Fd@ @fd@ is associated with a terminal.
777 getSysVar :: SysVar -> IO Limit
780 @getSysVar var@ calls @sysconf@ to obtain the
781 dynamic value of the requested configurable system limit or option.
782 For defined system limits, @getSysVar@ returns the associated
783 value. For defined system options, the result of @getSysVar@
784 is undefined, but not failure.
786 The operation may fail with:
789 <tag>@NoSuchThing@</tag>
790 The requested system limit or option is undefined.
793 <sect2>Posix operations on files and directories
794 <label id="Files and Directories">
796 <nidx>Posix, files and directories</nidx>
799 openDirStream :: FilePath -> IO DirStream
802 @openDirStream dir@ calls @opendir@ to obtain a
803 directory stream for @dir@.
806 readDirStream :: DirStream -> IO String
809 @readDirStream dp@ calls @readdir@ to obtain the
810 next directory entry (@struct dirent@) for the open directory
811 stream @dp@, and returns the @d_name@ member of that
814 The operation may fail with:
818 End of file has been reached.
819 <tag>@SystemError@</tag>
820 Various other causes.
824 rewindDirStream :: DirStream -> IO ()
827 @rewindDirStream dp@ calls @rewinddir@ to reposition
828 the directory stream @dp@ at the beginning of the directory.
831 closeDirStream :: DirStream -> IO ()
834 @closeDirStream dp@ calls @closedir@ to close
835 the directory stream @dp@.
838 getWorkingDirectory :: IO FilePath
841 @getWorkingDirectory@ calls @getcwd@ to obtain the name
842 of the current working directory.
845 changeWorkingDirectory :: FilePath -> IO ()
848 @changeWorkingDirectory dir@ calls @chdir@ to change
849 the current working directory to @dir@.
852 nullFileMode :: FileMode -- ---------
853 ownerReadMode :: FileMode -- r--------
854 ownerWriteMode :: FileMode -- -w-------
855 ownerExecuteMode :: FileMode -- --x------
856 groupReadMode :: FileMode -- ---r-----
857 groupWriteMode :: FileMode -- ----w----
858 groupExecuteMode :: FileMode -- -----x---
859 otherReadMode :: FileMode -- ------r--
860 otherWriteMode :: FileMode -- -------w-
861 otherExecuteMode :: FileMode -- --------x
862 setUserIDMode :: FileMode -- --S------
863 setGroupIDMode :: FileMode -- -----S---
865 stdFileMode :: FileMode -- rw-rw-rw-
867 ownerModes :: FileMode -- rwx------
868 groupModes :: FileMode -- ---rwx---
869 otherModes :: FileMode -- ------rwx
870 accessModes :: FileMode -- rwxrwxrwx
872 unionFileModes :: FileMode -> FileMode -> FileMode
873 intersectFileModes :: FileMode -> FileMode -> FileMode
879 stdOutput = intToFd 1
895 -> Maybe FileMode -- Just x => O_CREAT, Nothing => must exist
900 @openFd path acc mode (OpenFileFlags app excl noctty nonblock trunc)@ calls
901 @open@ to obtain a @Fd@ for the file @path@ with access
902 mode @acc@. If @mode@ is @Just m@, the @O_CREAT@ flag is
903 set and the file's permissions will be based on @m@ if it does not
904 already exist; otherwise, the @O_CREAT@ flag is not set. The
905 arguments @app@, @excl@, @noctty@, @nonblock@, and
906 @trunc@ control whether or not the flags @O_APPEND@,
907 @O_EXCL@, @O_NOCTTY@, @O_NONBLOCK@, and @O_TRUNC@ are set,
911 createFile :: FilePath -> FileMode -> IO Fd
914 @createFile path mode@ calls @creat@ to obtain a @Fd@
915 for file @path@, which will be created with permissions based on
916 @mode@ if it does not already exist.
919 setFileCreationMask :: FileMode -> IO FileMode
922 @setFileCreationMask mode@ calls @umask@ to set
923 the process's file creation mask to @mode@. The previous file
924 creation mask is returned.
927 createLink :: FilePath -> FilePath -> IO ()
930 @createLink old new@ calls @link@ to create a
931 new path, @new@, linked to an existing file, @old@.
933 createDirectory :: FilePath -> FileMode -> IO ()
936 @createDirectory dir mode@ calls @mkdir@ to
937 create a new directory, @dir@, with permissions based on
941 createNamedPipe :: FilePath -> FileMode -> IO ()
944 @createNamedPipe fifo mode@ calls @mkfifo@ to
945 create a new named pipe, @fifo@, with permissions based on
949 removeLink :: FilePath -> IO ()
952 @removeLink path@ calls @unlink@ to remove the link
956 removeDirectory :: FilePath -> IO ()
959 @removeDirectory dir@ calls @rmdir@ to remove the
960 directory named @dir@.
963 rename :: FilePath -> FilePath -> IO ()
966 @rename old new@ calls @rename@ to rename a
967 file or directory from @old@ to @new@.
970 fileMode :: FileStatus -> FileMode
972 fileID :: FileStatus -> FileID
973 deviceID :: FileStatus -> DeviceID
975 linkCount :: FileStatus -> LinkCount
977 fileOwner :: FileStatus -> UserID
978 fileGroup :: FileStatus -> GroupID
979 fileSize :: FileStatus -> FileOffset
981 accessTime :: FileStatus -> EpochTime
982 modificationTime :: FileStatus -> EpochTime
983 statusChangeTime :: FileStatus -> EpochTime
985 isDirectory :: FileStatus -> Bool
986 isCharacterDevice :: FileStatus -> Bool
987 isBlockDevice :: FileStatus -> Bool
988 isRegularFile :: FileStatus -> Bool
989 isNamedPipe :: FileStatus -> Bool
991 getFileStatus :: FilePath -> IO FileStatus
994 @getFileStatus path@ calls @stat@ to get the
995 @FileStatus@ information for the file @path@.
998 getFdStatus :: Fd -> IO FileStatus
1001 @getFdStatus fd@ calls @fstat@ to get the
1002 @FileStatus@ information for the file associated with
1006 queryAccess :: FilePath -> Bool -> Bool -> Bool -> IO Bool
1009 @queryAccess path r w x@ calls @access@ to test the access
1010 permissions for file @path@. The three arguments, @r@, @w@,
1011 and @x@ control whether or not @access@ is called with
1012 @R_OK@, @W_OK@, and @X_OK@ respectively.
1015 queryFile :: FilePath -> IO Bool
1018 @queryFile path@ calls @access@ with @F_OK@ to test for the
1019 existence for file @path@.
1022 setFileMode :: FilePath -> FileMode -> IO ()
1025 @setFileMode path mode@ calls @chmod@ to set the
1026 permission bits associated with file @path@ to @mode@.
1029 setOwnerAndGroup :: FilePath -> UserID -> GroupID -> IO ()
1032 @setOwnerAndGroup path uid gid@ calls @chown@ to
1033 set the @UserID@ and @GroupID@ associated with file
1034 @path@ to @uid@ and @gid@, respectively.
1037 setFileTimes :: FilePath -> EpochTime -> EpochTime -> IO ()
1040 @setFileTimes path atime mtime@ calls @utime@ to
1041 set the access and modification times associated with file
1042 @path@ to @atime@ and @mtime@, respectively.
1045 touchFile :: FilePath -> IO ()
1048 @touchFile path@ calls @utime@ to
1049 set the access and modification times associated with file
1050 @path@ to the current time.
1053 getPathVar :: PathVar -> FilePath -> IO Limit
1056 @getPathVar var path@ calls @pathconf@ to obtain the
1057 dynamic value of the requested configurable file limit or option associated
1058 with file or directory @path@. For
1059 defined file limits, @getPathVar@ returns the associated
1060 value. For defined file options, the result of @getPathVar@
1061 is undefined, but not failure.
1062 The operation may fail with:
1064 <tag>@NoSuchThing@</tag>
1065 The requested file limit or option is undefined.
1066 <tag>@SystemError@</tag>
1067 Various other causes.
1072 getFdVar :: PathVar -> Fd -> IO Limit
1075 @getFdVar var fd@ calls @fpathconf@ to obtain the
1076 dynamic value of the requested configurable file limit or option associated
1077 with the file or directory attached to the open channel @fd@.
1078 For defined file limits, @getFdVar@ returns the associated
1079 value. For defined file options, the result of @getFdVar@
1080 is undefined, but not failure.
1082 The operation may fail with:
1085 <tag>@NoSuchThing@</tag>
1086 The requested file limit or option is undefined.
1087 <tag>@SystemError@</tag>
1088 Various other causes.
1091 <sect2>Posix Input and Output Primitives
1092 <label id="Input/Output">
1094 <nidx>Posix, input/output</nidx>
1097 createPipe :: IO (Fd, Fd)
1100 @createPipe@ calls @pipe@ to create a pipe and returns a pair of
1101 @Fd@s, the first for reading and the second for writing.
1107 @dup fd@ calls @dup@ to duplicate @Fd@ @fd@ to
1111 dupTo :: Fd -> Fd -> IO ()
1114 @dupTo src dst@ calls @dup2@ to duplicate @Fd@
1115 @src@ to @Fd@ @dst@.
1118 fdClose :: Fd -> IO ()
1121 @fdClose fd@ calls @close@ to close @Fd@ @fd@.
1124 fdRead :: Fd -> ByteCount -> IO (String, ByteCount)
1127 @fdRead fd nbytes@ calls @read@ to read at most @nbytes@
1128 bytes from @Fd@ @fd@, and returns the result as a string
1129 paired with the number of bytes actually read.
1131 The operation may fail with:
1135 End of file has been reached.
1136 <tag>@SystemError@</tag>
1137 Various other causes.
1141 fdWrite :: Fd -> String -> IO ByteCount
1144 @fdWrite fd s@ calls @write@ to write
1145 the string @s@ to @Fd@ @fd@ as a
1146 contiguous sequence of bytes. It returns the number of bytes successfully
1150 queryFdOption :: FdOption -> Fd -> IO Bool
1153 @getFdOption opt fd@ calls @fcntl@ to determine whether or
1154 not the flag associated with @FdOption@ @opt@ is set for
1158 setFdOption :: Fd -> FdOption -> Bool -> IO ()
1161 @setFdOption fd opt val@ calls @fcntl@ to set the flag
1162 associated with @FdOption@ @opt@ on @Fd@ @fd@ to
1166 getLock :: Fd -> FileLock -> IO (Maybe (ProcessID, FileLock))
1169 @getLock fd lock@ calls @fcntl@ to get the first @FileLock@
1170 for @Fd@ @fd@ which blocks the @FileLock@ @lock@. If
1171 no such @FileLock@ exists, @getLock@ returns @Nothing@.
1172 Otherwise, it returns @Just (pid, block)@, where @block@ is the
1173 blocking @FileLock@ and @pid@ is the @ProcessID@ of the
1174 process holding the blocking @FileLock@.
1178 setLock :: Fd -> FileLock -> IO ()
1181 @setLock fd lock@ calls @fcntl@ with @F_SETLK@ to set or
1182 clear a lock segment for @Fd@ @fd@ as indicated by the
1183 @FileLock@ @lock@. @setLock@ does not block, but fails with
1184 @SystemError@ if the request cannot be satisfied immediately.
1187 waitToSetLock :: Fd -> FileLock -> IO ()
1190 @waitToSetLock fd lock@ calls @fcntl@ with @F_SETLKW@ to set
1191 or clear a lock segment for @Fd@ @fd@ as indicated by the
1192 @FileLock@ @lock@. If the request cannot be satisfied
1193 immediately, @waitToSetLock@ blocks until the request can be
1198 fdSeek :: Fd -> SeekMode -> FileOffset -> IO FileOffset
1201 @fdSeek fd whence offset@ calls @lseek@ to position the
1202 @Fd@ @fd@ at the given @offset@ from the starting location
1203 indicated by @whence@. It returns the resulting offset from the
1204 start of the file in bytes.
1206 <sect2>Posix, Device- and Class-Specific Functions
1207 <label id="Device Specific Functions">
1209 <nidx>Posix, device and class-specific functions</nidx>
1212 terminalMode :: TerminalMode -> TerminalAttributes -> Bool
1213 withMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
1214 withoutMode :: TerminalAttributes -> TerminalMode -> TerminalAttributes
1216 bitsPerByte :: TerminalAttributes -> Int
1217 withBits :: TerminalAttributes -> Int -> TerminalAttributes
1219 controlChar :: TerminalAttributes -> ControlCharacter -> Maybe Char
1220 withCC :: TerminalAttributes
1221 -> (ControlCharacter, Char)
1222 -> TerminalAttributes
1223 withoutCC :: TerminalAttributes
1225 -> TerminalAttributes
1227 inputTime :: TerminalAttributes -> Int
1228 withTime :: TerminalAttributes -> Int -> TerminalAttributes
1230 minInput :: TerminalAttributes -> Int
1231 withMinInput :: TerminalAttributes -> Int -> TerminalAttributes
1233 inputSpeed :: TerminalAttributes -> BaudRate
1234 withInputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
1236 outputSpeed :: TerminalAttributes -> BaudRate
1237 withOutputSpeed :: TerminalAttributes -> BaudRate -> TerminalAttributes
1239 getTerminalAttributes :: Fd -> IO TerminalAttributes
1242 @getTerminalAttributes fd@ calls @tcgetattr@ to obtain
1243 the @TerminalAttributes@ associated with @Fd@ @fd@.
1246 setTerminalAttributes :: Fd
1247 -> TerminalAttributes
1252 @setTerminalAttributes fd attr ts@ calls @tcsetattr@ to change
1253 the @TerminalAttributes@ associated with @Fd@ @fd@ to
1254 @attr@, when the terminal is in the state indicated by @ts@.
1257 sendBreak :: Fd -> Int -> IO ()
1260 @sendBreak fd duration@ calls @tcsendbreak@ to transmit a
1261 continuous stream of zero-valued bits on @Fd@ @fd@ for the
1262 specified implementation-dependent @duration@.
1265 drainOutput :: Fd -> IO ()
1268 @drainOutput fd@ calls @tcdrain@ to block until all output
1269 written to @Fd@ @fd@ has been transmitted.
1272 discardData :: Fd -> QueueSelector -> IO ()
1275 @discardData fd queues@ calls @tcflush@ to discard
1276 pending input and/or output for @Fd@ @fd@,
1277 as indicated by the @QueueSelector@ @queues@.
1280 controlFlow :: Fd -> FlowAction -> IO ()
1283 @controlFlow fd action@ calls @tcflow@ to control the
1284 flow of data on @Fd@ @fd@, as indicated by
1288 getTerminalProcessGroupID :: Fd -> IO ProcessGroupID
1291 @getTerminalProcessGroupID fd@ calls @tcgetpgrp@ to
1292 obtain the @ProcessGroupID@ of the foreground process group
1293 associated with the terminal attached to @Fd@ @fd@.
1296 setTerminalProcessGroupID :: Fd -> ProcessGroupID -> IO ()
1299 @setTerminalProcessGroupID fd pgid@ calls @tcsetpgrp@ to
1300 set the @ProcessGroupID@ of the foreground process group
1301 associated with the terminal attached to @Fd@
1304 <sect2>Posix System Databases
1305 <label id="System Database">
1307 <nidx>Posix, system databases</nidx>
1310 groupName :: GroupEntry -> String
1311 groupID :: GroupEntry -> GroupID
1312 groupMembers :: GroupEntry -> [String]
1314 getGroupEntryForID :: GroupID -> IO GroupEntry
1317 @getGroupEntryForID gid@ calls @getgrgid@ to obtain
1318 the @GroupEntry@ information associated with @GroupID@
1321 The operation may fail with:
1324 <tag>@NoSuchThing@</tag>
1325 There is no group entry for the GroupID.
1329 getGroupEntryForName :: String -> IO GroupEntry
1332 @getGroupEntryForName name@ calls @getgrnam@ to obtain
1333 the @GroupEntry@ information associated with the group called
1336 The operation may fail with:
1339 <tag>@NoSuchThing@</tag>
1340 There is no group entry for the name.
1344 userName :: UserEntry -> String
1345 userID :: UserEntry -> UserID
1346 userGroupID :: UserEntry -> GroupID
1347 homeDirectory :: UserEntry -> String
1348 userShell :: UserEntry -> String
1350 getUserEntryForID :: UserID -> IO UserEntry
1353 @getUserEntryForID gid@ calls @getpwuid@ to obtain
1354 the @UserEntry@ information associated with @UserID@
1356 The operation may fail with:
1359 <tag>@NoSuchThing@</tag>
1360 There is no user entry for the UserID.
1364 getUserEntryForName :: String -> IO UserEntry
1367 @getUserEntryForName name@ calls @getpwnam@ to obtain
1368 the @UserEntry@ information associated with the user login
1371 The operation may fail with:
1374 <tag>@NoSuchThing@</tag>
1375 There is no user entry for the name.
1379 <label id="Error reporting and handling">
1381 <nidx>Posix, errors</nidx>
1384 getErrorCode :: IO ErrorCode
1387 @getErrorCode@ returns the current value of the external
1388 variable @errno@. It never fails.
1391 setErrorCode :: ErrorCode -> IO ()
1394 @setErrorCode err@ sets the external
1395 variable @errno@ to @err@. It never fails.
1398 noError :: ErrorCode
1401 argumentListTooLong, e2BIG :: ErrorCode
1402 badFd, eBADF :: ErrorCode
1403 brokenPipe, ePIPE :: ErrorCode
1404 directoryNotEmpty, eNOTEMPTY :: ErrorCode
1405 execFormatError, eNOEXEC :: ErrorCode
1406 fileAlreadyExists, eEXIST :: ErrorCode
1407 fileTooLarge, eFBIG :: ErrorCode
1408 filenameTooLong, eNAMETOOLONG :: ErrorCode
1409 improperLink, eXDEV :: ErrorCode
1410 inappropriateIOControlOperation, eNOTTY :: ErrorCode
1411 inputOutputError, eIO :: ErrorCode
1412 interruptedOperation, eINTR :: ErrorCode
1413 invalidArgument, eINVAL :: ErrorCode
1414 invalidSeek, eSPIPE :: ErrorCode
1415 isADirectory, eISDIR :: ErrorCode
1416 noChildProcess, eCHILD :: ErrorCode
1417 noLocksAvailable, eNOLCK :: ErrorCode
1418 noSpaceLeftOnDevice, eNOSPC :: ErrorCode
1419 noSuchOperationOnDevice, eNODEV :: ErrorCode
1420 noSuchDeviceOrAddress, eNXIO :: ErrorCode
1421 noSuchFileOrDirectory, eNOENT :: ErrorCode
1422 noSuchProcess, eSRCH :: ErrorCode
1423 notADirectory, eNOTDIR :: ErrorCode
1424 notEnoughMemory, eNOMEM :: ErrorCode
1425 operationNotImplemented, eNOSYS :: ErrorCode
1426 operationNotPermitted, ePERM :: ErrorCode
1427 permissionDenied, eACCES :: ErrorCode
1428 readOnlyFileSystem, eROFS :: ErrorCode
1429 resourceBusy, eBUSY :: ErrorCode
1430 resourceDeadlockAvoided, eDEADLK :: ErrorCode
1431 resourceTemporarilyUnavailable, eAGAIN :: ErrorCode
1432 tooManyLinks, eMLINK :: ErrorCode
1433 tooManyOpenFiles, eMFILE :: ErrorCode
1434 tooManyOpenFilesInSystem, eNFILE :: ErrorCode