Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file compat.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief compatibility functions |
| 5 | * |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 6 | * Copyright (c) 2021 - 2023 CESNET, z.s.p.o. |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 14 | #define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */ |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 15 | #define _ISOC99_SOURCE /* vsnprintf */ |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 16 | |
| 17 | #include "compat.h" |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 18 | |
roman | 8b1a6c3 | 2023-10-26 13:35:22 +0200 | [diff] [blame] | 19 | #include <crypt.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 20 | #include <errno.h> |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 21 | #include <inttypes.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 22 | #include <limits.h> |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 23 | #include <pthread.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 24 | #include <stdarg.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <time.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 32 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 33 | int |
| 34 | pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) |
| 35 | { |
| 36 | int64_t nsec_diff; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 37 | struct timespec cur, dur; |
| 38 | int rc; |
| 39 | |
| 40 | /* try to acquire the lock and, if we fail, sleep for 5ms. */ |
| 41 | while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) { |
| 42 | /* get time */ |
| 43 | clock_gettime(COMPAT_CLOCK_ID, &cur); |
| 44 | |
| 45 | /* get time diff */ |
| 46 | nsec_diff = 0; |
| 47 | nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L; |
| 48 | nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 49 | |
Jan Kundrát | 922885a | 2023-06-05 18:12:39 +0200 | [diff] [blame] | 50 | if (nsec_diff <= 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 51 | /* timeout */ |
Jan Kundrát | 2a2b6de | 2023-06-05 18:19:49 +0200 | [diff] [blame] | 52 | rc = ETIMEDOUT; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 53 | break; |
Jan Kundrát | 922885a | 2023-06-05 18:12:39 +0200 | [diff] [blame] | 54 | } else if (nsec_diff < 5000000) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 55 | /* sleep until timeout */ |
| 56 | dur.tv_sec = 0; |
Jan Kundrát | 922885a | 2023-06-05 18:12:39 +0200 | [diff] [blame] | 57 | dur.tv_nsec = nsec_diff; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 58 | } else { |
| 59 | /* sleep 5 ms */ |
| 60 | dur.tv_sec = 0; |
| 61 | dur.tv_nsec = 5000000; |
| 62 | } |
| 63 | |
| 64 | nanosleep(&dur, NULL); |
| 65 | } |
| 66 | |
| 67 | return rc; |
| 68 | } |
| 69 | |
| 70 | #endif |
| 71 | |
| 72 | #ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK |
| 73 | int |
| 74 | pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime) |
| 75 | { |
| 76 | /* only real time supported without this function */ |
| 77 | if (clockid != CLOCK_REALTIME) { |
| 78 | return EINVAL; |
| 79 | } |
| 80 | |
| 81 | return pthread_mutex_timedlock(mutex, abstime); |
| 82 | } |
| 83 | |
| 84 | #endif |
| 85 | |
Michal Vasko | 62ba134 | 2023-10-17 08:54:26 +0200 | [diff] [blame] | 86 | #ifndef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK |
| 87 | int |
| 88 | pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime) |
| 89 | { |
| 90 | int64_t nsec_diff; |
| 91 | struct timespec cur, dur; |
| 92 | int rc; |
| 93 | |
| 94 | /* try to acquire the lock and, if we fail, sleep for 5ms. */ |
| 95 | while ((rc = pthread_rwlock_tryrdlock(rwlock)) == EBUSY) { |
| 96 | /* get time */ |
| 97 | clock_gettime(COMPAT_CLOCK_ID, &cur); |
| 98 | |
| 99 | /* get time diff */ |
| 100 | nsec_diff = 0; |
| 101 | nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L; |
| 102 | nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec); |
| 103 | |
| 104 | if (nsec_diff <= 0) { |
| 105 | /* timeout */ |
| 106 | rc = ETIMEDOUT; |
| 107 | break; |
| 108 | } else if (nsec_diff < 5000000) { |
| 109 | /* sleep until timeout */ |
| 110 | dur.tv_sec = 0; |
| 111 | dur.tv_nsec = nsec_diff; |
| 112 | } else { |
| 113 | /* sleep 5 ms */ |
| 114 | dur.tv_sec = 0; |
| 115 | dur.tv_nsec = 5000000; |
| 116 | } |
| 117 | |
| 118 | nanosleep(&dur, NULL); |
| 119 | } |
| 120 | |
| 121 | return rc; |
| 122 | } |
| 123 | |
| 124 | #endif |
| 125 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 126 | #ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK |
| 127 | int |
| 128 | pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime) |
| 129 | { |
| 130 | /* only real time supported without this function */ |
| 131 | if (clockid != CLOCK_REALTIME) { |
| 132 | return EINVAL; |
| 133 | } |
| 134 | |
| 135 | return pthread_rwlock_timedrdlock(rwlock, abstime); |
| 136 | } |
| 137 | |
| 138 | #endif |
| 139 | |
Michal Vasko | 62ba134 | 2023-10-17 08:54:26 +0200 | [diff] [blame] | 140 | #ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK |
| 141 | int |
| 142 | pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime) |
| 143 | { |
| 144 | int64_t nsec_diff; |
| 145 | struct timespec cur, dur; |
| 146 | int rc; |
| 147 | |
| 148 | /* try to acquire the lock and, if we fail, sleep for 5ms. */ |
| 149 | while ((rc = pthread_rwlock_trywrlock(rwlock)) == EBUSY) { |
| 150 | /* get time */ |
| 151 | clock_gettime(COMPAT_CLOCK_ID, &cur); |
| 152 | |
| 153 | /* get time diff */ |
| 154 | nsec_diff = 0; |
| 155 | nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L; |
| 156 | nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec); |
| 157 | |
| 158 | if (nsec_diff <= 0) { |
| 159 | /* timeout */ |
| 160 | rc = ETIMEDOUT; |
| 161 | break; |
| 162 | } else if (nsec_diff < 5000000) { |
| 163 | /* sleep until timeout */ |
| 164 | dur.tv_sec = 0; |
| 165 | dur.tv_nsec = nsec_diff; |
| 166 | } else { |
| 167 | /* sleep 5 ms */ |
| 168 | dur.tv_sec = 0; |
| 169 | dur.tv_nsec = 5000000; |
| 170 | } |
| 171 | |
| 172 | nanosleep(&dur, NULL); |
| 173 | } |
| 174 | |
| 175 | return rc; |
| 176 | } |
| 177 | |
| 178 | #endif |
| 179 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 180 | #ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK |
| 181 | int |
| 182 | pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime) |
| 183 | { |
| 184 | /* only real time supported without this function */ |
| 185 | if (clockid != CLOCK_REALTIME) { |
| 186 | return EINVAL; |
| 187 | } |
| 188 | |
| 189 | return pthread_rwlock_timedwrlock(rwlock, abstime); |
| 190 | } |
| 191 | |
| 192 | #endif |
| 193 | |
| 194 | #ifndef HAVE_PTHREAD_COND_CLOCKWAIT |
| 195 | int |
| 196 | pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t UNUSED(clockid), |
| 197 | const struct timespec *abstime) |
| 198 | { |
| 199 | /* assume the correct clock is set during cond init */ |
| 200 | return pthread_cond_timedwait(cond, mutex, abstime); |
| 201 | } |
| 202 | |
| 203 | #endif |
| 204 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 205 | #ifndef HAVE_VDPRINTF |
| 206 | int |
| 207 | vdprintf(int fd, const char *format, va_list ap) |
| 208 | { |
| 209 | FILE *stream; |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 210 | int count = 0; |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 211 | |
| 212 | stream = fdopen(dup(fd), "a+"); |
| 213 | if (stream) { |
| 214 | count = vfprintf(stream, format, ap); |
| 215 | fclose(stream); |
| 216 | } |
| 217 | return count; |
| 218 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 219 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 220 | #endif |
| 221 | |
| 222 | #ifndef HAVE_ASPRINTF |
| 223 | int |
| 224 | asprintf(char **strp, const char *fmt, ...) |
| 225 | { |
| 226 | int ret; |
| 227 | va_list ap; |
| 228 | |
| 229 | va_start(ap, fmt); |
| 230 | ret = vasprintf(strp, fmt, ap); |
| 231 | va_end(ap); |
| 232 | return ret; |
| 233 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 234 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 235 | #endif |
| 236 | |
| 237 | #ifndef HAVE_VASPRINTF |
| 238 | int |
| 239 | vasprintf(char **strp, const char *fmt, va_list ap) |
| 240 | { |
| 241 | va_list ap2; |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 242 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 243 | va_copy(ap2, ap); |
| 244 | int l = vsnprintf(0, 0, fmt, ap2); |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 245 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 246 | va_end(ap2); |
| 247 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 248 | if ((l < 0) || !(*strp = malloc(l + 1U))) { |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | return vsnprintf(*strp, l + 1U, fmt, ap); |
| 253 | } |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 254 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 255 | #endif |
| 256 | |
| 257 | #ifndef HAVE_GETLINE |
| 258 | ssize_t |
| 259 | getline(char **lineptr, size_t *n, FILE *stream) |
| 260 | { |
| 261 | static char line[256]; |
| 262 | char *ptr; |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 263 | ssize_t len; |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 264 | |
| 265 | if (!lineptr || !n) { |
| 266 | errno = EINVAL; |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | if (ferror(stream) || feof(stream)) { |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | if (!fgets(line, 256, stream)) { |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | ptr = strchr(line, '\n'); |
| 279 | if (ptr) { |
| 280 | *ptr = '\0'; |
| 281 | } |
| 282 | |
| 283 | len = strlen(line); |
| 284 | |
| 285 | if (len + 1 < 256) { |
| 286 | ptr = realloc(*lineptr, 256); |
| 287 | if (!ptr) { |
| 288 | return -1; |
| 289 | } |
| 290 | *lineptr = ptr; |
| 291 | *n = 256; |
| 292 | } |
| 293 | |
| 294 | strcpy(*lineptr, line); |
| 295 | return len; |
| 296 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 297 | |
| 298 | #endif |
| 299 | |
| 300 | #ifndef HAVE_STRNDUP |
| 301 | char * |
| 302 | strndup(const char *s, size_t n) |
| 303 | { |
| 304 | char *buf; |
| 305 | size_t len = 0; |
| 306 | |
| 307 | /* strnlen */ |
| 308 | for ( ; (len < n) && (s[len] != '\0'); ++len) {} |
| 309 | |
| 310 | if (!(buf = malloc(len + 1U))) { |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | memcpy(buf, s, len); |
| 315 | buf[len] = '\0'; |
| 316 | return buf; |
| 317 | } |
| 318 | |
| 319 | #endif |
| 320 | |
| 321 | #ifndef HAVE_STRNSTR |
| 322 | char * |
| 323 | strnstr(const char *s, const char *find, size_t slen) |
| 324 | { |
| 325 | char c, sc; |
| 326 | size_t len; |
| 327 | |
| 328 | if ((c = *find++) != '\0') { |
| 329 | len = strlen(find); |
| 330 | do { |
| 331 | do { |
| 332 | if ((slen-- < 1) || ((sc = *s++) == '\0')) { |
| 333 | return NULL; |
| 334 | } |
| 335 | } while (sc != c); |
| 336 | if (len > slen) { |
| 337 | return NULL; |
| 338 | } |
| 339 | } while (strncmp(s, find, len)); |
| 340 | s--; |
| 341 | } |
| 342 | return (char *)s; |
| 343 | } |
| 344 | |
| 345 | #endif |
| 346 | |
| 347 | #ifndef HAVE_STRCHRNUL |
| 348 | char * |
| 349 | strchrnul(const char *s, int c) |
| 350 | { |
| 351 | char *p = strchr(s, c); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 352 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 353 | return p ? p : (char *)s + strlen(s); |
| 354 | } |
| 355 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 356 | #endif |
| 357 | |
| 358 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 359 | char * |
| 360 | get_current_dir_name(void) |
| 361 | { |
| 362 | char tmp[_POSIX_PATH_MAX]; |
| 363 | char *retval = NULL; |
| 364 | |
| 365 | if (getcwd(tmp, sizeof(tmp))) { |
| 366 | retval = strdup(tmp); |
| 367 | if (!retval) { |
| 368 | errno = ENOMEM; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return retval; |
| 373 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 374 | |
| 375 | #endif |
roman | 8b1a6c3 | 2023-10-26 13:35:22 +0200 | [diff] [blame] | 376 | |
| 377 | #ifndef HAVE_CRYPT_R |
| 378 | char * |
| 379 | crypt_r(const char *phrase, const char *setting, struct crypt_data *data) |
| 380 | { |
| 381 | static pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER; |
| 382 | char *hash; |
| 383 | |
| 384 | (void) data; |
| 385 | |
| 386 | pthread_mutex_lock(&crypt_lock); |
| 387 | hash = crypt(phrase, setting); |
| 388 | pthread_mutex_unlock(&crypt_lock); |
| 389 | |
| 390 | return hash; |
| 391 | } |
| 392 | |
| 393 | #endif |