2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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>
51 #define mkdir(a,b) mkdir(a)
54 #ifdef HAVE_NT_SERVICE_MANAGER
55 #define MAIN_RETURN(x) return
56 #define SERVICE_EXIT( e, n ) do { \
57 if ( is_NT_Service ) { \
58 lutil_ServiceStatus.dwWin32ExitCode = (e); \
59 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
63 #define SERVICE_EXIT( e, n )
64 #define MAIN_RETURN(x) return(x)
67 #ifndef HAVE_MKVERSION
68 const char Versionstr[] =
69 OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Replicator (slurpd)";
72 #ifdef HAVE_NT_SERVICE_MANAGER
73 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
75 int main( int argc, char **argv )
79 /* Haven't yet written the non-threaded version */
80 fputs( "slurpd currently requires threads support\n", stderr );
86 /* initialize thread package */
87 ldap_pvt_thread_initialize();
90 * Create and initialize globals. init_globals() also initializes
91 * the main replication queue.
93 if (( sglob = init_globals()) == NULL ) {
94 fprintf( stderr, "Out of memory initializing globals\n" );
95 SERVICE_EXIT( ERROR_NOT_ENOUGH_MEMORY, 0 );
100 #ifdef HAVE_NT_SERVICE_MANAGER
104 char *regService = NULL;
106 if ( is_NT_Service ) {
107 sglob->serverName = argv[0];
108 lutil_CommenceStartupProcessing( sglob->serverName, slurp_set_shutdown );
109 if ( strcmp(sglob->serverName, SERVICE_NAME) )
110 regService = sglob->serverName;
113 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
118 lutil_log_initialize( argc, argv );
119 LDAP_LOG( SLURPD, INFO,
120 "main: new debug level from registry is: %d\n",
123 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
127 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
128 if ( newConfigFile != NULL )
130 sglob->slapd_configfile = newConfigFile;
132 LDAP_LOG( SLURPD, INFO,
133 "main: new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
135 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
143 * Process command-line args and fill in globals.
145 if ( doargs( argc, argv, sglob ) < 0 ) {
146 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
151 if ( sglob->version ) {
152 fprintf(stderr, "%s\n", Versionstr);
153 if (sglob->version > 1 ) {
160 LDAP_LOG( SLURPD, INFO, "%s\n", Versionstr, 0, 0 );
162 Debug ( LDAP_DEBUG_ANY, "%s\n", Versionstr, 0, 0 );
166 * Read slapd config file and initialize Re (per-replica) structs.
168 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
170 "Errors encountered while processing config file \"%s\"\n",
171 sglob->slapd_configfile );
172 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
178 if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
179 fprintf( stderr, "TLS Initialization failed.\n" );
180 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
187 * Make sure our directory exists
189 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
190 perror(sglob->slurpd_rdir);
191 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
197 * Get any saved state information off the disk.
199 if ( sglob->st->st_read( sglob->st )) {
200 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
201 sglob->slurpd_status_file );
202 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
208 * All readonly data should now be initialized.
209 * Check for any fatal error conditions before we get started
211 if ( sanity() < 0 ) {
212 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
219 * Detach from the controlling terminal
220 * unless the -d flag is given or in one-shot mode.
223 if ( ! (sglob->no_detach || sglob->one_shot_mode) ) {
224 lutil_detach( 0, 0 );
228 if ( slurpd_pid_file != NULL ) {
229 FILE *fp = fopen( slurpd_pid_file, "w" );
232 fprintf( fp, "%d\n", (int) getpid() );
236 free(slurpd_pid_file);
237 slurpd_pid_file = NULL;
241 if ( slurpd_args_file != NULL ) {
242 FILE *fp = fopen( slurpd_args_file, "w" );
245 for ( i = 0; i < argc; i++ ) {
246 fprintf( fp, "%s ", argv[i] );
251 free(slurpd_args_file);
252 slurpd_args_file = NULL;
256 if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
257 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
262 #ifdef HAVE_NT_EVENT_LOG
263 if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
267 * Start the main file manager thread (in fm.c).
269 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
270 0, fm, (void *) NULL ) != 0 )
273 LDAP_LOG ( SLURPD, ERR,
274 "main: file manager ldap_pvt_thread_create failed\n" , 0, 0, 0 );
276 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
279 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
286 * wait for fm to finish if in oneshot mode
288 if ( sglob->one_shot_mode ) {
289 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
293 * Start threads - one thread for each replica
295 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
296 start_replica_thread( sglob->replicas[ i ]);
299 #ifdef HAVE_NT_SERVICE_MANAGER
300 if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
304 * Wait for the fm thread to finish.
306 if ( !sglob->one_shot_mode ) {
307 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
311 * Wait for the replica threads to finish.
313 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
314 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
318 #ifdef HAVE_NT_SERVICE_MANAGER
320 ldap_pvt_thread_cond_destroy( &started_event );
321 lutil_LogStoppedEvent( sglob->serverName );
322 lutil_ReportShutdownComplete();
325 /* destroy the thread package */
326 ldap_pvt_thread_destroy();
329 ldap_pvt_tls_destroy();
333 LDAP_LOG ( SLURPD, RESULTS, "main: slurpd terminated\n", 0, 0, 0 );
335 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
338 if ( slurpd_pid_file != NULL ) {
339 unlink( slurpd_pid_file );
341 if ( slurpd_args_file != NULL ) {
342 unlink( slurpd_args_file );
347 #endif /* !NO_THREADS */