| 2955 | } |
| 2956 | |
| 2957 | cbm_daemon_ipc_connection_t *cbm_daemon_ipc_connect(const cbm_daemon_ipc_endpoint_t *endpoint, |
| 2958 | uint32_t timeout_ms) { |
| 2959 | if (!endpoint_runtime_still_valid(endpoint)) { |
| 2960 | return NULL; |
| 2961 | } |
| 2962 | uint64_t deadline_ms = ipc_deadline_after(timeout_ms); |
| 2963 | for (;;) { |
| 2964 | struct stat status; |
| 2965 | if (fstatat(endpoint->dir_fd, endpoint->socket_name, &status, AT_SYMLINK_NOFOLLOW) != 0) { |
| 2966 | int path_error = errno; |
| 2967 | if (path_error == ENOENT && ipc_retry_pause(deadline_ms)) { |
| 2968 | continue; |
| 2969 | } |
| 2970 | return NULL; |
| 2971 | } |
| 2972 | if (!posix_socket_status_secure_transport(&status)) { |
| 2973 | return NULL; |
| 2974 | } |
| 2975 | |
| 2976 | int fd = local_socket_new(); |
| 2977 | if (fd < 0) { |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | struct sockaddr_un address; |
| 2981 | socklen_t address_length; |
| 2982 | if (!unix_address_set(&address, endpoint->address, &address_length)) { |
| 2983 | (void)close(fd); |
| 2984 | return NULL; |
| 2985 | } |
| 2986 | |
| 2987 | int result = local_socket_connect(fd, &address, address_length); |
| 2988 | int connect_error = result == 0 ? 0 : errno; |
| 2989 | if (result != 0 && connect_error != EINPROGRESS && connect_error != EAGAIN && |
| 2990 | connect_error != EWOULDBLOCK) { |
| 2991 | (void)close(fd); |
| 2992 | if ((connect_error == ENOENT || connect_error == ECONNREFUSED) && |
| 2993 | ipc_retry_pause(deadline_ms)) { |
| 2994 | continue; |
| 2995 | } |
| 2996 | return NULL; |
| 2997 | } |
| 2998 | if (result != 0) { |
| 2999 | int ready = poll_until(fd, POLLOUT, deadline_ms); |
| 3000 | int socket_error = 0; |
| 3001 | socklen_t error_length = sizeof(socket_error); |
| 3002 | if (ready != 1 || |
| 3003 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_error, &error_length) != 0) { |
| 3004 | (void)close(fd); |
| 3005 | return NULL; |
| 3006 | } |
| 3007 | if (socket_error != 0) { |
| 3008 | (void)close(fd); |
| 3009 | if ((socket_error == ENOENT || socket_error == ECONNREFUSED) && |
| 3010 | ipc_retry_pause(deadline_ms)) { |
| 3011 | continue; |
| 3012 | } |
| 3013 | return NULL; |
| 3014 | } |