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 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 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 * Daemon log message. This can direct log messages to either stdout, 31*0Sstevel@tonic-gate * an error log file or syslog (or any combination). 32*0Sstevel@tonic-gate */ 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #include <stdarg.h> 35*0Sstevel@tonic-gate #include <stdio.h> 36*0Sstevel@tonic-gate #include <syslog.h> 37*0Sstevel@tonic-gate #include <errno.h> 38*0Sstevel@tonic-gate #include <string.h> 39*0Sstevel@tonic-gate #include <unistd.h> 40*0Sstevel@tonic-gate #include <stdlib.h> 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate #include <fcode/private.h> 43*0Sstevel@tonic-gate #include <fcode/log.h> 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #define LOG_LINESIZE 256 46*0Sstevel@tonic-gate #define LOG_EMIT_BUFSIZE LOG_LINESIZE 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate static FILE *error_log_fp = NULL; 49*0Sstevel@tonic-gate static int syslog_opened = 0; 50*0Sstevel@tonic-gate static int error_log_flags; 51*0Sstevel@tonic-gate static int syslog_log_flags; 52*0Sstevel@tonic-gate static int do_emit_flag = -1; 53*0Sstevel@tonic-gate static int daemon_log_flag; 54*0Sstevel@tonic-gate static int min_syslog_level = LOG_ERR; 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate static int log_to_stdout(int); 57*0Sstevel@tonic-gate static int log_to_error_log(int); 58*0Sstevel@tonic-gate static int log_to_syslog(int); 59*0Sstevel@tonic-gate static int msg_level_to_syslog(int); 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate /* 62*0Sstevel@tonic-gate * Complicated by not wanting to do any emit processing if no one's actually 63*0Sstevel@tonic-gate * going to see it. 64*0Sstevel@tonic-gate */ 65*0Sstevel@tonic-gate void 66*0Sstevel@tonic-gate log_emit(char c) 67*0Sstevel@tonic-gate { 68*0Sstevel@tonic-gate static char emit_buf[LOG_EMIT_BUFSIZE]; 69*0Sstevel@tonic-gate static char *emit_p = emit_buf; 70*0Sstevel@tonic-gate static int lastnl = 1; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate /* 73*0Sstevel@tonic-gate * No one has set the do_emit_flag, go ahead and figure it out. 74*0Sstevel@tonic-gate */ 75*0Sstevel@tonic-gate if (do_emit_flag < 0) { 76*0Sstevel@tonic-gate do_emit_flag = (log_to_stdout(MSG_EMIT) | 77*0Sstevel@tonic-gate log_to_error_log(MSG_EMIT) | log_to_syslog(MSG_EMIT)); 78*0Sstevel@tonic-gate } 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate if (!do_emit_flag) 81*0Sstevel@tonic-gate return; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate /* 84*0Sstevel@tonic-gate * Check for buffer overflow. 85*0Sstevel@tonic-gate */ 86*0Sstevel@tonic-gate if (emit_p >= &emit_buf[LOG_EMIT_BUFSIZE - 1]) { 87*0Sstevel@tonic-gate *emit_p = '\0'; 88*0Sstevel@tonic-gate log_message(MSG_EMIT, "emit: %s\n", emit_buf); 89*0Sstevel@tonic-gate emit_p = emit_buf; 90*0Sstevel@tonic-gate lastnl = 1; 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* 94*0Sstevel@tonic-gate * Fcode emit's may output both CR/LF, we go ahead and eat multiple 95*0Sstevel@tonic-gate * ones in succession. 96*0Sstevel@tonic-gate */ 97*0Sstevel@tonic-gate if (c == '\n' || c == '\r') { 98*0Sstevel@tonic-gate if (!lastnl) { 99*0Sstevel@tonic-gate *emit_p = '\0'; 100*0Sstevel@tonic-gate log_message(MSG_EMIT, "emit: %s\n", emit_buf); 101*0Sstevel@tonic-gate emit_p = emit_buf; 102*0Sstevel@tonic-gate } 103*0Sstevel@tonic-gate lastnl = 1; 104*0Sstevel@tonic-gate } else { 105*0Sstevel@tonic-gate lastnl = 0; 106*0Sstevel@tonic-gate *emit_p++ = c; 107*0Sstevel@tonic-gate } 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate /* 111*0Sstevel@tonic-gate * If stdout is a tty and this is MSG_EMIT, we should have alredy output it. 112*0Sstevel@tonic-gate * If running as daemon, output to stdout is a waste of time. 113*0Sstevel@tonic-gate */ 114*0Sstevel@tonic-gate static int 115*0Sstevel@tonic-gate log_to_stdout(int msg_level) 116*0Sstevel@tonic-gate { 117*0Sstevel@tonic-gate if (isatty(fileno(stdin)) && (msg_level & MSG_EMIT) != 0) 118*0Sstevel@tonic-gate return (0); 119*0Sstevel@tonic-gate return (daemon_log_flag == 0); 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* 123*0Sstevel@tonic-gate * Can't turn off FATAL or ERROR messages to error log file. 124*0Sstevel@tonic-gate */ 125*0Sstevel@tonic-gate static int 126*0Sstevel@tonic-gate log_to_error_log(int msg_level) 127*0Sstevel@tonic-gate { 128*0Sstevel@tonic-gate if (!error_log_fp) 129*0Sstevel@tonic-gate return (0); 130*0Sstevel@tonic-gate if (msg_level & (MSG_FATAL|MSG_ERROR)) 131*0Sstevel@tonic-gate return (1); 132*0Sstevel@tonic-gate return (msg_level & error_log_flags); 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate /* 136*0Sstevel@tonic-gate * Can't turn off FATAL or ERROR messages to syslog. 137*0Sstevel@tonic-gate */ 138*0Sstevel@tonic-gate static int 139*0Sstevel@tonic-gate log_to_syslog(int msg_level) 140*0Sstevel@tonic-gate { 141*0Sstevel@tonic-gate if (!syslog_opened) 142*0Sstevel@tonic-gate return (0); 143*0Sstevel@tonic-gate if (msg_level & (MSG_FATAL|MSG_ERROR)) 144*0Sstevel@tonic-gate return (1); 145*0Sstevel@tonic-gate return (msg_level & syslog_log_flags); 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate /* 149*0Sstevel@tonic-gate * Turn internal MSG level to syslog msg level. Don't return a msg level 150*0Sstevel@tonic-gate * lower priority than min_syslog_level. 151*0Sstevel@tonic-gate */ 152*0Sstevel@tonic-gate static int 153*0Sstevel@tonic-gate msg_level_to_syslog(int msg_level) 154*0Sstevel@tonic-gate { 155*0Sstevel@tonic-gate if (min_syslog_level <= LOG_ERR) 156*0Sstevel@tonic-gate return (min_syslog_level); 157*0Sstevel@tonic-gate if (msg_level & (MSG_FATAL|MSG_ERROR)) 158*0Sstevel@tonic-gate return (LOG_ERR); 159*0Sstevel@tonic-gate if (min_syslog_level <= LOG_WARNING) 160*0Sstevel@tonic-gate return (min_syslog_level); 161*0Sstevel@tonic-gate if (msg_level & MSG_WARN) 162*0Sstevel@tonic-gate return (LOG_WARNING); 163*0Sstevel@tonic-gate if (min_syslog_level <= LOG_NOTICE) 164*0Sstevel@tonic-gate return (min_syslog_level); 165*0Sstevel@tonic-gate if (msg_level & MSG_NOTE) 166*0Sstevel@tonic-gate return (LOG_NOTICE); 167*0Sstevel@tonic-gate if (min_syslog_level <= LOG_INFO) 168*0Sstevel@tonic-gate return (min_syslog_level); 169*0Sstevel@tonic-gate if (msg_level & MSG_INFO) 170*0Sstevel@tonic-gate return (LOG_INFO); 171*0Sstevel@tonic-gate return (min(min_syslog_level, LOG_DEBUG)); 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate /* 175*0Sstevel@tonic-gate * Log a message to the appropriate places. 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate void 178*0Sstevel@tonic-gate log_message(int msg_level, char *fmt, ...) 179*0Sstevel@tonic-gate { 180*0Sstevel@tonic-gate va_list ap; 181*0Sstevel@tonic-gate char msg[LOG_LINESIZE], *p; 182*0Sstevel@tonic-gate static char log_msg[LOG_LINESIZE]; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate va_start(ap, fmt); 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate vsprintf(msg, fmt, ap); 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate if (log_to_stdout(msg_level)) { 189*0Sstevel@tonic-gate printf(msg); 190*0Sstevel@tonic-gate fflush(stdout); 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate if (log_to_error_log(msg_level)) { 193*0Sstevel@tonic-gate fprintf(error_log_fp, msg); 194*0Sstevel@tonic-gate fflush(error_log_fp); 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate if (log_to_syslog(msg_level)) { 197*0Sstevel@tonic-gate if (strlen(log_msg) + strlen(msg) > LOG_LINESIZE - 1) { 198*0Sstevel@tonic-gate syslog(msg_level_to_syslog(msg_level), log_msg); 199*0Sstevel@tonic-gate log_msg[0] = '\0'; 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate strcat(log_msg, msg); 202*0Sstevel@tonic-gate if ((p = strchr(log_msg, '\n')) != NULL) { 203*0Sstevel@tonic-gate *p = '\0'; 204*0Sstevel@tonic-gate syslog(msg_level_to_syslog(msg_level), log_msg); 205*0Sstevel@tonic-gate log_msg[0] = '\0'; 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate } 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate /* 211*0Sstevel@tonic-gate * Output debug message 212*0Sstevel@tonic-gate */ 213*0Sstevel@tonic-gate void 214*0Sstevel@tonic-gate debug_msg(int debug_level, char *fmt, ...) 215*0Sstevel@tonic-gate { 216*0Sstevel@tonic-gate va_list ap; 217*0Sstevel@tonic-gate char msg[LOG_LINESIZE]; 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate if ((debug_level & get_interpreter_debug_level()) == 0) 220*0Sstevel@tonic-gate return; 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate va_start(ap, fmt); 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate vsprintf(msg, fmt, ap); 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate log_message(MSG_DEBUG, msg); 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate /* 230*0Sstevel@tonic-gate * Log a perror message to the appropriate places. 231*0Sstevel@tonic-gate */ 232*0Sstevel@tonic-gate void 233*0Sstevel@tonic-gate log_perror(int msg_level, char *fmt, ...) 234*0Sstevel@tonic-gate { 235*0Sstevel@tonic-gate va_list ap; 236*0Sstevel@tonic-gate char msg[LOG_LINESIZE], tmp[LOG_LINESIZE]; 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate va_start(ap, fmt); 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate vsprintf(msg, fmt, ap); 241*0Sstevel@tonic-gate sprintf(tmp, "%s: %s\n", msg, strerror(errno)); 242*0Sstevel@tonic-gate log_message(msg_level, tmp); 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate void 246*0Sstevel@tonic-gate set_min_syslog_level(int level) 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate min_syslog_level = level; 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate char *error_log_name; 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate void 254*0Sstevel@tonic-gate open_error_log(char *fname, int errflags) 255*0Sstevel@tonic-gate { 256*0Sstevel@tonic-gate if ((error_log_fp = fopen(fname, "a")) == NULL) { 257*0Sstevel@tonic-gate log_perror(MSG_FATAL, fname); 258*0Sstevel@tonic-gate exit(1); 259*0Sstevel@tonic-gate } 260*0Sstevel@tonic-gate error_log_name = STRDUP(fname); 261*0Sstevel@tonic-gate error_log_flags = errflags; 262*0Sstevel@tonic-gate do_emit_flag = (log_to_stdout(MSG_EMIT) | log_to_error_log(MSG_EMIT) | 263*0Sstevel@tonic-gate log_to_syslog(MSG_EMIT)); 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate void 267*0Sstevel@tonic-gate open_syslog_log(char *logname, int logflags) 268*0Sstevel@tonic-gate { 269*0Sstevel@tonic-gate openlog(logname, LOG_PID, LOG_DAEMON); 270*0Sstevel@tonic-gate syslog_log_flags = logflags; 271*0Sstevel@tonic-gate do_emit_flag = (log_to_stdout(MSG_EMIT) | log_to_error_log(MSG_EMIT) | 272*0Sstevel@tonic-gate log_to_syslog(MSG_EMIT)); 273*0Sstevel@tonic-gate syslog_opened = 1; 274*0Sstevel@tonic-gate } 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate /* 277*0Sstevel@tonic-gate * Turn on/off syslog LOG_DAEMON flag to syslog messages. Also controls 278*0Sstevel@tonic-gate * outputting to stdout, which is a waste of time when we're running as a 279*0Sstevel@tonic-gate * daemon. 280*0Sstevel@tonic-gate */ 281*0Sstevel@tonic-gate void 282*0Sstevel@tonic-gate set_daemon_log_flag(int flag) 283*0Sstevel@tonic-gate { 284*0Sstevel@tonic-gate if (flag) 285*0Sstevel@tonic-gate daemon_log_flag = LOG_DAEMON; 286*0Sstevel@tonic-gate else 287*0Sstevel@tonic-gate daemon_log_flag = 0; 288*0Sstevel@tonic-gate } 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate int 291*0Sstevel@tonic-gate parse_msg_flags(char *flags) 292*0Sstevel@tonic-gate { 293*0Sstevel@tonic-gate int msgflags = 0; 294*0Sstevel@tonic-gate char c; 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate while ((c = *flags++) != '\0') { 297*0Sstevel@tonic-gate switch (c) { 298*0Sstevel@tonic-gate case 'f': msgflags |= MSG_FATAL; break; 299*0Sstevel@tonic-gate case 'e': msgflags |= MSG_ERROR; break; 300*0Sstevel@tonic-gate case 'w': msgflags |= MSG_WARN; break; 301*0Sstevel@tonic-gate case 'i': msgflags |= MSG_INFO; break; 302*0Sstevel@tonic-gate case 'd': msgflags |= MSG_DEBUG; break; 303*0Sstevel@tonic-gate case 'D': msgflags |= MSG_FC_DEBUG; break; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate default: 306*0Sstevel@tonic-gate log_message(MSG_ERROR, "Invalid msglvl flag: %c\n", c); 307*0Sstevel@tonic-gate break; 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate return (msgflags); 311*0Sstevel@tonic-gate } 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate #define MAXERRBUF 256 314*0Sstevel@tonic-gate char error_buffer[MAXERRBUF]; 315*0Sstevel@tonic-gate 316*0Sstevel@tonic-gate static void 317*0Sstevel@tonic-gate dot_error_buffer(fcode_env_t *env) 318*0Sstevel@tonic-gate { 319*0Sstevel@tonic-gate log_message(MSG_INFO, "%s\n", error_buffer); 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate static void 323*0Sstevel@tonic-gate set_error_log(fcode_env_t *env) 324*0Sstevel@tonic-gate { 325*0Sstevel@tonic-gate char *fname; 326*0Sstevel@tonic-gate FILE *fp; 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate parse_word(env); 329*0Sstevel@tonic-gate fname = pop_a_string(env, NULL); 330*0Sstevel@tonic-gate if (fname != NULL) { 331*0Sstevel@tonic-gate if ((fp = fopen(fname, "a")) == NULL) { 332*0Sstevel@tonic-gate log_perror(MSG_ERROR, "Can't open '%s'\n", fname); 333*0Sstevel@tonic-gate return; 334*0Sstevel@tonic-gate } 335*0Sstevel@tonic-gate if (error_log_fp) 336*0Sstevel@tonic-gate fclose(error_log_fp); 337*0Sstevel@tonic-gate if (error_log_name) 338*0Sstevel@tonic-gate FREE(error_log_name); 339*0Sstevel@tonic-gate error_log_fp = fp; 340*0Sstevel@tonic-gate error_log_name = STRDUP(fname); 341*0Sstevel@tonic-gate error_log_flags = MSG_FATAL|MSG_ERROR|MSG_WARN|MSG_INFO| 342*0Sstevel@tonic-gate MSG_DEBUG|MSG_FC_DEBUG; 343*0Sstevel@tonic-gate } else if (error_log_name) 344*0Sstevel@tonic-gate log_message(MSG_INFO, "%s\n", error_log_name); 345*0Sstevel@tonic-gate else 346*0Sstevel@tonic-gate log_message(MSG_INFO, "NULL\n"); 347*0Sstevel@tonic-gate } 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate #pragma init(_init) 350*0Sstevel@tonic-gate 351*0Sstevel@tonic-gate static void 352*0Sstevel@tonic-gate _init(void) 353*0Sstevel@tonic-gate { 354*0Sstevel@tonic-gate fcode_env_t *env = initial_env; 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate ASSERT(env); 357*0Sstevel@tonic-gate NOTICE; 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate FORTH(0, ".error-buffer", dot_error_buffer); 360*0Sstevel@tonic-gate FORTH(0, "set-error-log", set_error_log); 361*0Sstevel@tonic-gate } 362