2014-06-19 10:38:03 +05:30
// Copyright 2014 The Gogs Authors. All rights reserved.
2019-10-12 05:43:27 +05:30
// Copyright 2019 The Gitea Authors. All rights reserved.
2014-06-19 10:38:03 +05:30
// 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 20:21:45 +05:30
"context"
2014-06-19 10:38:03 +05:30
"fmt"
2019-10-12 05:43:27 +05:30
"io"
2014-06-19 10:38:03 +05:30
"os/exec"
2019-11-30 20:10:22 +05:30
"sort"
2021-12-01 01:36:32 +05:30
"strconv"
2017-01-17 11:28:58 +05:30
"sync"
2014-06-19 10:38:03 +05:30
"time"
)
2017-01-17 11:28:58 +05:30
// 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-07 03:02:36 +05:30
var (
2021-04-09 13:10:34 +05:30
manager * Manager
managerInit sync . Once
2019-11-30 20:10:22 +05:30
// DefaultContext is the default context to run processing commands in
DefaultContext = context . Background ( )
2014-07-07 03:02:36 +05:30
)
2021-12-01 01:36:32 +05:30
// 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-19 10:38:03 +05:30
2021-12-01 01:36:32 +05:30
// Manager manages all processes and counts PIDs.
2017-01-17 11:28:58 +05:30
type Manager struct {
mutex sync . Mutex
2014-06-19 10:38:03 +05:30
2021-12-01 01:36:32 +05:30
next int64
lastTime int64
processes map [ IDType ] * Process
2017-01-17 11:28:58 +05:30
}
// GetManager returns a Manager and initializes one as singleton if there's none yet
func GetManager ( ) * Manager {
2021-02-04 03:06:38 +05:30
managerInit . Do ( func ( ) {
2017-01-17 11:28:58 +05:30
manager = & Manager {
2021-12-01 01:36:32 +05:30
processes : make ( map [ IDType ] * Process ) ,
next : 1 ,
2017-01-17 11:28:58 +05:30
}
2021-02-04 03:06:38 +05:30
} )
2017-01-17 11:28:58 +05:30
return manager
}
2021-12-01 01:36:32 +05:30
// 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
2022-01-10 15:02:37 +05:30
// to remove the process from the process table. It should not be called until the process is finished but must always be called.
2021-12-01 01:36:32 +05:30
//
// 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-17 11:28:58 +05:30
pm . mutex . Lock ( )
2021-12-01 01:36:32 +05:30
start , pid := pm . nextPID ( )
parent := pm . processes [ parentPID ]
if parent == nil {
parentPID = ""
}
process := & Process {
2017-01-17 11:28:58 +05:30
PID : pid ,
2021-12-01 01:36:32 +05:30
ParentPID : parentPID ,
2017-01-17 11:28:58 +05:30
Description : description ,
2021-12-01 01:36:32 +05:30
Start : start ,
2019-11-30 20:10:22 +05:30
Cancel : cancel ,
2017-01-17 11:28:58 +05:30
}
2021-12-01 01:36:32 +05:30
finished := func ( ) {
cancel ( )
pm . remove ( process )
}
if parent != nil {
parent . AddChild ( process )
}
pm . processes [ pid ] = process
2017-01-17 11:28:58 +05:30
pm . mutex . Unlock ( )
2021-12-01 01:36:32 +05:30
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-19 10:38:03 +05:30
}
2017-01-17 11:28:58 +05:30
// Remove a process from the ProcessManager.
2021-12-01 01:36:32 +05:30
func ( pm * Manager ) Remove ( pid IDType ) {
2017-01-17 11:28:58 +05:30
pm . mutex . Lock ( )
2019-11-30 20:10:22 +05:30
delete ( pm . processes , pid )
2017-01-17 11:28:58 +05:30
pm . mutex . Unlock ( )
}
2021-12-01 01:36:32 +05:30
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 20:10:22 +05:30
// Cancel a process in the ProcessManager.
2021-12-01 01:36:32 +05:30
func ( pm * Manager ) Cancel ( pid IDType ) {
2019-11-30 20:10:22 +05:30
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-12-01 01:36:32 +05:30
func ( pm * Manager ) Processes ( onlyRoots bool ) [ ] * Process {
2019-11-30 20:10:22 +05:30
pm . mutex . Lock ( )
processes := make ( [ ] * Process , 0 , len ( pm . processes ) )
2021-12-01 01:36:32 +05:30
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 20:10:22 +05:30
}
pm . mutex . Unlock ( )
2021-12-01 01:36:32 +05:30
sort . Slice ( processes , func ( i , j int ) bool {
left , right := processes [ i ] , processes [ j ]
return left . Start . Before ( right . Start )
} )
2019-11-30 20:10:22 +05:30
return processes
}
2017-01-17 11:28:58 +05:30
// 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-27 07:40:08 +05:30
// ExecDirEnv runs a command in given path and environment variables, and waits for its completion
2016-11-15 12:36:31 +05:30
// 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-17 11:28:58 +05:30
func ( pm * Manager ) ExecDirEnv ( timeout time . Duration , dir , desc string , env [ ] string , cmdName string , args ... string ) ( string , string , error ) {
2019-10-12 05:43:27 +05:30
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-07 03:02:36 +05:30
if timeout == - 1 {
2017-01-17 11:28:58 +05:30
timeout = 60 * time . Second
2014-07-07 03:02:36 +05:30
}
2017-01-17 11:28:58 +05:30
stdOut := new ( bytes . Buffer )
stdErr := new ( bytes . Buffer )
2014-06-19 10:38:03 +05:30
2021-12-01 01:36:32 +05:30
ctx , _ , finished := pm . AddContextTimeout ( DefaultContext , timeout , desc )
defer finished ( )
2017-11-13 20:21:45 +05:30
cmd := exec . CommandContext ( ctx , cmdName , args ... )
2014-06-19 10:38:03 +05:30
cmd . Dir = dir
2016-11-27 07:40:08 +05:30
cmd . Env = env
2017-01-17 11:28:58 +05:30
cmd . Stdout = stdOut
cmd . Stderr = stdErr
2019-10-12 05:43:27 +05:30
if stdIn != nil {
cmd . Stdin = stdIn
}
2014-07-07 03:02:36 +05:30
if err := cmd . Start ( ) ; err != nil {
2017-01-17 11:28:58 +05:30
return "" , "" , err
2014-07-07 03:02:36 +05:30
}
2014-06-19 10:38:03 +05:30
2017-11-13 20:21:45 +05:30
err := cmd . Wait ( )
2014-06-19 10:38:03 +05:30
2017-01-17 11:28:58 +05:30
if err != nil {
2020-06-14 03:17:31 +05:30
err = & Error {
2021-12-01 01:36:32 +05:30
PID : GetPID ( ctx ) ,
2020-06-14 03:17:31 +05:30
Description : desc ,
Err : err ,
CtxErr : ctx . Err ( ) ,
Stdout : stdOut . String ( ) ,
Stderr : stdErr . String ( ) ,
}
2014-06-19 10:38:03 +05:30
}
2017-01-17 11:28:58 +05:30
2017-11-13 20:21:45 +05:30
return stdOut . String ( ) , stdErr . String ( ) , err
2014-06-19 10:38:03 +05:30
}
2020-06-14 03:17:31 +05:30
// Error is a wrapped error describing the error results of Process Execution
type Error struct {
2021-12-01 01:36:32 +05:30
PID IDType
2020-06-14 03:17:31 +05:30
Description string
Err error
CtxErr error
Stdout string
Stderr string
}
func ( err * Error ) Error ( ) string {
2021-12-01 01:36:32 +05:30
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-14 03:17:31 +05:30
}
// Unwrap implements the unwrappable implicit interface for go1.13 Unwrap()
func ( err * Error ) Unwrap ( ) error {
return err . Err
}