github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/server/pipe.go (about)

     1  // +build !windows
     2  
     3  package server
     4  
     5  import (
     6  	"net"
     7  	"os"
     8  
     9  	"go.uber.org/zap"
    10  )
    11  
    12  func cleanupPipe(address string) error {
    13  	// Cleanup the leftover socket first.
    14  	if _, err := os.Stat(address); err == nil {
    15  		if err := os.Remove(address); err != nil {
    16  			zap.L().Error("Cannot remove existing pipe", zap.String("address", address), zap.Error(err))
    17  			return err
    18  		}
    19  	}
    20  	return nil
    21  }
    22  
    23  func makePipe(address string) (net.Listener, error) {
    24  	// Start a custom listener
    25  	addr, _ := net.ResolveUnixAddr("unix", address)
    26  	nl, err := net.ListenUnix("unix", addr)
    27  	if err != nil {
    28  		zap.L().Error("Unable to start the listener", zap.String("address", address), zap.Error(err))
    29  		return nil, err
    30  	}
    31  
    32  	// make it owner,group rw only.
    33  	// TODO: which group ID? or should it be owner root rw only ?
    34  	if err := os.Chmod(addr.String(), 0600); err != nil {
    35  		zap.L().Error("Cannot set permissions on the pipe", zap.String("address", address), zap.Error(err))
    36  		return nil, err
    37  	}
    38  
    39  	return nl, nil
    40  }