in source/aws_imds_client.c [786:850]
static enum imds_token_copy_result s_copy_token_safely(struct imds_user_data *user_data) {
struct aws_imds_client *client = user_data->client;
enum imds_token_copy_result ret = AWS_IMDS_TCR_UNEXPECTED_ERROR;
struct aws_linked_list pending_queries;
aws_linked_list_init(&pending_queries);
aws_mutex_lock(&client->token_lock);
if (client->token_state == AWS_IMDS_TS_VALID) {
aws_byte_buf_reset(&user_data->imds_token, true);
struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(&client->cached_token);
if (aws_byte_buf_append_dynamic(&user_data->imds_token, &cursor)) {
ret = AWS_IMDS_TCR_UNEXPECTED_ERROR;
} else {
ret = AWS_IMDS_TCR_SUCCESS;
}
} else {
ret = AWS_IMDS_TCR_WAITING_IN_QUEUE;
struct imds_token_query *query = aws_mem_calloc(client->allocator, 1, sizeof(struct imds_token_query));
if (query != NULL) {
query->user_data = user_data;
aws_linked_list_push_back(&client->pending_queries, &query->node);
} else {
ret = AWS_IMDS_TCR_UNEXPECTED_ERROR;
}
if (client->token_state == AWS_IMDS_TS_INVALID) {
if (s_client_start_query_token(client)) {
ret = AWS_IMDS_TCR_UNEXPECTED_ERROR;
aws_linked_list_swap_contents(&pending_queries, &client->pending_queries);
} else {
client->token_state = AWS_IMDS_TS_UPDATE_IN_PROGRESS;
}
}
}
aws_mutex_unlock(&client->token_lock);
s_complete_pending_queries(client, &pending_queries, true, NULL);
switch (ret) {
case AWS_IMDS_TCR_SUCCESS:
AWS_LOGF_DEBUG(
AWS_LS_IMDS_CLIENT,
"(id=%p) IMDS client copied token to requester %p successfully.",
(void *)client,
(void *)user_data);
break;
case AWS_IMDS_TCR_WAITING_IN_QUEUE:
AWS_LOGF_DEBUG(
AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client's token is invalid and is now updating.", (void *)client);
break;
case AWS_IMDS_TCR_UNEXPECTED_ERROR:
AWS_LOGF_DEBUG(
AWS_LS_IMDS_CLIENT,
"(id=%p) IMDS client encountered unexpected error when processing token query for requester %p, error: "
"%s.",
(void *)client,
(void *)user_data,
aws_error_str(aws_last_error()));
break;
}
return ret;
}