Skip to content

Commit 9d60019

Browse files
authored
Merge pull request #4271 from kolyshkin/two-inits
libct.Start: fix locking, do not allow a second container init
2 parents 349e5ab + 42cea2e commit 9d60019

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

libcontainer/container_linux.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,28 +204,16 @@ func (c *Container) Set(config configs.Config) error {
204204
func (c *Container) Start(process *Process) error {
205205
c.m.Lock()
206206
defer c.m.Unlock()
207-
if c.config.Cgroups.Resources.SkipDevices {
208-
return errors.New("can't start container with SkipDevices set")
209-
}
210-
if process.Init {
211-
if err := c.createExecFifo(); err != nil {
212-
return err
213-
}
214-
}
215-
if err := c.start(process); err != nil {
216-
if process.Init {
217-
c.deleteExecFifo()
218-
}
219-
return err
220-
}
221-
return nil
207+
return c.start(process)
222208
}
223209

224210
// Run immediately starts the process inside the container. Returns an error if
225211
// the process fails to start. It does not block waiting for the exec fifo
226212
// after start returns but opens the fifo after start returns.
227213
func (c *Container) Run(process *Process) error {
228-
if err := c.Start(process); err != nil {
214+
c.m.Lock()
215+
defer c.m.Unlock()
216+
if err := c.start(process); err != nil {
229217
return err
230218
}
231219
if process.Init {
@@ -314,6 +302,23 @@ type openResult struct {
314302
}
315303

316304
func (c *Container) start(process *Process) (retErr error) {
305+
if c.config.Cgroups.Resources.SkipDevices {
306+
return errors.New("can't start container with SkipDevices set")
307+
}
308+
if process.Init {
309+
if c.initProcessStartTime != 0 {
310+
return errors.New("container already has init process")
311+
}
312+
if err := c.createExecFifo(); err != nil {
313+
return err
314+
}
315+
defer func() {
316+
if retErr != nil {
317+
c.deleteExecFifo()
318+
}
319+
}()
320+
}
321+
317322
parent, err := c.newParentProcess(process)
318323
if err != nil {
319324
return fmt.Errorf("unable to create new parent process: %w", err)
@@ -417,9 +422,6 @@ func (c *Container) createExecFifo() error {
417422
}
418423

419424
fifoName := filepath.Join(c.stateDir, execFifoFilename)
420-
if _, err := os.Stat(fifoName); err == nil {
421-
return fmt.Errorf("exec fifo %s already exists", fifoName)
422-
}
423425
if err := unix.Mkfifo(fifoName, 0o622); err != nil {
424426
return &os.PathError{Op: "mkfifo", Path: fifoName, Err: err}
425427
}

libcontainer/integration/execin_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ func testExecInRlimit(t *testing.T, userns bool) {
115115
// increase process rlimit higher than container rlimit to test per-process limit
116116
{Type: unix.RLIMIT_NOFILE, Hard: 1026, Soft: 1026},
117117
},
118-
Init: true,
119118
}
120119
err = container.Run(ps)
121120
ok(t, err)
@@ -359,7 +358,6 @@ func TestExecInEnvironment(t *testing.T) {
359358
Stdin: buffers.Stdin,
360359
Stdout: buffers.Stdout,
361360
Stderr: buffers.Stderr,
362-
Init: true,
363361
}
364362
err = container.Run(process2)
365363
ok(t, err)

0 commit comments

Comments
 (0)