@@ -423,15 +423,6 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
423423 os .RemoveAll (logPath )
424424 }()
425425
426- f , err := ioutil .TempFile ("" , "exec-sync-process" )
427- if err != nil {
428- return nil , ExecSyncError {
429- ExitCode : - 1 ,
430- Err : err ,
431- }
432- }
433- defer os .RemoveAll (f .Name ())
434-
435426 var args []string
436427 args = append (args , "-c" , c .id )
437428 args = append (args , "-r" , r .Path (c ))
@@ -447,24 +438,16 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
447438 args = append (args , "-l" , logPath )
448439 args = append (args , "--socket-dir-path" , ContainerAttachSocketDir )
449440
450- pspec := c .Spec ().Process
451- pspec .Args = command
452- processJSON , err := json .Marshal (pspec )
441+ processFile , err := PrepareProcessExec (c , command , false )
453442 if err != nil {
454443 return nil , ExecSyncError {
455444 ExitCode : - 1 ,
456445 Err : err ,
457446 }
458447 }
448+ defer os .RemoveAll (processFile .Name ())
459449
460- if err := ioutil .WriteFile (f .Name (), processJSON , 0644 ); err != nil {
461- return nil , ExecSyncError {
462- ExitCode : - 1 ,
463- Err : err ,
464- }
465- }
466-
467- args = append (args , "--exec-process-spec" , f .Name ())
450+ args = append (args , "--exec-process-spec" , processFile .Name ())
468451
469452 cmd := exec .Command (r .conmonPath , args ... )
470453
@@ -772,3 +755,27 @@ func (r *Runtime) UnpauseContainer(c *Container) error {
772755 _ , err := utils .ExecCmd (r .Path (c ), "resume" , c .id )
773756 return err
774757}
758+
759+ // PrepareProcessExec returns the path of the process.json used in runc exec -p
760+ // caller is responsible to close the returned *os.File if needed.
761+ func PrepareProcessExec (c * Container , cmd []string , tty bool ) (* os.File , error ) {
762+ f , err := ioutil .TempFile ("" , "exec-process-" )
763+ if err != nil {
764+ return nil , err
765+ }
766+
767+ pspec := c .Spec ().Process
768+ pspec .Args = cmd
769+ if tty {
770+ pspec .Terminal = true
771+ }
772+ processJSON , err := json .Marshal (pspec )
773+ if err != nil {
774+ return nil , err
775+ }
776+
777+ if err := ioutil .WriteFile (f .Name (), processJSON , 0644 ); err != nil {
778+ return nil , err
779+ }
780+ return f , nil
781+ }
0 commit comments