Skip to content

Commit 0c199c4

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 aedc95e commit 0c199c4

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

libcontainer/container_linux.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ func (c *linuxContainer) Exec() error {
276276
func (c *linuxContainer) exec() error {
277277
path := filepath.Join(c.root, execFifoFilename)
278278

279+
defer func() {
280+
if c.config.Hooks != nil && len(c.config.Hooks.Poststart) > 0 {
281+
s, err := c.currentOCIState()
282+
if err != nil {
283+
return
284+
}
285+
for i, hook := range c.config.Hooks.Poststart {
286+
if err := hook.Run(s); err != nil {
287+
fmt.Printf("%v", newSystemErrorWithCausef(err, "running poststart hook %d", i))
288+
return
289+
}
290+
}
291+
}
292+
}()
293+
279294
fifoOpen := make(chan struct{})
280295
select {
281296
case <-awaitProcessExit(c.initProcess.pid(), fifoOpen):
@@ -366,17 +381,15 @@ func (c *linuxContainer) start(process *Process, isInit, isCreate bool) error {
366381
}
367382
c.initProcessStartTime = state.InitProcessStartTime
368383

369-
if c.config.Hooks != nil && len(c.config.Hooks.Poststart) > 0 {
384+
if c.config.Hooks != nil && len(c.config.Hooks.Prestart) > 0 {
370385
s, err := c.currentOCIState()
371386
if err != nil {
372387
return err
373388
}
374-
for i, hook := range c.config.Hooks.Poststart {
389+
s.Status = "created"
390+
for i, hook := range c.config.Hooks.Prestart {
375391
if err := hook.Run(s); err != nil {
376-
if err := ignoreTerminateErrors(parent.terminate()); err != nil {
377-
logrus.Warn(err)
378-
}
379-
return newSystemErrorWithCausef(err, "running poststart hook %d", i)
392+
return newSystemErrorWithCausef(err, "running prestart hook %d", i)
380393
}
381394
}
382395
}

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)