Skip to content

Commit 72657ea

Browse files
committed
libct: move StartInitialization
No code change, just moving a function from factory_linux.go to init_linux.go. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 8cbf640 commit 72657ea

File tree

2 files changed

+86
-87
lines changed

2 files changed

+86
-87
lines changed

libcontainer/factory_linux.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"os"
8-
"runtime/debug"
9-
"strconv"
108

119
securejoin "github.com/cyphar/filepath-securejoin"
1210
"golang.org/x/sys/unix"
@@ -18,7 +16,6 @@ import (
1816
"github.com/opencontainers/runc/libcontainer/configs/validate"
1917
"github.com/opencontainers/runc/libcontainer/intelrdt"
2018
"github.com/opencontainers/runc/libcontainer/utils"
21-
"github.com/sirupsen/logrus"
2219
)
2320

2421
const (
@@ -151,90 +148,6 @@ func Load(root, id string) (*Container, error) {
151148
return c, nil
152149
}
153150

154-
// StartInitialization loads a container by opening the pipe fd from the parent
155-
// to read the configuration and state. This is a low level implementation
156-
// detail of the reexec and should not be consumed externally.
157-
func StartInitialization() (err error) {
158-
// Get the INITPIPE.
159-
envInitPipe := os.Getenv("_LIBCONTAINER_INITPIPE")
160-
pipefd, err := strconv.Atoi(envInitPipe)
161-
if err != nil {
162-
err = fmt.Errorf("unable to convert _LIBCONTAINER_INITPIPE: %w", err)
163-
logrus.Error(err)
164-
return err
165-
}
166-
pipe := os.NewFile(uintptr(pipefd), "pipe")
167-
defer pipe.Close()
168-
169-
defer func() {
170-
// We have an error during the initialization of the container's init,
171-
// send it back to the parent process in the form of an initError.
172-
if werr := writeSync(pipe, procError); werr != nil {
173-
fmt.Fprintln(os.Stderr, err)
174-
return
175-
}
176-
if werr := utils.WriteJSON(pipe, &initError{Message: err.Error()}); werr != nil {
177-
fmt.Fprintln(os.Stderr, err)
178-
return
179-
}
180-
}()
181-
182-
// Only init processes have FIFOFD.
183-
fifofd := -1
184-
envInitType := os.Getenv("_LIBCONTAINER_INITTYPE")
185-
it := initType(envInitType)
186-
if it == initStandard {
187-
envFifoFd := os.Getenv("_LIBCONTAINER_FIFOFD")
188-
if fifofd, err = strconv.Atoi(envFifoFd); err != nil {
189-
return fmt.Errorf("unable to convert _LIBCONTAINER_FIFOFD: %w", err)
190-
}
191-
}
192-
193-
var consoleSocket *os.File
194-
if envConsole := os.Getenv("_LIBCONTAINER_CONSOLE"); envConsole != "" {
195-
console, err := strconv.Atoi(envConsole)
196-
if err != nil {
197-
return fmt.Errorf("unable to convert _LIBCONTAINER_CONSOLE: %w", err)
198-
}
199-
consoleSocket = os.NewFile(uintptr(console), "console-socket")
200-
defer consoleSocket.Close()
201-
}
202-
203-
logPipeFdStr := os.Getenv("_LIBCONTAINER_LOGPIPE")
204-
logPipeFd, err := strconv.Atoi(logPipeFdStr)
205-
if err != nil {
206-
return fmt.Errorf("unable to convert _LIBCONTAINER_LOGPIPE: %w", err)
207-
}
208-
209-
// Get mount files (O_PATH).
210-
mountFds, err := parseMountFds()
211-
if err != nil {
212-
return err
213-
}
214-
215-
// clear the current process's environment to clean any libcontainer
216-
// specific env vars.
217-
os.Clearenv()
218-
219-
defer func() {
220-
if e := recover(); e != nil {
221-
if ee, ok := e.(error); ok {
222-
err = fmt.Errorf("panic from initialization: %w, %s", ee, debug.Stack())
223-
} else {
224-
err = fmt.Errorf("panic from initialization: %v, %s", e, debug.Stack())
225-
}
226-
}
227-
}()
228-
229-
i, err := newContainerInit(it, pipe, consoleSocket, fifofd, logPipeFd, mountFds)
230-
if err != nil {
231-
return err
232-
}
233-
234-
// If Init succeeds, syscall.Exec will not return, hence none of the defers will be called.
235-
return i.Init()
236-
}
237-
238151
func loadState(root string) (*State, error) {
239152
stateFilePath, err := securejoin.SecureJoin(root, stateFilename)
240153
if err != nil {

libcontainer/init_linux.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io"
99
"net"
1010
"os"
11+
"runtime/debug"
12+
"strconv"
1113
"strings"
1214
"unsafe"
1315

@@ -71,6 +73,90 @@ type initConfig struct {
7173
Cgroup2Path string `json:"cgroup2_path,omitempty"`
7274
}
7375

76+
// StartInitialization loads a container by opening the pipe fd from the parent
77+
// to read the configuration and state. This is a low level implementation
78+
// detail of the reexec and should not be consumed externally.
79+
func StartInitialization() (err error) {
80+
// Get the INITPIPE.
81+
envInitPipe := os.Getenv("_LIBCONTAINER_INITPIPE")
82+
pipefd, err := strconv.Atoi(envInitPipe)
83+
if err != nil {
84+
err = fmt.Errorf("unable to convert _LIBCONTAINER_INITPIPE: %w", err)
85+
logrus.Error(err)
86+
return err
87+
}
88+
pipe := os.NewFile(uintptr(pipefd), "pipe")
89+
defer pipe.Close()
90+
91+
defer func() {
92+
// We have an error during the initialization of the container's init,
93+
// send it back to the parent process in the form of an initError.
94+
if werr := writeSync(pipe, procError); werr != nil {
95+
fmt.Fprintln(os.Stderr, err)
96+
return
97+
}
98+
if werr := utils.WriteJSON(pipe, &initError{Message: err.Error()}); werr != nil {
99+
fmt.Fprintln(os.Stderr, err)
100+
return
101+
}
102+
}()
103+
104+
// Only init processes have FIFOFD.
105+
fifofd := -1
106+
envInitType := os.Getenv("_LIBCONTAINER_INITTYPE")
107+
it := initType(envInitType)
108+
if it == initStandard {
109+
envFifoFd := os.Getenv("_LIBCONTAINER_FIFOFD")
110+
if fifofd, err = strconv.Atoi(envFifoFd); err != nil {
111+
return fmt.Errorf("unable to convert _LIBCONTAINER_FIFOFD: %w", err)
112+
}
113+
}
114+
115+
var consoleSocket *os.File
116+
if envConsole := os.Getenv("_LIBCONTAINER_CONSOLE"); envConsole != "" {
117+
console, err := strconv.Atoi(envConsole)
118+
if err != nil {
119+
return fmt.Errorf("unable to convert _LIBCONTAINER_CONSOLE: %w", err)
120+
}
121+
consoleSocket = os.NewFile(uintptr(console), "console-socket")
122+
defer consoleSocket.Close()
123+
}
124+
125+
logPipeFdStr := os.Getenv("_LIBCONTAINER_LOGPIPE")
126+
logPipeFd, err := strconv.Atoi(logPipeFdStr)
127+
if err != nil {
128+
return fmt.Errorf("unable to convert _LIBCONTAINER_LOGPIPE: %w", err)
129+
}
130+
131+
// Get mount files (O_PATH).
132+
mountFds, err := parseMountFds()
133+
if err != nil {
134+
return err
135+
}
136+
137+
// clear the current process's environment to clean any libcontainer
138+
// specific env vars.
139+
os.Clearenv()
140+
141+
defer func() {
142+
if e := recover(); e != nil {
143+
if ee, ok := e.(error); ok {
144+
err = fmt.Errorf("panic from initialization: %w, %s", ee, debug.Stack())
145+
} else {
146+
err = fmt.Errorf("panic from initialization: %v, %s", e, debug.Stack())
147+
}
148+
}
149+
}()
150+
151+
i, err := newContainerInit(it, pipe, consoleSocket, fifofd, logPipeFd, mountFds)
152+
if err != nil {
153+
return err
154+
}
155+
156+
// If Init succeeds, syscall.Exec will not return, hence none of the defers will be called.
157+
return i.Init()
158+
}
159+
74160
type initer interface {
75161
Init() error
76162
}

0 commit comments

Comments
 (0)