1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker * Copyright (c) 1993, 1994, 1995, 1996, 1998
3*8b26181fSAndroid Build Coastguard Worker * The Regents of the University of California. All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker *
5*8b26181fSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker * written permission.
17*8b26181fSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker */
21*8b26181fSAndroid Build Coastguard Worker
22*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
23*8b26181fSAndroid Build Coastguard Worker #include <config.h>
24*8b26181fSAndroid Build Coastguard Worker #endif
25*8b26181fSAndroid Build Coastguard Worker
26*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
27*8b26181fSAndroid Build Coastguard Worker #include <stdarg.h>
28*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
29*8b26181fSAndroid Build Coastguard Worker
30*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
31*8b26181fSAndroid Build Coastguard Worker #include <windows.h>
32*8b26181fSAndroid Build Coastguard Worker #else
33*8b26181fSAndroid Build Coastguard Worker #include <syslog.h>
34*8b26181fSAndroid Build Coastguard Worker #endif
35*8b26181fSAndroid Build Coastguard Worker
36*8b26181fSAndroid Build Coastguard Worker #include "portability.h"
37*8b26181fSAndroid Build Coastguard Worker
38*8b26181fSAndroid Build Coastguard Worker #include "log.h"
39*8b26181fSAndroid Build Coastguard Worker
40*8b26181fSAndroid Build Coastguard Worker static int log_to_systemlog;
41*8b26181fSAndroid Build Coastguard Worker static int log_debug_messages;
42*8b26181fSAndroid Build Coastguard Worker
43*8b26181fSAndroid Build Coastguard Worker static void rpcapd_vlog_stderr(log_priority,
44*8b26181fSAndroid Build Coastguard Worker PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
45*8b26181fSAndroid Build Coastguard Worker
rpcapd_vlog_stderr(log_priority priority,const char * message,va_list ap)46*8b26181fSAndroid Build Coastguard Worker static void rpcapd_vlog_stderr(log_priority priority, const char *message, va_list ap)
47*8b26181fSAndroid Build Coastguard Worker {
48*8b26181fSAndroid Build Coastguard Worker const char *tag;
49*8b26181fSAndroid Build Coastguard Worker
50*8b26181fSAndroid Build Coastguard Worker /*
51*8b26181fSAndroid Build Coastguard Worker * Squelch warnings from compilers that *don't* assume that
52*8b26181fSAndroid Build Coastguard Worker * priority always has a valid enum value and therefore don't
53*8b26181fSAndroid Build Coastguard Worker * assume that we'll always go through one of the case arms.
54*8b26181fSAndroid Build Coastguard Worker *
55*8b26181fSAndroid Build Coastguard Worker * If we have a default case, compilers that *do* assume that
56*8b26181fSAndroid Build Coastguard Worker * will then complain about the default case code being
57*8b26181fSAndroid Build Coastguard Worker * unreachable.
58*8b26181fSAndroid Build Coastguard Worker *
59*8b26181fSAndroid Build Coastguard Worker * Damned if you do, damned if you don't.
60*8b26181fSAndroid Build Coastguard Worker */
61*8b26181fSAndroid Build Coastguard Worker tag = "";
62*8b26181fSAndroid Build Coastguard Worker
63*8b26181fSAndroid Build Coastguard Worker switch (priority) {
64*8b26181fSAndroid Build Coastguard Worker
65*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_DEBUG:
66*8b26181fSAndroid Build Coastguard Worker tag = "DEBUG: ";
67*8b26181fSAndroid Build Coastguard Worker break;
68*8b26181fSAndroid Build Coastguard Worker
69*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_INFO:
70*8b26181fSAndroid Build Coastguard Worker tag = "";
71*8b26181fSAndroid Build Coastguard Worker break;
72*8b26181fSAndroid Build Coastguard Worker
73*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_WARNING:
74*8b26181fSAndroid Build Coastguard Worker tag = "warning: ";
75*8b26181fSAndroid Build Coastguard Worker break;
76*8b26181fSAndroid Build Coastguard Worker
77*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_ERROR:
78*8b26181fSAndroid Build Coastguard Worker tag = "error: ";
79*8b26181fSAndroid Build Coastguard Worker break;
80*8b26181fSAndroid Build Coastguard Worker }
81*8b26181fSAndroid Build Coastguard Worker
82*8b26181fSAndroid Build Coastguard Worker fprintf(stderr, "rpcapd: %s", tag);
83*8b26181fSAndroid Build Coastguard Worker vfprintf(stderr, message, ap);
84*8b26181fSAndroid Build Coastguard Worker putc('\n', stderr);
85*8b26181fSAndroid Build Coastguard Worker }
86*8b26181fSAndroid Build Coastguard Worker
87*8b26181fSAndroid Build Coastguard Worker static void rpcapd_vlog_systemlog(log_priority,
88*8b26181fSAndroid Build Coastguard Worker PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
89*8b26181fSAndroid Build Coastguard Worker
90*8b26181fSAndroid Build Coastguard Worker #ifdef _WIN32
91*8b26181fSAndroid Build Coastguard Worker #define MESSAGE_SUBKEY \
92*8b26181fSAndroid Build Coastguard Worker "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\rpcapd"
93*8b26181fSAndroid Build Coastguard Worker
rpcapd_vlog_systemlog(log_priority priority,const char * message,va_list ap)94*8b26181fSAndroid Build Coastguard Worker static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
95*8b26181fSAndroid Build Coastguard Worker va_list ap)
96*8b26181fSAndroid Build Coastguard Worker {
97*8b26181fSAndroid Build Coastguard Worker #if 0
98*8b26181fSAndroid Build Coastguard Worker static int initialized = 0;
99*8b26181fSAndroid Build Coastguard Worker HKEY hey_handle;
100*8b26181fSAndroid Build Coastguard Worker static HANDLE log_handle;
101*8b26181fSAndroid Build Coastguard Worker WORD eventlog_type;
102*8b26181fSAndroid Build Coastguard Worker DWORD event_id;
103*8b26181fSAndroid Build Coastguard Worker char msgbuf[1024];
104*8b26181fSAndroid Build Coastguard Worker char *strings[1];
105*8b26181fSAndroid Build Coastguard Worker
106*8b26181fSAndroid Build Coastguard Worker if (!initialized) {
107*8b26181fSAndroid Build Coastguard Worker /*
108*8b26181fSAndroid Build Coastguard Worker * Register our message stuff in the Registry.
109*8b26181fSAndroid Build Coastguard Worker *
110*8b26181fSAndroid Build Coastguard Worker * First, create the registry key for us. If the key
111*8b26181fSAndroid Build Coastguard Worker * already exists, this succeeds and returns a handle
112*8b26181fSAndroid Build Coastguard Worker * for it.
113*8b26181fSAndroid Build Coastguard Worker */
114*8b26181fSAndroid Build Coastguard Worker if (RegCreateKey(HKEY_LOCAL_MACHINE, MESSAGE_SUBKEY,
115*8b26181fSAndroid Build Coastguard Worker &key_handle) != ERROR_SUCCESS) {
116*8b26181fSAndroid Build Coastguard Worker /*
117*8b26181fSAndroid Build Coastguard Worker * Failed - give up and just log this message,
118*8b26181fSAndroid Build Coastguard Worker * and all subsequent messages, to the
119*8b26181fSAndroid Build Coastguard Worker * standard error.
120*8b26181fSAndroid Build Coastguard Worker */
121*8b26181fSAndroid Build Coastguard Worker log_to_systemlog = 0;
122*8b26181fSAndroid Build Coastguard Worker initialized = 1;
123*8b26181fSAndroid Build Coastguard Worker rpcapd_vlog_stderr(priority, message, ap);
124*8b26181fSAndroid Build Coastguard Worker return;
125*8b26181fSAndroid Build Coastguard Worker }
126*8b26181fSAndroid Build Coastguard Worker log_handle = RegisterEventSource(NULL, "rpcapd");
127*8b26181fSAndroid Build Coastguard Worker initialized = 1;
128*8b26181fSAndroid Build Coastguard Worker }
129*8b26181fSAndroid Build Coastguard Worker
130*8b26181fSAndroid Build Coastguard Worker switch (priority) {
131*8b26181fSAndroid Build Coastguard Worker
132*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_DEBUG:
133*8b26181fSAndroid Build Coastguard Worker //
134*8b26181fSAndroid Build Coastguard Worker // XXX - what *should* we do about debug messages?
135*8b26181fSAndroid Build Coastguard Worker //
136*8b26181fSAndroid Build Coastguard Worker eventlog_type = EVENTLOG_INFORMATION_TYPE;
137*8b26181fSAndroid Build Coastguard Worker event_id = RPCAPD_INFO_ID;
138*8b26181fSAndroid Build Coastguard Worker break;
139*8b26181fSAndroid Build Coastguard Worker
140*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_INFO:
141*8b26181fSAndroid Build Coastguard Worker eventlog_type = EVENTLOG_INFORMATION_TYPE;
142*8b26181fSAndroid Build Coastguard Worker event_id = RPCAPD_INFO_ID;
143*8b26181fSAndroid Build Coastguard Worker break;
144*8b26181fSAndroid Build Coastguard Worker
145*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_WARNING:
146*8b26181fSAndroid Build Coastguard Worker eventlog_type = EVENTLOG_WARNING_TYPE;
147*8b26181fSAndroid Build Coastguard Worker event_id = RPCAPD_WARNING_ID;
148*8b26181fSAndroid Build Coastguard Worker break;
149*8b26181fSAndroid Build Coastguard Worker
150*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_ERROR:
151*8b26181fSAndroid Build Coastguard Worker eventlog_type = EVENTLOG_ERROR_TYPE;
152*8b26181fSAndroid Build Coastguard Worker event_id = RPCAPD_ERROR_ID;
153*8b26181fSAndroid Build Coastguard Worker break;
154*8b26181fSAndroid Build Coastguard Worker
155*8b26181fSAndroid Build Coastguard Worker default:
156*8b26181fSAndroid Build Coastguard Worker /* Don't do this. */
157*8b26181fSAndroid Build Coastguard Worker return;
158*8b26181fSAndroid Build Coastguard Worker }
159*8b26181fSAndroid Build Coastguard Worker
160*8b26181fSAndroid Build Coastguard Worker vsprintf(msgbuf, message, ap);
161*8b26181fSAndroid Build Coastguard Worker
162*8b26181fSAndroid Build Coastguard Worker strings[0] = msgbuf;
163*8b26181fSAndroid Build Coastguard Worker /*
164*8b26181fSAndroid Build Coastguard Worker * If this fails, how are we going to report it?
165*8b26181fSAndroid Build Coastguard Worker */
166*8b26181fSAndroid Build Coastguard Worker (void) ReportEvent(log_handle, eventlog_type, 0, event_id, NULL, 1, 0,
167*8b26181fSAndroid Build Coastguard Worker strings, NULL);
168*8b26181fSAndroid Build Coastguard Worker #else
169*8b26181fSAndroid Build Coastguard Worker rpcapd_vlog_stderr(priority, message, ap);
170*8b26181fSAndroid Build Coastguard Worker #endif
171*8b26181fSAndroid Build Coastguard Worker }
172*8b26181fSAndroid Build Coastguard Worker #else
rpcapd_vlog_systemlog(log_priority priority,const char * message,va_list ap)173*8b26181fSAndroid Build Coastguard Worker static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
174*8b26181fSAndroid Build Coastguard Worker va_list ap)
175*8b26181fSAndroid Build Coastguard Worker {
176*8b26181fSAndroid Build Coastguard Worker static int initialized = 0;
177*8b26181fSAndroid Build Coastguard Worker int syslog_priority;
178*8b26181fSAndroid Build Coastguard Worker
179*8b26181fSAndroid Build Coastguard Worker if (!initialized) {
180*8b26181fSAndroid Build Coastguard Worker //
181*8b26181fSAndroid Build Coastguard Worker // Open the log.
182*8b26181fSAndroid Build Coastguard Worker //
183*8b26181fSAndroid Build Coastguard Worker openlog("rpcapd", LOG_PID, LOG_DAEMON);
184*8b26181fSAndroid Build Coastguard Worker initialized = 1;
185*8b26181fSAndroid Build Coastguard Worker }
186*8b26181fSAndroid Build Coastguard Worker
187*8b26181fSAndroid Build Coastguard Worker switch (priority) {
188*8b26181fSAndroid Build Coastguard Worker
189*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_DEBUG:
190*8b26181fSAndroid Build Coastguard Worker syslog_priority = LOG_DEBUG;
191*8b26181fSAndroid Build Coastguard Worker break;
192*8b26181fSAndroid Build Coastguard Worker
193*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_INFO:
194*8b26181fSAndroid Build Coastguard Worker syslog_priority = LOG_INFO;
195*8b26181fSAndroid Build Coastguard Worker break;
196*8b26181fSAndroid Build Coastguard Worker
197*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_WARNING:
198*8b26181fSAndroid Build Coastguard Worker syslog_priority = LOG_WARNING;
199*8b26181fSAndroid Build Coastguard Worker break;
200*8b26181fSAndroid Build Coastguard Worker
201*8b26181fSAndroid Build Coastguard Worker case LOGPRIO_ERROR:
202*8b26181fSAndroid Build Coastguard Worker syslog_priority = LOG_ERR;
203*8b26181fSAndroid Build Coastguard Worker break;
204*8b26181fSAndroid Build Coastguard Worker
205*8b26181fSAndroid Build Coastguard Worker default:
206*8b26181fSAndroid Build Coastguard Worker /* Don't do this. */
207*8b26181fSAndroid Build Coastguard Worker return;
208*8b26181fSAndroid Build Coastguard Worker }
209*8b26181fSAndroid Build Coastguard Worker
210*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_VSYSLOG
211*8b26181fSAndroid Build Coastguard Worker vsyslog(syslog_priority, message, ap);
212*8b26181fSAndroid Build Coastguard Worker #else
213*8b26181fSAndroid Build Coastguard Worker /*
214*8b26181fSAndroid Build Coastguard Worker * Thanks, IBM, for not providing vsyslog() in AIX!
215*8b26181fSAndroid Build Coastguard Worker *
216*8b26181fSAndroid Build Coastguard Worker * They also warn that the syslog functions shouldn't
217*8b26181fSAndroid Build Coastguard Worker * be used in multithreaded programs, but the only thing
218*8b26181fSAndroid Build Coastguard Worker * obvious that seems to make the syslog_r functions
219*8b26181fSAndroid Build Coastguard Worker * better is that they have an additional argument
220*8b26181fSAndroid Build Coastguard Worker * that points to the information that's static to
221*8b26181fSAndroid Build Coastguard Worker * the syslog code in non-thread-safe versions. Most
222*8b26181fSAndroid Build Coastguard Worker * of that data is set by openlog(); since we already
223*8b26181fSAndroid Build Coastguard Worker * do an openlog before doing logging, and don't
224*8b26181fSAndroid Build Coastguard Worker * change that data afterwards, I suspect that, in
225*8b26181fSAndroid Build Coastguard Worker * practice, the regular syslog routines are OK for
226*8b26181fSAndroid Build Coastguard Worker * us (especially given that we'd end up having one
227*8b26181fSAndroid Build Coastguard Worker * static struct syslog_data anyway, which means we'd
228*8b26181fSAndroid Build Coastguard Worker * just be like the non-thread-safe version).
229*8b26181fSAndroid Build Coastguard Worker */
230*8b26181fSAndroid Build Coastguard Worker char logbuf[1024+1];
231*8b26181fSAndroid Build Coastguard Worker
232*8b26181fSAndroid Build Coastguard Worker vsnprintf(logbuf, sizeof logbuf, message, ap);
233*8b26181fSAndroid Build Coastguard Worker syslog(syslog_priority, "%s", logbuf);
234*8b26181fSAndroid Build Coastguard Worker #endif
235*8b26181fSAndroid Build Coastguard Worker }
236*8b26181fSAndroid Build Coastguard Worker #endif
237*8b26181fSAndroid Build Coastguard Worker
rpcapd_log_set(int log_to_systemlog_arg,int log_debug_messages_arg)238*8b26181fSAndroid Build Coastguard Worker void rpcapd_log_set(int log_to_systemlog_arg, int log_debug_messages_arg)
239*8b26181fSAndroid Build Coastguard Worker {
240*8b26181fSAndroid Build Coastguard Worker log_debug_messages = log_debug_messages_arg;
241*8b26181fSAndroid Build Coastguard Worker log_to_systemlog = log_to_systemlog_arg;
242*8b26181fSAndroid Build Coastguard Worker }
243*8b26181fSAndroid Build Coastguard Worker
rpcapd_log(log_priority priority,const char * message,...)244*8b26181fSAndroid Build Coastguard Worker void rpcapd_log(log_priority priority, const char *message, ...)
245*8b26181fSAndroid Build Coastguard Worker {
246*8b26181fSAndroid Build Coastguard Worker va_list ap;
247*8b26181fSAndroid Build Coastguard Worker
248*8b26181fSAndroid Build Coastguard Worker if (priority != LOGPRIO_DEBUG || log_debug_messages) {
249*8b26181fSAndroid Build Coastguard Worker va_start(ap, message);
250*8b26181fSAndroid Build Coastguard Worker if (log_to_systemlog)
251*8b26181fSAndroid Build Coastguard Worker {
252*8b26181fSAndroid Build Coastguard Worker rpcapd_vlog_systemlog(priority, message, ap);
253*8b26181fSAndroid Build Coastguard Worker }
254*8b26181fSAndroid Build Coastguard Worker else
255*8b26181fSAndroid Build Coastguard Worker {
256*8b26181fSAndroid Build Coastguard Worker rpcapd_vlog_stderr(priority, message, ap);
257*8b26181fSAndroid Build Coastguard Worker }
258*8b26181fSAndroid Build Coastguard Worker va_end(ap);
259*8b26181fSAndroid Build Coastguard Worker }
260*8b26181fSAndroid Build Coastguard Worker }
261