3 * Copyright 1998-2002 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.
25 #include <ac/stdlib.h>
42 /* Haven't yet written the non-threaded version */
43 fputs( "slurpd currently requires threads support\n", stderr );
49 /* initialize thread package */
50 ldap_pvt_thread_initialize();
53 * Create and initialize globals. init_globals() also initializes
54 * the main replication queue.
56 if (( sglob = init_globals()) == NULL ) {
57 fprintf( stderr, "Out of memory initializing globals\n" );
62 * Process command-line args and fill in globals.
64 if ( doargs( argc, argv, sglob ) < 0 ) {
69 * Read slapd config file and initialize Re (per-replica) structs.
71 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
73 "Errors encountered while processing config file \"%s\"\n",
74 sglob->slapd_configfile );
79 if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
80 fprintf( stderr, "TLS Initialization failed.\n" );
86 * Make sure our directory exists
88 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
89 perror(sglob->slurpd_rdir);
94 * Get any saved state information off the disk.
96 if ( sglob->st->st_read( sglob->st )) {
97 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
98 sglob->slurpd_status_file, 0, 0 );
103 * All readonly data should now be initialized.
104 * Check for any fatal error conditions before we get started
106 if ( sanity() < 0 ) {
107 exit( EXIT_FAILURE );
111 * Detach from the controlling terminal
112 * unless the -d flag is given or in one-shot mode.
115 if ( ! (sglob->no_detach || sglob->one_shot_mode) )
116 lutil_detach( 0, 0 );
120 * Start the main file manager thread (in fm.c).
122 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
123 0, fm, (void *) NULL ) != 0 )
126 LDAP_LOG (( "main", LDAP_LEVEL_ERR,
127 "main: file manager ldap_pvt_thread_create failed\n" ));
129 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
132 exit( EXIT_FAILURE );
137 * wait for fm to finish if in oneshot mode
139 if ( sglob->one_shot_mode ) {
140 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
144 * Start threads - one thread for each replica
146 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
147 start_replica_thread( sglob->replicas[ i ]);
151 * Wait for the fm thread to finish.
153 if ( !sglob->one_shot_mode ) {
154 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
158 * Wait for the replica threads to finish.
160 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
161 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
164 /* destroy the thread package */
165 ldap_pvt_thread_destroy();
168 LDAP_LOG (( "main", LDAP_LEVEL_RESULTS,
169 "main: slurpd terminated\n" ));
171 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
174 #endif /* !NO_THREADS */