~ubuntu-branches/ubuntu/trusty/libnl3/trusty

« back to all changes in this revision

Viewing changes to lib/error.c

  • Committer: Bazaar Package Importer
  • Author(s): Heiko Stuebner
  • Date: 2011-05-21 19:25:13 UTC
  • Revision ID: james.westby@ubuntu.com-20110521192513-1ieyu9w9kym4bt16
Tags: upstream-3.0
ImportĀ upstreamĀ versionĀ 3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * lib/error.c          Error Handling
 
3
 *
 
4
 *      This library is free software; you can redistribute it and/or
 
5
 *      modify it under the terms of the GNU Lesser General Public
 
6
 *      License as published by the Free Software Foundation version 2.1
 
7
 *      of the License.
 
8
 *
 
9
 * Copyright (c) 2008 Thomas Graf <tgraf@suug.ch>
 
10
 */
 
11
 
 
12
#include <netlink-local.h>
 
13
#include <netlink/netlink.h>
 
14
 
 
15
static const char *errmsg[NLE_MAX+1] = {
 
16
[NLE_SUCCESS]           = "Success",
 
17
[NLE_FAILURE]           = "Unspecific failure",
 
18
[NLE_INTR]              = "Interrupted system call",
 
19
[NLE_BAD_SOCK]          = "Bad socket",
 
20
[NLE_AGAIN]             = "Try again",
 
21
[NLE_NOMEM]             = "Out of memory",
 
22
[NLE_EXIST]             = "Object exists",
 
23
[NLE_INVAL]             = "Invalid input data or parameter",
 
24
[NLE_RANGE]             = "Input data out of range",
 
25
[NLE_MSGSIZE]           = "Message size not sufficient",
 
26
[NLE_OPNOTSUPP]         = "Operation not supported",
 
27
[NLE_AF_NOSUPPORT]      = "Address family not supported",
 
28
[NLE_OBJ_NOTFOUND]      = "Object not found",
 
29
[NLE_NOATTR]            = "Attribute not available",
 
30
[NLE_MISSING_ATTR]      = "Missing attribute",
 
31
[NLE_AF_MISMATCH]       = "Address family mismatch",
 
32
[NLE_SEQ_MISMATCH]      = "Message sequence number mismatch",
 
33
[NLE_MSG_OVERFLOW]      = "Kernel reported message overflow",
 
34
[NLE_MSG_TRUNC]         = "Kernel reported truncated message",
 
35
[NLE_NOADDR]            = "Invalid address for specified address family",
 
36
[NLE_SRCRT_NOSUPPORT]   = "Source based routing not supported",
 
37
[NLE_MSG_TOOSHORT]      = "Netlink message is too short",
 
38
[NLE_MSGTYPE_NOSUPPORT] = "Netlink message type is not supported",
 
39
[NLE_OBJ_MISMATCH]      = "Object type does not match cache",
 
40
[NLE_NOCACHE]           = "Unknown or invalid cache type",
 
41
[NLE_BUSY]              = "Object busy",
 
42
[NLE_PROTO_MISMATCH]    = "Protocol mismatch",
 
43
[NLE_NOACCESS]          = "No Access",
 
44
[NLE_PERM]              = "Operation not permitted",
 
45
[NLE_PKTLOC_FILE]       = "Unable to open packet location file",
 
46
[NLE_PARSE_ERR]         = "Unable to parse object",
 
47
};
 
48
 
 
49
/**
 
50
 * Return error message for an error code
 
51
 * @return error message
 
52
 */
 
53
const char *nl_geterror(int error)
 
54
{
 
55
        error = abs(error);
 
56
 
 
57
        if (error > NLE_MAX)
 
58
                error = NLE_FAILURE;
 
59
 
 
60
        return errmsg[error];
 
61
}
 
62
 
 
63
/**
 
64
 * Print a libnl error message
 
65
 * @arg s               error message prefix
 
66
 *
 
67
 * Prints the error message of the call that failed last.
 
68
 *
 
69
 * If s is not NULL and *s is not a null byte the argument
 
70
 * string is printed, followed by a colon and a blank. Then
 
71
 * the error message and a new-line.
 
72
 */
 
73
void nl_perror(int error, const char *s)
 
74
{
 
75
        if (s && *s)
 
76
                fprintf(stderr, "%s: %s\n", s, nl_geterror(error));
 
77
        else
 
78
                fprintf(stderr, "%s\n", nl_geterror(error));
 
79
}
 
80
 
 
81
int nl_syserr2nlerr(int error)
 
82
{
 
83
        error = abs(error);
 
84
 
 
85
        switch (error) {
 
86
        case EBADF:             return NLE_BAD_SOCK;
 
87
        case EADDRINUSE:        return NLE_EXIST;
 
88
        case EEXIST:            return NLE_EXIST;
 
89
        case EADDRNOTAVAIL:     return NLE_NOADDR;
 
90
        case ENOENT:            return NLE_OBJ_NOTFOUND;
 
91
        case EINTR:             return NLE_INTR;
 
92
        case EAGAIN:            return NLE_AGAIN;
 
93
        case ENOTSOCK:          return NLE_BAD_SOCK;
 
94
        case ENOPROTOOPT:       return NLE_INVAL;
 
95
        case EFAULT:            return NLE_INVAL;
 
96
        case EACCES:            return NLE_NOACCESS;
 
97
        case EINVAL:            return NLE_INVAL;
 
98
        case ENOBUFS:           return NLE_NOMEM;
 
99
        case ENOMEM:            return NLE_NOMEM;
 
100
        case EAFNOSUPPORT:      return NLE_AF_NOSUPPORT;
 
101
        case EPROTONOSUPPORT:   return NLE_PROTO_MISMATCH;
 
102
        case EOPNOTSUPP:        return NLE_OPNOTSUPP;
 
103
        case EPERM:             return NLE_PERM;
 
104
        case EBUSY:             return NLE_BUSY;
 
105
        case ERANGE:            return NLE_RANGE;
 
106
        default:                return NLE_FAILURE;
 
107
        }
 
108
}
 
109
 
 
110
/** @} */
 
111