1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26 #pragma ident "%Z%%M% %I% %E% SMI"
27
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <sys/types.h>
36 #include <time.h>
37 #include <syslog.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <netdb.h>
42 #include <libgen.h>
43
44
45 #include "snmp_msg.h"
46 #include "impl.h"
47 #include "trace.h"
48 #include "error.h"
49
50
51 int error_size = DEFAULT_ERROR_SIZE;
52
53 char error_label[1000] = "";
54
55
56 static char *application_name = NULL;
57 static void (*application_end)() = NULL;
58
59
60 /*
61 * this function will exit on any error
62 */
63
error_init(char * name,void end ())64 void error_init(char *name, void end())
65 {
66 char *ptr;
67
68
69 if(name == NULL)
70 {
71 (void)fprintf(stderr, "BUG: error_init(): name is NULL");
72 exit(1);
73 }
74
75 ptr = basename(name);
76 if(ptr == NULL)
77 {
78 (void)fprintf(stderr, "error_init(): bad application name: %s",
79 name);
80 exit(1);
81 }
82
83 application_name = strdup(ptr);
84 if(application_name == NULL)
85 {
86 (void)fprintf(stderr, ERR_MSG_ALLOC);
87 exit(1);
88 }
89
90 if(end == NULL)
91 {
92 (void)fprintf(stderr, "BUG: error_init(): end is NULL");
93 exit(1);
94 }
95
96 application_end = end;
97
98 openlog(name, LOG_CONS, LOG_DAEMON);
99 }
100
101
102 /* ARGSUSED */
error_open(char * filename)103 void error_open(char *filename)
104 {
105 return;
106 }
107
108
error_close_stderr()109 void error_close_stderr()
110 {
111 return;
112 }
113
error(char * format,...)114 void error(char *format, ...)
115 {
116 va_list ap;
117 int32_t len;
118 char static_buffer[4096];
119
120 va_start(ap, format);
121
122 /* remove '\n's at the end of format */
123 /* LINTED */
124 len = (int32_t)strlen(format);
125 while((len > 0) && (format[len - 1] == '\n')) {
126 format[len - 1] = '\0';
127 len--;
128 }
129
130 (void) vsnprintf(static_buffer, sizeof (static_buffer), format, ap);
131 va_end(ap);
132
133 if(trace_level > 0)
134 trace("%s", static_buffer);
135
136 syslog(LOG_ERR, "%s", static_buffer);
137 }
138
139
error_exit(char * format,...)140 void error_exit(char *format, ...)
141 {
142 va_list ap;
143 int32_t len;
144 char static_buffer[4096];
145
146 va_start(ap, format);
147
148 /* remove '\n's at the end of format */
149 /* LINTED */
150 len = (int32_t)strlen(format);
151 while((len > 0) && (format[len - 1] == '\n')) {
152 format[len - 1] = '\0';
153 len--;
154 }
155
156 (void) vsnprintf(static_buffer, sizeof (static_buffer), format, ap);
157 va_end(ap);
158
159 application_end();
160
161 if(trace_level > 0)
162 trace("%s", static_buffer);
163
164 syslog(LOG_ERR, "%s", static_buffer);
165
166 exit(1);
167 }
168
169
errno_string()170 char *errno_string()
171 {
172 static char buffer[100];
173
174 sprintf(buffer, "[errno: %s(%d)]",
175 strerror(errno), errno);
176
177 return buffer;
178 }
179
180
h_errno_string()181 char *h_errno_string()
182 {
183 static char buffer[100];
184 char *ptr = NULL;
185
186 switch(h_errno)
187 {
188 case HOST_NOT_FOUND:
189 ptr = "host not found";
190 break;
191 case TRY_AGAIN:
192 ptr = "try again";
193 break;
194 case NO_RECOVERY:
195 ptr = "no recovery";
196 break;
197 case NO_DATA:
198 ptr = "no data";
199 break;
200 default:
201 ptr = "???";
202 break;
203 }
204
205 sprintf(buffer, "[h_errno: %s(%d)]",
206 ptr, h_errno);
207
208 return buffer;
209 }
210
211
212