Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill grandchildren spun up by child processes #258

Merged
merged 2 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
out/
.idea
3 changes: 3 additions & 0 deletions integration/dockerfiles/Dockerfile_test_daemons
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM busybox
RUN while true; do dd if=/dev/zero of=file`date +%s`.txt count=16000 bs=256 > /dev/null 2>&1; done &
RUN echo "wait a second..." && sleep 2 && ls -lrat file*.txt || echo "test passed."
24 changes: 22 additions & 2 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -88,10 +89,29 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
}
gid = uint32(gid64)
}
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uid, Gid: gid}
}
return cmd.Run()

if err := cmd.Start(); err != nil {
return errors.Wrap(err, "starting command")
}

pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err != nil {
return errors.Wrap(err, "getting group id for process")
}

if err := cmd.Wait(); err != nil {
return errors.Wrap(err, "waiting for process to exit")
}

//it's not an error if there are no grandchildren
if err := syscall.Kill(-pgid, syscall.SIGKILL); err != nil && err.Error() != "no such process" {
return err
}

return nil
}

// FilesToSnapshot returns nil for this command because we don't know which files
Expand Down