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