Skip to content

Commit 9ceed31

Browse files
author
Dongsu Park
committed
libcontainer: call Prestart, Poststart hooks from correct places
So far Prestart, Poststart hooks have been called from the context of create, which does not satisfy the runtime spec. That's why the runtime-tools validation tests like `hooks_stdin.t` have failed. Let's move call sites of Prestart, Poststart to correct places. Unfortunately as for the Poststart hook, it's not possible to tell whether a specific call site is from create context or run context. That's why we needed to allow Create and Run methods to accept another parameter `action` (of type `CtAct`). Doing that, it's possible to set a variable `initProcess.IsCreate` that allows us distinguish Create from Run. It depends on a pending PR #1741. See also #1710. Signed-off-by: Dongsu Park <[email protected]>
1 parent d430318 commit 9ceed31

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

libcontainer/container_linux.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,25 @@ func (c *linuxContainer) Exec() error {
272272
return c.exec()
273273
}
274274

275-
func (c *linuxContainer) exec() error {
275+
func (c *linuxContainer) exec() (errExec error) {
276276
path := filepath.Join(c.root, execFifoFilename)
277277

278+
if c.config.Hooks != nil && len(c.config.Hooks.Poststart) > 0 {
279+
defer func() {
280+
s, err := c.currentOCIState()
281+
if err != nil {
282+
errExec = err
283+
return
284+
}
285+
for i, hook := range c.config.Hooks.Poststart {
286+
if err := hook.Run(s); err != nil {
287+
errExec = newSystemErrorWithCausef(err, "running poststart hook %d", i)
288+
return
289+
}
290+
}
291+
}()
292+
}
293+
278294
fifoOpen := make(chan struct{})
279295
select {
280296
case <-awaitProcessExit(c.initProcess.pid(), fifoOpen):
@@ -365,17 +381,14 @@ func (c *linuxContainer) start(process *Process, isInit, isCreate bool) error {
365381
}
366382
c.initProcessStartTime = state.InitProcessStartTime
367383

368-
if c.config.Hooks != nil && len(c.config.Hooks.Poststart) > 0 {
384+
if c.config.Hooks != nil && len(c.config.Hooks.Prestart) > 0 {
369385
s, err := c.currentOCIState()
370386
if err != nil {
371387
return err
372388
}
373-
for i, hook := range c.config.Hooks.Poststart {
389+
for i, hook := range c.config.Hooks.Prestart {
374390
if err := hook.Run(s); err != nil {
375-
if err := ignoreTerminateErrors(parent.terminate()); err != nil {
376-
logrus.Warn(err)
377-
}
378-
return newSystemErrorWithCausef(err, "running poststart hook %d", i)
391+
return newSystemErrorWithCausef(err, "running prestart hook %d", i)
379392
}
380393
}
381394
}

libcontainer/process_linux.go

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,21 @@ func (p *initProcess) start() error {
321321
sentResume bool
322322
)
323323

324+
// runc start
325+
if !p.IsCreate {
326+
if p.config.Config.Hooks != nil && len(p.config.Config.Hooks.Poststart) > 0 {
327+
s, err := p.container.currentOCIState()
328+
if err != nil {
329+
return err
330+
}
331+
for i, hook := range p.config.Config.Hooks.Poststart {
332+
if err := hook.Run(s); err != nil {
333+
return newSystemErrorWithCausef(err, "running poststart hook %d", i)
334+
}
335+
}
336+
}
337+
}
338+
324339
ierr := parseSync(p.parentPipe, func(sync *syncT) error {
325340
switch sync.Type {
326341
case procReady:
@@ -340,21 +355,6 @@ func (p *initProcess) start() error {
340355
return newSystemErrorWithCause(err, "setting Intel RDT config for ready process")
341356
}
342357
}
343-
344-
if p.config.Config.Hooks != nil && len(p.config.Config.Hooks.Prestart) > 0 {
345-
s, err := p.container.currentOCIState()
346-
if err != nil {
347-
return err
348-
}
349-
// initProcessStartTime hasn't been set yet.
350-
s.Pid = p.cmd.Process.Pid
351-
s.Status = "creating"
352-
for i, hook := range p.config.Config.Hooks.Prestart {
353-
if err := hook.Run(s); err != nil {
354-
return newSystemErrorWithCausef(err, "running prestart hook %d", i)
355-
}
356-
}
357-
}
358358
}
359359
// Sync with child.
360360
if err := writeSync(p.parentPipe, procRun); err != nil {
@@ -371,20 +371,6 @@ func (p *initProcess) start() error {
371371
return newSystemErrorWithCause(err, "setting Intel RDT config for procHooks process")
372372
}
373373
}
374-
if p.config.Config.Hooks != nil && len(p.config.Config.Hooks.Prestart) > 0 {
375-
s, err := p.container.currentOCIState()
376-
if err != nil {
377-
return err
378-
}
379-
// initProcessStartTime hasn't been set yet.
380-
s.Pid = p.cmd.Process.Pid
381-
s.Status = "creating"
382-
for i, hook := range p.config.Config.Hooks.Prestart {
383-
if err := hook.Run(s); err != nil {
384-
return newSystemErrorWithCausef(err, "running prestart hook %d", i)
385-
}
386-
}
387-
}
388374
// Sync with child.
389375
if err := writeSync(p.parentPipe, procResume); err != nil {
390376
return newSystemErrorWithCause(err, "writing syncT 'resume'")

0 commit comments

Comments
 (0)