xref: /illumos-gate/usr/src/cmd/bnu/logent.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "uucp.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate static FILE	*_Lf = NULL;
337c478bd9Sstevel@tonic-gate static FILE	*_Cf = NULL;
347c478bd9Sstevel@tonic-gate static int	_Sf = -1;
357c478bd9Sstevel@tonic-gate static int	CurRole = MASTER;	/* Uucico's current role. */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Make log entry
397c478bd9Sstevel@tonic-gate  *	text	-> ptr to text string
407c478bd9Sstevel@tonic-gate  *	status	-> ptr to status string
417c478bd9Sstevel@tonic-gate  * Returns:
427c478bd9Sstevel@tonic-gate  *	none
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate void
logent(text,status)457c478bd9Sstevel@tonic-gate logent(text, status)
467c478bd9Sstevel@tonic-gate register char	*text, *status;
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	static	char	logfile[MAXFULLNAME];
497c478bd9Sstevel@tonic-gate 	char		*prev;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	if (*Rmtname == NULLCHAR) /* ignore logging if Rmtname is not yet set */
527c478bd9Sstevel@tonic-gate 		return;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	prev = _uu_setlocale(LC_ALL, "C");
557c478bd9Sstevel@tonic-gate 	if (Nstat.t_pid == 0)
567c478bd9Sstevel@tonic-gate 		Nstat.t_pid = getpid();
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	if (_Lf != NULL
597c478bd9Sstevel@tonic-gate 	   && strncmp(Rmtname, BASENAME(logfile, '/'), MAXBASENAME) != 0) {
607c478bd9Sstevel@tonic-gate 		fclose(_Lf);
617c478bd9Sstevel@tonic-gate 		_Lf = NULL;
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (_Lf == NULL) {
657c478bd9Sstevel@tonic-gate 		sprintf(logfile, "%s/%s", Logfile, Rmtname);
667c478bd9Sstevel@tonic-gate 		_Lf = fopen(logfile, "a");
677c478bd9Sstevel@tonic-gate 		(void) chmod(logfile, LOGFILEMODE);
687c478bd9Sstevel@tonic-gate 		if (_Lf == NULL) {
697c478bd9Sstevel@tonic-gate 			(void) _uu_resetlocale(LC_ALL, prev);
707c478bd9Sstevel@tonic-gate 			return;
717c478bd9Sstevel@tonic-gate 		}
727c478bd9Sstevel@tonic-gate 		setbuf(_Lf, CNULL);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	(void) fseek(_Lf, 0L, 2);
757c478bd9Sstevel@tonic-gate 	(void) fprintf(_Lf, "%s %s %s ", User, Rmtname, Jobid);
767c478bd9Sstevel@tonic-gate 	(void) fprintf(_Lf, "(%s,%ld,%d) ", timeStamp(), (long) Nstat.t_pid, Seqn);
777c478bd9Sstevel@tonic-gate 	(void) fprintf(_Lf, "%s (%s)\n", status, text);
787c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
797c478bd9Sstevel@tonic-gate 	return;
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Make entry for a conversation (uucico only)
857c478bd9Sstevel@tonic-gate  *	text	-> pointer to message string
867c478bd9Sstevel@tonic-gate  * Returns:
877c478bd9Sstevel@tonic-gate  *	none
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate void
usyslog(text)907c478bd9Sstevel@tonic-gate usyslog(text)
917c478bd9Sstevel@tonic-gate register char	*text;
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	int	sbuflen;
947c478bd9Sstevel@tonic-gate 	char	sysbuf[BUFSIZ];
957c478bd9Sstevel@tonic-gate 	char	*prev = _uu_setlocale(LC_ALL, "C");
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	(void) sprintf(sysbuf, "%s!%s %s (%s) (%c,%ld,%d) [%s] %s\n",
987c478bd9Sstevel@tonic-gate 		Rmtname, User, CurRole == SLAVE ? "S" : "M", timeStamp(),
997c478bd9Sstevel@tonic-gate 		Pchar, (long) getpid(), Seqn, Dc, text);
1007c478bd9Sstevel@tonic-gate 	sbuflen = strlen(sysbuf);
1017c478bd9Sstevel@tonic-gate 	if (_Sf < 0) {
1027c478bd9Sstevel@tonic-gate 		errno = 0;
1037c478bd9Sstevel@tonic-gate 		_Sf = open(SYSLOG, 1);
1047c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
1057c478bd9Sstevel@tonic-gate 			_Sf = creat(SYSLOG, LOGFILEMODE);
1067c478bd9Sstevel@tonic-gate 			(void) chmod(SYSLOG, LOGFILEMODE);
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		if (_Sf < 0) {
1097c478bd9Sstevel@tonic-gate 			(void) _uu_resetlocale(LC_ALL, prev);
1107c478bd9Sstevel@tonic-gate 			return;
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	(void) lseek(_Sf, 0L, 2);
1147c478bd9Sstevel@tonic-gate 	(void) write(_Sf, sysbuf, sbuflen);
1157c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
1167c478bd9Sstevel@tonic-gate 	return;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
120*2a8bcb4eSToomas Soome  * Make entry for a command
1217c478bd9Sstevel@tonic-gate  *	argc	-> number of command arguments
1227c478bd9Sstevel@tonic-gate  *	argv	-> pointer array to command arguments
1237c478bd9Sstevel@tonic-gate  * Returns:
1247c478bd9Sstevel@tonic-gate  *	none
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate void
commandlog(argc,argv)1277c478bd9Sstevel@tonic-gate commandlog(argc,argv)
1287c478bd9Sstevel@tonic-gate int argc;
1297c478bd9Sstevel@tonic-gate char **argv;
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int	fd;
1327c478bd9Sstevel@tonic-gate 	char	*prev = _uu_setlocale(LC_ALL, "C");
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (_Cf == NULL) {
1357c478bd9Sstevel@tonic-gate 		errno = 0;
1367c478bd9Sstevel@tonic-gate 		fd = open(CMDLOG, O_WRONLY | O_APPEND);
1377c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
1387c478bd9Sstevel@tonic-gate 			fd = creat(CMDLOG, LOGFILEMODE);
1397c478bd9Sstevel@tonic-gate 			(void) chmod(CMDLOG, LOGFILEMODE);
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 		if (fd < 0 || (_Cf = fdopen(fd, "a")) == NULL) {
1427c478bd9Sstevel@tonic-gate 			(void) _uu_resetlocale(LC_ALL, prev);
1437c478bd9Sstevel@tonic-gate 			return;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	(void) fprintf(_Cf, "%s (%s) ",User, timeStamp() );
1477c478bd9Sstevel@tonic-gate 	while (argc-- > 0) {
1487c478bd9Sstevel@tonic-gate 		(void) fprintf(_Cf, "%s%c", *argv++, (argc > 0)?' ':'\n');
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	(void) fflush(_Cf);
1517c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
1527c478bd9Sstevel@tonic-gate 	return;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Close log files before a fork
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate void
ucloselog()1597c478bd9Sstevel@tonic-gate ucloselog()
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	if (_Sf >= 0) {
1627c478bd9Sstevel@tonic-gate 		(void) close(_Sf);
1637c478bd9Sstevel@tonic-gate 		_Sf = -1;
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	if (_Lf) {
1667c478bd9Sstevel@tonic-gate 		(void) fclose(_Lf);
1677c478bd9Sstevel@tonic-gate 		_Lf = NULL;
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	if (_Cf) {
1707c478bd9Sstevel@tonic-gate 		(void) fclose(_Cf);
1717c478bd9Sstevel@tonic-gate 		_Cf = NULL;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  *	millitick()
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  *	return msec since last time called
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate #ifdef ATTSV
1827c478bd9Sstevel@tonic-gate #include <values.h>
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate time_t
millitick()1857c478bd9Sstevel@tonic-gate millitick()
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	struct tms	tbuf;
1887c478bd9Sstevel@tonic-gate 	time_t	now, rval;
1897c478bd9Sstevel@tonic-gate 	static time_t	past;	/* guaranteed 0 first time called */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (past == 0) {
1927c478bd9Sstevel@tonic-gate 		past = times(&tbuf);
1937c478bd9Sstevel@tonic-gate 		rval = 0;
1947c478bd9Sstevel@tonic-gate 	} else {
1957c478bd9Sstevel@tonic-gate 		now = times(&tbuf);
1967c478bd9Sstevel@tonic-gate 		if (now - past > MAXLONG / 1000)	/* would overflow */
1977c478bd9Sstevel@tonic-gate 			rval = (now - past) / HZ * 1000;
1987c478bd9Sstevel@tonic-gate 		else
1997c478bd9Sstevel@tonic-gate 			rval = (now - past) * 1000 / HZ;
2007c478bd9Sstevel@tonic-gate 		past = now;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	return(rval);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate #else /* !ATTSV */
2067c478bd9Sstevel@tonic-gate time_t
millitick()2077c478bd9Sstevel@tonic-gate millitick()
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	struct timeb	tbuf;
2107c478bd9Sstevel@tonic-gate 	static struct timeb	tbuf1;
2117c478bd9Sstevel@tonic-gate 	static past;		/* guaranteed 0 first time called */
2127c478bd9Sstevel@tonic-gate 	time_t	rval;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (past == 0) {
2157c478bd9Sstevel@tonic-gate 		past++;
2167c478bd9Sstevel@tonic-gate 		rval = 0;
2177c478bd9Sstevel@tonic-gate 		ftime(&tbuf1);
2187c478bd9Sstevel@tonic-gate 	} else {
2197c478bd9Sstevel@tonic-gate 		ftime(&tbuf);
2207c478bd9Sstevel@tonic-gate 		rval = (tbuf.time - tbuf1.time) * 1000
2217c478bd9Sstevel@tonic-gate 			+ tbuf.millitm - tbuf1.millitm;
2227c478bd9Sstevel@tonic-gate 		tbuf1 = tbuf;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	return(rval);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate #endif /* ATTSV */
227