3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
20 * main.c - main routine for slurpd.
27 #include <ac/stdlib.h>
28 #include <ac/unistd.h>
36 #ifdef HAVE_NT_SERVICE_MANAGER
37 #define MAIN_RETURN(x) return
38 #define SERVICE_EXIT( e, n ) do { \
39 if ( is_NT_Service ) { \
40 lutil_ServiceStatus.dwWin32ExitCode = (e); \
41 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
45 #define SERVICE_EXIT( e, n )
46 #define MAIN_RETURN(x) return(x)
49 #ifdef HAVE_NT_SERVICE_MANAGER
50 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
52 int main( int argc, char **argv )
56 /* Haven't yet written the non-threaded version */
57 fputs( "slurpd currently requires threads support\n", stderr );
63 /* initialize thread package */
64 ldap_pvt_thread_initialize();
67 * Create and initialize globals. init_globals() also initializes
68 * the main replication queue.
70 if (( sglob = init_globals()) == NULL ) {
71 fprintf( stderr, "Out of memory initializing globals\n" );
72 SERVICE_EXIT( ERROR_NOT_ENOUGH_MEMORY, 0 );
77 #ifdef HAVE_NT_SERVICE_MANAGER
81 char *regService = NULL;
83 if ( is_NT_Service ) {
84 sglob->serverName = argv[0];
85 lutil_CommenceStartupProcessing( sglob->serverName, slurp_set_shutdown );
86 if ( strcmp(sglob->serverName, SERVICE_NAME) )
87 regService = sglob->serverName;
90 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
95 lutil_log_initialize( argc, argv );
96 LDAP_LOG( SLURPD, INFO,
97 "main: new debug level from registry is: %d\n",
100 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
104 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
105 if ( newConfigFile != NULL )
107 sglob->slapd_configfile = newConfigFile;
109 LDAP_LOG( SLURPD, INFO,
110 "main: new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
112 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
120 * Process command-line args and fill in globals.
122 if ( doargs( argc, argv, sglob ) < 0 ) {
123 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
129 * Read slapd config file and initialize Re (per-replica) structs.
131 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
133 "Errors encountered while processing config file \"%s\"\n",
134 sglob->slapd_configfile );
135 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
141 if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
142 fprintf( stderr, "TLS Initialization failed.\n" );
143 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
150 * Make sure our directory exists
152 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
153 perror(sglob->slurpd_rdir);
154 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
160 * Get any saved state information off the disk.
162 if ( sglob->st->st_read( sglob->st )) {
163 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
164 sglob->slurpd_status_file );
165 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
171 * All readonly data should now be initialized.
172 * Check for any fatal error conditions before we get started
174 if ( sanity() < 0 ) {
175 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
181 if ( slurpd_pid_file != NULL ) {
182 FILE *fp = fopen( slurpd_pid_file, "w" );
185 fprintf( fp, "%d\n", (int) getpid() );
189 free(slurpd_pid_file);
190 slurpd_pid_file = NULL;
194 if ( slurpd_args_file != NULL ) {
195 FILE *fp = fopen( slurpd_args_file, "w" );
198 for ( i = 0; i < argc; i++ ) {
199 fprintf( fp, "%s ", argv[i] );
204 free(slurpd_args_file);
205 slurpd_args_file = NULL;
210 * Detach from the controlling terminal
211 * unless the -d flag is given or in one-shot mode.
214 if ( ! (sglob->no_detach || sglob->one_shot_mode) )
215 lutil_detach( 0, 0 );
218 if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
219 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
224 #ifdef HAVE_NT_EVENT_LOG
225 if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
229 * Start the main file manager thread (in fm.c).
231 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
232 0, fm, (void *) NULL ) != 0 )
235 LDAP_LOG ( SLURPD, ERR,
236 "main: file manager ldap_pvt_thread_create failed\n" , 0, 0, 0 );
238 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
241 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
248 * wait for fm to finish if in oneshot mode
250 if ( sglob->one_shot_mode ) {
251 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
255 * Start threads - one thread for each replica
257 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
258 start_replica_thread( sglob->replicas[ i ]);
261 #ifdef HAVE_NT_SERVICE_MANAGER
262 if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
266 * Wait for the fm thread to finish.
268 if ( !sglob->one_shot_mode ) {
269 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
273 * Wait for the replica threads to finish.
275 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
276 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
280 #ifdef HAVE_NT_SERVICE_MANAGER
282 ldap_pvt_thread_cond_destroy( &started_event );
283 lutil_LogStoppedEvent( sglob->serverName );
284 lutil_ReportShutdownComplete();
287 /* destroy the thread package */
288 ldap_pvt_thread_destroy();
291 LDAP_LOG ( SLURPD, RESULTS, "main: slurpd terminated\n", 0, 0, 0 );
293 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
296 if ( slurpd_pid_file != NULL ) {
297 unlink( slurpd_pid_file );
299 if ( slurpd_args_file != NULL ) {
300 unlink( slurpd_args_file );
305 #endif /* !NO_THREADS */