Commit 2abbf678 authored by Ondřej Kuzník's avatar Ondřej Kuzník Committed by Quanah Gibson-Mount
Browse files

ITS#6949 Extract logging code so lloadd can also use it

parent 28b67541
......@@ -22,7 +22,8 @@ NT_SRCS = ../slapd/nt_svc.c
NT_OBJS = ../slapd/nt_svc.o ../../libraries/liblutil/slapdmsg.res
SRCS += main.c value.c \
../slapd/ch_malloc.c ../slapd/proxyp.c ../slapd/sl_malloc.c ../slapd/user.c
../slapd/ch_malloc.c ../slapd/logging.c ../slapd/proxyp.c \
../slapd/sl_malloc.c ../slapd/user.c
OBJS = $(patsubst %.c,%.o,$(SRCS)) $(@PLAT@_OBJS)
......
......@@ -379,6 +379,7 @@ main( int argc, char **argv )
slap_sl_mem_init();
(void) ldap_pvt_thread_initialize();
ldap_pvt_thread_mutex_init( &logfile_mutex );
serverName = lutil_progname( "lloadd", argc, argv );
......@@ -596,6 +597,7 @@ unhandled_option:;
if ( optind != argc ) goto unhandled_option;
ber_set_option( NULL, LBER_OPT_LOG_PRINT_FN, slap_debug_print );
ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug );
ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug );
ldif_debug = slap_debug;
......@@ -907,6 +909,7 @@ stop:
/* kludge, get symbols referenced */
ldap_tavl_free( NULL, NULL );
ldap_pvt_thread_mutex_destroy( &logfile_mutex );
MAIN_RETURN(rc);
}
......
......@@ -29,7 +29,7 @@ SRCS = main.c globals.c bconfig.c config.c daemon.c \
dn.c compare.c modify.c delete.c modrdn.c ch_malloc.c \
value.c ava.c bind.c unbind.c abandon.c filterentry.c \
phonetic.c acl.c str2filter.c aclparse.c init.c user.c \
lock.c controls.c extended.c passwd.c proxyp.c \
lock.c logging.c controls.c extended.c passwd.c proxyp.c \
schema.c schema_check.c schema_init.c schema_prep.c \
schemaparse.c ad.c at.c mr.c syntax.c oc.c saslauthz.c \
oidm.c starttls.c index.c sets.c referral.c root_dse.c \
......@@ -47,7 +47,7 @@ OBJS = main.o globals.o bconfig.o config.o daemon.o \
dn.o compare.o modify.o delete.o modrdn.o ch_malloc.o \
value.o ava.o bind.o unbind.o abandon.o filterentry.o \
phonetic.o acl.o str2filter.o aclparse.o init.o user.o \
lock.o controls.o extended.o passwd.o proxyp.o \
lock.o logging.o controls.o extended.o passwd.o proxyp.o \
schema.o schema_check.o schema_init.o schema_prep.o \
schemaparse.o ad.o at.o mr.o syntax.o oc.o saslauthz.o \
oidm.o starttls.o index.o sets.o referral.o root_dse.o \
......
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2021 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/errno.h>
#include <ac/param.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/unistd.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include "slap.h"
static char logfile_suffix[sizeof(".xx.gz")];
char logfile_path[MAXPATHLEN - sizeof(logfile_suffix) -1];
long logfile_fslimit;
int logfile_age, logfile_only, logfile_max;
ldap_pvt_thread_mutex_t logfile_mutex;
static off_t logfile_fsize;
static time_t logfile_fcreated;
static int logfile_fd;
static char logpaths[2][MAXPATHLEN];
static int logpathlen;
void
slap_debug_print( const char *data )
{
char prefix[sizeof("ssssssssssssssss.ffffffff 0xtttttttttttttttt ")];
struct iovec iov[2];
int rotate = 0;
#ifdef HAVE_CLOCK_GETTIME
struct timespec tv;
#define TS "%08x"
#define Tfrac tv.tv_nsec
#define gettime(tv) clock_gettime( CLOCK_REALTIME, tv )
#else
struct timeval tv;
#define TS "%05x"
#define Tfrac tv.tv_usec
#define gettime(tv) gettimeofday( tv, NULL )
#endif
gettime( &tv );
iov[0].iov_base = prefix;
iov[0].iov_len = sprintf( prefix, "%lx." TS " %p ",
(long)tv.tv_sec, (unsigned int)Tfrac, (void *)ldap_pvt_thread_self() );
iov[1].iov_base = (void *)data;
iov[1].iov_len = strlen( data );
if ( !logfile_only )
writev( 2, iov, 2 );
if ( logfile_fd ) {
int len = iov[0].iov_len + iov[1].iov_len;
if ( logfile_fslimit || logfile_age ) {
ldap_pvt_thread_mutex_lock( &logfile_mutex );
if ( logfile_fslimit && logfile_fsize + len > logfile_fslimit )
rotate = 1;
if ( logfile_age && tv.tv_sec - logfile_fcreated >= logfile_age )
rotate |= 2;
if ( rotate ) {
close( logfile_fd );
strcpy( logpaths[0]+logpathlen, ".tmp" );
rename( logfile_path, logpaths[0] );
logfile_open( logfile_path );
}
}
len = writev( logfile_fd, iov, 2 );
if ( len > 0 )
logfile_fsize += len;
if ( logfile_fslimit || logfile_age )
ldap_pvt_thread_mutex_unlock( &logfile_mutex );
}
if ( rotate ) {
int i;
for (i=logfile_max; i > 1; i--) {
sprintf( logpaths[0]+logpathlen, ".%02d", i );
sprintf( logpaths[1]+logpathlen, ".%02d", i-1 );
rename( logpaths[1], logpaths[0] );
}
sprintf( logpaths[0]+logpathlen, ".tmp" );
rename( logpaths[0], logpaths[1] );
}
}
void
logfile_close()
{
if ( logfile_fd ) {
close( logfile_fd );
logfile_fd = 0;
}
logfile_path[0] = '\0';
}
int
logfile_open( const char *path )
{
struct stat st;
int fd;
fd = open( path, O_CREAT|O_WRONLY, 0640 );
if ( fd < 0 )
return errno;
if ( fstat( fd, &st ) ) {
close( fd );
return errno;
}
if ( !logfile_path[0] ) {
logpathlen = strlen( path );
if ( logpathlen >= sizeof(logfile_path) )
return ENAMETOOLONG;
strcpy( logfile_path, path );
strcpy( logpaths[0], path );
strcpy( logpaths[1], path );
}
logfile_fsize = st.st_size;
logfile_fcreated = st.st_ctime; /* not strictly true but close enough */
logfile_fd = fd;
lseek( fd, 0, SEEK_END );
return 0;
}
const char *
logfile_name()
{
return logfile_path[0] ? logfile_path : NULL;
}
......@@ -35,10 +35,6 @@
#include <ac/wait.h>
#include <ac/errno.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include "slap.h"
#include "lutil.h"
#include "ldif.h"
......@@ -381,123 +377,6 @@ usage( char *name )
);
}
static char logfile_suffix[sizeof(".xx.gz")];
char logfile_path[MAXPATHLEN - sizeof(logfile_suffix) -1];
long logfile_fslimit;
int logfile_age, logfile_only, logfile_max;
static ldap_pvt_thread_mutex_t logfile_mutex;
static off_t logfile_fsize;
static time_t logfile_fcreated;
static int logfile_fd;
static char logpaths[2][MAXPATHLEN];
static int logpathlen;
typedef void (BER_logger)(const char *buf);
static BER_logger *ber_logger;
static void debug_print( const char *data )
{
char prefix[sizeof("ssssssssssssssss.ffffffff 0xtttttttttttttttt ")];
struct iovec iov[2];
int rotate = 0;
#ifdef HAVE_CLOCK_GETTIME
struct timespec tv;
#define TS "%08x"
#define Tfrac tv.tv_nsec
#define gettime(tv) clock_gettime( CLOCK_REALTIME, tv )
#else
struct timeval tv;
#define TS "%05x"
#define Tfrac tv.tv_usec
#define gettime(tv) gettimeofday( tv, NULL )
#endif
gettime( &tv );
iov[0].iov_base = prefix;
iov[0].iov_len = sprintf( prefix, "%lx." TS " %p ",
(long)tv.tv_sec, (unsigned int)Tfrac, (void *)ldap_pvt_thread_self() );
iov[1].iov_base = (void *)data;
iov[1].iov_len = strlen( data );
if ( !logfile_only )
writev( 2, iov, 2 );
if ( logfile_fd ) {
int len = iov[0].iov_len + iov[1].iov_len;
if ( logfile_fslimit || logfile_age ) {
ldap_pvt_thread_mutex_lock( &logfile_mutex );
if ( logfile_fslimit && logfile_fsize + len > logfile_fslimit )
rotate = 1;
if ( logfile_age && tv.tv_sec - logfile_fcreated >= logfile_age )
rotate |= 2;
if ( rotate ) {
close( logfile_fd );
strcpy( logpaths[0]+logpathlen, ".tmp" );
rename( logfile_path, logpaths[0] );
logfile_open( logfile_path );
}
}
len = writev( logfile_fd, iov, 2 );
if ( len > 0 )
logfile_fsize += len;
if ( logfile_fslimit || logfile_age )
ldap_pvt_thread_mutex_unlock( &logfile_mutex );
}
if ( rotate ) {
int i;
for (i=logfile_max; i > 1; i--) {
sprintf( logpaths[0]+logpathlen, ".%02d", i );
sprintf( logpaths[1]+logpathlen, ".%02d", i-1 );
rename( logpaths[1], logpaths[0] );
}
sprintf( logpaths[0]+logpathlen, ".tmp" );
rename( logpaths[0], logpaths[1] );
}
}
void logfile_close()
{
if ( logfile_fd ) {
close( logfile_fd );
logfile_fd = 0;
}
logfile_path[0] = '\0';
}
int logfile_open( const char *path )
{
struct stat st;
int fd;
fd = open( path, O_CREAT|O_WRONLY, 0640 );
if ( fd < 0 )
return errno;
if ( fstat( fd, &st )) {
close( fd );
return errno;
}
if ( !logfile_path[0] ) {
logpathlen = strlen( path );
if ( logpathlen >= sizeof(logfile_path) )
return ENAMETOOLONG;
strcpy( logfile_path, path );
strcpy( logpaths[0], path );
strcpy( logpaths[1], path );
}
logfile_fsize = st.st_size;
logfile_fcreated = st.st_ctime; /* not strictly true but close enough */
logfile_fd = fd;
lseek( fd, 0, SEEK_END );
return 0;
}
const char *logfile_name()
{
return logfile_path[0] ? logfile_path : NULL;
}
#ifdef HAVE_NT_SERVICE_MANAGER
void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
#else
......@@ -840,8 +719,7 @@ unhandled_option:;
if ( optind != argc )
goto unhandled_option;
ber_get_option(NULL, LBER_OPT_LOG_PRINT_FN, &ber_logger);
ber_set_option(NULL, LBER_OPT_LOG_PRINT_FN, debug_print);
ber_set_option(NULL, LBER_OPT_LOG_PRINT_FN, slap_debug_print);
ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
ldif_debug = slap_debug;
......
......@@ -1241,6 +1241,19 @@ LDAP_SLAPD_F (FILE *) lock_fopen LDAP_P(( const char *fname,
const char *type, FILE **lfp ));
LDAP_SLAPD_F (int) lock_fclose LDAP_P(( FILE *fp, FILE *lfp ));
/*
* logging.c
*/
LDAP_SLAPD_F (void) slap_debug_print LDAP_P(( const char *data ));
LDAP_SLAPD_F (int) logfile_open LDAP_P(( const char *path ));
LDAP_SLAPD_F (void) logfile_close LDAP_P(( void ));
LDAP_SLAPD_F (const char *) logfile_name LDAP_P(( void ));
LDAP_SLAPD_V(ldap_pvt_thread_mutex_t) logfile_mutex;
LDAP_SLAPD_V(int) logfile_age;
LDAP_SLAPD_V(int) logfile_only;
LDAP_SLAPD_V(int) logfile_max;
LDAP_SLAPD_V(long) logfile_fslimit;
/*
* main.c
*/
......@@ -1254,16 +1267,6 @@ LDAP_SLAPD_F (int)
parse_debug_unknowns LDAP_P(( char **unknowns, int *levelp ));
LDAP_SLAPD_F (void)
slap_check_unknown_level LDAP_P(( char *levelstr, int level ));
LDAP_SLAPD_F (int)
logfile_open LDAP_P(( const char *path ));
LDAP_SLAPD_F (void)
logfile_close LDAP_P(( void ));
LDAP_SLAPD_F (const char *)
logfile_name LDAP_P(( void ));
LDAP_SLAPD_V(int) logfile_age;
LDAP_SLAPD_V(int) logfile_only;
LDAP_SLAPD_V(int) logfile_max;
LDAP_SLAPD_V(long) logfile_fslimit;
/*
* matchedValues.c
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment