| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // +build darwin dragonfly freebsd linux netbsd openbsd |
| 6 |
| 7 package streamserver |
| 8 |
| 9 import ( |
| 10 "net" |
| 11 "os" |
| 12 |
| 13 log "github.com/luci/luci-go/common/logging" |
| 14 "golang.org/x/net/context" |
| 15 ) |
| 16 |
| 17 // NewNamedPipeServer instantiates a new POSIX named pipe server instance. |
| 18 func NewNamedPipeServer(ctx context.Context, path string) StreamServer { |
| 19 ctx = log.SetField(ctx, "namedPipePath", path) |
| 20 return createNamedPipeServer(ctx, func() (net.Listener, error) { |
| 21 log.Infof(ctx, "Creating POSIX server socket Listener.") |
| 22 |
| 23 // Cleanup any previous named pipe. We don't bother checking for
the file |
| 24 // first since the remove is atomic. We also ignore any error he
re, since |
| 25 // it's probably related to the file not being found. |
| 26 // |
| 27 // If there was an actual error removing the file, we'll catch i
t shortly |
| 28 // when we try to create it. |
| 29 os.Remove(path) |
| 30 |
| 31 // Create a UNIX listener |
| 32 l, err := net.Listen("unix", path) |
| 33 if err != nil { |
| 34 return nil, err |
| 35 } |
| 36 |
| 37 ul := selfCleaningUNIXListener{ |
| 38 Context: ctx, |
| 39 Listener: l, |
| 40 path: path, |
| 41 } |
| 42 return &ul, nil |
| 43 }) |
| 44 } |
| 45 |
| 46 // Wrapper around the "unix"-type Listener that cleans up the named pipe on |
| 47 // creation and 'Close()' |
| 48 type selfCleaningUNIXListener struct { |
| 49 context.Context |
| 50 net.Listener |
| 51 |
| 52 path string |
| 53 } |
| 54 |
| 55 func (l *selfCleaningUNIXListener) Close() error { |
| 56 if err := l.Listener.Close(); err != nil { |
| 57 return err |
| 58 } |
| 59 |
| 60 if err := os.Remove(l.path); err != nil { |
| 61 log.Fields{ |
| 62 log.ErrorKey: err, |
| 63 }.Warningf(l, "Failed to remove named pipe file on Close().") |
| 64 } |
| 65 return nil |
| 66 } |
| OLD | NEW |