forked from apptainer/singularity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.go
165 lines (143 loc) · 4.09 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
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
// 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"
"unsafe"
"github.com/sylabs/singularity/internal/pkg/runtime/engines"
"github.com/sylabs/singularity/internal/pkg/sylog"
)
// Master initializes a runtime engine and runs it
func Master(rpcSocket, masterSocket int, isInstance bool, containerPid int, engine *engines.Engine) {
var fatal error
var status syscall.WaitStatus
fatalChan := make(chan error, 1)
ppid := os.Getppid()
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()
n, err := conn.Read(data)
if err != nil && err != io.EOF {
if isInstance && os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR2)
}
fatalChan <- fmt.Errorf("failed to start process: %s", err)
return
}
// special path for engines which needs to stop before executing
// container process
if n != 0 {
if obj, ok := engine.EngineOperations.(interface {
PreStartProcess(int, net.Conn, chan error) error
}); ok {
if err := obj.PreStartProcess(containerPid, conn, fatalChan); err != nil {
fatalChan <- fmt.Errorf("pre start process failed: %s", err)
return
}
}
}
err = engine.PostStartProcess(containerPid)
if err != nil {
if isInstance && os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR2)
}
fatalChan <- fmt.Errorf("post start process failed: %s", err)
return
}
if n == 0 && isInstance {
// sleep a bit to see if child exit
time.Sleep(100 * time.Millisecond)
if os.Getppid() == ppid {
syscall.Kill(ppid, syscall.SIGUSR1)
}
}
}()
go func() {
var err error
// 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(fatal, status); err != nil {
sylog.Errorf("container cleanup failed: %s", err)
}
runtime.UnlockOSThread()
if !isInstance {
pgrp := syscall.Getpgrp()
tcpgrp := 0
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, 1, uintptr(syscall.TIOCGPGRP), uintptr(unsafe.Pointer(&tcpgrp))); err == 0 {
if tcpgrp > 0 && pgrp != tcpgrp {
signal.Ignore(syscall.SIGTTOU)
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, 1, uintptr(syscall.TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp))); err != 0 {
sylog.Errorf("failed to set crontrolling terminal group: %s", err.Error())
}
}
}
}
if fatal != nil {
if isInstance {
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 isInstance && 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 isInstance {
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())
}
}