| 284 | } |
| 285 | |
| 286 | void |
| 287 | IPCSocketServer::bind(std::error_code &ec) |
| 288 | { |
| 289 | _lock_fd = open(_conf.lockPathName.c_str(), O_RDONLY | O_CREAT, 0600); |
| 290 | if (_lock_fd == -1) { |
| 291 | ec = std::make_error_code(static_cast<std::errc>(errno)); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | int ret = flock(_lock_fd, LOCK_EX | LOCK_NB); |
| 296 | |
| 297 | if (ret != 0) { |
| 298 | ec = std::make_error_code(static_cast<std::errc>(errno)); |
| 299 | return; |
| 300 | } |
| 301 | // TODO: we may be able to use SO_REUSEADDR |
| 302 | |
| 303 | // remove socket file |
| 304 | unlink(_conf.sockPathName.c_str()); |
| 305 | |
| 306 | ret = ::bind(_socket, (struct sockaddr *)&_serverAddr, sizeof(struct sockaddr_un)); |
| 307 | if (ret < 0) { |
| 308 | ec = std::make_error_code(static_cast<std::errc>(errno)); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | // If the socket is not administratively restricted, check whether we have platform |
| 313 | // support. Otherwise, default to making it restricted. |
| 314 | bool restricted{true}; |
| 315 | if (!_conf.restrictedAccessApi) { |
| 316 | restricted = !has_peereid(); |
| 317 | } |
| 318 | |
| 319 | mode_t mode = restricted ? 00700 : 00777; |
| 320 | if (chmod(_conf.sockPathName.c_str(), mode) < 0) { |
| 321 | ec = std::make_error_code(static_cast<std::errc>(errno)); |
| 322 | return; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void |
| 327 | IPCSocketServer::listen(std::error_code &ec) |
no test coverage detected