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" );
117 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
120 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
121 if ( newConfigFile != NULL )
123 sglob->slapd_configfile = newConfigFile;
124 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
131 * Process command-line args and fill in globals.
133 if ( doargs( argc, argv, sglob ) < 0 ) {
134 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
139 if ( sglob->version ) {
140 fprintf(stderr, "%s\n", Versionstr);
141 if (sglob->version > 1 ) {
147 Debug ( LDAP_DEBUG_ANY, "%s\n", Versionstr, 0, 0 );
150 * Read slapd config file and initialize Re (per-replica) structs.
152 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
154 "Errors encountered while processing config file \"%s\"\n",
155 sglob->slapd_configfile );
156 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
162 if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
163 fprintf( stderr, "TLS Initialization failed.\n" );
164 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
171 * Make sure our directory exists
173 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
174 perror(sglob->slurpd_rdir);
175 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
181 * Get any saved state information off the disk.
183 if ( sglob->st->st_read( sglob->st )) {
184 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
185 sglob->slurpd_status_file );
186 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
192 * All readonly data should now be initialized.
193 * Check for any fatal error conditions before we get started
195 if ( sanity() < 0 ) {
196 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
203 * Detach from the controlling terminal
204 * unless the -d flag is given or in one-shot mode.
207 if ( ! (sglob->no_detach || sglob->one_shot_mode) ) {
208 lutil_detach( 0, 0 );
212 if ( slurpd_pid_file != NULL ) {
213 FILE *fp = fopen( slurpd_pid_file, "w" );
216 fprintf( fp, "%d\n", (int) getpid() );
220 free(slurpd_pid_file);
221 slurpd_pid_file = NULL;
225 if ( slurpd_args_file != NULL ) {
226 FILE *fp = fopen( slurpd_args_file, "w" );
229 for ( i = 0; i < argc; i++ ) {
230 fprintf( fp, "%s ", argv[i] );
235 free(slurpd_args_file);
236 slurpd_args_file = NULL;
240 if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
241 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
246 #ifdef HAVE_NT_EVENT_LOG
247 if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
251 * Start the main file manager thread (in fm.c).
253 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
254 0, fm, (void *) NULL ) != 0 )
256 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
258 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
265 * wait for fm to finish if in oneshot mode
267 if ( sglob->one_shot_mode ) {
268 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
272 * Start threads - one thread for each replica
274 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
275 start_replica_thread( sglob->replicas[ i ]);
278 #ifdef HAVE_NT_SERVICE_MANAGER
279 if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
283 * Wait for the fm thread to finish.
285 if ( !sglob->one_shot_mode ) {
286 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
290 * Wait for the replica threads to finish.
292 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
293 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
297 #ifdef HAVE_NT_SERVICE_MANAGER
299 ldap_pvt_thread_cond_destroy( &started_event );
300 lutil_LogStoppedEvent( sglob->serverName );
301 lutil_ReportShutdownComplete();
304 /* destroy the thread package */
305 ldap_pvt_thread_destroy();
308 ldap_pvt_tls_destroy();
311 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
313 if ( slurpd_pid_file != NULL ) {
314 unlink( slurpd_pid_file );
316 if ( slurpd_args_file != NULL ) {
317 unlink( slurpd_args_file );
322 #endif /* !NO_THREADS */