xref: /illumos-gate/usr/src/cmd/saf/log.c (revision 2a8bcb4e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/fcntl.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include <strings.h>
37 #include <errno.h>
38 
39 #include "extern.h"
40 #include "misc.h"
41 #include "msgs.h"
42 #include <sac.h>
43 #include "structs.h"
44 
45 static	FILE	*Lfp;	/* log file */
46 #ifdef DEBUG
47 static	FILE	*Dfp;	/* debug file */
48 #endif
49 
50 
51 /*
52  * cons_printf - emit a message to the system console
53  */
54 
55 /*PRINTFLIKE1*/
56 static void
cons_printf(const char * fmt,...)57 cons_printf(const char *fmt, ...)
58 {
59 	char buf[MAXPATHLEN * 2]; /* enough space for msg including a path */
60 	int fd;
61 	va_list ap;
62 
63 	va_start(ap, fmt);
64 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
65 	va_end(ap);
66 
67 	if ((fd = open("/dev/console", O_WRONLY|O_NOCTTY)) != -1)
68 		(void) write(fd, buf, strlen(buf) + 1);
69 	(void) close(fd);
70 }
71 
72 /*
73  * openlog - open log file, sets global file pointer Lfp
74  */
75 
76 void
openlog()77 openlog()
78 {
79 	if ((Lfp = fopen(LOGFILE, "a+")) == NULL) {
80 		cons_printf("SAC: could not open logfile %s: %s\n",
81 		    LOGFILE, strerror(errno));
82 		exit(1);
83 	}
84 
85 	/*
86 	 * lock logfile to indicate presence
87 	 */
88 	if (lockf(fileno(Lfp), F_LOCK, 0) < 0) {
89 		cons_printf("SAC: could not lock logfile %s:%s\n",
90 		    LOGFILE, strerror(errno));
91 		exit(1);
92 	}
93 }
94 
95 
96 /*
97  * log - put a message into the log file
98  *
99  *	args:	msg - message to be logged
100  */
101 void
log(char * msg)102 log(char *msg)
103 {
104 	char *timestamp;	/* current time in readable form */
105 	time_t clock;		/* current time in seconds */
106 	char buf[SIZE];		/* scratch buffer */
107 
108 	(void) time(&clock);
109 	timestamp = ctime(&clock);
110 	*(strchr(timestamp, '\n')) = '\0';
111 	(void) snprintf(buf, sizeof (buf), "%s; %ld; %s\n",
112 	    timestamp, getpid(), msg);
113 	(void) fprintf(Lfp, buf);
114 	(void) fflush(Lfp);
115 }
116 
117 
118 /*
119  * error - put an error message into the log file and exit if indicated
120  *
121  *	args:	msgid - id of message to be output
122  *		action - action to be taken (EXIT or not)
123  */
124 
125 
126 void
error(int msgid,int action)127 error(int msgid, int action)
128 {
129 	if (msgid < 0 || msgid > N_msgs)
130 		return;
131 	log(Msgs[msgid].e_str);
132 	if (action == EXIT) {
133 		log("*** SAC exiting ***");
134 		exit(Msgs[msgid].e_exitcode);
135 	}
136 }
137 
138 
139 #ifdef DEBUG
140 
141 /*
142  * opendebug - open debugging file, sets global file pointer Dfp
143  */
144 
145 
146 void
opendebug()147 opendebug()
148 {
149 	FILE *fp;	/* scratch file pointer for problems */
150 
151 	if ((Dfp = fopen(DBGFILE, "a+")) == NULL) {
152 		cons_printf("SAC: could not open debugfile %s: %s\n",
153 		    DBGFILE, strerror(errno));
154 		exit(1);
155 	}
156 }
157 
158 
159 /*
160  * debug - put a message into debug file
161  *
162  *	args:	msg - message to be output
163  */
164 
165 
166 void
debug(char * msg)167 debug(char *msg)
168 {
169 	char *timestamp;	/* current time in readable form */
170 	time_t clock;		/* current time in seconds */
171 	char buf[SIZE];		/* scratch buffer */
172 
173 	(void) time(&clock);
174 	timestamp = ctime(&clock);
175 	*(strchr(timestamp, '\n')) = '\0';
176 	(void) sprintf(buf, "%s; %ld; %s\n", timestamp, getpid(), msg);
177 	(void) fprintf(Dfp, buf);
178 	(void) fflush(Dfp);
179 }
180 
181 #endif /* DEBUG */
182