1 |
/* |
2 |
* i-scream libstatgrab |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2004 i-scream |
5 |
* |
6 |
* This library is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU Lesser General Public |
8 |
* License as published by the Free Software Foundation; either |
9 |
* version 2.1 of the License, or (at your option) any later version. |
10 |
* |
11 |
* This library is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* Lesser General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU Lesser General Public |
17 |
* License along with this library; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
* 02111-1307 USA |
20 |
* |
21 |
* $Id: error.c,v 1.11 2004/04/08 11:14:02 ats Exp $ |
22 |
*/ |
23 |
|
24 |
#ifdef HAVE_CONFIG_H |
25 |
#include "config.h" |
26 |
#endif |
27 |
|
28 |
#include <stdlib.h> |
29 |
#include <string.h> |
30 |
#include <errno.h> |
31 |
|
32 |
#include "statgrab.h" |
33 |
#include "tools.h" |
34 |
|
35 |
static sg_error error = SG_ERROR_NONE; |
36 |
#define ERROR_ARG_MAX 256 |
37 |
static char error_arg[ERROR_ARG_MAX]; |
38 |
static char error_strerror[ERROR_ARG_MAX]; |
39 |
|
40 |
void sg_set_error(sg_error code, const char *arg) { |
41 |
error = code; |
42 |
if (arg != NULL) { |
43 |
sg_strlcpy(error_arg, arg, sizeof error_arg); |
44 |
} |
45 |
else { |
46 |
/* FIXME is this the best idea? */ |
47 |
error_arg[0] = '\0'; |
48 |
} |
49 |
} |
50 |
|
51 |
void sg_set_error_with_errno(sg_error code, const char *arg, int use_errno) { |
52 |
if(use_errno) { |
53 |
sg_strlcpy(error_strerror, strerror(errno), sizeof error_strerror); |
54 |
} |
55 |
else { |
56 |
/* FIXME is this the best idea? */ |
57 |
error_strerror[0] = '\0'; |
58 |
} |
59 |
sg_set_error(code, arg); |
60 |
} |
61 |
|
62 |
sg_error sg_get_error() { |
63 |
return error; |
64 |
} |
65 |
|
66 |
const char *sg_get_error_arg() { |
67 |
return error_arg; |
68 |
} |
69 |
|
70 |
const char *sg_get_error_strerror() { |
71 |
return error_strerror; |
72 |
} |
73 |
|
74 |
const char *sg_str_error(sg_error code) { |
75 |
switch (code) { |
76 |
case SG_ERROR_NONE: |
77 |
return "no error"; |
78 |
case SG_ERROR_ASPRINTF: |
79 |
return "asprintf failed"; |
80 |
case SG_ERROR_DEVSTAT_GETDEVS: |
81 |
return "devstat_getdevs failed"; |
82 |
case SG_ERROR_DEVSTAT_SELECTDEVS: |
83 |
return "devstat_selectdevs failed"; |
84 |
case SG_ERROR_ENOENT: |
85 |
return "system call received ENOENT"; |
86 |
case SG_ERROR_GETIFADDRS: |
87 |
return "getifaddress failed"; |
88 |
case SG_ERROR_GETMNTINFO: |
89 |
return "getmntinfo failed"; |
90 |
case SG_ERROR_GETPAGESIZE: |
91 |
return "getpagesize failed"; |
92 |
case SG_ERROR_KSTAT_DATA_LOOKUP: |
93 |
return "kstat_data_lookup failed"; |
94 |
case SG_ERROR_KSTAT_LOOKUP: |
95 |
return "kstat_lookup failed"; |
96 |
case SG_ERROR_KSTAT_OPEN: |
97 |
return "kstat_open failed"; |
98 |
case SG_ERROR_KSTAT_READ: |
99 |
return "kstat_read failed"; |
100 |
case SG_ERROR_KVM_GETSWAPINFO: |
101 |
return "kvm_getswapinfo failed"; |
102 |
case SG_ERROR_KVM_OPENFILES: |
103 |
return "kvm_openfiles failed"; |
104 |
case SG_ERROR_MALLOC: |
105 |
return "malloc failed"; |
106 |
case SG_ERROR_OPEN: |
107 |
return "failed to open file"; |
108 |
case SG_ERROR_OPENDIR: |
109 |
return "failed to open directory"; |
110 |
case SG_ERROR_PARSE: |
111 |
return "failed to parse input"; |
112 |
case SG_ERROR_SETEGID: |
113 |
return "setegid failed"; |
114 |
case SG_ERROR_SETEUID: |
115 |
return "seteuid failed"; |
116 |
case SG_ERROR_SETMNTENT: |
117 |
return "setmntent failed"; |
118 |
case SG_ERROR_SOCKET: |
119 |
return "socket failed"; |
120 |
case SG_ERROR_SWAPCTL: |
121 |
return "swapctl failed"; |
122 |
case SG_ERROR_SYSCONF: |
123 |
return "sysconf failed"; |
124 |
case SG_ERROR_SYSCTL: |
125 |
return "sysctl failed"; |
126 |
case SG_ERROR_SYSCTLBYNAME: |
127 |
return "sysctlbyname failed"; |
128 |
case SG_ERROR_SYSCTLNAMETOMIB: |
129 |
return "sysctlnametomib failed"; |
130 |
case SG_ERROR_UNAME: |
131 |
return "uname failed"; |
132 |
case SG_ERROR_UNSUPPORTED: |
133 |
return "unsupported function"; |
134 |
case SG_ERROR_XSW_VER_MISMATCH: |
135 |
return "XSW version mismatch"; |
136 |
} |
137 |
return "unknown error"; |
138 |
} |
139 |
|