xref: /onnv-gate/usr/src/cmd/ttymon/tmlog.c (revision 0:68f95e015346)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * error/logging/cleanup functions for ttymon.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <stdlib.h>
39*0Sstevel@tonic-gate #include <stdio.h>
40*0Sstevel@tonic-gate #include <fcntl.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate #include <errno.h>
43*0Sstevel@tonic-gate #include <stdarg.h>
44*0Sstevel@tonic-gate #include <time.h>
45*0Sstevel@tonic-gate #include <sys/types.h>
46*0Sstevel@tonic-gate #include <sys/stat.h>
47*0Sstevel@tonic-gate #include <sys/param.h>
48*0Sstevel@tonic-gate #include <signal.h>
49*0Sstevel@tonic-gate #include <syslog.h>
50*0Sstevel@tonic-gate #include "ttymon.h"
51*0Sstevel@tonic-gate #include "tmstruct.h"
52*0Sstevel@tonic-gate #include "tmextern.h"
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate # ifdef DEBUG
55*0Sstevel@tonic-gate extern FILE *Debugfp;
56*0Sstevel@tonic-gate # endif
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate const char *appname = "ttymon";
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate void
openttymonlog(void)61*0Sstevel@tonic-gate openttymonlog(void)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	int	fd, ret;
64*0Sstevel@tonic-gate 	char	logfile[MAXPATHLEN];
65*0Sstevel@tonic-gate 	extern	char	*Tag;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	/* the log file resides in /var/saf/pmtag/ */
68*0Sstevel@tonic-gate 	(void) snprintf(logfile, sizeof (logfile), "%s%s/%s", LOGDIR, Tag,
69*0Sstevel@tonic-gate 	    LOGFILE);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	Logfp = NULL;
72*0Sstevel@tonic-gate 	(void)close(0);
73*0Sstevel@tonic-gate 	if ((fd = open(logfile, O_WRONLY|O_CREAT|O_APPEND,0444)) != -1)
74*0Sstevel@tonic-gate 		if ((ret = fcntl(fd, F_DUPFD, 3)) == 3) {
75*0Sstevel@tonic-gate 			/* set close-on-exec flag */
76*0Sstevel@tonic-gate 			if (fcntl(ret, F_SETFD, FD_CLOEXEC) == 0) {
77*0Sstevel@tonic-gate 				Logfp = fdopen(ret, "a+");
78*0Sstevel@tonic-gate 			}
79*0Sstevel@tonic-gate 		}
80*0Sstevel@tonic-gate 	if (!Logfp) {
81*0Sstevel@tonic-gate 		cons_printf("ttymon cannot create log file \"%s\": %s\n",
82*0Sstevel@tonic-gate 		    logfile, strerror(errno));
83*0Sstevel@tonic-gate 		exit(1);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	log(" ");
86*0Sstevel@tonic-gate 	log("********** ttymon starting **********");
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate #ifdef	DEBUG
89*0Sstevel@tonic-gate 	log("fd(log)\t = %d", fileno(Logfp));
90*0Sstevel@tonic-gate #endif
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static void
roll_log()94*0Sstevel@tonic-gate roll_log()
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	char logf[MAXPATHLEN];
97*0Sstevel@tonic-gate 	char ologf[MAXPATHLEN];
98*0Sstevel@tonic-gate 	char tlogf[MAXPATHLEN];
99*0Sstevel@tonic-gate 	FILE *nlogfp;
100*0Sstevel@tonic-gate 	struct stat buf;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	(void) fprintf(Logfp, "Restarting log file\n");
103*0Sstevel@tonic-gate 	(void) snprintf(logf, sizeof (logf), "%s%s/%s", LOGDIR, Tag, LOGFILE);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	(void) snprintf(ologf, sizeof (ologf), "%s%s/%s", LOGDIR, Tag,
106*0Sstevel@tonic-gate 	    OLOGFILE);
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	(void) snprintf(tlogf, sizeof (tlogf), "%s%s/%s", LOGDIR, Tag,
109*0Sstevel@tonic-gate 	    TLOGFILE);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (!stat(ologf, &buf) && rename(ologf, tlogf)) {
112*0Sstevel@tonic-gate 		(void) fprintf(Logfp, "rename old to tmp file failed\n");
113*0Sstevel@tonic-gate 	} else if (!stat(logf, &buf) && rename(logf, ologf)) {
114*0Sstevel@tonic-gate 		(void) fprintf(Logfp, "rename log to old file failed\n");
115*0Sstevel@tonic-gate 		/* Restore old log file */
116*0Sstevel@tonic-gate 		if (!stat(tlogf, &buf) && rename(tlogf, ologf))
117*0Sstevel@tonic-gate 			(void) fprintf(Logfp,
118*0Sstevel@tonic-gate 			    "rename tmp to old file failed\n");
119*0Sstevel@tonic-gate 	} else if (nlogfp = fopen(logf, "w")) {
120*0Sstevel@tonic-gate 		(void) fclose(Logfp);
121*0Sstevel@tonic-gate 		Logfp = nlogfp;
122*0Sstevel@tonic-gate 		/* reset close-on-exec */
123*0Sstevel@tonic-gate 		(void) fcntl(fileno(Logfp), F_SETFD, 1);
124*0Sstevel@tonic-gate 	} else {
125*0Sstevel@tonic-gate 		(void) fprintf(Logfp, "log file open failed\n");
126*0Sstevel@tonic-gate 		/* Restore current and old log file */
127*0Sstevel@tonic-gate 		if (!stat(ologf, &buf) && rename(ologf, logf))
128*0Sstevel@tonic-gate 			(void) fprintf(Logfp,
129*0Sstevel@tonic-gate 			    "rename old to log file failed\n");
130*0Sstevel@tonic-gate 		else if (!stat(tlogf, &buf) && rename(tlogf, ologf))
131*0Sstevel@tonic-gate 			(void) fprintf(Logfp,
132*0Sstevel@tonic-gate 			    "rename tmp to old file failed\n");
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	(void) unlink(tlogf); /* remove any stale tmp logfile */
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate  * vlog(msg) - common message routine.
141*0Sstevel@tonic-gate  *	    - if Logfp is NULL, write message to stderr or CONSOLE
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate static void
vlog(const char * fmt,va_list ap)144*0Sstevel@tonic-gate vlog(const char *fmt, va_list ap)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	char *timestamp;	/* current time in readable form */
147*0Sstevel@tonic-gate 	time_t clock;		/* current time in seconds */
148*0Sstevel@tonic-gate 	int	fd;
149*0Sstevel@tonic-gate 	struct stat buf;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (Logfp) {
152*0Sstevel@tonic-gate 		if ((fstat(fileno(Logfp), &buf) != -1) &&
153*0Sstevel@tonic-gate 		    (buf.st_size >= Logmaxsz) && !Splflag) {
154*0Sstevel@tonic-gate 			Splflag = 1;
155*0Sstevel@tonic-gate 			roll_log();
156*0Sstevel@tonic-gate 			Splflag = 0;
157*0Sstevel@tonic-gate 		}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		(void) time(&clock);
160*0Sstevel@tonic-gate 		timestamp = ctime(&clock);
161*0Sstevel@tonic-gate 		*(strchr(timestamp, '\n')) = '\0';
162*0Sstevel@tonic-gate 		(void) fprintf(Logfp, "%s; %ld; ", timestamp, getpid());
163*0Sstevel@tonic-gate 		(void) vfprintf(Logfp, fmt, ap);
164*0Sstevel@tonic-gate 		if (fmt[strlen(fmt) - 1] != '\n')
165*0Sstevel@tonic-gate 			(void) fputc('\n', Logfp);
166*0Sstevel@tonic-gate 		(void) fflush(Logfp);
167*0Sstevel@tonic-gate 	} else if (isatty(STDERR_FILENO)) {
168*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", appname);
169*0Sstevel@tonic-gate 		(void) vfprintf(stderr, fmt, ap);
170*0Sstevel@tonic-gate 		if (fmt[strlen(fmt) - 1] != '\n')
171*0Sstevel@tonic-gate 			(void) fputc('\n', stderr);
172*0Sstevel@tonic-gate 		(void) fflush(stderr);
173*0Sstevel@tonic-gate 	} else if ((fd = open(CONSOLE, O_WRONLY|O_NOCTTY)) != -1) {
174*0Sstevel@tonic-gate 		FILE *f = fdopen(fd, "w");
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 		(void) fprintf(f, "%s: ", appname);
177*0Sstevel@tonic-gate 		(void) vfprintf(f, fmt, ap);
178*0Sstevel@tonic-gate 		if (fmt[strlen(fmt) - 1] != '\n')
179*0Sstevel@tonic-gate 			(void) fputc('\n', f);
180*0Sstevel@tonic-gate 		(void) fclose(f);
181*0Sstevel@tonic-gate 	} else {
182*0Sstevel@tonic-gate 		vsyslog(LOG_CRIT, fmt, ap);
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate /*
187*0Sstevel@tonic-gate  * log(fmt, ...) - put a message into the log file
188*0Sstevel@tonic-gate  *	    - if Logfp is NULL, write message to stderr or CONSOLE
189*0Sstevel@tonic-gate  */
190*0Sstevel@tonic-gate /*PRINTFLIKE1*/
191*0Sstevel@tonic-gate void
log(const char * fmt,...)192*0Sstevel@tonic-gate log(const char *fmt, ...)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate 	va_list ap;
195*0Sstevel@tonic-gate 	va_start(ap, fmt);
196*0Sstevel@tonic-gate 	vlog(fmt, ap);
197*0Sstevel@tonic-gate 	va_end(ap);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate  * fatal(fmt, ...) - put a message into the log file, then exit.
203*0Sstevel@tonic-gate  */
204*0Sstevel@tonic-gate /*PRINTFLIKE1*/
205*0Sstevel@tonic-gate void
fatal(const char * fmt,...)206*0Sstevel@tonic-gate fatal(const char *fmt, ...)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	if (fmt) {
209*0Sstevel@tonic-gate 		va_list ap;
210*0Sstevel@tonic-gate 		va_start(ap, fmt);
211*0Sstevel@tonic-gate 		vlog(fmt, ap);
212*0Sstevel@tonic-gate 		va_end(ap);
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 	log("********** ttymon exiting ***********");
215*0Sstevel@tonic-gate 	exit(1);
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate # ifdef DEBUG
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate /*
221*0Sstevel@tonic-gate  * opendebug - open debugging file, sets global file pointer Debugfp
222*0Sstevel@tonic-gate  * 	arg:   getty - if TRUE, ttymon is in getty_mode and use a different
223*0Sstevel@tonic-gate  *		       debug file
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate void
opendebug(int getty_mode)227*0Sstevel@tonic-gate opendebug(int getty_mode)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	int  fd, ret;
230*0Sstevel@tonic-gate 	char	debugfile[BUFSIZ];
231*0Sstevel@tonic-gate 	extern	char	*Tag;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	if (!getty_mode) {
234*0Sstevel@tonic-gate 		(void)strcpy(debugfile, LOGDIR);
235*0Sstevel@tonic-gate 		(void)strcat(debugfile, Tag);
236*0Sstevel@tonic-gate 		(void)strcat(debugfile, "/");
237*0Sstevel@tonic-gate 		(void)strcat(debugfile, DBGFILE);
238*0Sstevel@tonic-gate 		if ((Debugfp = fopen(debugfile, "a+")) == NULL)
239*0Sstevel@tonic-gate 			fatal("open debug file failed");
240*0Sstevel@tonic-gate 	}
241*0Sstevel@tonic-gate 	else {
242*0Sstevel@tonic-gate 		if ((fd = open(EX_DBG, O_WRONLY|O_APPEND|O_CREAT)) < 0)
243*0Sstevel@tonic-gate 			fatal("open %s failed: %s", EX_DBG, errno);
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 		if (fd >= 3)
246*0Sstevel@tonic-gate 			ret = fd;
247*0Sstevel@tonic-gate 		else {
248*0Sstevel@tonic-gate 			if ((ret = fcntl(fd, F_DUPFD, 3)) < 0)
249*0Sstevel@tonic-gate 				fatal("F_DUPFD fcntl failed: %s",
250*0Sstevel@tonic-gate 				    strerror(errno));
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 		}
253*0Sstevel@tonic-gate 		if ((Debugfp = fdopen(ret, "a+")) == NULL)
254*0Sstevel@tonic-gate 			fatal("fdopen failed: %s", strerror(errno));
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		if (ret != fd)
257*0Sstevel@tonic-gate 			(void)close(fd);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 	/* set close-on-exec flag */
260*0Sstevel@tonic-gate 	if (fcntl(fileno(Debugfp), F_SETFD, 1) == -1)
261*0Sstevel@tonic-gate 		fatal("F_SETFD fcntl failed: %s", strerror(errno));
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate  * debug(msg) - put a message into debug file
266*0Sstevel@tonic-gate  */
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate void
debug(const char * fmt,...)269*0Sstevel@tonic-gate debug(const char *fmt, ...)
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate 	va_list ap;
272*0Sstevel@tonic-gate 	char *timestamp;	/* current time in readable form */
273*0Sstevel@tonic-gate 	time_t clock;		/* current time in seconds */
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	(void) time(&clock);
276*0Sstevel@tonic-gate 	timestamp = ctime(&clock);
277*0Sstevel@tonic-gate 	*(strchr(timestamp, '\n')) = '\0';
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	(void) fprintf(Debugfp, "%s; %ld; ", timestamp, getpid());
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	va_start(ap, fmt);
282*0Sstevel@tonic-gate 	(void) vfprintf(Debugfp, fmt, ap);
283*0Sstevel@tonic-gate 	va_end(ap);
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	(void) fprintf(Debugfp, "\n");
286*0Sstevel@tonic-gate 	(void) fflush(Debugfp);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate #endif
289