summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-common/source/posix
diff options
context:
space:
mode:
authorthegeorg <[email protected]>2025-05-12 15:51:24 +0300
committerthegeorg <[email protected]>2025-05-12 16:06:27 +0300
commitd629bb70c8773d2c0c43f5088ddbb5a86d8c37ea (patch)
tree4f678e0d65ad08c800db21c657d3b0f71fafed06 /contrib/restricted/aws/aws-c-common/source/posix
parent92c4b696d7a1c03d54e13aff7a7c20a078d90dd7 (diff)
Update contrib/restricted/aws libraries to nixpkgs 24.05
commit_hash:f8083acb039e6005e820cdee77b84e0a6b6c6d6d
Diffstat (limited to 'contrib/restricted/aws/aws-c-common/source/posix')
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/clock.c4
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c6
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/cross_process_lock.c141
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/device_random.c3
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/file.c4
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/mutex.c8
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/system_info.c6
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/system_resource_utils.c32
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/thread.c21
9 files changed, 206 insertions, 19 deletions
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/clock.c b/contrib/restricted/aws/aws-c-common/source/posix/clock.c
index 90e213ea7c8..b2c3bc3f0e7 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/clock.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/clock.c
@@ -43,11 +43,11 @@ static int s_legacy_get_time(uint64_t *timestamp) {
# if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
static aws_thread_once s_thread_once_flag = AWS_THREAD_ONCE_STATIC_INIT;
-static int (*s_gettime_fn)(clockid_t __clock_id, struct timespec *__tp) = NULL;
+static int (*s_gettime_fn)(clockid_t clock_id, struct timespec *tp) = NULL;
static void s_do_osx_loads(void *user_data) {
(void)user_data;
- s_gettime_fn = (int (*)(clockid_t __clock_id, struct timespec * __tp)) dlsym(RTLD_DEFAULT, "clock_gettime");
+ s_gettime_fn = (int (*)(clockid_t clock_id, struct timespec * tp)) dlsym(RTLD_DEFAULT, "clock_gettime");
}
int aws_high_res_clock_get_ticks(uint64_t *timestamp) {
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c b/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c
index ca321c6bfad..64edb45b85a 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/condition_variable.c
@@ -93,12 +93,10 @@ int aws_condition_variable_wait_for(
return AWS_OP_ERR;
}
- time_to_wait += current_sys_time;
-
struct timespec ts;
uint64_t remainder = 0;
- ts.tv_sec =
- (time_t)aws_timestamp_convert((uint64_t)time_to_wait, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder);
+ ts.tv_sec = (time_t)aws_timestamp_convert(
+ (uint64_t)(time_to_wait + current_sys_time), AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder);
ts.tv_nsec = (long)remainder;
int err_code = pthread_cond_timedwait(&condition_variable->condition_handle, &mutex->mutex_handle, &ts);
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/cross_process_lock.c b/contrib/restricted/aws/aws-c-common/source/posix/cross_process_lock.c
new file mode 100644
index 00000000000..1ef5d2b5fe4
--- /dev/null
+++ b/contrib/restricted/aws/aws-c-common/source/posix/cross_process_lock.c
@@ -0,0 +1,141 @@
+/**
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ * SPDX-License-Identifier: Apache-2.0.
+ */
+#include <aws/common/cross_process_lock.h>
+
+#include <aws/common/byte_buf.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <aws/common/error.h>
+#include <aws/common/file.h>
+#include <aws/common/logging.h>
+
+struct aws_cross_process_lock {
+ struct aws_allocator *allocator;
+ int locked_fd;
+};
+
+struct aws_cross_process_lock *aws_cross_process_lock_try_acquire(
+ struct aws_allocator *allocator,
+ struct aws_byte_cursor instance_nonce) {
+
+ /* validate we don't have a directory slash. */
+ struct aws_byte_cursor to_find = aws_byte_cursor_from_c_str("/");
+ struct aws_byte_cursor found;
+ AWS_ZERO_STRUCT(found);
+ if (aws_byte_cursor_find_exact(&instance_nonce, &to_find, &found) != AWS_OP_ERR &&
+ aws_last_error() != AWS_ERROR_STRING_MATCH_NOT_FOUND) {
+ AWS_LOGF_ERROR(
+ AWS_LS_COMMON_GENERAL,
+ "static: Lock " PRInSTR "creation has illegal character /",
+ AWS_BYTE_CURSOR_PRI(instance_nonce));
+ aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
+ return NULL;
+ }
+
+ /*
+ * The unix standard says /tmp has to be there and be writable. However, while it may be tempting to just use the
+ * /tmp/ directory, it often has the sticky bit set which would prevent a subprocess from being able to call open
+ * with create on the file. The solution is simple, just write it to a subdirectory inside
+ * /tmp and override umask via. chmod of 0777.
+ */
+ struct aws_byte_cursor path_prefix = aws_byte_cursor_from_c_str("/tmp/aws_crt_cross_process_lock/");
+ struct aws_string *path_to_create = aws_string_new_from_cursor(allocator, &path_prefix);
+
+ /* It's probably there already and we don't care if it is. */
+ if (!aws_directory_exists(path_to_create)) {
+ /* if this call fails just let it fail on open below. */
+ aws_directory_create(path_to_create);
+ /* bypass umask by setting the perms we actually requested */
+ chmod(aws_string_c_str(path_to_create), S_IRWXU | S_IRWXG | S_IRWXO);
+ }
+ aws_string_destroy(path_to_create);
+
+ struct aws_byte_cursor path_suffix = aws_byte_cursor_from_c_str(".lock");
+
+ struct aws_byte_buf nonce_buf;
+ aws_byte_buf_init_copy_from_cursor(&nonce_buf, allocator, path_prefix);
+ aws_byte_buf_append_dynamic(&nonce_buf, &instance_nonce);
+ aws_byte_buf_append_dynamic(&nonce_buf, &path_suffix);
+ aws_byte_buf_append_null_terminator(&nonce_buf);
+
+ struct aws_cross_process_lock *instance_lock = NULL;
+
+ errno = 0;
+ int fd = open((const char *)nonce_buf.buffer, O_CREAT | O_RDWR, 0666);
+ if (fd < 0) {
+ AWS_LOGF_DEBUG(
+ AWS_LS_COMMON_GENERAL,
+ "static: Lock file %s failed to open with errno %d",
+ (const char *)nonce_buf.buffer,
+ errno);
+
+ aws_translate_and_raise_io_error_or(errno, AWS_ERROR_MUTEX_FAILED);
+
+ if (aws_last_error() == AWS_ERROR_NO_PERMISSION) {
+ AWS_LOGF_DEBUG(
+ AWS_LS_COMMON_GENERAL,
+ "static: Lock file %s couldn't be opened due to file ownership permissions. Attempting to open as read "
+ "only",
+ (const char *)nonce_buf.buffer);
+
+ errno = 0;
+ fd = open((const char *)nonce_buf.buffer, O_RDONLY);
+
+ if (fd < 0) {
+ AWS_LOGF_ERROR(
+ AWS_LS_COMMON_GENERAL,
+ "static: Lock file %s failed to open with read-only permissions with errno %d",
+ (const char *)nonce_buf.buffer,
+ errno);
+ aws_translate_and_raise_io_error_or(errno, AWS_ERROR_MUTEX_FAILED);
+ goto cleanup;
+ }
+ } else {
+ AWS_LOGF_ERROR(
+ AWS_LS_COMMON_GENERAL,
+ "static: Lock file %s failed to open. The lock cannot be acquired.",
+ (const char *)nonce_buf.buffer);
+ goto cleanup;
+ }
+ }
+
+ if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
+ AWS_LOGF_TRACE(
+ AWS_LS_COMMON_GENERAL,
+ "static: Lock file %s already acquired by another instance",
+ (const char *)nonce_buf.buffer);
+ close(fd);
+ aws_raise_error(AWS_ERROR_MUTEX_CALLER_NOT_OWNER);
+ goto cleanup;
+ }
+
+ instance_lock = aws_mem_calloc(allocator, 1, sizeof(struct aws_cross_process_lock));
+ instance_lock->locked_fd = fd;
+ instance_lock->allocator = allocator;
+
+ AWS_LOGF_TRACE(
+ AWS_LS_COMMON_GENERAL,
+ "static: Lock file %s acquired by this instance with fd %d",
+ (const char *)nonce_buf.buffer,
+ fd);
+
+cleanup:
+ aws_byte_buf_clean_up(&nonce_buf);
+
+ return instance_lock;
+}
+
+void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) {
+ if (instance_lock) {
+ flock(instance_lock->locked_fd, LOCK_UN);
+ close(instance_lock->locked_fd);
+ AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock file released for fd %d", instance_lock->locked_fd);
+ aws_mem_release(instance_lock->allocator, instance_lock);
+ }
+}
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/device_random.c b/contrib/restricted/aws/aws-c-common/source/posix/device_random.c
index 23ab1cd7b44..53f063ddf7b 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/device_random.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/device_random.c
@@ -51,7 +51,8 @@ int aws_device_random_buffer_append(struct aws_byte_buf *output, size_t n) {
/* read() can fail if N is too large (e.g. x64 macos fails if N > INT32_MAX),
* so work in reasonably sized chunks. */
while (n > 0) {
- size_t capped_n = aws_min_size(n, 1024 * 1024 * 1024 * 1 /* 1GiB */);
+ size_t capped_n = aws_min_size(
+ n, 1024 * 1024 * 1024 * 1 /* 1GiB */); /* NOLINT(bugprone-implicit-widening-of-multiplication-result) */
ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, capped_n);
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/file.c b/contrib/restricted/aws/aws-c-common/source/posix/file.c
index f8dbbd7e125..86811295523 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/file.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/file.c
@@ -18,7 +18,7 @@ FILE *aws_fopen_safe(const struct aws_string *file_path, const struct aws_string
FILE *f = fopen(aws_string_c_str(file_path), aws_string_c_str(mode));
if (!f) {
int errno_cpy = errno; /* Always cache errno before potential side-effect */
- aws_translate_and_raise_io_error(errno_cpy);
+ aws_translate_and_raise_io_error_or(errno_cpy, AWS_ERROR_FILE_OPEN_FAILURE);
AWS_LOGF_ERROR(
AWS_LS_COMMON_IO,
"static: Failed to open file. path:'%s' mode:'%s' errno:%d aws-error:%d(%s)",
@@ -285,7 +285,7 @@ int aws_fseek(FILE *file, int64_t offset, int whence) {
int errno_value = errno; /* Always cache errno before potential side-effect */
if (result != 0) {
- return aws_translate_and_raise_io_error(errno_value);
+ return aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_STREAM_UNSEEKABLE);
}
return AWS_OP_SUCCESS;
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/mutex.c b/contrib/restricted/aws/aws-c-common/source/posix/mutex.c
index 2cbf2db66ce..b5c865700a5 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/mutex.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/mutex.c
@@ -23,9 +23,11 @@ int aws_mutex_init(struct aws_mutex *mutex) {
int return_code = AWS_OP_SUCCESS;
if (!err_code) {
- if ((err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)) ||
- (err_code = pthread_mutex_init(&mutex->mutex_handle, &attr))) {
-
+ err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
+ if (!err_code) {
+ err_code = pthread_mutex_init(&mutex->mutex_handle, &attr);
+ }
+ if (err_code) {
return_code = aws_private_convert_and_raise_error_code(err_code);
}
pthread_mutexattr_destroy(&attr);
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/system_info.c b/contrib/restricted/aws/aws-c-common/source/posix/system_info.c
index 54bb502d80a..b5a0edc87e9 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/system_info.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/system_info.c
@@ -50,10 +50,10 @@ size_t aws_system_info_processor_count(void) {
uint16_t aws_get_cpu_group_count(void) {
if (g_numa_num_configured_nodes_ptr) {
- return (uint16_t)g_numa_num_configured_nodes_ptr();
+ return aws_max_u16(1, (uint16_t)g_numa_num_configured_nodes_ptr());
}
- return 1u;
+ return 1U;
}
size_t aws_get_cpu_count_for_group(uint16_t group_idx) {
@@ -242,7 +242,7 @@ int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info *
if (function_len >= (sizeof(frame->function) - 1)) {
function_len = sizeof(frame->function) - 1;
}
- strncpy(frame->function, function_start, function_end - function_start);
+ strncpy(frame->function, function_start, function_len);
/* find base addr for library/exe */
Dl_info addr_info;
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/system_resource_utils.c b/contrib/restricted/aws/aws-c-common/source/posix/system_resource_utils.c
new file mode 100644
index 00000000000..68165072b2f
--- /dev/null
+++ b/contrib/restricted/aws/aws-c-common/source/posix/system_resource_utils.c
@@ -0,0 +1,32 @@
+/**
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+ * SPDX-License-Identifier: Apache-2.0.
+ */
+
+#include <aws/common/system_resource_util.h>
+
+#include <sys/resource.h>
+
+int aws_init_memory_usage_for_current_process(struct aws_memory_usage_stats *memory_usage) {
+ AWS_PRECONDITION(memory_usage);
+
+ AWS_ZERO_STRUCT(*memory_usage);
+ struct rusage usage;
+
+ if (getrusage(RUSAGE_SELF, &usage)) {
+ return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
+ }
+
+#if defined(AWS_OS_APPLE)
+ /*
+ * For some reason Apple switched to reporting this in bytes instead of KB
+ * around MacOS 10.6.
+ * Make it back to KB. Result might be slightly off due to rounding.
+ */
+ memory_usage->maxrss = usage.ru_maxrss / 1024;
+#else
+ memory_usage->maxrss = usage.ru_maxrss;
+#endif
+ memory_usage->page_faults = usage.ru_majflt;
+ return AWS_OP_SUCCESS;
+}
diff --git a/contrib/restricted/aws/aws-c-common/source/posix/thread.c b/contrib/restricted/aws/aws-c-common/source/posix/thread.c
index 57d48aa9c70..af7fac84cf9 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/thread.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/thread.c
@@ -4,7 +4,7 @@
*/
#if !defined(__MACH__)
-# define _GNU_SOURCE
+# define _GNU_SOURCE /* NOLINT(bugprone-reserved-identifier) */
#endif
#include <aws/common/clock.h>
@@ -296,9 +296,9 @@ int aws_thread_launch(
attr_return = pthread_attr_setaffinity_np(attributes_ptr, sizeof(cpuset), &cpuset);
if (attr_return) {
- AWS_LOGF_ERROR(
+ AWS_LOGF_WARN(
AWS_LS_COMMON_THREAD,
- "id=%p: pthread_attr_setaffinity_np() failed with %d.",
+ "id=%p: pthread_attr_setaffinity_np() failed with %d. Continuing without cpu affinity",
(void *)thread,
attr_return);
goto cleanup;
@@ -382,7 +382,20 @@ cleanup:
if (attr_return) {
s_thread_wrapper_destroy(wrapper);
-
+ if (options && options->cpu_id >= 0) {
+ /*
+ * `pthread_create` can fail with an `EINVAL` error or `EDEADLK` on freebasd if the `cpu_id` is
+ * restricted/invalid. Since the pinning to a particular `cpu_id` is supposed to be best-effort, try to
+ * launch a thread again without pinning to a specific cpu_id.
+ */
+ AWS_LOGF_INFO(
+ AWS_LS_COMMON_THREAD,
+ "id=%p: Attempting to launch the thread again without pinning to a cpu_id",
+ (void *)thread);
+ struct aws_thread_options new_options = *options;
+ new_options.cpu_id = -1;
+ return aws_thread_launch(thread, func, arg, &new_options);
+ }
switch (attr_return) {
case EINVAL:
return aws_raise_error(AWS_ERROR_THREAD_INVALID_SETTINGS);