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