/* libhw_cr/host_util.c - Utilities for GNU/Linux hosts * * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ #include /* for E* */ #include /* for SIGRTMIN, SIGRTMAX */ #define error __error #include #undef error #include /* for {X}S_PER_S */ #include "host_util.h" int host_sigrt_alloc(void) { static int next = 0; if (!next) next = SIGRTMIN; int ret = next++; if (ret > SIGRTMAX) __error(1, 0, "SIGRTMAX exceeded"); return ret; } host_us_time_t ns_to_host_us_time(uint64_t time_ns) { host_us_time_t ret; ret.tv_sec = time_ns /NS_PER_S; ret.tv_usec = (time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S) /(NS_PER_S/US_PER_S); return ret; } host_ns_time_t ns_to_host_ns_time(uint64_t time_ns) { host_ns_time_t ret; ret.tv_sec = time_ns /NS_PER_S; ret.tv_nsec = time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S; return ret; } uint64_t ns_from_host_us_time(host_us_time_t host_time) { return (((uint64_t)host_time.tv_sec) * NS_PER_S) + ((uint64_t)host_time.tv_usec * (NS_PER_S/US_PER_S)); } uint64_t ns_from_host_ns_time(host_ns_time_t host_time) { return (((uint64_t)host_time.tv_sec) * NS_PER_S) + ((uint64_t)host_time.tv_nsec); } _errnum errno_host2lm(host_errno_t host) { switch (host) { case E2BIG: return E_POSIX_E2BIG; case EACCES: return E_POSIX_EACCES; case EADDRINUSE: return E_POSIX_EADDRINUSE; case EADDRNOTAVAIL: return E_POSIX_EADDRNOTAVAIL; case EAFNOSUPPORT: return E_POSIX_EAFNOSUPPORT; case EAGAIN: return E_POSIX_EAGAIN; case EALREADY: return E_POSIX_EALREADY; case EBADF: return E_POSIX_EBADF; case EBADMSG: return E_POSIX_EBADMSG; case EBUSY: return E_POSIX_EBUSY; case ECANCELED: return E_POSIX_ECANCELED; case ECHILD: return E_POSIX_ECHILD; case ECONNABORTED: return E_POSIX_ECONNABORTED; case ECONNREFUSED: return E_POSIX_ECONNREFUSED; case ECONNRESET: return E_POSIX_ECONNRESET; case EDEADLK: return E_POSIX_EDEADLK; case EDESTADDRREQ: return E_POSIX_EDESTADDRREQ; case EDOM: return E_POSIX_EDOM; case EDQUOT: return E_POSIX_EDQUOT; case EEXIST: return E_POSIX_EEXIST; case EFAULT: return E_POSIX_EFAULT; case EFBIG: return E_POSIX_EFBIG; case EHOSTUNREACH: return E_POSIX_EHOSTUNREACH; case EIDRM: return E_POSIX_EIDRM; case EILSEQ: return E_POSIX_EILSEQ; case EINPROGRESS: return E_POSIX_EINPROGRESS; case EINTR: return E_POSIX_EINTR; case EINVAL: return E_POSIX_EINVAL; case EIO: return E_POSIX_EIO; case EISCONN: return E_POSIX_EISCONN; case EISDIR: return E_POSIX_EISDIR; case ELOOP: return E_POSIX_ELOOP; case EMFILE: return E_POSIX_EMFILE; case EMLINK: return E_POSIX_EMLINK; case EMSGSIZE: return E_POSIX_EMSGSIZE; case EMULTIHOP: return E_POSIX_EMULTIHOP; case ENAMETOOLONG: return E_POSIX_ENAMETOOLONG; case ENETDOWN: return E_POSIX_ENETDOWN; case ENETRESET: return E_POSIX_ENETRESET; case ENETUNREACH: return E_POSIX_ENETUNREACH; case ENFILE: return E_POSIX_ENFILE; case ENOBUFS: return E_POSIX_ENOBUFS; case ENODEV: return E_POSIX_ENODEV; case ENOENT: return E_POSIX_ENOENT; case ENOEXEC: return E_POSIX_ENOEXEC; case ENOLCK: return E_POSIX_ENOLCK; case ENOLINK: return E_POSIX_ENOLINK; case ENOMEM: return E_POSIX_ENOMEM; case ENOMSG: return E_POSIX_ENOMSG; case ENOPROTOOPT: return E_POSIX_ENOPROTOOPT; case ENOSPC: return E_POSIX_ENOSPC; case ENOSYS: return E_POSIX_ENOSYS; case ENOTCONN: return E_POSIX_ENOTCONN; case ENOTDIR: return E_POSIX_ENOTDIR; case ENOTEMPTY: return E_POSIX_ENOTEMPTY; case ENOTRECOVERABLE: return E_POSIX_ENOTRECOVERABLE; case ENOTSOCK: return E_POSIX_ENOTSOCK; case ENOTSUP: return E_POSIX_ENOTSUP; case ENOTTY: return E_POSIX_ENOTTY; case ENXIO: return E_POSIX_ENXIO; case EOVERFLOW: return E_POSIX_EOVERFLOW; case EOWNERDEAD: return E_POSIX_EOWNERDEAD; case EPERM: return E_POSIX_EPERM; case EPIPE: return E_POSIX_EPIPE; case EPROTO: return E_POSIX_EPROTO; case EPROTONOSUPPORT: return E_POSIX_EPROTONOSUPPORT; case EPROTOTYPE: return E_POSIX_EPROTOTYPE; case ERANGE: return E_POSIX_ERANGE; case EROFS: return E_POSIX_EROFS; case ESOCKTNOSUPPORT: return E_POSIX_ESOCKTNOSUPPORT; case ESPIPE: return E_POSIX_ESPIPE; case ESRCH: return E_POSIX_ESRCH; case ESTALE: return E_POSIX_ESTALE; case ETIMEDOUT: return E_POSIX_ETIMEDOUT; case ETXTBSY: return E_POSIX_ETXTBSY; case EXDEV: return E_POSIX_EXDEV; default: switch (host) { case EOPNOTSUPP: return E_POSIX_EOPNOTSUPP; case EWOULDBLOCK: return E_POSIX_EWOULDBLOCK; default: return E_EUNKNOWN; } } } error errno2lm(host_errno_t host) { return error_new(errno_host2lm(host)); }