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; |
| 36 | int32_t diff; |
| 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); |
| 49 | diff = (nsec_diff ? nsec_diff / 1000000L : 0); |
| 50 | |
| 51 | if (diff < 1) { |
| 52 | /* timeout */ |
| 53 | break; |
| 54 | } else if (diff < 5) { |
| 55 | /* sleep until timeout */ |
| 56 | dur.tv_sec = 0; |
| 57 | dur.tv_nsec = (long)diff * 1000000; |
| 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 | |
| 86 | #ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK |
| 87 | int |
| 88 | pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime) |
| 89 | { |
| 90 | /* only real time supported without this function */ |
| 91 | if (clockid != CLOCK_REALTIME) { |
| 92 | return EINVAL; |
| 93 | } |
| 94 | |
| 95 | return pthread_rwlock_timedrdlock(rwlock, abstime); |
| 96 | } |
| 97 | |
| 98 | #endif |
| 99 | |
| 100 | #ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK |
| 101 | int |
| 102 | pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime) |
| 103 | { |
| 104 | /* only real time supported without this function */ |
| 105 | if (clockid != CLOCK_REALTIME) { |
| 106 | return EINVAL; |
| 107 | } |
| 108 | |
| 109 | return pthread_rwlock_timedwrlock(rwlock, abstime); |
| 110 | } |
| 111 | |
| 112 | #endif |
| 113 | |
| 114 | #ifndef HAVE_PTHREAD_COND_CLOCKWAIT |
| 115 | int |
| 116 | pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t UNUSED(clockid), |
| 117 | const struct timespec *abstime) |
| 118 | { |
| 119 | /* assume the correct clock is set during cond init */ |
| 120 | return pthread_cond_timedwait(cond, mutex, abstime); |
| 121 | } |
| 122 | |
| 123 | #endif |
| 124 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 125 | #ifndef HAVE_VDPRINTF |
| 126 | int |
| 127 | vdprintf(int fd, const char *format, va_list ap) |
| 128 | { |
| 129 | FILE *stream; |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 130 | int count = 0; |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 131 | |
| 132 | stream = fdopen(dup(fd), "a+"); |
| 133 | if (stream) { |
| 134 | count = vfprintf(stream, format, ap); |
| 135 | fclose(stream); |
| 136 | } |
| 137 | return count; |
| 138 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 139 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | #ifndef HAVE_ASPRINTF |
| 143 | int |
| 144 | asprintf(char **strp, const char *fmt, ...) |
| 145 | { |
| 146 | int ret; |
| 147 | va_list ap; |
| 148 | |
| 149 | va_start(ap, fmt); |
| 150 | ret = vasprintf(strp, fmt, ap); |
| 151 | va_end(ap); |
| 152 | return ret; |
| 153 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 154 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 155 | #endif |
| 156 | |
| 157 | #ifndef HAVE_VASPRINTF |
| 158 | int |
| 159 | vasprintf(char **strp, const char *fmt, va_list ap) |
| 160 | { |
| 161 | va_list ap2; |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 162 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 163 | va_copy(ap2, ap); |
| 164 | int l = vsnprintf(0, 0, fmt, ap2); |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 165 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 166 | va_end(ap2); |
| 167 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 168 | if ((l < 0) || !(*strp = malloc(l + 1U))) { |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | return vsnprintf(*strp, l + 1U, fmt, ap); |
| 173 | } |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 174 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 175 | #endif |
| 176 | |
| 177 | #ifndef HAVE_GETLINE |
| 178 | ssize_t |
| 179 | getline(char **lineptr, size_t *n, FILE *stream) |
| 180 | { |
| 181 | static char line[256]; |
| 182 | char *ptr; |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 183 | ssize_t len; |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 184 | |
| 185 | if (!lineptr || !n) { |
| 186 | errno = EINVAL; |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | if (ferror(stream) || feof(stream)) { |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | if (!fgets(line, 256, stream)) { |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | ptr = strchr(line, '\n'); |
| 199 | if (ptr) { |
| 200 | *ptr = '\0'; |
| 201 | } |
| 202 | |
| 203 | len = strlen(line); |
| 204 | |
| 205 | if (len + 1 < 256) { |
| 206 | ptr = realloc(*lineptr, 256); |
| 207 | if (!ptr) { |
| 208 | return -1; |
| 209 | } |
| 210 | *lineptr = ptr; |
| 211 | *n = 256; |
| 212 | } |
| 213 | |
| 214 | strcpy(*lineptr, line); |
| 215 | return len; |
| 216 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 217 | |
| 218 | #endif |
| 219 | |
| 220 | #ifndef HAVE_STRNDUP |
| 221 | char * |
| 222 | strndup(const char *s, size_t n) |
| 223 | { |
| 224 | char *buf; |
| 225 | size_t len = 0; |
| 226 | |
| 227 | /* strnlen */ |
| 228 | for ( ; (len < n) && (s[len] != '\0'); ++len) {} |
| 229 | |
| 230 | if (!(buf = malloc(len + 1U))) { |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | memcpy(buf, s, len); |
| 235 | buf[len] = '\0'; |
| 236 | return buf; |
| 237 | } |
| 238 | |
| 239 | #endif |
| 240 | |
| 241 | #ifndef HAVE_STRNSTR |
| 242 | char * |
| 243 | strnstr(const char *s, const char *find, size_t slen) |
| 244 | { |
| 245 | char c, sc; |
| 246 | size_t len; |
| 247 | |
| 248 | if ((c = *find++) != '\0') { |
| 249 | len = strlen(find); |
| 250 | do { |
| 251 | do { |
| 252 | if ((slen-- < 1) || ((sc = *s++) == '\0')) { |
| 253 | return NULL; |
| 254 | } |
| 255 | } while (sc != c); |
| 256 | if (len > slen) { |
| 257 | return NULL; |
| 258 | } |
| 259 | } while (strncmp(s, find, len)); |
| 260 | s--; |
| 261 | } |
| 262 | return (char *)s; |
| 263 | } |
| 264 | |
| 265 | #endif |
| 266 | |
| 267 | #ifndef HAVE_STRCHRNUL |
| 268 | char * |
| 269 | strchrnul(const char *s, int c) |
| 270 | { |
| 271 | char *p = strchr(s, c); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 272 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 273 | return p ? p : (char *)s + strlen(s); |
| 274 | } |
| 275 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 276 | #endif |
| 277 | |
| 278 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 279 | char * |
| 280 | get_current_dir_name(void) |
| 281 | { |
| 282 | char tmp[_POSIX_PATH_MAX]; |
| 283 | char *retval = NULL; |
| 284 | |
| 285 | if (getcwd(tmp, sizeof(tmp))) { |
| 286 | retval = strdup(tmp); |
| 287 | if (!retval) { |
| 288 | errno = ENOMEM; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return retval; |
| 293 | } |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 294 | |
| 295 | #endif |