blob: d0495b28036ecf3fb7df6b136e3de46cfa55e710 [file] [log] [blame]
Michal Vasko9e8ac262020-04-07 13:06:45 +02001/**
2 * @file compat.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief compatibility functions
5 *
Michal Vaskod8a74192023-02-06 15:51:50 +01006 * Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
Michal Vasko9e8ac262020-04-07 13:06:45 +02007 *
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 Vasko7a20d2e2021-05-19 16:40:23 +020014#define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */
Michal Vasko9e8ac262020-04-07 13:06:45 +020015#define _ISOC99_SOURCE /* vsnprintf */
Michal Vasko7a20d2e2021-05-19 16:40:23 +020016
17#include "compat.h"
Michal Vasko9e8ac262020-04-07 13:06:45 +020018
19#include <errno.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020020#include <inttypes.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020021#include <limits.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020022#include <pthread.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020023#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020026#include <string.h>
27#include <sys/time.h>
28#include <time.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020029#include <unistd.h>
30
Michal Vaskod8a74192023-02-06 15:51:50 +010031#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
32int
33pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
34{
35 int64_t nsec_diff;
Michal Vaskod8a74192023-02-06 15:51:50 +010036 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 Vaskod8a74192023-02-06 15:51:50 +010048
Jan Kundrát922885a2023-06-05 18:12:39 +020049 if (nsec_diff <= 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +010050 /* timeout */
Jan Kundrát2a2b6de2023-06-05 18:19:49 +020051 rc = ETIMEDOUT;
Michal Vaskod8a74192023-02-06 15:51:50 +010052 break;
Jan Kundrát922885a2023-06-05 18:12:39 +020053 } else if (nsec_diff < 5000000) {
Michal Vaskod8a74192023-02-06 15:51:50 +010054 /* sleep until timeout */
55 dur.tv_sec = 0;
Jan Kundrát922885a2023-06-05 18:12:39 +020056 dur.tv_nsec = nsec_diff;
Michal Vaskod8a74192023-02-06 15:51:50 +010057 } 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
72int
73pthread_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 Vasko62ba1342023-10-17 08:54:26 +020085#ifndef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
86int
87pthread_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 Vaskod8a74192023-02-06 15:51:50 +0100125#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
126int
127pthread_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 Vasko62ba1342023-10-17 08:54:26 +0200139#ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
140int
141pthread_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 Vaskod8a74192023-02-06 15:51:50 +0100179#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
180int
181pthread_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
194int
195pthread_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 Vasko9e8ac262020-04-07 13:06:45 +0200204#ifndef HAVE_VDPRINTF
205int
206vdprintf(int fd, const char *format, va_list ap)
207{
208 FILE *stream;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200209 int count = 0;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200210
211 stream = fdopen(dup(fd), "a+");
212 if (stream) {
213 count = vfprintf(stream, format, ap);
214 fclose(stream);
215 }
216 return count;
217}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200218
Michal Vasko9e8ac262020-04-07 13:06:45 +0200219#endif
220
221#ifndef HAVE_ASPRINTF
222int
223asprintf(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 Vasko7a20d2e2021-05-19 16:40:23 +0200233
Michal Vasko9e8ac262020-04-07 13:06:45 +0200234#endif
235
236#ifndef HAVE_VASPRINTF
237int
238vasprintf(char **strp, const char *fmt, va_list ap)
239{
240 va_list ap2;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200241
Michal Vasko9e8ac262020-04-07 13:06:45 +0200242 va_copy(ap2, ap);
243 int l = vsnprintf(0, 0, fmt, ap2);
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200244
Michal Vasko9e8ac262020-04-07 13:06:45 +0200245 va_end(ap2);
246
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200247 if ((l < 0) || !(*strp = malloc(l + 1U))) {
Michal Vasko9e8ac262020-04-07 13:06:45 +0200248 return -1;
249 }
250
251 return vsnprintf(*strp, l + 1U, fmt, ap);
252}
Michal Vasko9e8ac262020-04-07 13:06:45 +0200253
Michal Vasko9e8ac262020-04-07 13:06:45 +0200254#endif
255
256#ifndef HAVE_GETLINE
257ssize_t
258getline(char **lineptr, size_t *n, FILE *stream)
259{
260 static char line[256];
261 char *ptr;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200262 ssize_t len;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200263
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 Vasko7a20d2e2021-05-19 16:40:23 +0200296
297#endif
298
299#ifndef HAVE_STRNDUP
300char *
301strndup(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
321char *
322strnstr(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
347char *
348strchrnul(const char *s, int c)
349{
350 char *p = strchr(s, c);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200351
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200352 return p ? p : (char *)s + strlen(s);
353}
354
Michal Vasko9e8ac262020-04-07 13:06:45 +0200355#endif
356
357#ifndef HAVE_GET_CURRENT_DIR_NAME
358char *
359get_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 Vasko7a20d2e2021-05-19 16:40:23 +0200373
374#endif