forked from apptainer/singularity
/
monitor.go
34 lines (30 loc) · 941 Bytes
/
monitor.go
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
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package singularity
import (
"fmt"
"os"
"syscall"
)
// MonitorContainer monitors a container
func (engine *EngineOperations) MonitorContainer(pid int, signals chan os.Signal) (syscall.WaitStatus, error) {
var status syscall.WaitStatus
for {
s := <-signals
switch s {
case syscall.SIGCHLD:
if wpid, err := syscall.Wait4(pid, &status, syscall.WNOHANG, nil); err != nil {
return status, fmt.Errorf("error while waiting child: %s", err)
} else if wpid != pid {
continue
}
return status, nil
default:
if err := syscall.Kill(pid, s.(syscall.Signal)); err != nil {
return status, fmt.Errorf("interrupted by signal %s", s.String())
}
}
}
}