1
0
Fork 0

Merging upstream version 3.5.5 (Closes: #1098233).

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-18 11:33:30 +01:00
parent c86ae7dcba
commit 6af28b7e8e
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
144 changed files with 43534 additions and 11497 deletions

View file

@ -3,7 +3,7 @@
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief compatibility functions
*
* Copyright (c) 2021 CESNET, z.s.p.o.
* Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
#include "compat.h"
#include <crypt.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
@ -28,6 +29,179 @@
#include <time.h>
#include <unistd.h>
#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
int
pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
{
int64_t nsec_diff;
struct timespec cur, dur;
int rc;
/* try to acquire the lock and, if we fail, sleep for 5ms. */
while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
/* get time */
clock_gettime(COMPAT_CLOCK_ID, &cur);
/* get time diff */
nsec_diff = 0;
nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
if (nsec_diff <= 0) {
/* timeout */
rc = ETIMEDOUT;
break;
} else if (nsec_diff < 5000000) {
/* sleep until timeout */
dur.tv_sec = 0;
dur.tv_nsec = nsec_diff;
} else {
/* sleep 5 ms */
dur.tv_sec = 0;
dur.tv_nsec = 5000000;
}
nanosleep(&dur, NULL);
}
return rc;
}
#endif
#ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK
int
pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime)
{
/* only real time supported without this function */
if (clockid != CLOCK_REALTIME) {
return EINVAL;
}
return pthread_mutex_timedlock(mutex, abstime);
}
#endif
#ifndef HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
int
pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
{
int64_t nsec_diff;
struct timespec cur, dur;
int rc;
/* try to acquire the lock and, if we fail, sleep for 5ms. */
while ((rc = pthread_rwlock_tryrdlock(rwlock)) == EBUSY) {
/* get time */
clock_gettime(COMPAT_CLOCK_ID, &cur);
/* get time diff */
nsec_diff = 0;
nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
if (nsec_diff <= 0) {
/* timeout */
rc = ETIMEDOUT;
break;
} else if (nsec_diff < 5000000) {
/* sleep until timeout */
dur.tv_sec = 0;
dur.tv_nsec = nsec_diff;
} else {
/* sleep 5 ms */
dur.tv_sec = 0;
dur.tv_nsec = 5000000;
}
nanosleep(&dur, NULL);
}
return rc;
}
#endif
#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
int
pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
{
/* only real time supported without this function */
if (clockid != CLOCK_REALTIME) {
return EINVAL;
}
return pthread_rwlock_timedrdlock(rwlock, abstime);
}
#endif
#ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
int
pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abstime)
{
int64_t nsec_diff;
struct timespec cur, dur;
int rc;
/* try to acquire the lock and, if we fail, sleep for 5ms. */
while ((rc = pthread_rwlock_trywrlock(rwlock)) == EBUSY) {
/* get time */
clock_gettime(COMPAT_CLOCK_ID, &cur);
/* get time diff */
nsec_diff = 0;
nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
if (nsec_diff <= 0) {
/* timeout */
rc = ETIMEDOUT;
break;
} else if (nsec_diff < 5000000) {
/* sleep until timeout */
dur.tv_sec = 0;
dur.tv_nsec = nsec_diff;
} else {
/* sleep 5 ms */
dur.tv_sec = 0;
dur.tv_nsec = 5000000;
}
nanosleep(&dur, NULL);
}
return rc;
}
#endif
#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
int
pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime)
{
/* only real time supported without this function */
if (clockid != CLOCK_REALTIME) {
return EINVAL;
}
return pthread_rwlock_timedwrlock(rwlock, abstime);
}
#endif
#ifndef HAVE_PTHREAD_COND_CLOCKWAIT
int
pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t UNUSED(clockid),
const struct timespec *abstime)
{
/* assume the correct clock is set during cond init */
return pthread_cond_timedwait(cond, mutex, abstime);
}
#endif
#ifndef HAVE_VDPRINTF
int
vdprintf(int fd, const char *format, va_list ap)
@ -200,51 +374,54 @@ get_current_dir_name(void)
#endif
#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
int
pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
#ifndef HAVE_CRYPT_R
char *
crypt_r(const char *phrase, const char *setting, struct crypt_data *data)
{
int64_t nsec_diff;
int32_t diff;
struct timespec cur, dur;
int rc;
static pthread_mutex_t crypt_lock = PTHREAD_MUTEX_INITIALIZER;
char *hash;
/* try to acquire the lock and, if we fail, sleep for 5ms. */
while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
/* get real time */
#ifdef CLOCK_REALTIME
clock_gettime(CLOCK_REALTIME, &cur);
#else
struct timeval tv;
(void) data;
gettimeofday(&tv, NULL);
cur.tv_sec = (time_t)tv.tv_sec;
cur.tv_nsec = 1000L * (long)tv.tv_usec;
#endif
pthread_mutex_lock(&crypt_lock);
hash = crypt(phrase, setting);
pthread_mutex_unlock(&crypt_lock);
/* get time diff */
nsec_diff = 0;
nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
diff = (nsec_diff ? nsec_diff / 1000000L : 0);
if (diff < 1) {
/* timeout */
break;
} else if (diff < 5) {
/* sleep until timeout */
dur.tv_sec = 0;
dur.tv_nsec = (long)diff * 1000000;
} else {
/* sleep 5 ms */
dur.tv_sec = 0;
dur.tv_nsec = 5000000;
}
nanosleep(&dur, NULL);
}
return rc;
return hash;
}
#endif
#ifndef HAVE_TIMEGM
time_t
timegm(struct tm *tm)
{
pthread_mutex_t tz_lock = PTHREAD_MUTEX_INITIALIZER;
time_t ret;
char *tz;
pthread_mutex_lock(&tz_lock);
tz = getenv("TZ");
if (tz) {
tz = strdup(tz);
}
setenv("TZ", "", 1);
tzset();
ret = mktime(tm);
if (tz) {
setenv("TZ", tz, 1);
free(tz);
} else {
unsetenv("TZ");
}
tzset();
pthread_mutex_unlock(&tz_lock);
return ret;
}
#endif

View file

@ -3,7 +3,7 @@
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief compatibility functions header
*
* Copyright (c) 2021 CESNET, z.s.p.o.
* Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
@ -15,12 +15,22 @@
#ifndef _COMPAT_H_
#define _COMPAT_H_
#define _GNU_SOURCE /* pthread_rwlock_t */
#cmakedefine HAVE_CRYPT_H
#ifdef HAVE_CRYPT_H
# include <crypt.h>
#endif
#include <alloca.h>
#include <limits.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#ifndef __WORDSIZE
# if defined __x86_64__ && !defined __ILP32__
@ -48,6 +58,15 @@
# define _PACKED
#endif
#define COMPAT_CLOCK_ID @COMPAT_CLOCK_ID@
#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
#cmakedefine HAVE_PTHREAD_MUTEX_CLOCKLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
#cmakedefine HAVE_PTHREAD_COND_CLOCKWAIT
#cmakedefine HAVE_VDPRINTF
#cmakedefine HAVE_ASPRINTF
#cmakedefine HAVE_VASPRINTF
@ -57,7 +76,8 @@
#cmakedefine HAVE_STRDUPA
#cmakedefine HAVE_STRCHRNUL
#cmakedefine HAVE_GET_CURRENT_DIR_NAME
#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
#cmakedefine HAVE_CRYPT_R
#cmakedefine HAVE_TIMEGM
#ifndef bswap64
#define bswap64(val) \
@ -87,6 +107,10 @@
# define ATOMIC_T atomic_uint_fast32_t
# define ATOMIC_T_MAX UINT_FAST32_MAX
# define ATOMIC64_T atomic_uint_fast64_t
# define ATOMIC64_T_MAX UINT_FAST64_MAX
# define ATOMIC_PTR_T atomic_uintptr_t
# define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed)
# define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed)
@ -94,11 +118,20 @@
# define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed)
# define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed)
# define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed)
# define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \
result = atomic_compare_exchange_strong_explicit(&(var), &(exp), des, memory_order_relaxed, memory_order_relaxed)
# define ATOMIC_PTR_STORE_RELAXED(var, x) atomic_store_explicit(&(var), (uintptr_t)(x), memory_order_relaxed)
# define ATOMIC_PTR_LOAD_RELAXED(var) ((void *)atomic_load_explicit(&(var), memory_order_relaxed))
#else
# include <stdint.h>
# define ATOMIC_T uint32_t
# define ATOMIC_T_MAX UINT32_MAX
# define ATOMIC64_T uint64_t
# define ATOMIC64_T_MAX UINT64_MAX
# define ATOMIC_PTR_T void *
# define ATOMIC_STORE_RELAXED(var, x) ((var) = (x))
# define ATOMIC_LOAD_RELAXED(var) (var)
@ -106,6 +139,35 @@
# define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x)
# define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1)
# define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x)
# define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \
{ \
ATOMIC_T __old = __sync_val_compare_and_swap(&(var), exp, des); \
result = ATOMIC_LOAD_RELAXED(__old) == ATOMIC_LOAD_RELAXED(exp) ? 1 : 0; \
ATOMIC_STORE_RELAXED(exp, ATOMIC_LOAD_RELAXED(__old)); \
}
# define ATOMIC_PTR_STORE_RELAXED(var, x) ((var) = (x))
# define ATOMIC_PTR_LOAD_RELAXED(var) (var)
#endif
#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
#endif
#ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK
int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime);
#endif
#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
int pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime);
#endif
#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
int pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime);
#endif
#ifndef HAVE_PTHREAD_COND_CLOCKWAIT
int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime);
#endif
#ifndef HAVE_VDPRINTF
@ -151,8 +213,18 @@ char *strchrnul(const char *s, int c);
char *get_current_dir_name(void);
#endif
#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
#ifndef HAVE_CRYPT_R
/* unused anyway */
struct crypt_data {
char a;
};
char *crypt_r(const char *phrase, const char *setting, struct crypt_data *data);
#endif
#ifndef HAVE_TIMEGM
time_t timegm(struct tm *tm);
#endif
#endif /* _COMPAT_H_ */