Skip to content

Commit

Permalink
Merge pull request #41 from masters-of-cats/master
Browse files Browse the repository at this point in the history
Use user-specific temp directory if set
  • Loading branch information
estesp committed May 11, 2018
2 parents f271fa2 + 07e192d commit 301f7c1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
8 changes: 7 additions & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func NewConsoleSocket(path string) (*Socket, error) {
// NewTempConsoleSocket returns a temp console socket for use with a container
// On Close(), the socket is deleted
func NewTempConsoleSocket() (*Socket, error) {
dir, err := ioutil.TempDir("", "pty")
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
dir, err := ioutil.TempDir(runtimeDir, "pty")
if err != nil {
return nil, err
}
Expand All @@ -66,6 +67,11 @@ func NewTempConsoleSocket() (*Socket, error) {
if err != nil {
return nil, err
}
if runtimeDir != "" {
if err := os.Chmod(abs, 0755|os.ModeSticky); err != nil {
return nil, err
}
}
return &Socket{
l: l,
rmdir: true,
Expand Down
34 changes: 33 additions & 1 deletion console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,51 @@
package runc

import (
"errors"
"os"
"testing"
)

func TestTempConsole(t *testing.T) {
c, path := testSocketWithCorrectStickyBitMode(t, 0)
ensureSocketCleanup(t, c, path)
}

func TestTempConsoleWithXdgRuntimeDir(t *testing.T) {
tmpDir := "/tmp/foo"
if err := os.Setenv("XDG_RUNTIME_DIR", tmpDir); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(tmpDir, 0755); err != nil {
t.Fatal(err)
}

c, path := testSocketWithCorrectStickyBitMode(t, os.ModeSticky)
ensureSocketCleanup(t, c, path)

if err := os.RemoveAll(tmpDir); err != nil {
t.Fatal(err)
}
}

func testSocketWithCorrectStickyBitMode(t *testing.T, expectedMode os.FileMode) (*Socket, string) {
c, err := NewTempConsoleSocket()
if err != nil {
t.Fatal(err)
}
path := c.Path()
if _, err := os.Stat(path); err != nil {
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}

if (info.Mode() & os.ModeSticky) != expectedMode {
t.Fatal(errors.New("socket has incorrect mode"))
}
return c, path
}

func ensureSocketCleanup(t *testing.T, c *Socket, path string) {
if err := c.Close(); err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (o *ExecOpts) args() (out []string, err error) {
// Exec executres and additional process inside the container based on a full
// OCI Process specification
func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts *ExecOpts) error {
f, err := ioutil.TempFile("", "runc-process")
f, err := ioutil.TempFile(os.Getenv("XDG_RUNTIME_DIR"), "runc-process")
if err != nil {
return err
}
Expand Down

0 comments on commit 301f7c1

Please sign in to comment.