unix_socket.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //go:build !windows
  2. /*
  3. Package sockets is a simple unix domain socket wrapper.
  4. # Usage
  5. For example:
  6. import(
  7. "fmt"
  8. "net"
  9. "os"
  10. "github.com/docker/go-connections/sockets"
  11. )
  12. func main() {
  13. l, err := sockets.NewUnixSocketWithOpts("/path/to/sockets",
  14. sockets.WithChown(0,0),sockets.WithChmod(0660))
  15. if err != nil {
  16. panic(err)
  17. }
  18. echoStr := "hello"
  19. go func() {
  20. for {
  21. conn, err := l.Accept()
  22. if err != nil {
  23. return
  24. }
  25. conn.Write([]byte(echoStr))
  26. conn.Close()
  27. }
  28. }()
  29. conn, err := net.Dial("unix", path)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. buf := make([]byte, 5)
  34. if _, err := conn.Read(buf); err != nil {
  35. panic(err)
  36. } else if string(buf) != echoStr {
  37. panic(fmt.Errorf("msg may lost"))
  38. }
  39. }
  40. */
  41. package sockets
  42. import (
  43. "net"
  44. "os"
  45. "syscall"
  46. )
  47. // SockOption sets up socket file's creating option
  48. type SockOption func(string) error
  49. // WithChown modifies the socket file's uid and gid
  50. func WithChown(uid, gid int) SockOption {
  51. return func(path string) error {
  52. if err := os.Chown(path, uid, gid); err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. }
  58. // WithChmod modifies socket file's access mode.
  59. func WithChmod(mask os.FileMode) SockOption {
  60. return func(path string) error {
  61. if err := os.Chmod(path, mask); err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. }
  67. // NewUnixSocketWithOpts creates a unix socket with the specified options.
  68. // By default, socket permissions are 0000 (i.e.: no access for anyone); pass
  69. // WithChmod() and WithChown() to set the desired ownership and permissions.
  70. //
  71. // This function temporarily changes the system's "umask" to 0777 to work around
  72. // a race condition between creating the socket and setting its permissions. While
  73. // this should only be for a short duration, it may affect other processes that
  74. // create files/directories during that period.
  75. func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error) {
  76. if err := syscall.Unlink(path); err != nil && !os.IsNotExist(err) {
  77. return nil, err
  78. }
  79. // net.Listen does not allow for permissions to be set. As a result, when
  80. // specifying custom permissions ("WithChmod()"), there is a short time
  81. // between creating the socket and applying the permissions, during which
  82. // the socket permissions are Less restrictive than desired.
  83. //
  84. // To work around this limitation of net.Listen(), we temporarily set the
  85. // umask to 0777, which forces the socket to be created with 000 permissions
  86. // (i.e.: no access for anyone). After that, WithChmod() must be used to set
  87. // the desired permissions.
  88. //
  89. // We don't use "defer" here, to reset the umask to its original value as soon
  90. // as possible. Ideally we'd be able to detect if WithChmod() was passed as
  91. // an option, and skip changing umask if default permissions are used.
  92. origUmask := syscall.Umask(0o777)
  93. l, err := net.Listen("unix", path)
  94. syscall.Umask(origUmask)
  95. if err != nil {
  96. return nil, err
  97. }
  98. for _, op := range opts {
  99. if err := op(path); err != nil {
  100. _ = l.Close()
  101. return nil, err
  102. }
  103. }
  104. return l, nil
  105. }
  106. // NewUnixSocket creates a unix socket with the specified path and group.
  107. func NewUnixSocket(path string, gid int) (net.Listener, error) {
  108. return NewUnixSocketWithOpts(path, WithChown(0, gid), WithChmod(0o660))
  109. }