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

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