createNullSocket creates a socket that reads anything received. Only works for dgram sockets.
(path string, protocol int)
| 149 | // |
| 150 | // Only works for dgram sockets. |
| 151 | func createNullSocket(path string, protocol int) (cleanup func(), err error) { |
| 152 | fd, err := unix.Socket(unix.AF_UNIX, protocol, 0) |
| 153 | if err != nil { |
| 154 | return nil, fmt.Errorf("error creating null(%d) socket: %v", protocol, err) |
| 155 | } |
| 156 | |
| 157 | if err := unix.Bind(fd, &unix.SockaddrUnix{Name: path}); err != nil { |
| 158 | return nil, fmt.Errorf("error binding null(%d) socket: %v", protocol, err) |
| 159 | } |
| 160 | |
| 161 | s, err := unet.NewSocket(fd) |
| 162 | if err != nil { |
| 163 | return nil, fmt.Errorf("error creating null(%d) unet socket: %v", protocol, err) |
| 164 | } |
| 165 | |
| 166 | go func() { |
| 167 | buf := make([]byte, 512) |
| 168 | for { |
| 169 | n, err := s.Read(buf) |
| 170 | if err != nil { |
| 171 | log.Warningf("failed to read: %d, %v", n, err) |
| 172 | return |
| 173 | } |
| 174 | } |
| 175 | }() |
| 176 | |
| 177 | cleanup = func() { |
| 178 | if err := s.Close(); err != nil { |
| 179 | log.Warningf("Failed to close null(%d) socket: %v", protocol, err) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return cleanup, nil |
| 184 | } |
| 185 | |
| 186 | // createPipeWriter creates a pipe that writes a sequence of bytes starting from |
| 187 | // 0 to 256, wrapping it back to 0. |