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

1965 lines, 952 significant
1# Copyright 2016 Andy Chu. All rights reserved.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7"""
8process.py - Launch processes and manipulate file descriptors.
9"""
10from __future__ import print_function
11
12from errno import EACCES, EBADF, ECHILD, EINTR, ENOENT, ENOEXEC, EEXIST
13import fcntl as fcntl_
14from fcntl import F_DUPFD, F_GETFD, F_SETFD, FD_CLOEXEC
15from signal import (SIG_DFL, SIG_IGN, SIGINT, SIGPIPE, SIGQUIT, SIGTSTP,
16 SIGTTOU, SIGTTIN, SIGWINCH)
17
18from _devbuild.gen.id_kind_asdl import Id
19from _devbuild.gen.runtime_asdl import (job_state_e, job_state_t,
20 job_state_str, wait_status,
21 wait_status_t, RedirValue,
22 redirect_arg, redirect_arg_e, trace,
23 trace_t)
24from _devbuild.gen.syntax_asdl import (
25 loc_t,
26 redir_loc,
27 redir_loc_e,
28 redir_loc_t,
29)
30from _devbuild.gen.value_asdl import (value, value_e)
31from core import dev
32from core import error
33from core.error import e_die
34from core import pyutil
35from core import pyos
36from core import state
37from display import ui
38from core import util
39from data_lang import j8_lite
40from frontend import location
41from frontend import match
42from mycpp import mylib
43from mycpp.mylib import log, print_stderr, probe, tagswitch, iteritems
44
45import posix_ as posix
46from posix_ import (
47 # translated by mycpp and directly called! No wrapper!
48 WIFSIGNALED,
49 WIFEXITED,
50 WIFSTOPPED,
51 WEXITSTATUS,
52 WSTOPSIG,
53 WTERMSIG,
54 WNOHANG,
55 O_APPEND,
56 O_CREAT,
57 O_EXCL,
58 O_NONBLOCK,
59 O_NOCTTY,
60 O_RDONLY,
61 O_RDWR,
62 O_WRONLY,
63 O_TRUNC,
64)
65
66from typing import IO, List, Tuple, Dict, Optional, Any, cast, TYPE_CHECKING
67
68if TYPE_CHECKING:
69 from _devbuild.gen.runtime_asdl import cmd_value
70 from _devbuild.gen.syntax_asdl import command_t
71 from builtin import trap_osh
72 from core import optview
73 from core import pyos
74 from core.util import _DebugFile
75 from osh.cmd_eval import CommandEvaluator
76
77NO_FD = -1
78
79# Minimum file descriptor that the shell can use. Other descriptors can be
80# directly used by user programs, e.g. exec 9>&1
81#
82# Oil uses 100 because users are allowed TWO digits in frontend/lexer_def.py.
83# This is a compromise between bash (unlimited, but requires crazy
84# bookkeeping), and dash/zsh (10) and mksh (24)
85_SHELL_MIN_FD = 100
86
87# Style for 'jobs' builtin
88STYLE_DEFAULT = 0
89STYLE_LONG = 1
90STYLE_PID_ONLY = 2
91
92# To save on allocations in JobList::GetJobWithSpec()
93CURRENT_JOB_SPECS = ['', '%', '%%', '%+']
94
95
96class ctx_FileCloser(object):
97
98 def __init__(self, f):
99 # type: (mylib.LineReader) -> None
100 self.f = f
101
102 def __enter__(self):
103 # type: () -> None
104 pass
105
106 def __exit__(self, type, value, traceback):
107 # type: (Any, Any, Any) -> None
108 self.f.close()
109
110
111def InitInteractiveShell(signal_safe):
112 # type: (pyos.SignalSafe) -> None
113 """Called when initializing an interactive shell."""
114
115 # The shell itself should ignore Ctrl-\.
116 pyos.sigaction(SIGQUIT, SIG_IGN)
117
118 # This prevents Ctrl-Z from suspending OSH in interactive mode.
119 pyos.sigaction(SIGTSTP, SIG_IGN)
120
121 # More signals from
122 # https://www.gnu.org/software/libc/manual/html_node/Initializing-the-Shell.html
123 # (but not SIGCHLD)
124 pyos.sigaction(SIGTTOU, SIG_IGN)
125 pyos.sigaction(SIGTTIN, SIG_IGN)
126
127 # Register a callback to receive terminal width changes.
128 # NOTE: In line_input.c, we turned off rl_catch_sigwinch.
129
130 # This is ALWAYS on, which means that it can cause EINTR, and wait() and
131 # read() have to handle it
132 pyos.RegisterSignalInterest(SIGWINCH)
133
134
135def SaveFd(fd):
136 # type: (int) -> int
137 saved = fcntl_.fcntl(fd, F_DUPFD, _SHELL_MIN_FD) # type: int
138 return saved
139
140
141class _RedirFrame(object):
142
143 def __init__(self, saved_fd, orig_fd, forget):
144 # type: (int, int, bool) -> None
145 self.saved_fd = saved_fd
146 self.orig_fd = orig_fd
147 self.forget = forget
148
149
150class _FdFrame(object):
151
152 def __init__(self):
153 # type: () -> None
154 self.saved = [] # type: List[_RedirFrame]
155 self.need_wait = [] # type: List[Process]
156
157 def Forget(self):
158 # type: () -> None
159 """For exec 1>&2."""
160 for rf in reversed(self.saved):
161 if rf.saved_fd != NO_FD and rf.forget:
162 posix.close(rf.saved_fd)
163
164 del self.saved[:] # like list.clear() in Python 3.3
165 del self.need_wait[:]
166
167 def __repr__(self):
168 # type: () -> str
169 return '<_FdFrame %s>' % self.saved
170
171
172class FdState(object):
173 """File descriptor state for the current process.
174
175 For example, you can do 'myfunc > out.txt' without forking. Child
176 processes inherit our state.
177 """
178
179 def __init__(
180 self,
181 errfmt, # type: ui.ErrorFormatter
182 job_control, # type: JobControl
183 job_list, # type: JobList
184 mem, # type: state.Mem
185 tracer, # type: Optional[dev.Tracer]
186 waiter, # type: Optional[Waiter]
187 exec_opts, # type: optview.Exec
188 ):
189 # type: (...) -> None
190 """
191 Args:
192 errfmt: for errors
193 job_list: For keeping track of _HereDocWriterThunk
194 """
195 self.errfmt = errfmt
196 self.job_control = job_control
197 self.job_list = job_list
198 self.cur_frame = _FdFrame() # for the top level
199 self.stack = [self.cur_frame]
200 self.mem = mem
201 self.tracer = tracer
202 self.waiter = waiter
203 self.exec_opts = exec_opts
204
205 def Open(self, path):
206 # type: (str) -> mylib.LineReader
207 """Opens a path for read, but moves it out of the reserved 3-9 fd
208 range.
209
210 Returns:
211 A Python file object. The caller is responsible for Close().
212
213 Raises:
214 IOError or OSError if the path can't be found. (This is Python-induced wart)
215 """
216 fd_mode = O_RDONLY
217 f = self._Open(path, 'r', fd_mode)
218
219 # Hacky downcast
220 return cast('mylib.LineReader', f)
221
222 # used for util.DebugFile
223 def OpenForWrite(self, path):
224 # type: (str) -> mylib.Writer
225 fd_mode = O_CREAT | O_RDWR
226 f = self._Open(path, 'w', fd_mode)
227
228 # Hacky downcast
229 return cast('mylib.Writer', f)
230
231 def _Open(self, path, c_mode, fd_mode):
232 # type: (str, str, int) -> IO[str]
233 fd = posix.open(path, fd_mode, 0o666) # may raise OSError
234
235 # Immediately move it to a new location
236 new_fd = SaveFd(fd)
237 posix.close(fd)
238
239 # Return a Python file handle
240 f = posix.fdopen(new_fd, c_mode) # may raise IOError
241 return f
242
243 def _WriteFdToMem(self, fd_name, fd):
244 # type: (str, int) -> None
245 if self.mem:
246 # setvar, not setref
247 state.OshLanguageSetValue(self.mem, location.LName(fd_name),
248 value.Str(str(fd)))
249
250 def _ReadFdFromMem(self, fd_name):
251 # type: (str) -> int
252 val = self.mem.GetValue(fd_name)
253 if val.tag() == value_e.Str:
254 try:
255 return int(cast(value.Str, val).s)
256 except ValueError:
257 return NO_FD
258 return NO_FD
259
260 def _PushSave(self, fd):
261 # type: (int) -> bool
262 """Save fd to a new location and remember to restore it later."""
263 #log('---- _PushSave %s', fd)
264 ok = True
265 try:
266 new_fd = SaveFd(fd)
267 except (IOError, OSError) as e:
268 ok = False
269 # Example program that causes this error: exec 4>&1. Descriptor 4 isn't
270 # open.
271 # This seems to be ignored in dash too in savefd()?
272 if e.errno != EBADF:
273 raise
274 if ok:
275 posix.close(fd)
276 fcntl_.fcntl(new_fd, F_SETFD, FD_CLOEXEC)
277 self.cur_frame.saved.append(_RedirFrame(new_fd, fd, True))
278 else:
279 # if we got EBADF, we still need to close the original on Pop()
280 self._PushClose(fd)
281
282 return ok
283
284 def _PushDup(self, fd1, blame_loc):
285 # type: (int, redir_loc_t) -> int
286 """Save fd2 in a higher range, and dup fd1 onto fd2.
287
288 Returns whether F_DUPFD/dup2 succeeded, and the new descriptor.
289 """
290 UP_loc = blame_loc
291 if blame_loc.tag() == redir_loc_e.VarName:
292 fd2_name = cast(redir_loc.VarName, UP_loc).name
293 try:
294 # F_DUPFD: GREATER than range
295 new_fd = fcntl_.fcntl(fd1, F_DUPFD, _SHELL_MIN_FD) # type: int
296 except (IOError, OSError) as e:
297 if e.errno == EBADF:
298 print_stderr('F_DUPFD fd %d: %s' %
299 (fd1, pyutil.strerror(e)))
300 return NO_FD
301 else:
302 raise # this redirect failed
303
304 self._WriteFdToMem(fd2_name, new_fd)
305
306 elif blame_loc.tag() == redir_loc_e.Fd:
307 fd2 = cast(redir_loc.Fd, UP_loc).fd
308
309 if fd1 == fd2:
310 # The user could have asked for it to be open on descriptor 3, but open()
311 # already returned 3, e.g. echo 3>out.txt
312 return NO_FD
313
314 # Check the validity of fd1 before _PushSave(fd2)
315 try:
316 fcntl_.fcntl(fd1, F_GETFD)
317 except (IOError, OSError) as e:
318 print_stderr('F_GETFD fd %d: %s' % (fd1, pyutil.strerror(e)))
319 raise
320
321 need_restore = self._PushSave(fd2)
322
323 #log('==== dup2 %s %s\n' % (fd1, fd2))
324 try:
325 posix.dup2(fd1, fd2)
326 except (IOError, OSError) as e:
327 # bash/dash give this error too, e.g. for 'echo hi 1>&3'
328 print_stderr('dup2(%d, %d): %s' %
329 (fd1, fd2, pyutil.strerror(e)))
330
331 # Restore and return error
332 if need_restore:
333 rf = self.cur_frame.saved.pop()
334 posix.dup2(rf.saved_fd, rf.orig_fd)
335 posix.close(rf.saved_fd)
336
337 raise # this redirect failed
338
339 new_fd = fd2
340
341 else:
342 raise AssertionError()
343
344 return new_fd
345
346 def _PushCloseFd(self, blame_loc):
347 # type: (redir_loc_t) -> bool
348 """For 2>&-"""
349 # exec {fd}>&- means close the named descriptor
350
351 UP_loc = blame_loc
352 if blame_loc.tag() == redir_loc_e.VarName:
353 fd_name = cast(redir_loc.VarName, UP_loc).name
354 fd = self._ReadFdFromMem(fd_name)
355 if fd == NO_FD:
356 return False
357
358 elif blame_loc.tag() == redir_loc_e.Fd:
359 fd = cast(redir_loc.Fd, UP_loc).fd
360
361 else:
362 raise AssertionError()
363
364 self._PushSave(fd)
365
366 return True
367
368 def _PushClose(self, fd):
369 # type: (int) -> None
370 self.cur_frame.saved.append(_RedirFrame(NO_FD, fd, False))
371
372 def _PushWait(self, proc):
373 # type: (Process) -> None
374 self.cur_frame.need_wait.append(proc)
375
376 def _ApplyRedirect(self, r):
377 # type: (RedirValue) -> None
378 arg = r.arg
379 UP_arg = arg
380 with tagswitch(arg) as case:
381
382 if case(redirect_arg_e.Path):
383 arg = cast(redirect_arg.Path, UP_arg)
384 # noclobber flag is OR'd with other flags when allowed
385 noclobber_mode = O_EXCL if self.exec_opts.noclobber() else 0
386 if r.op_id in (Id.Redir_Great, Id.Redir_AndGreat): # > &>
387 # NOTE: This is different than >| because it respects noclobber, but
388 # that option is almost never used. See test/wild.sh.
389 mode = O_CREAT | O_WRONLY | O_TRUNC | noclobber_mode
390 elif r.op_id == Id.Redir_Clobber: # >|
391 mode = O_CREAT | O_WRONLY | O_TRUNC
392 elif r.op_id in (Id.Redir_DGreat,
393 Id.Redir_AndDGreat): # >> &>>
394 mode = O_CREAT | O_WRONLY | O_APPEND | noclobber_mode
395 elif r.op_id == Id.Redir_Less: # <
396 mode = O_RDONLY
397 elif r.op_id == Id.Redir_LessGreat: # <>
398 mode = O_CREAT | O_RDWR
399 else:
400 raise NotImplementedError(r.op_id)
401
402 # NOTE: 0666 is affected by umask, all shells use it.
403 try:
404 open_fd = posix.open(arg.filename, mode, 0o666)
405 except (IOError, OSError) as e:
406 if e.errno == EEXIST and self.exec_opts.noclobber():
407 extra = ' (noclobber)'
408 else:
409 extra = ''
410 self.errfmt.Print_(
411 "Can't open %r: %s%s" %
412 (arg.filename, pyutil.strerror(e), extra),
413 blame_loc=r.op_loc)
414 raise # redirect failed
415
416 new_fd = self._PushDup(open_fd, r.loc)
417 if new_fd != NO_FD:
418 posix.close(open_fd)
419
420 # Now handle &> and &>> and their variants. These pairs are the same:
421 #
422 # stdout_stderr.py &> out-err.txt
423 # stdout_stderr.py > out-err.txt 2>&1
424 #
425 # stdout_stderr.py 3&> out-err.txt
426 # stdout_stderr.py 3> out-err.txt 2>&3
427 #
428 # Ditto for {fd}> and {fd}&>
429
430 if r.op_id in (Id.Redir_AndGreat, Id.Redir_AndDGreat):
431 self._PushDup(new_fd, redir_loc.Fd(2))
432
433 elif case(redirect_arg_e.CopyFd): # e.g. echo hi 1>&2
434 arg = cast(redirect_arg.CopyFd, UP_arg)
435
436 if r.op_id == Id.Redir_GreatAnd: # 1>&2
437 self._PushDup(arg.target_fd, r.loc)
438
439 elif r.op_id == Id.Redir_LessAnd: # 0<&5
440 # The only difference between >& and <& is the default file
441 # descriptor argument.
442 self._PushDup(arg.target_fd, r.loc)
443
444 else:
445 raise NotImplementedError()
446
447 elif case(redirect_arg_e.MoveFd): # e.g. echo hi 5>&6-
448 arg = cast(redirect_arg.MoveFd, UP_arg)
449 new_fd = self._PushDup(arg.target_fd, r.loc)
450 if new_fd != NO_FD:
451 posix.close(arg.target_fd)
452
453 UP_loc = r.loc
454 if r.loc.tag() == redir_loc_e.Fd:
455 fd = cast(redir_loc.Fd, UP_loc).fd
456 else:
457 fd = NO_FD
458
459 self.cur_frame.saved.append(_RedirFrame(new_fd, fd, False))
460
461 elif case(redirect_arg_e.CloseFd): # e.g. echo hi 5>&-
462 self._PushCloseFd(r.loc)
463
464 elif case(redirect_arg_e.HereDoc):
465 arg = cast(redirect_arg.HereDoc, UP_arg)
466
467 # NOTE: Do these descriptors have to be moved out of the range 0-9?
468 read_fd, write_fd = posix.pipe()
469
470 self._PushDup(read_fd, r.loc) # stdin is now the pipe
471
472 # We can't close like we do in the filename case above? The writer can
473 # get a "broken pipe".
474 self._PushClose(read_fd)
475
476 thunk = _HereDocWriterThunk(write_fd, arg.body)
477
478 # Use PIPE_SIZE to save a process in the case of small here
479 # docs, which are the common case. (dash does this.)
480
481 # Note: could instrument this to see how often it happens.
482 # Though strace -ff can also work.
483 start_process = len(arg.body) > 4096
484 #start_process = True
485
486 if start_process:
487 here_proc = Process(thunk, self.job_control, self.job_list,
488 self.tracer)
489
490 # NOTE: we could close the read pipe here, but it doesn't really
491 # matter because we control the code.
492 here_proc.StartProcess(trace.HereDoc)
493 #log('Started %s as %d', here_proc, pid)
494 self._PushWait(here_proc)
495
496 # Now that we've started the child, close it in the parent.
497 posix.close(write_fd)
498
499 else:
500 posix.write(write_fd, arg.body)
501 posix.close(write_fd)
502
503 def Push(self, redirects, err_out):
504 # type: (List[RedirValue], List[error.IOError_OSError]) -> None
505 """Apply a group of redirects and remember to undo them."""
506
507 #log('> fd_state.Push %s', redirects)
508 new_frame = _FdFrame()
509 self.stack.append(new_frame)
510 self.cur_frame = new_frame
511
512 for r in redirects:
513 #log('apply %s', r)
514 with ui.ctx_Location(self.errfmt, r.op_loc):
515 try:
516 self._ApplyRedirect(r)
517 except (IOError, OSError) as e:
518 err_out.append(e)
519 # This can fail too
520 self.Pop(err_out)
521 return # for bad descriptor, etc.
522
523 def PushStdinFromPipe(self, r):
524 # type: (int) -> bool
525 """Save the current stdin and make it come from descriptor 'r'.
526
527 'r' is typically the read-end of a pipe. For 'lastpipe'/ZSH
528 semantics of
529
530 echo foo | read line; echo $line
531 """
532 new_frame = _FdFrame()
533 self.stack.append(new_frame)
534 self.cur_frame = new_frame
535
536 self._PushDup(r, redir_loc.Fd(0))
537 return True
538
539 def Pop(self, err_out):
540 # type: (List[error.IOError_OSError]) -> None
541 frame = self.stack.pop()
542 #log('< Pop %s', frame)
543 for rf in reversed(frame.saved):
544 if rf.saved_fd == NO_FD:
545 #log('Close %d', orig)
546 try:
547 posix.close(rf.orig_fd)
548 except (IOError, OSError) as e:
549 err_out.append(e)
550 log('Error closing descriptor %d: %s', rf.orig_fd,
551 pyutil.strerror(e))
552 return
553 else:
554 try:
555 posix.dup2(rf.saved_fd, rf.orig_fd)
556 except (IOError, OSError) as e:
557 err_out.append(e)
558 log('dup2(%d, %d) error: %s', rf.saved_fd, rf.orig_fd,
559 pyutil.strerror(e))
560 #log('fd state:')
561 #posix.system('ls -l /proc/%s/fd' % posix.getpid())
562 return
563 posix.close(rf.saved_fd)
564 #log('dup2 %s %s', saved, orig)
565
566 # Wait for here doc processes to finish.
567 for proc in frame.need_wait:
568 unused_status = proc.Wait(self.waiter)
569
570 def MakePermanent(self):
571 # type: () -> None
572 self.cur_frame.Forget()
573
574
575class ChildStateChange(object):
576
577 def __init__(self):
578 # type: () -> None
579 """Empty constructor for mycpp."""
580 pass
581
582 def Apply(self):
583 # type: () -> None
584 raise NotImplementedError()
585
586 def ApplyFromParent(self, proc):
587 # type: (Process) -> None
588 """Noop for all state changes other than SetPgid for mycpp."""
589 pass
590
591
592class StdinFromPipe(ChildStateChange):
593
594 def __init__(self, pipe_read_fd, w):
595 # type: (int, int) -> None
596 self.r = pipe_read_fd
597 self.w = w
598
599 def __repr__(self):
600 # type: () -> str
601 return '<StdinFromPipe %d %d>' % (self.r, self.w)
602
603 def Apply(self):
604 # type: () -> None
605 posix.dup2(self.r, 0)
606 posix.close(self.r) # close after dup
607
608 posix.close(self.w) # we're reading from the pipe, not writing
609 #log('child CLOSE w %d pid=%d', self.w, posix.getpid())
610
611
612class StdoutToPipe(ChildStateChange):
613
614 def __init__(self, r, pipe_write_fd):
615 # type: (int, int) -> None
616 self.r = r
617 self.w = pipe_write_fd
618
619 def __repr__(self):
620 # type: () -> str
621 return '<StdoutToPipe %d %d>' % (self.r, self.w)
622
623 def Apply(self):
624 # type: () -> None
625 posix.dup2(self.w, 1)
626 posix.close(self.w) # close after dup
627
628 posix.close(self.r) # we're writing to the pipe, not reading
629 #log('child CLOSE r %d pid=%d', self.r, posix.getpid())
630
631
632INVALID_PGID = -1
633# argument to setpgid() that means the process is its own leader
634OWN_LEADER = 0
635
636
637class SetPgid(ChildStateChange):
638
639 def __init__(self, pgid, tracer):
640 # type: (int, dev.Tracer) -> None
641 self.pgid = pgid
642 self.tracer = tracer
643
644 def Apply(self):
645 # type: () -> None
646 try:
647 posix.setpgid(0, self.pgid)
648 except (IOError, OSError) as e:
649 self.tracer.OtherMessage(
650 'osh: child %d failed to set its process group to %d: %s' %
651 (posix.getpid(), self.pgid, pyutil.strerror(e)))
652
653 def ApplyFromParent(self, proc):
654 # type: (Process) -> None
655 try:
656 posix.setpgid(proc.pid, self.pgid)
657 except (IOError, OSError) as e:
658 self.tracer.OtherMessage(
659 'osh: parent failed to set process group for PID %d to %d: %s'
660 % (proc.pid, self.pgid, pyutil.strerror(e)))
661
662
663class ExternalProgram(object):
664 """The capability to execute an external program like 'ls'."""
665
666 def __init__(
667 self,
668 hijack_shebang, # type: str
669 fd_state, # type: FdState
670 errfmt, # type: ui.ErrorFormatter
671 debug_f, # type: _DebugFile
672 ):
673 # type: (...) -> None
674 """
675 Args:
676 hijack_shebang: The path of an interpreter to run instead of the one
677 specified in the shebang line. May be empty.
678 """
679 self.hijack_shebang = hijack_shebang
680 self.fd_state = fd_state
681 self.errfmt = errfmt
682 self.debug_f = debug_f
683
684 def Exec(self, argv0_path, cmd_val, environ):
685 # type: (str, cmd_value.Argv, Dict[str, str]) -> None
686 """Execute a program and exit this process.
687
688 Called by: ls / exec ls / ( ls / )
689 """
690 probe('process', 'ExternalProgram_Exec', argv0_path)
691 self._Exec(argv0_path, cmd_val.argv, cmd_val.arg_locs[0], environ,
692 True)
693 assert False, "This line should never execute" # NO RETURN
694
695 def _Exec(self, argv0_path, argv, argv0_loc, environ, should_retry):
696 # type: (str, List[str], loc_t, Dict[str, str], bool) -> None
697 if len(self.hijack_shebang):
698 opened = True
699 try:
700 f = self.fd_state.Open(argv0_path)
701 except (IOError, OSError) as e:
702 opened = False
703
704 if opened:
705 with ctx_FileCloser(f):
706 # Test if the shebang looks like a shell. TODO: The file might be
707 # binary with no newlines, so read 80 bytes instead of readline().
708
709 #line = f.read(80) # type: ignore # TODO: fix this
710 line = f.readline()
711
712 if match.ShouldHijack(line):
713 h_argv = [self.hijack_shebang, argv0_path]
714 h_argv.extend(argv[1:])
715 argv = h_argv
716 argv0_path = self.hijack_shebang
717 self.debug_f.writeln('Hijacked: %s' % argv0_path)
718 else:
719 #self.debug_f.log('Not hijacking %s (%r)', argv, line)
720 pass
721
722 try:
723 posix.execve(argv0_path, argv, environ)
724 except (IOError, OSError) as e:
725 # Run with /bin/sh when ENOEXEC error (no shebang). All shells do this.
726 if e.errno == ENOEXEC and should_retry:
727 new_argv = ['/bin/sh', argv0_path]
728 new_argv.extend(argv[1:])
729 self._Exec('/bin/sh', new_argv, argv0_loc, environ, False)
730 # NO RETURN
731
732 # Would be nice: when the path is relative and ENOENT: print PWD and do
733 # spelling correction?
734
735 self.errfmt.Print_(
736 "Can't execute %r: %s" % (argv0_path, pyutil.strerror(e)),
737 argv0_loc)
738
739 # POSIX mentions 126 and 127 for two specific errors. The rest are
740 # unspecified.
741 #
742 # http://pubs.opengroup.org/onlinepubs/9699919799.2016edition/utilities/V3_chap02.html#tag_18_08_02
743 if e.errno == EACCES:
744 status = 126
745 elif e.errno == ENOENT:
746 # TODO: most shells print 'command not found', rather than strerror()
747 # == "No such file or directory". That's better because it's at the
748 # end of the path search, and we're never searching for a directory.
749 status = 127
750 else:
751 # dash uses 2, but we use that for parse errors. This seems to be
752 # consistent with mksh and zsh.
753 status = 127
754
755 posix._exit(status)
756 # NO RETURN
757
758
759class Thunk(object):
760 """Abstract base class for things runnable in another process."""
761
762 def __init__(self):
763 # type: () -> None
764 """Empty constructor for mycpp."""
765 pass
766
767 def Run(self):
768 # type: () -> None
769 """Returns a status code."""
770 raise NotImplementedError()
771
772 def UserString(self):
773 # type: () -> str
774 """Display for the 'jobs' list."""
775 raise NotImplementedError()
776
777 def __repr__(self):
778 # type: () -> str
779 return self.UserString()
780
781
782class ExternalThunk(Thunk):
783 """An external executable."""
784
785 def __init__(self, ext_prog, argv0_path, cmd_val, environ):
786 # type: (ExternalProgram, str, cmd_value.Argv, Dict[str, str]) -> None
787 self.ext_prog = ext_prog
788 self.argv0_path = argv0_path
789 self.cmd_val = cmd_val
790 self.environ = environ
791
792 def UserString(self):
793 # type: () -> str
794
795 # NOTE: This is the format the Tracer uses.
796 # bash displays sleep $n & (code)
797 # but OSH displays sleep 1 & (argv array)
798 # We could switch the former but I'm not sure it's necessary.
799 tmp = [j8_lite.MaybeShellEncode(a) for a in self.cmd_val.argv]
800 return '[process] %s' % ' '.join(tmp)
801
802 def Run(self):
803 # type: () -> None
804 """An ExternalThunk is run in parent for the exec builtin."""
805 self.ext_prog.Exec(self.argv0_path, self.cmd_val, self.environ)
806
807
808class SubProgramThunk(Thunk):
809 """A subprogram that can be executed in another process."""
810
811 def __init__(self, cmd_ev, node, trap_state, multi_trace, inherit_errexit,
812 inherit_errtrace):
813 # type: (CommandEvaluator, command_t, trap_osh.TrapState, dev.MultiTracer, bool, bool) -> None
814 self.cmd_ev = cmd_ev
815 self.node = node
816 self.trap_state = trap_state
817 self.multi_trace = multi_trace
818 self.inherit_errexit = inherit_errexit # for bash errexit compatibility
819 self.inherit_errtrace = inherit_errtrace # for bash errtrace compatibility
820
821 def UserString(self):
822 # type: () -> str
823
824 # NOTE: These can be pieces of a pipeline, so they're arbitrary nodes.
825 # TODO: Extract SPIDS from node to display source? Note that
826 # CompoundStatus also has locations of each pipeline component; see
827 # Executor.RunPipeline()
828 thunk_str = ui.CommandType(self.node)
829 return '[subprog] %s' % thunk_str
830
831 def Run(self):
832 # type: () -> None
833 #self.errfmt.OneLineErrExit() # don't quote code in child processes
834 probe('process', 'SubProgramThunk_Run')
835
836 # TODO: break circular dep. Bit flags could go in ASDL or headers.
837 from osh import cmd_eval
838
839 # signal handlers aren't inherited
840 self.trap_state.ClearForSubProgram(self.inherit_errtrace)
841
842 # NOTE: may NOT return due to exec().
843 if not self.inherit_errexit:
844 self.cmd_ev.mutable_opts.DisableErrExit()
845 try:
846 # optimize to eliminate redundant subshells like ( echo hi ) | wc -l etc.
847 self.cmd_ev.ExecuteAndCatch(
848 self.node,
849 cmd_eval.OptimizeSubshells | cmd_eval.MarkLastCommands)
850 status = self.cmd_ev.LastStatus()
851 # NOTE: We ignore the is_fatal return value. The user should set -o
852 # errexit so failures in subprocesses cause failures in the parent.
853 except util.UserExit as e:
854 status = e.status
855
856 # Handle errors in a subshell. These two cases are repeated from main()
857 # and the core/completion.py hook.
858 except KeyboardInterrupt:
859 print('')
860 status = 130 # 128 + 2
861 except (IOError, OSError) as e:
862 print_stderr('oils I/O error (subprogram): %s' %
863 pyutil.strerror(e))
864 status = 2
865
866 # If ProcessInit() doesn't turn off buffering, this is needed before
867 # _exit()
868 pyos.FlushStdout()
869
870 self.multi_trace.WriteDumps()
871
872 # We do NOT want to raise SystemExit here. Otherwise dev.Tracer::Pop()
873 # gets called in BOTH processes.
874 # The crash dump seems to be unaffected.
875 posix._exit(status)
876
877
878class _HereDocWriterThunk(Thunk):
879 """Write a here doc to one end of a pipe.
880
881 May be be executed in either a child process or the main shell
882 process.
883 """
884
885 def __init__(self, w, body_str):
886 # type: (int, str) -> None
887 self.w = w
888 self.body_str = body_str
889
890 def UserString(self):
891 # type: () -> str
892
893 # You can hit Ctrl-Z and the here doc writer will be suspended! Other
894 # shells don't have this problem because they use temp files! That's a bit
895 # unfortunate.
896 return '[here doc writer]'
897
898 def Run(self):
899 # type: () -> None
900 """do_exit: For small pipelines."""
901 probe('process', 'HereDocWriterThunk_Run')
902 #log('Writing %r', self.body_str)
903 posix.write(self.w, self.body_str)
904 #log('Wrote %r', self.body_str)
905 posix.close(self.w)
906 #log('Closed %d', self.w)
907
908 posix._exit(0)
909
910
911class Job(object):
912 """Interface for both Process and Pipeline.
913
914 They both can be put in the background and waited on.
915
916 Confusing thing about pipelines in the background: They have TOO MANY NAMES.
917
918 sleep 1 | sleep 2 &
919
920 - The LAST PID is what's printed at the prompt. This is $!, a PROCESS ID and
921 not a JOB ID.
922 # https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#Special-Parameters
923 - The process group leader (setpgid) is the FIRST PID.
924 - It's also %1 or %+. The last job started.
925 """
926
927 def __init__(self):
928 # type: () -> None
929 # Initial state with & or Ctrl-Z is Running.
930 self.state = job_state_e.Running
931 self.job_id = -1
932 self.in_background = False
933
934 def DisplayJob(self, job_id, f, style):
935 # type: (int, mylib.Writer, int) -> None
936 raise NotImplementedError()
937
938 def State(self):
939 # type: () -> job_state_t
940 return self.state
941
942 def ProcessGroupId(self):
943 # type: () -> int
944 """Return the process group ID associated with this job."""
945 raise NotImplementedError()
946
947 def JobWait(self, waiter):
948 # type: (Waiter) -> wait_status_t
949 """Wait for this process/pipeline to be stopped or finished."""
950 raise NotImplementedError()
951
952 def SetBackground(self):
953 # type: () -> None
954 """Record that this job is running in the background."""
955 self.in_background = True
956
957 def SetForeground(self):
958 # type: () -> None
959 """Record that this job is running in the foreground."""
960 self.in_background = False
961
962
963class Process(Job):
964 """A process to run.
965
966 TODO: Should we make it clear that this is a FOREGROUND process? A
967 background process is wrapped in a "job". It is unevaluated.
968
969 It provides an API to manipulate file descriptor state in parent and child.
970 """
971
972 def __init__(self, thunk, job_control, job_list, tracer):
973 # type: (Thunk, JobControl, JobList, dev.Tracer) -> None
974 """
975 Args:
976 thunk: Thunk instance
977 job_list: for process bookkeeping
978 """
979 Job.__init__(self)
980 assert isinstance(thunk, Thunk), thunk
981 self.thunk = thunk
982 self.job_control = job_control
983 self.job_list = job_list
984 self.tracer = tracer
985
986 # For pipelines
987 self.parent_pipeline = None # type: Pipeline
988 self.state_changes = [] # type: List[ChildStateChange]
989 self.close_r = -1
990 self.close_w = -1
991
992 self.pid = -1
993 self.status = -1
994
995 def Init_ParentPipeline(self, pi):
996 # type: (Pipeline) -> None
997 """For updating PIPESTATUS."""
998 self.parent_pipeline = pi
999
1000 def __repr__(self):
1001 # type: () -> str
1002
1003 # note: be wary of infinite mutual recursion
1004 #s = ' %s' % self.parent_pipeline if self.parent_pipeline else ''
1005 #return '<Process %s%s>' % (self.thunk, s)
1006 return '<Process %s %s>' % (_JobStateStr(self.state), self.thunk)
1007
1008 def ProcessGroupId(self):
1009 # type: () -> int
1010 """Returns the group ID of this process."""
1011 # This should only ever be called AFTER the process has started
1012 assert self.pid != -1
1013 if self.parent_pipeline:
1014 # XXX: Maybe we should die here instead? Unclear if this branch
1015 # should even be reachable with the current builtins.
1016 return self.parent_pipeline.ProcessGroupId()
1017
1018 return self.pid
1019
1020 def DisplayJob(self, job_id, f, style):
1021 # type: (int, mylib.Writer, int) -> None
1022 if job_id == -1:
1023 job_id_str = ' '
1024 else:
1025 job_id_str = '%%%d' % job_id
1026 if style == STYLE_PID_ONLY:
1027 f.write('%d\n' % self.pid)
1028 else:
1029 f.write('%s %d %7s ' %
1030 (job_id_str, self.pid, _JobStateStr(self.state)))
1031 f.write(self.thunk.UserString())
1032 f.write('\n')
1033
1034 def AddStateChange(self, s):
1035 # type: (ChildStateChange) -> None
1036 self.state_changes.append(s)
1037
1038 def AddPipeToClose(self, r, w):
1039 # type: (int, int) -> None
1040 self.close_r = r
1041 self.close_w = w
1042
1043 def MaybeClosePipe(self):
1044 # type: () -> None
1045 if self.close_r != -1:
1046 posix.close(self.close_r)
1047 posix.close(self.close_w)
1048
1049 def StartProcess(self, why):
1050 # type: (trace_t) -> int
1051 """Start this process with fork(), handling redirects."""
1052 pid = posix.fork()
1053 if pid < 0:
1054 # When does this happen?
1055 e_die('Fatal error in posix.fork()')
1056
1057 elif pid == 0: # child
1058 # Note: this happens in BOTH interactive and non-interactive shells.
1059 # We technically don't need to do most of it in non-interactive, since we
1060 # did not change state in InitInteractiveShell().
1061
1062 for st in self.state_changes:
1063 st.Apply()
1064
1065 # Python sets SIGPIPE handler to SIG_IGN by default. Child processes
1066 # shouldn't have this.
1067 # https://docs.python.org/2/library/signal.html
1068 # See Python/pythonrun.c.
1069 pyos.sigaction(SIGPIPE, SIG_DFL)
1070
1071 # Respond to Ctrl-\ (core dump)
1072 pyos.sigaction(SIGQUIT, SIG_DFL)
1073
1074 # Only standalone children should get Ctrl-Z. Pipelines remain in the
1075 # foreground because suspending them is difficult with our 'lastpipe'
1076 # semantics.
1077 pid = posix.getpid()
1078 if posix.getpgid(0) == pid and self.parent_pipeline is None:
1079 pyos.sigaction(SIGTSTP, SIG_DFL)
1080
1081 # More signals from
1082 # https://www.gnu.org/software/libc/manual/html_node/Launching-Jobs.html
1083 # (but not SIGCHLD)
1084 pyos.sigaction(SIGTTOU, SIG_DFL)
1085 pyos.sigaction(SIGTTIN, SIG_DFL)
1086
1087 self.tracer.OnNewProcess(pid)
1088 # clear foreground pipeline for subshells
1089 self.thunk.Run()
1090 # Never returns
1091
1092 #log('STARTED process %s, pid = %d', self, pid)
1093 self.tracer.OnProcessStart(pid, why)
1094
1095 # Class invariant: after the process is started, it stores its PID.
1096 self.pid = pid
1097
1098 # SetPgid needs to be applied from the child and the parent to avoid
1099 # racing in calls to tcsetpgrp() in the parent. See APUE sec. 9.2.
1100 for st in self.state_changes:
1101 st.ApplyFromParent(self)
1102
1103 # Program invariant: We keep track of every child process!
1104 self.job_list.AddChildProcess(pid, self)
1105
1106 return pid
1107
1108 def Wait(self, waiter):
1109 # type: (Waiter) -> int
1110 """Wait for this process to finish."""
1111 while self.state == job_state_e.Running:
1112 # Only return if there's nothing to wait for. Keep waiting if we were
1113 # interrupted with a signal.
1114 if waiter.WaitForOne() == W1_ECHILD:
1115 break
1116
1117 assert self.status >= 0, self.status
1118 return self.status
1119
1120 def JobWait(self, waiter):
1121 # type: (Waiter) -> wait_status_t
1122 # wait builtin can be interrupted
1123 while self.state == job_state_e.Running:
1124 result = waiter.WaitForOne()
1125
1126 if result >= 0: # signal
1127 return wait_status.Cancelled(result)
1128
1129 if result == W1_ECHILD:
1130 break
1131
1132 return wait_status.Proc(self.status)
1133
1134 def WhenStopped(self, stop_sig):
1135 # type: (int) -> None
1136
1137 # 128 is a shell thing
1138 # https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
1139 self.status = 128 + stop_sig
1140 self.state = job_state_e.Stopped
1141
1142 if self.job_id == -1:
1143 # This process was started in the foreground
1144 self.job_list.AddJob(self)
1145
1146 if not self.in_background:
1147 self.job_control.MaybeTakeTerminal()
1148 self.SetBackground()
1149
1150 def WhenDone(self, pid, status):
1151 # type: (int, int) -> None
1152 """Called by the Waiter when this Process finishes."""
1153
1154 #log('Process WhenDone %d %d', pid, status)
1155 assert pid == self.pid, 'Expected %d, got %d' % (self.pid, pid)
1156 self.status = status
1157 self.state = job_state_e.Done
1158 if self.parent_pipeline:
1159 self.parent_pipeline.WhenDone(pid, status)
1160 else:
1161 if self.job_id != -1:
1162 # Job might have been brought to the foreground after being
1163 # assigned a job ID.
1164 if self.in_background:
1165 # TODO: bash only prints this interactively
1166 print_stderr('[%%%d] PID %d Done' %
1167 (self.job_id, self.pid))
1168
1169 self.job_list.RemoveJob(self.job_id)
1170
1171 self.job_list.RemoveChildProcess(self.pid)
1172
1173 if not self.in_background:
1174 self.job_control.MaybeTakeTerminal()
1175
1176 def RunProcess(self, waiter, why):
1177 # type: (Waiter, trace_t) -> int
1178 """Run this process synchronously."""
1179 self.StartProcess(why)
1180 # ShellExecutor might be calling this for the last part of a pipeline.
1181 if self.parent_pipeline is None:
1182 # QUESTION: Can the PGID of a single process just be the PID? i.e. avoid
1183 # calling getpgid()?
1184 self.job_control.MaybeGiveTerminal(posix.getpgid(self.pid))
1185 return self.Wait(waiter)
1186
1187
1188class ctx_Pipe(object):
1189
1190 def __init__(self, fd_state, fd, err_out):
1191 # type: (FdState, int, List[error.IOError_OSError]) -> None
1192 fd_state.PushStdinFromPipe(fd)
1193 self.fd_state = fd_state
1194 self.err_out = err_out
1195
1196 def __enter__(self):
1197 # type: () -> None
1198 pass
1199
1200 def __exit__(self, type, value, traceback):
1201 # type: (Any, Any, Any) -> None
1202 self.fd_state.Pop(self.err_out)
1203
1204
1205class Pipeline(Job):
1206 """A pipeline of processes to run.
1207
1208 Cases we handle:
1209
1210 foo | bar
1211 $(foo | bar)
1212 foo | bar | read v
1213 """
1214
1215 def __init__(self, sigpipe_status_ok, job_control, job_list, tracer):
1216 # type: (bool, JobControl, JobList, dev.Tracer) -> None
1217 Job.__init__(self)
1218 self.job_control = job_control
1219 self.job_list = job_list
1220 self.tracer = tracer
1221
1222 self.procs = [] # type: List[Process]
1223 self.pids = [] # type: List[int] # pids in order
1224 self.pipe_status = [] # type: List[int] # status in order
1225 self.status = -1 # for 'wait' jobs
1226
1227 self.pgid = INVALID_PGID
1228
1229 # Optional for foreground
1230 self.last_thunk = None # type: Tuple[CommandEvaluator, command_t]
1231 self.last_pipe = None # type: Tuple[int, int]
1232
1233 self.sigpipe_status_ok = sigpipe_status_ok
1234
1235 def ProcessGroupId(self):
1236 # type: () -> int
1237 """Returns the group ID of this pipeline."""
1238 return self.pgid
1239
1240 def DisplayJob(self, job_id, f, style):
1241 # type: (int, mylib.Writer, int) -> None
1242 if style == STYLE_PID_ONLY:
1243 f.write('%d\n' % self.procs[0].pid)
1244 else:
1245 # Note: this is STYLE_LONG.
1246 for i, proc in enumerate(self.procs):
1247 if i == 0: # show job ID for first element in pipeline
1248 job_id_str = '%%%d' % job_id
1249 else:
1250 job_id_str = ' ' # 2 spaces
1251
1252 f.write('%s %d %7s ' %
1253 (job_id_str, proc.pid, _JobStateStr(proc.state)))
1254 f.write(proc.thunk.UserString())
1255 f.write('\n')
1256
1257 def DebugPrint(self):
1258 # type: () -> None
1259 print('Pipeline in state %s' % _JobStateStr(self.state))
1260 if mylib.PYTHON: # %s for Process not allowed in C++
1261 for proc in self.procs:
1262 print(' proc %s' % proc)
1263 _, last_node = self.last_thunk
1264 print(' last %s' % last_node)
1265 print(' pipe_status %s' % self.pipe_status)
1266
1267 def Add(self, p):
1268 # type: (Process) -> None
1269 """Append a process to the pipeline."""
1270 if len(self.procs) == 0:
1271 self.procs.append(p)
1272 return
1273
1274 r, w = posix.pipe()
1275 #log('pipe for %s: %d %d', p, r, w)
1276 prev = self.procs[-1]
1277
1278 prev.AddStateChange(StdoutToPipe(r, w)) # applied on StartPipeline()
1279 p.AddStateChange(StdinFromPipe(r, w)) # applied on StartPipeline()
1280
1281 p.AddPipeToClose(r, w) # MaybeClosePipe() on StartPipeline()
1282
1283 self.procs.append(p)
1284
1285 def AddLast(self, thunk):
1286 # type: (Tuple[CommandEvaluator, command_t]) -> None
1287 """Append the last noden to the pipeline.
1288
1289 This is run in the CURRENT process. It is OPTIONAL, because
1290 pipelines in the background are run uniformly.
1291 """
1292 self.last_thunk = thunk
1293
1294 assert len(self.procs) != 0
1295
1296 r, w = posix.pipe()
1297 prev = self.procs[-1]
1298 prev.AddStateChange(StdoutToPipe(r, w))
1299
1300 self.last_pipe = (r, w) # So we can connect it to last_thunk
1301
1302 def StartPipeline(self, waiter):
1303 # type: (Waiter) -> None
1304
1305 # If we are creating a pipeline in a subshell or we aren't running with job
1306 # control, our children should remain in our inherited process group.
1307 # the pipelines's group ID.
1308 if self.job_control.Enabled():
1309 self.pgid = OWN_LEADER # first process in pipeline is the leader
1310
1311 for i, proc in enumerate(self.procs):
1312 if self.pgid != INVALID_PGID:
1313 proc.AddStateChange(SetPgid(self.pgid, self.tracer))
1314
1315 # Figure out the pid
1316 pid = proc.StartProcess(trace.PipelinePart)
1317 if i == 0 and self.pgid != INVALID_PGID:
1318 # Mimic bash and use the PID of the FIRST process as the group for the
1319 # whole pipeline.
1320 self.pgid = pid
1321
1322 self.pids.append(pid)
1323 self.pipe_status.append(-1) # uninitialized
1324
1325 # NOTE: This is done in the SHELL PROCESS after every fork() call.
1326 # It can't be done at the end; otherwise processes will have descriptors
1327 # from non-adjacent pipes.
1328 proc.MaybeClosePipe()
1329
1330 if self.last_thunk:
1331 self.pipe_status.append(-1) # for self.last_thunk
1332
1333 def LastPid(self):
1334 # type: () -> int
1335 """For the odd $! variable.
1336
1337 It would be better if job IDs or PGIDs were used consistently.
1338 """
1339 return self.pids[-1]
1340
1341 def Wait(self, waiter):
1342 # type: (Waiter) -> List[int]
1343 """Wait for this pipeline to finish."""
1344
1345 assert self.procs, "no procs for Wait()"
1346 # waitpid(-1) zero or more times
1347 while self.state == job_state_e.Running:
1348 # Keep waiting until there's nothing to wait for.
1349 if waiter.WaitForOne() == W1_ECHILD:
1350 break
1351
1352 return self.pipe_status
1353
1354 def JobWait(self, waiter):
1355 # type: (Waiter) -> wait_status_t
1356 """Called by 'wait' builtin, e.g. 'wait %1'."""
1357 # wait builtin can be interrupted
1358 assert self.procs, "no procs for Wait()"
1359 while self.state == job_state_e.Running:
1360 result = waiter.WaitForOne()
1361
1362 if result >= 0: # signal
1363 return wait_status.Cancelled(result)
1364
1365 if result == W1_ECHILD:
1366 break
1367
1368 return wait_status.Pipeline(self.pipe_status)
1369
1370 def RunLastPart(self, waiter, fd_state):
1371 # type: (Waiter, FdState) -> List[int]
1372 """Run this pipeline synchronously (foreground pipeline).
1373
1374 Returns:
1375 pipe_status (list of integers).
1376 """
1377 assert len(self.pids) == len(self.procs)
1378
1379 # TODO: break circular dep. Bit flags could go in ASDL or headers.
1380 from osh import cmd_eval
1381
1382 # This is tcsetpgrp()
1383 # TODO: fix race condition -- I believe the first process could have
1384 # stopped already, and thus getpgid() will fail
1385 self.job_control.MaybeGiveTerminal(self.pgid)
1386
1387 # Run the last part of the pipeline IN PARALLEL with other processes. It
1388 # may or may not fork:
1389 # echo foo | read line # no fork, the builtin runs in THIS shell process
1390 # ls | wc -l # fork for 'wc'
1391
1392 cmd_ev, last_node = self.last_thunk
1393
1394 assert self.last_pipe is not None
1395 r, w = self.last_pipe # set in AddLast()
1396 posix.close(w) # we will not write here
1397
1398 # Fix lastpipe / job control / DEBUG trap interaction
1399 cmd_flags = cmd_eval.NoDebugTrap if self.job_control.Enabled() else 0
1400
1401 # The ERR trap only runs for the WHOLE pipeline, not the COMPONENTS in
1402 # a pipeline.
1403 cmd_flags |= cmd_eval.NoErrTrap
1404
1405 io_errors = [] # type: List[error.IOError_OSError]
1406 with ctx_Pipe(fd_state, r, io_errors):
1407 cmd_ev.ExecuteAndCatch(last_node, cmd_flags)
1408
1409 if len(io_errors):
1410 e_die('Error setting up last part of pipeline: %s' %
1411 pyutil.strerror(io_errors[0]))
1412
1413 # We won't read anymore. If we don't do this, then 'cat' in 'cat
1414 # /dev/urandom | sleep 1' will never get SIGPIPE.
1415 posix.close(r)
1416
1417 self.pipe_status[-1] = cmd_ev.LastStatus()
1418 if self.AllDone():
1419 self.state = job_state_e.Done
1420
1421 #log('pipestatus before all have finished = %s', self.pipe_status)
1422 return self.Wait(waiter)
1423
1424 def AllDone(self):
1425 # type: () -> bool
1426
1427 # mycpp rewrite: all(status != -1 for status in self.pipe_status)
1428 for status in self.pipe_status:
1429 if status == -1:
1430 return False
1431 return True
1432
1433 def WhenDone(self, pid, status):
1434 # type: (int, int) -> None
1435 """Called by Process.WhenDone."""
1436 #log('Pipeline WhenDone %d %d', pid, status)
1437 i = self.pids.index(pid)
1438 assert i != -1, 'Unexpected PID %d' % pid
1439
1440 if status == 141 and self.sigpipe_status_ok:
1441 status = 0
1442
1443 self.job_list.RemoveChildProcess(pid)
1444 self.pipe_status[i] = status
1445 if self.AllDone():
1446 if self.job_id != -1:
1447 # Job might have been brought to the foreground after being
1448 # assigned a job ID.
1449 if self.in_background:
1450 print_stderr('[%%%d] PGID %d Done' %
1451 (self.job_id, self.pids[0]))
1452
1453 self.job_list.RemoveJob(self.job_id)
1454
1455 # status of pipeline is status of last process
1456 self.status = self.pipe_status[-1]
1457 self.state = job_state_e.Done
1458 if not self.in_background:
1459 self.job_control.MaybeTakeTerminal()
1460
1461
1462def _JobStateStr(i):
1463 # type: (job_state_t) -> str
1464 return job_state_str(i)[10:] # remove 'job_state.'
1465
1466
1467def _GetTtyFd():
1468 # type: () -> int
1469 """Returns -1 if stdio is not a TTY."""
1470 try:
1471 return posix.open("/dev/tty", O_NONBLOCK | O_NOCTTY | O_RDWR, 0o666)
1472 except (IOError, OSError) as e:
1473 return -1
1474
1475
1476class ctx_TerminalControl(object):
1477
1478 def __init__(self, job_control, errfmt):
1479 # type: (JobControl, ui.ErrorFormatter) -> None
1480 job_control.InitJobControl()
1481 self.job_control = job_control
1482 self.errfmt = errfmt
1483
1484 def __enter__(self):
1485 # type: () -> None
1486 pass
1487
1488 def __exit__(self, type, value, traceback):
1489 # type: (Any, Any, Any) -> None
1490
1491 # Return the TTY to the original owner before exiting.
1492 try:
1493 self.job_control.MaybeReturnTerminal()
1494 except error.FatalRuntime as e:
1495 # Don't abort the shell on error, just print a message.
1496 self.errfmt.PrettyPrintError(e)
1497
1498
1499class JobControl(object):
1500 """Interface to setpgid(), tcsetpgrp(), etc."""
1501
1502 def __init__(self):
1503 # type: () -> None
1504
1505 # The main shell's PID and group ID.
1506 self.shell_pid = -1
1507 self.shell_pgid = -1
1508
1509 # The fd of the controlling tty. Set to -1 when job control is disabled.
1510 self.shell_tty_fd = -1
1511
1512 # For giving the terminal back to our parent before exiting (if not a login
1513 # shell).
1514 self.original_tty_pgid = -1
1515
1516 def InitJobControl(self):
1517 # type: () -> None
1518 self.shell_pid = posix.getpid()
1519 orig_shell_pgid = posix.getpgid(0)
1520 self.shell_pgid = orig_shell_pgid
1521 self.shell_tty_fd = _GetTtyFd()
1522
1523 # If we aren't the leader of our process group, create a group and mark
1524 # ourselves as the leader.
1525 if self.shell_pgid != self.shell_pid:
1526 try:
1527 posix.setpgid(self.shell_pid, self.shell_pid)
1528 self.shell_pgid = self.shell_pid
1529 except (IOError, OSError) as e:
1530 self.shell_tty_fd = -1
1531
1532 if self.shell_tty_fd != -1:
1533 self.original_tty_pgid = posix.tcgetpgrp(self.shell_tty_fd)
1534
1535 # If stdio is a TTY, put the shell's process group in the foreground.
1536 try:
1537 posix.tcsetpgrp(self.shell_tty_fd, self.shell_pgid)
1538 except (IOError, OSError) as e:
1539 # We probably aren't in the session leader's process group. Disable job
1540 # control.
1541 self.shell_tty_fd = -1
1542 self.shell_pgid = orig_shell_pgid
1543 posix.setpgid(self.shell_pid, self.shell_pgid)
1544
1545 def Enabled(self):
1546 # type: () -> bool
1547 """
1548 Only the main shell process should bother with job control functions.
1549 """
1550 #log('ENABLED? %d', self.shell_tty_fd)
1551
1552 # TODO: get rid of getpid()? I think SubProgramThunk should set a
1553 # flag.
1554 return self.shell_tty_fd != -1 and posix.getpid() == self.shell_pid
1555
1556 # TODO: This isn't a PID. This is a process group ID?
1557 #
1558 # What should the table look like?
1559 #
1560 # Do we need the last PID? I don't know why bash prints that. Probably so
1561 # you can do wait $!
1562 # wait -n waits for any node to go from job_state_e.Running to job_state_e.Done?
1563 #
1564 # And it needs a flag for CURRENT, for the implicit arg to 'fg'.
1565 # job_id is just an integer. This is sort of lame.
1566 #
1567 # [job_id, flag, pgid, job_state, node]
1568
1569 def MaybeGiveTerminal(self, pgid):
1570 # type: (int) -> None
1571 """If stdio is a TTY, move the given process group to the
1572 foreground."""
1573 if not self.Enabled():
1574 # Only call tcsetpgrp when job control is enabled.
1575 return
1576
1577 try:
1578 posix.tcsetpgrp(self.shell_tty_fd, pgid)
1579 except (IOError, OSError) as e:
1580 e_die('osh: Failed to move process group %d to foreground: %s' %
1581 (pgid, pyutil.strerror(e)))
1582
1583 def MaybeTakeTerminal(self):
1584 # type: () -> None
1585 """If stdio is a TTY, return the main shell's process group to the
1586 foreground."""
1587 self.MaybeGiveTerminal(self.shell_pgid)
1588
1589 def MaybeReturnTerminal(self):
1590 # type: () -> None
1591 """Called before the shell exits."""
1592 self.MaybeGiveTerminal(self.original_tty_pgid)
1593
1594
1595class JobList(object):
1596 """Global list of jobs, used by a few builtins."""
1597
1598 def __init__(self):
1599 # type: () -> None
1600
1601 # job_id -> Job instance
1602 self.jobs = {} # type: Dict[int, Job]
1603
1604 # pid -> Process. This is for STOP notification.
1605 self.child_procs = {} # type: Dict[int, Process]
1606 self.debug_pipelines = [] # type: List[Pipeline]
1607
1608 # Counter used to assign IDs to jobs. It is incremented every time a job
1609 # is created. Once all active jobs are done it is reset to 1. I'm not
1610 # sure if this reset behavior is mandated by POSIX, but other shells do
1611 # it, so we mimic for the sake of compatibility.
1612 self.job_id = 1
1613
1614 def AddJob(self, job):
1615 # type: (Job) -> int
1616 """Add a background job to the list.
1617
1618 A job is either a Process or Pipeline. You can resume a job with 'fg',
1619 kill it with 'kill', etc.
1620
1621 Two cases:
1622
1623 1. async jobs: sleep 5 | sleep 4 &
1624 2. stopped jobs: sleep 5; then Ctrl-Z
1625 """
1626 job_id = self.job_id
1627 self.jobs[job_id] = job
1628 job.job_id = job_id
1629 self.job_id += 1
1630 return job_id
1631
1632 def RemoveJob(self, job_id):
1633 # type: (int) -> None
1634 """Process and Pipeline can call this."""
1635 mylib.dict_erase(self.jobs, job_id)
1636
1637 if len(self.jobs) == 0:
1638 self.job_id = 1
1639
1640 def AddChildProcess(self, pid, proc):
1641 # type: (int, Process) -> None
1642 """Every child process should be added here as soon as we know its PID.
1643
1644 When the Waiter gets an EXITED or STOPPED notification, we need
1645 to know about it so 'jobs' can work.
1646 """
1647 self.child_procs[pid] = proc
1648
1649 def RemoveChildProcess(self, pid):
1650 # type: (int) -> None
1651 """Remove the child process with the given PID."""
1652 mylib.dict_erase(self.child_procs, pid)
1653
1654 if mylib.PYTHON:
1655
1656 def AddPipeline(self, pi):
1657 # type: (Pipeline) -> None
1658 """For debugging only."""
1659 self.debug_pipelines.append(pi)
1660
1661 def ProcessFromPid(self, pid):
1662 # type: (int) -> Process
1663 """For wait $PID.
1664
1665 There's no way to wait for a pipeline with a PID. That uses job
1666 syntax, e.g. %1. Not a great interface.
1667 """
1668 return self.child_procs.get(pid)
1669
1670 def GetCurrentAndPreviousJobs(self):
1671 # type: () -> Tuple[Optional[Job], Optional[Job]]
1672 """Return the "current" and "previous" jobs (AKA `%+` and `%-`).
1673
1674 See the POSIX specification for the `jobs` builtin for details:
1675 https://pubs.opengroup.org/onlinepubs/007904875/utilities/jobs.html
1676
1677 IMPORTANT NOTE: This method assumes that the jobs list will not change
1678 during its execution! This assumption holds for now because we only ever
1679 update the jobs list from the main loop after WaitPid() informs us of a
1680 change. If we implement `set -b` and install a signal handler for
1681 SIGCHLD we should be careful to synchronize it with this function. The
1682 unsafety of mutating GC data structures from a signal handler should
1683 make this a non-issue, but if bugs related to this appear this note may
1684 be helpful...
1685 """
1686 # Split all active jobs by state and sort each group by decreasing job
1687 # ID to approximate newness.
1688 stopped_jobs = [] # type: List[Job]
1689 running_jobs = [] # type: List[Job]
1690 for i in xrange(0, self.job_id):
1691 job = self.jobs.get(i, None)
1692 if not job:
1693 continue
1694
1695 if job.state == job_state_e.Stopped:
1696 stopped_jobs.append(job)
1697
1698 elif job.state == job_state_e.Running:
1699 running_jobs.append(job)
1700
1701 current = None # type: Optional[Job]
1702 previous = None # type: Optional[Job]
1703 # POSIX says: If there is any suspended job, then the current job shall
1704 # be a suspended job. If there are at least two suspended jobs, then the
1705 # previous job also shall be a suspended job.
1706 #
1707 # So, we will only return running jobs from here if there are no recent
1708 # stopped jobs.
1709 if len(stopped_jobs) > 0:
1710 current = stopped_jobs.pop()
1711
1712 if len(stopped_jobs) > 0:
1713 previous = stopped_jobs.pop()
1714
1715 if len(running_jobs) > 0 and not current:
1716 current = running_jobs.pop()
1717
1718 if len(running_jobs) > 0 and not previous:
1719 previous = running_jobs.pop()
1720
1721 if not previous:
1722 previous = current
1723
1724 return current, previous
1725
1726 def GetJobWithSpec(self, job_spec):
1727 # type: (str) -> Optional[Job]
1728 """Parse the given job spec and return the matching job. If there is no
1729 matching job, this function returns None.
1730
1731 See the POSIX spec for the `jobs` builtin for details about job specs:
1732 https://pubs.opengroup.org/onlinepubs/007904875/utilities/jobs.html
1733 """
1734 if job_spec in CURRENT_JOB_SPECS:
1735 current, _ = self.GetCurrentAndPreviousJobs()
1736 return current
1737
1738 if job_spec == '%-':
1739 _, previous = self.GetCurrentAndPreviousJobs()
1740 return previous
1741
1742 # TODO: Add support for job specs based on prefixes of process argv.
1743 m = util.RegexSearch(r'^%([0-9]+)$', job_spec)
1744 if m is not None:
1745 assert len(m) == 2
1746 job_id = int(m[1])
1747 if job_id in self.jobs:
1748 return self.jobs[job_id]
1749
1750 return None
1751
1752 def DisplayJobs(self, style):
1753 # type: (int) -> None
1754 """Used by the 'jobs' builtin.
1755
1756 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/jobs.html
1757
1758 "By default, the jobs utility shall display the status of all stopped jobs,
1759 running background jobs and all jobs whose status has changed and have not
1760 been reported by the shell."
1761 """
1762 # NOTE: A job is a background process or pipeline.
1763 #
1764 # echo hi | wc -l -- this starts two processes. Wait for TWO
1765 # echo hi | wc -l & -- this starts a process which starts two processes
1766 # Wait for ONE.
1767 #
1768 # 'jobs -l' GROUPS the PIDs by job. It has the job number, + - indicators
1769 # for %% and %-, PID, status, and "command".
1770 #
1771 # Every component of a pipeline is on the same line with 'jobs', but
1772 # they're separated into different lines with 'jobs -l'.
1773 #
1774 # See demo/jobs-builtin.sh
1775
1776 # $ jobs -l
1777 # [1]+ 24414 Stopped sleep 5
1778 # 24415 | sleep 5
1779 # [2] 24502 Running sleep 6
1780 # 24503 | sleep 6
1781 # 24504 | sleep 5 &
1782 # [3]- 24508 Running sleep 6
1783 # 24509 | sleep 6
1784 # 24510 | sleep 5 &
1785
1786 f = mylib.Stdout()
1787 for job_id, job in iteritems(self.jobs):
1788 # Use the %1 syntax
1789 job.DisplayJob(job_id, f, style)
1790
1791 def DebugPrint(self):
1792 # type: () -> None
1793
1794 f = mylib.Stdout()
1795 f.write('\n')
1796 f.write('[process debug info]\n')
1797
1798 for pid, proc in iteritems(self.child_procs):
1799 proc.DisplayJob(-1, f, STYLE_DEFAULT)
1800 #p = ' |' if proc.parent_pipeline else ''
1801 #print('%d %7s %s%s' % (pid, _JobStateStr(proc.state), proc.thunk.UserString(), p))
1802
1803 if len(self.debug_pipelines):
1804 f.write('\n')
1805 f.write('[pipeline debug info]\n')
1806 for pi in self.debug_pipelines:
1807 pi.DebugPrint()
1808
1809 def ListRecent(self):
1810 # type: () -> None
1811 """For jobs -n, which I think is also used in the interactive
1812 prompt."""
1813 pass
1814
1815 def NumRunning(self):
1816 # type: () -> int
1817 """Return the number of running jobs.
1818
1819 Used by 'wait' and 'wait -n'.
1820 """
1821 count = 0
1822 for _, job in iteritems(self.jobs): # mycpp rewrite: from itervalues()
1823 if job.State() == job_state_e.Running:
1824 count += 1
1825 return count
1826
1827
1828# Some WaitForOne() return values
1829W1_OK = -2 # waitpid(-1) returned
1830W1_ECHILD = -3 # no processes to wait for
1831W1_AGAIN = -4 # WNOHANG was passed and there were no state changes
1832
1833
1834class Waiter(object):
1835 """A capability to wait for processes.
1836
1837 This must be a singleton (and is because CommandEvaluator is a singleton).
1838
1839 Invariants:
1840 - Every child process is registered once
1841 - Every child process is waited for
1842
1843 Canonical example of why we need a GLOBAL waiter:
1844
1845 { sleep 3; echo 'done 3'; } &
1846 { sleep 4; echo 'done 4'; } &
1847
1848 # ... do arbitrary stuff ...
1849
1850 { sleep 1; exit 1; } | { sleep 2; exit 2; }
1851
1852 Now when you do wait() after starting the pipeline, you might get a pipeline
1853 process OR a background process! So you have to distinguish between them.
1854 """
1855
1856 def __init__(self, job_list, exec_opts, signal_safe, tracer):
1857 # type: (JobList, optview.Exec, pyos.SignalSafe, dev.Tracer) -> None
1858 self.job_list = job_list
1859 self.exec_opts = exec_opts
1860 self.signal_safe = signal_safe
1861 self.tracer = tracer
1862 self.last_status = 127 # wait -n error code
1863
1864 def WaitForOne(self, waitpid_options=0):
1865 # type: (int) -> int
1866 """Wait until the next process returns (or maybe Ctrl-C).
1867
1868 Returns:
1869 One of these negative numbers:
1870 W1_ECHILD Nothing to wait for
1871 W1_OK Caller should keep waiting
1872 UNTRAPPED_SIGWINCH
1873 Or
1874 result > 0 Signal that waitpid() was interrupted with
1875
1876 In the interactive shell, we return 0 if we get a Ctrl-C, so the caller
1877 will try again.
1878
1879 Callers:
1880 wait -n -- loop until there is one fewer process (TODO)
1881 wait -- loop until there are no processes
1882 wait $! -- loop until job state is Done (process or pipeline)
1883 Process::Wait() -- loop until Process state is done
1884 Pipeline::Wait() -- loop until Pipeline state is done
1885
1886 Comparisons:
1887 bash: jobs.c waitchld() Has a special case macro(!) CHECK_WAIT_INTR for
1888 the wait builtin
1889
1890 dash: jobs.c waitproc() uses sigfillset(), sigprocmask(), etc. Runs in a
1891 loop while (gotsigchld), but that might be a hack for System V!
1892
1893 Should we have a cleaner API like named posix::wait_for_one() ?
1894
1895 wait_result =
1896 ECHILD -- nothing to wait for
1897 | Done(int pid, int status) -- process done
1898 | EINTR(bool sigint) -- may or may not retry
1899 """
1900 pid, status = pyos.WaitPid(waitpid_options)
1901 if pid == 0: # WNOHANG passed, and no state changes
1902 return W1_AGAIN
1903 elif pid < 0: # error case
1904 err_num = status
1905 #log('waitpid() error => %d %s', e.errno, pyutil.strerror(e))
1906 if err_num == ECHILD:
1907 return W1_ECHILD # nothing to wait for caller should stop
1908 elif err_num == EINTR: # Bug #858 fix
1909 #log('WaitForOne() => %d', self.trap_state.GetLastSignal())
1910 return self.signal_safe.LastSignal() # e.g. 1 for SIGHUP
1911 else:
1912 # The signature of waitpid() means this shouldn't happen
1913 raise AssertionError()
1914
1915 # All child processes are supposed to be in this dict. But this may
1916 # legitimately happen if a grandchild outlives the child (its parent).
1917 # Then it is reparented under this process, so we might receive
1918 # notification of its exit, even though we didn't start it. We can't have
1919 # any knowledge of such processes, so print a warning.
1920 if pid not in self.job_list.child_procs:
1921 print_stderr("oils: PID %d Stopped, but osh didn't start it" % pid)
1922 return W1_OK
1923
1924 proc = self.job_list.child_procs[pid]
1925 if 0:
1926 self.job_list.DebugPrint()
1927
1928 if WIFSIGNALED(status):
1929 term_sig = WTERMSIG(status)
1930 status = 128 + term_sig
1931
1932 # Print newline after Ctrl-C.
1933 if term_sig == SIGINT:
1934 print('')
1935
1936 proc.WhenDone(pid, status)
1937
1938 elif WIFEXITED(status):
1939 status = WEXITSTATUS(status)
1940 #log('exit status: %s', status)
1941 proc.WhenDone(pid, status)
1942
1943 elif WIFSTOPPED(status):
1944 #status = WEXITSTATUS(status)
1945 stop_sig = WSTOPSIG(status)
1946
1947 print_stderr('')
1948 print_stderr('oils: PID %d Stopped with signal %d' %
1949 (pid, stop_sig))
1950 proc.WhenStopped(stop_sig)
1951
1952 else:
1953 raise AssertionError(status)
1954
1955 self.last_status = status # for wait -n
1956 self.tracer.OnProcessEnd(pid, status)
1957 return W1_OK
1958
1959 def PollNotifications(self):
1960 # type: () -> None
1961 """
1962 Process all pending state changes.
1963 """
1964 while self.WaitForOne(waitpid_options=WNOHANG) == W1_OK:
1965 continue