OILS / core / pyos.py View on Github | oilshell.org

402 lines, 180 significant
1"""
2pyos.py -- Wrappers for the operating system.
3
4Like py{error,util}.py, it won't be translated to C++.
5"""
6from __future__ import print_function
7
8from errno import EINTR
9import pwd
10import resource
11import signal
12import select
13import sys
14import termios # for read -n
15import time
16
17from core import pyutil
18from mycpp.mylib import log
19
20import posix_ as posix
21from posix_ import WUNTRACED
22
23from typing import Optional, Tuple, List, Dict, cast, Any
24
25_ = log
26
27EOF_SENTINEL = 256 # bigger than any byte
28NEWLINE_CH = 10 # ord('\n')
29
30
31def FlushStdout():
32 # type: () -> None
33 """Flush CPython buffers."""
34 sys.stdout.flush()
35
36
37def WaitPid(waitpid_options):
38 # type: (int) -> Tuple[int, int]
39 """
40 Return value:
41 pid is 0 if WNOHANG passed, and nothing has changed state
42 status: value that can be parsed with WIFEXITED() etc.
43 """
44 try:
45 # Notes:
46 # - The arg -1 makes it like wait(), which waits for any process.
47 # - WUNTRACED is necessary to get stopped jobs. What about WCONTINUED?
48 # - We don't retry on EINTR, because the 'wait' builtin should be
49 # interruptible.
50 # - waitpid_options can be WNOHANG
51 pid, status = posix.waitpid(-1, WUNTRACED | waitpid_options)
52 except OSError as e:
53 return -1, e.errno
54
55 return pid, status
56
57
58class ReadError(Exception):
59 """Wraps errno returned by read().
60
61 Used by 'read' and 'mapfile' builtins.
62 """
63
64 def __init__(self, err_num):
65 # type: (int) -> None
66 self.err_num = err_num
67
68
69def Read(fd, n, chunks):
70 # type: (int, int, List[str]) -> Tuple[int, int]
71 """C-style wrapper around Python's posix.read() that uses return values
72 instead of exceptions for errors. We will implement this directly in C++
73 and not use exceptions at all.
74
75 It reads n bytes from the given file descriptor and appends it to chunks.
76
77 Returns:
78 (-1, errno) on failure
79 (number of bytes read, 0) on success. Where 0 bytes read indicates EOF.
80 """
81 try:
82 chunk = posix.read(fd, n)
83 except OSError as e:
84 return -1, e.errno
85 else:
86 length = len(chunk)
87 if length:
88 chunks.append(chunk)
89 return length, 0
90
91
92def ReadByte(fd):
93 # type: (int) -> Tuple[int, int]
94 """Another low level interface with a return value interface. Used by
95 _ReadUntilDelim() and _ReadLineSlowly().
96
97 Returns:
98 failure: (-1, errno) on failure
99 success: (ch integer value or EOF_SENTINEL, 0)
100 """
101 try:
102 b = posix.read(fd, 1)
103 except OSError as e:
104 return -1, e.errno
105 else:
106 if len(b):
107 return ord(b), 0
108 else:
109 return EOF_SENTINEL, 0
110
111
112if 0:
113
114 def ReadLineBuffered():
115 # type: () -> str
116 """Obsolete
117 """
118 ch_array = [] # type: List[int]
119 while True:
120 ch, err_num = ReadByte(0)
121
122 if ch < 0:
123 if err_num == EINTR:
124 # Instead of retrying, return EOF, which is what libc.stdin_readline()
125 # did. I think this interface is easier with getline().
126 # This causes 'read --line' to return status 1.
127 return ''
128 else:
129 raise ReadError(err_num)
130
131 elif ch == EOF_SENTINEL:
132 break
133
134 else:
135 ch_array.append(ch)
136
137 # TODO: Add option to omit newline
138 if ch == NEWLINE_CH:
139 break
140
141 return pyutil.ChArrayToString(ch_array)
142
143
144def Environ():
145 # type: () -> Dict[str, str]
146 return posix.environ
147
148
149def Chdir(dest_dir):
150 # type: (str) -> int
151 """Returns 0 for success and nonzero errno for error."""
152 try:
153 posix.chdir(dest_dir)
154 except OSError as e:
155 return e.errno
156 return 0
157
158
159def GetMyHomeDir():
160 # type: () -> Optional[str]
161 """Get the user's home directory from the /etc/pyos.
162
163 Used by $HOME initialization in osh/state.py. Tilde expansion and
164 readline initialization use mem.GetValue('HOME').
165 """
166 uid = posix.getuid()
167 try:
168 e = pwd.getpwuid(uid)
169 except KeyError:
170 return None
171
172 return e.pw_dir
173
174
175def GetHomeDir(user_name):
176 # type: (str) -> Optional[str]
177 """For ~otheruser/src.
178
179 TODO: Should this be cached?
180 """
181 # http://linux.die.net/man/3/getpwnam
182 try:
183 e = pwd.getpwnam(user_name)
184 except KeyError:
185 return None
186
187 return e.pw_dir
188
189
190class PasswdEntry(object):
191
192 def __init__(self, pw_name, uid, gid):
193 # type: (str, int, int) -> None
194 self.pw_name = pw_name
195 self.pw_uid = uid
196 self.pw_gid = gid
197
198
199def GetAllUsers():
200 # type: () -> List[PasswdEntry]
201 users = [
202 PasswdEntry(u.pw_name, u.pw_uid, u.pw_gid) for u in pwd.getpwall()
203 ]
204 return users
205
206
207def GetUserName(uid):
208 # type: (int) -> str
209 try:
210 e = pwd.getpwuid(uid)
211 except KeyError:
212 return "<ERROR: Couldn't determine user name for uid %d>" % uid
213 else:
214 return e.pw_name
215
216
217def Time():
218 # type: () -> Tuple[float, float, float]
219 t = time.time() # calls gettimeofday() under the hood
220 u = resource.getrusage(resource.RUSAGE_SELF)
221 return t, u.ru_utime, u.ru_stime
222
223
224def PrintTimes():
225 # type: () -> None
226 utime, stime, cutime, cstime, elapsed = posix.times()
227 print("%dm%.3fs %dm%.3fs" %
228 (utime / 60, utime % 60, stime / 60, stime % 60))
229 print("%dm%.3fs %dm%.3fs" %
230 (cutime / 60, cutime % 60, cstime / 60, cstime % 60))
231
232
233# So builtin_misc.py doesn't depend on termios, which makes C++ translation
234# easier
235TERM_ICANON = termios.ICANON
236TERM_ECHO = termios.ECHO
237
238
239def PushTermAttrs(fd, mask):
240 # type: (int, int) -> Tuple[int, Any]
241 """Returns opaque type (void* in C++) to be reused in the PopTermAttrs()"""
242 # https://docs.python.org/2/library/termios.html
243 term_attrs = termios.tcgetattr(fd)
244
245 # Flip the bits in one field, e.g. ICANON to disable canonical (buffered)
246 # mode.
247 orig_local_modes = cast(int, term_attrs[3])
248 term_attrs[3] = orig_local_modes & mask
249
250 termios.tcsetattr(fd, termios.TCSANOW, term_attrs)
251 return orig_local_modes, term_attrs
252
253
254def PopTermAttrs(fd, orig_local_modes, term_attrs):
255 # type: (int, int, Any) -> None
256
257 term_attrs[3] = orig_local_modes
258 try:
259 termios.tcsetattr(fd, termios.TCSANOW, term_attrs)
260 except termios.error as e:
261 # Superficial fix for issue #1001. I'm not sure why we get errno.EIO,
262 # but we can't really handle it here. In C++ I guess we ignore the
263 # error.
264 pass
265
266
267def OsType():
268 # type: () -> str
269 """Compute $OSTYPE variable."""
270 return posix.uname()[0].lower()
271
272
273def InputAvailable(fd):
274 # type: (int) -> bool
275 # similar to lib/sh/input_avail.c in bash
276 # read, write, except
277 r, w, exc = select.select([fd], [], [fd], 0)
278 return len(r) != 0
279
280
281UNTRAPPED_SIGWINCH = -1
282
283
284class SignalSafe(object):
285 """State that is shared between the main thread and signal handlers.
286
287 See C++ implementation in cpp/core.h
288 """
289
290 def __init__(self):
291 # type: () -> None
292 self.pending_signals = [] # type: List[int]
293 self.last_sig_num = 0 # type: int
294 self.received_sigint = False
295 self.received_sigwinch = False
296 self.sigwinch_code = UNTRAPPED_SIGWINCH
297
298 def UpdateFromSignalHandler(self, sig_num, unused_frame):
299 # type: (int, Any) -> None
300 """Receive the given signal, and update shared state.
301
302 This method is registered as a Python signal handler.
303 """
304 self.pending_signals.append(sig_num)
305
306 if sig_num == signal.SIGINT:
307 self.received_sigint = True
308
309 if sig_num == signal.SIGWINCH:
310 self.received_sigwinch = True
311 sig_num = self.sigwinch_code # mutate param
312
313 self.last_sig_num = sig_num
314
315 def LastSignal(self):
316 # type: () -> int
317 """Return the number of the last signal that fired."""
318 return self.last_sig_num
319
320 def PollSigInt(self):
321 # type: () -> bool
322 """Has SIGINT received since the last time PollSigInt() was called?"""
323 result = self.received_sigint
324 self.received_sigint = False
325 return result
326
327 def SetSigWinchCode(self, code):
328 # type: (int) -> None
329 """Depending on whether or not SIGWINCH is trapped by a user, it is
330 expected to report a different code to `wait`.
331
332 SetSigwinchCode() lets us set which code is reported.
333 """
334 self.sigwinch_code = code
335
336 def PollSigWinch(self):
337 # type: () -> bool
338 """Has SIGWINCH been received since the last time PollSigWinch() was
339 called?"""
340 result = self.received_sigwinch
341 self.received_sigwinch = False
342 return result
343
344 def TakePendingSignals(self):
345 # type: () -> List[int]
346 # A note on signal-safety here. The main loop might be calling this function
347 # at the same time a signal is firing and appending to
348 # `self.pending_signals`. We can forgoe using a lock here
349 # (which would be problematic for the signal handler) because mutual
350 # exclusivity should be maintained by the atomic nature of pointer
351 # assignment (i.e. word-sized writes) on most modern platforms.
352 # The replacement run list is allocated before the swap, so it can be
353 # interuppted at any point without consequence.
354 # This means the signal handler always has exclusive access to
355 # `self.pending_signals`. In the worst case the signal handler might write to
356 # `new_queue` and the corresponding trap handler won't get executed
357 # until the main loop calls this function again.
358 # NOTE: It's important to distinguish between signal-safety an
359 # thread-safety here. Signals run in the same process context as the main
360 # loop, while concurrent threads do not and would have to worry about
361 # cache-coherence and instruction reordering.
362 new_queue = [] # type: List[int]
363 ret = self.pending_signals
364 self.pending_signals = new_queue
365 return ret
366
367 def ReuseEmptyList(self, empty_list):
368 # type: (List[int]) -> None
369 """This optimization only happens in C++."""
370 pass
371
372
373gSignalSafe = None # type: SignalSafe
374
375
376def InitSignalSafe():
377 # type: () -> SignalSafe
378 """Set global instance so the signal handler can access it."""
379 global gSignalSafe
380 gSignalSafe = SignalSafe()
381 return gSignalSafe
382
383
384def Sigaction(sig_num, handler):
385 # type: (int, Any) -> None
386 """Register a signal handler."""
387 signal.signal(sig_num, handler)
388
389
390def RegisterSignalInterest(sig_num):
391 # type: (int) -> None
392 """Have the kernel notify the main loop about the given signal."""
393 assert gSignalSafe is not None
394 signal.signal(sig_num, gSignalSafe.UpdateFromSignalHandler)
395
396
397def MakeDirCacheKey(path):
398 # type: (str) -> Tuple[str, int]
399 """Returns a pair (path with last modified time) that can be used to cache
400 directory accesses."""
401 st = posix.stat(path)
402 return (path, int(st.st_mtime))