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