From ebf984b6b7ec855363f60bdb147e827d0737a5cb Mon Sep 17 00:00:00 2001 From: brl Date: Sun, 7 Jun 2015 15:59:33 -0400 Subject: [PATCH 1/5] add MsgServer.Close() method --- ipc/ipc.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ipc/ipc.go b/ipc/ipc.go index 72555a9..d6bfbad 100644 --- a/ipc/ipc.go +++ b/ipc/ipc.go @@ -64,8 +64,6 @@ func (s *MsgServer) Run() error { for { conn, err := s.listener.AcceptUnix() if err != nil { - s.disp.close() - s.listener.Close() return err } if err := setPassCred(conn); err != nil { @@ -84,6 +82,12 @@ func (s *MsgServer) Run() error { return nil } +func (s *MsgServer) Close() error { + s.disp.close() + close(s.done) + return s.listener.Close() +} + func Connect(address string, factory MsgFactory, log *logging.Logger, handlers ...interface{}) (*MsgConn, error) { md, err := createDispatcher(log, handlers...) if err != nil { From 7535992e63a5505259fe16f2fdf402165632d0fd Mon Sep 17 00:00:00 2001 From: brl Date: Sun, 7 Jun 2015 15:59:43 -0400 Subject: [PATCH 2/5] fix tests --- ipc/ipc_test.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/ipc/ipc_test.go b/ipc/ipc_test.go index 81d1cb0..d5311a1 100644 --- a/ipc/ipc_test.go +++ b/ipc/ipc_test.go @@ -11,7 +11,7 @@ type TestMsg struct { } type testConnection struct { - server *MsgConn + server *MsgServer client *MsgConn wg sync.WaitGroup called bool @@ -27,27 +27,23 @@ const testSocket = "@test" var testFactory = NewMsgFactory(new(TestMsg)) func testConnect(handler func(*TestMsg, *Message) error) (*testConnection, error) { - s := NewMsgConn(testFactory, testSocket) - c := NewMsgConn(testFactory, testSocket) - tc := &testConnection{ - server: s, - client: c, - } + tc := &testConnection{} wrapper := func(tm *TestMsg, msg *Message) error { err := handler(tm, msg) tc.called = true tc.wg.Done() return err } - if err := s.AddHandlers(wrapper); err != nil { - return nil, err - } - if err := s.Listen(); err != nil { + s,err := NewServer(testSocket, testFactory, nil, wrapper) + if err != nil { return nil, err } - if err := c.Connect(); err != nil { + c,err := Connect(testSocket, testFactory, nil) + if err != nil { return nil, err } + tc.server = s + tc.client = c tc.wg.Add(1) go tc.server.Run() return tc, nil From 476df1772674e5e377666618b05206b9fa0b98fe Mon Sep 17 00:00:00 2001 From: brl Date: Sun, 7 Jun 2015 19:39:53 -0400 Subject: [PATCH 3/5] Don't attempt to set SO_PASSCRED if Listen fails --- ipc/ipc.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ipc/ipc.go b/ipc/ipc.go index d6bfbad..2c87707 100644 --- a/ipc/ipc.go +++ b/ipc/ipc.go @@ -42,13 +42,13 @@ func NewServer(address string, factory MsgFactory, log *logging.Logger, handlers } listener, err := net.ListenUnix("unix", &net.UnixAddr{address, "unix"}) - if err := setPassCred(listener); err != nil { - return nil, errors.New("Failed to set SO_PASSCRED on listening socket: " + err.Error()) - } if err != nil { md.close() return nil, err } + if err := setPassCred(listener); err != nil { + return nil, errors.New("Failed to set SO_PASSCRED on listening socket: " + err.Error()) + } done := make(chan bool) idGen := newIdGen(done) return &MsgServer{ From afcd61da21875fcbca270b9ae76c469e9c27f313 Mon Sep 17 00:00:00 2001 From: brl Date: Sun, 7 Jun 2015 19:40:53 -0400 Subject: [PATCH 4/5] log xpra client output instead of dumping it to stdout --- oz-daemon/launch.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/oz-daemon/launch.go b/oz-daemon/launch.go index e5888d2..e8fa4ec 100644 --- a/oz-daemon/launch.go +++ b/oz-daemon/launch.go @@ -7,7 +7,6 @@ import ( "github.com/subgraph/oz/fs" "github.com/subgraph/oz/xpra" "io" - "os" "os/exec" "os/user" "path" @@ -178,9 +177,34 @@ func (sbox *Sandbox) startXpraClient() { sbox.fs.Xpra(), sbox.profile.Name, sbox.daemon.log) - sbox.xpra.Process.Stdout = os.Stdout - sbox.xpra.Process.Stderr = os.Stdout + + if sbox.daemon.config.LogXpra { + sbox.setupXpraLogging() + } if err := sbox.xpra.Process.Start(); err != nil { sbox.daemon.Warning("Failed to start xpra client: %v", err) } } + +func (sbox *Sandbox) setupXpraLogging() { + stdout,err := sbox.xpra.Process.StdoutPipe() + if err != nil { + sbox.daemon.Warning("Failed to create xpra stdout pipe: %v", err) + return + } + stderr,err := sbox.xpra.Process.StderrPipe() + if err != nil { + stdout.Close() + sbox.daemon.Warning("Failed to create xpra stderr pipe: %v", err) + } + go sbox.logPipeOutput(stdout, "xpra-stdout") + go sbox.logPipeOutput(stderr, "xpra-stderr") +} + +func (sbox *Sandbox) logPipeOutput(p io.Reader, label string) { + scanner := bufio.NewScanner(p) + for scanner.Scan() { + line := scanner.Text() + sbox.daemon.log.Info("(%s) %s", label, line) + } +} From 5c667946e9b31bbe670d7a5c63249b9e98926d58 Mon Sep 17 00:00:00 2001 From: brl Date: Sun, 7 Jun 2015 19:42:04 -0400 Subject: [PATCH 5/5] gofmt --- config.go | 10 +++++----- ipc/ipc_test.go | 4 ++-- oz-daemon/launch.go | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index a13fe2e..d9ffaf5 100644 --- a/config.go +++ b/config.go @@ -6,11 +6,11 @@ import ( ) type Config struct { - ProfileDir string `json:"profile_dir"` - ShellPath string `json:"shell_path"` - SandboxPath string `json:"sandbox_path"` - AllowRootShell bool `json:"allow_root_shell"` - LogXpra bool `json:"log_xpra"` + ProfileDir string `json:"profile_dir"` + ShellPath string `json:"shell_path"` + SandboxPath string `json:"sandbox_path"` + AllowRootShell bool `json:"allow_root_shell"` + LogXpra bool `json:"log_xpra"` } const DefaultConfigPath = "/etc/oz/oz.conf" diff --git a/ipc/ipc_test.go b/ipc/ipc_test.go index d5311a1..c385b83 100644 --- a/ipc/ipc_test.go +++ b/ipc/ipc_test.go @@ -34,11 +34,11 @@ func testConnect(handler func(*TestMsg, *Message) error) (*testConnection, error tc.wg.Done() return err } - s,err := NewServer(testSocket, testFactory, nil, wrapper) + s, err := NewServer(testSocket, testFactory, nil, wrapper) if err != nil { return nil, err } - c,err := Connect(testSocket, testFactory, nil) + c, err := Connect(testSocket, testFactory, nil) if err != nil { return nil, err } diff --git a/oz-daemon/launch.go b/oz-daemon/launch.go index e8fa4ec..0736736 100644 --- a/oz-daemon/launch.go +++ b/oz-daemon/launch.go @@ -187,12 +187,12 @@ func (sbox *Sandbox) startXpraClient() { } func (sbox *Sandbox) setupXpraLogging() { - stdout,err := sbox.xpra.Process.StdoutPipe() + stdout, err := sbox.xpra.Process.StdoutPipe() if err != nil { sbox.daemon.Warning("Failed to create xpra stdout pipe: %v", err) return } - stderr,err := sbox.xpra.Process.StderrPipe() + stderr, err := sbox.xpra.Process.StderrPipe() if err != nil { stdout.Close() sbox.daemon.Warning("Failed to create xpra stderr pipe: %v", err)