test some more
[dragonfly.git] / usr.sbin / ppp / log.c
blob7d6433df70a580d575dd66d89b666bddaa6f3432
1 /*-
2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/ppp/log.c,v 1.44.2.5 2002/09/01 02:12:28 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/log.c,v 1.4 2008/05/19 10:19:49 corecode Exp $
30 #include <sys/types.h>
31 #include <sys/select.h>
33 #include <ctype.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <syslog.h>
38 #include <termios.h>
40 #include "defs.h"
41 #include "command.h"
42 #include "mbuf.h"
43 #include "log.h"
44 #include "descriptor.h"
45 #include "prompt.h"
47 static const char *const LogNames[] = {
48 "Async",
49 "CBCP",
50 "CCP",
51 "Chat",
52 "Command",
53 "Connect",
54 "Debug",
55 "DNS",
56 "Filter", /* Log discarded packets */
57 "HDLC",
58 "ID0",
59 "IPCP",
60 "IPV6CP",
61 "LCP",
62 "LQM",
63 "Phase",
64 "Physical",
65 "Sync",
66 "TCP/IP",
67 "Timer",
68 "Tun",
69 "Warning",
70 "Error",
71 "Alert"
74 #define MSK(n) (1<<((n)-1))
76 static u_long LogMask = MSK(LogPHASE);
77 static u_long LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
78 static int LogTunno = -1;
79 static struct prompt *promptlist; /* Where to log local stuff */
80 struct prompt *log_PromptContext;
81 int log_PromptListChanged;
83 struct prompt *
84 log_PromptList(void)
86 return promptlist;
89 void
90 log_RegisterPrompt(struct prompt *prompt)
92 prompt->next = promptlist;
93 promptlist = prompt;
94 prompt->active = 1;
95 log_DiscardAllLocal(&prompt->logmask);
98 void
99 log_ActivatePrompt(struct prompt *prompt)
101 prompt->active = 1;
102 LogMaskLocal |= prompt->logmask;
105 static void
106 LogSetMaskLocal(void)
108 struct prompt *p;
110 LogMaskLocal = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
111 for (p = promptlist; p; p = p->next)
112 LogMaskLocal |= p->logmask;
115 void
116 log_DeactivatePrompt(struct prompt *prompt)
118 if (prompt->active) {
119 prompt->active = 0;
120 LogSetMaskLocal();
124 void
125 log_UnRegisterPrompt(struct prompt *prompt)
127 if (prompt) {
128 struct prompt **p;
130 for (p = &promptlist; *p; p = &(*p)->next)
131 if (*p == prompt) {
132 *p = prompt->next;
133 prompt->next = NULL;
134 break;
136 LogSetMaskLocal();
137 log_PromptListChanged++;
141 void
142 log_DestroyPrompts(struct server *s)
144 struct prompt *p, *pn, *pl;
146 p = promptlist;
147 pl = NULL;
148 while (p) {
149 pn = p->next;
150 if (s && p->owner == s) {
151 if (pl)
152 pl->next = p->next;
153 else
154 promptlist = p->next;
155 p->next = NULL;
156 prompt_Destroy(p, 1);
157 } else
158 pl = p;
159 p = pn;
163 void
164 log_DisplayPrompts(void)
166 struct prompt *p;
168 for (p = promptlist; p; p = p->next)
169 prompt_Required(p);
172 void
173 log_WritePrompts(struct datalink *dl, const char *fmt,...)
175 va_list ap;
176 struct prompt *p;
178 va_start(ap, fmt);
179 for (p = promptlist; p; p = p->next)
180 if (prompt_IsTermMode(p, dl))
181 prompt_vPrintf(p, fmt, ap);
182 va_end(ap);
185 void
186 log_SetTtyCommandMode(struct datalink *dl)
188 struct prompt *p;
190 for (p = promptlist; p; p = p->next)
191 if (prompt_IsTermMode(p, dl))
192 prompt_TtyCommandMode(p);
195 static int
196 syslogLevel(int lev)
198 switch (lev) {
199 case LogLOG:
200 return LOG_INFO;
201 case LogDEBUG:
202 case LogTIMER:
203 return LOG_DEBUG;
204 case LogWARN:
205 return LOG_WARNING;
206 case LogERROR:
207 return LOG_ERR;
208 case LogALERT:
209 return LOG_ALERT;
211 return lev >= LogMIN && lev <= LogMAX ? LOG_INFO : 0;
214 const char *
215 log_Name(int id)
217 if (id == LogLOG)
218 return "LOG";
219 return id < LogMIN || id > LogMAX ? "Unknown" : LogNames[id - 1];
222 void
223 log_Keep(int id)
225 if (id >= LogMIN && id <= LogMAXCONF)
226 LogMask |= MSK(id);
229 void
230 log_KeepLocal(int id, u_long *mask)
232 if (id >= LogMIN && id <= LogMAXCONF) {
233 LogMaskLocal |= MSK(id);
234 *mask |= MSK(id);
238 void
239 log_Discard(int id)
241 if (id >= LogMIN && id <= LogMAXCONF)
242 LogMask &= ~MSK(id);
245 void
246 log_DiscardLocal(int id, u_long *mask)
248 if (id >= LogMIN && id <= LogMAXCONF) {
249 *mask &= ~MSK(id);
250 LogSetMaskLocal();
254 void
255 log_DiscardAll(void)
257 LogMask = 0;
260 void
261 log_DiscardAllLocal(u_long *mask)
263 *mask = MSK(LogERROR) | MSK(LogALERT) | MSK(LogWARN);
264 LogSetMaskLocal();
268 log_IsKept(int id)
270 if (id == LogLOG)
271 return LOG_KEPT_SYSLOG;
272 if (id < LogMIN || id > LogMAX)
273 return 0;
274 if (id > LogMAXCONF)
275 return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
277 return ((LogMaskLocal & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
278 ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
282 log_IsKeptLocal(int id, u_long mask)
284 if (id < LogMIN || id > LogMAX)
285 return 0;
286 if (id > LogMAXCONF)
287 return LOG_KEPT_LOCAL | LOG_KEPT_SYSLOG;
289 return ((mask & MSK(id)) ? LOG_KEPT_LOCAL : 0) |
290 ((LogMask & MSK(id)) ? LOG_KEPT_SYSLOG : 0);
293 void
294 log_Open(const char *Name)
296 openlog(Name, LOG_PID, LOG_DAEMON);
299 void
300 log_SetTun(int tunno)
302 LogTunno = tunno;
305 void
306 log_Close(void)
308 closelog();
309 LogTunno = -1;
312 void
313 log_Printf(int lev, const char *fmt,...)
315 va_list ap;
316 struct prompt *prompt;
318 if (log_IsKept(lev)) {
319 char nfmt[200];
321 va_start(ap, fmt);
322 if (promptlist && (log_IsKept(lev) & LOG_KEPT_LOCAL)) {
323 if ((log_IsKept(LogTUN) & LOG_KEPT_LOCAL) && LogTunno != -1)
324 snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
325 LogTunno, log_Name(lev), fmt);
326 else
327 snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
329 if (log_PromptContext && lev == LogWARN)
330 /* Warnings just go to the current prompt */
331 prompt_vPrintf(log_PromptContext, nfmt, ap);
332 else for (prompt = promptlist; prompt; prompt = prompt->next)
333 if (lev > LogMAXCONF || (prompt->logmask & MSK(lev)))
334 prompt_vPrintf(prompt, nfmt, ap);
336 va_end(ap);
338 va_start(ap, fmt);
339 if ((log_IsKept(lev) & LOG_KEPT_SYSLOG) &&
340 (lev != LogWARN || !log_PromptContext)) {
341 if ((log_IsKept(LogTUN) & LOG_KEPT_SYSLOG) && LogTunno != -1)
342 snprintf(nfmt, sizeof nfmt, "%s%d: %s: %s", TUN_NAME,
343 LogTunno, log_Name(lev), fmt);
344 else
345 snprintf(nfmt, sizeof nfmt, "%s: %s", log_Name(lev), fmt);
346 vsyslog(syslogLevel(lev), nfmt, ap);
348 va_end(ap);
352 void
353 log_DumpBp(int lev, const char *hdr, const struct mbuf *bp)
355 if (log_IsKept(lev)) {
356 char buf[68];
357 char *b, *c;
358 const u_char *ptr;
359 int f;
361 if (hdr && *hdr)
362 log_Printf(lev, "%s\n", hdr);
364 b = buf;
365 c = b + 50;
366 do {
367 f = bp->m_len;
368 ptr = CONST_MBUF_CTOP(bp);
369 while (f--) {
370 sprintf(b, " %02x", (int) *ptr);
371 *c++ = isprint(*ptr) ? *ptr : '.';
372 ptr++;
373 b += 3;
374 if (b == buf + 48) {
375 memset(b, ' ', 2);
376 *c = '\0';
377 log_Printf(lev, "%s\n", buf);
378 b = buf;
379 c = b + 50;
382 } while ((bp = bp->m_next) != NULL);
384 if (b > buf) {
385 memset(b, ' ', 50 - (b - buf));
386 *c = '\0';
387 log_Printf(lev, "%s\n", buf);
392 void
393 log_DumpBuff(int lev, const char *hdr, const u_char *ptr, int n)
395 if (log_IsKept(lev)) {
396 char buf[68];
397 char *b, *c;
399 if (hdr && *hdr)
400 log_Printf(lev, "%s\n", hdr);
401 while (n > 0) {
402 b = buf;
403 c = b + 50;
404 for (b = buf; b != buf + 48 && n--; b += 3, ptr++) {
405 sprintf(b, " %02x", (int) *ptr);
406 *c++ = isprint(*ptr) ? *ptr : '.';
408 memset(b, ' ', 50 - (b - buf));
409 *c = '\0';
410 log_Printf(lev, "%s\n", buf);
416 log_ShowLevel(struct cmdargs const *arg)
418 int i;
420 prompt_Printf(arg->prompt, "Log: ");
421 for (i = LogMIN; i <= LogMAX; i++)
422 if (log_IsKept(i) & LOG_KEPT_SYSLOG)
423 prompt_Printf(arg->prompt, " %s", log_Name(i));
425 prompt_Printf(arg->prompt, "\nLocal:");
426 for (i = LogMIN; i <= LogMAX; i++)
427 if (log_IsKeptLocal(i, arg->prompt->logmask) & LOG_KEPT_LOCAL)
428 prompt_Printf(arg->prompt, " %s", log_Name(i));
430 prompt_Printf(arg->prompt, "\n");
432 return 0;
436 log_SetLevel(struct cmdargs const *arg)
438 int i, res, argc, local;
439 char const *const *argv, *argp;
441 argc = arg->argc - arg->argn;
442 argv = arg->argv + arg->argn;
443 res = 0;
445 if (argc == 0 || strcasecmp(argv[0], "local"))
446 local = 0;
447 else {
448 if (arg->prompt == NULL) {
449 log_Printf(LogWARN, "set log local: Only available on the"
450 " command line\n");
451 return 1;
453 argc--;
454 argv++;
455 local = 1;
458 if (argc == 0 || (argv[0][0] != '+' && argv[0][0] != '-')) {
459 if (local)
460 log_DiscardAllLocal(&arg->prompt->logmask);
461 else
462 log_DiscardAll();
465 while (argc--) {
466 argp = **argv == '+' || **argv == '-' ? *argv + 1 : *argv;
467 /* Special case 'all' */
468 if (strcasecmp(argp, "all") == 0) {
469 if (**argv == '-') {
470 if (local)
471 for (i = LogMIN; i <= LogMAX; i++)
472 log_DiscardLocal(i, &arg->prompt->logmask);
473 else
474 for (i = LogMIN; i <= LogMAX; i++)
475 log_Discard(i);
476 } else if (local)
477 for (i = LogMIN; i <= LogMAX; i++)
478 log_KeepLocal(i, &arg->prompt->logmask);
479 else
480 for (i = LogMIN; i <= LogMAX; i++)
481 log_Keep(i);
482 argv++;
483 continue;
485 for (i = LogMIN; i <= LogMAX; i++)
486 if (strcasecmp(argp, log_Name(i)) == 0) {
487 if (**argv == '-') {
488 if (local)
489 log_DiscardLocal(i, &arg->prompt->logmask);
490 else
491 log_Discard(i);
492 } else if (local)
493 log_KeepLocal(i, &arg->prompt->logmask);
494 else
495 log_Keep(i);
496 break;
498 if (i > LogMAX) {
499 log_Printf(LogWARN, "%s: Invalid log value\n", argp);
500 res = -1;
502 argv++;
504 return res;
508 log_ShowWho(struct cmdargs const *arg)
510 struct prompt *p;
512 for (p = promptlist; p; p = p->next) {
513 prompt_Printf(arg->prompt, "%s (%s)", p->src.type, p->src.from);
514 if (p == arg->prompt)
515 prompt_Printf(arg->prompt, " *");
516 if (!p->active)
517 prompt_Printf(arg->prompt, " ^Z");
518 prompt_Printf(arg->prompt, "\n");
521 return 0;