2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP). Additional significant contributors
34 * main.c - main routine for slurpd.
41 #include <ac/stdlib.h>
42 #include <ac/unistd.h>
50 #ifdef HAVE_NT_SERVICE_MANAGER
51 #define MAIN_RETURN(x) return
52 #define SERVICE_EXIT( e, n ) do { \
53 if ( is_NT_Service ) { \
54 lutil_ServiceStatus.dwWin32ExitCode = (e); \
55 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
59 #define SERVICE_EXIT( e, n )
60 #define MAIN_RETURN(x) return(x)
63 #ifndef HAVE_MKVERSION
64 const char Versionstr[] =
65 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Replicator (slurpd)";
68 #ifdef HAVE_NT_SERVICE_MANAGER
69 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
71 int main( int argc, char **argv )
75 /* Haven't yet written the non-threaded version */
76 fputs( "slurpd currently requires threads support\n", stderr );
82 /* initialize thread package */
83 ldap_pvt_thread_initialize();
86 * Create and initialize globals. init_globals() also initializes
87 * the main replication queue.
89 if (( sglob = init_globals()) == NULL ) {
90 fprintf( stderr, "Out of memory initializing globals\n" );
91 SERVICE_EXIT( ERROR_NOT_ENOUGH_MEMORY, 0 );
96 #ifdef HAVE_NT_SERVICE_MANAGER
100 char *regService = NULL;
102 if ( is_NT_Service ) {
103 sglob->serverName = argv[0];
104 lutil_CommenceStartupProcessing( sglob->serverName, slurp_set_shutdown );
105 if ( strcmp(sglob->serverName, SERVICE_NAME) )
106 regService = sglob->serverName;
109 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
113 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
116 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
117 if ( newConfigFile != NULL )
119 sglob->slapd_configfile = newConfigFile;
120 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
127 * Process command-line args and fill in globals.
129 if ( doargs( argc, argv, sglob ) < 0 ) {
130 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
135 if ( sglob->version ) {
136 fprintf(stderr, "%s\n", Versionstr);
137 if (sglob->version > 1 ) {
143 Debug ( LDAP_DEBUG_ANY, "%s\n", Versionstr, 0, 0 );
146 * Read slapd config file and initialize Re (per-replica) structs.
148 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
150 "Errors encountered while processing config file \"%s\"\n",
151 sglob->slapd_configfile );
152 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
158 if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx( 0 ) ) {
160 /* See if we actually need TLS */
161 for ( i=0; i < sglob->num_replicas; i++ ) {
162 if ( sglob->replicas[i]->ri_tls || ( sglob->replicas[i]->ri_uri &&
163 !strncmp( sglob->replicas[i]->ri_uri, "ldaps:", 6 ))) {
169 fprintf( stderr, "TLS Initialization failed.\n" );
170 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
177 * Make sure our directory exists
179 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
180 perror(sglob->slurpd_rdir);
181 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
187 * Get any saved state information off the disk.
189 if ( sglob->st->st_read( sglob->st )) {
190 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
191 sglob->slurpd_status_file );
192 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
198 * All readonly data should now be initialized.
199 * Check for any fatal error conditions before we get started
201 if ( sanity() < 0 ) {
202 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
209 * Detach from the controlling terminal
210 * unless the -d flag is given or in one-shot mode.
213 if ( ! (sglob->no_detach || sglob->one_shot_mode) ) {
214 lutil_detach( 0, 0 );
219 * don't open pid/args file in one-shot mode (ITS#4152)
221 * bail out if files were specified but cannot be opened (ITS#4074)
223 if ( !sglob->one_shot_mode) {
224 if ( slurpd_pid_file != NULL ) {
225 FILE *fp = fopen( slurpd_pid_file, "w" );
228 int save_errno = errno;
230 fprintf( stderr, "unable to open pid file "
233 save_errno, strerror( save_errno ) );
235 free( slurpd_pid_file );
236 slurpd_pid_file = NULL;
242 fprintf( fp, "%d\n", (int) getpid() );
246 if ( slurpd_args_file != NULL ) {
247 FILE *fp = fopen( slurpd_args_file, "w" );
250 int save_errno = errno;
252 fprintf( stderr, "unable to open args file "
255 save_errno, strerror( save_errno ) );
257 free( slurpd_args_file );
258 slurpd_pid_file = NULL;
264 for ( i = 0; i < argc; i++ ) {
265 fprintf( fp, "%s ", argv[i] );
272 if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
273 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
278 #ifdef HAVE_NT_EVENT_LOG
279 if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
283 * Start the main file manager thread (in fm.c).
285 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
286 0, fm, (void *) NULL ) != 0 )
288 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
290 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
297 * wait for fm to finish if in oneshot mode
299 if ( sglob->one_shot_mode ) {
300 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
304 * Start threads - one thread for each replica
306 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
307 start_replica_thread( sglob->replicas[ i ]);
310 #ifdef HAVE_NT_SERVICE_MANAGER
311 if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
315 * Wait for the fm thread to finish.
317 if ( !sglob->one_shot_mode ) {
318 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
322 * Wait for the replica threads to finish.
324 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
325 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
329 #ifdef HAVE_NT_SERVICE_MANAGER
331 ldap_pvt_thread_cond_destroy( &started_event );
332 lutil_LogStoppedEvent( sglob->serverName );
333 lutil_ReportShutdownComplete();
336 /* destroy the thread package */
337 ldap_pvt_thread_destroy();
340 ldap_pvt_tls_destroy();
343 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
345 if ( slurpd_pid_file != NULL ) {
346 unlink( slurpd_pid_file );
348 if ( slurpd_args_file != NULL ) {
349 unlink( slurpd_args_file );
354 #endif /* !NO_THREADS */