xref: /titanic_52/usr/src/cmd/bnu/logent.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "uucp.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate static FILE	*_Lf = NULL;
36*7c478bd9Sstevel@tonic-gate static FILE	*_Cf = NULL;
37*7c478bd9Sstevel@tonic-gate static int	_Sf = -1;
38*7c478bd9Sstevel@tonic-gate static int	CurRole = MASTER;	/* Uucico's current role. */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * Make log entry
42*7c478bd9Sstevel@tonic-gate  *	text	-> ptr to text string
43*7c478bd9Sstevel@tonic-gate  *	status	-> ptr to status string
44*7c478bd9Sstevel@tonic-gate  * Returns:
45*7c478bd9Sstevel@tonic-gate  *	none
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate void
48*7c478bd9Sstevel@tonic-gate logent(text, status)
49*7c478bd9Sstevel@tonic-gate register char	*text, *status;
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	static	char	logfile[MAXFULLNAME];
52*7c478bd9Sstevel@tonic-gate 	char		*prev;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if (*Rmtname == NULLCHAR) /* ignore logging if Rmtname is not yet set */
55*7c478bd9Sstevel@tonic-gate 		return;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	prev = _uu_setlocale(LC_ALL, "C");
58*7c478bd9Sstevel@tonic-gate 	if (Nstat.t_pid == 0)
59*7c478bd9Sstevel@tonic-gate 		Nstat.t_pid = getpid();
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	if (_Lf != NULL
62*7c478bd9Sstevel@tonic-gate 	   && strncmp(Rmtname, BASENAME(logfile, '/'), MAXBASENAME) != 0) {
63*7c478bd9Sstevel@tonic-gate 		fclose(_Lf);
64*7c478bd9Sstevel@tonic-gate 		_Lf = NULL;
65*7c478bd9Sstevel@tonic-gate 	}
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (_Lf == NULL) {
68*7c478bd9Sstevel@tonic-gate 		sprintf(logfile, "%s/%s", Logfile, Rmtname);
69*7c478bd9Sstevel@tonic-gate 		_Lf = fopen(logfile, "a");
70*7c478bd9Sstevel@tonic-gate 		(void) chmod(logfile, LOGFILEMODE);
71*7c478bd9Sstevel@tonic-gate 		if (_Lf == NULL) {
72*7c478bd9Sstevel@tonic-gate 			(void) _uu_resetlocale(LC_ALL, prev);
73*7c478bd9Sstevel@tonic-gate 			return;
74*7c478bd9Sstevel@tonic-gate 		}
75*7c478bd9Sstevel@tonic-gate 		setbuf(_Lf, CNULL);
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 	(void) fseek(_Lf, 0L, 2);
78*7c478bd9Sstevel@tonic-gate 	(void) fprintf(_Lf, "%s %s %s ", User, Rmtname, Jobid);
79*7c478bd9Sstevel@tonic-gate 	(void) fprintf(_Lf, "(%s,%ld,%d) ", timeStamp(), (long) Nstat.t_pid, Seqn);
80*7c478bd9Sstevel@tonic-gate 	(void) fprintf(_Lf, "%s (%s)\n", status, text);
81*7c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
82*7c478bd9Sstevel@tonic-gate 	return;
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /*
87*7c478bd9Sstevel@tonic-gate  * Make entry for a conversation (uucico only)
88*7c478bd9Sstevel@tonic-gate  *	text	-> pointer to message string
89*7c478bd9Sstevel@tonic-gate  * Returns:
90*7c478bd9Sstevel@tonic-gate  *	none
91*7c478bd9Sstevel@tonic-gate  */
92*7c478bd9Sstevel@tonic-gate void
93*7c478bd9Sstevel@tonic-gate usyslog(text)
94*7c478bd9Sstevel@tonic-gate register char	*text;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	int	sbuflen;
97*7c478bd9Sstevel@tonic-gate 	char	sysbuf[BUFSIZ];
98*7c478bd9Sstevel@tonic-gate 	char	*prev = _uu_setlocale(LC_ALL, "C");
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	(void) sprintf(sysbuf, "%s!%s %s (%s) (%c,%ld,%d) [%s] %s\n",
101*7c478bd9Sstevel@tonic-gate 		Rmtname, User, CurRole == SLAVE ? "S" : "M", timeStamp(),
102*7c478bd9Sstevel@tonic-gate 		Pchar, (long) getpid(), Seqn, Dc, text);
103*7c478bd9Sstevel@tonic-gate 	sbuflen = strlen(sysbuf);
104*7c478bd9Sstevel@tonic-gate 	if (_Sf < 0) {
105*7c478bd9Sstevel@tonic-gate 		errno = 0;
106*7c478bd9Sstevel@tonic-gate 		_Sf = open(SYSLOG, 1);
107*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
108*7c478bd9Sstevel@tonic-gate 			_Sf = creat(SYSLOG, LOGFILEMODE);
109*7c478bd9Sstevel@tonic-gate 			(void) chmod(SYSLOG, LOGFILEMODE);
110*7c478bd9Sstevel@tonic-gate 		}
111*7c478bd9Sstevel@tonic-gate 		if (_Sf < 0) {
112*7c478bd9Sstevel@tonic-gate 			(void) _uu_resetlocale(LC_ALL, prev);
113*7c478bd9Sstevel@tonic-gate 			return;
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	(void) lseek(_Sf, 0L, 2);
117*7c478bd9Sstevel@tonic-gate 	(void) write(_Sf, sysbuf, sbuflen);
118*7c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
119*7c478bd9Sstevel@tonic-gate 	return;
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate /*
123*7c478bd9Sstevel@tonic-gate  * Make entry for a command
124*7c478bd9Sstevel@tonic-gate  *	argc	-> number of command arguments
125*7c478bd9Sstevel@tonic-gate  *	argv	-> pointer array to command arguments
126*7c478bd9Sstevel@tonic-gate  * Returns:
127*7c478bd9Sstevel@tonic-gate  *	none
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate void
130*7c478bd9Sstevel@tonic-gate commandlog(argc,argv)
131*7c478bd9Sstevel@tonic-gate int argc;
132*7c478bd9Sstevel@tonic-gate char **argv;
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	int	fd;
135*7c478bd9Sstevel@tonic-gate 	char	*prev = _uu_setlocale(LC_ALL, "C");
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (_Cf == NULL) {
138*7c478bd9Sstevel@tonic-gate 		errno = 0;
139*7c478bd9Sstevel@tonic-gate 		fd = open(CMDLOG, O_WRONLY | O_APPEND);
140*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
141*7c478bd9Sstevel@tonic-gate 			fd = creat(CMDLOG, LOGFILEMODE);
142*7c478bd9Sstevel@tonic-gate 			(void) chmod(CMDLOG, LOGFILEMODE);
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 		if (fd < 0 || (_Cf = fdopen(fd, "a")) == NULL) {
145*7c478bd9Sstevel@tonic-gate 			(void) _uu_resetlocale(LC_ALL, prev);
146*7c478bd9Sstevel@tonic-gate 			return;
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	(void) fprintf(_Cf, "%s (%s) ",User, timeStamp() );
150*7c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
151*7c478bd9Sstevel@tonic-gate 		(void) fprintf(_Cf, "%s%c", *argv++, (argc > 0)?' ':'\n');
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 	(void) fflush(_Cf);
154*7c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
155*7c478bd9Sstevel@tonic-gate 	return;
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate  * Close log files before a fork
160*7c478bd9Sstevel@tonic-gate  */
161*7c478bd9Sstevel@tonic-gate void
162*7c478bd9Sstevel@tonic-gate ucloselog()
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	if (_Sf >= 0) {
165*7c478bd9Sstevel@tonic-gate 		(void) close(_Sf);
166*7c478bd9Sstevel@tonic-gate 		_Sf = -1;
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 	if (_Lf) {
169*7c478bd9Sstevel@tonic-gate 		(void) fclose(_Lf);
170*7c478bd9Sstevel@tonic-gate 		_Lf = NULL;
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	if (_Cf) {
173*7c478bd9Sstevel@tonic-gate 		(void) fclose(_Cf);
174*7c478bd9Sstevel@tonic-gate 		_Cf = NULL;
175*7c478bd9Sstevel@tonic-gate 	}
176*7c478bd9Sstevel@tonic-gate 	return;
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate /*
180*7c478bd9Sstevel@tonic-gate  *	millitick()
181*7c478bd9Sstevel@tonic-gate  *
182*7c478bd9Sstevel@tonic-gate  *	return msec since last time called
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate #ifdef ATTSV
185*7c478bd9Sstevel@tonic-gate #include <values.h>
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate time_t
188*7c478bd9Sstevel@tonic-gate millitick()
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate 	struct tms	tbuf;
191*7c478bd9Sstevel@tonic-gate 	time_t	now, rval;
192*7c478bd9Sstevel@tonic-gate 	static time_t	past;	/* guaranteed 0 first time called */
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if (past == 0) {
195*7c478bd9Sstevel@tonic-gate 		past = times(&tbuf);
196*7c478bd9Sstevel@tonic-gate 		rval = 0;
197*7c478bd9Sstevel@tonic-gate 	} else {
198*7c478bd9Sstevel@tonic-gate 		now = times(&tbuf);
199*7c478bd9Sstevel@tonic-gate 		if (now - past > MAXLONG / 1000)	/* would overflow */
200*7c478bd9Sstevel@tonic-gate 			rval = (now - past) / HZ * 1000;
201*7c478bd9Sstevel@tonic-gate 		else
202*7c478bd9Sstevel@tonic-gate 			rval = (now - past) * 1000 / HZ;
203*7c478bd9Sstevel@tonic-gate 		past = now;
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 	return(rval);
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate #else /* !ATTSV */
209*7c478bd9Sstevel@tonic-gate time_t
210*7c478bd9Sstevel@tonic-gate millitick()
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	struct timeb	tbuf;
213*7c478bd9Sstevel@tonic-gate 	static struct timeb	tbuf1;
214*7c478bd9Sstevel@tonic-gate 	static past;		/* guaranteed 0 first time called */
215*7c478bd9Sstevel@tonic-gate 	time_t	rval;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	if (past == 0) {
218*7c478bd9Sstevel@tonic-gate 		past++;
219*7c478bd9Sstevel@tonic-gate 		rval = 0;
220*7c478bd9Sstevel@tonic-gate 		ftime(&tbuf1);
221*7c478bd9Sstevel@tonic-gate 	} else {
222*7c478bd9Sstevel@tonic-gate 		ftime(&tbuf);
223*7c478bd9Sstevel@tonic-gate 		rval = (tbuf.time - tbuf1.time) * 1000
224*7c478bd9Sstevel@tonic-gate 			+ tbuf.millitm - tbuf1.millitm;
225*7c478bd9Sstevel@tonic-gate 		tbuf1 = tbuf;
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 	return(rval);
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate #endif /* ATTSV */
230