Debian Codegraph @

crt/aws-c-common/source/windows/cross_process_lock.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <windows.h>
6
7 #include <aws/common/byte_buf.h>
8 #include <aws/common/cross_process_lock.h>
9 #include <aws/common/error.h>
10 #include <aws/common/logging.h>
11 #include <inttypes.h>
12
13 struct aws_cross_process_lock {
14 struct aws_allocator *allocator;
15 HANDLE mutex;
16 };
17
18 struct aws_cross_process_lock *aws_cross_process_lock_try_acquire(
19 struct aws_allocator *allocator,
20 struct aws_byte_cursor instance_nonce) {
21
22 /* validate we don't have a directory slash. */
23 struct aws_byte_cursor to_find = aws_byte_cursor_from_c_str("\\");
24 struct aws_byte_cursor found;
25 AWS_ZERO_STRUCT(found);
26 if (aws_byte_cursor_find_exact(&instance_nonce, &to_find, &found) != AWS_OP_ERR &&
27 aws_last_error() != AWS_ERROR_STRING_MATCH_NOT_FOUND) {
28 AWS_LOGF_ERROR(
29 AWS_LS_COMMON_GENERAL,
30 "static: Lock " PRInSTR " creation has illegal character \\",
31 AWS_BYTE_CURSOR_PRI(instance_nonce));
32 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
33 return NULL;
34 }
35
36 struct aws_cross_process_lock *instance_lock = NULL;
37
38 /* "Local\" prefix, per the docs, specifies user session scope (rather than "Global\" to the system). */
39 struct aws_byte_cursor path_prefix = aws_byte_cursor_from_c_str("Local\\aws_crt_cross_process_lock/");
40 struct aws_byte_buf nonce_buf;
41 aws_byte_buf_init_copy_from_cursor(&nonce_buf, allocator, path_prefix);
42 aws_byte_buf_append_dynamic(&nonce_buf, &instance_nonce);
43 aws_byte_buf_append_null_terminator(&nonce_buf);
44
45 HANDLE mutex = CreateMutexA(NULL, FALSE, (LPCSTR)nonce_buf.buffer);
46
47 if (!mutex) {
48 AWS_LOGF_WARN(
49 AWS_LS_COMMON_GENERAL,
50 "static: Lock %s creation failed with error %" PRIx32,
51 (const char *)nonce_buf.buffer,
52 GetLastError());
53 aws_translate_and_raise_io_error_or(GetLastError(), AWS_ERROR_MUTEX_FAILED);
54 goto cleanup;
55 }
56
57 /* from the docs:
58 * If the mutex is a named mutex and the object existed before this function call, the return value is a handle
59 * to the existing object, and the GetLastError function returns ERROR_ALREADY_EXISTS. */
60 if (GetLastError() == ERROR_ALREADY_EXISTS) {
61 AWS_LOGF_TRACE(
62 AWS_LS_COMMON_GENERAL,
63 "static: Lock %s is already acquired by another instance",
64 (const char *)nonce_buf.buffer);
65 CloseHandle(mutex);
66 aws_raise_error(AWS_ERROR_MUTEX_CALLER_NOT_OWNER);
67 goto cleanup;
68 }
69
70 instance_lock = aws_mem_calloc(allocator, 1, sizeof(struct aws_cross_process_lock));
71 instance_lock->mutex = mutex;
72 instance_lock->allocator = allocator;
73
74 AWS_LOGF_TRACE(
75 AWS_LS_COMMON_GENERAL,
76 "static: Lock %s acquired by this instance with HANDLE %p",
77 (const char *)nonce_buf.buffer,
78 (void *)mutex);
79
80 cleanup:
81
82 /* we could do this once above but then we'd lose logging for the buffer. */
83 aws_byte_buf_clean_up(&nonce_buf);
84 return instance_lock;
85 }
86
87 void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) {
88 if (instance_lock) {
89 CloseHandle(instance_lock->mutex);
90 AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock released for handle %p", (void *)instance_lock->mutex);
91 aws_mem_release(instance_lock->allocator, instance_lock);
92 }
93 }