forked from shenwei356/rush
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
1424 lines (1275 loc) · 37.1 KB
/
Copy pathprocess.go
File metadata and controls
1424 lines (1275 loc) · 37.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2017-2023 Wei Shen <shenwei356@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package process
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/cznic/sortutil"
"github.com/pkg/errors"
pb "github.com/schollz/progressbar/v3"
"github.com/shenwei356/go-logging"
psutil "github.com/shirou/gopsutil/process"
)
// Log is *logging.Logger
var Log *logging.Logger
// pid_numSecondsSinceEpoch
var ChildMarker string = strconv.Itoa(os.Getpid()) + "_" + strconv.FormatInt(time.Now().Unix(), 16)
func init() {
if Log == nil {
logFormat := logging.MustStringFormatter(`%{color}[%{level:.4s}]%{color:reset} %{message}`)
backend := logging.NewLogBackend(os.Stderr, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, logFormat)
logging.SetBackend(backendFormatter)
Log = logging.MustGetLogger("process")
}
}
// Command is the Command struct
type Command struct {
ID uint64 // ID
Cmd string // command
Cancel <-chan struct{} // channel for close
Timeout time.Duration // time out
ctx context.Context // context.WithTimeout
ctxCancel context.CancelFunc // cancel func for timetout
Ch chan string // channel for stdout
reader *bufio.Reader // reader for stdout
tmpfile string // tmpfile for stdout
tmpfh *os.File // file handler for tmpfile
finishSendOutput bool // a flag of whether finished sending output to Ch
Err error // Error
Duration time.Duration // runtime
dryrun bool
exitStatus int
Executed chan int // for checking if the command has been executed
}
// NewCommand create a Command
func NewCommand(id uint64, cmdStr string, cancel <-chan struct{}, timeout time.Duration) *Command {
command := &Command{
ID: id,
Cmd: strings.TrimLeft(cmdStr, " \t\r\n"),
Cancel: cancel,
Timeout: timeout,
Executed: make(chan int, 2),
}
return command
}
func (c *Command) String() string {
return fmt.Sprintf("cmd #%d: %s", c.ID, c.Cmd)
}
// Verbose decides whether print extra information
var Verbose bool
var tmpfilePrefix = fmt.Sprintf("rush.%d.", os.Getpid())
// TmpOutputDataBuffer is buffer size for output of a command before saving to tmpfile,
// default 1M.
var TmpOutputDataBuffer = 1048576 // 1M
// OutputChunkSize is buffer size of output string chunk sent to channel, default 16K.
var OutputChunkSize = 16384 // 16K
// Run runs a command and send output to command.Ch in background.
func (c *Command) Run(opts *Options, tryNumber int) (chan string, error) {
// create a return chan here; we will set the c.Ch in the parent
ch := make(chan string, 1)
if c.dryrun {
ch <- c.Cmd + "\n"
close(ch)
c.finishSendOutput = true
close(c.Executed)
return ch, nil
}
c.Err = c.run(opts, tryNumber)
// don't return here, keep going so we can display
// the output from commands that error
var readErr error = nil
if Verbose {
if c.exitStatus == 0 {
Log.Infof("finish cmd #%d in %s: %s: exit status %d", c.ID, c.Duration, c.Cmd, c.exitStatus)
} else {
// exitStatus will appear in wait cmd message
Log.Infof("finish cmd #%d in %s: %s", c.ID, c.Duration, c.Cmd)
}
}
go func() {
if opts.ImmediateOutput {
close(ch)
c.finishSendOutput = true
} else {
if c.tmpfile != "" { // data saved in tempfile
c.reader = bufio.NewReader(c.tmpfh)
}
buf := make([]byte, OutputChunkSize)
var n int
var i int
var b bytes.Buffer
var bb []byte
var existedN int
// var N uint64
for {
if c.reader != nil {
n, readErr = c.reader.Read(buf)
} else {
n = 0
readErr = io.EOF
}
existedN = b.Len()
b.Write(buf[0:n])
if readErr != nil {
if readErr == io.EOF {
if b.Len() > 0 {
// if Verbose {
// N += uint64(b.Len())
// }
ch <- b.String() // string(buf[0:n])
}
b.Reset()
readErr = nil
}
break
}
bb = b.Bytes()
i = bytes.LastIndexByte(bb, '\n')
if i < 0 {
continue
}
// if Verbose {
// N += uint64(len(bb[0 : i+1]))
// }
ch <- string(bb[0 : i+1]) // string(buf[0:n])
b.Reset()
if i-existedN+1 < n {
// ------ ======i========n
// existed buf
// 5 4 6
b.Write(buf[i-existedN+1 : n])
}
// N += n
}
// if Verbose {
// Log.Debugf("cmd #%d sent %d bytes\n", c.ID, N)
// }
// if Verbose {
// Log.Infof("finish reading data from: %s", c.Cmd)
// }
close(ch)
c.finishSendOutput = true
}
}()
if c.Err != nil {
return ch, c.Err
} else {
if readErr != nil {
return ch, readErr
} else {
return ch, nil
}
}
}
// Cleanup removes tmpfile
func (c *Command) Cleanup() error {
var err error
if c.tmpfh != nil {
// if Verbose {
// Log.Infof("close tmpfh for: %s", c.Cmd)
// }
err = c.tmpfh.Close()
if err != nil {
return err
}
}
if c.tmpfile != "" {
if Verbose {
Log.Infof("remove tmpfile (%s) for command: %s", c.tmpfile, c.Cmd)
}
err = os.Remove(c.tmpfile)
}
return err
}
// ErrTimeout means command timeout
var ErrTimeout = fmt.Errorf("time out")
// ErrCancelled means command being cancelled
var ErrCancelled = fmt.Errorf("cancelled")
func (c *Command) getExitStatus(err error) int {
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
return waitStatus.ExitStatus()
}
// no error, so return exitStatus 0
return 0
}
type TopLevelEnum int
const (
NotTopLevel TopLevelEnum = 0
TopLevel TopLevelEnum = 1
)
// lexicographically encode integer
// based on http://www.zanopha.com/docs/elen.pdf
func lexEncode(n uint64, topLevel TopLevelEnum) string {
var encoded string
// recursively calculate lex prefix
// the lex prefix allows the user to lexicographically sort the output
// need lex prefix if n has more than one digit
nstr := fmt.Sprintf("%d", n)
nlen := uint64(len(nstr))
if nlen > 1 {
// include non-numeric part of lex prefix
// to allow proper sorting, this char must come after numerics in the ascii table
encoded = "_"
// then include recursive part
encoded += lexEncode(nlen, NotTopLevel)
// conditionally include lex separator
if topLevel == TopLevel {
// the lex separator allows the user to differentiate a numeric part of the lex prefix from the original number
// to allow proper sorting, the lex separator must come before numerics in the ascii table
encoded += "."
}
}
// include numeric part of lex prefix, or
// original number (if topLevel==true)
encoded += nstr
return encoded
}
func getEntrySeparator() string {
// to allow proper sorting, the entry separator must come before numerics in the ascii table
return "/"
}
// ImmediateLineWriter is safe to use concurrently
type ImmediateLineWriter struct {
lock *sync.Mutex
numJobs int
cmdId uint64
tryNumber int
line string
lineNumber uint64
includePrefix bool
}
func includeImmediatePrefix(cmdId uint64, tryNumber int, lineNumber uint64, data *string) {
prefix := fmt.Sprintf("(%s", lexEncode(cmdId, TopLevel))
prefix += fmt.Sprintf("%s%s", getEntrySeparator(), lexEncode(uint64(tryNumber), TopLevel))
prefix += fmt.Sprintf("%s%s): ", getEntrySeparator(), lexEncode(lineNumber, TopLevel))
if data != nil {
*data = *data + prefix
}
}
func NewImmediateLineWriter(lock *sync.Mutex, numJobs int, cmdId uint64, tryNumber int) *ImmediateLineWriter {
lw := &ImmediateLineWriter{}
lw.lock = lock
lw.numJobs = numJobs
lw.cmdId = cmdId
lw.tryNumber = tryNumber
lw.lineNumber = 1 // start with 1
lw.includePrefix = true // start line 1 with a prefix
return lw
}
func (lw *ImmediateLineWriter) addPrefixIfNeeded(output *string) {
if lw.includePrefix {
includeImmediatePrefix(lw.cmdId, lw.tryNumber, lw.lineNumber, output)
lw.includePrefix = false
}
}
func (lw *ImmediateLineWriter) WritePrefixedLines(input string, outfh *os.File) {
if lw.lock != nil {
// make immediate output thread-safe and do one write at a time
lw.lock.Lock()
// only include prefixes if jobs are running in parallel
if lw.numJobs > 1 {
var output string
// split by \r\n or \n
reg := regexp.MustCompile("(?:\r\n|\n)")
matchExtents := reg.FindAllStringIndex(input, -1)
if len(matchExtents) > 0 {
lastStart := 0
for _, matchExtent := range matchExtents {
beforePart := input[lastStart:matchExtent[0]]
lw.line = lw.line + beforePart
// skip empty lines
if len(lw.line) > 0 {
// there is some data in this part, so add prefix if needed
lw.addPrefixIfNeeded(&output)
// append the chars up to and including the delimiter
delimiterPart := input[matchExtent[0]:matchExtent[1]]
output = output + beforePart + delimiterPart
// defer including prefix, so only add it on next non-empty data
lw.includePrefix = true
// clear line, since saw delimiter
lw.line = ""
lw.lineNumber++
}
lastStart = matchExtent[1]
}
// append any remaining chars after the last delimiter
if lastStart < len(input) {
lastPart := input[lastStart:]
// there is some data in this part, so add prefix if needed
lw.addPrefixIfNeeded(&output)
lw.line = lw.line + lastPart
output = output + lastPart
}
} else {
// no delimiters in this section
// there is some input, so add prefix if needed
lw.addPrefixIfNeeded(&output)
lw.line = lw.line + input
output = output + input
}
if outfh != nil {
outfh.WriteString(output)
}
} else {
// no prefixes needed, since jobs are running serially
// just use the input string
if outfh != nil {
outfh.WriteString(input)
}
}
lw.lock.Unlock()
}
}
type ImmediateWriter struct {
lineWriter *ImmediateLineWriter
fh *os.File
}
func NewImmediateWriter(lineWriter *ImmediateLineWriter, fh *os.File) *ImmediateWriter {
iw := &ImmediateWriter{}
iw.lineWriter = lineWriter
iw.fh = fh
return iw
}
func (iw ImmediateWriter) Write(p []byte) (n int, err error) {
dataLen := len(p)
// only write non-empty data
if dataLen > 0 {
iw.lineWriter.WritePrefixedLines(string(p), iw.fh)
}
return dataLen, nil
}
// from https://softwareengineering.stackexchange.com/questions/177428/sets-data-structure-in-golang
type IntSet struct {
// set map[int]bool
set sync.Map
}
func (set *IntSet) Add(i int) bool {
// _, found := set.set[i]
// set.set[i] = true
_, found := set.set.Load(i)
set.set.Store(i, true)
return !found //False if it existed already
}
const (
INVALID_HANDLE int = 0
CTRL_C_SIGNAL int = 0
CTRL_BREAK_SIGNAL int = 1
KILL_SIGNAL int = 2
// bit mask
SEND_NO_SIGNAL int = 0
SEND_CTRL_C_SIGNAL int = 1
SEND_CTRL_BREAK_SIGNAL int = 2
SEND_KILL_SIGNAL int = 4
)
func canSendSignal(childProcessName string, noSignalExes []string) (canSendSignal bool, err error) {
canSendSignal = true // first assume true
err = nil // first assume no error
if len(noSignalExes) > 0 {
for _, noSignalExe := range noSignalExes {
if noSignalExe == "all" {
canSendSignal = false
break
} else {
if childProcessName == noSignalExe {
canSendSignal = false
break
}
}
}
}
return canSendSignal, err
}
type ProcessRecord struct {
pid int
processHandle int
processExists bool
accessGranted bool
signalsToSend int
}
var pidRecords = make(map[int]ProcessRecord)
func getProcessRecordFromPid(pidRecordsLock *sync.Mutex, pid int) (processRecord ProcessRecord, err error) {
pidRecordsLock.Lock()
processRecord, keyPresent := pidRecords[pid]
if !keyPresent {
processHandle, processExists, accessGranted, err := getProcess(pid)
if processHandle != INVALID_HANDLE && err == nil {
processRecord = ProcessRecord{
pid: pid,
processHandle: processHandle,
processExists: processExists,
accessGranted: accessGranted,
signalsToSend: SEND_NO_SIGNAL}
pidRecords[pid] = processRecord
}
}
pidRecordsLock.Unlock()
return
}
func checkChildProcess(pidRecordsLock *sync.Mutex, childCheckProcess *psutil.Process, noStopExes []string, noKillExes []string) (
processHandle int,
considerChild bool,
signalsToSend int,
err error) {
considerChild = false // first assume false
signalsToSend = SEND_NO_SIGNAL // first assume no signal
// use err2 for getProcessRecordFromPid(), since child may no longer exist
processRecord, err2 := getProcessRecordFromPid(pidRecordsLock, int(childCheckProcess.Pid))
processHandle = processRecord.processHandle
if err2 == nil {
if processHandle != INVALID_HANDLE {
// Don't look at parent-child relationships, since children, grandchildren, etc.
// could become orphaned at any time. Just look for the child marker to know.
considerChild, err = doesChildHaveMarker(childCheckProcess, processHandle)
if err == nil {
if considerChild {
var childProcessName string = ""
if len(noStopExes) > 0 || len(noKillExes) > 0 {
childProcessName, err = childCheckProcess.Name()
if err == nil {
if len(childProcessName) == 0 {
err = errors.New("childProcessName is empty")
}
}
if err != nil {
if Verbose {
Log.Error(err)
}
}
}
signalsToSend, err = getSignalsToSend(childProcessName, noStopExes, noKillExes)
}
} else {
if Verbose {
Log.Error(err)
}
}
} else {
// failed to open child process, so don't consider it
}
} else {
// failed to open child process, so don't consider it
// check response
if processRecord.processExists {
if processRecord.accessGranted {
// report errors from processes we could access
if Verbose {
Log.Error(err2)
}
} else { // access denied
// ignore error, since we failed to get a handle to the child
// it could be a system process that we are skipping anyway
}
} else { // process no longer exists
// ignore error since no process to signal
}
}
return
}
// get a process record when the process belongs to this rush invocation
func getProcessTreeRecursive(
pidRecordsLock *sync.Mutex,
childCheckProcess *psutil.Process,
noStopExes []string,
noKillExes []string,
pidsVisited *IntSet,
) (processRecords []ProcessRecord) {
if considerPid(int(childCheckProcess.Pid)) {
// avoid cycles in pid tree by looking at visited set
if pidsVisited.Add(int(childCheckProcess.Pid)) {
processHandle, considerChild, signalsToSend, err := checkChildProcess(
pidRecordsLock,
childCheckProcess,
noStopExes,
noKillExes)
if err != nil {
if Verbose {
Log.Error(err)
}
}
if processHandle != INVALID_HANDLE {
if considerChild {
var processRecord = ProcessRecord{
processHandle: processHandle, pid: int(childCheckProcess.Pid), signalsToSend: signalsToSend}
processRecords = append(processRecords, processRecord)
} else {
pidRecordsLock.Lock()
releaseProcessByPid(int(childCheckProcess.Pid))
pidRecordsLock.Unlock()
}
}
}
}
return processRecords
}
func getChildProcesses(pidRecordsLock *sync.Mutex, noStopExes []string, noKillExes []string) (processRecords []ProcessRecord) {
// handle normal and orphaned children by getting all processes
// we'll check for duplicates later
allProcesses, err := psutil.Processes()
if err == nil {
// pidsVisited := IntSet{set: make(map[int]bool)}
pidsVisited := IntSet{set: sync.Map{}}
threads := 8 // runtime.NumCPU() 16 will panic
done := make(chan int)
ch := make(chan ProcessRecord, threads)
go func() {
for p := range ch {
processRecords = append(processRecords, p)
}
done <- 1
}()
tokens := make(chan int, threads)
var wg sync.WaitGroup
for _, childCheckProcess := range allProcesses {
wg.Add(1)
tokens <- 1
go func(childCheckProcess *psutil.Process) {
subProcessRecords := getProcessTreeRecursive(
pidRecordsLock,
childCheckProcess,
noStopExes,
noKillExes,
&pidsVisited)
for _, subProcessRecord := range subProcessRecords {
// processRecords = append(processRecords, subProcessRecord)
ch <- subProcessRecord
}
wg.Done()
<-tokens
}(childCheckProcess)
}
wg.Wait()
close(ch)
<-done
}
return processRecords
}
func signalChildProcesses(processRecords []ProcessRecord, signalNum int) (numChildrenSignaled int) {
// signal child processes
numChildrenSignaled = 0
expectedNumChildrenSignaled := 0
for _, processRecord := range processRecords {
sendSignal := false // first assume false
switch signalNum {
case CTRL_C_SIGNAL:
if processRecord.signalsToSend&SEND_CTRL_C_SIGNAL != 0 {
sendSignal = true
}
case CTRL_BREAK_SIGNAL:
if processRecord.signalsToSend&SEND_CTRL_BREAK_SIGNAL != 0 {
sendSignal = true
}
case KILL_SIGNAL:
if processRecord.signalsToSend&SEND_KILL_SIGNAL != 0 {
sendSignal = true
}
default:
Log.Error(errors.New("Unexpected signalNum"))
}
if sendSignal {
expectedNumChildrenSignaled += 1
err := signalProcess(processRecord, signalNum)
if err == nil {
numChildrenSignaled += 1
} else {
if Verbose {
Log.Error(err)
}
}
}
}
if expectedNumChildrenSignaled > 0 && numChildrenSignaled == 0 {
switch signalNum {
case CTRL_C_SIGNAL:
Log.Info("no child processes sent Ctrl+C signal")
case CTRL_BREAK_SIGNAL:
Log.Info("no child processes sent Ctrl+Break signal")
case KILL_SIGNAL:
Log.Info("no child processes killed")
default:
Log.Error(errors.New("Unexpected signalNum"))
}
}
return numChildrenSignaled
}
func anyRemainingChildren(processRecords []ProcessRecord) (anyRemaining bool) {
anyRemaining = false
for _, processRecord := range processRecords {
if doesProcessExist(processRecord.processHandle) {
anyRemaining = true
break
}
}
return anyRemaining
}
func pollRemainingChildren(processRecords []ProcessRecord, cleanupTime time.Duration, forceStop <-chan struct{}) (anyRemaining bool) {
anyRemaining = false
startTime := time.Now()
sleepTime := 250 * time.Millisecond
for {
continuePolling := false
anyRemaining = anyRemainingChildren(processRecords)
if anyRemaining && cleanupTime > 0 {
select {
case <-time.After(sleepTime):
case <-forceStop:
return true
}
elapsedTime := time.Since(startTime)
if elapsedTime < cleanupTime {
// exponential back off with limit:
// increase sleep time if next elapsedTime is below 1/2 of cleanupTime
if elapsedTime+sleepTime*2 < cleanupTime/2 {
// exponential back off
sleepTime *= 2
} else {
// use the same sleepTime as before
}
continuePolling = true
}
}
if !continuePolling {
break
}
}
return anyRemaining
}
// ensure our child processes are stopped
func stopChildProcesses(pidRecordsLock *sync.Mutex, noStopExes []string, noKillExes []string, cleanupTime time.Duration, forceStop <-chan struct{}) (err error) {
err = nil // first assume no error
anyRemaining := true // first assume some children
totalNumSignaled := 0
if canStopChildProcesses() {
processRecords := getChildProcesses(pidRecordsLock, noStopExes, noKillExes)
// progress from most graceful to most invasive stop signal
// if no matching children, then call is a noop
numSignaled := signalChildProcesses(processRecords, CTRL_C_SIGNAL)
if numSignaled > 0 {
totalNumSignaled += numSignaled
anyRemaining = pollRemainingChildren(processRecords, cleanupTime, forceStop)
} else {
anyRemaining = true
}
if anyRemaining {
numSignaled = signalChildProcesses(processRecords, CTRL_BREAK_SIGNAL)
if numSignaled > 0 {
totalNumSignaled += numSignaled
anyRemaining = pollRemainingChildren(processRecords, cleanupTime, forceStop)
} else {
anyRemaining = true
}
if anyRemaining {
numSignaled = signalChildProcesses(processRecords, KILL_SIGNAL)
totalNumSignaled += numSignaled
}
}
anyRemaining = pollRemainingChildren(processRecords, 0, forceStop) // wait zero time, since already waited above
// release process handles only after descending into all processes,
// to ensure pids do not get reused while descending
releaseProcesses(pidRecordsLock)
}
if anyRemaining && totalNumSignaled == 0 {
msg := "No child processes stopped or killed\n"
msg += " " // seven spaces indent
msg += "You will need to manually stop or kill them"
err = errors.New(msg)
}
return err
}
func releaseProcessByPid(pid int) {
// no pidRecordsLock here, rely on caller to do it
processRecord, keyPresent := pidRecords[pid]
if keyPresent {
delete(pidRecords, processRecord.pid)
releaseProcessByHandle(processRecord.processHandle)
}
}
func releaseProcesses(pidRecordsLock *sync.Mutex) {
pidRecordsLock.Lock()
for _, processRecord := range pidRecords {
releaseProcessByPid(processRecord.pid)
}
pidRecordsLock.Unlock()
}
func getChildMarkerKey() string {
return "RUSH_CHILD_GROUP"
}
func getChildMarkerValue() string {
// place brackets on either side of the marker,
// so we only find exact matches
return "[" + ChildMarker + "]"
}
func getChildMarkerRegex() *regexp.Regexp {
// match string with one or more [pid_timestamp] values
return regexp.MustCompile(getChildMarkerKey() + "=\\[[0-z]+\\]")
}
func containsMarker(env string) bool {
childMarkerRegex := getChildMarkerRegex()
childMarkerValue := getChildMarkerValue()
match := childMarkerRegex.FindString(env)
return strings.Contains(match, childMarkerValue)
}
// run a command and pass output to c.reader.
// Note that output returns only after finishing run.
// This function is mainly borrowed from https://github.com/brentp/gargs .
func (c *Command) run(opts *Options, tryNumber int) error {
t := time.Now()
chCancelMonitor := make(chan struct{})
defer func() {
close(chCancelMonitor)
c.Duration = time.Since(t)
close(c.Executed)
}()
var command *exec.Cmd
qcmd := fmt.Sprintf(`%s`, c.Cmd)
if Verbose {
Log.Infof("start cmd #%d: %s", c.ID, qcmd)
}
if c.Timeout > 0 {
c.ctx, c.ctxCancel = context.WithTimeout(context.Background(), c.Timeout)
command = getCommand(c.ctx, qcmd)
} else {
command = getCommand(context.TODO(), qcmd)
}
// mark child processes with our pid,
// so we can identify them later,
// in case we need to signal them
childMarkerKey := getChildMarkerKey()
childMarkerValue := getChildMarkerValue()
priorValue, found := os.LookupEnv(childMarkerKey)
if found {
// append marker values to sames key, so
// we can handle the nested calls case
childMarkerValue = priorValue + childMarkerValue
}
childMarker := fmt.Sprintf("%s=%s", childMarkerKey, childMarkerValue)
// command de-dups variables, in favor of later values
command.Env = append(os.Environ(), childMarker)
var pipeStdout io.ReadCloser = nil
var err error = nil
if opts.ImmediateOutput {
lineWriter := NewImmediateLineWriter(&opts.ImmediateLock, opts.Jobs, c.ID, tryNumber)
command.Stdout = NewImmediateWriter(lineWriter, opts.OutFileHandle)
command.Stderr = NewImmediateWriter(lineWriter, opts.ErrFileHandle)
} else {
pipeStdout, err = command.StdoutPipe()
if err != nil {
return errors.Wrapf(err, "get stdout pipe of cmd #%d: %s", c.ID, c.Cmd)
}
// no code yet for stderr handling, so just have it go to os.Stderr
command.Stderr = os.Stderr
}
err = command.Start()
if err != nil {
return errors.Wrapf(err, "start cmd #%d: %s", c.ID, c.Cmd)
}
var outPipe *bufio.Reader = nil
if !opts.ImmediateOutput {
outPipe = bufio.NewReaderSize(pipeStdout, TmpOutputDataBuffer)
// no errPipe setting here, since having the command's stderr go to os.Stderr above
}
chErr := make(chan error, 2) // may come from three sources, must be buffered
chEndBeforeTimeout := make(chan struct{})
go func() {
select {
case <-c.Cancel:
if Verbose {
Log.Warningf("cancel cmd #%d: %s", c.ID, c.Cmd)
}
opts.stopChildren()
chErr <- ErrCancelled
case <-chCancelMonitor:
// default: // must not use default, if you must use, use for loop
}
}()
// detect timeout
if c.Timeout > 0 {
go func() { // goroutine #T
select {
case <-c.ctx.Done():
chErr <- ErrTimeout
c.ctxCancel()
return
case <-chEndBeforeTimeout:
chErr <- nil
return
}
}()
}
// --------------------------------
// handle output
var readed []byte
if c.Timeout > 0 {
// known shortcoming: this goroutine will remains even after timeout!
// this will cause data race.
go func() { // goroutine #P
// Peek is blocked method, it waits command even after timeout!!
if opts.ImmediateOutput {
// set EOF here, since handling output in readLine() above
err = io.EOF
} else {
readed, err = outPipe.Peek(TmpOutputDataBuffer)
}
chErr <- err
}()
err = <-chErr // from timeout #T or peek #P
} else {
if opts.ImmediateOutput {
// set EOF here, since handling output in readLine() above
err = io.EOF
} else {
readed, err = outPipe.Peek(TmpOutputDataBuffer)
}
}
// less than TmpOutputDataBuffer bytes in output...
if err == bufio.ErrBufferFull || err == io.EOF {
if c.Timeout > 0 {
go func() { // goroutine #W
err1 := command.Wait()
chErr <- err1
close(chEndBeforeTimeout)
}()
err = <-chErr // from timeout #T or normal exit #W
<-chErr // from normal exit #W or timeout #T
} else {
err = command.Wait()
}
if opts.PropExitStatus {
c.exitStatus = c.getExitStatus(err)
}
if !opts.ImmediateOutput {
// get reader even on error, so we can still print the stdout and stderr of the failed child process
c.reader = bufio.NewReader(bytes.NewReader(readed))
}
if err != nil {
if strings.Contains(err.Error(), "interrupt") {
return nil
}
return errors.Wrapf(err, "wait cmd #%d: %s", c.ID, c.Cmd)
}
c.Executed <- 1 // the command is executed!
return nil
}
// more than TmpOutputDataBuffer bytes in output. must use tmpfile
if opts.ImmediateOutput {
panic("code assumes immediate output case does not use tmpfile")
}
if err != nil {
return errors.Wrapf(err, "run cmd #%d: %s", c.ID, c.Cmd)
}
c.tmpfh, err = os.CreateTemp("", tmpfilePrefix)
if err != nil {
return errors.Wrapf(err, "create tmpfile for cmd #%d: %s", c.ID, c.Cmd)
}
c.tmpfile = c.tmpfh.Name()
if Verbose {
Log.Infof("create tmpfile (%s) for command: %s", c.tmpfile, c.Cmd)
}