Skip to content

Commit

Permalink
Copy posix stuff to qnx
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Sep 16, 2024
1 parent d709f28 commit d8a8085
Show file tree
Hide file tree
Showing 7 changed files with 2,839 additions and 82 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "NetB

elseif(CMAKE_SYSTEM_NAME STREQUAL "QNX")
file(GLOB AWS_IO_OS_SRC
"source/posix/*.c"
"source/qnx/*.c"
)
set(EVENT_LOOP_DEFINE "ON_EVENT_WITH_RESULT")
Expand Down
28 changes: 0 additions & 28 deletions source/posix/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,28 +278,10 @@ int aws_pipe_read(struct aws_pipe_read_end *read_end, struct aws_byte_buf *dst_b
if (read_val < 0) {
int errno_value = errno; /* Always cache errno before potential side-effect */
if (errno_value == EAGAIN || errno_value == EWOULDBLOCK) {
#if AWS_USE_ON_EVENT_WITH_RESULT
if (read_impl->handle.update_io_result) {
struct aws_io_handle_io_op_result io_op_result;
AWS_ZERO_STRUCT(io_op_result);
io_op_result.read_error_code = AWS_IO_READ_WOULD_BLOCK;
read_impl->handle.update_io_result(read_impl->event_loop, &read_impl->handle, &io_op_result);
}
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */
return aws_raise_error(AWS_IO_READ_WOULD_BLOCK);
}
return s_raise_posix_error(errno_value);
}
#if AWS_USE_ON_EVENT_WITH_RESULT
else if (read_val == 0) {
if (read_impl->handle.update_io_result) {
struct aws_io_handle_io_op_result io_op_result;
memset(&io_op_result, 0, sizeof(struct aws_io_handle_io_op_result));
io_op_result.error_code = AWS_IO_SOCKET_CLOSED;
read_impl->handle.update_io_result(read_impl->event_loop, &read_impl->handle, &io_op_result);
}
}
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

/* Success */
dst_buffer->len += read_val;
Expand Down Expand Up @@ -472,16 +454,6 @@ static void s_write_end_process_requests(struct aws_pipe_write_end *write_end) {
if (errno_value == EAGAIN || errno_value == EWOULDBLOCK) {
/* The pipe is no longer writable. Bail out */
write_impl->is_writable = false;

#if AWS_USE_ON_EVENT_WITH_RESULT
if (write_impl->handle.update_io_result) {
struct aws_io_handle_io_op_result io_op_result;
AWS_ZERO_STRUCT(io_op_result);
io_op_result.write_error_code = AWS_IO_READ_WOULD_BLOCK;
write_impl->handle.update_io_result(write_impl->event_loop, &write_impl->handle, &io_op_result);
}
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

return;
}

Expand Down
54 changes: 1 addition & 53 deletions source/posix/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,6 @@ static void s_socket_connect_event(
"id=%p fd=%d: spurious event, waiting for another notification.",
(void *)socket_args->socket,
handle->data.fd);

#if AWS_USE_ON_EVENT_WITH_RESULT
if (handle->update_io_result) {
struct aws_io_handle_io_op_result io_op_result;
AWS_ZERO_STRUCT(io_op_result);
io_op_result.read_error_code = AWS_IO_READ_WOULD_BLOCK;
handle->update_io_result(event_loop, handle, &io_op_result);
}
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

return;
}

Expand Down Expand Up @@ -965,11 +955,6 @@ static void s_socket_accept_event(
AWS_LOGF_DEBUG(
AWS_LS_IO_SOCKET, "id=%p fd=%d: listening event received", (void *)socket, socket->io_handle.data.fd);

#if AWS_USE_ON_EVENT_WITH_RESULT
struct aws_io_handle_io_op_result io_op_result;
AWS_ZERO_STRUCT(io_op_result);
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

if (socket_impl->continue_accept && events & AWS_IO_EVENT_TYPE_READABLE) {
int in_fd = 0;
while (socket_impl->continue_accept && in_fd != -1) {
Expand All @@ -981,18 +966,12 @@ static void s_socket_accept_event(
int errno_value = errno; /* Always cache errno before potential side-effect */

if (errno_value == EAGAIN || errno_value == EWOULDBLOCK) {
#if AWS_USE_ON_EVENT_WITH_RESULT
io_op_result.read_error_code = AWS_IO_READ_WOULD_BLOCK;
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */
break;
}

int aws_error = aws_socket_get_error(socket);
aws_raise_error(aws_error);
s_on_connection_error(socket, aws_error);
#if AWS_USE_ON_EVENT_WITH_RESULT
io_op_result.read_error_code = aws_error;
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */
break;
}

Expand Down Expand Up @@ -1085,12 +1064,6 @@ static void s_socket_accept_event(
}
}

#if AWS_USE_ON_EVENT_WITH_RESULT
if (handle->update_io_result) {
handle->update_io_result(event_loop, handle, &io_op_result);
}
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET,
"id=%p fd=%d: finished processing incoming connections, "
Expand Down Expand Up @@ -1659,11 +1632,6 @@ static int s_process_socket_write_requests(struct aws_socket *socket, struct soc
bool parent_request_failed = false;
bool pushed_to_written_queue = false;

#if AWS_USE_ON_EVENT_WITH_RESULT
struct aws_io_handle_io_op_result io_op_result;
AWS_ZERO_STRUCT(io_op_result);
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

/* if a close call happens in the middle, this queue will have been cleaned out from under us. */
while (!aws_linked_list_empty(&socket_impl->write_queue)) {
struct aws_linked_list_node *node = aws_linked_list_front(&socket_impl->write_queue);
Expand Down Expand Up @@ -1692,9 +1660,6 @@ static int s_process_socket_write_requests(struct aws_socket *socket, struct soc
if (errno_value == EAGAIN) {
AWS_LOGF_TRACE(
AWS_LS_IO_SOCKET, "id=%p fd=%d: returned would block", (void *)socket, socket->io_handle.data.fd);
#if AWS_USE_ON_EVENT_WITH_RESULT
io_op_result.write_error_code = AWS_IO_READ_WOULD_BLOCK;
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */
break;
}

Expand All @@ -1707,9 +1672,6 @@ static int s_process_socket_write_requests(struct aws_socket *socket, struct soc
aws_error = AWS_IO_SOCKET_CLOSED;
aws_raise_error(aws_error);
purge = true;
#if AWS_USE_ON_EVENT_WITH_RESULT
io_op_result.write_error_code = aws_error;
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */
break;
}

Expand All @@ -1722,16 +1684,9 @@ static int s_process_socket_write_requests(struct aws_socket *socket, struct soc
errno_value);
aws_error = s_determine_socket_error(errno_value);
aws_raise_error(aws_error);
#if AWS_USE_ON_EVENT_WITH_RESULT
io_op_result.write_error_code = aws_error;
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */
break;
}

#if AWS_USE_ON_EVENT_WITH_RESULT
io_op_result.written_bytes += (size_t)written;
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

size_t remaining_to_write = write_request->cursor_cpy.len;

aws_byte_cursor_advance(&write_request->cursor_cpy, (size_t)written);
Expand Down Expand Up @@ -1777,12 +1732,6 @@ static int s_process_socket_write_requests(struct aws_socket *socket, struct soc
aws_event_loop_schedule_task_now(socket->event_loop, &socket_impl->written_task);
}

#if AWS_USE_ON_EVENT_WITH_RESULT
if (socket->io_handle.update_io_result) {
socket->io_handle.update_io_result(socket->event_loop, &socket->io_handle, &io_op_result);
}
#endif /* AWS_USE_ON_EVENT_WITH_RESULT */

/* Only report error if aws_socket_write() invoked this function and its write_request failed */
if (!parent_request_failed) {
return AWS_OP_SUCCESS;
Expand Down Expand Up @@ -2056,6 +2005,5 @@ void aws_socket_endpoint_init_local_address_for_test(struct aws_socket_endpoint
char uuid_str[AWS_UUID_STR_LEN] = {0};
struct aws_byte_buf uuid_buf = aws_byte_buf_from_empty_array(uuid_str, sizeof(uuid_str));
AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &uuid_buf) == AWS_OP_SUCCESS);
/* TODO QNX allows creating a socket file only in /tmp directory. */
snprintf(endpoint->address, sizeof(endpoint->address), "/tmp/testsock" PRInSTR ".sock", AWS_BYTE_BUF_PRI(uuid_buf));
snprintf(endpoint->address, sizeof(endpoint->address), "testsock" PRInSTR ".sock", AWS_BYTE_BUF_PRI(uuid_buf));
}
121 changes: 121 additions & 0 deletions source/qnx/host_resolver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/io/host_resolver.h>

#include <aws/io/logging.h>

#include <aws/common/string.h>

#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>

int aws_default_dns_resolve(
struct aws_allocator *allocator,
const struct aws_string *host_name,
struct aws_array_list *output_addresses,
void *user_data) {

(void)user_data;
struct addrinfo *result = NULL;
struct addrinfo *iter = NULL;
/* max string length for ipv6. */
socklen_t max_len = INET6_ADDRSTRLEN;
char address_buffer[max_len];

const char *hostname_cstr = aws_string_c_str(host_name);
AWS_LOGF_DEBUG(AWS_LS_IO_DNS, "static: resolving host %s", hostname_cstr);

/* Android would prefer NO HINTS IF YOU DON'T MIND, SIR */
#if defined(ANDROID)
int err_code = getaddrinfo(hostname_cstr, NULL, NULL, &result);
#else
struct addrinfo hints;
AWS_ZERO_STRUCT(hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
# if !defined(__OpenBSD__)
hints.ai_flags = AI_ALL | AI_V4MAPPED;
# endif /* __OpenBSD__ */

int err_code = getaddrinfo(hostname_cstr, NULL, &hints, &result);
#endif

if (err_code) {
AWS_LOGF_ERROR(
AWS_LS_IO_DNS, "static: getaddrinfo failed with error_code %d: %s", err_code, gai_strerror(err_code));
goto clean_up;
}

for (iter = result; iter != NULL; iter = iter->ai_next) {
struct aws_host_address host_address;

AWS_ZERO_ARRAY(address_buffer);

if (iter->ai_family == AF_INET6) {
host_address.record_type = AWS_ADDRESS_RECORD_TYPE_AAAA;
inet_ntop(iter->ai_family, &((struct sockaddr_in6 *)iter->ai_addr)->sin6_addr, address_buffer, max_len);
} else {
host_address.record_type = AWS_ADDRESS_RECORD_TYPE_A;
inet_ntop(iter->ai_family, &((struct sockaddr_in *)iter->ai_addr)->sin_addr, address_buffer, max_len);
}

size_t address_len = strlen(address_buffer);
const struct aws_string *address =
aws_string_new_from_array(allocator, (const uint8_t *)address_buffer, address_len);

if (!address) {
goto clean_up;
}

const struct aws_string *host_cpy = aws_string_new_from_string(allocator, host_name);

if (!host_cpy) {
aws_string_destroy((void *)address);
goto clean_up;
}

AWS_LOGF_DEBUG(AWS_LS_IO_DNS, "static: resolved record: %s", address_buffer);

host_address.address = address;
host_address.weight = 0;
host_address.allocator = allocator;
host_address.use_count = 0;
host_address.connection_failure_count = 0;
host_address.host = host_cpy;

if (aws_array_list_push_back(output_addresses, &host_address)) {
aws_host_address_clean_up(&host_address);
goto clean_up;
}
}

freeaddrinfo(result);
return AWS_OP_SUCCESS;

clean_up:
if (result) {
freeaddrinfo(result);
}

if (err_code) {
switch (err_code) {
case EAI_FAIL:
case EAI_AGAIN:
return aws_raise_error(AWS_IO_DNS_QUERY_FAILED);
case EAI_MEMORY:
return aws_raise_error(AWS_ERROR_OOM);
case EAI_NONAME:
case EAI_SERVICE:
return aws_raise_error(AWS_IO_DNS_INVALID_NAME);
default:
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
}
}

return AWS_OP_ERR;
}
Loading

0 comments on commit d8a8085

Please sign in to comment.