unix_socket.go 610 B

1234567891011121314151617181920212223242526272829303132
  1. // +build !windows
  2. package sockets
  3. import (
  4. "net"
  5. "os"
  6. "syscall"
  7. )
  8. // NewUnixSocket creates a unix socket with the specified path and group.
  9. func NewUnixSocket(path string, gid int) (net.Listener, error) {
  10. if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) {
  11. return nil, err
  12. }
  13. mask := syscall.Umask(0777)
  14. defer syscall.Umask(mask)
  15. l, err := net.Listen("unix", path)
  16. if err != nil {
  17. return nil, err
  18. }
  19. if err := os.Chown(path, 0, gid); err != nil {
  20. l.Close()
  21. return nil, err
  22. }
  23. if err := os.Chmod(path, 0660); err != nil {
  24. l.Close()
  25. return nil, err
  26. }
  27. return l, nil
  28. }