/
master.go
137 lines (123 loc) · 3.48 KB
/
master.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
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
// 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 starter
import (
"fmt"
"io"
"net"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/sylabs/singularity/internal/pkg/runtime/engines"
starterConfig "github.com/sylabs/singularity/internal/pkg/runtime/engines/config/starter"
"github.com/sylabs/singularity/internal/pkg/sylog"
)
// Master initializes a runtime engine and runs it
func Master(rpcSocket, masterSocket int, sconfig *starterConfig.Config, jsonBytes []byte) {
var fatal error
var status syscall.WaitStatus
fatalChan := make(chan error, 1)
ppid := os.Getppid()
containerPid := sconfig.GetContainerPid()
engine, err := engines.NewEngine(jsonBytes)
if err != nil {
sylog.Fatalf("failed to initialize runtime: %s\n", err)
}
go func() {
comm := os.NewFile(uintptr(rpcSocket), "socket")
rpcConn, err := net.FileConn(comm)
comm.Close()
if err != nil {
fatalChan <- fmt.Errorf("failed to copy unix socket descriptor: %s", err)
return
}
runtime.LockOSThread()
err = engine.CreateContainer(containerPid, rpcConn)
if err != nil {
fatalChan <- fmt.Errorf("container creation failed: %s", err)
} else {
rpcConn.Close()
}
runtime.Goexit()
}()
go func() {
data := make([]byte, 1)
comm := os.NewFile(uintptr(masterSocket), "master-socket")
conn, err := net.FileConn(comm)
comm.Close()
if err != nil {
fatalChan <- fmt.Errorf("failed to create master connection: %s", err)
}
defer conn.Close()
_, err = conn.Read(data)
if err != nil && err != io.EOF {
if sconfig.GetInstance() && os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR2)
}
fatalChan <- fmt.Errorf("failed to start process: %s", err)
return
}
err = engine.PostStartProcess(containerPid)
if err != nil {
if sconfig.GetInstance() && os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR2)
}
fatalChan <- fmt.Errorf("post start process failed: %s", err)
return
}
if sconfig.GetInstance() {
// sleep a bit to see if child exit
time.Sleep(100 * time.Millisecond)
if os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR1)
}
}
}()
go func() {
// catch all signals
signals := make(chan os.Signal, 1)
signal.Notify(signals)
status, err = engine.MonitorContainer(containerPid, signals)
fatalChan <- err
}()
fatal = <-fatalChan
runtime.LockOSThread()
if err := engine.CleanupContainer(); err != nil {
sylog.Errorf("container cleanup failed: %s", err)
}
runtime.UnlockOSThread()
if fatal != nil {
if sconfig.GetInstance() {
if os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR2)
}
}
syscall.Kill(containerPid, syscall.SIGKILL)
sylog.Fatalf("%s", fatal)
}
if status.Signaled() {
sylog.Debugf("Child exited due to signal %d", status.Signal())
if sconfig.GetInstance() && os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR2)
}
os.Exit(128 + int(status.Signal()))
} else if status.Exited() {
sylog.Debugf("Child exited with exit status %d", status.ExitStatus())
if sconfig.GetInstance() {
if status.ExitStatus() != 0 {
if os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR2)
sylog.Fatalf("failed to spawn instance")
}
}
if os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR1)
}
}
os.Exit(status.ExitStatus())
}
}