xref: /illumos-gate/usr/src/cmd/bnu/logent.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, 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 #include "uucp.h"
31 
32 static FILE	*_Lf = NULL;
33 static FILE	*_Cf = NULL;
34 static int	_Sf = -1;
35 static int	CurRole = MASTER;	/* Uucico's current role. */
36 
37 /*
38  * Make log entry
39  *	text	-> ptr to text string
40  *	status	-> ptr to status string
41  * Returns:
42  *	none
43  */
44 void
logent(text,status)45 logent(text, status)
46 register char	*text, *status;
47 {
48 	static	char	logfile[MAXFULLNAME];
49 	char		*prev;
50 
51 	if (*Rmtname == NULLCHAR) /* ignore logging if Rmtname is not yet set */
52 		return;
53 
54 	prev = _uu_setlocale(LC_ALL, "C");
55 	if (Nstat.t_pid == 0)
56 		Nstat.t_pid = getpid();
57 
58 	if (_Lf != NULL
59 	   && strncmp(Rmtname, BASENAME(logfile, '/'), MAXBASENAME) != 0) {
60 		fclose(_Lf);
61 		_Lf = NULL;
62 	}
63 
64 	if (_Lf == NULL) {
65 		sprintf(logfile, "%s/%s", Logfile, Rmtname);
66 		_Lf = fopen(logfile, "a");
67 		(void) chmod(logfile, LOGFILEMODE);
68 		if (_Lf == NULL) {
69 			(void) _uu_resetlocale(LC_ALL, prev);
70 			return;
71 		}
72 		setbuf(_Lf, CNULL);
73 	}
74 	(void) fseek(_Lf, 0L, 2);
75 	(void) fprintf(_Lf, "%s %s %s ", User, Rmtname, Jobid);
76 	(void) fprintf(_Lf, "(%s,%ld,%d) ", timeStamp(), (long) Nstat.t_pid, Seqn);
77 	(void) fprintf(_Lf, "%s (%s)\n", status, text);
78 	(void) _uu_resetlocale(LC_ALL, prev);
79 	return;
80 }
81 
82 
83 /*
84  * Make entry for a conversation (uucico only)
85  *	text	-> pointer to message string
86  * Returns:
87  *	none
88  */
89 void
usyslog(text)90 usyslog(text)
91 register char	*text;
92 {
93 	int	sbuflen;
94 	char	sysbuf[BUFSIZ];
95 	char	*prev = _uu_setlocale(LC_ALL, "C");
96 
97 	(void) sprintf(sysbuf, "%s!%s %s (%s) (%c,%ld,%d) [%s] %s\n",
98 		Rmtname, User, CurRole == SLAVE ? "S" : "M", timeStamp(),
99 		Pchar, (long) getpid(), Seqn, Dc, text);
100 	sbuflen = strlen(sysbuf);
101 	if (_Sf < 0) {
102 		errno = 0;
103 		_Sf = open(SYSLOG, 1);
104 		if (errno == ENOENT) {
105 			_Sf = creat(SYSLOG, LOGFILEMODE);
106 			(void) chmod(SYSLOG, LOGFILEMODE);
107 		}
108 		if (_Sf < 0) {
109 			(void) _uu_resetlocale(LC_ALL, prev);
110 			return;
111 		}
112 	}
113 	(void) lseek(_Sf, 0L, 2);
114 	(void) write(_Sf, sysbuf, sbuflen);
115 	(void) _uu_resetlocale(LC_ALL, prev);
116 	return;
117 }
118 
119 /*
120  * Make entry for a command
121  *	argc	-> number of command arguments
122  *	argv	-> pointer array to command arguments
123  * Returns:
124  *	none
125  */
126 void
commandlog(argc,argv)127 commandlog(argc,argv)
128 int argc;
129 char **argv;
130 {
131 	int	fd;
132 	char	*prev = _uu_setlocale(LC_ALL, "C");
133 
134 	if (_Cf == NULL) {
135 		errno = 0;
136 		fd = open(CMDLOG, O_WRONLY | O_APPEND);
137 		if (errno == ENOENT) {
138 			fd = creat(CMDLOG, LOGFILEMODE);
139 			(void) chmod(CMDLOG, LOGFILEMODE);
140 		}
141 		if (fd < 0 || (_Cf = fdopen(fd, "a")) == NULL) {
142 			(void) _uu_resetlocale(LC_ALL, prev);
143 			return;
144 		}
145 	}
146 	(void) fprintf(_Cf, "%s (%s) ",User, timeStamp() );
147 	while (argc-- > 0) {
148 		(void) fprintf(_Cf, "%s%c", *argv++, (argc > 0)?' ':'\n');
149 	}
150 	(void) fflush(_Cf);
151 	(void) _uu_resetlocale(LC_ALL, prev);
152 	return;
153 }
154 
155 /*
156  * Close log files before a fork
157  */
158 void
ucloselog()159 ucloselog()
160 {
161 	if (_Sf >= 0) {
162 		(void) close(_Sf);
163 		_Sf = -1;
164 	}
165 	if (_Lf) {
166 		(void) fclose(_Lf);
167 		_Lf = NULL;
168 	}
169 	if (_Cf) {
170 		(void) fclose(_Cf);
171 		_Cf = NULL;
172 	}
173 	return;
174 }
175 
176 /*
177  *	millitick()
178  *
179  *	return msec since last time called
180  */
181 #ifdef ATTSV
182 #include <values.h>
183 
184 time_t
millitick()185 millitick()
186 {
187 	struct tms	tbuf;
188 	time_t	now, rval;
189 	static time_t	past;	/* guaranteed 0 first time called */
190 
191 	if (past == 0) {
192 		past = times(&tbuf);
193 		rval = 0;
194 	} else {
195 		now = times(&tbuf);
196 		if (now - past > MAXLONG / 1000)	/* would overflow */
197 			rval = (now - past) / HZ * 1000;
198 		else
199 			rval = (now - past) * 1000 / HZ;
200 		past = now;
201 	}
202 	return(rval);
203 }
204 
205 #else /* !ATTSV */
206 time_t
millitick()207 millitick()
208 {
209 	struct timeb	tbuf;
210 	static struct timeb	tbuf1;
211 	static past;		/* guaranteed 0 first time called */
212 	time_t	rval;
213 
214 	if (past == 0) {
215 		past++;
216 		rval = 0;
217 		ftime(&tbuf1);
218 	} else {
219 		ftime(&tbuf);
220 		rval = (tbuf.time - tbuf1.time) * 1000
221 			+ tbuf.millitm - tbuf1.millitm;
222 		tbuf1 = tbuf;
223 	}
224 	return(rval);
225 }
226 #endif /* ATTSV */
227