12309Srsmaeda /*
22309Srsmaeda * CDDL HEADER START
32309Srsmaeda *
42309Srsmaeda * The contents of this file are subject to the terms of the
52309Srsmaeda * Common Development and Distribution License (the "License").
62309Srsmaeda * You may not use this file except in compliance with the License.
72309Srsmaeda *
82309Srsmaeda * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92309Srsmaeda * or http://www.opensolaris.org/os/licensing.
102309Srsmaeda * See the License for the specific language governing permissions
112309Srsmaeda * and limitations under the License.
122309Srsmaeda *
132309Srsmaeda * When distributing Covered Code, include this CDDL HEADER in each
142309Srsmaeda * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152309Srsmaeda * If applicable, add the following below this CDDL HEADER, with the
162309Srsmaeda * fields enclosed by brackets "[]" replaced with your own identifying
172309Srsmaeda * information: Portions Copyright [yyyy] [name of copyright owner]
182309Srsmaeda *
192309Srsmaeda * CDDL HEADER END
202309Srsmaeda */
212309Srsmaeda
222309Srsmaeda /*
232309Srsmaeda * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
242309Srsmaeda * Use is subject to license terms.
252309Srsmaeda */
262309Srsmaeda
272309Srsmaeda #pragma ident "%Z%%M% %I% %E% SMI"
282309Srsmaeda
292309Srsmaeda /*
302309Srsmaeda * Logging support for the DR Daemon
312309Srsmaeda */
322309Srsmaeda
332309Srsmaeda #include <stdio.h>
342309Srsmaeda #include <stdarg.h>
35*3263Sjm22469 #include <string.h>
36*3263Sjm22469 #include <errno.h>
372309Srsmaeda #include <syslog.h>
382309Srsmaeda
392309Srsmaeda #include "drd.h"
402309Srsmaeda
412309Srsmaeda #define DRD_MAX_MSG_LEN 512
42*3263Sjm22469 #define DRD_MAX_TIME_LEN 32
432309Srsmaeda
442309Srsmaeda static char *log_prio_str[] = {
452309Srsmaeda "EMERG: ", /* LOG_EMERG */
462309Srsmaeda "ALERT: ", /* LOG_ALERT */
472309Srsmaeda "CRIT: ", /* LOG_CRIT */
482309Srsmaeda "ERROR: ", /* LOG_ERR */
492309Srsmaeda "WARNING: ", /* LOG_WARNING */
502309Srsmaeda "NOTICE: ", /* LOG_NOTICE */
512309Srsmaeda "INFO: ", /* LOG_INFO */
522309Srsmaeda "" /* LOG_DEBUG */
532309Srsmaeda };
542309Srsmaeda
55*3263Sjm22469 /*
56*3263Sjm22469 * Generate a timestamp string in the provided buffer.
57*3263Sjm22469 * If any errors are encountered, the function returns
58*3263Sjm22469 * with the buffer containing an empty string.
59*3263Sjm22469 */
602309Srsmaeda static void
drd_timestamp(char * buf,size_t buflen)61*3263Sjm22469 drd_timestamp(char *buf, size_t buflen)
622309Srsmaeda {
63*3263Sjm22469 struct tm ltime;
64*3263Sjm22469 struct timeval now;
652309Srsmaeda
66*3263Sjm22469 if ((buf == NULL) || (buflen == 0))
67*3263Sjm22469 return;
68*3263Sjm22469
69*3263Sjm22469 buf[0] = '\0';
702309Srsmaeda
71*3263Sjm22469 if (gettimeofday(&now, NULL) != 0) {
72*3263Sjm22469 (void) fprintf(stderr, "gettimeofday failed: %s\n",
73*3263Sjm22469 strerror(errno));
74*3263Sjm22469 return;
75*3263Sjm22469 }
76*3263Sjm22469
77*3263Sjm22469 if (localtime_r(&now.tv_sec, <ime) == NULL) {
78*3263Sjm22469 (void) fprintf(stderr, "localtime_r failed: %s\n",
79*3263Sjm22469 strerror(errno));
802309Srsmaeda return;
812309Srsmaeda }
822309Srsmaeda
83*3263Sjm22469 if (strftime(buf, buflen, "%b %e %T ", <ime) == 0) {
84*3263Sjm22469 (void) fprintf(stderr, "strftime failed: buffer[%d] too "
85*3263Sjm22469 "small\n", buflen);
86*3263Sjm22469 /*
87*3263Sjm22469 * On failure, the contents of the buffer
88*3263Sjm22469 * are indeterminate. Restore it to a known
89*3263Sjm22469 * state before returning.
90*3263Sjm22469 */
91*3263Sjm22469 buf[0] = '\0';
92*3263Sjm22469 }
93*3263Sjm22469 }
94*3263Sjm22469
95*3263Sjm22469 static void
drd_log_msg(int prio,char * fmt,va_list vap)96*3263Sjm22469 drd_log_msg(int prio, char *fmt, va_list vap)
97*3263Sjm22469 {
98*3263Sjm22469 char msgbuf[DRD_MAX_MSG_LEN];
99*3263Sjm22469 char timebuf[DRD_MAX_TIME_LEN] = "";
100*3263Sjm22469
101*3263Sjm22469 /* generate a timestamp for the SMF log */
102*3263Sjm22469 drd_timestamp(timebuf, sizeof (timebuf));
103*3263Sjm22469
104*3263Sjm22469 (void) vsnprintf(msgbuf, DRD_MAX_MSG_LEN, fmt, vap);
105*3263Sjm22469
106*3263Sjm22469 /*
107*3263Sjm22469 * Print the message to stderr. In daemon mode, it
108*3263Sjm22469 * will be sent to the SMF log. In standalone mode,
109*3263Sjm22469 * it will be sent to the controlling terminal.
110*3263Sjm22469 */
111*3263Sjm22469 (void) fprintf(stderr, "%s%s%s\n", timebuf, log_prio_str[prio], msgbuf);
112*3263Sjm22469
113*3263Sjm22469 if (drd_daemonized)
114*3263Sjm22469 syslog(prio, msgbuf);
1152309Srsmaeda }
1162309Srsmaeda
1172309Srsmaeda void
drd_err(char * fmt,...)1182309Srsmaeda drd_err(char *fmt, ...)
1192309Srsmaeda {
1202309Srsmaeda va_list vap;
1212309Srsmaeda
1222309Srsmaeda va_start(vap, fmt);
1232309Srsmaeda drd_log_msg(LOG_ERR, fmt, vap);
1242309Srsmaeda va_end(vap);
1252309Srsmaeda }
1262309Srsmaeda
1272309Srsmaeda void
drd_info(char * fmt,...)1282309Srsmaeda drd_info(char *fmt, ...)
1292309Srsmaeda {
1302309Srsmaeda va_list vap;
1312309Srsmaeda
1322309Srsmaeda va_start(vap, fmt);
1332309Srsmaeda drd_log_msg(LOG_INFO, fmt, vap);
1342309Srsmaeda va_end(vap);
1352309Srsmaeda }
1362309Srsmaeda
1372309Srsmaeda void
drd_dbg(char * fmt,...)1382309Srsmaeda drd_dbg(char *fmt, ...)
1392309Srsmaeda {
1402309Srsmaeda va_list vap;
1412309Srsmaeda
1422309Srsmaeda if (!drd_debug) {
1432309Srsmaeda /* not debugging */
1442309Srsmaeda return;
1452309Srsmaeda }
1462309Srsmaeda
1472309Srsmaeda va_start(vap, fmt);
1482309Srsmaeda drd_log_msg(LOG_DEBUG, fmt, vap);
1492309Srsmaeda va_end(vap);
1502309Srsmaeda }
151