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

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