@@ -415,15 +415,6 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
415415 os .RemoveAll (logPath )
416416 }()
417417
418- f , err := ioutil .TempFile ("" , "exec-sync-process" )
419- if err != nil {
420- return nil , ExecSyncError {
421- ExitCode : - 1 ,
422- Err : err ,
423- }
424- }
425- defer os .RemoveAll (f .Name ())
426-
427418 var args []string
428419 args = append (args , "-c" , c .id )
429420 args = append (args , "-r" , r .Path (c ))
@@ -439,24 +430,16 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
439430 args = append (args , "-l" , logPath )
440431 args = append (args , "--socket-dir-path" , ContainerAttachSocketDir )
441432
442- pspec := c .Spec ().Process
443- pspec .Args = command
444- processJSON , err := json .Marshal (pspec )
433+ processFile , err := PrepareProcessExec (c , command , false )
445434 if err != nil {
446435 return nil , ExecSyncError {
447436 ExitCode : - 1 ,
448437 Err : err ,
449438 }
450439 }
440+ defer os .RemoveAll (processFile .Name ())
451441
452- if err := ioutil .WriteFile (f .Name (), processJSON , 0644 ); err != nil {
453- return nil , ExecSyncError {
454- ExitCode : - 1 ,
455- Err : err ,
456- }
457- }
458-
459- args = append (args , "--exec-process-spec" , f .Name ())
442+ args = append (args , "--exec-process-spec" , processFile .Name ())
460443
461444 cmd := exec .Command (r .conmonPath , args ... )
462445
@@ -764,3 +747,27 @@ func (r *Runtime) UnpauseContainer(c *Container) error {
764747 _ , err := utils .ExecCmd (r .Path (c ), "resume" , c .id )
765748 return err
766749}
750+
751+ // PrepareProcessExec returns the path of the process.json used in runc exec -p
752+ // caller is responsible to close the returned *os.File if needed.
753+ func PrepareProcessExec (c * Container , cmd []string , tty bool ) (* os.File , error ) {
754+ f , err := ioutil .TempFile ("" , "exec-process-" )
755+ if err != nil {
756+ return nil , err
757+ }
758+
759+ pspec := c .Spec ().Process
760+ pspec .Args = cmd
761+ if tty {
762+ pspec .Terminal = true
763+ }
764+ processJSON , err := json .Marshal (pspec )
765+ if err != nil {
766+ return nil , err
767+ }
768+
769+ if err := ioutil .WriteFile (f .Name (), processJSON , 0644 ); err != nil {
770+ return nil , err
771+ }
772+ return f , nil
773+ }
0 commit comments