blob: 88a3b6982c2b4faebfd86cdb7a13026c3d6ee855 [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
roman8b1a6c32023-10-26 13:35:22 +020019#include <crypt.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020020#include <errno.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020021#include <inttypes.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020022#include <limits.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020023#include <pthread.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020024#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020027#include <string.h>
28#include <sys/time.h>
29#include <time.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020030#include <unistd.h>
31
Michal Vaskod8a74192023-02-06 15:51:50 +010032#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
33int
34pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
35{
36 int64_t nsec_diff;
Michal Vaskod8a74192023-02-06 15:51:50 +010037 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 Vaskod8a74192023-02-06 15:51:50 +010049
Jan Kundrát922885a2023-06-05 18:12:39 +020050 if (nsec_diff <= 0) {
Michal Vaskod8a74192023-02-06 15:51:50 +010051 /* timeout */
Jan Kundrát2a2b6de2023-06-05 18:19:49 +020052 rc = ETIMEDOUT;
Michal Vaskod8a74192023-02-06 15:51:50 +010053 break;
Jan Kundrát922885a2023-06-05 18:12:39 +020054 } else if (nsec_diff < 5000000) {
Michal Vaskod8a74192023-02-06 15:51:50 +010055 /* sleep until timeout */
56 dur.tv_sec = 0;
Jan Kundrát922885a2023-06-05 18:12:39 +020057 dur.tv_nsec = nsec_diff;
Michal Vaskod8a74192023-02-06 15:51:50 +010058 } 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
73int
74pthread_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 Vasko62ba1342023-10-17 08:54:26 +020086#ifndef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
87int
88pthread_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 Vaskod8a74192023-02-06 15:51:50 +0100126#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
127int
128pthread_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 Vasko62ba1342023-10-17 08:54:26 +0200140#ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
141int
142pthread_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 Vaskod8a74192023-02-06 15:51:50 +0100180#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
181int
182pthread_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
195int
196pthread_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 Vasko9e8ac262020-04-07 13:06:45 +0200205#ifndef HAVE_VDPRINTF
206int
207vdprintf(int fd, const char *format, va_list ap)
208{
209 FILE *stream;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200210 int count = 0;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200211
212 stream = fdopen(dup(fd), "a+");
213 if (stream) {
214 count = vfprintf(stream, format, ap);
215 fclose(stream);
216 }
217 return count;
218}
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200219
Michal Vasko9e8ac262020-04-07 13:06:45 +0200220#endif
221
222#ifndef HAVE_ASPRINTF
223int
224asprintf(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 Vasko7a20d2e2021-05-19 16:40:23 +0200234
Michal Vasko9e8ac262020-04-07 13:06:45 +0200235#endif
236
237#ifndef HAVE_VASPRINTF
238int
239vasprintf(char **strp, const char *fmt, va_list ap)
240{
241 va_list ap2;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200242
Michal Vasko9e8ac262020-04-07 13:06:45 +0200243 va_copy(ap2, ap);
244 int l = vsnprintf(0, 0, fmt, ap2);
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200245
Michal Vasko9e8ac262020-04-07 13:06:45 +0200246 va_end(ap2);
247
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200248 if ((l < 0) || !(*strp = malloc(l + 1U))) {
Michal Vasko9e8ac262020-04-07 13:06:45 +0200249 return -1;
250 }
251
252 return vsnprintf(*strp, l + 1U, fmt, ap);
253}
Michal Vasko9e8ac262020-04-07 13:06:45 +0200254
Michal Vasko9e8ac262020-04-07 13:06:45 +0200255#endif
256
257#ifndef HAVE_GETLINE
258ssize_t
259getline(char **lineptr, size_t *n, FILE *stream)
260{
261 static char line[256];
262 char *ptr;
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200263 ssize_t len;
Michal Vasko9e8ac262020-04-07 13:06:45 +0200264
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 Vasko7a20d2e2021-05-19 16:40:23 +0200297
298#endif
299
300#ifndef HAVE_STRNDUP
301char *
302strndup(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
322char *
323strnstr(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
348char *
349strchrnul(const char *s, int c)
350{
351 char *p = strchr(s, c);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200352
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200353 return p ? p : (char *)s + strlen(s);
354}
355
Michal Vasko9e8ac262020-04-07 13:06:45 +0200356#endif
357
358#ifndef HAVE_GET_CURRENT_DIR_NAME
359char *
360get_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 Vasko7a20d2e2021-05-19 16:40:23 +0200374
375#endif
roman8b1a6c32023-10-26 13:35:22 +0200376
377#ifndef HAVE_CRYPT_R
378char *
379crypt_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