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.
114 if ( ! (sglob->no_detach || sglob->one_shot_mode) )
115 lutil_detach( 0, 0 );
118 * Start the main file manager thread (in fm.c).
120 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
121 0, fm, (void *) NULL ) != 0 )
123 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
125 exit( EXIT_FAILURE );
130 * wait for fm to finish if in oneshot mode
132 if ( sglob->one_shot_mode ) {
133 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
137 * Start threads - one thread for each replica
139 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
140 start_replica_thread( sglob->replicas[ i ]);
144 * Wait for the fm thread to finish.
146 if ( !sglob->one_shot_mode ) {
147 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
151 * Wait for the replica threads to finish.
153 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
154 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
157 /* destroy the thread package */
158 ldap_pvt_thread_destroy();
160 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
162 #endif /* !NO_THREADS */