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>
41 /* Haven't yet written the non-threaded version */
42 fputs( "slurpd currently requires threads support\n", stderr );
48 /* initialize thread package */
49 ldap_pvt_thread_initialize();
52 * Create and initialize globals. init_globals() also initializes
53 * the main replication queue.
55 if (( sglob = init_globals()) == NULL ) {
56 fprintf( stderr, "Out of memory initializing globals\n" );
61 * Process command-line args and fill in globals.
63 if ( doargs( argc, argv, sglob ) < 0 ) {
68 * Read slapd config file and initialize Re (per-replica) structs.
70 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
72 "Errors encountered while processing config file \"%s\"\n",
73 sglob->slapd_configfile );
78 if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
79 fprintf( stderr, "TLS Initialization failed.\n" );
85 * Make sure our directory exists
87 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
88 perror(sglob->slurpd_rdir);
93 * Get any saved state information off the disk.
95 if ( sglob->st->st_read( sglob->st )) {
96 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
97 sglob->slurpd_status_file, 0, 0 );
102 * All readonly data should now be initialized.
103 * Check for any fatal error conditions before we get started
105 if ( sanity() < 0 ) {
106 exit( EXIT_FAILURE );
110 * Detach from the controlling terminal
111 * unless the -d flag is given or in one-shot mode.
113 if ( ! (sglob->no_detach || sglob->one_shot_mode) )
114 lutil_detach( 0, 0 );
117 * Start the main file manager thread (in fm.c).
119 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
120 0, fm, (void *) NULL ) != 0 )
122 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
124 exit( EXIT_FAILURE );
129 * wait for fm to finish if in oneshot mode
131 if ( sglob->one_shot_mode ) {
132 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
136 * Start threads - one thread for each replica
138 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
139 start_replica_thread( sglob->replicas[ i ]);
143 * Wait for the fm thread to finish.
145 if ( !sglob->one_shot_mode ) {
146 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
150 * Wait for the replica threads to finish.
152 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
153 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
156 /* destroy the thread package */
157 ldap_pvt_thread_destroy();
159 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
161 #endif /* !NO_THREADS */