1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
|
/*
Unix SMB/CIFS implementation.
Samba utility functions
Copyright (C) Andrew Tridgell 1992-1998
Copyright (C) Elrond 2002
Copyright (C) Simo Sorce 2002
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "system/filesys.h"
#include "system/syslog.h"
#include "lib/util/time.h"
/* define what facility to use for syslog */
#ifndef SYSLOG_FACILITY
#define SYSLOG_FACILITY LOG_DAEMON
#endif
/* -------------------------------------------------------------------------- **
* Defines...
*
* FORMAT_BUFR_MAX - Index of the last byte of the format buffer;
* format_bufr[FORMAT_BUFR_MAX] should always be reserved
* for a terminating null byte.
*/
#define FORMAT_BUFR_SIZE 1024
#define FORMAT_BUFR_MAX (FORMAT_BUFR_SIZE - 1)
/* -------------------------------------------------------------------------- **
* This module implements Samba's debugging utility.
*
* The syntax of a debugging log file is represented as:
*
* <debugfile> :== { <debugmsg> }
*
* <debugmsg> :== <debughdr> '\n' <debugtext>
*
* <debughdr> :== '[' TIME ',' LEVEL ']' [ [FILENAME ':'] [FUNCTION '()'] ]
*
* <debugtext> :== { <debugline> }
*
* <debugline> :== TEXT '\n'
*
* TEXT is a string of characters excluding the newline character.
* LEVEL is the DEBUG level of the message (an integer in the range 0..10).
* TIME is a timestamp.
* FILENAME is the name of the file from which the debug message was generated.
* FUNCTION is the function from which the debug message was generated.
*
* Basically, what that all means is:
*
* - A debugging log file is made up of debug messages.
*
* - Each debug message is made up of a header and text. The header is
* separated from the text by a newline.
*
* - The header begins with the timestamp and debug level of the message
* enclosed in brackets. The filename and function from which the
* message was generated may follow. The filename is terminated by a
* colon, and the function name is terminated by parenthesis.
*
* - The message text is made up of zero or more lines, each terminated by
* a newline.
*/
/* state variables for the debug system */
static struct {
bool initialized;
int fd; /* The log file handle */
enum debug_logtype logtype; /* The type of logging we are doing: eg stdout, file, stderr */
const char *prog_name;
bool reopening_logs;
bool schedule_reopen_logs;
struct debug_settings settings;
char *debugf;
debug_callback_fn callback;
void *callback_private;
} state = {
.settings = {
.timestamp_logs = true
},
.fd = 2 /* stderr by default */
};
/* -------------------------------------------------------------------------- **
* External variables.
*
* debugf - Debug file name.
* DEBUGLEVEL - System-wide debug message limit. Messages with message-
* levels higher than DEBUGLEVEL will not be processed.
*/
/*
used to check if the user specified a
logfile on the command line
*/
bool override_logfile;
/*
* This is to allow reading of DEBUGLEVEL_CLASS before the debug
* system has been initialized.
*/
static const int debug_class_list_initial[DBGC_MAX_FIXED + 1];
static int debug_num_classes = 0;
int *DEBUGLEVEL_CLASS = discard_const_p(int, debug_class_list_initial);
/* -------------------------------------------------------------------------- **
* Internal variables.
*
* debug_count - Number of debug messages that have been output.
* Used to check log size.
*
* current_msg_level - Internal copy of the message debug level. Written by
* dbghdr() and read by Debug1().
*
* format_bufr - Used to format debug messages. The dbgtext() function
* prints debug messages to a string, and then passes the
* string to format_debug_text(), which uses format_bufr
* to build the formatted output.
*
* format_pos - Marks the first free byte of the format_bufr.
*
*
* log_overflow - When this variable is true, never attempt to check the
* size of the log. This is a hack, so that we can write
* a message using DEBUG, from open_logs() when we
* are unable to open a new log file for some reason.
*/
static int debug_count = 0;
static int current_msg_level = 0;
static char *format_bufr = NULL;
static size_t format_pos = 0;
static bool log_overflow = false;
/*
* Define all the debug class selection names here. Names *MUST NOT* contain
* white space. There must be one name for each DBGC_<class name>, and they
* must be in the table in the order of DBGC_<class name>..
*/
static const char *default_classname_table[] = {
"all", /* DBGC_ALL; index refs traditional DEBUGLEVEL */
"tdb", /* DBGC_TDB */
"printdrivers", /* DBGC_PRINTDRIVERS */
"lanman", /* DBGC_LANMAN */
"smb", /* DBGC_SMB */
"rpc_parse", /* DBGC_RPC_PARSE */
"rpc_srv", /* DBGC_RPC_SRV */
"rpc_cli", /* DBGC_RPC_CLI */
"passdb", /* DBGC_PASSDB */
"sam", /* DBGC_SAM */
"auth", /* DBGC_AUTH */
"winbind", /* DBGC_WINBIND */
"vfs", /* DBGC_VFS */
"idmap", /* DBGC_IDMAP */
"quota", /* DBGC_QUOTA */
"acls", /* DBGC_ACLS */
"locking", /* DBGC_LOCKING */
"msdfs", /* DBGC_MSDFS */
"dmapi", /* DBGC_DMAPI */
"registry", /* DBGC_REGISTRY */
"scavenger", /* DBGC_SCAVENGER */
"dns", /* DBGC_DNS */
"ldb", /* DBGC_LDB */
NULL
};
static char **classname_table = NULL;
/* -------------------------------------------------------------------------- **
* Functions...
*/
static void debug_init(void);
/***************************************************************************
Free memory pointed to by global pointers.
****************************************************************************/
void gfree_debugsyms(void)
{
TALLOC_FREE(classname_table);
if ( DEBUGLEVEL_CLASS != debug_class_list_initial ) {
TALLOC_FREE( DEBUGLEVEL_CLASS );
DEBUGLEVEL_CLASS = discard_const_p(int, debug_class_list_initial);
}
TALLOC_FREE(format_bufr);
debug_num_classes = 0;
state.initialized = false;
}
/****************************************************************************
utility lists registered debug class names's
****************************************************************************/
char *debug_list_class_names_and_levels(void)
{
char *buf = NULL;
unsigned int i;
/* prepare strings */
for (i = 0; i < debug_num_classes; i++) {
buf = talloc_asprintf_append(buf,
"%s:%d%s",
classname_table[i],
DEBUGLEVEL_CLASS[i],
i == (debug_num_classes - 1) ? "\n" : " ");
if (buf == NULL) {
return NULL;
}
}
return buf;
}
/****************************************************************************
Utility to translate names to debug class index's (internal version).
****************************************************************************/
static int debug_lookup_classname_int(const char* classname)
{
int i;
if (!classname) return -1;
for (i=0; i < debug_num_classes; i++) {
if (strcmp(classname, classname_table[i])==0)
return i;
}
return -1;
}
/****************************************************************************
Add a new debug class to the system.
****************************************************************************/
int debug_add_class(const char *classname)
{
int ndx;
int *new_class_list;
char **new_name_list;
int default_level;
if (!classname)
return -1;
/* check the init has yet been called */
debug_init();
ndx = debug_lookup_classname_int(classname);
if (ndx >= 0)
return ndx;
ndx = debug_num_classes;
if (DEBUGLEVEL_CLASS == debug_class_list_initial) {
/* Initial loading... */
new_class_list = NULL;
} else {
new_class_list = DEBUGLEVEL_CLASS;
}
default_level = DEBUGLEVEL_CLASS[DBGC_ALL];
new_class_list = talloc_realloc(NULL, new_class_list, int, ndx + 1);
if (!new_class_list)
return -1;
DEBUGLEVEL_CLASS = new_class_list;
DEBUGLEVEL_CLASS[ndx] = default_level;
new_name_list = talloc_realloc(NULL, classname_table, char *, ndx + 1);
if (!new_name_list)
return -1;
classname_table = new_name_list;
classname_table[ndx] = talloc_strdup(classname_table, classname);
if (! classname_table[ndx])
return -1;
debug_num_classes = ndx + 1;
return ndx;
}
/****************************************************************************
Utility to translate names to debug class index's (public version).
****************************************************************************/
int debug_lookup_classname(const char *classname)
{
int ndx;
if (!classname || !*classname)
return -1;
ndx = debug_lookup_classname_int(classname);
if (ndx != -1)
return ndx;
DEBUG(0, ("debug_lookup_classname(%s): Unknown class\n",
classname));
return debug_add_class(classname);
}
/****************************************************************************
Dump the current registered debug levels.
****************************************************************************/
static void debug_dump_status(int level)
{
int q;
DEBUG(level, ("INFO: Current debug levels:\n"));
for (q = 0; q < debug_num_classes; q++) {
const char *classname = classname_table[q];
DEBUGADD(level, (" %s: %d\n",
classname,
DEBUGLEVEL_CLASS[q]));
}
}
/****************************************************************************
parse the debug levels from smbcontrol. Example debug level parameter:
printdrivers:7
****************************************************************************/
static bool debug_parse_params(char **params)
{
int i, ndx;
char *class_name;
char *class_level;
if (!params)
return false;
/* Allow DBGC_ALL to be specified w/o requiring its class name e.g."10"
* v.s. "all:10", this is the traditional way to set DEBUGLEVEL
*/
if (isdigit((int)params[0][0])) {
DEBUGLEVEL_CLASS[DBGC_ALL] = atoi(params[0]);
i = 1; /* start processing at the next params */
} else {
DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
i = 0; /* DBGC_ALL not specified OR class name was included */
}
/* Array is debug_num_classes long */
for (ndx = DBGC_ALL; ndx < debug_num_classes; ndx++) {
DEBUGLEVEL_CLASS[ndx] = DEBUGLEVEL_CLASS[DBGC_ALL];
}
/* Fill in new debug class levels */
for (; i < debug_num_classes && params[i]; i++) {
char *saveptr;
if ((class_name = strtok_r(params[i],":", &saveptr)) &&
(class_level = strtok_r(NULL, "\0", &saveptr)) &&
((ndx = debug_lookup_classname(class_name)) != -1)) {
DEBUGLEVEL_CLASS[ndx] = atoi(class_level);
} else {
DEBUG(0,("debug_parse_params: unrecognized debug class name or format [%s]\n", params[i]));
return false;
}
}
return true;
}
/****************************************************************************
Parse the debug levels from smb.conf. Example debug level string:
3 tdb:5 printdrivers:7
Note: the 1st param has no "name:" preceeding it.
****************************************************************************/
bool debug_parse_levels(const char *params_str)
{
char **params;
/* Just in case */
debug_init();
params = str_list_make(NULL, params_str, NULL);
if (debug_parse_params(params)) {
debug_dump_status(5);
TALLOC_FREE(params);
return true;
} else {
TALLOC_FREE(params);
return false;
}
}
/* setup for logging of talloc warnings */
static void talloc_log_fn(const char *msg)
{
DEBUG(0,("%s", msg));
}
void debug_setup_talloc_log(void)
{
talloc_set_log_fn(talloc_log_fn);
}
/****************************************************************************
Init debugging (one time stuff)
****************************************************************************/
static void debug_init(void)
{
const char **p;
if (state.initialized)
return;
state.initialized = true;
debug_setup_talloc_log();
for(p = default_classname_table; *p; p++) {
debug_add_class(*p);
}
format_bufr = talloc_array(NULL, char, FORMAT_BUFR_SIZE);
if (!format_bufr) {
smb_panic("debug_init: unable to create buffer");
}
}
/* This forces in some smb.conf derived values into the debug system.
* There are no pointers in this structure, so we can just
* structure-assign it in */
void debug_set_settings(struct debug_settings *settings)
{
state.settings = *settings;
}
/**
control the name of the logfile and whether logging will be to stdout, stderr
or a file, and set up syslog
new_log indicates the destination for the debug log (an enum in
order of precedence - once set to DEBUG_FILE, it is not possible to
reset to DEBUG_STDOUT for example. This makes it easy to override
for debug to stderr on the command line, as the smb.conf cannot
reset it back to file-based logging
*/
void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
{
debug_init();
if (state.logtype < new_logtype) {
state.logtype = new_logtype;
}
if (prog_name) {
state.prog_name = prog_name;
}
reopen_logs_internal();
if (state.logtype == DEBUG_FILE) {
#ifdef WITH_SYSLOG
if (prog_name) {
const char *p = strrchr_m( prog_name,'/' );
if (p)
prog_name = p + 1;
#ifdef LOG_DAEMON
openlog( prog_name, LOG_PID, SYSLOG_FACILITY );
#else
/* for old systems that have no facility codes. */
openlog( prog_name, LOG_PID );
#endif
}
#endif
}
}
/***************************************************************************
Set the logfile name.
**************************************************************************/
void debug_set_logfile(const char *name)
{
if (name == NULL || *name == 0) {
/* this copes with calls when smb.conf is not loaded yet */
return;
}
TALLOC_FREE(state.debugf);
state.debugf = talloc_strdup(NULL, name);
}
static void debug_close_fd(int fd)
{
if (fd > 2) {
close(fd);
}
}
bool debug_get_output_is_stderr(void)
{
return (state.logtype == DEBUG_DEFAULT_STDERR) || (state.logtype == DEBUG_STDERR);
}
bool debug_get_output_is_stdout(void)
{
return (state.logtype == DEBUG_DEFAULT_STDOUT) || (state.logtype == DEBUG_STDOUT);
}
void debug_set_callback(void *private_ptr, debug_callback_fn fn)
{
debug_init();
if (fn) {
state.logtype = DEBUG_CALLBACK;
state.callback_private = private_ptr;
state.callback = fn;
} else {
state.logtype = DEBUG_DEFAULT_STDERR;
state.callback_private = NULL;
state.callback = NULL;
}
}
/**************************************************************************
reopen the log files
note that we now do this unconditionally
We attempt to open the new debug fp before closing the old. This means
if we run out of fd's we just keep using the old fd rather than aborting.
Fix from dgibson@linuxcare.com.
**************************************************************************/
/**
reopen the log file (usually called because the log file name might have changed)
*/
bool reopen_logs_internal(void)
{
mode_t oldumask;
int new_fd = 0;
int old_fd = 0;
bool ret = true;
if (state.reopening_logs) {
return true;
}
/* Now clear the SIGHUP induced flag */
state.schedule_reopen_logs = false;
switch (state.logtype) {
case DEBUG_CALLBACK:
return true;
case DEBUG_STDOUT:
case DEBUG_DEFAULT_STDOUT:
debug_close_fd(state.fd);
state.fd = 1;
return true;
case DEBUG_DEFAULT_STDERR:
case DEBUG_STDERR:
debug_close_fd(state.fd);
state.fd = 2;
return true;
case DEBUG_FILE:
break;
}
oldumask = umask( 022 );
if (!state.debugf) {
return false;
}
state.reopening_logs = true;
new_fd = open( state.debugf, O_WRONLY|O_APPEND|O_CREAT, 0644);
if (new_fd == -1) {
log_overflow = true;
DEBUG(0, ("Unable to open new log file '%s': %s\n", state.debugf, strerror(errno)));
log_overflow = false;
ret = false;
} else {
old_fd = state.fd;
state.fd = new_fd;
debug_close_fd(old_fd);
}
/* Fix from klausr@ITAP.Physik.Uni-Stuttgart.De
* to fix problem where smbd's that generate less
* than 100 messages keep growing the log.
*/
force_check_log_size();
(void)umask(oldumask);
/* Take over stderr to catch output into logs */
if (state.fd > 0) {
if (dup2(state.fd, 2) == -1) {
/* Close stderr too, if dup2 can't point it -
at the logfile. There really isn't much
that can be done on such a fundemental
failure... */
close_low_fds(false, false, true);
}
}
state.reopening_logs = false;
return ret;
}
/**************************************************************************
Force a check of the log size.
***************************************************************************/
void force_check_log_size( void )
{
debug_count = 100;
}
_PUBLIC_ void debug_schedule_reopen_logs(void)
{
state.schedule_reopen_logs = true;
}
/***************************************************************************
Check to see if there is any need to check if the logfile has grown too big.
**************************************************************************/
bool need_to_check_log_size( void )
{
int maxlog;
if( debug_count < 100)
return( false );
maxlog = state.settings.max_log_size * 1024;
if ( state.fd <=2 || maxlog <= 0 ) {
debug_count = 0;
return(false);
}
return( true );
}
/**************************************************************************
Check to see if the log has grown to be too big.
**************************************************************************/
void check_log_size( void )
{
int maxlog;
struct stat st;
/*
* We need to be root to check/change log-file, skip this and let the main
* loop check do a new check as root.
*/
#if _SAMBA_BUILD_ == 3
if (geteuid() != sec_initial_uid())
#else
if( geteuid() != 0)
#endif
{
/* We don't check sec_initial_uid() here as it isn't
* available in common code and we don't generally
* want to rotate and the possibly lose logs in
* make test or the build farm */
return;
}
if(log_overflow || (!state.schedule_reopen_logs && !need_to_check_log_size())) {
return;
}
maxlog = state.settings.max_log_size * 1024;
if (state.schedule_reopen_logs) {
(void)reopen_logs_internal();
}
if (maxlog && (fstat(state.fd, &st) == 0
&& st.st_size > maxlog )) {
(void)reopen_logs_internal();
if (state.fd > 2 && (fstat(state.fd, &st) == 0
&& st.st_size > maxlog)) {
char *name = NULL;
if (asprintf(&name, "%s.old", state.debugf ) < 0) {
return;
}
(void)rename(state.debugf, name);
if (!reopen_logs_internal()) {
/* We failed to reopen a log - continue using the old name. */
(void)rename(name, state.debugf);
}
SAFE_FREE(name);
}
}
/*
* Here's where we need to panic if state.fd == 0 or -1 (invalid values)
*/
if (state.fd <= 0) {
/* This code should only be reached in very strange
* circumstances. If we merely fail to open the new log we
* should stick with the old one. ergo this should only be
* reached when opening the logs for the first time: at
* startup or when the log level is increased from zero.
* -dwg 6 June 2000
*/
int fd = open( "/dev/console", O_WRONLY, 0);
if (fd != -1) {
state.fd = fd;
DEBUG(0,("check_log_size: open of debug file %s failed - using console.\n",
state.debugf ));
} else {
/*
* We cannot continue without a debug file handle.
*/
abort();
}
}
debug_count = 0;
}
/*************************************************************************
Write an debug message on the debugfile.
This is called by dbghdr() and format_debug_text().
************************************************************************/
int Debug1( const char *format_str, ... )
{
va_list ap;
int old_errno = errno;
debug_count++;
if (state.logtype == DEBUG_CALLBACK) {
char *msg;
int ret;
va_start( ap, format_str );
ret = vasprintf( &msg, format_str, ap );
if (ret != -1) {
if (msg[ret - 1] == '\n') {
msg[ret - 1] = '\0';
}
state.callback(state.callback_private, current_msg_level, msg);
free(msg);
}
va_end( ap );
goto done;
} else if ( state.logtype != DEBUG_FILE ) {
va_start( ap, format_str );
if (state.fd > 0)
(void)vdprintf( state.fd, format_str, ap );
va_end( ap );
errno = old_errno;
goto done;
}
#ifdef WITH_SYSLOG
if( !state.settings.syslog_only)
#endif
{
if( state.fd <= 0 ) {
mode_t oldumask = umask( 022 );
int fd = open( state.debugf, O_WRONLY|O_APPEND|O_CREAT, 0644 );
(void)umask( oldumask );
if(fd == -1) {
errno = old_errno;
goto done;
}
state.fd = fd;
}
}
#ifdef WITH_SYSLOG
if( current_msg_level < state.settings.syslog ) {
/* map debug levels to syslog() priorities
* note that not all DEBUG(0, ...) calls are
* necessarily errors */
static const int priority_map[4] = {
LOG_ERR, /* 0 */
LOG_WARNING, /* 1 */
LOG_NOTICE, /* 2 */
LOG_INFO, /* 3 */
};
int priority;
char *msgbuf = NULL;
int ret;
if( current_msg_level >= ARRAY_SIZE(priority_map) || current_msg_level < 0)
priority = LOG_DEBUG;
else
priority = priority_map[current_msg_level];
/*
* Specify the facility to interoperate with other syslog
* callers (vfs_full_audit for example).
*/
priority |= SYSLOG_FACILITY;
va_start(ap, format_str);
ret = vasprintf(&msgbuf, format_str, ap);
va_end(ap);
if (ret != -1) {
syslog(priority, "%s", msgbuf);
}
SAFE_FREE(msgbuf);
}
#endif
check_log_size();
#ifdef WITH_SYSLOG
if( !state.settings.syslog_only)
#endif
{
va_start( ap, format_str );
if (state.fd > 0)
(void)vdprintf( state.fd, format_str, ap );
va_end( ap );
}
done:
errno = old_errno;
return( 0 );
}
/**************************************************************************
Print the buffer content via Debug1(), then reset the buffer.
Input: none
Output: none
****************************************************************************/
static void bufr_print( void )
{
format_bufr[format_pos] = '\0';
(void)Debug1( "%s", format_bufr );
format_pos = 0;
}
/***************************************************************************
Format the debug message text.
Input: msg - Text to be added to the "current" debug message text.
Output: none.
Notes: The purpose of this is two-fold. First, each call to syslog()
(used by Debug1(), see above) generates a new line of syslog
output. This is fixed by storing the partial lines until the
newline character is encountered. Second, printing the debug
message lines when a newline is encountered allows us to add
spaces, thus indenting the body of the message and making it
more readable.
**************************************************************************/
static void format_debug_text( const char *msg )
{
size_t i;
bool timestamp = (state.logtype == DEBUG_FILE && (state.settings.timestamp_logs));
if (!format_bufr) {
debug_init();
}
for( i = 0; msg[i]; i++ ) {
/* Indent two spaces at each new line. */
if(timestamp && 0 == format_pos) {
format_bufr[0] = format_bufr[1] = ' ';
format_pos = 2;
}
/* If there's room, copy the character to the format buffer. */
if( format_pos < FORMAT_BUFR_MAX )
format_bufr[format_pos++] = msg[i];
/* If a newline is encountered, print & restart. */
if( '\n' == msg[i] )
bufr_print();
/* If the buffer is full dump it out, reset it, and put out a line
* continuation indicator.
*/
if( format_pos >= FORMAT_BUFR_MAX ) {
bufr_print();
(void)Debug1( " +>\n" );
}
}
/* Just to be safe... */
format_bufr[format_pos] = '\0';
}
/***************************************************************************
Flush debug output, including the format buffer content.
Input: none
Output: none
***************************************************************************/
void dbgflush( void )
{
bufr_print();
}
/***************************************************************************
Print a Debug Header.
Input: level - Debug level of the message (not the system-wide debug
level. )
cls - Debuglevel class of the calling module.
file - Pointer to a string containing the name of the file
from which this function was called, or an empty string
if the __FILE__ macro is not implemented.
func - Pointer to a string containing the name of the function
from which this function was called, or an empty string
if the __FUNCTION__ macro is not implemented.
line - line number of the call to dbghdr, assuming __LINE__
works.
Output: Always true. This makes it easy to fudge a call to dbghdr()
in a macro, since the function can be called as part of a test.
Eg: ( (level <= DEBUGLEVEL) && (dbghdr(level,"",line)) )
Notes: This function takes care of setting current_msg_level.
****************************************************************************/
bool dbghdrclass(int level, int cls, const char *location, const char *func)
{
/* Ensure we don't lose any real errno value. */
int old_errno = errno;
if( format_pos ) {
/* This is a fudge. If there is stuff sitting in the format_bufr, then
* the *right* thing to do is to call
* format_debug_text( "\n" );
* to write the remainder, and then proceed with the new header.
* Unfortunately, there are several places in the code at which
* the DEBUG() macro is used to build partial lines. That in mind,
* we'll work under the assumption that an incomplete line indicates
* that a new header is *not* desired.
*/
return( true );
}
/* Set current_msg_level. */
current_msg_level = level;
/* Don't print a header if we're logging to stdout. */
if ( state.logtype != DEBUG_FILE ) {
return( true );
}
/* Print the header if timestamps are turned on. If parameters are
* not yet loaded, then default to timestamps on.
*/
if( state.settings.timestamp_logs || state.settings.debug_prefix_timestamp) {
bool verbose = false;
char header_str[200];
header_str[0] = '\0';
if (unlikely(DEBUGLEVEL_CLASS[ cls ] >= 10)) {
verbose = true;
}
if (verbose || state.settings.debug_pid)
slprintf(header_str,sizeof(header_str)-1,", pid=%u",(unsigned int)getpid());
if (verbose || state.settings.debug_uid) {
size_t hs_len = strlen(header_str);
slprintf(header_str + hs_len,
sizeof(header_str) - 1 - hs_len,
", effective(%u, %u), real(%u, %u)",
(unsigned int)geteuid(), (unsigned int)getegid(),
(unsigned int)getuid(), (unsigned int)getgid());
}
if ((verbose || state.settings.debug_class)
&& (cls != DBGC_ALL)) {
size_t hs_len = strlen(header_str);
slprintf(header_str + hs_len,
sizeof(header_str) -1 - hs_len,
", class=%s",
classname_table[cls]);
}
/* Print it all out at once to prevent split syslog output. */
if( state.settings.debug_prefix_timestamp ) {
char *time_str = current_timestring(NULL,
state.settings.debug_hires_timestamp);
(void)Debug1( "[%s, %2d%s] ",
time_str,
level, header_str);
talloc_free(time_str);
} else {
char *time_str = current_timestring(NULL,
state.settings.debug_hires_timestamp);
(void)Debug1( "[%s, %2d%s] %s(%s)\n",
time_str,
level, header_str, location, func );
talloc_free(time_str);
}
}
errno = old_errno;
return( true );
}
/***************************************************************************
Add text to the body of the "current" debug message via the format buffer.
Input: format_str - Format string, as used in printf(), et. al.
... - Variable argument list.
..or.. va_alist - Old style variable parameter list starting point.
Output: Always true. See dbghdr() for more info, though this is not
likely to be used in the same way.
***************************************************************************/
bool dbgtext( const char *format_str, ... )
{
va_list ap;
char *msgbuf = NULL;
bool ret = true;
int res;
va_start(ap, format_str);
res = vasprintf(&msgbuf, format_str, ap);
va_end(ap);
if (res != -1) {
format_debug_text(msgbuf);
} else {
ret = false;
}
SAFE_FREE(msgbuf);
return ret;
}
/* the registered mutex handlers */
static struct {
const char *name;
struct debug_ops ops;
} debug_handlers;
/**
log suspicious usage - print comments and backtrace
*/
_PUBLIC_ void log_suspicious_usage(const char *from, const char *info)
{
if (!debug_handlers.ops.log_suspicious_usage) return;
debug_handlers.ops.log_suspicious_usage(from, info);
}
/**
print suspicious usage - print comments and backtrace
*/
_PUBLIC_ void print_suspicious_usage(const char* from, const char* info)
{
if (!debug_handlers.ops.print_suspicious_usage) return;
debug_handlers.ops.print_suspicious_usage(from, info);
}
_PUBLIC_ uint32_t get_task_id(void)
{
if (debug_handlers.ops.get_task_id) {
return debug_handlers.ops.get_task_id();
}
return getpid();
}
_PUBLIC_ void log_task_id(void)
{
if (!debug_handlers.ops.log_task_id) return;
if (!reopen_logs_internal()) return;
debug_handlers.ops.log_task_id(state.fd);
}
/**
register a set of debug handlers.
*/
_PUBLIC_ void register_debug_handlers(const char *name, struct debug_ops *ops)
{
debug_handlers.name = name;
debug_handlers.ops = *ops;
}
|