2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* -------------------------------------------------------------------------- **
27 * FORMAT_BUFR_MAX - Index of the last byte of the format buffer;
28 * format_bufr[FORMAT_BUFR_MAX] should always be reserved
29 * for a terminating nul byte.
32 #define FORMAT_BUFR_MAX ( sizeof( format_bufr ) - 1 )
34 /* -------------------------------------------------------------------------- **
35 * This module implements Samba's debugging utility.
37 * The syntax of a debugging log file is represented as:
39 * <debugfile> :== { <debugmsg> }
41 * <debugmsg> :== <debughdr> '\n' <debugtext>
43 * <debughdr> :== '[' TIME ',' LEVEL ']' [ [FILENAME ':'] [FUNCTION '()'] ]
45 * <debugtext> :== { <debugline> }
47 * <debugline> :== TEXT '\n'
49 * TEXT is a string of characters excluding the newline character.
50 * LEVEL is the DEBUG level of the message (an integer in the range 0..10).
51 * TIME is a timestamp.
52 * FILENAME is the name of the file from which the debug message was generated.
53 * FUNCTION is the function from which the debug message was generated.
55 * Basically, what that all means is:
57 * - A debugging log file is made up of debug messages.
59 * - Each debug message is made up of a header and text. The header is
60 * separated from the text by a newline.
62 * - The header begins with the timestamp and debug level of the message
63 * enclosed in brackets. The filename and function from which the
64 * message was generated may follow. The filename is terminated by a
65 * colon, and the function name is terminated by parenthesis.
67 * - The message text is made up of zero or more lines, each terminated by
71 /* -------------------------------------------------------------------------- **
74 * dbf - Global debug file handle.
75 * debugf - Debug file name.
76 * append_log - If True, then the output file will be opened in append
78 * DEBUGLEVEL - System-wide debug message limit. Messages with message-
79 * levels higher than DEBUGLEVEL will not be processed.
84 BOOL append_log = False;
88 /* -------------------------------------------------------------------------- **
91 * stdout_logging - Default False, if set to True then dbf will be set to
92 * stdout and debug output will go to dbf only, and not
93 * to syslog. Set in setup_logging() and read in Debug1().
95 * debug_count - Number of debug messages that have been output.
96 * Used to check log size.
98 * syslog_level - Internal copy of the message debug level. Written by
99 * dbghdr() and read by Debug1().
101 * format_bufr - Used to format debug messages. The dbgtext() function
102 * prints debug messages to a string, and then passes the
103 * string to format_debug_text(), which uses format_bufr
104 * to build the formatted output.
106 * format_pos - Marks the first free byte of the format_bufr.
109 static BOOL stdout_logging = False;
110 static int debug_count = 0;
112 static int syslog_level = 0;
114 static pstring format_bufr = { '\0' };
115 static size_t format_pos = 0;
118 /* -------------------------------------------------------------------------- **
123 /* ************************************************************************** **
124 * catch a sigusr2 - decrease the debug log level.
125 * ************************************************************************** **
127 void sig_usr2( int sig )
133 DEBUG( 0, ( "Got SIGUSR2; set debug level to %d.\n", DEBUGLEVEL ) );
137 #if !defined(HAVE_SIGACTION)
138 CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
145 /* ************************************************************************** **
146 * catch a sigusr1 - increase the debug log level.
147 * ************************************************************************** **
149 void sig_usr1( int sig )
154 if( DEBUGLEVEL > 10 )
157 DEBUG( 0, ( "Got SIGUSR1; set debug level to %d.\n", DEBUGLEVEL ) );
161 #if !defined(HAVE_SIGACTION)
162 CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
169 /* ************************************************************************** **
170 * get ready for syslog stuff
171 * ************************************************************************** **
173 void setup_logging( char *pname, BOOL interactive )
177 stdout_logging = True;
183 char *p = strrchr( pname,'/' );
188 openlog( pname, LOG_PID, SYSLOG_FACILITY );
189 #else /* for old systems that have no facility codes. */
190 openlog( pname, LOG_PID );
194 } /* setup_logging */
196 /* ************************************************************************** **
197 * reopen the log files
198 * note that we now do this unconditionally
199 * ************************************************************************** **
201 void reopen_logs( void )
206 if (DEBUGLEVEL <= 0) {
214 oldumask = umask( 022 );
216 pstrcpy(fname, debugf );
217 if (lp_loaded() && (*lp_logfile()))
218 pstrcpy(fname, lp_logfile());
220 pstrcpy( debugf, fname );
224 dbf = sys_fopen( debugf, "a" );
226 dbf = sys_fopen( debugf, "w" );
227 /* Fix from klausr@ITAP.Physik.Uni-Stuttgart.De
228 * to fix problem where smbd's that generate less
229 * than 100 messages keep growing the log.
231 force_check_log_size();
234 (void)umask(oldumask);
238 /* ************************************************************************** **
239 * Force a check of the log size.
240 * ************************************************************************** **
242 void force_check_log_size( void )
247 /***************************************************************************
248 Check to see if there is any need to check if the logfile has grown too big.
249 **************************************************************************/
251 BOOL need_to_check_log_size( void )
255 if( debug_count++ < 100 )
258 maxlog = lp_max_log_size() * 1024;
259 if( !dbf || maxlog <= 0 ) {
266 /* ************************************************************************** **
267 * Check to see if the log has grown to be too big.
268 * ************************************************************************** **
271 void check_log_size( void )
277 * We need to be root to check/change log-file, skip this and let the main
278 * loop check do a new check as root.
284 if( !need_to_check_log_size() )
287 maxlog = lp_max_log_size() * 1024;
289 if( sys_fstat( fileno( dbf ), &st ) == 0 && st.st_size > maxlog )
294 if( dbf && get_file_size( debugf ) > maxlog )
300 slprintf( name, sizeof(name)-1, "%s.old", debugf );
301 (void)rename( debugf, name );
306 * Here's where we need to panic if dbf == NULL..
309 dbf = sys_fopen( "/dev/console", "w" );
311 DEBUG(0,("check_log_size: open of debug file %s failed - using console.\n",
315 * We cannot continue without a debug file handle.
321 } /* check_log_size */
323 /* ************************************************************************** **
324 * Write an debug message on the debugfile.
325 * This is called by dbghdr() and format_debug_text().
326 * ************************************************************************** **
329 int Debug1( char *format_str, ... )
338 int old_errno = errno;
343 va_start( ap, format_str );
346 format_str = va_arg( ap, char * );
349 (void)vfprintf( dbf, format_str, ap );
356 if( !lp_syslog_only() )
361 mode_t oldumask = umask( 022 );
364 dbf = sys_fopen( debugf, "a" );
366 dbf = sys_fopen( debugf, "w" );
367 (void)umask( oldumask );
381 if( syslog_level < lp_syslog() )
383 /* map debug levels to syslog() priorities
384 * note that not all DEBUG(0, ...) calls are
387 static int priority_map[] = {
396 if( syslog_level >= ( sizeof(priority_map) / sizeof(priority_map[0]) )
398 priority = LOG_DEBUG;
400 priority = priority_map[syslog_level];
403 va_start( ap, format_str );
406 format_str = va_arg( ap, char * );
408 vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
412 syslog( priority, "%s", msgbuf );
419 if( !lp_syslog_only() )
423 va_start( ap, format_str );
426 format_str = va_arg( ap, char * );
429 (void)vfprintf( dbf, format_str, ap );
441 /* ************************************************************************** **
442 * Print the buffer content via Debug1(), then reset the buffer.
447 * ************************************************************************** **
449 static void bufr_print( void )
451 format_bufr[format_pos] = '\0';
452 (void)Debug1( "%s", format_bufr );
456 /* ************************************************************************** **
457 * Format the debug message text.
459 * Input: msg - Text to be added to the "current" debug message text.
463 * Notes: The purpose of this is two-fold. First, each call to syslog()
464 * (used by Debug1(), see above) generates a new line of syslog
465 * output. This is fixed by storing the partial lines until the
466 * newline character is encountered. Second, printing the debug
467 * message lines when a newline is encountered allows us to add
468 * spaces, thus indenting the body of the message and making it
471 * ************************************************************************** **
473 static void format_debug_text( char *msg )
476 BOOL timestamp = (!stdout_logging && (lp_timestamp_logs() ||
479 for( i = 0; msg[i]; i++ )
481 /* Indent two spaces at each new line. */
482 if(timestamp && 0 == format_pos)
484 format_bufr[0] = format_bufr[1] = ' ';
488 /* If there's room, copy the character to the format buffer. */
489 if( format_pos < FORMAT_BUFR_MAX )
490 format_bufr[format_pos++] = msg[i];
492 /* If a newline is encountered, print & restart. */
496 /* If the buffer is full dump it out, reset it, and put out a line
497 * continuation indicator.
499 if( format_pos >= FORMAT_BUFR_MAX )
502 (void)Debug1( " +>\n" );
506 /* Just to be safe... */
507 format_bufr[format_pos] = '\0';
508 } /* format_debug_text */
510 /* ************************************************************************** **
511 * Flush debug output, including the format buffer content.
516 * ************************************************************************** **
518 void dbgflush( void )
525 /* ************************************************************************** **
526 * Print a Debug Header.
528 * Input: level - Debug level of the message (not the system-wide debug
530 * file - Pointer to a string containing the name of the file
531 * from which this function was called, or an empty string
532 * if the __FILE__ macro is not implemented.
533 * func - Pointer to a string containing the name of the function
534 * from which this function was called, or an empty string
535 * if the __FUNCTION__ macro is not implemented.
536 * line - line number of the call to dbghdr, assuming __LINE__
539 * Output: Always True. This makes it easy to fudge a call to dbghdr()
540 * in a macro, since the function can be called as part of a test.
541 * Eg: ( (level <= DEBUGLEVEL) && (dbghdr(level,"",line)) )
543 * Notes: This function takes care of setting syslog_level.
545 * ************************************************************************** **
548 BOOL dbghdr( int level, char *file, char *func, int line )
550 /* Ensure we don't lose any real errno value. */
551 int old_errno = errno;
554 /* This is a fudge. If there is stuff sitting in the format_bufr, then
555 * the *right* thing to do is to call
556 * format_debug_text( "\n" );
557 * to write the remainder, and then proceed with the new header.
558 * Unfortunately, there are several places in the code at which
559 * the DEBUG() macro is used to build partial lines. That in mind,
560 * we'll work under the assumption that an incomplete line indicates
561 * that a new header is *not* desired.
567 /* Set syslog_level. */
568 syslog_level = level;
571 /* Don't print a header if we're logging to stdout. */
575 /* Print the header if timestamps are turned on. If parameters are
576 * not yet loaded, then default to timestamps on.
578 if( lp_timestamp_logs() || !(lp_loaded()) ) {
579 char header_str[200];
581 header_str[0] = '\0';
584 slprintf(header_str,sizeof(header_str)-1,", pid=%u",(unsigned int)sys_getpid());
586 if( lp_debug_uid()) {
587 size_t hs_len = strlen(header_str);
588 slprintf(header_str + hs_len,
589 sizeof(header_str) - 1 - hs_len,
590 ", effective(%u, %u), real(%u, %u)",
591 (unsigned int)geteuid(), (unsigned int)getegid(),
592 (unsigned int)getuid(), (unsigned int)getgid());
595 /* Print it all out at once to prevent split syslog output. */
596 (void)Debug1( "[%s, %d%s] %s:%s(%d)\n",
597 timestring(lp_debug_hires_timestamp()), level,
598 header_str, file, func, line );
605 /* ************************************************************************** **
606 * Add text to the body of the "current" debug message via the format buffer.
608 * Input: format_str - Format string, as used in printf(), et. al.
609 * ... - Variable argument list.
611 * ..or.. va_alist - Old style variable parameter list starting point.
613 * Output: Always True. See dbghdr() for more info, though this is not
614 * likely to be used in the same way.
616 * ************************************************************************** **
619 BOOL dbgtext( char *format_str, ... )
624 va_start( ap, format_str );
625 vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
628 format_debug_text( msgbuf );
634 BOOL dbgtext( va_alist )
642 format_str = va_arg( ap, char * );
643 vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
646 format_debug_text( msgbuf );
653 /* ************************************************************************** */