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