xref: /onnv-gate/usr/src/cmd/saf/log.c (revision 334)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*334Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/fcntl.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <stdarg.h>
39*334Sdp #include <strings.h>
40*334Sdp #include <errno.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "extern.h"
430Sstevel@tonic-gate #include "misc.h"
440Sstevel@tonic-gate #include "msgs.h"
450Sstevel@tonic-gate #include <sac.h>
460Sstevel@tonic-gate #include "structs.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static	FILE	*Lfp;	/* log file */
490Sstevel@tonic-gate #ifdef DEBUG
500Sstevel@tonic-gate static	FILE	*Dfp;	/* debug file */
510Sstevel@tonic-gate #endif
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * cons_printf - emit a message to the system console
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*PRINTFLIKE1*/
590Sstevel@tonic-gate static void
600Sstevel@tonic-gate cons_printf(const char *fmt, ...)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
630Sstevel@tonic-gate 	int fd;
640Sstevel@tonic-gate 	va_list ap;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	va_start(ap, fmt);
670Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
680Sstevel@tonic-gate 	va_end(ap);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if ((fd = open("/dev/console", O_WRONLY|O_NOCTTY)) != -1)
710Sstevel@tonic-gate 		(void) write(fd, buf, strlen(buf) + 1);
720Sstevel@tonic-gate 	(void) close(fd);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * openlog - open log file, sets global file pointer Lfp
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate void
800Sstevel@tonic-gate openlog()
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	if ((Lfp = fopen(LOGFILE, "a+")) == NULL) {
830Sstevel@tonic-gate 		cons_printf("SAC: could not open logfile %s: %s\n",
840Sstevel@tonic-gate 		    LOGFILE, strerror(errno));
850Sstevel@tonic-gate 		exit(1);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/*
890Sstevel@tonic-gate 	 * lock logfile to indicate presence
900Sstevel@tonic-gate 	 */
910Sstevel@tonic-gate 	if (lockf(fileno(Lfp), F_LOCK, 0) < 0) {
920Sstevel@tonic-gate 		cons_printf("SAC: could not lock logfile %s:%s\n",
930Sstevel@tonic-gate 		    LOGFILE, strerror(errno));
940Sstevel@tonic-gate 		exit(1);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * log - put a message into the log file
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  *	args:	msg - message to be logged
1030Sstevel@tonic-gate  */
1040Sstevel@tonic-gate void
1050Sstevel@tonic-gate log(char *msg)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	char *timestamp;	/* current time in readable form */
1080Sstevel@tonic-gate 	time_t clock;		/* current time in seconds */
1090Sstevel@tonic-gate 	char buf[SIZE];		/* scratch buffer */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	(void) time(&clock);
1120Sstevel@tonic-gate 	timestamp = ctime(&clock);
1130Sstevel@tonic-gate 	*(strchr(timestamp, '\n')) = '\0';
1140Sstevel@tonic-gate 	(void) sprintf(buf, "%s; %ld; %s\n", 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
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
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
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