summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-common/source/posix
diff options
context:
space:
mode:
authordakovalkov <[email protected]>2023-12-03 13:33:55 +0300
committerdakovalkov <[email protected]>2023-12-03 14:04:39 +0300
commit2a718325637e5302334b6d0a6430f63168f8dbb3 (patch)
tree64be81080b7df9ec1d86d053a0c394ae53fcf1fe /contrib/restricted/aws/aws-c-common/source/posix
parente0d94a470142d95c3007e9c5d80380994940664a (diff)
Update contrib/libs/aws-sdk-cpp to 1.11.37
Diffstat (limited to 'contrib/restricted/aws/aws-c-common/source/posix')
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/file.c102
-rw-r--r--contrib/restricted/aws/aws-c-common/source/posix/thread.c40
2 files changed, 100 insertions, 42 deletions
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 7c26ade8c36..e8b39c509c3 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/file.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/file.c
@@ -5,47 +5,39 @@
#include <aws/common/environment.h>
#include <aws/common/file.h>
+#include <aws/common/logging.h>
#include <aws/common/string.h>
#include <dirent.h>
#include <errno.h>
+#include <pwd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
FILE *aws_fopen_safe(const struct aws_string *file_path, const struct aws_string *mode) {
- return fopen(aws_string_c_str(file_path), aws_string_c_str(mode));
-}
-
-static int s_parse_and_raise_error(int errno_cpy) {
- if (errno_cpy == 0) {
- return AWS_OP_SUCCESS;
- }
-
- if (errno_cpy == ENOENT || errno_cpy == ENOTDIR) {
- return aws_raise_error(AWS_ERROR_FILE_INVALID_PATH);
- }
-
- if (errno_cpy == EMFILE || errno_cpy == ENFILE) {
- return aws_raise_error(AWS_ERROR_MAX_FDS_EXCEEDED);
- }
-
- if (errno_cpy == EACCES) {
- return aws_raise_error(AWS_ERROR_NO_PERMISSION);
+ 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_LOGF_ERROR(
+ AWS_LS_COMMON_IO,
+ "static: Failed to open file. path:'%s' mode:'%s' errno:%d aws-error:%d(%s)",
+ aws_string_c_str(file_path),
+ aws_string_c_str(mode),
+ errno_cpy,
+ aws_last_error(),
+ aws_error_name(aws_last_error()));
}
-
- if (errno_cpy == ENOTEMPTY) {
- return aws_raise_error(AWS_ERROR_DIRECTORY_NOT_EMPTY);
- }
-
- return aws_raise_error(AWS_ERROR_UNKNOWN);
+ return f;
}
int aws_directory_create(const struct aws_string *dir_path) {
int mkdir_ret = mkdir(aws_string_c_str(dir_path), S_IRWXU | S_IRWXG | S_IRWXO);
+ int errno_value = errno; /* Always cache errno before potential side-effect */
/** nobody cares if it already existed. */
- if (mkdir_ret != 0 && errno != EEXIST) {
- return s_parse_and_raise_error(errno);
+ if (mkdir_ret != 0 && errno_value != EEXIST) {
+ return aws_translate_and_raise_io_error(errno_value);
}
return AWS_OP_SUCCESS;
@@ -101,24 +93,27 @@ int aws_directory_delete(const struct aws_string *dir_path, bool recursive) {
}
int error_code = rmdir(aws_string_c_str(dir_path));
+ int errno_value = errno; /* Always cache errno before potential side-effect */
- return error_code == 0 ? AWS_OP_SUCCESS : s_parse_and_raise_error(errno);
+ return error_code == 0 ? AWS_OP_SUCCESS : aws_translate_and_raise_io_error(errno_value);
}
int aws_directory_or_file_move(const struct aws_string *from, const struct aws_string *to) {
int error_code = rename(aws_string_c_str(from), aws_string_c_str(to));
+ int errno_value = errno; /* Always cache errno before potential side-effect */
- return error_code == 0 ? AWS_OP_SUCCESS : s_parse_and_raise_error(errno);
+ return error_code == 0 ? AWS_OP_SUCCESS : aws_translate_and_raise_io_error(errno_value);
}
int aws_file_delete(const struct aws_string *file_path) {
int error_code = unlink(aws_string_c_str(file_path));
+ int errno_value = errno; /* Always cache errno before potential side-effect */
- if (!error_code || errno == ENOENT) {
+ if (!error_code || errno_value == ENOENT) {
return AWS_OP_SUCCESS;
}
- return s_parse_and_raise_error(errno);
+ return aws_translate_and_raise_io_error(errno_value);
}
int aws_directory_traverse(
@@ -128,9 +123,10 @@ int aws_directory_traverse(
aws_on_directory_entry *on_entry,
void *user_data) {
DIR *dir = opendir(aws_string_c_str(path));
+ int errno_value = errno; /* Always cache errno before potential side-effect */
if (!dir) {
- return s_parse_and_raise_error(errno);
+ return aws_translate_and_raise_io_error(errno_value);
}
struct aws_byte_cursor current_path = aws_byte_cursor_from_string(path);
@@ -227,13 +223,39 @@ AWS_STATIC_STRING_FROM_LITERAL(s_home_env_var, "HOME");
struct aws_string *aws_get_home_directory(struct aws_allocator *allocator) {
- /* ToDo: check getpwuid_r if environment check fails */
- struct aws_string *home_env_var_value = NULL;
- if (aws_get_environment_value(allocator, s_home_env_var, &home_env_var_value) == 0 && home_env_var_value != NULL) {
- return home_env_var_value;
+ /* First, check "HOME" environment variable.
+ * If it's set, then return it, even if it's an empty string. */
+ struct aws_string *home_value = NULL;
+ aws_get_environment_value(allocator, s_home_env_var, &home_value);
+ if (home_value != NULL) {
+ return home_value;
+ }
+
+ /* Next, check getpwuid_r().
+ * We need to allocate a tmp buffer to store the result strings,
+ * and the max possible size for this thing can be pretty big,
+ * so start with a reasonable allocation, and if that's not enough try something bigger. */
+ uid_t uid = getuid(); /* cannot fail */
+ struct passwd pwd;
+ struct passwd *result = NULL;
+ char *buf = NULL;
+ int status = ERANGE;
+ for (size_t bufsize = 1024; bufsize <= 16384 && status == ERANGE; bufsize *= 2) {
+ if (buf) {
+ aws_mem_release(allocator, buf);
+ }
+ buf = aws_mem_acquire(allocator, bufsize);
+ status = getpwuid_r(uid, &pwd, buf, bufsize, &result);
+ }
+
+ if (status == 0 && result != NULL && result->pw_dir != NULL) {
+ home_value = aws_string_new_from_c_str(allocator, result->pw_dir);
+ } else {
+ aws_raise_error(AWS_ERROR_GET_HOME_DIRECTORY_FAILED);
}
- return NULL;
+ aws_mem_release(allocator, buf);
+ return home_value;
}
bool aws_path_exists(const struct aws_string *path) {
@@ -251,10 +273,11 @@ int aws_fseek(FILE *file, int64_t offset, int whence) {
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}
int result = fseek(file, offset, whence);
-#endif /* AWS_HAVE_POSIX_LFS */
+#endif /* AWS_HAVE_POSIX_LFS */
+ int errno_value = errno; /* Always cache errno before potential side-effect */
if (result != 0) {
- return aws_translate_and_raise_io_error(errno);
+ return aws_translate_and_raise_io_error(errno_value);
}
return AWS_OP_SUCCESS;
@@ -270,7 +293,8 @@ int aws_file_get_length(FILE *file, int64_t *length) {
}
if (fstat(fd, &file_stats)) {
- return aws_translate_and_raise_io_error(errno);
+ int errno_value = errno; /* Always cache errno before potential side-effect */
+ return aws_translate_and_raise_io_error(errno_value);
}
*length = file_stats.st_size;
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 d17d859c3b1..57d48aa9c70 100644
--- a/contrib/restricted/aws/aws-c-common/source/posix/thread.c
+++ b/contrib/restricted/aws/aws-c-common/source/posix/thread.c
@@ -23,9 +23,11 @@
#include <time.h>
#include <unistd.h>
-#if defined(__FreeBSD__) || defined(__NETBSD__)
+#if defined(__FreeBSD__) || defined(__NetBSD__)
# include <pthread_np.h>
typedef cpuset_t cpu_set_t;
+#elif defined(__OpenBSD__)
+# include <pthread_np.h>
#endif
#if !defined(AWS_AFFINITY_METHOD)
@@ -128,6 +130,8 @@ static void s_set_thread_name(pthread_t thread_id, const char *name) {
pthread_setname_np(name);
#elif defined(AWS_PTHREAD_SETNAME_TAKES_2ARGS)
pthread_setname_np(thread_id, name);
+#elif defined(AWS_PTHREAD_SET_NAME_TAKES_2ARGS)
+ pthread_set_name_np(thread_id, name);
#elif defined(AWS_PTHREAD_SETNAME_TAKES_3ARGS)
pthread_setname_np(thread_id, name, NULL);
#else
@@ -165,8 +169,9 @@ static void *thread_fn(void *arg) {
* and makes sure the numa node of the cpu we launched this thread on is where memory gets allocated. However,
* we don't want to fail the application if this fails, so make the call, and ignore the result. */
long resp = g_set_mempolicy_ptr(AWS_MPOL_PREFERRED_ALIAS, NULL, 0);
+ int errno_value = errno; /* Always cache errno before potential side-effect */
if (resp) {
- AWS_LOGF_WARN(AWS_LS_COMMON_THREAD, "call to set_mempolicy() failed with errno %d", errno);
+ AWS_LOGF_WARN(AWS_LS_COMMON_THREAD, "call to set_mempolicy() failed with errno %d", errno_value);
}
}
wrapper.func(wrapper.arg);
@@ -274,7 +279,7 @@ int aws_thread_launch(
/* AFAIK you can't set thread affinity on apple platforms, and it doesn't really matter since all memory
* NUMA or not is setup in interleave mode.
- * Thread afinity is also not supported on Android systems, and honestly, if you're running android on a NUMA
+ * Thread affinity is also not supported on Android systems, and honestly, if you're running android on a NUMA
* configuration, you've got bigger problems. */
#if AWS_AFFINITY_METHOD == AWS_AFFINITY_METHOD_PTHREAD_ATTR
if (options->cpu_id >= 0) {
@@ -460,3 +465,32 @@ int aws_thread_current_at_exit(aws_thread_atexit_fn *callback, void *user_data)
tl_wrapper->atexit = cb;
return AWS_OP_SUCCESS;
}
+
+int aws_thread_current_name(struct aws_allocator *allocator, struct aws_string **out_name) {
+ return aws_thread_name(allocator, aws_thread_current_thread_id(), out_name);
+}
+
+#define THREAD_NAME_BUFFER_SIZE 256
+int aws_thread_name(struct aws_allocator *allocator, aws_thread_id_t thread_id, struct aws_string **out_name) {
+ *out_name = NULL;
+#if defined(AWS_PTHREAD_GETNAME_TAKES_2ARGS) || defined(AWS_PTHREAD_GETNAME_TAKES_3ARGS) || \
+ defined(AWS_PTHREAD_GET_NAME_TAKES_2_ARGS)
+ char name[THREAD_NAME_BUFFER_SIZE] = {0};
+# ifdef AWS_PTHREAD_GETNAME_TAKES_3ARGS
+ if (pthread_getname_np(thread_id, name, THREAD_NAME_BUFFER_SIZE)) {
+# elif AWS_PTHREAD_GETNAME_TAKES_2ARGS
+ if (pthread_getname_np(thread_id, name)) {
+# elif AWS_PTHREAD_GET_NAME_TAKES_2ARGS
+ if (pthread_get_name_np(thread_id, name)) {
+# endif
+
+ return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
+ }
+
+ *out_name = aws_string_new_from_c_str(allocator, name);
+ return AWS_OP_SUCCESS;
+#else
+
+ return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
+#endif
+}