/
stage_linux.go
73 lines (63 loc) · 2.44 KB
/
stage_linux.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
// Copyright (c) 2018-2020, 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 (
"context"
"net"
"os"
"github.com/sylabs/singularity/v4/internal/pkg/runtime/engine"
starterConfig "github.com/sylabs/singularity/v4/internal/pkg/runtime/engine/config/starter"
"github.com/sylabs/singularity/v4/pkg/sylog"
)
// StageOne validates and prepares container configuration which is
// used during container creation. Updated (possibly) engine configuration
// is wrote back into a shared sconfig so that new values will appear
// in next stages of engine execution and in master process.
//
// Any privileges gained from SUID flow or capabilities in
// extended attributes are already dropped by this moment.
func StageOne(sconfig *starterConfig.Config, e *engine.Engine) {
sylog.Debugf("Entering stage 1\n")
if err := e.PrepareConfig(sconfig); err != nil {
if cleanErr := e.CleanupHost(context.TODO()); cleanErr != nil {
sylog.Errorf("While running host cleanup tasks: %s", cleanErr)
}
sylog.Fatalf("%s\n", err)
}
if err := sconfig.Write(e.Common); err != nil {
if cleanErr := e.CleanupHost(context.TODO()); cleanErr != nil {
sylog.Errorf("While running host cleanup tasks: %s", cleanErr)
}
sylog.Fatalf("%s", err)
}
os.Exit(0)
}
// StageTwo performs container execution.
func StageTwo(masterSocket int, e *engine.Engine) {
sylog.Debugf("Entering stage 2\n")
// master socket allows communications between
// stage 2 and master process, typically used for
// synchronization or for sending state
comm := os.NewFile(uintptr(masterSocket), "master-socket")
conn, err := net.FileConn(comm)
comm.Close()
if err != nil {
sylog.Fatalf("failed to copy master unix socket descriptor: %s", err)
return
}
// call engine operation StartProcess, at this stage
// we are in a container context, chroot was done.
// The privileges are those applied by the container
// configuration, in the case of Singularity engine
// and if run as a user, there is no privileges set
if err := e.StartProcess(conn); err != nil {
// write data to just tell master to not execute PostStartProcess
// in case of failure
if _, err := conn.Write([]byte("f")); err != nil {
sylog.Errorf("fail to send data to master: %s", err)
}
sylog.Fatalf("%s\n", err)
}
}