xref: /openbsd-src/usr.sbin/ypserv/common/yplog.c (revision 33dd5606aea218813bc4cea9b2e78e64a39ef20a)
1*33dd5606Sderaadt /*	$OpenBSD: yplog.c,v 1.8 2002/07/19 02:38:40 deraadt Exp $ */
2c6db6f0eSchuck 
314c399efSderaadt /*
4c6db6f0eSchuck  * Copyright (c) 1996 Charles D. Cranor
514c399efSderaadt  * All rights reserved.
614c399efSderaadt  *
714c399efSderaadt  * Redistribution and use in source and binary forms, with or without
814c399efSderaadt  * modification, are permitted provided that the following conditions
914c399efSderaadt  * are met:
1014c399efSderaadt  * 1. Redistributions of source code must retain the above copyright
1114c399efSderaadt  *    notice, this list of conditions and the following disclaimer.
1214c399efSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1314c399efSderaadt  *    notice, this list of conditions and the following disclaimer in the
1414c399efSderaadt  *    documentation and/or other materials provided with the distribution.
15c6db6f0eSchuck  * 3. All advertising materials mentioning features or use of this software
16c6db6f0eSchuck  *    must display the following acknowledgement:
17c6db6f0eSchuck  *      This product includes software developed by Charles D. Cranor.
18c6db6f0eSchuck  * 4. The name of the author may not be used to endorse or promote products
19c6db6f0eSchuck  *    derived from this software without specific prior written permission.
2014c399efSderaadt  *
21c6db6f0eSchuck  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22c6db6f0eSchuck  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23c6db6f0eSchuck  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24c6db6f0eSchuck  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25c6db6f0eSchuck  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26c6db6f0eSchuck  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27c6db6f0eSchuck  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28c6db6f0eSchuck  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29c6db6f0eSchuck  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30c6db6f0eSchuck  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3114c399efSderaadt  */
3214c399efSderaadt 
33c6db6f0eSchuck /*
34c6db6f0eSchuck  * yplog.c: replacement yplog routines for
35c6db6f0eSchuck  * Mats O Jansson's ypserv program, as added by
36c6db6f0eSchuck  * Chuck Cranor.
37c6db6f0eSchuck  */
3814c399efSderaadt 
3914c399efSderaadt #include <stdio.h>
4014c399efSderaadt #include <unistd.h>
41c6db6f0eSchuck #include <sys/types.h>
42716486b5Smaja #include <sys/stat.h>
43c6db6f0eSchuck #include <stdarg.h>
44c6db6f0eSchuck #include "yplog.h"
45c6db6f0eSchuck 
46c6db6f0eSchuck static FILE	*logfp = NULL;		/* the log file */
47c6db6f0eSchuck 
48c6db6f0eSchuck /*
49c6db6f0eSchuck  * yplog(): like a printf, but to the log file.   does the flush
50c6db6f0eSchuck  * and data for you.
51c6db6f0eSchuck  */
5214c399efSderaadt void
yplog(const char * fmt,...)53c6db6f0eSchuck yplog(const char *fmt, ...)
5414c399efSderaadt {
55c6db6f0eSchuck 	va_list ap;
56c6db6f0eSchuck 
57c6db6f0eSchuck 	va_start(ap, fmt);
58c6db6f0eSchuck 	vyplog(fmt, ap);
59c6db6f0eSchuck 	va_end(ap);
60c6db6f0eSchuck }
61c6db6f0eSchuck 
62c6db6f0eSchuck /*
63c6db6f0eSchuck  * vyplog() support routine for yplog()
64c6db6f0eSchuck  */
65c6db6f0eSchuck void
vyplog(const char * fmt,va_list ap)66*33dd5606Sderaadt vyplog(const char *fmt, va_list ap)
67c6db6f0eSchuck {
6814c399efSderaadt 	time_t t;
6914c399efSderaadt 
70716486b5Smaja 	if (logfp == NULL)
71716486b5Smaja 		return;
7214c399efSderaadt 	(void)time(&t);
73c6db6f0eSchuck 	fprintf(logfp,"%.15s ", ctime(&t) + 4);
74c6db6f0eSchuck 	vfprintf(logfp, fmt, ap);
75c6db6f0eSchuck 	fprintf(logfp,"\n");
76c6db6f0eSchuck 	fflush(logfp);
7714c399efSderaadt }
78c6db6f0eSchuck 
79c6db6f0eSchuck /*
80c6db6f0eSchuck  * open log
81c6db6f0eSchuck  */
8214c399efSderaadt void
ypopenlog(void)83*33dd5606Sderaadt ypopenlog(void)
8414c399efSderaadt {
85716486b5Smaja 	static char logfn[] = "/var/yp/ypserv.log";
86716486b5Smaja 
87716486b5Smaja 	if (access(logfn, W_OK) == -1)
88716486b5Smaja 		return;
89c6db6f0eSchuck 	logfp = fopen("/var/yp/ypserv.log", "a");
90716486b5Smaja 	if (logfp == NULL)
91716486b5Smaja 		return;
92c6db6f0eSchuck 	yplog("yplog opened");
9314c399efSderaadt }
94c6db6f0eSchuck 
95c6db6f0eSchuck /*
96c6db6f0eSchuck  * close log
97c6db6f0eSchuck  */
9814c399efSderaadt void
ypcloselog(void)99*33dd5606Sderaadt ypcloselog(void)
10014c399efSderaadt {
101c6db6f0eSchuck 	if (logfp) {
102c6db6f0eSchuck 		yplog("yplog closed");
103c6db6f0eSchuck 		fclose(logfp);
104c6db6f0eSchuck 		logfp = NULL;
10514c399efSderaadt 	}
10614c399efSderaadt }
107