Skip to content

Commit e3e1072

Browse files
committed
libct: fix locking in Start/Run/Exec
1. The code to call c.exec from c.Run was initially added by commit 3aacff6. At the time, there was a lock in c.Run. That lock was removed by commit bd3c4f8, which resulted in part of c.Run executing without the lock. 2. All the Start/Run/Exec calls were a mere wrappers for start/run/exec adding a lock, but some more code crept into Start at some point, e.g. by commits 805b8c7 and 108ee85. Since the reason mentioned in commit 805b8c7 is no longer true after refactoring, we can fix this. Fix both issues by moving code out of wrappers, and adding locking into c.Run. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 304a4c0 commit e3e1072

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

libcontainer/container_linux.go

Lines changed: 18 additions & 16 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,20 @@ 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 err := c.createExecFifo(); err != nil {
310+
return err
311+
}
312+
defer func() {
313+
if retErr != nil {
314+
c.deleteExecFifo()
315+
}
316+
}()
317+
}
318+
317319
parent, err := c.newParentProcess(process)
318320
if err != nil {
319321
return fmt.Errorf("unable to create new parent process: %w", err)

0 commit comments

Comments
 (0)