Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / atm / scspd / scsp_log.c
1 /*
2  *
3  * ===================================
4  * HARP  |  Host ATM Research Platform
5  * ===================================
6  *
7  *
8  * This Host ATM Research Platform ("HARP") file (the "Software") is
9  * made available by Network Computing Services, Inc. ("NetworkCS")
10  * "AS IS".  NetworkCS does not provide maintenance, improvements or
11  * support of any kind.
12  *
13  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17  * In no event shall NetworkCS be responsible for any damages, including
18  * but not limited to consequential damages, arising from or relating to
19  * any use of the Software or related support.
20  *
21  * Copyright 1994-1998 Network Computing Services, Inc.
22  *
23  * Copies of this Software may be made, however, the above copyright
24  * notice must be reproduced on all copies.
25  *
26  *      @(#) $FreeBSD: src/usr.sbin/atm/scspd/scsp_log.c,v 1.3 1999/08/28 01:15:33 peter Exp $
27  *      @(#) $DragonFly: src/usr.sbin/atm/scspd/scsp_log.c,v 1.2 2003/06/17 04:29:52 dillon Exp $
28  */
29
30
31 /*
32  * Server Cache Synchronization Protocol (SCSP) Support
33  * ----------------------------------------------------
34  *
35  * SCSP logging routines
36  *
37  */
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <netatm/port.h> 
45 #include <netatm/queue.h> 
46 #include <netatm/atm.h>
47 #include <netatm/atm_if.h>
48 #include <netatm/atm_sap.h>
49 #include <netatm/atm_sys.h>
50 #include <netatm/atm_ioctl.h>
51   
52 #include <errno.h>
53 #include <libatm.h>
54 #if __STDC__
55 #include <stdarg.h>
56 #else
57 #include <varargs.h>
58 #endif
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <syslog.h>
63 #include <unistd.h>
64
65 #include "scsp_msg.h"
66 #include "scsp_if.h"
67 #include "scsp_var.h"
68
69 /*
70  * Global variables
71  */
72 FILE    *scsp_trace_file = (FILE *)0;
73
74
75 /*
76  * Write a message to SCSP's log
77  *
78  * Arguments:
79  *      level   pointer to an SCSP cache key structure
80  *      fmt     printf-style format string
81  *      ...     parameters for printf-style use according to fmt
82  *
83  * Returns:
84  *      none
85  *
86  */
87 void
88 #if __STDC__
89 scsp_log(const int level, const char *fmt, ...)
90 #else
91 scsp_log(level, fmt, va_alist)
92         int     level;
93         char    *fmt;
94         va_dcl
95 #endif
96 {
97         va_list ap;
98
99 #if __STDC__
100         va_start(ap, fmt);
101 #else
102         va_start(ap);
103 #endif
104
105         /*
106          * In debug mode, just write to stdout
107          */
108         if (scsp_debug_mode) {
109                 vprintf(fmt, ap);
110                 printf("\n");
111                 return;
112         }
113
114         /*
115          * Write to syslog if it's active or if no log file is set up
116          */
117         if (scsp_log_syslog || !scsp_log_file) {
118                 vsyslog(level, fmt, ap);
119         }
120
121         /*
122          * Write to the log file if there's one set up
123          */
124         if (scsp_log_file) {
125                 vfprintf(scsp_log_file, fmt, ap);
126                 fprintf(scsp_log_file, "\n");
127         }
128
129         va_end(ap);
130 }
131
132
133 /*
134  * Open SCSP's trace file
135  *
136  * Arguments:
137  *      none
138  *
139  * Returns:
140  *      none
141  *
142  */
143 void
144 scsp_open_trace()
145 {
146         char    fname[64];
147
148         /*
149          * Build a file name
150          */
151         UM_ZERO(fname, sizeof(fname));
152         sprintf(fname, "/tmp/scspd.%d.trace", getpid());
153
154         /*
155          * Open the trace file.  If the open fails, log an error, but
156          * keep going.  The trace routine will notice that the file
157          * isn't open and won't try to write to it.
158          */
159         scsp_trace_file = fopen(fname, "w");
160         if (scsp_trace_file == (FILE *)0) {
161                 scsp_log(LOG_ERR, "Can't open trace file");
162         }
163 }
164
165
166 /*
167  * Write a message to SCSP's trace file
168  *
169  * Arguments:
170  *      fmt     printf-style format string
171  *      ...     parameters for printf-style use according to fmt
172  *
173  * Returns:
174  *      none
175  *
176  */
177 void
178 #if __STDC__
179 scsp_trace(const char *fmt, ...)
180 #else
181 scsp_trace(fmt, va_alist)
182         char    *fmt;
183         va_dcl
184 #endif
185 {
186         va_list ap;
187
188 #if __STDC__
189         va_start(ap, fmt);
190 #else
191         va_start(ap);
192 #endif
193
194         /*
195          * Write the message to the trace file, if it's open
196          */
197         if (scsp_trace_file) {
198                 vfprintf(scsp_trace_file, fmt, ap);
199         }
200
201         va_end(ap);
202 }
203
204
205 /*
206  * Write an SCSP message to SCSP's trace file
207  *
208  * Arguments:
209  *      dcsp    pointer to DCS block for the message
210  *      msg     pointer to the message
211  *      dir     a direction indicator--0 for sending, 1 for receiving
212  *
213  * Returns:
214  *      none
215  *
216  */
217 void
218 scsp_trace_msg(dcsp, msg, dir)
219         Scsp_dcs        *dcsp;
220         Scsp_msg        *msg;
221         int             dir;
222 {
223         struct in_addr  addr;
224
225         /*
226          * Copy the remote IP address into a struct in_addr
227          */
228         UM_COPY(dcsp->sd_dcsid.id, &addr.s_addr,
229                         sizeof(struct in_addr));
230         
231         /*
232          * Write the message to the trace file, if it's open
233          */
234         if (scsp_trace_file) {
235                 scsp_trace("SCSP message at 0x%x %s %s\n",
236                                 (u_long)msg,
237                                 (dir ? "received from" : "sent to"),
238                                 format_ip_addr(&addr));
239                 print_scsp_msg(scsp_trace_file, msg);
240         }
241 }
242
243
244 /*
245  * Log a memory error and exit
246  *
247  * Arguments:
248  *      cp      message to log
249  *
250  * Returns:
251  *      exits, does not return
252  *
253  */
254 void
255 scsp_mem_err(cp)
256         char    *cp;
257 {
258         scsp_log(LOG_CRIT, "out of memory: %s", cp);
259         exit(2);
260 }