The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 6b6bbfcaf1d57e366e3596b87f17feb04e469d34 $
19 *
20 * @brief Logging functions used by the server core.
21 * @file src/lib/server/log.c
22 *
23 * @copyright 2000,2006 The FreeRADIUS server project
24 * @copyright 2000 Miquel van Smoorenburg (miquels@cistron.nl)
25 * @copyright 2000 Alan DeKok (aland@freeradius.org)
26 * @copyright 2001 Chad Miller (cmiller@surfsouth.com)
27 */
28RCSID("$Id: 6b6bbfcaf1d57e366e3596b87f17feb04e469d34 $")
29
30#include <freeradius-devel/util/debug.h>
31#include <freeradius-devel/util/dict.h>
32#include <freeradius-devel/util/log.h>
33#include <freeradius-devel/util/syserror.h>
34#include <freeradius-devel/util/file.h>
35
36#include <freeradius-devel/server/log.h>
37#include <freeradius-devel/server/pair.h>
38#include <freeradius-devel/server/util.h>
39
40#include <freeradius-devel/unlang/xlat.h>
41
42#ifdef HAVE_SYS_STAT_H
43# include <sys/stat.h>
44#endif
45
46#include <fcntl.h>
47
48#ifdef HAVE_SYSLOG_H
49# include <syslog.h>
50#endif
51
52#include <sys/file.h>
53#include <pthread.h>
54
57
58/** Syslog facility table
59 *
60 * Maps syslog facility keywords, to the syslog facility macros defined
61 * in the system's syslog.h.
62 *
63 * @note Not all facilities are supported by every operating system.
64 * If a facility is unavailable it will not appear in the table.
65 */
67#ifdef LOG_AUTH
68 { L("auth"), LOG_AUTH },
69#endif
70
71#ifdef LOG_AUTHPRIV
72 { L("authpriv"), LOG_AUTHPRIV },
73#endif
74
75#ifdef LOG_CRON
76 { L("cron"), LOG_CRON },
77#endif
78
79#ifdef LOG_DAEMON
80 { L("daemon"), LOG_DAEMON },
81#endif
82
83#ifdef LOG_FTP
84 { L("ftp"), LOG_FTP },
85#endif
86
87#ifdef LOG_KERN
88 { L("kern"), LOG_KERN },
89#endif
90
91#ifdef LOG_LOCAL0
92 { L("local0"), LOG_LOCAL0 },
93#endif
94
95#ifdef LOG_LOCAL1
96 { L("local1"), LOG_LOCAL1 },
97#endif
98
99#ifdef LOG_LOCAL2
100 { L("local2"), LOG_LOCAL2 },
101#endif
102
103#ifdef LOG_LOCAL3
104 { L("local3"), LOG_LOCAL3 },
105#endif
106
107#ifdef LOG_LOCAL4
108 { L("local4"), LOG_LOCAL4 },
109#endif
110
111#ifdef LOG_LOCAL5
112 { L("local5"), LOG_LOCAL5 },
113#endif
114
115#ifdef LOG_LOCAL6
116 { L("local6"), LOG_LOCAL6 },
117#endif
118
119#ifdef LOG_LOCAL7
120 { L("local7"), LOG_LOCAL7 },
121#endif
122
123#ifdef LOG_LPR
124 { L("lpr"), LOG_LPR },
125#endif
126
127#ifdef LOG_MAIL
128 { L("mail"), LOG_MAIL },
129#endif
130
131#ifdef LOG_NEWS
132 { L("news"), LOG_NEWS },
133#endif
134
135#ifdef LOG_USER
136 { L("user"), LOG_USER },
137#endif
138
139#ifdef LOG_UUCP
140 { L("uucp"), LOG_UUCP }
141#endif
142};
144
145/** Syslog severity table
146 *
147 * Maps syslog severity keywords, to the syslog severity macros defined
148 * in the system's syslog.h file.
149 *
150 */
152#ifdef LOG_ALERT
153 { L("alert"), LOG_ALERT },
154#endif
155
156#ifdef LOG_CRIT
157 { L("critical"), LOG_CRIT },
158#endif
159
160#ifdef LOG_DEBUG
161 { L("debug"), LOG_DEBUG },
162#endif
163
164#ifdef LOG_EMERG
165 { L("emergency"), LOG_EMERG },
166#endif
167
168#ifdef LOG_ERR
169 { L("error"), LOG_ERR },
170#endif
171
172#ifdef LOG_INFO
173 { L("info"), LOG_INFO },
174#endif
175
176#ifdef LOG_NOTICE
177 { L("notice"), LOG_NOTICE },
178#endif
179
180#ifdef LOG_WARNING
181 { L("warning"), LOG_WARNING },
182#endif
183};
185
187 { L("file"), L_DST_FILES },
188 { L("files"), L_DST_FILES },
189 { L("null"), L_DST_NULL },
190 { L("stderr"), L_DST_STDERR },
191 { L("stdout"), L_DST_STDOUT },
192 { L("syslog"), L_DST_SYSLOG },
193};
195
196static char const spaces[] = " ";
197
199
202 { .out = &dict_freeradius, .proto = "freeradius" },
204};
205
207
210 { .out = &attr_module_failure_message, .name = "Module-Failure-Message", .type = FR_TYPE_STRING, .dict = &dict_freeradius },
212};
213
214typedef struct {
215 char const *name; //!< name of this logging destination
216 fr_log_t *log; //!< pointer to the log structure
217 CONF_SECTION *cs; //!< where this log configuration came from
218
219 fr_rb_node_t name_node; //!< tree by name
220 fr_rb_node_t filename_node; //!< tree by name
222
223typedef struct {
224 char const *name; //!< name of this logging source
225 uint32_t id; //!< LOG_ID of this source
226 fr_log_t *original; //!< the original fr_log_t
227 fr_log_t **log; //!< where the logs should go
228
229 fr_rb_node_t name_node; //!< tree by name only
230 fr_rb_node_t id_node; //!< tree by ID
232
233static fr_rb_tree_t *dst_tree = NULL;
235static fr_rb_tree_t *src_tree = NULL;
236
237/** Send a server log message to its destination without evaluating its debug level
238 *
239 * @param[in] log destination.
240 * @param[in] type of log message.
241 * @param[in] file src file the log message was generated in.
242 * @param[in] line number the log message was generated on.
243 * @param[in] fmt with printf style substitution tokens.
244 * @param[in] ... Substitution arguments.
245 */
246
247static CC_HINT(format (printf, 5, 6))
249 char const *file, int line,
250 char const *fmt, ...)
251{
252 va_list ap;
253
254 va_start(ap, fmt);
255 fr_vlog(log, type, file, line, fmt, ap);
256 va_end(ap);
257}
258
259/** Whether a request specific debug message should be logged
260 *
261 * @param lvl of debugging this message should be logged at.
262 * @param request The current request.
263 * @return
264 * - true if message should be logged.
265 * - false if message shouldn't be logged.
266 */
267inline bool log_rdebug_enabled(fr_log_lvl_t lvl, request_t const *request)
268{
269 if (!request->log.dst) return false;
270
271 return (request->log.lvl >= lvl);
272}
273
274/** Cleanup the memory pool used by vlog_request
275 *
276 */
277static int _fr_vlog_request_pool_free(void *arg)
278{
279 return talloc_free(arg);
280}
281
282/** Send a log message to its destination, possibly including fields from the request
283 *
284 * @param[in] type of log message, #L_ERR, #L_WARN, #L_INFO, #L_DBG.
285 * @param[in] lvl Minimum required server or request level to output this message.
286 * @param[in] request The current request.
287 * @param[in] file src file the log message was generated in.
288 * @param[in] line number the log message was generated on.
289 * @param[in] fmt with printf style substitution tokens.
290 * @param[in] ap Substitution arguments.
291 * @param[in] uctx The #fr_log_t specifying the destination for log messages.
292 */
294 char const *file, int line,
295 char const *fmt, va_list ap, void *uctx)
296{
297 char const *filename;
298 FILE *fp = NULL;
299
300 char *p;
301 char const *extra = "";
302 uint8_t unlang_indent, module_indent;
303 va_list aq;
304
305 char const *fmt_location = "";
306 char const *fmt_prefix = "";
307 char const *fmt_module = "";
308 char const *fmt_exp;
309
310 fr_log_t *log_dst = uctx;
311 TALLOC_CTX *pool;
312
313 /*
314 * No output means no output.
315 */
316 if (!log_dst) return;
317 if (!log_rdebug_enabled(lvl, request)) return;
318
319 /*
320 * Allocate a thread local, 4k pool so we don't
321 * need to keep allocating memory on the heap.
322 */
324 if (!pool) {
325 pool = talloc_pool(NULL, 4096);
326 if (!pool) {
327 fr_perror("Failed allocating memory for vlog_request_pool");
328 return;
329 }
331 }
332
333 filename = log_dst->file;
334
335 /*
336 * Debug messages get treated specially.
337 */
338 if ((type & L_DBG) != 0) {
339 /*
340 * If we're debugging to a file, then use that.
341 *
342 * @todo: have fr_vlog() take a fr_log_t*, so
343 * that we can cache the opened descriptor, and
344 * we don't need to re-open it on every log
345 * message.
346 */
347 switch (log_dst->dst) {
348 case L_DST_FILES:
349 fp = fopen(log_dst->file, "a");
350 if (!fp) goto finish;
351 break;
352
353#if defined(HAVE_FOPENCOOKIE) || defined (HAVE_FUNOPEN)
354 case L_DST_FUNC:
355 {
356# ifdef HAVE_FOPENCOOKIE
358
359 /*
360 * These must be set separately as they have different prototypes.
361 */
362 io.read = NULL;
363 io.seek = NULL;
364 io.close = NULL;
365 io.write = log_dst->cookie_write;
366
367 fp = fopencookie(log_dst->cookie, "w", io);
368# else
369 fp = funopen(log_dst->cookie, NULL, log_dst->cookie_write, NULL, NULL);
370
371# endif
372 if (!fp) goto finish;
373 }
374 break;
375#endif
376 default:
377 break;
378 }
379 goto print_fmt;
380 }
381
382 if (filename) {
383 char *exp;
384 log_dst_t *dst;
385
386 dst = request->log.dst;
387
388 /*
389 * Prevent infinitely recursive calls if
390 * xlat_aeval attempts to write to the request log.
391 */
392 request->log.dst = NULL;
393
394 /*
395 * This is SLOW! Doing it for every log message
396 * in every request is NOT recommended!
397 */
398 if (xlat_aeval(request, &exp, request, filename, rad_filename_escape, NULL) < 0) return;
399
400 /*
401 * Restore the original logging function
402 */
403 request->log.dst = dst;
404
405 /*
406 * Ensure the directory structure exists, for
407 * where we're going to write the log file.
408 */
409 p = strrchr(exp, FR_DIR_SEP);
410 if (p) {
411 *p = '\0';
412 if (fr_mkdir(NULL, exp, -1, S_IRWXU, NULL, NULL) < 0) {
413 ERROR("Failed creating %s: %s", exp, fr_syserror(errno));
414 talloc_free(exp);
415 return;
416 }
417 *p = FR_DIR_SEP;
418 }
419
420 fp = fopen(exp, "a");
421 talloc_free(exp);
422 }
423
424print_fmt:
425 /*
426 * Request prefix i.e.
427 *
428 * (0) <fmt>
429 */
430 if (request->name) {
431 if ((request->seq_start == 0) || (request->number == request->seq_start)) {
432 fmt_prefix = talloc_typed_asprintf(pool, "(%s) ", request->name);
433 } else {
434 fmt_prefix = talloc_typed_asprintf(pool, "(%s,%" PRIu64 ") ",
435 request->name, request->seq_start);
436 }
437 }
438
439 /*
440 * Make sure the indent isn't set to something crazy
441 */
442 unlang_indent = request->log.indent.unlang > sizeof(spaces) - 1 ?
443 sizeof(spaces) - 1 :
444 request->log.indent.unlang;
445
446 module_indent = request->log.indent.module > sizeof(spaces) - 1 ?
447 sizeof(spaces) - 1 :
448 request->log.indent.module;
449
450 /*
451 * Module name and indentation i.e.
452 *
453 * test - <fmt>
454 */
455 if (request->module) {
456 fmt_module = talloc_typed_asprintf(pool, "%s - %.*s", request->module, module_indent, spaces);
457 }
458
459 /*
460 * If we don't copy the original ap we get a segfault from vasprintf. This is apparently
461 * due to ap sometimes being implemented with a stack offset which is invalidated if
462 * ap is passed into another function. See here:
463 * http://julipedia.meroh.net/2011/09/using-vacopy-to-safely-pass-ap.html
464 *
465 * I don't buy that explanation, but doing a va_copy here does prevent SEGVs seen when
466 * running unit tests which generate errors under CI.
467 */
468 va_copy(aq, ap);
469 if (!log_dst->suppress_secrets) {
470 fmt_exp = fr_vasprintf(pool, fmt, aq);
471 } else {
472 fmt_exp = fr_vasprintf_secure(pool, fmt, aq);
473 }
474 va_end(aq);
475
476 /*
477 * Logging to a file descriptor
478 */
479 if (fp) {
480 char time_buff[64]; /* The current timestamp */
481
482 time_t timeval;
483 timeval = time(NULL);
484
485#if 0
486 fmt_location = talloc_typed_asprintf(pool, "%s[%i]: ", file, line);
487#endif
488
489#ifdef HAVE_GMTIME_R
490 if (log_dates_utc) {
491 struct tm utc;
492 gmtime_r(&timeval, &utc);
493 ASCTIME_R(&utc, time_buff, sizeof(time_buff));
494 } else
495#endif
496 {
497 CTIME_R(&timeval, time_buff, sizeof(time_buff));
498 }
499
500 /*
501 * Strip trailing new lines
502 */
503 p = strrchr(time_buff, '\n');
504 if (p) p[0] = '\0';
505
506 fprintf(fp,
507 "%s" /* location */
508 "%s" /* prefix */
509 "%s : " /* time */
510 "%s" /* facility */
511 "%.*s" /* indent */
512 "%s" /* module */
513 "%s" /* message */
514 "\n",
515 fmt_location,
516 fmt_prefix,
517 time_buff,
519 unlang_indent, spaces,
520 fmt_module,
521 fmt_exp);
522 fclose(fp);
523 goto finish;
524 }
525
526 /*
527 * Logging everywhere else
528 */
529 if (!DEBUG_ENABLED3) switch (type) {
530 case L_DBG_WARN:
531 extra = "WARNING: ";
533 break;
534
535 case L_DBG_ERR:
536 extra = "ERROR: ";
538 break;
539 default:
540 break;
541 }
542
544 "%s" /* prefix */
545 "%.*s" /* indent */
546 "%s" /* module */
547 "%s" /* extra */
548 "%s", /* message */
549 fmt_prefix,
550 unlang_indent, spaces,
551 fmt_module,
552 extra,
553 fmt_exp);
554
555finish:
556 talloc_free_children(pool);
557}
558
559/** Add a module failure message fr_pair_t to the request
560 *
561 * @param[in] request The current request.
562 * @param[in] fmt with printf style substitution tokens.
563 * @param[in] ap Substitution arguments.
564 */
565static void vlog_module_failure_msg(request_t *request, char const *fmt, va_list ap)
566{
567 char *p;
568 fr_pair_t *vp;
569 va_list aq;
570
571 if (!fmt || !request || !request->packet) return;
572
574
576
577 /*
578 * If we don't copy the original ap we get a segfault from vasprintf. This is apparently
579 * due to ap sometimes being implemented with a stack offset which is invalidated if
580 * ap is passed into another function. See here:
581 * http://julipedia.meroh.net/2011/09/using-vacopy-to-safely-pass-ap.html
582 *
583 * I don't buy that explanation, but doing a va_copy here does prevent SEGVs seen when
584 * running unit tests which generate errors under CI.
585 */
586 va_copy(aq, ap);
587 p = fr_vasprintf(vp, fmt, aq);
588 va_end(aq);
589
590 if (request->module && (request->module[0] != '\0')) {
591 fr_pair_value_aprintf(vp, "%s: %s", request->module, p);
592 talloc_free(p);
593 } else {
595 }
596}
597
598/** Marshal variadic log arguments into a va_list and pass to normal logging functions
599 *
600 * @see log_request_error for more details.
601 *
602 * @param[in] type the log category.
603 * @param[in] lvl of debugging this message should be logged at.
604 * @param[in] request The current request.
605 * @param[in] file src file the log message was generated in.
606 * @param[in] line number the log message was generated on.
607 * @param[in] fmt with printf style substitution tokens.
608 * @param[in] ... Substitution arguments.
609 */
611 char const *file, int line, char const *fmt, ...)
612{
613 va_list ap;
614
615 if (!request->log.dst) return;
616
617 va_start(ap, fmt);
618 log_dst_foreach(request->log.dst, dst) {
619 if ((lvl > request->log.lvl) && (lvl > dst->lvl)) continue;
620
621 dst->func(type, lvl, request, file, line, fmt, ap, dst->uctx);
622 }
623 va_end(ap);
624}
625
626/** Marshal variadic log arguments into a va_list and pass to error logging functions
627 *
628 * This could all be done in a macro, but it turns out some implementations of the
629 * variadic macros do not work at all well if the va_list being written to is further
630 * up the stack (which is required as you still need a function to convert the ellipsis
631 * into a va_list).
632 *
633 * So, we use this small wrapper function instead, which will hopefully guarantee
634 * consistent behaviour.
635 *
636 * @param[in] type the log category.
637 * @param[in] lvl of debugging this message should be logged at.
638 * @param[in] request The current request.
639 * @param[in] file src file the log message was generated in.
640 * @param[in] line number the log message was generated on.
641 * @param[in] fmt with printf style substitution tokens.
642 * @param[in] ... Substitution arguments.
643 */
645 char const *file, int line, char const *fmt, ...)
646{
647 va_list ap;
648
649 if (!request->log.dst) return;
650
651 va_start(ap, fmt);
652 log_dst_foreach(request->log.dst, dst) {
653 va_list copy;
654 va_copy(copy, ap);
655
656 dst->func(type, lvl, request, file, line, fmt, copy, dst->uctx);
657
658 va_end(copy);
659 }
660
661 if ((type == L_ERR) || (type == L_DBG_ERR) || (type == L_DBG_ERR_REQ)) {
662 vlog_module_failure_msg(request, fmt, ap);
663 }
664
665 va_end(ap);
666}
667
668/** Drain any outstanding messages from the fr_strerror buffers
669 *
670 * This function drains any messages from fr_strerror buffer adding a prefix (fmt)
671 * to the first message.
672 *
673 * @param[in] type the log category.
674 * @param[in] lvl of debugging this message should be logged at.
675 * @param[in] request The current request.
676 * @param[in] file src file the log message was generated in.
677 * @param[in] line number the log message was generated on.
678 * @param[in] fmt with printf style substitution tokens.
679 * @param[in] ... Substitution arguments.
680 */
682 char const *file, int line, char const *fmt, ...)
683{
684 char const *error;
685
686 if (!request->log.dst) return;
687
688 /*
689 * No strerror gets us identical behaviour to log_request_error
690 */
691 error = fr_strerror_pop();
692 if (!error) {
693 va_list ap;
694
695 if (!fmt) return; /* NOOP */
696
697 va_start(ap, fmt);
698 log_dst_foreach(request->log.dst, dst) {
699 va_list copy;
700 va_copy(copy, ap);
701
702 dst->func(type, lvl, request, file, line, fmt, copy, dst->uctx);
703
704 va_end(copy);
705 }
706 if ((type == L_ERR) || (type == L_DBG_ERR) || (type == L_DBG_ERR_REQ)) {
707 vlog_module_failure_msg(request, fmt, ap);
708 }
709 va_end(ap);
710
711 return; /* DONE */
712 }
713
714 /*
715 * Concatenate fmt with fr_strerror()
716 */
717 if (fmt) {
718 va_list ap;
719 char *tmp;
720
721 va_start(ap, fmt);
722 tmp = fr_vasprintf(request, fmt, ap);
723 va_end(ap);
724
725 if (!tmp) return;
726
727 log_request_error(type, lvl, request, file, line, "%s: %s", tmp, error);
728 talloc_free(tmp);
729 } else {
730 log_request_error(type, lvl, request, file, line, "%s", error);
731 }
732
733 /*
734 * Only the first message gets the prefix
735 */
736 while ((error = fr_strerror_pop())) log_request_error(type, lvl, request, file, line, "%s", error);
737}
738
739/** Cleanup the memory pool used by the OID sbuff
740 *
741 */
742static int _fr_log_request_oid_buff_free(void *arg)
743{
744 return talloc_free(arg);
745}
746
747/** Allocate an extensible sbuff for printing OID strings
748 *
749 */
750static inline CC_HINT(always_inline) fr_sbuff_t *log_request_oid_buff(void)
751{
752 fr_sbuff_t *sbuff;
754
756 if (unlikely(!sbuff)) {
757 sbuff = talloc(NULL, fr_sbuff_t);
758 if (!sbuff) {
759 fr_perror("Failed allocating memory for fr_log_request_oid_buff");
760 return NULL;
761 }
762 tctx = talloc(sbuff, fr_sbuff_uctx_talloc_t);
763 if (!tctx) {
764 fr_perror("Failed allocating memory for fr_sbuff_uctx_talloc_t");
765 talloc_free(sbuff);
766 return NULL;
767 }
768
769 fr_sbuff_init_talloc(sbuff, sbuff, tctx, 1024, (FR_DICT_ATTR_MAX_NAME_LEN + 1) * FR_DICT_MAX_TLV_STACK);
770
772 } else {
773 fr_sbuff_set(sbuff, fr_sbuff_start(sbuff)); /* Reset position */
774 }
775
776 return sbuff;
777}
778
779/** Print a #fr_pair_t.
780 *
781 * @param[in] lvl Debug lvl (1-4).
782 * @param[in] request to read logging params from.
783 * @param[in] parent of pair to print, may be NULL.
784 * @param[in] vp to print.
785 * @param[in] prefix (optional).
786 */
788 fr_pair_t const *parent, fr_pair_t const *vp, char const *prefix)
789{
790 fr_sbuff_t *oid_buff;
791
792 if (!request->log.dst) return;
793
794 if (!log_rdebug_enabled(lvl, request)) return;
795
797
798 oid_buff = log_request_oid_buff();
799
800 if (fr_pair_print_name(oid_buff, parent ? parent->da : NULL, &vp) <= 0) return;
801
802 /*
803 * Recursively print grouped attributes.
804 */
805 switch (vp->vp_type) {
807 RDEBUGX(lvl, "%s%s{", prefix ? prefix : "", fr_sbuff_start(oid_buff));
808 log_request_pair_list(lvl, request, vp, &vp->vp_group, NULL);
809 RDEBUGX(lvl, "}");
810 break;
811
812 default:
813 fr_assert(fr_type_is_leaf(vp->vp_type));
814
815 RDEBUGX(lvl, "%s%s%pV", prefix ? prefix : "", fr_sbuff_start(oid_buff), &vp->data);
816 break;
817 }
818}
819
820/** Print a #fr_pair_list_t
821 *
822 * @param[in] lvl Debug lvl (1-4).
823 * @param[in] request to read logging params from.
824 * @param[in] parent of vps to print, may be NULL.
825 * @param[in] vps to print.
826 * @param[in] prefix (optional).
827 */
829 fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
830{
831 if (fr_pair_list_empty(vps) || !request->log.dst) return;
832
833 if (!log_rdebug_enabled(lvl, request)) return;
834
835 RINDENT();
838
839 log_request_pair(lvl, request, parent, vp, prefix);
840 }
841 REXDENT();
842}
843
844/** Print a packet which we received or sent
845 *
846 * @param[in] request to read logging params from.
847 * @param[in] packet the packet in question
848 * @param[in] list to print.
849 * @param[in] type_da of Packet-Type
850 * @param[in] received is this packet being received or being sent
851 * @param[in] id do we log IDs, too
852 */
853void log_request_packet(request_t *request, fr_packet_t *packet, fr_pair_list_t *list, fr_dict_attr_t const *type_da, bool received, bool id)
854{
855 ssize_t slen;
856 fr_dict_enum_value_t const *enumv;
857 char const *name;
858 char name_buffer[32];
859 char id_buffer[32];
860 char buffer[256];
861
862 if (!RDEBUG_ENABLED) return;
863
864 slen = fr_socket_to_str(buffer, sizeof(buffer), &packet->socket, received);
865 if (slen <= 0) buffer[0] = '\0';
866
867 if (id) {
868 (void) snprintf(id_buffer, sizeof(id_buffer), " ID %u", packet->id);
869 } else {
870 id_buffer[0] = '\0';
871 }
872
873 enumv = fr_dict_enum_by_value(type_da, fr_box_uint32((uint32_t) packet->code));
874 if (enumv) {
875 name = enumv->name;
876 } else {
877 (void) snprintf(name_buffer, sizeof(name_buffer), "%u", packet->code);
878 name = name_buffer;
879 }
880
881 log_request(L_DBG, L_DBG_LVL_1, request, __FILE__, __LINE__, "%s %s%s %s",
882 received ? "Received" : "Sending",
883 name,
884 id_buffer,
885 buffer);
886
887 if (received || request->parent) {
888 log_request_pair_list(L_DBG_LVL_1, request, NULL, list, NULL);
889 } else {
890 log_request_proto_pair_list(L_DBG_LVL_1, request, NULL, list, NULL);
891 }
892}
893
894/** Print a list of protocol fr_pair_ts.
895 *
896 * @param[in] lvl Debug lvl (1-4).
897 * @param[in] request to read logging params from.
898 * @param[in] parent of vps to print, may be NULL.
899 * @param[in] vps to print.
900 * @param[in] prefix (optional).
901 */
903 fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
904{
905 if (fr_pair_list_empty(vps) || !request->log.dst) return;
906
907 if (!log_rdebug_enabled(lvl, request)) return;
908
909 RINDENT();
912
913 if (!fr_dict_attr_common_parent(fr_dict_root(request->proto_dict), vp->da, true)) continue;
914
915 log_request_pair(lvl, request, parent, vp, prefix);
916 }
917 REXDENT();
918}
919
920/** Write the string being parsed, and a marker showing where the parse error occurred
921 *
922 * @param[in] type the log category.
923 * @param[in] lvl of debugging this message should be logged at.
924 * @param[in] request The current request.
925 * @param[in] file src file the log message was generated in.
926 * @param[in] line number the log message was generated on.
927 * @param[in] str Subject string we're printing a marker for.
928 * @param[in] str_len Subject string length. Use SIZE_MAX for the
929 * length of the string.
930 * @param[in] marker_idx The position of the marker relative to the string.
931 * @param[in] marker_fmt What the parse error was.
932 * @param[in] ... Arguments for fmt string.
933 */
935 char const *file, int line,
936 char const *str, size_t str_len,
937 ssize_t marker_idx, char const *marker_fmt, ...)
938{
939 char const *ellipses = "";
940 rindent_t indent;
941 va_list ap;
942 char *error;
943 static char const marker_spaces[] = " "; /* 60 */
944
945 if (str_len == SIZE_MAX) str_len = strlen(str);
946
947 if (marker_idx < 0) marker_idx = (marker_idx == SSIZE_MIN) ? SSIZE_MAX : -marker_idx;
948
949 if ((size_t) marker_idx >= sizeof(marker_spaces)) {
950 size_t offset = (marker_idx - (sizeof(marker_spaces) - 1)) + (sizeof(marker_spaces) * 0.75);
951
952 marker_idx -= offset;
953
954 if (offset >= str_len) {
955 str += str_len;
956 str_len = 0;
957 } else {
958 str += offset;
959 str_len -= offset;
960 }
961
962 ellipses = "... ";
963 }
964
965 /*
966 * Don't want format markers being indented
967 */
968 indent = request->log.indent;
969 request->log.indent.module = 0;
970 request->log.indent.unlang = 0;
971
972 va_start(ap, marker_fmt);
973 error = fr_vasprintf(request, marker_fmt, ap);
974 va_end(ap);
975
976 log_request(type, lvl, request, file, line, "%s%.*s", ellipses, (int) str_len, str);
977 log_request(type, lvl, request, file, line, "%s%.*s^ %s", ellipses, (int) marker_idx, marker_spaces, error);
978 talloc_free(error);
979
980 request->log.indent = indent;
981}
982
984 char const *file, int line,
985 uint8_t const *data, size_t data_len)
986{
987 size_t i, j, len;
988 char buffer[(0x10 * 3) + 1];
989 char *p, *end = buffer + sizeof(buffer);
990
991 for (i = 0; i < data_len; i += 0x10) {
992 len = 0x10;
993 if ((i + len) > data_len) len = data_len - i;
994
995 for (p = buffer, j = 0; j < len; j++, p += 3) snprintf(p, end - p, "%02x ", data[i + j]);
996 log_request(type, lvl, request, file, line, "%04x: %s", (int)i, buffer);
997 }
998}
999
1000/** Function to provide as the readable callback to the event loop
1001 *
1002 * Writes any data read from a file descriptor to the request log,
1003 * tries very hard not to chop lines in the middle, but will split
1004 * at 1024 byte boundaries if forced to.
1005 *
1006 * @param[in] el UNUSED
1007 * @param[in] fd UNUSED
1008 * @param[in] flags UNUSED
1009 * @param[in] uctx Pointer to a log_fd_event_ctx_t
1010 */
1011void log_request_fd_event(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
1012{
1013 char buffer[1024];
1014 log_fd_event_ctx_t *log_info = uctx;
1015 request_t *request = log_info->request;
1016 fr_sbuff_t sbuff;
1017 fr_sbuff_marker_t m_start, m_end;
1018
1019 fr_sbuff_term_t const line_endings = FR_SBUFF_TERMS(L("\n"), L("\r"));
1020
1021 if (!RDEBUG_ENABLEDX(log_info->lvl)) {
1022 while (read(fd, buffer, sizeof(buffer)) > 0); /* discard all input */
1023 return;
1024 }
1025
1026 fr_sbuff_init_out(&sbuff, buffer, sizeof(buffer));
1027 fr_sbuff_marker(&m_start, &sbuff);
1028 fr_sbuff_marker(&m_end, &sbuff);
1029
1030#ifndef NDEBUG
1031 memset(buffer, 0x42, sizeof(buffer));
1032#endif
1033
1034 for (;;) {
1035 ssize_t slen;
1036
1037 slen = read(fd, fr_sbuff_current(&m_end), fr_sbuff_remaining(&m_end));
1038 if (slen < 0) {
1039 if (errno == EINTR) continue;
1040
1041 /*
1042 * This can happen if the I/O handler is
1043 * manually called to clear out any pending
1044 * data, and we're using a nonblocking FD.
1045 *
1046 * This can happen with the exec code if
1047 * the EVFILT_PROC event gets passed before
1048 * the EVFILT_READ event.
1049 */
1050 if (errno == EWOULDBLOCK) slen = 0;
1051 }
1052 if ((slen < 0) && (errno == EINTR)) continue;
1053
1054 if (slen > 0) fr_sbuff_advance(&m_end, slen);
1055
1056 while (fr_sbuff_ahead(&m_end) > 0) {
1057 fr_sbuff_adv_until(&sbuff, fr_sbuff_ahead(&m_end), &line_endings, '\0');
1058
1059 /*
1060 * Incomplete line, try and read the rest.
1061 */
1062 if ((slen > 0) && (fr_sbuff_used(&m_start) > 0) &&
1063 !fr_sbuff_is_terminal(&sbuff, &line_endings)) {
1064 break;
1065 }
1066
1067 log_request(log_info->type, log_info->lvl, log_info->request,
1068 __FILE__, __LINE__,
1069 "%s%s%pV",
1070 log_info->prefix ? log_info->prefix : "",
1071 log_info->prefix ? " - " : "",
1073 fr_sbuff_behind(&m_start)));
1074
1075 fr_sbuff_advance(&sbuff, 1); /* Skip the whitespace */
1076 fr_sbuff_set(&m_start, &sbuff);
1077 }
1078
1079 /*
1080 * Error or done
1081 */
1082 if (slen <= 0) break;
1083
1084 /*
1085 * Clear out the existing data
1086 */
1087 fr_sbuff_shift(&sbuff, fr_sbuff_used(&m_start), false);
1088 }
1089}
1090
1091/** Log a fatal error, then exit
1092 *
1093 */
1094void log_fatal(fr_log_t const *log, char const *file, int line, char const *fmt, ...)
1095{
1096 va_list ap;
1097
1098 va_start(ap, fmt);
1099 fr_vlog(log, L_ERR, file, line, fmt, ap);
1100 va_end(ap);
1101
1102 fr_exit_now(EXIT_FAILURE);
1103}
1104
1105/** Register a logging destination.
1106 *
1107 */
1108static void log_register_dst(char const *name, fr_log_t *log, CONF_SECTION *cs)
1109{
1110 fr_log_track_t *dst;
1111
1112 MEM(dst = talloc_zero(dst_tree, fr_log_track_t));
1113 dst->name = name;
1114 dst->log = log;
1115 dst->cs = cs;
1116
1117 fr_rb_insert(dst_tree, dst);
1118
1119 if (log->dst != L_DST_FILES) return;
1120
1121 fr_assert(log->file != NULL);
1122
1124}
1125
1126/** Get a logging destination by name.
1127 *
1128 */
1130{
1131 fr_log_track_t find, *found;
1132
1133 memset(&find, 0, sizeof(find));
1134 find.name = name;
1135
1136 found = fr_rb_find(dst_tree, &find);
1137 return (found) ? found->log : NULL;
1138}
1139
1140static int _log_free(fr_log_t *log)
1141{
1142 fr_assert(log->dst == L_DST_FILES);
1143
1144 fclose(log->handle);
1145 return 0;
1146}
1147
1148static char const *log_destination = NULL;
1149static bool log_timestamp;
1151
1152/*
1153 * Parse an fr_log_t configuration.
1154 */
1155static const conf_parser_t log_config[] = {
1156 { FR_CONF_POINTER("destination", FR_TYPE_STRING, 0, &log_destination), .dflt = "file" },
1157#if 0
1158 /*
1159 * @todo - once we allow this, also check that there's only _one_ destination
1160 * which uses syslog_facility.
1161 */
1162 { FR_CONF_OFFSET("syslog_facility", main_config_t, syslog_facility), .dflt = "daemon",
1164 .uctx = &(cf_table_parse_ctx_t){
1165 .table = syslog_facility_table,
1167 }
1168 },
1169#endif
1170 { FR_CONF_POINTER_IS_SET("timestamp", FR_TYPE_BOOL, 0, &log_timestamp) },
1171 { FR_CONF_OFFSET("file", fr_log_t, file), },
1172 { FR_CONF_OFFSET("colourise", fr_log_t, colourise) },
1173 { FR_CONF_OFFSET("line_number", fr_log_t, line_number) },
1174 { FR_CONF_OFFSET("use_utc", fr_log_t, dates_utc) },
1175 { FR_CONF_OFFSET("print_level", fr_log_t, print_level) },
1176 { FR_CONF_OFFSET("suppress_secrets", fr_log_t, suppress_secrets) },
1178};
1179
1180/** Parse a named logging section.
1181 *
1182 * @todo - we should probably allow for TCP sockets, too. But then
1183 * those can block. So we then also need a way to buffer outbound
1184 * log messages, and discard log messages if the buffer is full.
1185 *
1186 * This should probably be done with a FILE*, and L_DST_FUNC.
1187 */
1189{
1190 fr_log_track_t *dst;
1191 fr_log_t *log;
1192 char const *name;
1193
1194 name = cf_section_name2(cs);
1195 if (!name) name = "DEFAULT";
1196
1198 .name = name,
1199 });
1200 if (dst) {
1201 fr_strerror_printf("Cannot add duplicate log destination '%s'", name);
1202 return -1;
1203 }
1204
1205 MEM(log = talloc_zero(dst_tree, fr_log_t));
1206
1207 if (cf_section_rules_push(cs, log_config) < 0) {
1208 error:
1209 talloc_free(log);
1210 return -1;
1211 }
1212
1213 if (cf_section_parse(log, log, cs) < 0) goto error;
1214
1216 switch (log->dst) {
1217 case L_DST_NUM_DEST:
1218 fr_strerror_printf("Unknown log_destination '%s'", log_destination);
1220 log_destination = NULL;
1221 goto error;
1222
1223#ifdef HAVE_SYSLOG_H
1224 case L_DST_SYSLOG:
1226 log_destination = NULL;
1227
1228 if (fr_log_init_syslog(log) < 0) goto error;
1229 break;
1230#endif
1231
1232 case L_DST_STDOUT:
1234 log_destination = NULL;
1235
1236 if (fr_log_init_std(log, L_DST_STDOUT) < 0) goto error;
1237 break;
1238
1239 case L_DST_STDERR:
1241 log_destination = NULL;
1242
1243 if (fr_log_init_std(log, L_DST_STDERR) < 0) goto error;
1244 break;
1245
1246 case L_DST_FILES:
1248 log_destination = NULL;
1249
1250 if (!log->file) {
1251 fr_strerror_const("Specified \"files\" as a log destination, but no log filename was given!");
1252 goto error;
1253 }
1254
1256 .log = log,
1257 });
1258 if (dst) {
1259 fr_strerror_printf("The log destination '%s' is already logging to file %s",
1260 dst->name, log->file);
1261 goto error;
1262 }
1263
1264 if (fr_log_init_file(log, log->file) < 0) goto error;
1265
1266 talloc_set_destructor(log, _log_free);
1267 break;
1268
1269 case L_DST_NULL:
1270 break;
1271
1272 default:
1274 log_destination = NULL;
1275
1276 fr_assert(0);
1277 break;
1278 }
1279
1282 } else {
1284 }
1285
1286 log_register_dst(name, log, cs);
1287
1288 return 0;
1289}
1290
1291static int8_t _log_track_name_cmp(void const *two, void const *one)
1292{
1293 fr_log_track_t const *a = one;
1294 fr_log_track_t const *b = two;
1295
1296 return CMP(strcmp(a->name, b->name), 0);
1297}
1298
1299static int8_t _log_track_filename_cmp(void const *two, void const *one)
1300{
1301 fr_log_track_t const *a = one;
1302 fr_log_track_t const *b = two;
1303
1304 fr_assert(a->log);
1305 fr_assert(a->log->dst == L_DST_FILES);
1306
1307 fr_assert(b->log);
1308 fr_assert(b->log->dst == L_DST_FILES);
1309
1310 return CMP(strcmp(a->log->file, b->log->file), 0);
1311}
1312
1313/** Initialises the server logging functionality, and the underlying libfreeradius log
1314 *
1315 * @note Call log free when the server is done to fix any spurious memory leaks.
1316 *
1317 * @param[in] log Logging parameters.
1318 * @param[in] daemonize Changes what we do with stdout/stderr.
1319 * @return
1320 * - 0 on success.
1321 * - -1 on failure.
1322 */
1323int log_global_init(fr_log_t *log, bool daemonize)
1324{
1325 int ret;
1326
1327 ret = fr_log_init_legacy(log, daemonize);
1328 if (ret < 0) return ret;
1329
1330 if (fr_dict_autoload(log_dict) < 0) {
1331 fr_perror("log_init");
1332 return -1;
1333 }
1334
1336 fr_perror("log_init");
1337 return -1;
1338 }
1339
1340 dst_tree = fr_rb_inline_alloc(NULL, fr_log_track_t, name_node,
1341 _log_track_name_cmp, NULL);
1342 if (!dst_tree) {
1343 fr_perror("log_init");
1344 return -1;
1345 }
1346
1347 filename_tree = fr_rb_inline_alloc(NULL, fr_log_track_t, filename_node,
1349 if (!filename_tree) {
1350 fr_perror("log_init");
1351 return -1;
1352 }
1353
1354 log_register_dst("default", log, NULL);
1355
1356 return ret;
1357}
1358
1360{
1362 TALLOC_FREE(src_tree);
1363 TALLOC_FREE(dst_tree);
1364 TALLOC_FREE(filename_tree);
1365}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
va_end(args)
static int const char * fmt
Definition acutest.h:573
int const char int line
Definition acutest.h:702
va_start(args, fmt)
#define _Thread_local
Definition atexit.h:213
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:224
#define RCSID(id)
Definition build.h:560
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:228
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:113
#define unlikely(_x)
Definition build.h:455
#define UNUSED
Definition build.h:384
#define NUM_ELEMENTS(_t)
Definition build.h:406
int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
Parse a configuration section into user-supplied variables.
Definition cf_parse.c:1289
int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int.
Definition cf_parse.c:1724
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:669
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:623
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_POINTER_IS_SET(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result, recording if a defaul...
Definition cf_parse.h:349
#define cf_section_rules_push(_cs, _rule)
Definition cf_parse.h:701
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:334
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:606
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1359
static fr_atomic_queue_t ** aq
#define MEM(x)
Definition debug.h:36
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:226
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define fr_dict_autofree(_to_free)
Definition dict.h:915
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2287
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2639
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:292
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:305
fr_dict_enum_value_t const * fr_dict_enum_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition dict_util.c:3634
int fr_dict_attr_autoload(fr_dict_attr_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4374
#define fr_dict_autoload(_to_load)
Definition dict.h:912
#define FR_DICT_MAX_TLV_STACK
Maximum TLV stack size.
Definition dict.h:517
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:311
char const * name
Enum name.
Definition dict.h:254
#define FR_DICT_ATTR_MAX_NAME_LEN
Maximum length of a attribute name.
Definition dict.h:501
Specifies an attribute which must be present for the module to function.
Definition dict.h:291
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:304
Value of an enumerated attribute.
Definition dict.h:253
FILE * fopencookie(void *cookie, const char *mode, cookie_io_functions_t io_funcs)
Definition fopencookie.c:99
cookie_close_function_t close
Definition fopencookie.h:55
cookie_seek_function_t seek
Definition fopencookie.h:54
cookie_read_function_t read
Definition fopencookie.h:52
cookie_write_function_t write
Definition fopencookie.h:53
talloc_free(hp)
static fr_rb_tree_t * filename_tree
Definition log.c:234
void log_request_hex(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, uint8_t const *data, size_t data_len)
Definition log.c:983
static const conf_parser_t log_config[]
Definition log.c:1155
static bool log_timestamp
Definition log.c:1149
fr_rb_node_t filename_node
tree by name
Definition log.c:220
static int8_t _log_track_name_cmp(void const *two, void const *one)
Definition log.c:1291
static fr_sbuff_t * log_request_oid_buff(void)
Allocate an extensible sbuff for printing OID strings.
Definition log.c:750
static fr_dict_attr_t const * attr_module_failure_message
Definition log.c:206
void log_request_proto_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a list of protocol fr_pair_ts.
Definition log.c:902
static char const * log_destination
Definition log.c:1148
void log_request_packet(request_t *request, fr_packet_t *packet, fr_pair_list_t *list, fr_dict_attr_t const *type_da, bool received, bool id)
Print a packet which we received or sent.
Definition log.c:853
fr_rb_node_t id_node
tree by ID
Definition log.c:230
fr_table_num_sorted_t const log_destination_table[]
Definition log.c:186
void log_request(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Marshal variadic log arguments into a va_list and pass to normal logging functions.
Definition log.c:610
int log_global_init(fr_log_t *log, bool daemonize)
Initialises the server logging functionality, and the underlying libfreeradius log.
Definition log.c:1323
static fr_rb_tree_t * dst_tree
Definition log.c:233
static _Thread_local fr_sbuff_t * fr_log_request_oid_buff
Definition log.c:56
fr_dict_autoload_t log_dict[]
Definition log.c:201
static fr_dict_t const * dict_freeradius
Definition log.c:198
static int _fr_log_request_oid_buff_free(void *arg)
Cleanup the memory pool used by the OID sbuff.
Definition log.c:742
void log_request_perror(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Drain any outstanding messages from the fr_strerror buffers.
Definition log.c:681
static int _log_free(fr_log_t *log)
Definition log.c:1140
size_t log_destination_table_len
Definition log.c:194
bool log_rdebug_enabled(fr_log_lvl_t lvl, request_t const *request)
Whether a request specific debug message should be logged.
Definition log.c:267
fr_rb_node_t name_node
tree by name only
Definition log.c:229
static int8_t _log_track_filename_cmp(void const *two, void const *one)
Definition log.c:1299
void log_request_marker(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *str, size_t str_len, ssize_t marker_idx, char const *marker_fmt,...)
Write the string being parsed, and a marker showing where the parse error occurred.
Definition log.c:934
void log_request_pair_list(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_list_t const *vps, char const *prefix)
Print a fr_pair_list_t.
Definition log.c:828
int log_parse_section(CONF_SECTION *cs)
Parse a named logging section.
Definition log.c:1188
fr_table_num_sorted_t const syslog_severity_table[]
Syslog severity table.
Definition log.c:151
fr_log_t * log_dst_by_name(char const *name)
Get a logging destination by name.
Definition log.c:1129
fr_dict_attr_autoload_t log_dict_attr[]
Definition log.c:209
fr_log_t ** log
where the logs should go
Definition log.c:227
static fr_rb_tree_t * src_tree
Definition log.c:235
fr_log_t * original
the original fr_log_t
Definition log.c:226
void log_fatal(fr_log_t const *log, char const *file, int line, char const *fmt,...)
Log a fatal error, then exit.
Definition log.c:1094
static void vlog_module_failure_msg(request_t *request, char const *fmt, va_list ap)
Add a module failure message fr_pair_t to the request.
Definition log.c:565
fr_log_t * log
pointer to the log structure
Definition log.c:216
void log_request_fd_event(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
Function to provide as the readable callback to the event loop.
Definition log.c:1011
size_t syslog_facility_table_len
Definition log.c:143
static bool log_timestamp_is_set
Definition log.c:1150
static void log_always(fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
Send a server log message to its destination without evaluating its debug level.
Definition log.c:248
CONF_SECTION * cs
where this log configuration came from
Definition log.c:217
size_t syslog_severity_table_len
Definition log.c:184
char const * name
name of this logging source
Definition log.c:224
void log_request_error(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt,...)
Marshal variadic log arguments into a va_list and pass to error logging functions.
Definition log.c:644
char const * name
name of this logging destination
Definition log.c:215
uint32_t id
LOG_ID of this source.
Definition log.c:225
void vlog_request(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt, va_list ap, void *uctx)
Send a log message to its destination, possibly including fields from the request.
Definition log.c:293
static void log_register_dst(char const *name, fr_log_t *log, CONF_SECTION *cs)
Register a logging destination.
Definition log.c:1108
void log_request_pair(fr_log_lvl_t lvl, request_t *request, fr_pair_t const *parent, fr_pair_t const *vp, char const *prefix)
Print a fr_pair_t.
Definition log.c:787
fr_rb_node_t name_node
tree by name
Definition log.c:219
static _Thread_local TALLOC_CTX * fr_vlog_request_pool
Definition log.c:55
fr_table_num_sorted_t const syslog_facility_table[]
Syslog facility table.
Definition log.c:66
void log_global_free(void)
Definition log.c:1359
static int _fr_vlog_request_pool_free(void *arg)
Cleanup the memory pool used by vlog_request.
Definition log.c:277
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:460
#define log_dst_foreach(_head, _iter)
Iterate over the contents of a log_dst_t.
Definition log.h:773
request_t * request
request to log messages in the context of.
Definition log.h:86
#define RDEBUG_ENABLEDX(_x)
True if specified lvl is enabled.
Definition log.h:355
#define RDEBUGX(_l, fmt,...)
Definition log.h:357
fr_log_lvl_t lvl
Priority of the message.
Definition log.h:85
#define DEBUG_ENABLED3
True if global debug level 1-3 messages are enabled.
Definition log.h:264
char const * prefix
To add to log messages.
Definition log.h:87
fr_log_type_t type
What type of log message it is.
Definition log.h:84
#define RINDENT()
Indent R* messages by one level.
Definition log.h:447
Definition log.h:70
Context structure for the log fd event function.
Definition log.h:83
ssize_t rad_filename_escape(UNUSED request_t *request, char *out, size_t outlen, char const *in, UNUSED void *arg)
Escapes the raw string such that it should be safe to use as part of a file path.
Definition util.c:178
Stores all information relating to an event list.
Definition event.c:377
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
Definition file.c:218
fr_table_num_ordered_t const fr_log_levels[]
Maps log categories to message prefixes.
Definition log.c:260
int fr_log_init_legacy(fr_log_t *log, bool daemonize)
Initialise file descriptors based on logging destination.
Definition log.c:948
int fr_log_init_syslog(fr_log_t *log)
Initialise a syslog logging destination.
Definition log.c:1176
int fr_log_init_std(fr_log_t *log, fr_log_dst_t dst_type)
Initialise log dst for stdout, stderr or /dev/null.
Definition log.c:1069
int fr_log_init_file(fr_log_t *log, char const *file)
Initialise a file logging destination.
Definition log.c:1124
bool log_dates_utc
Definition log.c:304
@ L_DST_NULL
Discard log messages.
Definition log.h:80
@ L_DST_STDERR
Log to stderr.
Definition log.h:78
@ L_DST_FILES
Log to a file on disk.
Definition log.h:76
@ L_DST_FUNC
Send log messages to a FILE*, via fopencookie()
Definition log.h:79
@ L_DST_NUM_DEST
Definition log.h:81
@ L_DST_STDOUT
Log to stdout.
Definition log.h:75
@ L_DST_SYSLOG
Log to syslog.
Definition log.h:77
@ L_TIMESTAMP_ON
Always log timestamps.
Definition log.h:87
@ L_TIMESTAMP_OFF
Never log timestamps.
Definition log.h:88
@ L_TIMESTAMP_AUTO
Timestamp logging preference not specified.
Definition log.h:85
fr_log_lvl_t
Definition log.h:64
@ L_DBG_LVL_1
Highest priority debug messages (-x).
Definition log.h:67
#define fr_vlog(_log, _lvl, _file, _line, _fmt, _ap)
Definition log.h:166
fr_log_type_t
Definition log.h:51
@ L_DBG_WARN_REQ
Less severe warning only displayed when debugging is enabled.
Definition log.h:60
@ L_ERR
Error message.
Definition log.h:53
@ L_DBG_ERR
Error only displayed when debugging is enabled.
Definition log.h:59
@ L_DBG_ERR_REQ
Less severe error only displayed when debugging is enabled.
Definition log.h:61
@ L_DBG_WARN
Warning only displayed when debugging is enabled.
Definition log.h:58
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:56
Main server configuration.
Definition main_config.h:51
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_BOOL
A truth value.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
struct tm * gmtime_r(time_t const *l_clock, struct tm *result)
Definition missing.c:205
int fr_pair_value_aprintf(fr_pair_t *vp, char const *fmt,...)
Print data into an "string" data type.
Definition pair.c:2727
int fr_pair_value_strdup_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a vp, but don't copy it.
Definition pair.c:2687
char * fr_vasprintf_secure(TALLOC_CTX *ctx, char const *fmt, va_list ap)
Definition print.c:865
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
Definition print.c:860
#define fr_assert(_expr)
Definition rad_assert.h:37
#define RDEBUG_ENABLED()
static const char * spaces
Definition radict.c:177
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
#define fr_rb_inline_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black tree.
Definition rb.h:269
The main red black tree structure.
Definition rb.h:71
static char const * name
size_t fr_sbuff_shift(fr_sbuff_t *sbuff, size_t shift, bool move_end)
Shift the contents of the sbuff, returning the number of bytes we managed to shift.
Definition sbuff.c:201
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2242
size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
Wind position until we hit a character in the terminal set.
Definition sbuff.c:1942
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_current(_sbuff_or_marker)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:190
#define fr_sbuff_init_out(_out, _start, _len_or_end)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define fr_sbuff_used(_sbuff_or_marker)
#define fr_sbuff_behind(_sbuff_or_marker)
#define fr_sbuff_ahead(_sbuff_or_marker)
Set of terminal elements.
Talloc sbuff extension structure.
Definition sbuff.h:137
#define pair_prepend_request(_attr, _da)
Allocate and prepend a fr_pair_t to the request list.
Definition pair.h:77
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
ssize_t fr_socket_to_str(char *out, size_t outlen, fr_socket_t const *sock, bool received)
Print an fr_socket_t to a string.
Definition socket.c:1018
fr_aka_sim_id_type_t type
fr_pair_t * vp
Definition log.h:93
fr_log_dst_t dst
Log destination.
Definition log.h:94
fr_log_timestamp_t timestamp
Prefix log messages with timestamps.
Definition log.h:107
char const * file
Path to log file.
Definition log.h:110
FILE * handle
Path to log file.
Definition log.h:113
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:685
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:546
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:288
static fr_event_list_t * el
ssize_t xlat_aeval(TALLOC_CTX *ctx, char **out, request_t *request, char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx))
Definition xlat_eval.c:1893
unsigned int code
Packet code (type).
Definition packet.h:61
fr_socket_t socket
This packet was received on.
Definition packet.h:57
int id
Packet ID (used to link requests/responses).
Definition packet.h:60
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:204
#define fr_pair_list_foreach(_list_head, _iter)
Iterate over the contents of a fr_pair_list_t.
Definition pair.h:279
static fr_slen_t quote ssize_t fr_pair_print_name(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t const **vp_p)
Print an attribute name.
Definition pair_print.c:136
static fr_slen_t parent
Definition pair.h:858
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:737
char const * fr_strerror_pop(void)
Pop the last library error.
Definition strerror.c:685
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define FR_TYPE_STRUCTURAL
Definition types.h:316
#define fr_type_is_leaf(_x)
Definition types.h:393
static fr_slen_t data
Definition value.h:1340
#define fr_box_strvalue_len(_val, _len)
Definition value.h:309
#define fr_box_uint32(_val)
Definition value.h:335