X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslapd%2Fmain.c;h=4d91ee7847d095323e15df0467a5209321b7a9bf;hb=a6883d270464bb74e86141435dc4324e5a5a6b6a;hp=01930b970910c9c1ad6cd9a8bf6939af58baed05;hpb=42e0d83cb3a1a1c5b25183f1ab74ce7edbe25de7;p=openldap diff --git a/servers/slapd/main.c b/servers/slapd/main.c index 01930b9709..4d91ee7847 100644 --- a/servers/slapd/main.c +++ b/servers/slapd/main.c @@ -1,89 +1,89 @@ -#include -#include -#include -#include -#include -#include -#include -#include #include "portable.h" -#include "slap.h" -#include "ldapconfig.h" -extern void daemon(); -extern int lber_debug; +#include -extern char Versionstr[]; +#include +#include +#include +#include +#include + +#include "ldapconfig.h" +#include "slap.h" +#include "lutil.h" /* Get lutil_detach() */ /* - * read-only global variables or variables only written by the listener - * thread (after they are initialized) - no need to protect them with a mutex. - */ -int ldap_debug; -#ifdef LDAP_DEBUG -int ldap_syslog = LDAP_DEBUG_STATS; -#else -int ldap_syslog; -#endif -int ldap_syslog_level = LOG_DEBUG; -int udp; -int slapd_shutdown; -char *default_referral; -char *configfile; -time_t starttime; -pthread_t listener_tid; -int g_argc; -char **g_argv; -/* - * global variables that need mutex protection - */ -time_t currenttime; -pthread_mutex_t currenttime_mutex; -int active_threads; -pthread_mutex_t active_threads_mutex; -pthread_mutex_t new_conn_mutex; -long ops_initiated; -long ops_completed; -int num_conns; -pthread_mutex_t ops_mutex; -long num_entries_sent; -long num_bytes_sent; -pthread_mutex_t num_sent_mutex; -/* - * these mutexes must be used when calling the entry2str() - * routine since it returns a pointer to static data. + * when more than one slapd is running on one machine, each one might have + * it's own LOCAL for syslogging and must have its own pid/args files */ -pthread_mutex_t entry2str_mutex; -pthread_mutex_t replog_mutex; -#ifndef sunos5 -pthread_mutex_t regex_mutex; -#endif -static -usage( name ) - char *name; +#ifdef LOG_LOCAL4 + +#define DEFAULT_SYSLOG_USER LOG_LOCAL4 + +typedef struct _str2intDispatch { + + char *stringVal; + int abbr; + int intVal; + +} STRDISP, *STRDISP_P; + + +/* table to compute syslog-options to integer */ +static STRDISP syslog_types[] = { + + { "LOCAL0", 6, LOG_LOCAL0 }, + { "LOCAL1", 6, LOG_LOCAL1 }, + { "LOCAL2", 6, LOG_LOCAL2 }, + { "LOCAL3", 6, LOG_LOCAL3 }, + { "LOCAL4", 6, LOG_LOCAL4 }, + { "LOCAL5", 6, LOG_LOCAL5 }, + { "LOCAL6", 6, LOG_LOCAL6 }, + { "LOCAL7", 6, LOG_LOCAL7 }, + NULL + +}; + +static int cnvt_str2int(); + +#endif /* LOG_LOCAL4 */ + + +static void +usage( char *name ) { - fprintf( stderr, "usage: %s [-d debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]\n", name ); + fprintf( stderr, "usage: %s [-d ?|debuglevel] [-f configfile] [-p portnumber] [-s sysloglevel]", name ); +#ifdef LOG_LOCAL4 + fprintf( stderr, " [-l sysloguser]" ); +#endif + fprintf( stderr, "\n" ); } -main( argc, argv ) - int argc; - char **argv; +int +main( int argc, char **argv ) { int i; int inetd = 0; int port; - char *myname; + int udp; Backend *be = NULL; FILE *fp = NULL; - extern char *optarg; + char *configfile; + char *serverName; +#ifdef LOG_LOCAL4 + char *optstring = "d:f:ip:s:ul:"; + int syslogUser = DEFAULT_SYSLOG_USER; +#else + char *optstring = "d:f:ip:s:u"; +#endif configfile = SLAPD_DEFAULT_CONFIGFILE; port = LDAP_PORT; g_argc = argc; g_argv = argv; - while ( (i = getopt( argc, argv, "d:f:ip:s:u" )) != EOF ) { + while ( (i = getopt( argc, argv, optstring )) != EOF ) { switch ( i ) { #ifdef LDAP_DEBUG case 'd': /* turn on debugging */ @@ -105,19 +105,19 @@ main( argc, argv ) LDAP_DEBUG_CONFIG ); printf( "\tLDAP_DEBUG_ACL\t\t%d\n", LDAP_DEBUG_ACL ); - printf( "\tLDAP_DEBUG_STATS\t\t%d\n", + printf( "\tLDAP_DEBUG_STATS\t%d\n", LDAP_DEBUG_STATS ); - printf( "\tLDAP_DEBUG_STATS2\t\t%d\n", + printf( "\tLDAP_DEBUG_STATS2\t%d\n", LDAP_DEBUG_STATS2 ); - printf( "\tLDAP_DEBUG_SHELL\t\t%d\n", + printf( "\tLDAP_DEBUG_SHELL\t%d\n", LDAP_DEBUG_SHELL ); - printf( "\tLDAP_DEBUG_PARSE\t\t%d\n", + printf( "\tLDAP_DEBUG_PARSE\t%d\n", LDAP_DEBUG_PARSE ); printf( "\tLDAP_DEBUG_ANY\t\t%d\n", LDAP_DEBUG_ANY ); exit( 0 ); } else { - ldap_debug = atoi( optarg ); + ldap_debug |= atoi( optarg ); lber_debug = (ldap_debug & LDAP_DEBUG_BER); } break; @@ -129,7 +129,7 @@ main( argc, argv ) #endif case 'f': /* read config file */ - configfile = strdup( optarg ); + configfile = ch_strdup( optarg ); break; case 'i': /* run from inetd */ @@ -144,6 +144,13 @@ main( argc, argv ) ldap_syslog = atoi( optarg ); break; +#ifdef LOG_LOCAL4 + case 'l': /* set syslog local user */ + syslogUser = cnvt_str2int( optarg, syslog_types, + DEFAULT_SYSLOG_USER ); + break; +#endif + case 'u': /* do udp */ udp = 1; break; @@ -156,43 +163,55 @@ main( argc, argv ) Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 ); - if ( (myname = strrchr( argv[0], '/' )) == NULL ) { - myname = strdup( argv[0] ); + if ( (serverName = strrchr( argv[0], '/' )) == NULL ) { + serverName = ch_strdup( argv[0] ); } else { - myname = strdup( myname + 1 ); + serverName = ch_strdup( serverName + 1 ); } if ( ! inetd ) { /* pre-open config file before detach in case it is a relative path */ fp = fopen( configfile, "r" ); - detach(); +#ifdef LDAP_DEBUG + lutil_detach( ldap_debug, 0 ); +#else + lutil_detach( 0, 0 ); +#endif } + #ifdef LOG_LOCAL4 - openlog( myname, OPENLOG_OPTIONS, LOG_LOCAL4 ); + openlog( serverName, OPENLOG_OPTIONS, syslogUser ); #else - openlog( myname, OPENLOG_OPTIONS ); + openlog( serverName, OPENLOG_OPTIONS ); #endif init(); read_config( configfile, &be, fp ); if ( ! inetd ) { - pthread_attr_t attr; int status; + (void) SIGNAL( SIGPIPE, SIG_IGN ); + (void) SIGNAL( LDAP_SIGUSR1, slap_do_nothing ); + (void) SIGNAL( LDAP_SIGUSR2, slap_set_shutdown ); + (void) SIGNAL( SIGTERM, slap_set_shutdown ); + (void) SIGNAL( SIGINT, slap_set_shutdown ); + (void) SIGNAL( SIGHUP, slap_set_shutdown ); + time( &starttime ); - pthread_attr_init( &attr ); - pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ); - if ( pthread_create( &listener_tid, attr, (void *) daemon, - (void *) port ) != 0 ) { + if ( status = ldap_pvt_thread_create( &listener_tid, 0, + slapd_daemon, (void *) port ) != 0 ) + { Debug( LDAP_DEBUG_ANY, - "listener pthread_create failed\n", 0, 0, 0 ); + "listener ldap_pvt_thread_create failed (%d)\n", status, 0, 0 ); exit( 1 ); } - pthread_attr_destroy( &attr ); - pthread_join( listener_tid, (void *) &status ); - pthread_exit( 0 ); + + ldap_pvt_thread_join( listener_tid, (void *) NULL ); + + return 0; + } else { Connection c; Operation *o; @@ -204,6 +223,7 @@ main( argc, argv ) struct hostent *hp; c.c_dn = NULL; + c.c_cdn = NULL; c.c_ops = NULL; c.c_sb.sb_sd = 0; c.c_sb.sb_options = 0; @@ -211,9 +231,9 @@ main( argc, argv ) c.c_sb.sb_ber.ber_buf = NULL; c.c_sb.sb_ber.ber_ptr = NULL; c.c_sb.sb_ber.ber_end = NULL; - pthread_mutex_init( &c.c_dnmutex, pthread_mutexattr_default ); - pthread_mutex_init( &c.c_opsmutex, pthread_mutexattr_default ); - pthread_mutex_init( &c.c_pdumutex, pthread_mutexattr_default ); + ldap_pvt_thread_mutex_init( &c.c_dnmutex ); + ldap_pvt_thread_mutex_init( &c.c_opsmutex ); + ldap_pvt_thread_mutex_init( &c.c_pdumutex ); #ifdef notdefcldap c.c_sb.sb_addrs = (void **) saddrlist; c.c_sb.sb_fromaddr = &faddr; @@ -221,7 +241,7 @@ main( argc, argv ) #endif flen = sizeof(from); if ( getpeername( 0, (struct sockaddr *) &from, &flen ) == 0 ) { -#ifdef REVERSE_LOOKUP +#ifdef SLAPD_RLOOKUPS hp = gethostbyaddr( (char *) &(from.sin_addr.s_addr), sizeof(from.sin_addr.s_addr), AF_INET ); #else @@ -233,7 +253,7 @@ main( argc, argv ) inet_ntoa( from.sin_addr ), 0 ); c.c_addr = inet_ntoa( from.sin_addr ); - c.c_domain = strdup( hp == NULL ? "" : hp->h_name ); + c.c_domain = ch_strdup( hp == NULL ? "" : hp->h_name ); } else { Debug( LDAP_DEBUG_ARGS, "connection from unknown\n", 0, 0, 0 ); @@ -242,27 +262,28 @@ main( argc, argv ) ber_init( &ber, 0 ); while ( (tag = ber_get_next( &c.c_sb, &len, &ber )) == LDAP_TAG_MESSAGE ) { - pthread_mutex_lock( ¤ttime_mutex ); + ldap_pvt_thread_mutex_lock( ¤ttime_mutex ); time( ¤ttime ); - pthread_mutex_unlock( ¤ttime_mutex ); + ldap_pvt_thread_mutex_unlock( ¤ttime_mutex ); if ( (tag = ber_get_int( &ber, &msgid )) != LDAP_TAG_MSGID ) { /* log and send error */ Debug( LDAP_DEBUG_ANY, - "ber_get_int returns 0x%x\n", tag, 0, 0 ); - return; + "ber_get_int returns 0x%lx\n", tag, 0, 0 ); + ber_free( &ber, 1 ); + return 1; } if ( (tag = ber_peek_tag( &ber, &len )) == LBER_ERROR ) { /* log, close and send error */ Debug( LDAP_DEBUG_ANY, - "ber_peek_tag returns 0x%x\n", tag, 0, 0 ); + "ber_peek_tag returns 0x%lx\n", tag, 0, 0 ); ber_free( &ber, 1 ); close( c.c_sb.sb_sd ); c.c_sb.sb_sd = -1; - return; + return 1; } connection_activity( &c ); @@ -270,4 +291,39 @@ main( argc, argv ) ber_free( &ber, 1 ); } } + return 1; } + + +#ifdef LOG_LOCAL4 + +/* + * Convert a string to an integer by means of a dispatcher table + * if the string is not in the table return the default + */ + +static int +cnvt_str2int (stringVal, dispatcher, defaultVal) +char *stringVal; +STRDISP_P dispatcher; +int defaultVal; +{ + int retVal = defaultVal; + STRDISP_P disp; + + for (disp = dispatcher; disp->stringVal; disp++) { + + if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) { + + retVal = disp->intVal; + break; + + } + } + + return (retVal); + +} /* cnvt_str2int */ + +#endif /* LOG_LOCAL4 */ +