diff --git a/ipc/ipc.go b/ipc/ipc.go index 72555a9..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{ @@ -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 { diff --git a/ipc/ipc_test.go b/ipc/ipc_test.go index 81d1cb0..c385b83 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 diff --git a/oz-daemon/launch.go b/oz-daemon/launch.go index 9f52df9..67a94cc 100644 --- a/oz-daemon/launch.go +++ b/oz-daemon/launch.go @@ -4,7 +4,6 @@ import ( "bufio" "fmt" "io" - "os" "os/exec" "os/user" "path" @@ -226,9 +225,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) + } +}