@@ -10,6 +10,7 @@ import (
1010 "os/exec"
1111 "path/filepath"
1212 "strconv"
13+ "strings"
1314 "syscall"
1415 "time"
1516
@@ -275,9 +276,7 @@ func (r *runtimeOCI) StartContainer(ctx context.Context, c *Container) error {
275276 return nil
276277 }
277278
278- if _ , err := utils .ExecCmd (
279- r .handler .RuntimePath , rootFlag , r .root , "start" , c .ID (),
280- ); err != nil {
279+ if _ , err := r .runtimeCmd ("start" , c .ID ()); err != nil {
281280 return err
282281 }
283282 c .state .Started = time .Now ()
@@ -359,8 +358,8 @@ func (r *runtimeOCI) ExecContainer(ctx context.Context, c *Container, cmd []stri
359358 }
360359 defer os .RemoveAll (processFile )
361360
362- args := [] string { rootFlag , r . root , "exec" }
363- args = append (args , "--process" , processFile , c .ID ())
361+ args := r . defaultRuntimeArgs ()
362+ args = append (args , "exec" , " --process" , processFile , c .ID ())
364363 execCmd := cmdrunner .Command (r .handler .RuntimePath , args ... ) // nolint: gosec
365364 if v , found := os .LookupEnv ("XDG_RUNTIME_DIR" ); found {
366365 execCmd .Env = append (execCmd .Env , fmt .Sprintf ("XDG_RUNTIME_DIR=%s" , v ))
@@ -494,6 +493,9 @@ func (r *runtimeOCI) ExecSyncContainer(ctx context.Context, c *Container, comman
494493 if timeout > 0 {
495494 args = append (args , "-T" , fmt .Sprintf ("%d" , timeout ))
496495 }
496+ if r .config .CgroupManager ().IsSystemd () {
497+ args = append (args , "-s" )
498+ }
497499
498500 processFile , err := prepareProcessExec (c , command , c .terminal )
499501 if err != nil {
@@ -839,9 +841,7 @@ func (r *runtimeOCI) StopContainer(ctx context.Context, c *Container, timeout in
839841 }
840842
841843 if timeout > 0 {
842- if _ , err := utils .ExecCmd (
843- r .handler .RuntimePath , rootFlag , r .root , "kill" , c .ID (), c .GetStopSignal (),
844- ); err != nil {
844+ if _ , err := r .runtimeCmd ("kill" , c .ID (), c .GetStopSignal ()); err != nil {
845845 checkProcessGone (c )
846846 }
847847 err := WaitContainerStop (ctx , c , time .Duration (timeout )* time .Second , true )
@@ -851,9 +851,7 @@ func (r *runtimeOCI) StopContainer(ctx context.Context, c *Container, timeout in
851851 log .Warnf (ctx , "Stopping container %v with stop signal timed out: %v" , c .ID (), err )
852852 }
853853
854- if _ , err := utils .ExecCmd (
855- r .handler .RuntimePath , rootFlag , r .root , "kill" , c .ID (), "KILL" ,
856- ); err != nil {
854+ if _ , err := r .runtimeCmd ("kill" , c .ID (), "KILL" ); err != nil {
857855 checkProcessGone (c )
858856 }
859857
@@ -877,7 +875,7 @@ func (r *runtimeOCI) DeleteContainer(ctx context.Context, c *Container) error {
877875 return nil
878876 }
879877
880- _ , err := utils . ExecCmd ( r . handler . RuntimePath , rootFlag , r . root , "delete" , "--force" , c .ID ())
878+ _ , err := r . runtimeCmd ( "delete" , "--force" , c .ID ())
881879 return err
882880}
883881
@@ -918,11 +916,7 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er
918916 }
919917
920918 stateCmd := func () (* ContainerState , bool , error ) {
921- cmd := cmdrunner .Command (r .handler .RuntimePath , rootFlag , r .root , "state" , c .ID ()) // nolint: gosec
922- if v , found := os .LookupEnv ("XDG_RUNTIME_DIR" ); found {
923- cmd .Env = append (cmd .Env , fmt .Sprintf ("XDG_RUNTIME_DIR=%s" , v ))
924- }
925- out , err := cmd .Output ()
919+ out , err := r .runtimeCmd ("state" , c .ID ())
926920 if err != nil {
927921 // there are many code paths that could lead to have a bad state in the
928922 // underlying runtime.
@@ -943,7 +937,7 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er
943937 return nil , true , nil
944938 }
945939 state := * c .state
946- if err := json .NewDecoder (bytes . NewBuffer (out )).Decode (& state ); err != nil {
940+ if err := json .NewDecoder (strings . NewReader (out )).Decode (& state ); err != nil {
947941 return & state , false , fmt .Errorf ("failed to decode container status for %s: %s" , c .ID (), err )
948942 }
949943 return & state , false , nil
@@ -1024,7 +1018,7 @@ func (r *runtimeOCI) PauseContainer(ctx context.Context, c *Container) error {
10241018 return nil
10251019 }
10261020
1027- _ , err := utils . ExecCmd ( r . handler . RuntimePath , rootFlag , r . root , "pause" , c .ID ())
1021+ _ , err := r . runtimeCmd ( "pause" , c .ID ())
10281022 return err
10291023}
10301024
@@ -1037,7 +1031,7 @@ func (r *runtimeOCI) UnpauseContainer(ctx context.Context, c *Container) error {
10371031 return nil
10381032 }
10391033
1040- _ , err := utils . ExecCmd ( r . handler . RuntimePath , rootFlag , r . root , "resume" , c .ID ())
1034+ _ , err := r . runtimeCmd ( "resume" , c .ID ())
10411035 return err
10421036}
10431037
@@ -1066,17 +1060,13 @@ func (r *runtimeOCI) SignalContainer(ctx context.Context, c *Container, sig sysc
10661060
10671061func (r * runtimeOCI ) signalContainer (c * Container , sig syscall.Signal , all bool ) error {
10681062 args := []string {
1069- rootFlag ,
1070- r .root ,
10711063 "kill" ,
10721064 }
10731065 if all {
10741066 args = append (args , "-a" )
10751067 }
10761068 args = append (args , c .ID (), strconv .Itoa (int (sig )))
1077- _ , err := utils .ExecCmd (
1078- r .handler .RuntimePath , args ... ,
1079- )
1069+ _ , err := r .runtimeCmd (args ... )
10801070 return err
10811071}
10821072
@@ -1357,3 +1347,32 @@ func prepareProcessExec(c *Container, cmd []string, tty bool) (processFile strin
13571347func (c * Container ) conmonPidFilePath () string {
13581348 return filepath .Join (c .bundlePath , "conmon-pidfile" )
13591349}
1350+
1351+ // runtimeCmd executes a command with args and returns its output as a string along
1352+ // with an error, if any
1353+ func (r * runtimeOCI ) runtimeCmd (args ... string ) (string , error ) {
1354+ runtimeArgs := append (r .defaultRuntimeArgs (), args ... )
1355+ cmd := cmdrunner .Command (r .handler .RuntimePath , runtimeArgs ... )
1356+ var stdout bytes.Buffer
1357+ var stderr bytes.Buffer
1358+ cmd .Stdout = & stdout
1359+ cmd .Stderr = & stderr
1360+ if v , found := os .LookupEnv ("XDG_RUNTIME_DIR" ); found {
1361+ cmd .Env = append (cmd .Env , fmt .Sprintf ("XDG_RUNTIME_DIR=%s" , v ))
1362+ }
1363+
1364+ err := cmd .Run ()
1365+ if err != nil {
1366+ return "" , fmt .Errorf ("`%v %v` failed: %v %v: %w" , r .handler .RuntimePath , strings .Join (runtimeArgs , " " ), stderr .String (), stdout .String (), err )
1367+ }
1368+
1369+ return stdout .String (), nil
1370+ }
1371+
1372+ func (r * runtimeOCI ) defaultRuntimeArgs () []string {
1373+ args := []string {rootFlag , r .root }
1374+ if r .config .CgroupManager ().IsSystemd () {
1375+ args = append (args , "--systemd-cgroup" )
1376+ }
1377+ return args
1378+ }
0 commit comments