xref: /openbsd-src/usr.sbin/ypserv/common/yplog.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: yplog.c,v 1.5 1997/08/09 22:44:04 maja 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 #ifdef __STDC__
44 #include <stdarg.h>
45 #else
46 #include <varargs.h>
47 #endif
48 #include "yplog.h"
49 
50 static FILE	*logfp = NULL;		/* the log file */
51 
52 /*
53  * yplog(): like a printf, but to the log file.   does the flush
54  * and data for you.
55  */
56 
57 void
58 #ifdef __STDC__
59 yplog(const char *fmt, ...)
60 #else
61 yplog(fmt, va_alist)
62 	char *fmt;
63 	va_dcl
64 #endif
65 {
66 	va_list ap;
67 
68 #ifdef __STDC__
69 	va_start(ap, fmt);
70 #else
71 	va_start(ap);
72 #endif
73 	vyplog(fmt, ap);
74 	va_end(ap);
75 }
76 
77 /*
78  * vyplog() support routine for yplog()
79  */
80 
81 void
82 vyplog(fmt, ap)
83 	register const char *fmt;
84 	va_list ap;
85 {
86         time_t t;
87 
88 	if (logfp == NULL)
89 		return;
90 	(void)time(&t);
91 	fprintf(logfp,"%.15s ", ctime(&t) + 4);
92 	vfprintf(logfp, fmt, ap);
93 	fprintf(logfp,"\n");
94 	fflush(logfp);
95 }
96 
97 /*
98  * open log
99  */
100 
101 void
102 ypopenlog()
103 {
104 	static char logfn[] = "/var/yp/ypserv.log";
105 
106 	if (access(logfn, W_OK) == -1)
107 		return;
108 	logfp = fopen("/var/yp/ypserv.log", "a");
109 	if (logfp == NULL)
110 		return;
111 	yplog("yplog opened");
112 }
113 
114 /*
115  * close log
116  */
117 
118 void
119 ypcloselog()
120 {
121 	if (logfp) {
122 		yplog("yplog closed");
123 		fclose(logfp);
124 		logfp = NULL;
125 	}
126 }
127