Skip to content

Commit

Permalink
Merge pull request #889 from katiewasnothere/argon_execinhost
Browse files Browse the repository at this point in the history
Add new function to exec cmds in host for process isolated containers
  • Loading branch information
katiewasnothere authored Oct 30, 2020
2 parents 0af9b9d + 13f64b4 commit 6e6b6ce
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/containerd-shim-runhcs-v1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"time"

"github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
Expand All @@ -11,6 +12,8 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
)

var errTaskNotIsolated = errors.New("task is not isolated")

// shimTaskPidPair groups a process pid to its execID if it was user generated.
type shimTaskPidPair struct {
// Pid is the pid of the container process.
Expand Down
4 changes: 2 additions & 2 deletions cmd/containerd-shim-runhcs-v1/task_hcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func (ht *hcsTask) closeHost(ctx context.Context) {

func (ht *hcsTask) ExecInHost(ctx context.Context, req *shimdiag.ExecProcessRequest) (int, error) {
if ht.host == nil {
return 0, errors.New("task is not isolated")
return cmd.ExecInShimHost(ctx, req)
}
return cmd.ExecInUvm(ctx, ht.host, req)
}
Expand All @@ -613,7 +613,7 @@ func (ht *hcsTask) DumpGuestStacks(ctx context.Context) string {

func (ht *hcsTask) Share(ctx context.Context, req *shimdiag.ShareRequest) error {
if ht.host == nil {
return errors.New("task is not isolated")
return errTaskNotIsolated
}
// For hyper-v isolated WCOW the task used isn't the standard hcsTask so we
// only have to deal with the LCOW case here.
Expand Down
4 changes: 2 additions & 2 deletions cmd/containerd-shim-runhcs-v1/task_wcow_podsandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (wpst *wcowPodSandboxTask) waitParentExit() {

func (wpst *wcowPodSandboxTask) ExecInHost(ctx context.Context, req *shimdiag.ExecProcessRequest) (int, error) {
if wpst.host == nil {
return 0, errors.New("task is not isolated")
return 0, errTaskNotIsolated
}
return cmd.ExecInUvm(ctx, wpst.host, req)
}
Expand All @@ -241,7 +241,7 @@ func (wpst *wcowPodSandboxTask) DumpGuestStacks(ctx context.Context) string {

func (wpst *wcowPodSandboxTask) Share(ctx context.Context, req *shimdiag.ShareRequest) error {
if wpst.host == nil {
return errors.New("task is not isolated")
return errTaskNotIsolated
}
options := wpst.host.DefaultVSMBOptions(req.ReadOnly)
_, err := wpst.host.AddVSMB(ctx, req.HostPath, options)
Expand Down
24 changes: 24 additions & 0 deletions internal/cmd/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package cmd
import (
"context"
"errors"
"os/exec"

"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/shimdiag"
"github.com/Microsoft/hcsshim/internal/uvm"
errorspkg "github.com/pkg/errors"
)

// ExecInUvm is a helper function used to execute commands specified in `req` inside the given UVM.
func ExecInUvm(ctx context.Context, vm *uvm.UtilityVM, req *shimdiag.ExecProcessRequest) (int, error) {
if len(req.Args) == 0 {
return 0, errors.New("missing command")
Expand All @@ -34,3 +37,24 @@ func ExecInUvm(ctx context.Context, vm *uvm.UtilityVM, req *shimdiag.ExecProcess
err = cmd.Run()
return cmd.ExitState.ExitCode(), err
}

// ExecInShimHost is a helper function used to execute commands specified in `req` in the shim's
// hosting system.
func ExecInShimHost(ctx context.Context, req *shimdiag.ExecProcessRequest) (int, error) {
if len(req.Args) == 0 {
return 0, errors.New("missing command")
}
cmdArgsWithoutName := []string{""}
if len(req.Args) > 1 {
cmdArgsWithoutName = req.Args[1:]
}
cmd := exec.Command(req.Args[0], cmdArgsWithoutName...)
output, err := cmd.CombinedOutput()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
return exiterr.ExitCode(), errorspkg.Wrapf(exiterr, "command output: %v", string(output))
}
return -1, errorspkg.Wrapf(err, "command output: %v", string(output))
}
return 0, nil
}

0 comments on commit 6e6b6ce

Please sign in to comment.