2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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 #ifdef HAVE_NT_SERVICE_MANAGER
64 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
66 int main( int argc, char **argv )
70 /* Haven't yet written the non-threaded version */
71 fputs( "slurpd currently requires threads support\n", stderr );
77 /* initialize thread package */
78 ldap_pvt_thread_initialize();
81 * Create and initialize globals. init_globals() also initializes
82 * the main replication queue.
84 if (( sglob = init_globals()) == NULL ) {
85 fprintf( stderr, "Out of memory initializing globals\n" );
86 SERVICE_EXIT( ERROR_NOT_ENOUGH_MEMORY, 0 );
91 #ifdef HAVE_NT_SERVICE_MANAGER
95 char *regService = NULL;
97 if ( is_NT_Service ) {
98 sglob->serverName = argv[0];
99 lutil_CommenceStartupProcessing( sglob->serverName, slurp_set_shutdown );
100 if ( strcmp(sglob->serverName, SERVICE_NAME) )
101 regService = sglob->serverName;
104 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
109 lutil_log_initialize( argc, argv );
110 LDAP_LOG( SLURPD, INFO,
111 "main: new debug level from registry is: %d\n",
114 Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
118 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
119 if ( newConfigFile != NULL )
121 sglob->slapd_configfile = newConfigFile;
123 LDAP_LOG( SLURPD, INFO,
124 "main: new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
126 Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
134 * Process command-line args and fill in globals.
136 if ( doargs( argc, argv, sglob ) < 0 ) {
137 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
143 * Read slapd config file and initialize Re (per-replica) structs.
145 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
147 "Errors encountered while processing config file \"%s\"\n",
148 sglob->slapd_configfile );
149 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
155 if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
156 fprintf( stderr, "TLS Initialization failed.\n" );
157 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
164 * Make sure our directory exists
166 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
167 perror(sglob->slurpd_rdir);
168 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
174 * Get any saved state information off the disk.
176 if ( sglob->st->st_read( sglob->st )) {
177 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
178 sglob->slurpd_status_file );
179 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
185 * All readonly data should now be initialized.
186 * Check for any fatal error conditions before we get started
188 if ( sanity() < 0 ) {
189 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
196 * Detach from the controlling terminal
197 * unless the -d flag is given or in one-shot mode.
200 if ( ! (sglob->no_detach || sglob->one_shot_mode) ) {
201 lutil_detach( 0, 0 );
205 if ( slurpd_pid_file != NULL ) {
206 FILE *fp = fopen( slurpd_pid_file, "w" );
209 fprintf( fp, "%d\n", (int) getpid() );
213 free(slurpd_pid_file);
214 slurpd_pid_file = NULL;
218 if ( slurpd_args_file != NULL ) {
219 FILE *fp = fopen( slurpd_args_file, "w" );
222 for ( i = 0; i < argc; i++ ) {
223 fprintf( fp, "%s ", argv[i] );
228 free(slurpd_args_file);
229 slurpd_args_file = NULL;
233 if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
234 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
239 #ifdef HAVE_NT_EVENT_LOG
240 if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
244 * Start the main file manager thread (in fm.c).
246 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
247 0, fm, (void *) NULL ) != 0 )
250 LDAP_LOG ( SLURPD, ERR,
251 "main: file manager ldap_pvt_thread_create failed\n" , 0, 0, 0 );
253 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
256 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
263 * wait for fm to finish if in oneshot mode
265 if ( sglob->one_shot_mode ) {
266 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
270 * Start threads - one thread for each replica
272 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
273 start_replica_thread( sglob->replicas[ i ]);
276 #ifdef HAVE_NT_SERVICE_MANAGER
277 if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
281 * Wait for the fm thread to finish.
283 if ( !sglob->one_shot_mode ) {
284 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
288 * Wait for the replica threads to finish.
290 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
291 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
295 #ifdef HAVE_NT_SERVICE_MANAGER
297 ldap_pvt_thread_cond_destroy( &started_event );
298 lutil_LogStoppedEvent( sglob->serverName );
299 lutil_ReportShutdownComplete();
302 /* destroy the thread package */
303 ldap_pvt_thread_destroy();
306 ldap_pvt_tls_destroy();
310 LDAP_LOG ( SLURPD, RESULTS, "main: slurpd terminated\n", 0, 0, 0 );
312 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
315 if ( slurpd_pid_file != NULL ) {
316 unlink( slurpd_pid_file );
318 if ( slurpd_args_file != NULL ) {
319 unlink( slurpd_args_file );
324 #endif /* !NO_THREADS */