| 1 |
|
| #include "shared.h" |
| 2 |
|
| #include "safe_posix.h" |
| 3 |
|
| |
| 4 |
|
| #include <stdlib.h> |
| 5 |
|
| #include <stdio.h> |
| 6 |
|
| #include <string.h> |
| 7 |
|
| #include <stdatomic.h> |
| 8 |
|
| #include <unistd.h> |
| 9 |
|
| #include <sys/mman.h> |
| 10 |
|
| #include <sys/stat.h> |
| 11 |
|
| #include <fcntl.h> |
| 12 |
|
| #include <errno.h> |
| 13 |
|
| #include <time.h> |
| 14 |
|
| |
| 15 |
|
| #define SHM_SIZE (sizeof(shm_sync_block)) |
| 16 |
|
| |
| 17 |
|
| #include "log.h" |
| 18 |
|
| |
| 19 |
0 |
| static uint64_t current_time_micros() { |
| 20 |
|
| struct timespec ts; |
| 21 |
0 |
| clock_gettime(CLOCK_MONOTONIC, &ts); |
| 22 |
0 |
| return (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000; |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
4 |
| int open_shared_memory(shm_mem_chunk* mem_chunk, const char *cb_name, size_t mem_size) { |
| 26 |
4 |
| int fd = -1; |
| 27 |
4 |
| void *mem = {0}; |
| 28 |
|
| |
| 29 |
4 |
▶ | if (cb_name[0] != '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb_name[0] != '/') { |
0 |
4 |
4 |
|
| 30 |
0 |
▶ | LOG_ERR("name must start with '/'"); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG_ERR("name must start with '/'"); |
0 |
0 |
0 |
|
| 31 |
0 |
| goto ERROR_BLOCK; |
| 32 |
|
| } |
| 33 |
4 |
▶ | if (strlen(cb_name) >= NAME_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strlen(cb_name) >= NAME_MAX) { |
0 |
4 |
4 |
|
| 34 |
0 |
▶ | LOG_ERR("name is too long"); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG_ERR("name is too long"); |
0 |
0 |
0 |
|
| 35 |
0 |
| goto ERROR_BLOCK; |
| 36 |
|
| } |
| 37 |
4 |
▶ | SAFE_POSIX_CALL_WITH_RES(fd, shm_open(cb_name, O_CREAT | O_RDWR , 0666), -1); |
Branch: 2 branches
| Branch | True | False | Total |
| SAFE_POSIX_CALL_WITH_RES(fd, shm_open(cb_name, O_CREAT | O_RDWR , 0666), -1); |
0 |
4 |
4 |
| SAFE_POSIX_CALL_WITH_RES(fd, shm_open(cb_name, O_CREAT | O_RDWR , 0666), -1); |
0 |
0 |
0 |
|
| 38 |
|
| struct stat st; |
| 39 |
|
| /* Race 2 fix: also ftruncate when the file exists but is smaller than |
| 40 |
|
| requested — this handles the resize (grow) case in reallocate(). */ |
| 41 |
4 |
▶ | if (fstat(fd, &st) == 0 && st.st_size < (off_t)mem_size) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fstat(fd, &st) == 0 && st.st_size < (off_t)mem_size) { |
4 |
0 |
4 |
| if (fstat(fd, &st) == 0 && st.st_size < (off_t)mem_size) { |
4 |
0 |
4 |
|
| 42 |
4 |
▶ | SAFE_POSIX_CALL(ftruncate(fd, mem_size), -1); |
Branch: 2 branches
| Branch | True | False | Total |
| SAFE_POSIX_CALL(ftruncate(fd, mem_size), -1); |
0 |
4 |
4 |
| SAFE_POSIX_CALL(ftruncate(fd, mem_size), -1); |
0 |
0 |
0 |
|
| 43 |
|
| } |
| 44 |
4 |
▶ | SAFE_POSIX_CALL_WITH_RES(mem, mmap(0, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| SAFE_POSIX_CALL_WITH_RES(mem, mmap(0, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0), 0); |
0 |
4 |
4 |
| SAFE_POSIX_CALL_WITH_RES(mem, mmap(0, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0), 0); |
0 |
0 |
0 |
|
| 45 |
4 |
| close(fd); |
| 46 |
4 |
| fd = -1; |
| 47 |
4 |
| mem_chunk->data = mem; |
| 48 |
4 |
| mem_chunk->size = mem_size; |
| 49 |
4 |
| return 0; |
| 50 |
0 |
| ERROR_BLOCK: |
| 51 |
0 |
▶ | if (fd != -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd != -1) { |
0 |
0 |
0 |
|
| 52 |
0 |
| close(fd); |
| 53 |
|
| } |
| 54 |
0 |
▶ | if (mem) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mem) { |
0 |
0 |
0 |
|
| 55 |
0 |
| munmap(mem, mem_size); |
| 56 |
|
| } |
| 57 |
0 |
| return -1; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
2 |
| int try_lock_shm_for_init(shm_control_block* blk, uint64_t timeout_ms) { |
| 61 |
2 |
▶ | if (!blk) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!blk) { |
0 |
2 |
2 |
|
| 62 |
0 |
| return -1; |
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
|
| /* Try to claim initialization: SHM_UNINIT(0) -> SHM_INIT_IN_PROGRESS(1) */ |
| 66 |
2 |
| int expected = SHM_UNINIT; |
| 67 |
2 |
▶ | if (atomic_compare_exchange_strong_explicit( |
Branch: 1 branch
| Branch | True | False | Total |
| if (atomic_compare_exchange_strong_explicit( |
2 |
0 |
2 |
|
| 68 |
|
| &blk->init_state, &expected, |
| 69 |
|
| SHM_INIT_IN_PROGRESS, |
| 70 |
|
| memory_order_acq_rel, memory_order_acquire)) { |
| 71 |
2 |
| return 1; /* won the race — caller must initialize */ |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| /* Lost the race: spin until the winner reaches SHM_INIT_DONE or timeout */ |
| 75 |
0 |
| uint64_t deadline = current_time_micros() + timeout_ms * 1000; |
| 76 |
0 |
| while (atomic_load_explicit(&blk->init_state, memory_order_acquire) |
| 77 |
0 |
▶ | == SHM_INIT_IN_PROGRESS) { |
Branch: 1 branch
| Branch | True | False | Total |
| == SHM_INIT_IN_PROGRESS) { |
0 |
0 |
0 |
|
| 78 |
0 |
▶ | if (current_time_micros() > deadline) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (current_time_micros() > deadline) { |
0 |
0 |
0 |
|
| 79 |
0 |
▶ | LOG("try_lock_shm_for_init: timed out waiting for init\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG("try_lock_shm_for_init: timed out waiting for init\n"); |
0 |
0 |
0 |
|
| 80 |
0 |
| return -1; |
| 81 |
|
| } |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
0 |
| return (atomic_load_explicit(&blk->init_state, memory_order_acquire) |
| 85 |
0 |
▶ | == SHM_INIT_DONE) ? 0 : -1; |
Branch: 1 branch
| Branch | True | False | Total |
| == SHM_INIT_DONE) ? 0 : -1; |
0 |
0 |
0 |
|
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
0 |
| int open_table_in_shm(process *pp) { |
| 89 |
0 |
| shm_control_block* blk = pp->cntrl_blk.data; |
| 90 |
0 |
▶ | if (pp->data_blk.data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pp->data_blk.data) { |
0 |
0 |
0 |
|
| 91 |
0 |
| munmap(pp->data_blk.data, pp->data_blk.size); |
| 92 |
|
| } |
| 93 |
0 |
| int res = open_shared_memory(&pp->data_blk, blk->name, blk->data_size); |
| 94 |
0 |
▶ | if (res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res) { |
0 |
0 |
0 |
|
| 95 |
0 |
▶ | LOG("init() open_shared_memory bad_ret_code %d, data block\n", res ); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG("init() open_shared_memory bad_ret_code %d, data block\n", res ); |
0 |
0 |
0 |
|
| 96 |
0 |
| goto ERROR_BLOCK; |
| 97 |
|
| } |
| 98 |
0 |
| void* data_mem = pp->data_blk.data; |
| 99 |
0 |
▶ | if (data_mem == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (data_mem == NULL) { |
0 |
0 |
0 |
|
| 100 |
0 |
▶ | LOG("init() open_shared_memory, 0 link for data block\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG("init() open_shared_memory, 0 link for data block\n"); |
0 |
0 |
0 |
|
| 101 |
0 |
| goto ERROR_BLOCK; |
| 102 |
|
| } |
| 103 |
0 |
| pp->table.sizes = (HashDataSizes*) data_mem; |
| 104 |
|
| |
| 105 |
0 |
▶ | LOG("init() open existed shm, cap:{index: %u, data: %u}, sizes:{index: %u, data: %u}\n", |
Branch: 1 branch
| Branch | True | False | Total |
| LOG("init() open existed shm, cap:{index: %u, data: %u}, sizes:{index: %u, data: %u}\n", |
0 |
0 |
0 |
|
| 106 |
|
| ((Sizes) pp->table.sizes->capacities).index, ((Sizes) pp->table.sizes->capacities).data, |
| 107 |
|
| ((Sizes) pp->table.sizes->sizes).index, ((Sizes) pp->table.sizes->sizes).data); |
| 108 |
0 |
| pp->table.index_entries = OFFSET(pp->table.sizes, pp->table.index_entries); |
| 109 |
0 |
| pp->table.data_entries = OFFSETA(pp->table.index_entries, ((Sizes)pp->table.sizes->capacities).index, pp->table.data_entries); |
| 110 |
|
| |
| 111 |
0 |
| return 0; |
| 112 |
0 |
| ERROR_BLOCK: |
| 113 |
0 |
▶ | if (pp->data_blk.data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pp->data_blk.data) { |
0 |
0 |
0 |
|
| 114 |
0 |
| munmap(pp->data_blk.data, pp->data_blk.size); |
| 115 |
0 |
| pp->data_blk.data = 0; pp->data_blk.size = 0; |
| 116 |
|
| } |
| 117 |
0 |
| return -1; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
0 |
| int reallocate (shm_control_block* cntrl_blk, shm_mem_chunk* data_blk, HashTable* table, Sizes new_capacities) { |
| 121 |
|
| int res; |
| 122 |
0 |
| size_t mem_size = ht_table_size(new_capacities.index, new_capacities.data); |
| 123 |
0 |
| HashTable tmp = {0}; |
| 124 |
0 |
▶ | if (ht_copy_to_local_memory(table, &tmp) != OA_HASH_OK) {; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ht_copy_to_local_memory(table, &tmp) != OA_HASH_OK) {; |
0 |
0 |
0 |
|
| 125 |
0 |
▶ | LOG("reallocate() failed to copy hash table to local memory\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG("reallocate() failed to copy hash table to local memory\n"); |
0 |
0 |
0 |
|
| 126 |
0 |
| return -1; |
| 127 |
|
| } |
| 128 |
0 |
| ht_clear_table(table); |
| 129 |
0 |
| munmap(data_blk->data, data_blk->size); |
| 130 |
0 |
| data_blk->data = 0; data_blk->size = 0; |
| 131 |
0 |
| res = open_shared_memory(data_blk, cntrl_blk->name, mem_size); |
| 132 |
0 |
▶ | if (res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res) { |
0 |
0 |
0 |
|
| 133 |
0 |
▶ | LOG("reallocate() failed to open shared memory for data block\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG("reallocate() failed to open shared memory for data block\n"); |
0 |
0 |
0 |
|
| 134 |
0 |
| free(tmp.sizes); |
| 135 |
0 |
| return -2; |
| 136 |
|
| } |
| 137 |
0 |
| ht_init_table(table, new_capacities, data_blk->data); |
| 138 |
0 |
| res = ht_copy(table, &tmp); |
| 139 |
0 |
▶ | if (res) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (res) { |
0 |
0 |
0 |
|
| 140 |
0 |
▶ | LOG("reallocate() failed to copy hash table to shared memory\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| LOG("reallocate() failed to copy hash table to shared memory\n"); |
0 |
0 |
0 |
|
| 141 |
0 |
| free(tmp.sizes); |
| 142 |
0 |
| return res; |
| 143 |
|
| } |
| 144 |
0 |
| free(tmp.sizes); |
| 145 |
|
| |
| 146 |
0 |
| atomic_store_explicit(&cntrl_blk->data_size, data_blk->size, memory_order_release); |
| 147 |
0 |
| return 0; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| |
| 151 |
2 |
| int init_shared_mutex(pthread_mutex_t *mutex) { |
| 152 |
|
| pthread_mutexattr_t a; |
| 153 |
2 |
| pthread_mutexattr_init(&a); |
| 154 |
2 |
| pthread_mutexattr_setpshared(&a, PTHREAD_PROCESS_SHARED); |
| 155 |
|
| #ifdef PTHREAD_MUTEX_ROBUST |
| 156 |
|
| pthread_mutexattr_setrobust(&a, PTHREAD_MUTEX_ROBUST); |
| 157 |
|
| #endif |
| 158 |
2 |
| int rc = pthread_mutex_init(mutex, &a); |
| 159 |
2 |
| pthread_mutexattr_destroy(&a); |
| 160 |
2 |
| return rc; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
8 |
| int shared_lock(pthread_mutex_t *mutex) { |
| 164 |
8 |
| int rc = pthread_mutex_lock(mutex); |
| 165 |
|
| #ifdef PTHREAD_MUTEX_ROBUST |
| 166 |
|
| if (rc == EOWNERDEAD) { |
| 167 |
|
| // Previous owner died while holding the lock. |
| 168 |
|
| // Repair shared state to a consistent point here. |
| 169 |
|
| // ... |
| 170 |
|
| LOG_ERR("shared_lock: EOWNERDEAD, recovering"); |
| 171 |
|
| // Tell the system the state is now consistent: |
| 172 |
|
| pthread_mutex_consistent(mutex); |
| 173 |
|
| return 0; |
| 174 |
|
| } |
| 175 |
|
| #endif |
| 176 |
8 |
| return rc; // 0 on success; EAGAIN can also occur for robust mutexes |
| 177 |
|
| } |
| 178 |
|
| |