xref: /onnv-gate/usr/src/cmd/saf/log.c (revision 3030:d3552bcc4eed)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3030Ssn199410  * Common Development and Distribution License (the "License").
6*3030Ssn199410  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3030Ssn199410  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/fcntl.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <stdarg.h>
38334Sdp #include <strings.h>
39334Sdp #include <errno.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "extern.h"
420Sstevel@tonic-gate #include "misc.h"
430Sstevel@tonic-gate #include "msgs.h"
440Sstevel@tonic-gate #include <sac.h>
450Sstevel@tonic-gate #include "structs.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static	FILE	*Lfp;	/* log file */
480Sstevel@tonic-gate #ifdef DEBUG
490Sstevel@tonic-gate static	FILE	*Dfp;	/* debug file */
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * cons_printf - emit a message to the system console
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*PRINTFLIKE1*/
580Sstevel@tonic-gate static void
cons_printf(const char * fmt,...)590Sstevel@tonic-gate cons_printf(const char *fmt, ...)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
620Sstevel@tonic-gate 	int fd;
630Sstevel@tonic-gate 	va_list ap;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	va_start(ap, fmt);
660Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
670Sstevel@tonic-gate 	va_end(ap);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if ((fd = open("/dev/console", O_WRONLY|O_NOCTTY)) != -1)
700Sstevel@tonic-gate 		(void) write(fd, buf, strlen(buf) + 1);
710Sstevel@tonic-gate 	(void) close(fd);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * openlog - open log file, sets global file pointer Lfp
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate void
openlog()790Sstevel@tonic-gate openlog()
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	if ((Lfp = fopen(LOGFILE, "a+")) == NULL) {
820Sstevel@tonic-gate 		cons_printf("SAC: could not open logfile %s: %s\n",
830Sstevel@tonic-gate 		    LOGFILE, strerror(errno));
840Sstevel@tonic-gate 		exit(1);
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * lock logfile to indicate presence
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 	if (lockf(fileno(Lfp), F_LOCK, 0) < 0) {
910Sstevel@tonic-gate 		cons_printf("SAC: could not lock logfile %s:%s\n",
920Sstevel@tonic-gate 		    LOGFILE, strerror(errno));
930Sstevel@tonic-gate 		exit(1);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * log - put a message into the log file
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  *	args:	msg - message to be logged
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate void
log(char * msg)1040Sstevel@tonic-gate log(char *msg)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	char *timestamp;	/* current time in readable form */
1070Sstevel@tonic-gate 	time_t clock;		/* current time in seconds */
1080Sstevel@tonic-gate 	char buf[SIZE];		/* scratch buffer */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	(void) time(&clock);
1110Sstevel@tonic-gate 	timestamp = ctime(&clock);
1120Sstevel@tonic-gate 	*(strchr(timestamp, '\n')) = '\0';
113*3030Ssn199410 	(void) snprintf(buf, sizeof (buf), "%s; %ld; %s\n",
114*3030Ssn199410 	    timestamp, getpid(), msg);
1150Sstevel@tonic-gate 	(void) fprintf(Lfp, buf);
1160Sstevel@tonic-gate 	(void) fflush(Lfp);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * error - put an error message into the log file and exit if indicated
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  *	args:	msgid - id of message to be output
1240Sstevel@tonic-gate  *		action - action to be taken (EXIT or not)
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate void
error(int msgid,int action)1290Sstevel@tonic-gate error(int msgid, int action)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	if (msgid < 0 || msgid > N_msgs)
1320Sstevel@tonic-gate 		return;
1330Sstevel@tonic-gate 	log(Msgs[msgid].e_str);
1340Sstevel@tonic-gate 	if (action == EXIT) {
1350Sstevel@tonic-gate 		log("*** SAC exiting ***");
1360Sstevel@tonic-gate 		exit(Msgs[msgid].e_exitcode);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #ifdef DEBUG
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * opendebug - open debugging file, sets global file pointer Dfp
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate void
opendebug()1490Sstevel@tonic-gate opendebug()
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	FILE *fp;	/* scratch file pointer for problems */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if ((Dfp = fopen(DBGFILE, "a+")) == NULL) {
1540Sstevel@tonic-gate 		cons_printf("SAC: could not open debugfile %s: %s\n",
1550Sstevel@tonic-gate 		    DBGFILE, strerror(errno));
1560Sstevel@tonic-gate 		exit(1);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * debug - put a message into debug file
1630Sstevel@tonic-gate  *
1640Sstevel@tonic-gate  *	args:	msg - message to be output
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate void
debug(char * msg)1690Sstevel@tonic-gate debug(char *msg)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	char *timestamp;	/* current time in readable form */
1720Sstevel@tonic-gate 	time_t clock;		/* current time in seconds */
1730Sstevel@tonic-gate 	char buf[SIZE];		/* scratch buffer */
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	(void) time(&clock);
1760Sstevel@tonic-gate 	timestamp = ctime(&clock);
1770Sstevel@tonic-gate 	*(strchr(timestamp, '\n')) = '\0';
1780Sstevel@tonic-gate 	(void) sprintf(buf, "%s; %ld; %s\n", timestamp, getpid(), msg);
1790Sstevel@tonic-gate 	(void) fprintf(Dfp, buf);
1800Sstevel@tonic-gate 	(void) fflush(Dfp);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate #endif /* DEBUG */
184