2017-12-08 08:45:10 +08:00
|
|
|
// +build !windows,!plan9
|
2017-12-03 02:44:12 +08:00
|
|
|
|
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2019-04-19 05:15:34 +08:00
|
|
|
"errors"
|
2017-12-03 02:44:12 +08:00
|
|
|
"os"
|
2017-12-05 07:48:38 +08:00
|
|
|
"os/exec"
|
2017-12-03 02:44:12 +08:00
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
|
|
|
"github.com/elves/elvish/pkg/sys"
|
2017-12-03 02:44:12 +08:00
|
|
|
)
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// ErrNotInSameProcessGroup is thrown when the process IDs passed to fg are not
|
|
|
|
// in the same process group.
|
|
|
|
var ErrNotInSameProcessGroup = errors.New("not in the same process group")
|
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
func execFn(fm *Frame, args ...interface{}) error {
|
2017-12-03 02:44:12 +08:00
|
|
|
var argstrings []string
|
|
|
|
if len(args) == 0 {
|
|
|
|
argstrings = []string{"elvish"}
|
|
|
|
} else {
|
|
|
|
argstrings = make([]string, len(args))
|
|
|
|
for i, a := range args {
|
2018-02-15 17:14:05 +08:00
|
|
|
argstrings[i] = vals.ToString(a)
|
2017-12-03 02:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2017-12-05 07:48:38 +08:00
|
|
|
argstrings[0], err = exec.LookPath(argstrings[0])
|
2018-02-04 12:52:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-03 02:44:12 +08:00
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
preExit(fm)
|
2017-12-03 02:44:12 +08:00
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
return syscall.Exec(argstrings[0], argstrings, os.Environ())
|
2017-12-03 02:44:12 +08:00
|
|
|
}
|
|
|
|
|
2018-02-04 12:52:54 +08:00
|
|
|
func fg(pids ...int) error {
|
2017-12-03 02:44:12 +08:00
|
|
|
if len(pids) == 0 {
|
2018-02-04 12:52:54 +08:00
|
|
|
return ErrArgs
|
2017-12-03 02:44:12 +08:00
|
|
|
}
|
|
|
|
var thepgid int
|
|
|
|
for i, pid := range pids {
|
|
|
|
pgid, err := syscall.Getpgid(pid)
|
2018-02-04 12:52:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-03 02:44:12 +08:00
|
|
|
if i == 0 {
|
|
|
|
thepgid = pgid
|
|
|
|
} else if pgid != thepgid {
|
2019-04-19 05:15:34 +08:00
|
|
|
return ErrNotInSameProcessGroup
|
2017-12-03 02:44:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sys.Tcsetpgrp(0, thepgid)
|
2018-02-04 12:52:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-03 02:44:12 +08:00
|
|
|
|
|
|
|
errors := make([]*Exception, len(pids))
|
|
|
|
|
|
|
|
for i, pid := range pids {
|
|
|
|
err := syscall.Kill(pid, syscall.SIGCONT)
|
|
|
|
if err != nil {
|
|
|
|
errors[i] = &Exception{err, nil}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, pid := range pids {
|
|
|
|
if errors[i] != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var ws syscall.WaitStatus
|
|
|
|
_, err = syscall.Wait4(pid, &ws, syscall.WUNTRACED, nil)
|
|
|
|
if err != nil {
|
|
|
|
errors[i] = &Exception{err, nil}
|
|
|
|
} else {
|
|
|
|
// TODO find command name
|
|
|
|
errors[i] = &Exception{NewExternalCmdExit(
|
|
|
|
"[pid "+strconv.Itoa(pid)+"]", ws, pid), nil}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:25:55 +08:00
|
|
|
return makePipelineError(errors)
|
2017-12-03 02:44:12 +08:00
|
|
|
}
|