| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | #include "gate/platforms.h" | ||
| 29 | |||
| 30 | #if defined(GATE_SYS_POSIX) && !defined(GATE_SYS_BEOS) | ||
| 31 | |||
| 32 | #define __USE_GNU | ||
| 33 | #include <unistd.h> | ||
| 34 | #include <sys/time.h> | ||
| 35 | #include <time.h> | ||
| 36 | #if defined(GATE_SYS_ANDROID) || defined(GATE_SYS_BEOS) | ||
| 37 | # define GATE_SYS_USE_POSIX_SEMAPHORE | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #if defined(HAVE_SEMTIMEDOP) | ||
| 41 | # define _GNU_SOURCE | ||
| 42 | # include <sys/sem.h> | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #include <sys/ipc.h> | ||
| 46 | #include <sys/types.h> | ||
| 47 | #include <errno.h> | ||
| 48 | #include <pthread.h> | ||
| 49 | #include <sched.h> | ||
| 50 | #if defined(GATE_SYS_USE_POSIX_SEMAPHORE) | ||
| 51 | # include <fcntl.h> | ||
| 52 | # include <semaphore.h> | ||
| 53 | #else | ||
| 54 | # include <sys/shm.h> | ||
| 55 | # include <sys/sem.h> | ||
| 56 | #endif | ||
| 57 | |||
| 58 | #if defined(GATE_SYS_DARWIN) | ||
| 59 | # include <mach/clock.h> | ||
| 60 | # include <mach/mach.h> | ||
| 61 | #endif | ||
| 62 | |||
| 63 | #if defined(GATE_SYS_WASM_WASI) | ||
| 64 | # define GATE_POSIX_NO_PROCS 1 /* no-process-features*/ | ||
| 65 | #else | ||
| 66 | #endif | ||
| 67 | |||
| 68 | #include <stdlib.h> | ||
| 69 | #include <stdarg.h> | ||
| 70 | #include <string.h> | ||
| 71 | #include <sys/stat.h> | ||
| 72 | #include <sys/ioctl.h> | ||
| 73 | #if !defined(GATE_POSIX_NO_PROCS) | ||
| 74 | #include <signal.h> | ||
| 75 | #include <sys/wait.h> | ||
| 76 | #endif | ||
| 77 | #include <sys/file.h> | ||
| 78 | #include <fcntl.h> | ||
| 79 | #include <pwd.h> | ||
| 80 | #include <grp.h> | ||
| 81 | #include <signal.h> | ||
| 82 | #include <sys/select.h> | ||
| 83 | #include <dlfcn.h> | ||
| 84 | |||
| 85 | #include "gate/results.h" | ||
| 86 | #include "gate/atomics.h" | ||
| 87 | #include "gate/debugging.h" | ||
| 88 | #include "gate/strings.h" | ||
| 89 | |||
| 90 | #if defined(GATE_SYS_ANDROID) || defined(GATE_SYS_WASM_WASI) | ||
| 91 | # define GATE_SYS_POSIX_HAS_FLOCK | ||
| 92 | #else | ||
| 93 | # define GATE_SYS_POSIX_HAS_FLOCK | ||
| 94 | # define GATE_SYS_POSIX_HAS_LOCKF | ||
| 95 | #endif | ||
| 96 | |||
| 97 | static gate_atomic_int_t global_platform_mutex_usable = 0; | ||
| 98 | static gate_atomic_lock_t global_platform_mutex_init_lock = GATE_ATOMIC_LOCK_INIT; | ||
| 99 | |||
| 100 | static gate_atomic_flag_t global_platform_mutex_init_done = GATE_ATOMIC_FLAG_INIT; | ||
| 101 | static pthread_mutex_t global_platform_mutex; | ||
| 102 | |||
| 103 | 684 | static gate_result_t global_platform_mutex_init() | |
| 104 | { | ||
| 105 | 684 | gate_result_t ret = GATE_RESULT_OK; | |
| 106 | 684 | const gate_int32_t init_state = gate_atomic_int_get(&global_platform_mutex_usable); | |
| 107 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 640 times.
|
684 | if (0 == init_state) |
| 108 | { | ||
| 109 | 44 | gate_atomic_lock_acquire(&global_platform_mutex_init_lock); | |
| 110 | { | ||
| 111 |
1/2✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
|
44 | if (!gate_atomic_flag_set(&global_platform_mutex_init_done)) |
| 112 | { | ||
| 113 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
|
44 | if (0 != pthread_mutex_init(&global_platform_mutex, NULL)) |
| 114 | { | ||
| 115 | ✗ | ret = GATE_RESULT_FAILED; | |
| 116 | ✗ | gate_atomic_flag_clear(&global_platform_mutex_init_done); | |
| 117 | } | ||
| 118 | else | ||
| 119 | { | ||
| 120 | 44 | gate_atomic_int_set(&global_platform_mutex_usable, 1); | |
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | 44 | gate_atomic_lock_release(&global_platform_mutex_init_lock); | |
| 125 | } | ||
| 126 | 684 | return ret; | |
| 127 | } | ||
| 128 | |||
| 129 | 44 | gate_result_t gate_platform_init(void) | |
| 130 | { | ||
| 131 | 44 | gate_result_t ret = GATE_RESULT_OK; | |
| 132 | |||
| 133 | 44 | global_platform_mutex_init(); | |
| 134 | |||
| 135 | 44 | return ret; | |
| 136 | } | ||
| 137 | |||
| 138 | 2 | void gate_platform_exit(int exit_code) | |
| 139 | { | ||
| 140 | 2 | exit(exit_code); | |
| 141 | } | ||
| 142 | |||
| 143 | 21 | void gate_platform_atexit(void(*func)(void)) | |
| 144 | { | ||
| 145 | 21 | atexit(func); | |
| 146 | 21 | } | |
| 147 | |||
| 148 | 320 | gate_result_t gate_platform_lock() | |
| 149 | { | ||
| 150 | 320 | gate_result_t ret = global_platform_mutex_init(); | |
| 151 |
1/2✓ Branch 0 taken 320 times.
✗ Branch 1 not taken.
|
320 | if (GATE_SUCCEEDED(ret)) |
| 152 | { | ||
| 153 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 320 times.
|
320 | if (0 != pthread_mutex_lock(&global_platform_mutex)) |
| 154 | { | ||
| 155 | ✗ | ret = GATE_RESULT_FAILED; | |
| 156 | } | ||
| 157 | } | ||
| 158 | 320 | return ret; | |
| 159 | } | ||
| 160 | 320 | gate_result_t gate_platform_unlock() | |
| 161 | { | ||
| 162 | 320 | gate_result_t ret = global_platform_mutex_init(); | |
| 163 |
1/2✓ Branch 0 taken 320 times.
✗ Branch 1 not taken.
|
320 | if (GATE_SUCCEEDED(ret)) |
| 164 | { | ||
| 165 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 320 times.
|
320 | if (0 != pthread_mutex_unlock(&global_platform_mutex)) |
| 166 | { | ||
| 167 | ✗ | ret = GATE_RESULT_FAILED; | |
| 168 | } | ||
| 169 | } | ||
| 170 | 320 | return ret; | |
| 171 | } | ||
| 172 | 1734026 | gate_int32_t gate_platform_get_last_error() | |
| 173 | { | ||
| 174 | 1734026 | return (gate_int32_t)errno; | |
| 175 | } | ||
| 176 | ✗ | gate_result_t gate_platform_set_last_error(gate_int32_t platform_error_code) | |
| 177 | { | ||
| 178 | ✗ | gate_result_t ret = GATE_RESULT_OK; | |
| 179 | ✗ | errno = (int)platform_error_code; | |
| 180 | ✗ | return ret; | |
| 181 | } | ||
| 182 | |||
| 183 | 238 | static gate_result_t gate_posix_convert_errno(int error_code) | |
| 184 | { | ||
| 185 | 238 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 186 | |||
| 187 |
6/75✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 times.
✓ Branch 28 taken 2 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✓ Branch 43 taken 228 times.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✓ Branch 61 taken 4 times.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✗ Branch 73 not taken.
✗ Branch 74 not taken.
|
238 | switch (error_code) |
| 188 | { | ||
| 189 | 1 | case 0: ret = GATE_RESULT_OK; break; | |
| 190 | ✗ | case E2BIG: ret = GATE_RESULT_OUTOFBOUNDS; break; | |
| 191 | ✗ | case EACCES: ret = GATE_RESULT_ACCESSDENIED; break; | |
| 192 | ✗ | case EADDRINUSE: ret = GATE_RESULT_NOTREADY; break; | |
| 193 | ✗ | case EADDRNOTAVAIL: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 194 | ✗ | case EAFNOSUPPORT: ret = GATE_RESULT_NOTSUPPORTED; break; | |
| 195 | /*case EWOULDBLOCK: ret = GATE_RESULT_BLOCKED; break;*/ | ||
| 196 | ✗ | case EAGAIN: ret = GATE_RESULT_BLOCKED; break; | |
| 197 | ✗ | case EALREADY: ret = GATE_RESULT_ALREADYEXISTS; break; | |
| 198 | 2 | case EBADF: ret = GATE_RESULT_INVALIDDATA; break; | |
| 199 | ✗ | case EBADMSG: ret = GATE_RESULT_INVALIDCONTENT; break; | |
| 200 | ✗ | case EBUSY: ret = GATE_RESULT_NOTREADY; break; | |
| 201 | ✗ | case ECANCELED: ret = GATE_RESULT_CANCELED; break; | |
| 202 | ✗ | case ECHILD: ret = GATE_RESULT_EXECUTIONFAILED; break; | |
| 203 | ✗ | case ECONNABORTED: ret = GATE_RESULT_CANCELED; break; | |
| 204 | ✗ | case ECONNREFUSED: ret = GATE_RESULT_ACCESSDENIED; break; | |
| 205 | ✗ | case ECONNRESET: ret = GATE_RESULT_INVALIDSTATE; break; | |
| 206 | ✗ | case EDEADLK: ret = GATE_RESULT_LOCKED; break; | |
| 207 | ✗ | case EDESTADDRREQ: ret = GATE_RESULT_INVALIDDATA; break; | |
| 208 | ✗ | case EDOM: ret = GATE_RESULT_ARITHMETICERROR; break; | |
| 209 | /*case EDQUOT: ret = GATE_RESULT_FAILED; break;*/ | ||
| 210 | ✗ | case EEXIST: ret = GATE_RESULT_ALREADYEXISTS; break; | |
| 211 | ✗ | case EFAULT: ret = GATE_RESULT_BADADDRESS; break; | |
| 212 | ✗ | case EFBIG: ret = GATE_RESULT_OUTOFBOUNDS; break; | |
| 213 | ✗ | case EHOSTUNREACH: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 214 | ✗ | case EIDRM: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 215 | ✗ | case EILSEQ: ret = GATE_RESULT_INVALIDDATA; break; | |
| 216 | ✗ | case EINPROGRESS: ret = GATE_RESULT_LOCKED; break; | |
| 217 | ✗ | case EINTR: ret = GATE_RESULT_EXECUTIONINTERRUPTED; break; | |
| 218 | 1 | case EINVAL: ret = GATE_RESULT_INVALIDARG; break; | |
| 219 | 2 | case EIO: ret = GATE_RESULT_INVALIDDATA; break; | |
| 220 | ✗ | case EISCONN: ret = GATE_RESULT_INVALIDSTATE; break; | |
| 221 | ✗ | case EISDIR: ret = GATE_RESULT_INVALIDSTATE; break; | |
| 222 | ✗ | case ELOOP: ret = GATE_RESULT_OUTOFRESOURCES; break; | |
| 223 | ✗ | case EMFILE: ret = GATE_RESULT_OUTOFRESOURCES; break; | |
| 224 | ✗ | case EMLINK: ret = GATE_RESULT_OUTOFRESOURCES; break; | |
| 225 | ✗ | case EMSGSIZE: ret = GATE_RESULT_OUTOFBOUNDS; break; | |
| 226 | /*case EMULTIHOP: ret = GATE_RESULT_FAILED; break;*/ | ||
| 227 | ✗ | case ENAMETOOLONG: ret = GATE_RESULT_OUTOFBOUNDS; break; | |
| 228 | ✗ | case ENETDOWN: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 229 | ✗ | case ENETRESET: ret = GATE_RESULT_CANCELED; break; | |
| 230 | ✗ | case ENETUNREACH: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 231 | ✗ | case ENFILE: ret = GATE_RESULT_OUTOFRESOURCES; break; | |
| 232 | ✗ | case ENOBUFS: ret = GATE_RESULT_OUTOFMEMORY; break; | |
| 233 | #ifdef ENODATA | ||
| 234 | ✗ | case ENODATA: ret = GATE_RESULT_INVALIDCONTENT; break; | |
| 235 | #endif | ||
| 236 | ✗ | case ENODEV: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 237 | 228 | case ENOENT: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 238 | ✗ | case ENOEXEC: ret = GATE_RESULT_INVALIDHEADER; break; | |
| 239 | ✗ | case ENOLCK: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 240 | /*case ENOLINK: ret = GATE_RESULT_; break;*/ | ||
| 241 | ✗ | case ENOMEM: ret = GATE_RESULT_OUTOFMEMORY; break; | |
| 242 | ✗ | case ENOMSG: ret = GATE_RESULT_OUTOFRESOURCES; break; | |
| 243 | ✗ | case ENOPROTOOPT: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 244 | ✗ | case ENOSPC: ret = GATE_RESULT_OUTOFRESOURCES; break; | |
| 245 | #ifdef ENOSR | ||
| 246 | ✗ | case ENOSR: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 247 | #endif | ||
| 248 | #ifdef ENOSTR | ||
| 249 | ✗ | case ENOSTR: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 250 | #endif | ||
| 251 | ✗ | case ENOSYS: ret = GATE_RESULT_NOTSUPPORTED; break; | |
| 252 | ✗ | case ENOTCONN: ret = GATE_RESULT_INVALIDSTATE; break; | |
| 253 | ✗ | case ENOTDIR: ret = GATE_RESULT_INVALIDSTATE; break; | |
| 254 | ✗ | case ENOTEMPTY: ret = GATE_RESULT_INVALIDSTATE; break; | |
| 255 | ✗ | case ENOTSOCK: ret = GATE_RESULT_INCORRECTTYPE; break; | |
| 256 | ✗ | case ENOTSUP: ret = GATE_RESULT_NOTSUPPORTED; break; | |
| 257 | ✗ | case ENOTTY: ret = GATE_RESULT_INCORRECTTYPE; break; | |
| 258 | ✗ | case ENXIO: ret = GATE_RESULT_NOTAVAILABLE; break; | |
| 259 | ✗ | case EOVERFLOW: ret = GATE_RESULT_OVERFLOW; break; | |
| 260 | 4 | case EPERM: ret = GATE_RESULT_PERMISSIONDENIED; break; | |
| 261 | ✗ | case EPIPE: ret = GATE_RESULT_NOTREADY; break; | |
| 262 | ✗ | case EPROTO: ret = GATE_RESULT_INVALIDDATA; break; | |
| 263 | ✗ | case EPROTONOSUPPORT: ret = GATE_RESULT_NOTSUPPORTED; break; | |
| 264 | ✗ | case EPROTOTYPE: ret = GATE_RESULT_INCORRECTTYPE; break; | |
| 265 | ✗ | case ERANGE: ret = GATE_RESULT_OUTOFBOUNDS; break; | |
| 266 | ✗ | case EROFS: ret = GATE_RESULT_LOCKED; break; | |
| 267 | ✗ | case ESPIPE: ret = GATE_RESULT_INVALIDARG; break; | |
| 268 | ✗ | case ESRCH: ret = GATE_RESULT_NOMATCH; break; | |
| 269 | /*case ESTALE: ret = GATE_RESULT_FAILED; break;*/ | ||
| 270 | #ifdef ETIME | ||
| 271 | ✗ | case ETIME: ret = GATE_RESULT_TIMEOUT; break; | |
| 272 | #endif | ||
| 273 | ✗ | case ETIMEDOUT: ret = GATE_RESULT_TIMEOUT; break; | |
| 274 | ✗ | case ETXTBSY: ret = GATE_RESULT_NOTREADY; break; | |
| 275 | ✗ | case EXDEV: ret = GATE_RESULT_NOTSUPPORTED; break; | |
| 276 | ✗ | default: ret = GATE_RESULT_FAILED; break; | |
| 277 | } | ||
| 278 | 238 | return ret; | |
| 279 | } | ||
| 280 | |||
| 281 | 4 | gate_result_t gate_platform_print_error(gate_int32_t platform_error_code, char* buffer, gate_size_t buffer_len) | |
| 282 | { | ||
| 283 | gate_result_t result; | ||
| 284 | 4 | char* error_text = NULL; | |
| 285 | |||
| 286 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
4 | if ((buffer != NULL) && (buffer_len != 0)) |
| 287 | { | ||
| 288 | 1 | result = gate_platform_lock(); | |
| 289 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (GATE_SUCCEEDED(result)) |
| 290 | { | ||
| 291 | 1 | error_text = strerror((int)platform_error_code); | |
| 292 | 1 | gate_str_print_text(buffer, buffer_len, error_text, gate_str_length(error_text)); | |
| 293 | 1 | gate_platform_unlock(); | |
| 294 | } | ||
| 295 | } | ||
| 296 | 4 | return gate_posix_convert_errno(platform_error_code); | |
| 297 | } | ||
| 298 | |||
| 299 | 236 | int gate_posix_errno(gate_result_t* result) | |
| 300 | { | ||
| 301 | 236 | int ret = errno; | |
| 302 | |||
| 303 |
2/2✓ Branch 0 taken 234 times.
✓ Branch 1 taken 2 times.
|
236 | if (result != NULL) |
| 304 | { | ||
| 305 | 234 | *result = gate_posix_convert_errno(ret); | |
| 306 | } | ||
| 307 | 236 | return ret; | |
| 308 | } | ||
| 309 | ✗ | void gate_posix_set_errno(int code) | |
| 310 | { | ||
| 311 | ✗ | errno = code; | |
| 312 | ✗ | } | |
| 313 | |||
| 314 | |||
| 315 | 86 | void gate_posix_block_signals(gate_posix_sigblocker_t* blocker, int sig1, int sig2, ...) | |
| 316 | { | ||
| 317 | int arg; | ||
| 318 | va_list args; | ||
| 319 | |||
| 320 | 86 | gate_mem_clear(blocker, sizeof(gate_posix_sigblocker_t)); | |
| 321 | #if !defined(GATE_SYS_WASM_WASI) | ||
| 322 | 86 | sigemptyset(&blocker->signals); | |
| 323 | 86 | sigaddset(&blocker->signals, sig1); | |
| 324 | |||
| 325 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
|
86 | if (sig2 != 0) |
| 326 | { | ||
| 327 | ✗ | sigaddset(&blocker->signals, sig2); | |
| 328 | ✗ | va_start(args, sig2); | |
| 329 | ✗ | while (0 != (arg = va_arg(args, int))) | |
| 330 | { | ||
| 331 | ✗ | sigaddset(&blocker->signals, arg); | |
| 332 | } | ||
| 333 | ✗ | va_end(args); | |
| 334 | } | ||
| 335 | 86 | blocker->unblock_required = (pthread_sigmask(SIG_BLOCK, &blocker->signals, &blocker->signals) == 0); | |
| 336 | #endif | ||
| 337 | 86 | } | |
| 338 | |||
| 339 | 86 | void gate_posix_unblock_signals(gate_posix_sigblocker_t* blocker) | |
| 340 | { | ||
| 341 | #if !defined(GATE_SYS_WASM_WASI) | ||
| 342 |
1/2✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
|
86 | if (blocker->unblock_required) |
| 343 | { | ||
| 344 | 86 | pthread_sigmask(SIG_SETMASK, &blocker->signals, 0); | |
| 345 | } | ||
| 346 | #endif | ||
| 347 | 86 | gate_mem_clear(blocker, sizeof(gate_posix_sigblocker_t)); | |
| 348 | 86 | } | |
| 349 | |||
| 350 | static int gate_posix_inheritable_fds[1024]; | ||
| 351 | static gate_size_t gate_posix_inheritable_fd_count = 0; | ||
| 352 | static gate_atomic_lock_t gate_posix_inheritable_lock = GATE_ATOMIC_LOCK_INIT; | ||
| 353 | |||
| 354 | ✗ | gate_result_t gate_posix_register_inheritable_fd(int fd) | |
| 355 | { | ||
| 356 | gate_size_t ndx; | ||
| 357 | ✗ | gate_atomic_lock_acquire(&gate_posix_inheritable_lock); | |
| 358 | |||
| 359 | ✗ | for (ndx = 0; ndx != gate_posix_inheritable_fd_count; ++ndx) | |
| 360 | { | ||
| 361 | ✗ | if (gate_posix_inheritable_fds[ndx] == fd) | |
| 362 | { | ||
| 363 | ✗ | --gate_posix_inheritable_fd_count; | |
| 364 | ✗ | break; | |
| 365 | } | ||
| 366 | } | ||
| 367 | ✗ | gate_posix_inheritable_fds[ndx] = fd; | |
| 368 | ✗ | ++gate_posix_inheritable_fd_count; | |
| 369 | |||
| 370 | ✗ | gate_atomic_lock_release(&gate_posix_inheritable_lock); | |
| 371 | ✗ | return GATE_RESULT_OK; | |
| 372 | } | ||
| 373 | ✗ | gate_result_t gate_posix_unregister_inheritable_fd(int fd) | |
| 374 | { | ||
| 375 | ✗ | gate_result_t ret = GATE_RESULT_NOMATCH; | |
| 376 | gate_size_t ndx; | ||
| 377 | |||
| 378 | ✗ | gate_atomic_lock_acquire(&gate_posix_inheritable_lock); | |
| 379 | ✗ | if (gate_posix_inheritable_fd_count != 0) | |
| 380 | { | ||
| 381 | ✗ | for (ndx = 0; ndx < gate_posix_inheritable_fd_count; ++ndx) | |
| 382 | { | ||
| 383 | ✗ | if (gate_posix_inheritable_fds[ndx] == fd) | |
| 384 | { | ||
| 385 | ✗ | gate_posix_inheritable_fds[ndx] = gate_posix_inheritable_fds[gate_posix_inheritable_fd_count - 1]; | |
| 386 | ✗ | --gate_posix_inheritable_fd_count; | |
| 387 | ✗ | ret = GATE_RESULT_OK; | |
| 388 | ✗ | break; | |
| 389 | } | ||
| 390 | } | ||
| 391 | } | ||
| 392 | ✗ | gate_atomic_lock_release(&gate_posix_inheritable_lock); | |
| 393 | |||
| 394 | ✗ | return ret; | |
| 395 | } | ||
| 396 | 11 | gate_size_t gate_posix_get_inheritable_fds(int* fdbuffer, gate_size_t fdbuffer_length) | |
| 397 | { | ||
| 398 | 11 | gate_size_t used = 0; | |
| 399 | 11 | gate_size_t ndx = 0; | |
| 400 | |||
| 401 | 11 | gate_atomic_lock_acquire(&gate_posix_inheritable_lock); | |
| 402 | 11 | used = fdbuffer_length < gate_posix_inheritable_fd_count ? fdbuffer_length : gate_posix_inheritable_fd_count; | |
| 403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | for (ndx = 0; ndx != used; ++ndx) |
| 404 | { | ||
| 405 | ✗ | *fdbuffer = gate_posix_inheritable_fds[ndx]; | |
| 406 | ✗ | ++fdbuffer; | |
| 407 | } | ||
| 408 | |||
| 409 | 11 | gate_atomic_lock_release(&gate_posix_inheritable_lock); | |
| 410 | 11 | return used; | |
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | 9 | int gate_posix_open(char const* path, int oflag, int mode) | |
| 415 | { | ||
| 416 | 9 | return open(path, oflag, (mode_t)mode); | |
| 417 | } | ||
| 418 | 20437 | int gate_posix_close(int fd) | |
| 419 | { | ||
| 420 | 20437 | return close(fd); | |
| 421 | } | ||
| 422 | 1733950 | ptrdiff_t gate_posix_read(int fd, void* buf, size_t nbyte) | |
| 423 | { | ||
| 424 | ptrdiff_t result; | ||
| 425 | do | ||
| 426 | { | ||
| 427 | 1733950 | result = (ptrdiff_t)read(fd, buf, nbyte); | |
| 428 |
3/4✓ Branch 0 taken 1733893 times.
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1733893 times.
|
1733950 | } while ((result == -1) && (errno == EINTR)); |
| 429 | 1733950 | return result; | |
| 430 | } | ||
| 431 | 61 | ptrdiff_t gate_posix_write(int fd, void const* buf, size_t nbyte) | |
| 432 | { | ||
| 433 | ptrdiff_t result; | ||
| 434 | do | ||
| 435 | { | ||
| 436 | 61 | result = (ptrdiff_t)write(fd, buf, nbyte); | |
| 437 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
61 | } while ((result == -1) && (errno == EINTR)); |
| 438 | 61 | return result; | |
| 439 | } | ||
| 440 | 7 | gate_result_t gate_posix_await_read(int fd, gate_uint32_t timeout_ms) | |
| 441 | { | ||
| 442 | fd_set read_set; | ||
| 443 | struct timeval timeout; | ||
| 444 | |||
| 445 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
|
7 | if (timeout_ms != 0xffffffff) |
| 446 | { | ||
| 447 | 4 | timeout.tv_sec = timeout_ms / 1000; | |
| 448 | 4 | timeout.tv_usec = (timeout_ms % 1000) * 1000; | |
| 449 | } | ||
| 450 | else | ||
| 451 | { | ||
| 452 | 3 | timeout.tv_sec = 0x7fffffff; | |
| 453 | 3 | timeout.tv_usec = 0; | |
| 454 | } | ||
| 455 | |||
| 456 | 7 | FD_ZERO(&read_set); | |
| 457 | 7 | FD_SET(fd, &read_set); | |
| 458 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if (-1 == select(fd + 1, &read_set, NULL, NULL, &timeout)) |
| 459 | { | ||
| 460 | ✗ | return GATE_RESULT_FAILED; | |
| 461 | } | ||
| 462 | |||
| 463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (!FD_ISSET(fd, &read_set)) |
| 464 | { | ||
| 465 | ✗ | return GATE_RESULT_TIMEOUT; | |
| 466 | } | ||
| 467 | else | ||
| 468 | { | ||
| 469 | 7 | return GATE_RESULT_OK; | |
| 470 | } | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | 31 | int gate_posix_flock(int fd, gate_posix_flock_operation operation) | |
| 475 | { | ||
| 476 | #if defined(GATE_SYS_POSIX_HAS_FLOCK) | ||
| 477 | /* standard flock implemenation: */ | ||
| 478 | int result; | ||
| 479 | int op; | ||
| 480 |
3/5✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
|
31 | switch (operation) |
| 481 | { | ||
| 482 | ✗ | case gate_posix_flock_shared: op = LOCK_SH; break; | |
| 483 | 14 | case gate_posix_flock_exclusive: op = LOCK_EX; break; | |
| 484 | 2 | case gate_posix_flock_exclusive_test: op = (LOCK_EX | LOCK_NB); break; | |
| 485 | 15 | case gate_posix_flock_unlock: op = LOCK_UN; break; | |
| 486 | ✗ | default: | |
| 487 | ✗ | gate_posix_set_errno(EINVAL); | |
| 488 | ✗ | return -1; | |
| 489 | } | ||
| 490 | |||
| 491 | ✗ | do | |
| 492 | { | ||
| 493 | 31 | result = flock(fd, op); | |
| 494 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
31 | } while ((result == -1) && (errno == EINTR)); |
| 495 | 31 | return result; | |
| 496 | #elif defined(GATE_SYS_POSIX_HAS_LOCKF) | ||
| 497 | /* flock emulation using lockf for the whole file */ | ||
| 498 | int result; | ||
| 499 | gate_posix_lockf_cmd op; | ||
| 500 | |||
| 501 | switch (operation) | ||
| 502 | { | ||
| 503 | case gate_posix_flock_exclusive: op = gate_posix_lockf_lock; break; | ||
| 504 | case gate_posix_flock_exclusive_test: op = gate_posix_lockf_test; break; | ||
| 505 | case gate_posix_flock_unlock: op = gate_posix_lockf_unlock; break; | ||
| 506 | default: | ||
| 507 | gate_posix_set_errno(EINVAL); | ||
| 508 | return -1; | ||
| 509 | } | ||
| 510 | |||
| 511 | do | ||
| 512 | { | ||
| 513 | result = gate_posix_lockf(fd, op, 0, 0); | ||
| 514 | } while ((result == -1) && (errno == EINTR)); | ||
| 515 | return result; | ||
| 516 | #else | ||
| 517 | /* no flock support at all */ | ||
| 518 | gate_posix_set_errno(EACCES); | ||
| 519 | return -1; | ||
| 520 | #endif | ||
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | ✗ | int gate_posix_lockf(int fd, gate_posix_lockf_cmd cmd, gate_int64_t start_from, gate_int64_t length) | |
| 525 | { | ||
| 526 | #if defined(GATE_SYS_POSIX_HAS_LOCKF) | ||
| 527 | /* standard lockf implementation */ | ||
| 528 | int ret; | ||
| 529 | off_t current_pos; | ||
| 530 | int lock_cmd; | ||
| 531 | int stored_error; | ||
| 532 | |||
| 533 | do | ||
| 534 | { | ||
| 535 | ✗ | switch (cmd) | |
| 536 | { | ||
| 537 | ✗ | case gate_posix_lockf_lock: lock_cmd = F_LOCK; break; | |
| 538 | ✗ | case gate_posix_lockf_testlock: lock_cmd = F_TLOCK; break; | |
| 539 | ✗ | case gate_posix_lockf_unlock: lock_cmd = F_ULOCK; break; | |
| 540 | ✗ | case gate_posix_lockf_test: lock_cmd = F_TEST; break; | |
| 541 | ✗ | default: | |
| 542 | { | ||
| 543 | ✗ | gate_posix_set_errno(EINVAL); | |
| 544 | ✗ | return -1; | |
| 545 | } | ||
| 546 | } | ||
| 547 | |||
| 548 | ✗ | current_pos = lseek(fd, 0, SEEK_CUR); | |
| 549 | ✗ | if (current_pos == (off_t)-1) | |
| 550 | { | ||
| 551 | ✗ | ret = -1; | |
| 552 | ✗ | break; | |
| 553 | } | ||
| 554 | ✗ | if (lseek(fd, (off_t)start_from, SEEK_SET) == (off_t)-1) | |
| 555 | { | ||
| 556 | ✗ | ret = -1; | |
| 557 | ✗ | break; | |
| 558 | } | ||
| 559 | |||
| 560 | ✗ | ret = lockf(fd, lock_cmd, (off_t)length); | |
| 561 | ✗ | stored_error = errno; | |
| 562 | |||
| 563 | ✗ | lseek(fd, (off_t)current_pos, SEEK_SET); | |
| 564 | ✗ | errno = stored_error; | |
| 565 | } while (0); | ||
| 566 | ✗ | return ret; | |
| 567 | #elif defined(GATE_SYS_POSIX_HAS_FLOCK) | ||
| 568 | /* partially simulate lockf by locking the whole file with flock */ | ||
| 569 | int ret = -1; | ||
| 570 | do | ||
| 571 | { | ||
| 572 | switch (cmd) | ||
| 573 | { | ||
| 574 | case gate_posix_lockf_lock: | ||
| 575 | { | ||
| 576 | ret = gate_posix_flock(fd, gate_posix_flock_exclusive); | ||
| 577 | break; | ||
| 578 | } | ||
| 579 | case gate_posix_lockf_unlock: | ||
| 580 | { | ||
| 581 | ret = gate_posix_flock(fd, gate_posix_flock_unlock); | ||
| 582 | break; | ||
| 583 | } | ||
| 584 | case gate_posix_lockf_testlock: | ||
| 585 | /* TODO: LOCK_NB implementation */ | ||
| 586 | default: | ||
| 587 | { | ||
| 588 | gate_posix_set_errno(EINVAL); | ||
| 589 | return -1; | ||
| 590 | } | ||
| 591 | } | ||
| 592 | } while (0); | ||
| 593 | return ret; | ||
| 594 | #else | ||
| 595 | /* no lockf support at all */ | ||
| 596 | gate_posix_set_errno(EACCES); | ||
| 597 | return -1; | ||
| 598 | #endif | ||
| 599 | } | ||
| 600 | |||
| 601 | |||
| 602 | ✗ | int gate_posix_ioctl(int fd, unsigned long request, void* param) | |
| 603 | { | ||
| 604 | ✗ | return ioctl(fd, request, param); | |
| 605 | } | ||
| 606 | 38 | gate_bool_t gate_posix_path_exists(char const* path) | |
| 607 | { | ||
| 608 | struct stat filestat; | ||
| 609 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | if (0 == stat(path, &filestat)) |
| 610 | { | ||
| 611 | 38 | return true; | |
| 612 | } | ||
| 613 | ✗ | return false; | |
| 614 | } | ||
| 615 | |||
| 616 | #if defined(GATE_SYS_WASM_WASI) | ||
| 617 | |||
| 618 | static uid_t getuid() | ||
| 619 | { | ||
| 620 | return 0; | ||
| 621 | } | ||
| 622 | static uid_t geteuid() | ||
| 623 | { | ||
| 624 | return 0; | ||
| 625 | } | ||
| 626 | static uid_t getgid() | ||
| 627 | { | ||
| 628 | return 0; | ||
| 629 | } | ||
| 630 | |||
| 631 | #endif | ||
| 632 | |||
| 633 | 227 | uid_t gate_posix_getuid() | |
| 634 | { | ||
| 635 | 227 | return getuid(); | |
| 636 | } | ||
| 637 | |||
| 638 | 1 | uid_t gate_posix_geteuid() | |
| 639 | { | ||
| 640 | 1 | return geteuid(); | |
| 641 | } | ||
| 642 | |||
| 643 | 227 | gid_t gate_posix_getgid() | |
| 644 | { | ||
| 645 | 227 | return getgid(); | |
| 646 | } | ||
| 647 | |||
| 648 | #if defined(GATE_SYS_ANDROID) | ||
| 649 | gate_size_t gate_posix_enum_users(uid_t* uids, gate_size_t max_uids) | ||
| 650 | { | ||
| 651 | return 0; | ||
| 652 | } | ||
| 653 | gate_result_t gate_posix_get_user_info(uid_t uid, gate_posix_user_info_t* info) | ||
| 654 | { | ||
| 655 | return GATE_RESULT_NOMATCH; | ||
| 656 | } | ||
| 657 | gate_result_t gate_posix_resolve_user_info(char const* user_name, gate_posix_user_info_t* info) | ||
| 658 | { | ||
| 659 | return GATE_RESULT_NOMATCH; | ||
| 660 | } | ||
| 661 | gate_size_t gate_posix_enum_groups(gid_t* gids, gate_size_t max_gids) | ||
| 662 | { | ||
| 663 | return 0; | ||
| 664 | } | ||
| 665 | gate_result_t gate_posix_get_usergroup_info(gid_t gid, gate_posix_group_info_t* info) | ||
| 666 | { | ||
| 667 | return GATE_RESULT_NOMATCH; | ||
| 668 | } | ||
| 669 | gate_result_t gate_posix_resolve_usergroup_info(char const* group_name, gate_posix_group_info_t* info) | ||
| 670 | { | ||
| 671 | return GATE_RESULT_NOMATCH; | ||
| 672 | } | ||
| 673 | #else | ||
| 674 | |||
| 675 | 2 | gate_size_t gate_posix_enum_users(uid_t* uids, gate_size_t max_uids) | |
| 676 | { | ||
| 677 | struct passwd* entry; | ||
| 678 | 2 | gate_size_t index = 0; | |
| 679 | gate_result_t result; | ||
| 680 | |||
| 681 | do | ||
| 682 | { | ||
| 683 | 2 | result = gate_platform_lock(); | |
| 684 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATE_BREAK_IF_FAILED(result); |
| 685 | 2 | setpwent(); | |
| 686 |
2/2✓ Branch 1 taken 42 times.
✓ Branch 2 taken 2 times.
|
44 | while (NULL != (entry = getpwent())) |
| 687 | { | ||
| 688 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (index < max_uids) |
| 689 | { | ||
| 690 | 42 | uids[index] = entry->pw_uid; | |
| 691 | 42 | ++index; | |
| 692 | } | ||
| 693 | } | ||
| 694 | 2 | endpwent(); | |
| 695 | 2 | gate_platform_unlock(); | |
| 696 | } while (0); | ||
| 697 | 2 | return index; | |
| 698 | } | ||
| 699 | |||
| 700 | |||
| 701 | |||
| 702 | 97 | static void gate_posix_load_user_info(struct passwd const* entry, gate_posix_user_info_t* info) | |
| 703 | { | ||
| 704 | 97 | info->uid = entry->pw_uid; | |
| 705 | 97 | info->gid = entry->pw_gid; | |
| 706 | 97 | gate_str_print_text(info->name, sizeof(info->name), entry->pw_name, gate_str_length(entry->pw_name)); | |
| 707 | 97 | gate_str_print_text(info->gecos, sizeof(info->gecos), entry->pw_gecos, gate_str_length(entry->pw_gecos)); | |
| 708 | 97 | gate_str_print_text(info->home, sizeof(info->home), entry->pw_dir, gate_str_length(entry->pw_dir)); | |
| 709 | 97 | gate_str_print_text(info->shell, sizeof(info->shell), entry->pw_shell, gate_str_length(entry->pw_shell)); | |
| 710 | 97 | } | |
| 711 | 95 | gate_result_t gate_posix_get_user_info(uid_t uid, gate_posix_user_info_t* info) | |
| 712 | { | ||
| 713 | 95 | gate_result_t ret = GATE_RESULT_NOMATCH; | |
| 714 | struct passwd* entry; | ||
| 715 | |||
| 716 | do | ||
| 717 | { | ||
| 718 | 95 | gate_mem_clear(info, sizeof(gate_posix_user_info_t)); | |
| 719 | 95 | ret = gate_platform_lock(); | |
| 720 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
|
95 | GATE_BREAK_IF_FAILED(ret); |
| 721 | |||
| 722 | do | ||
| 723 | { | ||
| 724 | 95 | entry = getpwuid(uid); | |
| 725 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
|
95 | if (!entry) |
| 726 | { | ||
| 727 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 728 | ✗ | break; | |
| 729 | } | ||
| 730 | |||
| 731 | 95 | gate_posix_load_user_info(entry, info); | |
| 732 | 95 | ret = GATE_RESULT_OK; | |
| 733 | } while (0); | ||
| 734 | |||
| 735 | 95 | gate_platform_unlock(); | |
| 736 | } while (0); | ||
| 737 | 95 | return ret; | |
| 738 | } | ||
| 739 | 2 | gate_result_t gate_posix_resolve_user_info(char const* user_name, gate_posix_user_info_t* info) | |
| 740 | { | ||
| 741 | 2 | gate_result_t ret = GATE_RESULT_NOMATCH; | |
| 742 | struct passwd* entry; | ||
| 743 | |||
| 744 | do | ||
| 745 | { | ||
| 746 | 2 | gate_mem_clear(info, sizeof(gate_posix_user_info_t)); | |
| 747 | |||
| 748 | 2 | ret = gate_platform_lock(); | |
| 749 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATE_BREAK_IF_FAILED(ret); |
| 750 | |||
| 751 | do | ||
| 752 | { | ||
| 753 | 2 | entry = getpwnam(user_name); | |
| 754 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!entry) |
| 755 | { | ||
| 756 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 757 | ✗ | break; | |
| 758 | } | ||
| 759 | |||
| 760 | 2 | gate_posix_load_user_info(entry, info); | |
| 761 | 2 | ret = GATE_RESULT_OK; | |
| 762 | } while (0); | ||
| 763 | |||
| 764 | 2 | gate_platform_unlock(); | |
| 765 | } while (0); | ||
| 766 | 2 | return ret; | |
| 767 | } | ||
| 768 | 2 | gate_size_t gate_posix_enum_groups(gid_t* gids, gate_size_t max_gids) | |
| 769 | { | ||
| 770 | struct group* entry; | ||
| 771 | 2 | gate_size_t index = 0; | |
| 772 | gate_result_t result; | ||
| 773 | |||
| 774 | do | ||
| 775 | { | ||
| 776 | 2 | result = gate_platform_lock(); | |
| 777 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATE_BREAK_IF_FAILED(result); |
| 778 | |||
| 779 | 2 | setgrent(); | |
| 780 |
2/2✓ Branch 1 taken 82 times.
✓ Branch 2 taken 2 times.
|
84 | while (NULL != (entry = getgrent())) |
| 781 | { | ||
| 782 |
1/2✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
|
82 | if (index < max_gids) |
| 783 | { | ||
| 784 | 82 | gids[index] = entry->gr_gid; | |
| 785 | 82 | ++index; | |
| 786 | } | ||
| 787 | } | ||
| 788 | 2 | endgrent(); | |
| 789 | 2 | gate_platform_unlock(); | |
| 790 | } while (0); | ||
| 791 | 2 | return index; | |
| 792 | } | ||
| 793 | |||
| 794 | 211 | static void gate_posix_load_group_info(struct group const* entry, gate_posix_group_info_t* info) | |
| 795 | { | ||
| 796 | struct passwd const* user_entry; | ||
| 797 | char** ptr; | ||
| 798 | 211 | gate_size_t max_members = sizeof(info->members) / sizeof(info->members[0]); | |
| 799 | |||
| 800 | 211 | info->gid = entry->gr_gid; | |
| 801 | 211 | gate_str_print_text(info->name, sizeof(info->name), entry->gr_name, gate_str_length(entry->gr_name)); | |
| 802 | 211 | ptr = entry->gr_mem; | |
| 803 |
1/2✓ Branch 0 taken 211 times.
✗ Branch 1 not taken.
|
211 | if (ptr) |
| 804 | { | ||
| 805 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 211 times.
|
211 | while (*ptr) |
| 806 | { | ||
| 807 | ✗ | if (info->members_count >= max_members) | |
| 808 | { | ||
| 809 | ✗ | break; | |
| 810 | } | ||
| 811 | ✗ | user_entry = getpwnam(*ptr); | |
| 812 | ✗ | if (user_entry) | |
| 813 | { | ||
| 814 | ✗ | info->members[info->members_count] = user_entry->pw_uid; | |
| 815 | ✗ | ++info->members_count; | |
| 816 | } | ||
| 817 | ✗ | ++ptr; | |
| 818 | } | ||
| 819 | } | ||
| 820 | 211 | } | |
| 821 | |||
| 822 | 209 | gate_result_t gate_posix_get_usergroup_info(gid_t gid, gate_posix_group_info_t* info) | |
| 823 | { | ||
| 824 | 209 | gate_result_t ret = GATE_RESULT_NOMATCH; | |
| 825 | struct group const* entry; | ||
| 826 | |||
| 827 | do | ||
| 828 | { | ||
| 829 | 209 | gate_mem_clear(info, sizeof(gate_posix_group_info_t)); | |
| 830 | 209 | ret = gate_platform_lock(); | |
| 831 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 209 times.
|
209 | GATE_BREAK_IF_FAILED(ret); |
| 832 | |||
| 833 | do | ||
| 834 | { | ||
| 835 | 209 | entry = getgrgid(gid); | |
| 836 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 209 times.
|
209 | if (!entry) |
| 837 | { | ||
| 838 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 839 | ✗ | break; | |
| 840 | } | ||
| 841 | |||
| 842 | 209 | gate_posix_load_group_info(entry, info); | |
| 843 | 209 | ret = GATE_RESULT_OK; | |
| 844 | } while (0); | ||
| 845 | |||
| 846 | 209 | gate_platform_unlock(); | |
| 847 | } while (0); | ||
| 848 | 209 | return ret; | |
| 849 | } | ||
| 850 | 2 | gate_result_t gate_posix_resolve_usergroup_info(char const* group_name, gate_posix_group_info_t* info) | |
| 851 | { | ||
| 852 | 2 | gate_result_t ret = GATE_RESULT_NOMATCH; | |
| 853 | struct group const* entry; | ||
| 854 | struct passwd const* user_entry; | ||
| 855 | char const** ptr; | ||
| 856 | gate_size_t index; | ||
| 857 | 2 | gate_size_t max_members = sizeof(info->members) / sizeof(info->members[0]); | |
| 858 | |||
| 859 | do | ||
| 860 | { | ||
| 861 | 2 | gate_mem_clear(info, sizeof(gate_posix_group_info_t)); | |
| 862 | |||
| 863 | 2 | ret = gate_platform_lock(); | |
| 864 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATE_BREAK_IF_FAILED(ret); |
| 865 | |||
| 866 | do | ||
| 867 | { | ||
| 868 | 2 | entry = getgrnam(group_name); | |
| 869 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!entry) |
| 870 | { | ||
| 871 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 872 | ✗ | break; | |
| 873 | } | ||
| 874 | |||
| 875 | 2 | gate_posix_load_group_info(entry, info); | |
| 876 | 2 | ret = GATE_RESULT_OK; | |
| 877 | } while (0); | ||
| 878 | |||
| 879 | 2 | gate_platform_unlock(); | |
| 880 | } while (0); | ||
| 881 | 2 | return ret; | |
| 882 | } | ||
| 883 | #endif /* POSIX account info */ | ||
| 884 | |||
| 885 | |||
| 886 | |||
| 887 | 3 | gate_bool_t gate_platform_stream_valid(gate_platform_stream_t strm) | |
| 888 | { | ||
| 889 | 3 | int fd = (int)(gate_intptr_t)strm; | |
| 890 | 3 | return (fd >= 0); | |
| 891 | } | ||
| 892 | 1 | void gate_platform_stream_set_invalid(gate_platform_stream_t* ptr_strm) | |
| 893 | { | ||
| 894 | 1 | *ptr_strm = (gate_platform_stream_t)(gate_intptr_t)(-1); | |
| 895 | 1 | } | |
| 896 | |||
| 897 | |||
| 898 | 4 | gate_result_t gate_platform_stream_open(char const* path, int open_flags, int native_flags, gate_platform_stream_t* strm) | |
| 899 | { | ||
| 900 | 4 | int openflags = native_flags; | |
| 901 | int fd; | ||
| 902 | 4 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; | |
| 903 | |||
| 904 |
2/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
4 | switch (open_flags) |
| 905 | { | ||
| 906 | 2 | case GATE_PLATFORM_STREAM_OPEN_READ: | |
| 907 | { | ||
| 908 | 2 | openflags = (O_RDONLY); | |
| 909 | 2 | break; | |
| 910 | } | ||
| 911 | 2 | case GATE_PLATFORM_STREAM_OPEN_WRITE: | |
| 912 | { | ||
| 913 | 2 | openflags = (O_CREAT | O_WRONLY | O_TRUNC); | |
| 914 | 2 | break; | |
| 915 | } | ||
| 916 | ✗ | case GATE_PLATFORM_STREAM_OPEN_READWRITE: | |
| 917 | { | ||
| 918 | ✗ | openflags = (O_CREAT | O_RDWR); | |
| 919 | ✗ | break; | |
| 920 | } | ||
| 921 | } | ||
| 922 | #ifdef O_CLOEXEC | ||
| 923 | 4 | openflags |= O_CLOEXEC; | |
| 924 | #endif | ||
| 925 | |||
| 926 | 4 | fd = open(path, openflags, mode); | |
| 927 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (fd < 0) |
| 928 | { | ||
| 929 | 1 | return gate_platform_print_last_error(NULL, 0); | |
| 930 | } | ||
| 931 | |||
| 932 | 3 | *strm = (gate_platform_stream_t)(gate_intptr_t)fd; | |
| 933 | 3 | return GATE_RESULT_OK; | |
| 934 | } | ||
| 935 | |||
| 936 | 1 | gate_result_t gate_platform_stream_read(gate_platform_stream_t strm, char* buffer, gate_size_t bufferlength, gate_size_t* returned) | |
| 937 | { | ||
| 938 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 939 | 1 | int fd = (int)(gate_intptr_t)strm; | |
| 940 | gate_int32_t error_code; | ||
| 941 | |||
| 942 | 1 | ptrdiff_t result = gate_posix_read(fd, buffer, bufferlength); | |
| 943 | |||
| 944 | |||
| 945 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result == -1) |
| 946 | { | ||
| 947 | ✗ | error_code = gate_platform_get_last_error(); | |
| 948 | ✗ | ret = gate_platform_print_error(error_code, NULL, 0); | |
| 949 | } | ||
| 950 | else | ||
| 951 | { | ||
| 952 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (returned != NULL) |
| 953 | { | ||
| 954 | 1 | *returned = (gate_size_t)result; | |
| 955 | } | ||
| 956 | 1 | ret = GATE_RESULT_OK; | |
| 957 | } | ||
| 958 | 1 | return ret; | |
| 959 | } | ||
| 960 | 2 | gate_result_t gate_platform_stream_write(gate_platform_stream_t strm, char const* buffer, gate_size_t bufferlength, gate_size_t* written) | |
| 961 | { | ||
| 962 | 2 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 963 | 2 | int fd = (int)(gate_intptr_t)strm; | |
| 964 | gate_int32_t error_code; | ||
| 965 | |||
| 966 | 2 | ptrdiff_t result = gate_posix_write(fd, buffer, bufferlength); | |
| 967 | |||
| 968 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (result == -1) |
| 969 | { | ||
| 970 | ✗ | error_code = gate_platform_get_last_error(); | |
| 971 | ✗ | ret = gate_platform_print_error(error_code, NULL, 0); | |
| 972 | } | ||
| 973 | else | ||
| 974 | { | ||
| 975 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (written != NULL) |
| 976 | { | ||
| 977 | 2 | *written = (gate_size_t)result; | |
| 978 | } | ||
| 979 | 2 | ret = GATE_RESULT_OK; | |
| 980 | } | ||
| 981 | 2 | return ret; | |
| 982 | } | ||
| 983 | 3 | gate_result_t gate_platform_stream_seek(gate_platform_stream_t strm, gate_int64_t position, int origin, gate_int64_t* final_position) | |
| 984 | { | ||
| 985 | 3 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 986 | 3 | int fd = (int)(gate_intptr_t)strm; | |
| 987 | gate_int32_t error_code; | ||
| 988 | int whence; | ||
| 989 | off_t result; | ||
| 990 | |||
| 991 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
3 | switch (origin) |
| 992 | { | ||
| 993 | 1 | case GATE_PLATFORM_STREAM_ORIGIN_BEGIN: | |
| 994 | 1 | whence = SEEK_SET; | |
| 995 | 1 | break; | |
| 996 | 1 | case GATE_PLATFORM_STREAM_ORIGIN_CURRENT: | |
| 997 | 1 | whence = SEEK_CUR; | |
| 998 | 1 | break; | |
| 999 | 1 | case GATE_PLATFORM_STREAM_ORIGIN_END: | |
| 1000 | 1 | whence = SEEK_END; | |
| 1001 | 1 | break; | |
| 1002 | ✗ | default: | |
| 1003 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1004 | } | ||
| 1005 | |||
| 1006 | 3 | result = lseek(fd, (off_t)position, whence); | |
| 1007 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (result == (off_t)-1) |
| 1008 | { | ||
| 1009 | ✗ | error_code = gate_platform_get_last_error(); | |
| 1010 | ✗ | ret = gate_platform_print_error(error_code, NULL, 0); | |
| 1011 | } | ||
| 1012 | else | ||
| 1013 | { | ||
| 1014 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (final_position != NULL) |
| 1015 | { | ||
| 1016 | 3 | *final_position = (gate_int64_t)result; | |
| 1017 | } | ||
| 1018 | 3 | ret = GATE_RESULT_OK; | |
| 1019 | } | ||
| 1020 | 3 | return ret; | |
| 1021 | } | ||
| 1022 | |||
| 1023 | 3 | gate_result_t gate_platform_stream_close(gate_platform_stream_t* strm) | |
| 1024 | { | ||
| 1025 | 3 | gate_result_t ret = GATE_RESULT_OK; | |
| 1026 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (strm != NULL) |
| 1027 | { | ||
| 1028 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if ((int)(gate_intptr_t)*strm != -1) |
| 1029 | { | ||
| 1030 | 3 | close((int)(gate_intptr_t)*strm); | |
| 1031 | 3 | *strm = (gate_platform_stream_t)(gate_intptr_t)-1; | |
| 1032 | } | ||
| 1033 | } | ||
| 1034 | 3 | return ret; | |
| 1035 | } | ||
| 1036 | |||
| 1037 | 43 | int gate_platform_convert_gate_signal(int gate_platform_signal) | |
| 1038 | { | ||
| 1039 |
2/8✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 42 times.
|
43 | switch (gate_platform_signal) |
| 1040 | { | ||
| 1041 | ✗ | case GATE_PLATFORM_SIGNAL_ABRT: return SIGABRT; | |
| 1042 | ✗ | case GATE_PLATFORM_SIGNAL_FPE: return SIGFPE; | |
| 1043 | ✗ | case GATE_PLATFORM_SIGNAL_ILL: return SIGILL; | |
| 1044 | ✗ | case GATE_PLATFORM_SIGNAL_INT: return SIGINT; | |
| 1045 | ✗ | case GATE_PLATFORM_SIGNAL_SEGV: return SIGSEGV; | |
| 1046 | ✗ | case GATE_PLATFORM_SIGNAL_TERM: return SIGTERM; | |
| 1047 | 1 | case GATE_PLATFORM_SIGNAL_CONTINUE: return SIGCONT; | |
| 1048 | 42 | default: return gate_platform_signal; | |
| 1049 | } | ||
| 1050 | } | ||
| 1051 | ✗ | int gate_platform_convert_native_signal(int native_signal) | |
| 1052 | { | ||
| 1053 | ✗ | switch (native_signal) | |
| 1054 | { | ||
| 1055 | ✗ | case SIGABRT: return GATE_PLATFORM_SIGNAL_ABRT; | |
| 1056 | ✗ | case SIGFPE: return GATE_PLATFORM_SIGNAL_FPE; | |
| 1057 | ✗ | case SIGILL: return GATE_PLATFORM_SIGNAL_ILL; | |
| 1058 | ✗ | case SIGINT: return GATE_PLATFORM_SIGNAL_INT; | |
| 1059 | ✗ | case SIGSEGV: return GATE_PLATFORM_SIGNAL_SEGV; | |
| 1060 | ✗ | case SIGTERM: return GATE_PLATFORM_SIGNAL_TERM; | |
| 1061 | ✗ | case SIGCONT: return GATE_PLATFORM_SIGNAL_CONTINUE; | |
| 1062 | ✗ | default: return native_signal; | |
| 1063 | } | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | |||
| 1067 | 42 | gate_result_t gate_platform_set_signal_handler(int sig, | |
| 1068 | gate_platform_signal_handler_t new_handler, | ||
| 1069 | gate_platform_signal_handler_t* ptr_old_handler) | ||
| 1070 | { | ||
| 1071 | gate_platform_signal_handler_t ret; | ||
| 1072 | 42 | int native_signal = gate_platform_convert_gate_signal(sig); | |
| 1073 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (native_signal == 0) |
| 1074 | { | ||
| 1075 | ✗ | return GATE_RESULT_NOTSUPPORTED; | |
| 1076 | } | ||
| 1077 | 42 | ret = signal(native_signal, new_handler); | |
| 1078 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (SIG_ERR == ret) |
| 1079 | { | ||
| 1080 | ✗ | return GATE_RESULT_FAILED; | |
| 1081 | } | ||
| 1082 | else | ||
| 1083 | { | ||
| 1084 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (ptr_old_handler) |
| 1085 | { | ||
| 1086 | ✗ | *ptr_old_handler = ret; | |
| 1087 | } | ||
| 1088 | 42 | return GATE_RESULT_OK; | |
| 1089 | } | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | 1 | gate_result_t gate_platform_raise_signal(int sig) | |
| 1093 | { | ||
| 1094 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 1095 | int result; | ||
| 1096 | 1 | int native_signal = gate_platform_convert_gate_signal(sig); | |
| 1097 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (native_signal == 0) |
| 1098 | { | ||
| 1099 | ✗ | return GATE_RESULT_NOTSUPPORTED; | |
| 1100 | } | ||
| 1101 | |||
| 1102 | 1 | result = raise(native_signal); | |
| 1103 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (result == 0) |
| 1104 | { | ||
| 1105 | 1 | ret = GATE_RESULT_OK; | |
| 1106 | } | ||
| 1107 | else | ||
| 1108 | { | ||
| 1109 | ✗ | gate_posix_errno(&ret); | |
| 1110 | } | ||
| 1111 | 1 | return ret; | |
| 1112 | } | ||
| 1113 | |||
| 1114 | 25 | pid_t gate_posix_getpid() | |
| 1115 | { | ||
| 1116 | 25 | return getpid(); | |
| 1117 | } | ||
| 1118 | |||
| 1119 | #if defined(GATE_SYS_WASM_WASI) | ||
| 1120 | |||
| 1121 | gate_result_t gate_posix_daemonize(pid_t* ptr_pid) | ||
| 1122 | { | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | #else | ||
| 1126 | |||
| 1127 | 1 | gate_result_t gate_posix_daemonize(pid_t* ptr_pid) | |
| 1128 | { | ||
| 1129 | pid_t child_pid; | ||
| 1130 | pid_t daemon_pid; | ||
| 1131 | int fd[2]; | ||
| 1132 | int result; | ||
| 1133 | ssize_t transferred; | ||
| 1134 | int daemon_fd; | ||
| 1135 | |||
| 1136 | 1 | result = pipe(fd); | |
| 1137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result < 0) |
| 1138 | { | ||
| 1139 | /* failed to create communication pipe */ | ||
| 1140 | ✗ | GATE_DEBUG_TRACE("daemonize: failed to create pipes"); | |
| 1141 | ✗ | return GATE_RESULT_OUTOFRESOURCES; | |
| 1142 | } | ||
| 1143 | |||
| 1144 | 1 | child_pid = fork(); | |
| 1145 | |||
| 1146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (child_pid < 0) |
| 1147 | { | ||
| 1148 | ✗ | GATE_DEBUG_TRACE("daemonize: initial fork() failed"); | |
| 1149 | ✗ | return GATE_RESULT_FAILED; | |
| 1150 | } | ||
| 1151 | |||
| 1152 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (child_pid > 0) |
| 1153 | { | ||
| 1154 | /* caller branch hosting the dispatcher */ | ||
| 1155 | ✗ | close(fd[1]); /* close the remote write-end */ | |
| 1156 | ✗ | transferred = read(fd[0], &daemon_pid, sizeof(daemon_pid)); | |
| 1157 | ✗ | close(fd[0]); /* close the local read-end */ | |
| 1158 | ✗ | waitpid(child_pid, NULL, 0); | |
| 1159 | ✗ | if (transferred != sizeof(daemon_pid)) | |
| 1160 | { | ||
| 1161 | /* could not retrieve daemon-PID -> failed to create daemon */ | ||
| 1162 | ✗ | GATE_DEBUG_TRACE("daemonize: waitpid() completed - NO PID - failed"); | |
| 1163 | ✗ | return GATE_RESULT_FAILED; | |
| 1164 | } | ||
| 1165 | |||
| 1166 | ✗ | GATE_DEBUG_TRACE("daemonize: waitpid() completed - PID received"); | |
| 1167 | ✗ | GATE_DEBUG_TRACE_VALUE(daemon_pid); | |
| 1168 | ✗ | if (ptr_pid) | |
| 1169 | { | ||
| 1170 | ✗ | *ptr_pid = daemon_pid; | |
| 1171 | } | ||
| 1172 | /* notify the caller, that the daemon process is running */ | ||
| 1173 | ✗ | return GATE_RESULT_OK; | |
| 1174 | } | ||
| 1175 | |||
| 1176 | /* dispatcher child process */ | ||
| 1177 | 1 | close(fd[0]); /* close read side of pipe*/ | |
| 1178 | |||
| 1179 | 1 | result = setsid(); | |
| 1180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (result < 0) |
| 1181 | { | ||
| 1182 | ✗ | GATE_DEBUG_TRACE("daemonize: setsid() failed"); | |
| 1183 | ✗ | _exit(1); | |
| 1184 | } | ||
| 1185 | |||
| 1186 | 1 | signal(SIGHUP, SIG_IGN); | |
| 1187 | |||
| 1188 | /* 2nd fork: */ | ||
| 1189 | 1 | daemon_pid = fork(); | |
| 1190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (daemon_pid < 0) |
| 1191 | { | ||
| 1192 | ✗ | GATE_DEBUG_TRACE("daemonize: final fork() failed"); | |
| 1193 | ✗ | _exit(2); | |
| 1194 | } | ||
| 1195 | |||
| 1196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (daemon_pid > 0) |
| 1197 | { | ||
| 1198 | /* dispatcher has retrieved the final daemon child PID: */ | ||
| 1199 | |||
| 1200 | /* send daemon-PID to parent process */ | ||
| 1201 | ✗ | GATE_DEBUG_TRACE("daemonize: send daemon PID back to parent process"); | |
| 1202 | ✗ | GATE_DEBUG_TRACE_VALUE(daemon_pid); | |
| 1203 | ✗ | transferred = write(fd[1], &daemon_pid, sizeof(daemon_pid)); | |
| 1204 | ✗ | close(fd[1]); /* close write end */ | |
| 1205 | ✗ | if (transferred == sizeof(daemon_pid)) | |
| 1206 | { | ||
| 1207 | ✗ | GATE_DEBUG_TRACE("daemonize: dispatcher process succeeded"); | |
| 1208 | ✗ | _exit(0); /* terminate dispatcher with success state */ | |
| 1209 | } | ||
| 1210 | else | ||
| 1211 | { | ||
| 1212 | ✗ | GATE_DEBUG_TRACE("daemonize: dispatcher process failed"); | |
| 1213 | ✗ | _exit(3); /* terminate dispatcher with error state*/ | |
| 1214 | } | ||
| 1215 | return GATE_RESULT_CRITICALERROR; /* this point should never be reached */ | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | /* this is the daemonized branch */ | ||
| 1219 | 1 | GATE_DEBUG_TRACE("daemonize: daemon branch reached"); | |
| 1220 | |||
| 1221 | 1 | close(fd[1]); /* close write end */ | |
| 1222 | |||
| 1223 | /* prepare daemon environment */ | ||
| 1224 | 1 | result = chdir("/"); | |
| 1225 | 1 | umask(0); | |
| 1226 | |||
| 1227 | /* assign STDIN/OUT/ERR to null device: */ | ||
| 1228 | 1 | daemon_fd = open("/dev/null", O_RDONLY); | |
| 1229 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if ((daemon_fd != -1) && (daemon_fd != STDIN_FILENO)) |
| 1230 | { | ||
| 1231 | 1 | dup2(daemon_fd, STDIN_FILENO); | |
| 1232 | 1 | close(daemon_fd); | |
| 1233 | } | ||
| 1234 | |||
| 1235 | 1 | daemon_fd = open("/dev/null", O_WRONLY); | |
| 1236 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (daemon_fd != -1) |
| 1237 | { | ||
| 1238 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (daemon_fd != STDOUT_FILENO) |
| 1239 | { | ||
| 1240 | 1 | dup2(daemon_fd, STDOUT_FILENO); | |
| 1241 | } | ||
| 1242 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (daemon_fd != STDERR_FILENO) |
| 1243 | { | ||
| 1244 | 1 | dup2(daemon_fd, STDERR_FILENO); | |
| 1245 | } | ||
| 1246 | 1 | close(daemon_fd); | |
| 1247 | } | ||
| 1248 | |||
| 1249 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ptr_pid) |
| 1250 | { | ||
| 1251 | 1 | *ptr_pid = 0; /* mark as daemon-branch */ | |
| 1252 | } | ||
| 1253 | 1 | return GATE_RESULT_OK; | |
| 1254 | } | ||
| 1255 | |||
| 1256 | #endif /* */ | ||
| 1257 | |||
| 1258 | 27 | void* gate_posix_dlopen(char const* libpath, int flags) | |
| 1259 | { | ||
| 1260 | void* ret; | ||
| 1261 | 27 | dlerror(); /* clear previous error state */ | |
| 1262 | 27 | ret = dlopen(libpath, flags); | |
| 1263 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 21 times.
|
27 | if (NULL == ret) |
| 1264 | { | ||
| 1265 | 6 | char* last_errmsg = dlerror(); | |
| 1266 | 6 | GATE_DEBUG_TRACE("dlopen() failed"); | |
| 1267 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (libpath) |
| 1268 | { | ||
| 1269 | 6 | GATE_DEBUG_TRACE(libpath); | |
| 1270 | } | ||
| 1271 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (last_errmsg) |
| 1272 | { | ||
| 1273 | 6 | GATE_DEBUG_TRACE(last_errmsg); | |
| 1274 | } | ||
| 1275 | } | ||
| 1276 | 27 | return ret; | |
| 1277 | } | ||
| 1278 | |||
| 1279 | 13 | int gate_posix_dlclose(void* handle) | |
| 1280 | { | ||
| 1281 | 13 | int ret = dlclose(handle); | |
| 1282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (ret != 0) |
| 1283 | { | ||
| 1284 | ✗ | char* last_errmsg = dlerror(); | |
| 1285 | ✗ | GATE_DEBUG_TRACE("dlclose() failed"); | |
| 1286 | ✗ | if (last_errmsg) | |
| 1287 | { | ||
| 1288 | ✗ | GATE_DEBUG_TRACE(last_errmsg); | |
| 1289 | } | ||
| 1290 | } | ||
| 1291 | 13 | return ret; | |
| 1292 | } | ||
| 1293 | |||
| 1294 | 480 | void* gate_posix_dlsym(void* handle, char const* symbol) | |
| 1295 | { | ||
| 1296 | void* ret; | ||
| 1297 | |||
| 1298 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 480 times.
|
480 | if (!symbol) |
| 1299 | { | ||
| 1300 | ✗ | GATE_DEBUG_TRACE("Invalid arg for dlsym()"); | |
| 1301 | ✗ | return NULL; | |
| 1302 | } | ||
| 1303 | |||
| 1304 | 480 | dlerror(); | |
| 1305 | 480 | ret = dlsym(handle, symbol); | |
| 1306 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 476 times.
|
480 | if (NULL == ret) |
| 1307 | { | ||
| 1308 | 4 | char* last_errmsg = dlerror(); | |
| 1309 | 4 | GATE_DEBUG_TRACE("dlsym() failed:"); | |
| 1310 | 4 | GATE_DEBUG_TRACE(symbol); | |
| 1311 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (last_errmsg) |
| 1312 | { | ||
| 1313 | 4 | GATE_DEBUG_TRACE(last_errmsg); | |
| 1314 | } | ||
| 1315 | } | ||
| 1316 | 480 | return ret; | |
| 1317 | } | ||
| 1318 | |||
| 1319 | |||
| 1320 | static void* volatile dl_global = NULL; | ||
| 1321 | |||
| 1322 | 8 | static void close_global_dlhandle_atexit() | |
| 1323 | { | ||
| 1324 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (NULL != dl_global) |
| 1325 | { | ||
| 1326 | 8 | void* handle = dl_global; | |
| 1327 | 8 | dl_global = NULL; | |
| 1328 | 8 | dlclose(handle); | |
| 1329 | } | ||
| 1330 | 8 | } | |
| 1331 | |||
| 1332 | 26 | void* gate_posix_global_sym(char const* symbol) | |
| 1333 | { | ||
| 1334 | void* ret; | ||
| 1335 | |||
| 1336 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (NULL == symbol) |
| 1337 | { | ||
| 1338 | ✗ | return NULL; | |
| 1339 | } | ||
| 1340 | |||
| 1341 | 26 | dlerror(); | |
| 1342 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 18 times.
|
26 | if (dl_global == NULL) |
| 1343 | { | ||
| 1344 | 8 | dl_global = dlopen(NULL, RTLD_GLOBAL | RTLD_NOW); | |
| 1345 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (NULL == dl_global) |
| 1346 | { | ||
| 1347 | ✗ | char* last_errmsg = dlerror(); | |
| 1348 | ✗ | if (last_errmsg) | |
| 1349 | { | ||
| 1350 | ✗ | GATE_DEBUG_TRACE(last_errmsg); | |
| 1351 | } | ||
| 1352 | } | ||
| 1353 | else | ||
| 1354 | { | ||
| 1355 | 8 | gate_platform_atexit(&close_global_dlhandle_atexit); | |
| 1356 | } | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | 26 | ret = dlsym(dl_global, symbol); | |
| 1360 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 12 times.
|
26 | if (NULL == ret) |
| 1361 | { | ||
| 1362 | 14 | char* last_errmsg = dlerror(); | |
| 1363 | 14 | GATE_DEBUG_TRACE("dlsym() failed to load global symbol"); | |
| 1364 | 14 | GATE_DEBUG_TRACE(symbol); | |
| 1365 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (last_errmsg) |
| 1366 | { | ||
| 1367 | 14 | GATE_DEBUG_TRACE(last_errmsg); | |
| 1368 | } | ||
| 1369 | } | ||
| 1370 | 26 | return ret; | |
| 1371 | } | ||
| 1372 | |||
| 1373 | #if defined(GATE_SYS_DARWIN) | ||
| 1374 | |||
| 1375 | gate_bool_t gate_posix_clock_gettime(long* ptr_seconds, long* ptr_nanoseconds) | ||
| 1376 | { | ||
| 1377 | clock_serv_t cclock; | ||
| 1378 | mach_timespec_t mts; | ||
| 1379 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); | ||
| 1380 | clock_get_time(cclock, &mts); | ||
| 1381 | mach_port_deallocate(mach_task_self(), cclock); | ||
| 1382 | if (ptr_seconds) *ptr_seconds = mts.tv_sec; | ||
| 1383 | if (ptr_nanoseconds) *ptr_nanoseconds = mts.tv_nsec; | ||
| 1384 | return true; | ||
| 1385 | } | ||
| 1386 | gate_bool_t gate_posix_clock_gettime_monotonic(long* ptr_seconds, long* ptr_nanoseconds) | ||
| 1387 | { | ||
| 1388 | return gate_posix_clock_gettime(ptr_seconds, ptr_nanoseconds); | ||
| 1389 | } | ||
| 1390 | |||
| 1391 | #else | ||
| 1392 | |||
| 1393 | ✗ | gate_bool_t gate_posix_clock_gettime(long* ptr_seconds, long* ptr_nanoseconds) | |
| 1394 | { | ||
| 1395 | struct timespec ts; | ||
| 1396 | ✗ | if (0 == clock_gettime(CLOCK_REALTIME, &ts)) | |
| 1397 | { | ||
| 1398 | ✗ | if (ptr_seconds) *ptr_seconds = ts.tv_sec; | |
| 1399 | ✗ | if (ptr_nanoseconds) *ptr_nanoseconds = ts.tv_nsec; | |
| 1400 | ✗ | return true; | |
| 1401 | } | ||
| 1402 | ✗ | return false; | |
| 1403 | } | ||
| 1404 | 11808500 | gate_bool_t gate_posix_clock_gettime_monotonic(long* ptr_seconds, long* ptr_nanoseconds) | |
| 1405 | { | ||
| 1406 | struct timespec ts; | ||
| 1407 |
1/2✓ Branch 1 taken 11808499 times.
✗ Branch 2 not taken.
|
11808500 | if (0 == clock_gettime(CLOCK_MONOTONIC, &ts)) |
| 1408 | { | ||
| 1409 |
1/2✓ Branch 0 taken 11808499 times.
✗ Branch 1 not taken.
|
11808499 | if (ptr_seconds) *ptr_seconds = ts.tv_sec; |
| 1410 |
1/2✓ Branch 0 taken 11808499 times.
✗ Branch 1 not taken.
|
11808499 | if (ptr_nanoseconds) *ptr_nanoseconds = ts.tv_nsec; |
| 1411 | 11808499 | return true; | |
| 1412 | } | ||
| 1413 | ✗ | return false; | |
| 1414 | } | ||
| 1415 | |||
| 1416 | #endif | ||
| 1417 | |||
| 1418 | 4 | static void create_semaphore_timeout(struct timespec* ptr_timeout, gate_uint32_t timeout_ms, gate_bool_t use_clocktime) | |
| 1419 | { | ||
| 1420 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (use_clocktime) |
| 1421 | { | ||
| 1422 | long seconds, nanoseconds; | ||
| 1423 | ✗ | gate_posix_clock_gettime(&seconds, &nanoseconds); | |
| 1424 | ✗ | ptr_timeout->tv_sec = seconds; | |
| 1425 | ✗ | ptr_timeout->tv_nsec = nanoseconds; | |
| 1426 | } | ||
| 1427 | else | ||
| 1428 | { | ||
| 1429 | 4 | ptr_timeout->tv_sec = 0; | |
| 1430 | 4 | ptr_timeout->tv_nsec = 0; | |
| 1431 | } | ||
| 1432 | 4 | ptr_timeout->tv_sec += timeout_ms / 1000; | |
| 1433 | 4 | ptr_timeout->tv_nsec += (timeout_ms % 1000) * 1000000; | |
| 1434 | 4 | ptr_timeout->tv_sec += ptr_timeout->tv_nsec / 1000000000; | |
| 1435 | 4 | ptr_timeout->tv_nsec %= 1000000000; | |
| 1436 | 4 | } | |
| 1437 | |||
| 1438 | #if defined(GATE_SYS_USE_POSIX_SEMAPHORE) | ||
| 1439 | |||
| 1440 | gate_result_t gate_platform_semaphore_create(gate_platform_semaphore_t* ptr_sem, unsigned int count) | ||
| 1441 | { | ||
| 1442 | sem_t* s = NULL; | ||
| 1443 | int result; | ||
| 1444 | |||
| 1445 | if (!ptr_sem || (count == 0)) | ||
| 1446 | { | ||
| 1447 | return GATE_RESULT_INVALIDARG; | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | s = (sem_t*)gate_mem_alloc(sizeof(sem_t)); | ||
| 1451 | if (!s) | ||
| 1452 | { | ||
| 1453 | return GATE_RESULT_OUTOFMEMORY; | ||
| 1454 | } | ||
| 1455 | result = sem_init(s, 0, (unsigned int)count); | ||
| 1456 | if (result != 0) | ||
| 1457 | { | ||
| 1458 | gate_mem_dealloc(s); | ||
| 1459 | return GATE_RESULT_FAILED; | ||
| 1460 | } | ||
| 1461 | else | ||
| 1462 | { | ||
| 1463 | *ptr_sem = (gate_platform_semaphore_t)s; | ||
| 1464 | return GATE_RESULT_OK; | ||
| 1465 | } | ||
| 1466 | } | ||
| 1467 | gate_result_t gate_platform_semaphore_acquire(gate_platform_semaphore_t* ptr_sem, gate_uint32_t const* ptr_timeout_ms) | ||
| 1468 | { | ||
| 1469 | sem_t** pps = (sem_t**)ptr_sem; | ||
| 1470 | if (!pps || !*pps) | ||
| 1471 | { | ||
| 1472 | return GATE_RESULT_INVALIDARG; | ||
| 1473 | } | ||
| 1474 | |||
| 1475 | if (!ptr_timeout_ms) | ||
| 1476 | { | ||
| 1477 | if (0 == sem_wait(*pps)) | ||
| 1478 | { | ||
| 1479 | /* success case */ | ||
| 1480 | return GATE_RESULT_OK; | ||
| 1481 | } | ||
| 1482 | } | ||
| 1483 | else | ||
| 1484 | { | ||
| 1485 | struct timespec timeout; | ||
| 1486 | int last_error; | ||
| 1487 | |||
| 1488 | create_semaphore_timeout(&timeout, *ptr_timeout_ms, true); | ||
| 1489 | |||
| 1490 | if (0 == sem_timedwait(*pps, &timeout)) | ||
| 1491 | { | ||
| 1492 | /* success case */ | ||
| 1493 | return GATE_RESULT_OK; | ||
| 1494 | } | ||
| 1495 | last_error = gate_posix_errno(NULL); | ||
| 1496 | if (last_error == ETIMEDOUT) | ||
| 1497 | { | ||
| 1498 | return GATE_RESULT_TIMEOUT; | ||
| 1499 | } | ||
| 1500 | } | ||
| 1501 | |||
| 1502 | /* generic error case */ | ||
| 1503 | return GATE_RESULT_FAILED; | ||
| 1504 | } | ||
| 1505 | gate_result_t gate_platform_semaphore_release(gate_platform_semaphore_t* ptr_sem) | ||
| 1506 | { | ||
| 1507 | sem_t** pps = (sem_t**)ptr_sem; | ||
| 1508 | if (!pps || !*pps) | ||
| 1509 | { | ||
| 1510 | return GATE_RESULT_INVALIDARG; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | if (0 == sem_post(*pps)) | ||
| 1514 | { | ||
| 1515 | return GATE_RESULT_OK; | ||
| 1516 | } | ||
| 1517 | return GATE_RESULT_FAILED; | ||
| 1518 | } | ||
| 1519 | gate_result_t gate_platform_semaphore_destroy(gate_platform_semaphore_t* ptr_sem) | ||
| 1520 | { | ||
| 1521 | sem_t** pps = (sem_t**)ptr_sem; | ||
| 1522 | if (!pps || !*pps) | ||
| 1523 | { | ||
| 1524 | return GATE_RESULT_INVALIDARG; | ||
| 1525 | } | ||
| 1526 | if (0 == sem_close(*pps)) | ||
| 1527 | { | ||
| 1528 | gate_mem_dealloc(*pps); | ||
| 1529 | *pps = NULL; | ||
| 1530 | return GATE_RESULT_OK; | ||
| 1531 | } | ||
| 1532 | else | ||
| 1533 | { | ||
| 1534 | return GATE_RESULT_FAILED; | ||
| 1535 | } | ||
| 1536 | } | ||
| 1537 | |||
| 1538 | #else /* GATE_SYS_USE_POSIX_SEMAPHORE */ | ||
| 1539 | |||
| 1540 | #if defined(GATE_SYS_LINUX) || defined(GATE_SYS_NETBSD) || defined(GATE_SYS_FREEBSD) || defined(GATE_SYS_WASM_WASI) | ||
| 1541 | union semun | ||
| 1542 | { | ||
| 1543 | int val; | ||
| 1544 | struct semid_ds* buf; | ||
| 1545 | unsigned short* array; | ||
| 1546 | }; | ||
| 1547 | #endif | ||
| 1548 | |||
| 1549 | 3 | gate_result_t gate_platform_semaphore_create(gate_platform_semaphore_t* ptr_sem, unsigned int count) | |
| 1550 | { | ||
| 1551 | 3 | int* s = (int*)ptr_sem; | |
| 1552 | union semun arg; | ||
| 1553 | |||
| 1554 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!s || (count == 0)) |
| 1555 | { | ||
| 1556 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1557 | } | ||
| 1558 | |||
| 1559 | 3 | *s = semget(IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | SHM_R | SHM_W); | |
| 1560 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (*s == -1) |
| 1561 | { | ||
| 1562 | ✗ | return GATE_RESULT_OUTOFRESOURCES; | |
| 1563 | } | ||
| 1564 | |||
| 1565 | 3 | arg.val = (int)count; | |
| 1566 | 3 | semctl(*s, 0, SETVAL, arg); | |
| 1567 | 3 | return GATE_RESULT_OK; | |
| 1568 | } | ||
| 1569 | |||
| 1570 | typedef int(*semtimedop_func_ptr)(int semid, struct sembuf* sops, unsigned nsops, struct timespec* timeout); | ||
| 1571 | |||
| 1572 | #if defined(HAVE_SEMTIMEDOP) | ||
| 1573 | |||
| 1574 | 4 | static int semtimedop_wrapper(int semid, struct sembuf* sops, unsigned nsops, struct timespec* timeout) | |
| 1575 | { | ||
| 1576 | 4 | return semtimedop(semid, sops, nsops, timeout); | |
| 1577 | } | ||
| 1578 | |||
| 1579 | 4 | static semtimedop_func_ptr get_semtimedop() | |
| 1580 | { | ||
| 1581 | 4 | return &semtimedop_wrapper; | |
| 1582 | } | ||
| 1583 | |||
| 1584 | #else | ||
| 1585 | |||
| 1586 | static int fallback_semtimedop(int semid, struct sembuf* sops, unsigned nsops, struct timespec* timeout) | ||
| 1587 | { | ||
| 1588 | int ret = -1; | ||
| 1589 | struct sembuf op[1024]; | ||
| 1590 | unsigned index; | ||
| 1591 | struct timespec now; | ||
| 1592 | long seconds = 0; | ||
| 1593 | long nanoseconds = 0; | ||
| 1594 | |||
| 1595 | gate_posix_clock_gettime_monotonic(&seconds, &nanoseconds); | ||
| 1596 | now.tv_sec = seconds; | ||
| 1597 | now.tv_nsec = nanoseconds; | ||
| 1598 | do | ||
| 1599 | { | ||
| 1600 | int errcode = 0; | ||
| 1601 | const long nsec = (now.tv_nsec + timeout->tv_nsec); | ||
| 1602 | struct timespec end; | ||
| 1603 | struct timespec sleep_timeout; | ||
| 1604 | end.tv_sec = (now.tv_sec + timeout->tv_sec) + (nsec) / 1000000000; | ||
| 1605 | end.tv_nsec = nsec % 1000000000; | ||
| 1606 | |||
| 1607 | if (nsops > sizeof(op) / sizeof(op[0])) | ||
| 1608 | { | ||
| 1609 | nsops = sizeof(op) / sizeof(op[0]); | ||
| 1610 | } | ||
| 1611 | |||
| 1612 | for (index = 0; index != nsops; ++index) | ||
| 1613 | { | ||
| 1614 | op[index].sem_num = sops[index].sem_num; | ||
| 1615 | op[index].sem_op = sops[index].sem_op; | ||
| 1616 | op[index].sem_flg = sops[index].sem_flg | IPC_NOWAIT; | ||
| 1617 | } | ||
| 1618 | |||
| 1619 | sleep_timeout.tv_sec = 0; | ||
| 1620 | sleep_timeout.tv_nsec = 10; | ||
| 1621 | do | ||
| 1622 | { | ||
| 1623 | ret = semop(semid, &op[0], nsops); | ||
| 1624 | if (ret == 0) | ||
| 1625 | { | ||
| 1626 | /* operation succeeded, we are done (ret == 0) */ | ||
| 1627 | break; | ||
| 1628 | } | ||
| 1629 | errcode = gate_posix_errno(NULL); | ||
| 1630 | |||
| 1631 | |||
| 1632 | if (!gate_posix_clock_gettime_monotonic(&seconds, &nanoseconds)) | ||
| 1633 | { | ||
| 1634 | break; | ||
| 1635 | } | ||
| 1636 | now.tv_sec = seconds; | ||
| 1637 | now.tv_nsec = nanoseconds; | ||
| 1638 | |||
| 1639 | if ((now.tv_sec > end.tv_sec) || ((now.tv_sec == end.tv_sec) && (now.tv_nsec > end.tv_nsec))) | ||
| 1640 | { | ||
| 1641 | /* timeout reached, (ret == -1) */ | ||
| 1642 | gate_posix_set_errno(EAGAIN); | ||
| 1643 | break; | ||
| 1644 | } | ||
| 1645 | nanosleep(&sleep_timeout, NULL); | ||
| 1646 | if (sleep_timeout.tv_nsec < 50000000) | ||
| 1647 | { | ||
| 1648 | sleep_timeout.tv_nsec *= 2; | ||
| 1649 | } | ||
| 1650 | } while (errcode == EAGAIN); | ||
| 1651 | } while (0); | ||
| 1652 | return ret; | ||
| 1653 | } | ||
| 1654 | |||
| 1655 | static semtimedop_func_ptr get_semtimedop() | ||
| 1656 | { | ||
| 1657 | static semtimedop_func_ptr semtimedop_func = NULL; | ||
| 1658 | if (!semtimedop_func) | ||
| 1659 | { | ||
| 1660 | semtimedop_func = (semtimedop_func_ptr)gate_posix_global_sym("semtimedop"); | ||
| 1661 | if (!semtimedop_func) | ||
| 1662 | { | ||
| 1663 | semtimedop_func = &fallback_semtimedop; | ||
| 1664 | } | ||
| 1665 | } | ||
| 1666 | return semtimedop_func; | ||
| 1667 | } | ||
| 1668 | |||
| 1669 | #endif | ||
| 1670 | |||
| 1671 | 8 | gate_result_t gate_platform_semaphore_acquire(gate_platform_semaphore_t* ptr_sem, gate_uint32_t const* ptr_timeout_ms) | |
| 1672 | { | ||
| 1673 | 8 | int* s = (int*)ptr_sem; | |
| 1674 | struct sembuf op; | ||
| 1675 | |||
| 1676 |
2/4✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
8 | if (!s || (*s == -1)) |
| 1677 | { | ||
| 1678 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1679 | } | ||
| 1680 | |||
| 1681 | 8 | op.sem_num = 0; | |
| 1682 | 8 | op.sem_op = -1; | |
| 1683 | 8 | op.sem_flg = SEM_UNDO; | |
| 1684 | |||
| 1685 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | if (!ptr_timeout_ms) |
| 1686 | { | ||
| 1687 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if (-1 == semop(*s, &op, 1)) |
| 1688 | { | ||
| 1689 | ✗ | return GATE_RESULT_FAILED; | |
| 1690 | } | ||
| 1691 | } | ||
| 1692 | else | ||
| 1693 | { | ||
| 1694 | struct timespec timeout; | ||
| 1695 | 4 | semtimedop_func_ptr semtimedop_func = get_semtimedop(); | |
| 1696 | |||
| 1697 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (semtimedop_func == NULL) |
| 1698 | { | ||
| 1699 | 2 | return GATE_RESULT_NOTSUPPORTED; | |
| 1700 | } | ||
| 1701 | |||
| 1702 | 4 | create_semaphore_timeout(&timeout, *ptr_timeout_ms, false); | |
| 1703 | |||
| 1704 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
4 | if (-1 == semtimedop_func(*s, &op, 1, &timeout)) |
| 1705 | { | ||
| 1706 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (gate_posix_errno(NULL) == EAGAIN) |
| 1707 | { | ||
| 1708 | 2 | return GATE_RESULT_TIMEOUT; | |
| 1709 | } | ||
| 1710 | else | ||
| 1711 | { | ||
| 1712 | ✗ | return GATE_RESULT_FAILED; | |
| 1713 | } | ||
| 1714 | } | ||
| 1715 | } | ||
| 1716 | |||
| 1717 | /* generic success case */ | ||
| 1718 | 6 | return GATE_RESULT_OK; | |
| 1719 | } | ||
| 1720 | |||
| 1721 | 6 | gate_result_t gate_platform_semaphore_release(gate_platform_semaphore_t* ptr_sem) | |
| 1722 | { | ||
| 1723 | 6 | int* s = (int*)ptr_sem; | |
| 1724 | struct sembuf op; | ||
| 1725 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if (!s || (*s == -1)) |
| 1726 | { | ||
| 1727 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1728 | } | ||
| 1729 | |||
| 1730 | 6 | op.sem_num = 0; | |
| 1731 | 6 | op.sem_op = 1; | |
| 1732 | 6 | op.sem_flg = SEM_UNDO; | |
| 1733 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (-1 == semop(*s, &op, 1)) |
| 1734 | { | ||
| 1735 | ✗ | return GATE_RESULT_FAILED; | |
| 1736 | } | ||
| 1737 | 6 | return GATE_RESULT_OK; | |
| 1738 | } | ||
| 1739 | 3 | gate_result_t gate_platform_semaphore_destroy(gate_platform_semaphore_t* ptr_sem) | |
| 1740 | { | ||
| 1741 | 3 | int* s = (int*)ptr_sem; | |
| 1742 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!s || (*s == -1)) |
| 1743 | { | ||
| 1744 | ✗ | return GATE_RESULT_INVALIDARG; | |
| 1745 | } | ||
| 1746 | |||
| 1747 | 3 | semctl(*s, 0, IPC_RMID, NULL); | |
| 1748 | 3 | *s = -1; | |
| 1749 | 3 | return GATE_RESULT_OK; | |
| 1750 | } | ||
| 1751 | |||
| 1752 | #endif /* GATE_SYS_USE_POSIX_SEMAPHORE*/ | ||
| 1753 | |||
| 1754 | 20 | gate_bool_t gate_platform_yield() | |
| 1755 | { | ||
| 1756 | 20 | sched_yield(); | |
| 1757 | 20 | return true; | |
| 1758 | } | ||
| 1759 | |||
| 1760 | #endif /* GATE_SYS_POSIX */ | ||
| 1761 |