-
Notifications
You must be signed in to change notification settings - Fork 425
/
cleanup.go
72 lines (57 loc) · 1.56 KB
/
cleanup.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
// 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"
"github.com/sylabs/singularity/internal/pkg/util/mainthread"
"github.com/sylabs/singularity/internal/pkg/instance"
"github.com/sylabs/singularity/internal/pkg/sylog"
)
/*
* see https://github.com/opencontainers/runtime-spec/blob/master/runtime.md#lifecycle
* we will run step 8/9 there
*/
// CleanupContainer cleans up the container
func (engine *EngineOperations) CleanupContainer() error {
sylog.Debugf("Cleanup container")
if engine.EngineConfig.Network != nil {
if err := engine.EngineConfig.Network.DelNetworks(); err != nil {
return err
}
}
if engine.EngineConfig.Cgroups != nil {
if err := engine.EngineConfig.Cgroups.Remove(); err != nil {
sylog.Errorf("%s", err)
}
}
if engine.EngineConfig.GetInstance() {
uid := os.Getuid()
file, err := instance.Get(engine.CommonConfig.ContainerID)
if err != nil {
return err
}
if file.PPid != os.Getpid() {
return nil
}
if file.Privileged {
var err error
mainthread.Execute(func() {
if err := syscall.Setresuid(0, 0, uid); err != nil {
err = fmt.Errorf("failed to escalate privileges")
return
}
defer syscall.Setresuid(uid, uid, 0)
if err = file.Delete(); err != nil {
return
}
})
return err
}
return file.Delete()
}
return nil
}