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