@@ -18,6 +18,7 @@ import (
18
18
"github.com/opencontainers/runc/libcontainer"
19
19
"github.com/opencontainers/runc/libcontainer/configs"
20
20
"github.com/opencontainers/runc/libcontainer/specconv"
21
+ "github.com/opencontainers/runc/libcontainer/system"
21
22
"github.com/opencontainers/runc/libcontainer/system/kernelversion"
22
23
"github.com/opencontainers/runc/libcontainer/utils"
23
24
)
@@ -217,8 +218,9 @@ type runner struct {
217
218
}
218
219
219
220
func (r * runner ) run (config * specs.Process ) (_ int , retErr error ) {
221
+ detach := r .detach || (r .action == CT_ACT_CREATE )
220
222
defer func () {
221
- if retErr != nil {
223
+ if ! detach || retErr != nil {
222
224
r .destroy ()
223
225
}
224
226
}()
@@ -247,11 +249,19 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
247
249
}
248
250
process .ExtraFiles = append (process .ExtraFiles , os .NewFile (uintptr (i ), "PreserveFD:" + strconv .Itoa (i )))
249
251
}
250
- detach := r .detach || (r .action == CT_ACT_CREATE )
251
252
// Setting up IO is a two stage process. We need to modify process to deal
252
253
// with detaching containers, and then we get a tty after the container has
253
254
// started.
254
- handlerCh := newSignalHandler (r .enableSubreaper )
255
+ if r .enableSubreaper {
256
+ // set us as the subreaper before registering the signal handler for the container
257
+ if err := system .SetSubreaper (1 ); err != nil {
258
+ logrus .Warn (err )
259
+ }
260
+ }
261
+ var handlerCh chan * signalHandler
262
+ if ! detach {
263
+ handlerCh = newSignalHandler ()
264
+ }
255
265
tty , err := setupIO (process , r .container , config .Terminal , detach , r .consoleSocket )
256
266
if err != nil {
257
267
return - 1 , err
@@ -296,16 +306,14 @@ func (r *runner) run(config *specs.Process) (_ int, retErr error) {
296
306
return - 1 , err
297
307
}
298
308
}
299
- handler := <- handlerCh
300
- status , err := handler .forward (process , tty , detach )
301
- if err != nil {
302
- r .terminate (process )
303
- }
304
309
if detach {
305
310
return 0 , nil
306
311
}
307
- if err == nil {
308
- r .destroy ()
312
+ // For non-detached container, we should forward signals to the container.
313
+ var status int
314
+ handler := <- handlerCh
315
+ if status , err = handler .forward (process , tty ); err != nil {
316
+ r .terminate (process )
309
317
}
310
318
return status , err
311
319
}
0 commit comments