builtin_fn_cmd_unix.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //go:build !windows && !plan9
  2. package eval
  3. import (
  4. "errors"
  5. "os"
  6. "os/exec"
  7. "strconv"
  8. "syscall"
  9. "src.elv.sh/pkg/env"
  10. "src.elv.sh/pkg/eval/errs"
  11. "src.elv.sh/pkg/eval/vals"
  12. "src.elv.sh/pkg/sys/eunix"
  13. )
  14. // ErrNotInSameProcessGroup is thrown when the process IDs passed to fg are not
  15. // in the same process group.
  16. var ErrNotInSameProcessGroup = errors.New("not in the same process group")
  17. //elvdoc:fn exec
  18. //
  19. // ```elvish
  20. // exec $command? $args...
  21. // ```
  22. //
  23. // Replace the Elvish process with an external `$command`, defaulting to
  24. // `elvish`, passing the given arguments. This decrements `$E:SHLVL` before
  25. // starting the new process.
  26. //
  27. // This command always raises an exception on Windows with the message "not
  28. // supported on Windows".
  29. // Reference to syscall.Exec. Can be overridden in tests.
  30. var syscallExec = syscall.Exec
  31. func execFn(fm *Frame, args ...interface{}) error {
  32. var argstrings []string
  33. if len(args) == 0 {
  34. argstrings = []string{"elvish"}
  35. } else {
  36. argstrings = make([]string, len(args))
  37. for i, a := range args {
  38. argstrings[i] = vals.ToString(a)
  39. }
  40. }
  41. var err error
  42. argstrings[0], err = exec.LookPath(argstrings[0])
  43. if err != nil {
  44. return err
  45. }
  46. preExit(fm)
  47. decSHLVL()
  48. return syscallExec(argstrings[0], argstrings, os.Environ())
  49. }
  50. // Decrements $E:SHLVL. Called from execFn to ensure that $E:SHLVL remains the
  51. // same in the new command.
  52. func decSHLVL() {
  53. i, err := strconv.Atoi(os.Getenv(env.SHLVL))
  54. if err != nil {
  55. return
  56. }
  57. os.Setenv(env.SHLVL, strconv.Itoa(i-1))
  58. }
  59. func fg(pids ...int) error {
  60. if len(pids) == 0 {
  61. return errs.ArityMismatch{What: "arguments", ValidLow: 1, ValidHigh: -1, Actual: len(pids)}
  62. }
  63. var thepgid int
  64. for i, pid := range pids {
  65. pgid, err := syscall.Getpgid(pid)
  66. if err != nil {
  67. return err
  68. }
  69. if i == 0 {
  70. thepgid = pgid
  71. } else if pgid != thepgid {
  72. return ErrNotInSameProcessGroup
  73. }
  74. }
  75. err := eunix.Tcsetpgrp(0, thepgid)
  76. if err != nil {
  77. return err
  78. }
  79. errors := make([]Exception, len(pids))
  80. for i, pid := range pids {
  81. err := syscall.Kill(pid, syscall.SIGCONT)
  82. if err != nil {
  83. errors[i] = &exception{err, nil}
  84. }
  85. }
  86. for i, pid := range pids {
  87. if errors[i] != nil {
  88. continue
  89. }
  90. var ws syscall.WaitStatus
  91. _, err = syscall.Wait4(pid, &ws, syscall.WUNTRACED, nil)
  92. if err != nil {
  93. errors[i] = &exception{err, nil}
  94. } else {
  95. // TODO find command name
  96. errors[i] = &exception{NewExternalCmdExit(
  97. "[pid "+strconv.Itoa(pid)+"]", ws, pid), nil}
  98. }
  99. }
  100. return MakePipelineError(errors)
  101. }