2014-06-19 13:08:03 +08:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-07-07 05:32:36 +08:00
|
|
|
"errors"
|
2014-06-19 13:08:03 +08:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2014-06-19 13:08:03 +08:00
|
|
|
)
|
|
|
|
|
2014-07-07 05:32:36 +08:00
|
|
|
var (
|
2016-11-15 15:06:31 +08:00
|
|
|
// ErrExecTimeout represent a timeout error
|
2014-07-07 05:32:36 +08:00
|
|
|
ErrExecTimeout = errors.New("Process execution timeout")
|
|
|
|
|
2016-11-15 15:06:31 +08:00
|
|
|
// DefaultTimeout is the timeout used by Exec* family
|
|
|
|
// of function when timeout parameter is omitted or
|
|
|
|
// passed as -1
|
2014-07-07 05:32:36 +08:00
|
|
|
// NOTE: could be custom in config file for default.
|
2016-11-15 15:06:31 +08:00
|
|
|
DefaultTimeout = 60 * time.Second
|
2014-07-07 05:32:36 +08:00
|
|
|
)
|
|
|
|
|
2014-06-19 13:08:03 +08:00
|
|
|
// Process represents a working process inherit from Gogs.
|
|
|
|
type Process struct {
|
|
|
|
Pid int64 // Process ID, not system one.
|
|
|
|
Description string
|
|
|
|
Start time.Time
|
|
|
|
Cmd *exec.Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// List of existing processes.
|
|
|
|
var (
|
|
|
|
curPid int64 = 1
|
|
|
|
Processes []*Process
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add adds a existing process and returns its PID.
|
|
|
|
func Add(desc string, cmd *exec.Cmd) int64 {
|
|
|
|
pid := curPid
|
|
|
|
Processes = append(Processes, &Process{
|
|
|
|
Pid: pid,
|
|
|
|
Description: desc,
|
|
|
|
Start: time.Now(),
|
|
|
|
Cmd: cmd,
|
|
|
|
})
|
|
|
|
curPid++
|
|
|
|
return pid
|
|
|
|
}
|
|
|
|
|
2016-11-27 10:10:08 +08:00
|
|
|
// ExecDirEnv runs a command in given path and environment variables, and waits for its completion
|
2016-11-15 15:06:31 +08:00
|
|
|
// up to the given timeout (or DefaultTimeout if -1 is given).
|
|
|
|
// Returns its complete stdout and stderr
|
|
|
|
// outputs and an error, if any (including timeout)
|
2016-11-27 10:10:08 +08:00
|
|
|
func ExecDirEnv(timeout time.Duration, dir, desc string, env []string, cmdName string, args ...string) (string, string, error) {
|
2014-07-07 05:32:36 +08:00
|
|
|
if timeout == -1 {
|
2016-11-15 15:06:31 +08:00
|
|
|
timeout = DefaultTimeout
|
2014-07-07 05:32:36 +08:00
|
|
|
}
|
|
|
|
|
2014-06-19 13:08:03 +08:00
|
|
|
bufOut := new(bytes.Buffer)
|
|
|
|
bufErr := new(bytes.Buffer)
|
|
|
|
|
|
|
|
cmd := exec.Command(cmdName, args...)
|
|
|
|
cmd.Dir = dir
|
2016-11-27 10:10:08 +08:00
|
|
|
cmd.Env = env
|
2014-06-19 13:08:03 +08:00
|
|
|
cmd.Stdout = bufOut
|
|
|
|
cmd.Stderr = bufErr
|
2014-07-07 05:32:36 +08:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return "", err.Error(), err
|
|
|
|
}
|
2014-06-19 13:08:03 +08:00
|
|
|
|
|
|
|
pid := Add(desc, cmd)
|
2014-07-07 05:32:36 +08:00
|
|
|
done := make(chan error)
|
|
|
|
go func() {
|
|
|
|
done <- cmd.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
if errKill := Kill(pid); errKill != nil {
|
2014-07-26 12:24:27 +08:00
|
|
|
log.Error(4, "Exec(%d:%s): %v", pid, desc, errKill)
|
2014-07-07 05:32:36 +08:00
|
|
|
}
|
|
|
|
<-done
|
|
|
|
return "", ErrExecTimeout.Error(), ErrExecTimeout
|
|
|
|
case err = <-done:
|
2014-06-19 13:08:03 +08:00
|
|
|
}
|
2014-07-07 05:32:36 +08:00
|
|
|
|
|
|
|
Remove(pid)
|
2014-06-19 13:08:03 +08:00
|
|
|
return bufOut.String(), bufErr.String(), err
|
|
|
|
}
|
|
|
|
|
2016-11-27 10:10:08 +08:00
|
|
|
// ExecDir works exactly like ExecDirEnv except no environment variable is provided.
|
|
|
|
func ExecDir(timeout time.Duration, dir, desc, cmdName string, args ...string) (string, string, error) {
|
|
|
|
return ExecDirEnv(timeout, dir, desc, nil, cmdName, args...)
|
|
|
|
}
|
|
|
|
|
2016-11-15 15:06:31 +08:00
|
|
|
// ExecTimeout runs a command and waits for its completion
|
|
|
|
// up to the given timeout (or DefaultTimeout if -1 is given).
|
|
|
|
// Returns its complete stdout and stderr
|
|
|
|
// outputs and an error, if any (including timeout)
|
2014-07-07 05:32:36 +08:00
|
|
|
func ExecTimeout(timeout time.Duration, desc, cmdName string, args ...string) (string, string, error) {
|
|
|
|
return ExecDir(timeout, "", desc, cmdName, args...)
|
|
|
|
}
|
|
|
|
|
2016-11-15 15:06:31 +08:00
|
|
|
// Exec runs a command and waits for its completion
|
|
|
|
// up to DefaultTimeout. Returns its complete stdout and stderr
|
|
|
|
// outputs and an error, if any (including timeout)
|
2014-06-19 13:08:03 +08:00
|
|
|
func Exec(desc, cmdName string, args ...string) (string, string, error) {
|
2014-07-07 05:32:36 +08:00
|
|
|
return ExecDir(-1, "", desc, cmdName, args...)
|
2014-06-19 13:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes a process from list.
|
|
|
|
func Remove(pid int64) {
|
|
|
|
for i, proc := range Processes {
|
|
|
|
if proc.Pid == pid {
|
|
|
|
Processes = append(Processes[:i], Processes[i+1:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill kills and removes a process from list.
|
|
|
|
func Kill(pid int64) error {
|
|
|
|
for i, proc := range Processes {
|
|
|
|
if proc.Pid == pid {
|
2015-11-27 06:33:45 +08:00
|
|
|
if proc.Cmd != nil && proc.Cmd.Process != nil &&
|
|
|
|
proc.Cmd.ProcessState != nil && !proc.Cmd.ProcessState.Exited() {
|
2014-06-19 13:08:03 +08:00
|
|
|
if err := proc.Cmd.Process.Kill(); err != nil {
|
|
|
|
return fmt.Errorf("fail to kill process(%d/%s): %v", proc.Pid, proc.Description, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Processes = append(Processes[:i], Processes[i+1:]...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|