Debian Codegraph @

crt/aws-c-common/source/posix/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 <aws/common/cross_process_lock.h>
6
7 #include <aws/common/byte_buf.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <sys/file.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 #include <aws/common/error.h>
15 #include <aws/common/file.h>
16 #include <aws/common/logging.h>
17
18 struct aws_cross_process_lock {
19 struct aws_allocator *allocator;
20 int locked_fd;
21 };
22
23 struct aws_cross_process_lock *aws_cross_process_lock_try_acquire(
24 struct aws_allocator *allocator,
25 struct aws_byte_cursor instance_nonce) {
26
27 /* validate we don't have a directory slash. */
28 struct aws_byte_cursor to_find = aws_byte_cursor_from_c_str("/");
29 struct aws_byte_cursor found;
30 AWS_ZERO_STRUCT(found);
31 if (aws_byte_cursor_find_exact(&instance_nonce, &to_find, &found) != AWS_OP_ERR &&
32 aws_last_error() != AWS_ERROR_STRING_MATCH_NOT_FOUND) {
33 AWS_LOGF_ERROR(
34 AWS_LS_COMMON_GENERAL,
35 "static: Lock " PRInSTR "creation has illegal character /",
36 AWS_BYTE_CURSOR_PRI(instance_nonce));
37 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
38 return NULL;
39 }
40
41 /*
42 * The unix standard says /tmp has to be there and be writable. However, while it may be tempting to just use the
43 * /tmp/ directory, it often has the sticky bit set which would prevent a subprocess from being able to call open
44 * with create on the file. The solution is simple, just write it to a subdirectory inside
45 * /tmp and override umask via. chmod of 0777.
46 */
47 struct aws_byte_cursor path_prefix = aws_byte_cursor_from_c_str("/tmp/aws_crt_cross_process_lock/");
48 struct aws_string *path_to_create = aws_string_new_from_cursor(allocator, &path_prefix);
49
50 /* It's probably there already and we don't care if it is. */
51 if (!aws_directory_exists(path_to_create)) {
52 /* if this call fails just let it fail on open below. */
53 aws_directory_create(path_to_create);
54 /* bypass umask by setting the perms we actually requested */
55 chmod(aws_string_c_str(path_to_create), S_IRWXU | S_IRWXG | S_IRWXO);
56 }
57 aws_string_destroy(path_to_create);
58
59 struct aws_byte_cursor path_suffix = aws_byte_cursor_from_c_str(".lock");
60
61 struct aws_byte_buf nonce_buf;
62 aws_byte_buf_init_copy_from_cursor(&nonce_buf, allocator, path_prefix);
63 aws_byte_buf_append_dynamic(&nonce_buf, &instance_nonce);
64 aws_byte_buf_append_dynamic(&nonce_buf, &path_suffix);
65 aws_byte_buf_append_null_terminator(&nonce_buf);
66
67 struct aws_cross_process_lock *instance_lock = NULL;
68
69 errno = 0;
70 int fd = open((const char *)nonce_buf.buffer, O_CREAT | O_RDWR, 0666);
71 if (fd < 0) {
72 AWS_LOGF_DEBUG(
73 AWS_LS_COMMON_GENERAL,
74 "static: Lock file %s failed to open with errno %d",
75 (const char *)nonce_buf.buffer,
76 errno);
77
78 aws_translate_and_raise_io_error_or(errno, AWS_ERROR_MUTEX_FAILED);
79
80 if (aws_last_error() == AWS_ERROR_NO_PERMISSION) {
81 AWS_LOGF_DEBUG(
82 AWS_LS_COMMON_GENERAL,
83 "static: Lock file %s couldn't be opened due to file ownership permissions. Attempting to open as read "
84 "only",
85 (const char *)nonce_buf.buffer);
86
87 errno = 0;
88 fd = open((const char *)nonce_buf.buffer, O_RDONLY);
89
90 if (fd < 0) {
91 AWS_LOGF_ERROR(
92 AWS_LS_COMMON_GENERAL,
93 "static: Lock file %s failed to open with read-only permissions with errno %d",
94 (const char *)nonce_buf.buffer,
95 errno);
96 aws_translate_and_raise_io_error_or(errno, AWS_ERROR_MUTEX_FAILED);
97 goto cleanup;
98 }
99 } else {
100 AWS_LOGF_ERROR(
101 AWS_LS_COMMON_GENERAL,
102 "static: Lock file %s failed to open. The lock cannot be acquired.",
103 (const char *)nonce_buf.buffer);
104 goto cleanup;
105 }
106 }
107
108 if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
109 AWS_LOGF_TRACE(
110 AWS_LS_COMMON_GENERAL,
111 "static: Lock file %s already acquired by another instance",
112 (const char *)nonce_buf.buffer);
113 close(fd);
114 aws_raise_error(AWS_ERROR_MUTEX_CALLER_NOT_OWNER);
115 goto cleanup;
116 }
117
118 instance_lock = aws_mem_calloc(allocator, 1, sizeof(struct aws_cross_process_lock));
119 instance_lock->locked_fd = fd;
120 instance_lock->allocator = allocator;
121
122 AWS_LOGF_TRACE(
123 AWS_LS_COMMON_GENERAL,
124 "static: Lock file %s acquired by this instance with fd %d",
125 (const char *)nonce_buf.buffer,
126 fd);
127
128 cleanup:
129 aws_byte_buf_clean_up(&nonce_buf);
130
131 return instance_lock;
132 }
133
134 void aws_cross_process_lock_release(struct aws_cross_process_lock *instance_lock) {
135 if (instance_lock) {
136 close(instance_lock->locked_fd);
137 AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "static: Lock file released for fd %d", instance_lock->locked_fd);
138 aws_mem_release(instance_lock->allocator, instance_lock);
139 }
140 }