2014-06-18 23:08:03 -06:00
// Copyright 2014 The Gogs Authors. All rights reserved.
2019-10-11 18:13:27 -06:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2014-06-18 23:08:03 -06:00
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package process
import (
"bytes"
2017-11-13 07:51:45 -07:00
"context"
2014-06-18 23:08:03 -06:00
"fmt"
2019-10-11 18:13:27 -06:00
"io"
2014-06-18 23:08:03 -06:00
"os/exec"
2019-11-30 07:40:22 -07:00
"sort"
2021-11-30 13:06:32 -07:00
"strconv"
2017-01-16 22:58:58 -07:00
"sync"
2014-06-18 23:08:03 -06:00
"time"
)
2017-01-16 22:58:58 -07:00
// TODO: This packages still uses a singleton for the Manager.
// Once there's a decent web framework and dependencies are passed around like they should,
// then we delete the singleton.
2014-07-06 15:32:36 -06:00
var (
2021-04-09 01:40:34 -06:00
manager * Manager
managerInit sync . Once
2019-11-30 07:40:22 -07:00
// DefaultContext is the default context to run processing commands in
DefaultContext = context . Background ( )
2014-07-06 15:32:36 -06:00
)
2021-11-30 13:06:32 -07:00
// IDType is a pid type
type IDType string
// FinishedFunc is a function that marks that the process is finished and can be removed from the process table
// - it is simply an alias for context.CancelFunc and is only for documentary purposes
type FinishedFunc = context . CancelFunc
2014-06-18 23:08:03 -06:00
2021-11-30 13:06:32 -07:00
// Manager manages all processes and counts PIDs.
2017-01-16 22:58:58 -07:00
type Manager struct {
mutex sync . Mutex
2014-06-18 23:08:03 -06:00
2021-11-30 13:06:32 -07:00
next int64
lastTime int64
processes map [ IDType ] * Process
2017-01-16 22:58:58 -07:00
}
// GetManager returns a Manager and initializes one as singleton if there's none yet
func GetManager ( ) * Manager {
2021-02-03 14:36:38 -07:00
managerInit . Do ( func ( ) {
2017-01-16 22:58:58 -07:00
manager = & Manager {
2021-11-30 13:06:32 -07:00
processes : make ( map [ IDType ] * Process ) ,
next : 1 ,
2017-01-16 22:58:58 -07:00
}
2021-02-03 14:36:38 -07:00
} )
2017-01-16 22:58:58 -07:00
return manager
}
2021-11-30 13:06:32 -07:00
// AddContext creates a new context and adds it as a process. Once the process is finished, finished must be called
// to remove the process from the process table. It should not be called until the process is finished but must always be called.
//
// cancel should be used to cancel the returned context, however it will not remove the process from the process table.
// finished will cancel the returned context and remove it from the process table.
//
// Most processes will not need to use the cancel function but there will be cases whereby you want to cancel the process but not immediately remove it from the
// process table.
func ( pm * Manager ) AddContext ( parent context . Context , description string ) ( ctx context . Context , cancel context . CancelFunc , finished FinishedFunc ) {
parentPID := GetParentPID ( parent )
ctx , cancel = context . WithCancel ( parent )
pid , finished := pm . Add ( parentPID , description , cancel )
return & Context {
Context : ctx ,
pid : pid ,
} , cancel , finished
}
// AddContextTimeout creates a new context and add it as a process. Once the process is finished, finished must be called
// to remove the process from the process table. It should not be called until the process is finsihed but must always be called.
//
// cancel should be used to cancel the returned context, however it will not remove the process from the process table.
// finished will cancel the returned context and remove it from the process table.
//
// Most processes will not need to use the cancel function but there will be cases whereby you want to cancel the process but not immediately remove it from the
// process table.
func ( pm * Manager ) AddContextTimeout ( parent context . Context , timeout time . Duration , description string ) ( ctx context . Context , cancel context . CancelFunc , finshed FinishedFunc ) {
parentPID := GetParentPID ( parent )
ctx , cancel = context . WithTimeout ( parent , timeout )
pid , finshed := pm . Add ( parentPID , description , cancel )
return & Context {
Context : ctx ,
pid : pid ,
} , cancel , finshed
}
// Add create a new process
func ( pm * Manager ) Add ( parentPID IDType , description string , cancel context . CancelFunc ) ( IDType , FinishedFunc ) {
2017-01-16 22:58:58 -07:00
pm . mutex . Lock ( )
2021-11-30 13:06:32 -07:00
start , pid := pm . nextPID ( )
parent := pm . processes [ parentPID ]
if parent == nil {
parentPID = ""
}
process := & Process {
2017-01-16 22:58:58 -07:00
PID : pid ,
2021-11-30 13:06:32 -07:00
ParentPID : parentPID ,
2017-01-16 22:58:58 -07:00
Description : description ,
2021-11-30 13:06:32 -07:00
Start : start ,
2019-11-30 07:40:22 -07:00
Cancel : cancel ,
2017-01-16 22:58:58 -07:00
}
2021-11-30 13:06:32 -07:00
finished := func ( ) {
cancel ( )
pm . remove ( process )
}
if parent != nil {
parent . AddChild ( process )
}
pm . processes [ pid ] = process
2017-01-16 22:58:58 -07:00
pm . mutex . Unlock ( )
2021-11-30 13:06:32 -07:00
return pid , finished
}
// nextPID will return the next available PID. pm.mutex should already be locked.
func ( pm * Manager ) nextPID ( ) ( start time . Time , pid IDType ) {
start = time . Now ( )
startUnix := start . Unix ( )
if pm . lastTime == startUnix {
pm . next ++
} else {
pm . next = 1
}
pm . lastTime = startUnix
pid = IDType ( strconv . FormatInt ( start . Unix ( ) , 16 ) )
if pm . next == 1 {
return
}
pid = IDType ( string ( pid ) + "-" + strconv . FormatInt ( pm . next , 10 ) )
return
2014-06-18 23:08:03 -06:00
}
2017-01-16 22:58:58 -07:00
// Remove a process from the ProcessManager.
2021-11-30 13:06:32 -07:00
func ( pm * Manager ) Remove ( pid IDType ) {
2017-01-16 22:58:58 -07:00
pm . mutex . Lock ( )
2019-11-30 07:40:22 -07:00
delete ( pm . processes , pid )
2017-01-16 22:58:58 -07:00
pm . mutex . Unlock ( )
}
2021-11-30 13:06:32 -07:00
func ( pm * Manager ) remove ( process * Process ) {
pm . mutex . Lock ( )
if p := pm . processes [ process . PID ] ; p == process {
delete ( pm . processes , process . PID )
}
parent := pm . processes [ process . ParentPID ]
pm . mutex . Unlock ( )
if parent == nil {
return
}
parent . RemoveChild ( process )
}
2019-11-30 07:40:22 -07:00
// Cancel a process in the ProcessManager.
2021-11-30 13:06:32 -07:00
func ( pm * Manager ) Cancel ( pid IDType ) {
2019-11-30 07:40:22 -07:00
pm . mutex . Lock ( )
process , ok := pm . processes [ pid ]
pm . mutex . Unlock ( )
if ok {
process . Cancel ( )
}
}
// Processes gets the processes in a thread safe manner
2021-11-30 13:06:32 -07:00
func ( pm * Manager ) Processes ( onlyRoots bool ) [ ] * Process {
2019-11-30 07:40:22 -07:00
pm . mutex . Lock ( )
processes := make ( [ ] * Process , 0 , len ( pm . processes ) )
2021-11-30 13:06:32 -07:00
if onlyRoots {
for _ , process := range pm . processes {
if _ , has := pm . processes [ process . ParentPID ] ; ! has {
processes = append ( processes , process )
}
}
} else {
for _ , process := range pm . processes {
processes = append ( processes , process )
}
2019-11-30 07:40:22 -07:00
}
pm . mutex . Unlock ( )
2021-11-30 13:06:32 -07:00
sort . Slice ( processes , func ( i , j int ) bool {
left , right := processes [ i ] , processes [ j ]
return left . Start . Before ( right . Start )
} )
2019-11-30 07:40:22 -07:00
return processes
}
2017-01-16 22:58:58 -07:00
// Exec a command and use the default timeout.
func ( pm * Manager ) Exec ( desc , cmdName string , args ... string ) ( string , string , error ) {
return pm . ExecDir ( - 1 , "" , desc , cmdName , args ... )
}
// ExecTimeout a command and use a specific timeout duration.
func ( pm * Manager ) ExecTimeout ( timeout time . Duration , desc , cmdName string , args ... string ) ( string , string , error ) {
return pm . ExecDir ( timeout , "" , desc , cmdName , args ... )
}
// ExecDir a command and use the default timeout.
func ( pm * Manager ) ExecDir ( timeout time . Duration , dir , desc , cmdName string , args ... string ) ( string , string , error ) {
return pm . ExecDirEnv ( timeout , dir , desc , nil , cmdName , args ... )
}
2016-11-26 19:10:08 -07:00
// ExecDirEnv runs a command in given path and environment variables, and waits for its completion
2016-11-15 00:06:31 -07: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)
2017-01-16 22:58:58 -07:00
func ( pm * Manager ) ExecDirEnv ( timeout time . Duration , dir , desc string , env [ ] string , cmdName string , args ... string ) ( string , string , error ) {
2019-10-11 18:13:27 -06:00
return pm . ExecDirEnvStdIn ( timeout , dir , desc , env , nil , cmdName , args ... )
}
// ExecDirEnvStdIn runs a command in given path and environment variables with provided stdIN, 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)
func ( pm * Manager ) ExecDirEnvStdIn ( timeout time . Duration , dir , desc string , env [ ] string , stdIn io . Reader , cmdName string , args ... string ) ( string , string , error ) {
2014-07-06 15:32:36 -06:00
if timeout == - 1 {
2017-01-16 22:58:58 -07:00
timeout = 60 * time . Second
2014-07-06 15:32:36 -06:00
}
2017-01-16 22:58:58 -07:00
stdOut := new ( bytes . Buffer )
stdErr := new ( bytes . Buffer )
2014-06-18 23:08:03 -06:00
2021-11-30 13:06:32 -07:00
ctx , _ , finished := pm . AddContextTimeout ( DefaultContext , timeout , desc )
defer finished ( )
2017-11-13 07:51:45 -07:00
cmd := exec . CommandContext ( ctx , cmdName , args ... )
2014-06-18 23:08:03 -06:00
cmd . Dir = dir
2016-11-26 19:10:08 -07:00
cmd . Env = env
2017-01-16 22:58:58 -07:00
cmd . Stdout = stdOut
cmd . Stderr = stdErr
2019-10-11 18:13:27 -06:00
if stdIn != nil {
cmd . Stdin = stdIn
}
2014-07-06 15:32:36 -06:00
if err := cmd . Start ( ) ; err != nil {
2017-01-16 22:58:58 -07:00
return "" , "" , err
2014-07-06 15:32:36 -06:00
}
2014-06-18 23:08:03 -06:00
2017-11-13 07:51:45 -07:00
err := cmd . Wait ( )
2014-06-18 23:08:03 -06:00
2017-01-16 22:58:58 -07:00
if err != nil {
2020-06-13 15:47:31 -06:00
err = & Error {
2021-11-30 13:06:32 -07:00
PID : GetPID ( ctx ) ,
2020-06-13 15:47:31 -06:00
Description : desc ,
Err : err ,
CtxErr : ctx . Err ( ) ,
Stdout : stdOut . String ( ) ,
Stderr : stdErr . String ( ) ,
}
2014-06-18 23:08:03 -06:00
}
2017-01-16 22:58:58 -07:00
2017-11-13 07:51:45 -07:00
return stdOut . String ( ) , stdErr . String ( ) , err
2014-06-18 23:08:03 -06:00
}
2020-06-13 15:47:31 -06:00
// Error is a wrapped error describing the error results of Process Execution
type Error struct {
2021-11-30 13:06:32 -07:00
PID IDType
2020-06-13 15:47:31 -06:00
Description string
Err error
CtxErr error
Stdout string
Stderr string
}
func ( err * Error ) Error ( ) string {
2021-11-30 13:06:32 -07:00
return fmt . Sprintf ( "exec(%s:%s) failed: %v(%v) stdout: %s stderr: %s" , err . PID , err . Description , err . Err , err . CtxErr , err . Stdout , err . Stderr )
2020-06-13 15:47:31 -06:00
}
// Unwrap implements the unwrappable implicit interface for go1.13 Unwrap()
func ( err * Error ) Unwrap ( ) error {
return err . Err
}