1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
%
% (c) The GRASP/AQUA Project, Glasgow University, 1995-1996
%
\section[PosixProcEnv]{Haskell 1.3 POSIX Process Environment}
\begin{code}
#include "config.h"
module PosixProcEnv (
ProcessTimes,
SysVar(..),
SystemID,
childSystemTime,
childUserTime,
createProcessGroup,
createSession,
elapsedTime,
epochTime,
#if !defined(cygwin32_TARGET_OS)
getControllingTerminalName,
#endif
getEffectiveGroupID,
getEffectiveUserID,
getEffectiveUserName,
#if !defined(cygwin32_TARGET_OS)
getGroups,
#endif
getLoginName,
getParentProcessID,
getProcessGroupID,
getProcessID,
getProcessTimes,
getRealGroupID,
getRealUserID,
getSysVar,
getSystemID,
getTerminalName,
joinProcessGroup,
machine,
nodeName,
queryTerminal,
release,
setGroupID,
setProcessGroupID,
setUserID,
systemName,
systemTime,
userTime,
version
) where
import GlaExts
import PrelArr (ByteArray(..)) -- see internals
import PrelIOBase
import IO
import Addr ( nullAddr )
import PosixErr
import PosixUtil
import CString ( strcpy, allocWords, freeze, allocChars )
\end{code}
\begin{code}
getProcessID :: IO ProcessID
getProcessID = _ccall_ getpid
getParentProcessID :: IO ProcessID
getParentProcessID = _ccall_ getppid
getRealUserID :: IO UserID
getRealUserID = _ccall_ getuid
getEffectiveUserID :: IO UserID
getEffectiveUserID = _ccall_ geteuid
setUserID :: UserID -> IO ()
setUserID uid = nonzero_error (_ccall_ setuid uid) "setUserID"
getLoginName :: IO String
getLoginName = do
str <- _ccall_ getlogin
if str == nullAddr
then syserr "getLoginName"
else strcpy str
getRealGroupID :: IO GroupID
getRealGroupID = _ccall_ getgid
getEffectiveGroupID :: IO GroupID
getEffectiveGroupID = _ccall_ getegid
setGroupID :: GroupID -> IO ()
setGroupID gid = nonzero_error (_ccall_ setgid gid) "setGroupID"
-- getgroups() is not supported in beta18 of
-- cygwin32
#if !defined(cygwin32_TARGET_OS)
getGroups :: IO [GroupID]
getGroups = do
ngroups <- _ccall_ getgroups (0::Int) nullAddr
words <- allocWords ngroups
ngroups <- _casm_ ``%r = getgroups(%0,(gid_t *)%1);'' ngroups words
if ngroups /= ((-1)::Int)
then do
arr <- freeze words
return (map (extract arr) [0..(ngroups-1)])
else
syserr "getGroups"
where
extract (ByteArray _ _ barr#) (I# n#) =
case indexIntArray# barr# n# of
r# -> (I# r#)
#endif
getEffectiveUserName :: IO String
getEffectiveUserName = do
{- cuserid() is deprecated, using getpwuid() instead. -}
euid <- getEffectiveUserID
ptr <- _ccall_ getpwuid euid
str <- _casm_ ``%r = ((struct passwd *)%0)->pw_name;'' (ptr::Addr)
strcpy str
{- OLD:
str <- _ccall_ cuserid nullAddr
if str == nullAddr
then syserr "getEffectiveUserName"
else strcpy str
-}
getProcessGroupID :: IO ProcessGroupID
getProcessGroupID = _ccall_ getpgrp
createProcessGroup :: ProcessID -> IO ProcessGroupID
createProcessGroup pid = do
pgid <- _ccall_ setpgid pid (0::Int)
if pgid == (0::Int)
then return pgid
else syserr "createProcessGroup"
joinProcessGroup :: ProcessGroupID -> IO ()
joinProcessGroup pgid =
nonzero_error (_ccall_ setpgid (0::Int) pgid) "joinProcessGroupID"
setProcessGroupID :: ProcessID -> ProcessGroupID -> IO ()
setProcessGroupID pid pgid =
nonzero_error (_ccall_ setpgid pid pgid) "setProcessGroupID"
createSession :: IO ProcessGroupID
createSession = do
pgid <- _ccall_ setsid
if pgid /= ((-1)::Int)
then return pgid
else syserr "createSession"
type SystemID = ByteArray Int
systemName :: SystemID -> String
systemName sid = unsafePerformIO $ do
str <-_casm_ ``%r = ((struct utsname *)%0)->sysname;'' sid
strcpy str
nodeName :: SystemID -> String
nodeName sid = unsafePerformIO $ do
str <- _casm_ ``%r = ((struct utsname *)%0)->nodename;'' sid
strcpy str
release :: SystemID -> String
release sid = unsafePerformIO $ do
str <- _casm_ ``%r = ((struct utsname *)%0)->release;'' sid
strcpy str
version :: SystemID -> String
version sid = unsafePerformIO $ do
str <- _casm_ ``%r = ((struct utsname *)%0)->version;'' sid
strcpy str
machine :: SystemID -> String
machine sid = unsafePerformIO $ do
str <- _casm_ ``%r = ((struct utsname *)%0)->machine;'' sid
strcpy str
getSystemID :: IO SystemID
getSystemID = do
bytes <- allocChars (``sizeof(struct utsname)''::Int)
rc <- _casm_ ``%r = uname((struct utsname *)%0);'' bytes
if rc /= ((-1)::Int)
then freeze bytes
else syserr "getSystemID"
epochTime :: IO EpochTime
epochTime = do
secs <- _ccall_ time nullAddr
if secs /= ((-1)::Int)
then return secs
else syserr "epochTime"
-- All times in clock ticks (see getClockTick)
type ProcessTimes = (ClockTick, ByteArray Int)
elapsedTime :: ProcessTimes -> ClockTick
elapsedTime (realtime, _) = realtime
userTime :: ProcessTimes -> ClockTick
userTime (_, times) = unsafePerformIO $
_casm_ ``%r = ((struct tms *)%0)->tms_utime;'' times
systemTime :: ProcessTimes -> ClockTick
systemTime (_, times) = unsafePerformIO $
_casm_ ``%r = ((struct tms *)%0)->tms_stime;'' times
childUserTime :: ProcessTimes -> ClockTick
childUserTime (_, times) = unsafePerformIO $
_casm_ ``%r = ((struct tms *)%0)->tms_cutime;'' times
childSystemTime :: ProcessTimes -> ClockTick
childSystemTime (_, times) = unsafePerformIO $
_casm_ ``%r = ((struct tms *)%0)->tms_cstime;'' times
getProcessTimes :: IO ProcessTimes
getProcessTimes = do
bytes <- allocChars (``sizeof(struct tms)''::Int)
elapsed <- _casm_ ``%r = times((struct tms *)%0);'' bytes
if elapsed /= ((-1)::Int)
then do
times <- freeze bytes
return (elapsed, times)
else
syserr "getProcessTimes"
#if !defined(cygwin32_TARGET_OS)
getControllingTerminalName :: IO FilePath
getControllingTerminalName = do
str <- _ccall_ ctermid nullAddr
if str == nullAddr
then ioError (IOError Nothing NoSuchThing "getControllingTerminalName" "no controlling terminal")
else strcpy str
#endif
getTerminalName :: Fd -> IO FilePath
getTerminalName fd = do
str <- _ccall_ ttyname fd
if str == nullAddr
then do
err <- try (queryTerminal fd)
either (\ _err -> syserr "getTerminalName")
(\ succ -> if succ then ioError (IOError Nothing NoSuchThing
"getTerminalName" "no name")
else ioError (IOError Nothing InappropriateType
"getTerminalName" "not a terminal"))
err
else strcpy str
queryTerminal :: Fd -> IO Bool
queryTerminal (FD# fd) = do
rc <- _ccall_ isatty fd
case (rc::Int) of
-1 -> syserr "queryTerminal"
0 -> return False
1 -> return True
data SysVar = ArgumentLimit
| ChildLimit
| ClockTick
| GroupLimit
| OpenFileLimit
| PosixVersion
| HasSavedIDs
| HasJobControl
getSysVar :: SysVar -> IO Limit
getSysVar v =
case v of
ArgumentLimit -> sysconf ``_SC_ARG_MAX''
ChildLimit -> sysconf ``_SC_CHILD_MAX''
ClockTick -> sysconf ``_SC_CLK_TCK''
GroupLimit -> sysconf ``_SC_NGROUPS_MAX''
OpenFileLimit -> sysconf ``_SC_OPEN_MAX''
PosixVersion -> sysconf ``_SC_VERSION''
HasSavedIDs -> sysconf ``_SC_SAVED_IDS''
HasJobControl -> sysconf ``_SC_JOB_CONTROL''
-- where
sysconf :: Int -> IO Limit
sysconf n = do
rc <- _ccall_ sysconf n
if rc /= (-1::Int)
then return rc
else ioError (IOError Nothing NoSuchThing
"getSysVar"
"no such system limit or option")
\end{code}
|