summaryrefslogtreecommitdiff
path: root/libhw_cr/host_util.c
blob: 2d454903ef486a22fc129da301d96b6265acbf07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* libhw_cr/host_util.c - Utilities for GNU/Linux hosts
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <errno.h>  /* for E* */
#include <signal.h> /* for SIGRTMIN, SIGRTMAX */

#define error __error
#include <error.h>
#undef error

#include <libhw/generic/alarmclock.h> /* 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));
}