2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1996
4 \section[PosixProcEnv]{Haskell 1.3 POSIX Process Environment}
20 #if !defined(cygwin32_TARGET_OS)
21 getControllingTerminalName,
26 #if !defined(cygwin32_TARGET_OS)
54 import PrelArr (ByteArray(..)) -- see internals
61 getProcessID :: IO ProcessID
62 getProcessID = _ccall_ getpid
64 getParentProcessID :: IO ProcessID
65 getParentProcessID = _ccall_ getppid
67 getRealUserID :: IO UserID
68 getRealUserID = _ccall_ getuid
70 getEffectiveUserID :: IO UserID
71 getEffectiveUserID = _ccall_ geteuid
73 setUserID :: UserID -> IO ()
74 setUserID uid = nonzero_error (_ccall_ setuid uid) "setUserID"
76 getLoginName :: IO String
78 str <- _ccall_ getlogin
80 then syserr "getLoginName"
83 getRealGroupID :: IO GroupID
84 getRealGroupID = _ccall_ getgid
86 getEffectiveGroupID :: IO GroupID
87 getEffectiveGroupID = _ccall_ getegid
89 setGroupID :: GroupID -> IO ()
90 setGroupID gid = nonzero_error (_ccall_ setgid gid) "setGroupID"
92 -- getgroups() is not supported in beta18 of
94 #if !defined(cygwin32_TARGET_OS)
95 getGroups :: IO [GroupID]
97 ngroups <- _ccall_ getgroups 0 (``NULL''::Addr)
98 words <- allocWords ngroups
99 ngroups <- _casm_ ``%r = getgroups(%0,(gid_t *)%1);'' ngroups words
103 return (map (extract arr) [0..(ngroups-1)])
107 extract (ByteArray _ barr#) (I# n#) =
108 case indexIntArray# barr# n# of
112 getEffectiveUserName :: IO String
113 getEffectiveUserName = do
114 {- cuserid() is deprecated, using getpwuid() instead. -}
115 euid <- getEffectiveUserID
116 ptr <- _ccall_ getpwuid euid
117 str <- _casm_ ``%r = ((struct passwd *)%0)->pw_name;'' (ptr::Addr)
121 str <- _ccall_ cuserid (``NULL''::Addr)
123 then syserr "getEffectiveUserName"
127 getProcessGroupID :: IO ProcessGroupID
128 getProcessGroupID = _ccall_ getpgrp
130 createProcessGroup :: ProcessID -> IO ProcessGroupID
131 createProcessGroup pid = do
132 pgid <- _ccall_ setpgid pid 0
135 else syserr "createProcessGroup"
137 joinProcessGroup :: ProcessGroupID -> IO ()
138 joinProcessGroup pgid =
139 nonzero_error (_ccall_ setpgid 0 pgid) "joinProcessGroupID"
141 setProcessGroupID :: ProcessID -> ProcessGroupID -> IO ()
142 setProcessGroupID pid pgid =
143 nonzero_error (_ccall_ setpgid pid pgid) "setProcessGroupID"
145 createSession :: IO ProcessGroupID
147 pgid <- _ccall_ setsid
150 else syserr "createSession"
152 type SystemID = ByteArray ()
154 systemName :: SystemID -> String
155 systemName sid = unsafePerformIO $ do
156 str <-_casm_ ``%r = ((struct utsname *)%0)->sysname;'' sid
159 nodeName :: SystemID -> String
160 nodeName sid = unsafePerformIO $ do
161 str <- _casm_ ``%r = ((struct utsname *)%0)->nodename;'' sid
164 release :: SystemID -> String
165 release sid = unsafePerformIO $ do
166 str <- _casm_ ``%r = ((struct utsname *)%0)->release;'' sid
169 version :: SystemID -> String
170 version sid = unsafePerformIO $ do
171 str <- _casm_ ``%r = ((struct utsname *)%0)->version;'' sid
174 machine :: SystemID -> String
175 machine sid = unsafePerformIO $ do
176 str <- _casm_ ``%r = ((struct utsname *)%0)->machine;'' sid
179 getSystemID :: IO SystemID
181 bytes <- allocChars (``sizeof(struct utsname)''::Int)
182 rc <- _casm_ ``%r = uname((struct utsname *)%0);'' bytes
185 else syserr "getSystemID"
187 epochTime :: IO EpochTime
189 secs <- _ccall_ time (``NULL''::Addr)
192 else syserr "epochTime"
194 -- All times in clock ticks (see getClockTick)
196 type ProcessTimes = (ClockTick, ByteArray ())
198 elapsedTime :: ProcessTimes -> ClockTick
199 elapsedTime (realtime, _) = realtime
201 userTime :: ProcessTimes -> ClockTick
202 userTime (_, times) = unsafePerformIO $
203 _casm_ ``%r = ((struct tms *)%0)->tms_utime;'' times
205 systemTime :: ProcessTimes -> ClockTick
206 systemTime (_, times) = unsafePerformIO $
207 _casm_ ``%r = ((struct tms *)%0)->tms_stime;'' times
209 childUserTime :: ProcessTimes -> ClockTick
210 childUserTime (_, times) = unsafePerformIO $
211 _casm_ ``%r = ((struct tms *)%0)->tms_cutime;'' times
213 childSystemTime :: ProcessTimes -> ClockTick
214 childSystemTime (_, times) = unsafePerformIO $
215 _casm_ ``%r = ((struct tms *)%0)->tms_cstime;'' times
217 getProcessTimes :: IO ProcessTimes
219 bytes <- allocChars (``sizeof(struct tms)''::Int)
220 elapsed <- _casm_ ``%r = times((struct tms *)%0);'' bytes
223 times <- freeze bytes
224 return (elapsed, times)
226 syserr "getProcessTimes"
228 #if !defined(cygwin32_TARGET_OS)
229 getControllingTerminalName :: IO FilePath
230 getControllingTerminalName = do
231 str <- _ccall_ ctermid (``NULL''::Addr)
233 then fail (IOError Nothing NoSuchThing "getControllingTerminalName" "no controlling terminal")
237 getTerminalName :: Fd -> IO FilePath
238 getTerminalName fd = do
239 str <- _ccall_ ttyname fd
242 err <- try (queryTerminal fd)
243 either (\err -> syserr "getTerminalName")
244 (\succ -> if succ then fail (IOError Nothing NoSuchThing
245 "getTerminalName" "no name")
246 else fail (IOError Nothing InappropriateType
247 "getTerminalName" "not a terminal"))
251 queryTerminal :: Fd -> IO Bool
252 queryTerminal (FD# fd) = do
253 rc <- _ccall_ isatty fd
255 -1 -> syserr "queryTerminal"
259 data SysVar = ArgumentLimit
268 getSysVar :: SysVar -> IO Limit
271 ArgumentLimit -> sysconf ``_SC_ARG_MAX''
272 ChildLimit -> sysconf ``_SC_CHILD_MAX''
273 ClockTick -> sysconf ``_SC_CLK_TCK''
274 GroupLimit -> sysconf ``_SC_NGROUPS_MAX''
275 OpenFileLimit -> sysconf ``_SC_OPEN_MAX''
276 PosixVersion -> sysconf ``_SC_VERSION''
277 HasSavedIDs -> sysconf ``_SC_SAVED_IDS''
278 HasJobControl -> sysconf ``_SC_JOB_CONTROL''
281 sysconf :: Int -> IO Limit
283 rc <- _ccall_ sysconf n
286 else fail (IOError Nothing NoSuchThing
288 "no such system limit or option")