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 (c) 2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * This file is a module that handles the logging features of the
31*0Sstevel@tonic-gate * DCS. All error messages that are generated by the DCS are kept in
32*0Sstevel@tonic-gate * a static array and accessed through one of the access functions
33*0Sstevel@tonic-gate * defined in this file.
34*0Sstevel@tonic-gate */
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <stdio.h>
38*0Sstevel@tonic-gate #include <stdarg.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <assert.h>
42*0Sstevel@tonic-gate #include <syslog.h>
43*0Sstevel@tonic-gate #include <libintl.h>
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate #include "dcs.h"
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate #define SYSLOG_FMT "<%d> %s"
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate * This is an array of strings representing all of the error and
53*0Sstevel@tonic-gate * informational messages that are used by the DCS. This includes
54*0Sstevel@tonic-gate * messages that are logged using syslog(3C) and those that are
55*0Sstevel@tonic-gate * displayed to the user through a message callback.
56*0Sstevel@tonic-gate */
57*0Sstevel@tonic-gate static const char *dcs_err_fmt[] = {
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate * Network Errors:
61*0Sstevel@tonic-gate */
62*0Sstevel@tonic-gate /* DCS_INIT_ERR */ "network initialization failed",
63*0Sstevel@tonic-gate /* DCS_NO_PORT */ "failed to acquire reserved port",
64*0Sstevel@tonic-gate /* DCS_CONNECT_ERR */ "connection attempt failed",
65*0Sstevel@tonic-gate /* DCS_RECEIVE_ERR */ "unable to receive message",
66*0Sstevel@tonic-gate /* DCS_OP_REPLY_ERR */ "unable to send message for %s operation",
67*0Sstevel@tonic-gate /* DCS_NO_SERV */ "%s service not found, using reserved port 665",
68*0Sstevel@tonic-gate /* DCS_DISCONNECT */ "client disconnected",
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate /*
71*0Sstevel@tonic-gate * Session Errors:
72*0Sstevel@tonic-gate */
73*0Sstevel@tonic-gate /* DCS_SES_HAND_ERR */ "failed to start a new session handler",
74*0Sstevel@tonic-gate /* DCS_ABORT_ERR */ "abort attempt of session, %d, unsuccessful",
75*0Sstevel@tonic-gate /* DCS_VER_INVAL */ "unsupported message protocol version %d.%d",
76*0Sstevel@tonic-gate /* DCS_SES_ABORTED */ "session aborted",
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate * DR Request Errors:
80*0Sstevel@tonic-gate */
81*0Sstevel@tonic-gate /* DCS_UNKNOWN_OP */ "unknown operation requested",
82*0Sstevel@tonic-gate /* DCS_OP_FAILED */ "operation failed",
83*0Sstevel@tonic-gate /* DCS_SEQ_INVAL */ "invalid session establishment sequence",
84*0Sstevel@tonic-gate /* DCS_NO_SES_ESTBL */ "%s operation issued before session established",
85*0Sstevel@tonic-gate /* DCS_MSG_INVAL */ "received an invalid message",
86*0Sstevel@tonic-gate /* DCS_CONF_CB_ERR */ "confirm callback failed, aborting operation",
87*0Sstevel@tonic-gate /* DCS_MSG_CB_ERR */ "message callback failed, continuing",
88*0Sstevel@tonic-gate /* DCS_BAD_RETRY */ "retry value invalid (%d)",
89*0Sstevel@tonic-gate /* DCS_BAD_TIMEOUT */ "timeout value invalid (%d)",
90*0Sstevel@tonic-gate /* DCS_RETRY */ "retrying operation, attempt %d",
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate * General Errors:
94*0Sstevel@tonic-gate */
95*0Sstevel@tonic-gate /* DCS_NO_PRIV */ "permission denied",
96*0Sstevel@tonic-gate /* DCS_INT_ERR */ "internal error: %s: %s",
97*0Sstevel@tonic-gate /* DCS_UNKNOWN_ERR */ "unrecognized error reported",
98*0Sstevel@tonic-gate /* DCS_BAD_OPT */ "illegal option (-%c), exiting",
99*0Sstevel@tonic-gate /* DCS_BAD_OPT_ARG */ "illegal argument to -%c flag (%s), %s",
100*0Sstevel@tonic-gate /* DCS_CFGA_UNKNOWN */ "configuration administration unknown error",
101*0Sstevel@tonic-gate /* DCS_CFGA_ERR */ "%s: %s",
102*0Sstevel@tonic-gate /* DCS_RSRC_ERR */ "resource info init error (%d)",
103*0Sstevel@tonic-gate /* DCS_MSG_COUNT */ NULL
104*0Sstevel@tonic-gate };
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate * dcs_log_msg:
109*0Sstevel@tonic-gate *
110*0Sstevel@tonic-gate * Based on an error code, construct an error string and output it to
111*0Sstevel@tonic-gate * a logfile using syslog(3C). Note that the string will not be localized.
112*0Sstevel@tonic-gate */
113*0Sstevel@tonic-gate void
dcs_log_msg(int priority,int err_code,...)114*0Sstevel@tonic-gate dcs_log_msg(int priority, int err_code, ...)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate va_list vap;
117*0Sstevel@tonic-gate char err_str[MAX_MSG_LEN];
118*0Sstevel@tonic-gate char syslog_str[MAX_MSG_LEN];
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate /* check if error code is out of bounds */
122*0Sstevel@tonic-gate if ((err_code < 0) || (err_code >= DCS_MSG_COUNT)) {
123*0Sstevel@tonic-gate syslog(LOG_NOTICE, dcs_err_fmt[DCS_UNKNOWN_ERR]);
124*0Sstevel@tonic-gate return;
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate va_start(vap, err_code);
128*0Sstevel@tonic-gate (void) vsnprintf(err_str, MAX_MSG_LEN, dcs_err_fmt[err_code], vap);
129*0Sstevel@tonic-gate va_end(vap);
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate /* prepend session identifier */
132*0Sstevel@tonic-gate snprintf(syslog_str, MAX_MSG_LEN, SYSLOG_FMT, curr_ses_id(), err_str);
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate syslog(priority, syslog_str);
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate if (standalone) {
137*0Sstevel@tonic-gate fprintf(stderr, "%s\n", syslog_str);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate * dcs_strerror:
144*0Sstevel@tonic-gate *
145*0Sstevel@tonic-gate * Return the format string associated with a supplied DCS specific
146*0Sstevel@tonic-gate * error code. dgettext(3C) is used to retrieve the localized version
147*0Sstevel@tonic-gate * of the format string.
148*0Sstevel@tonic-gate */
149*0Sstevel@tonic-gate const char *
dcs_strerror(int err_code)150*0Sstevel@tonic-gate dcs_strerror(int err_code)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate /* check if code is out of bounds */
153*0Sstevel@tonic-gate if ((err_code < 0) || (err_code >= DCS_MSG_COUNT)) {
154*0Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, dcs_err_fmt[DCS_UNKNOWN_ERR]));
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate return (dgettext(TEXT_DOMAIN, dcs_err_fmt[err_code]));
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate /*
162*0Sstevel@tonic-gate * dcs_cfga_str:
163*0Sstevel@tonic-gate *
164*0Sstevel@tonic-gate * Assemble a string that describes a particular libcfgadm error code.
165*0Sstevel@tonic-gate * This string will contain both the platform dependent and platform
166*0Sstevel@tonic-gate * independent message strings available from a libcfgadm function call.
167*0Sstevel@tonic-gate * The resulting string will be localized indirectly through the call
168*0Sstevel@tonic-gate * to config_strerror() and the localized error string returned from
169*0Sstevel@tonic-gate * the libcfgadm operation.
170*0Sstevel@tonic-gate */
171*0Sstevel@tonic-gate char *
dcs_cfga_str(char ** err_strp,int err_code)172*0Sstevel@tonic-gate dcs_cfga_str(char **err_strp, int err_code)
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate const char *ep;
175*0Sstevel@tonic-gate char *buf;
176*0Sstevel@tonic-gate char *err_str;
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate /*
180*0Sstevel@tonic-gate * Extract the platform specific message passed as
181*0Sstevel@tonic-gate * a parameter, or use NULL to signal that no error
182*0Sstevel@tonic-gate * string was passed.
183*0Sstevel@tonic-gate */
184*0Sstevel@tonic-gate if (err_strp && *err_strp) {
185*0Sstevel@tonic-gate err_str = *err_strp;
186*0Sstevel@tonic-gate } else {
187*0Sstevel@tonic-gate err_str = NULL;
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate buf = (char *)malloc(MAX_MSG_LEN);
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate if (buf == NULL) {
193*0Sstevel@tonic-gate dcs_log_msg(LOG_ERR, DCS_INT_ERR, "malloc", strerror(errno));
194*0Sstevel@tonic-gate return (NULL);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate /* get the platform independent message */
198*0Sstevel@tonic-gate ep = config_strerror(err_code);
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate if (ep == NULL) {
201*0Sstevel@tonic-gate ep = dgettext(TEXT_DOMAIN, dcs_err_fmt[DCS_CFGA_UNKNOWN]);
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate * Check if a platform specific message was provided, and
206*0Sstevel@tonic-gate * generate the appropriate message.
207*0Sstevel@tonic-gate */
208*0Sstevel@tonic-gate if ((err_str != NULL) && (*err_str != '\0')) {
209*0Sstevel@tonic-gate snprintf(buf, MAX_MSG_LEN, "%s: %s\n", ep, err_str);
210*0Sstevel@tonic-gate } else {
211*0Sstevel@tonic-gate snprintf(buf, MAX_MSG_LEN, "%s\n", ep);
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate return (buf);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate /*
219*0Sstevel@tonic-gate * dcs_dbg:
220*0Sstevel@tonic-gate *
221*0Sstevel@tonic-gate * Output a debugging message to a logfile using syslog(3C). The bits
222*0Sstevel@tonic-gate * in the debug mask specify the category of the message. They have
223*0Sstevel@tonic-gate * the following meanings:
224*0Sstevel@tonic-gate *
225*0Sstevel@tonic-gate * 0x1 - the string contains basic information
226*0Sstevel@tonic-gate * 0x2 - the string contains message information
227*0Sstevel@tonic-gate * 0x4 - the string contains session information
228*0Sstevel@tonic-gate * 0x8 - the string contains state information
229*0Sstevel@tonic-gate *
230*0Sstevel@tonic-gate * The debug mask is compared to the global value of dcs_debug which is
231*0Sstevel@tonic-gate * set through a command line option. This determines whether or not
232*0Sstevel@tonic-gate * to output the message to the logfile.
233*0Sstevel@tonic-gate */
234*0Sstevel@tonic-gate void
dcs_dbg(int dbg_mask,char * fmt,...)235*0Sstevel@tonic-gate dcs_dbg(int dbg_mask, char *fmt, ...)
236*0Sstevel@tonic-gate {
237*0Sstevel@tonic-gate va_list vap;
238*0Sstevel@tonic-gate char err_str[MAX_MSG_LEN];
239*0Sstevel@tonic-gate char syslog_str[MAX_MSG_LEN];
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate if ((dcs_debug & dbg_mask) == 0) {
243*0Sstevel@tonic-gate return;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate va_start(vap, fmt);
247*0Sstevel@tonic-gate (void) vsnprintf(err_str, MAX_MSG_LEN, fmt, vap);
248*0Sstevel@tonic-gate va_end(vap);
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate /* prepend session identifier */
251*0Sstevel@tonic-gate snprintf(syslog_str, MAX_MSG_LEN, SYSLOG_FMT, curr_ses_id(), err_str);
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gate syslog(LOG_DEBUG, syslog_str);
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate if (standalone) {
256*0Sstevel@tonic-gate fprintf(stderr, "%s\n", syslog_str);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate }
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate /*
262*0Sstevel@tonic-gate * print_msg_hdr:
263*0Sstevel@tonic-gate *
264*0Sstevel@tonic-gate * Print selected information from the header for a given message. The
265*0Sstevel@tonic-gate * information logged includes the information needed to track the flow
266*0Sstevel@tonic-gate * of messages: opcode, send/receive, request/reply, and success/failure.
267*0Sstevel@tonic-gate */
268*0Sstevel@tonic-gate void
print_msg_hdr(dcs_msg_type_t type,rdr_msg_hdr_t * hdr)269*0Sstevel@tonic-gate print_msg_hdr(dcs_msg_type_t type, rdr_msg_hdr_t *hdr)
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate static char *type_str[] = {
272*0Sstevel@tonic-gate "INVALID TYPE",
273*0Sstevel@tonic-gate "RDR_REQUEST",
274*0Sstevel@tonic-gate "RDR_REPLY"
275*0Sstevel@tonic-gate };
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate static char *op_str[] = {
278*0Sstevel@tonic-gate "RDR_INVALID_OP",
279*0Sstevel@tonic-gate "RDR_SES_REQ",
280*0Sstevel@tonic-gate "RDR_SES_ESTBL",
281*0Sstevel@tonic-gate "RDR_SES_END",
282*0Sstevel@tonic-gate "RDR_CONF_CHANGE_STATE",
283*0Sstevel@tonic-gate "RDR_CONF_PRIVATE_FUNC",
284*0Sstevel@tonic-gate "RDR_CONF_TEST",
285*0Sstevel@tonic-gate "RDR_CONF_LIST_EXT",
286*0Sstevel@tonic-gate "RDR_CONF_HELP",
287*0Sstevel@tonic-gate "RDR_CONF_AP_ID_CMP",
288*0Sstevel@tonic-gate "RDR_CONF_ABORT_CMD",
289*0Sstevel@tonic-gate "RDR_CONF_CONFIRM_CALLBACK",
290*0Sstevel@tonic-gate "RDR_CONF_MSG_CALLBACK",
291*0Sstevel@tonic-gate "RDR_RSRC_INFO"
292*0Sstevel@tonic-gate };
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate assert(hdr);
295*0Sstevel@tonic-gate
296*0Sstevel@tonic-gate /* clamp an invalid opcode */
297*0Sstevel@tonic-gate if (hdr->message_opcode >= RDR_NUM_OPS) {
298*0Sstevel@tonic-gate hdr->message_opcode = 0;
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate /* clamp an invalid type */
302*0Sstevel@tonic-gate if (hdr->data_type > RDR_REPLY) {
303*0Sstevel@tonic-gate hdr->data_type = 0;
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate DCS_DBG(DBG_MSG, "message %s: <%s, %s%s>",
307*0Sstevel@tonic-gate (type == DCS_RECEIVE) ? "received" : "sent",
308*0Sstevel@tonic-gate op_str[hdr->message_opcode],
309*0Sstevel@tonic-gate type_str[hdr->data_type],
310*0Sstevel@tonic-gate ((hdr->data_type == RDR_REQUEST) ||
311*0Sstevel@tonic-gate (hdr->message_opcode == RDR_CONF_AP_ID_CMP)) ? "" :
312*0Sstevel@tonic-gate (hdr->status == RDR_SUCCESS) ? ", RDR_SUCCESS" :
313*0Sstevel@tonic-gate ", RDR_FAILED");
314*0Sstevel@tonic-gate }
315