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