Skip to content

Commit 301f7c1

Browse files
authored
Merge pull request #41 from masters-of-cats/master
Use user-specific temp directory if set
2 parents f271fa2 + 07e192d commit 301f7c1

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

console.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func NewConsoleSocket(path string) (*Socket, error) {
5050
// NewTempConsoleSocket returns a temp console socket for use with a container
5151
// On Close(), the socket is deleted
5252
func NewTempConsoleSocket() (*Socket, error) {
53-
dir, err := ioutil.TempDir("", "pty")
53+
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
54+
dir, err := ioutil.TempDir(runtimeDir, "pty")
5455
if err != nil {
5556
return nil, err
5657
}
@@ -66,6 +67,11 @@ func NewTempConsoleSocket() (*Socket, error) {
6667
if err != nil {
6768
return nil, err
6869
}
70+
if runtimeDir != "" {
71+
if err := os.Chmod(abs, 0755|os.ModeSticky); err != nil {
72+
return nil, err
73+
}
74+
}
6975
return &Socket{
7076
l: l,
7177
rmdir: true,

console_test.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,51 @@
1717
package runc
1818

1919
import (
20+
"errors"
2021
"os"
2122
"testing"
2223
)
2324

2425
func TestTempConsole(t *testing.T) {
26+
c, path := testSocketWithCorrectStickyBitMode(t, 0)
27+
ensureSocketCleanup(t, c, path)
28+
}
29+
30+
func TestTempConsoleWithXdgRuntimeDir(t *testing.T) {
31+
tmpDir := "/tmp/foo"
32+
if err := os.Setenv("XDG_RUNTIME_DIR", tmpDir); err != nil {
33+
t.Fatal(err)
34+
}
35+
if err := os.MkdirAll(tmpDir, 0755); err != nil {
36+
t.Fatal(err)
37+
}
38+
39+
c, path := testSocketWithCorrectStickyBitMode(t, os.ModeSticky)
40+
ensureSocketCleanup(t, c, path)
41+
42+
if err := os.RemoveAll(tmpDir); err != nil {
43+
t.Fatal(err)
44+
}
45+
}
46+
47+
func testSocketWithCorrectStickyBitMode(t *testing.T, expectedMode os.FileMode) (*Socket, string) {
2548
c, err := NewTempConsoleSocket()
2649
if err != nil {
2750
t.Fatal(err)
2851
}
2952
path := c.Path()
30-
if _, err := os.Stat(path); err != nil {
53+
info, err := os.Stat(path)
54+
if err != nil {
3155
t.Fatal(err)
3256
}
57+
58+
if (info.Mode() & os.ModeSticky) != expectedMode {
59+
t.Fatal(errors.New("socket has incorrect mode"))
60+
}
61+
return c, path
62+
}
63+
64+
func ensureSocketCleanup(t *testing.T, c *Socket, path string) {
3365
if err := c.Close(); err != nil {
3466
t.Fatal(err)
3567
}

runc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func (o *ExecOpts) args() (out []string, err error) {
208208
// Exec executres and additional process inside the container based on a full
209209
// OCI Process specification
210210
func (r *Runc) Exec(context context.Context, id string, spec specs.Process, opts *ExecOpts) error {
211-
f, err := ioutil.TempFile("", "runc-process")
211+
f, err := ioutil.TempFile(os.Getenv("XDG_RUNTIME_DIR"), "runc-process")
212212
if err != nil {
213213
return err
214214
}

0 commit comments

Comments
 (0)