summaryrefslogtreecommitdiff
path: root/src/platform/posix/posix_debug.c
blob: 71006b2514aa3e5befe0c5f751dd5b56c12ec03c (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
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "core/nng_impl.h"

#ifdef PLATFORM_POSIX_DEBUG

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void
nni_plat_abort(void)
{
	abort();
}


void
nni_plat_println(const char *message)
{
	(void) fprintf(stderr, "%s\n", message);
}


const char *
nni_plat_strerror(int errnum)
{
	if (errnum > NNG_ESYSERR) {
		errnum -= NNG_ESYSERR;
	}
	return (strerror(errnum));
}


#define NNI_ERR(x, y)    { x, y },

// There are of course other errors than these, but these are the ones
// that we might reasonably expect and want to handle "cleanly".  Most of
// the others should be handled by the system error code.  Note that EFAULT
// is very special, because if the error code is *that*, then we should panic
// because an invalid system call has been made.  (That would be a sign
// of a serious software bug, in other words.)  POSIX says that all these
// error codes should exist, and be distinct positive numbers. (EWOULDBLOCK
// and EAGAIN are permitted to have the same value.)
static struct {
	int	posix_err;
	int	nng_err;
}
nni_plat_errnos[] = {
	NNI_ERR(EINTR, NNG_EINTR)
	NNI_ERR(EINVAL, NNG_EINVAL)
	NNI_ERR(ENOMEM, NNG_ENOMEM)
	NNI_ERR(EACCES, NNG_EPERM)
	NNI_ERR(EADDRINUSE, NNG_EADDRINUSE)
	NNI_ERR(EADDRNOTAVAIL, NNG_EADDRINVAL)
	NNI_ERR(EAFNOSUPPORT, NNG_ENOTSUP)
	NNI_ERR(EAGAIN, NNG_EAGAIN)
	NNI_ERR(EBADF, NNG_ECLOSED)
	NNI_ERR(EBUSY, NNG_EBUSY)
	NNI_ERR(ECONNABORTED, NNG_ECLOSED)
	NNI_ERR(ECONNREFUSED, NNG_ECONNREFUSED)
	NNI_ERR(ECONNRESET, NNG_ECLOSED)
	NNI_ERR(EHOSTUNREACH, NNG_EUNREACHABLE)
	NNI_ERR(ENETUNREACH, NNG_EUNREACHABLE)
	NNI_ERR(ENAMETOOLONG, NNG_EINVAL)
	NNI_ERR(ENOENT, NNG_ENOENT)
	NNI_ERR(ENOBUFS, NNG_ENOMEM)
	NNI_ERR(ENOPROTOOPT, NNG_ENOTSUP)
	NNI_ERR(ENOSYS, NNG_ENOTSUP)
	NNI_ERR(ENOTSUP, NNG_ENOTSUP)
	NNI_ERR(EPERM, NNG_EPERM)
	NNI_ERR(EPIPE, NNG_ECLOSED)
	NNI_ERR(EPROTO, NNG_EPROTO)
	NNI_ERR(EPROTONOSUPPORT, NNG_ENOTSUP)
	NNI_ERR(ETIME, NNG_ETIMEDOUT)
	NNI_ERR(ETIMEDOUT, NNG_ETIMEDOUT)
	NNI_ERR(EWOULDBLOCK, NNG_EAGAIN)
	NNI_ERR(0, 0)                   // must be last
};

int
nni_plat_errno(int errnum)
{
	int i;

	if (errnum == 0) {
		return (0);
	}
	if (errnum == EFAULT) {
		nni_panic("System EFAULT encountered!");
	}
	for (i = 0; nni_plat_errnos[i].nng_err != 0; i++) {
		if (errnum == nni_plat_errnos[i].posix_err) {
			return (nni_plat_errnos[i].nng_err);
		}
	}
	// Other system errno.
	return (NNG_ESYSERR + errnum);
}


#endif // PLATFORM_POSIX_DEBUG