3 * Copyright 1998-2000 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 * Make sure our directory exists
80 if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
81 perror(sglob->slurpd_rdir);
86 * Get any saved state information off the disk.
88 if ( sglob->st->st_read( sglob->st )) {
89 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
90 sglob->slurpd_status_file, 0, 0 );
95 * All readonly data should now be initialized.
96 * Check for any fatal error conditions before we get started
103 * Detach from the controlling terminal
104 * unless the -d flag is given or in one-shot mode.
106 if ( ! (sglob->no_detach || sglob->one_shot_mode) )
107 lutil_detach( 0, 0 );
110 * Start the main file manager thread (in fm.c).
112 if ( ldap_pvt_thread_create( &(sglob->fm_tid),
113 0, fm, (void *) NULL ) != 0 )
115 Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
117 exit( EXIT_FAILURE );
122 * wait for fm to finish if in oneshot mode
124 if ( sglob->one_shot_mode ) {
125 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
129 * Start threads - one thread for each replica
131 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
132 start_replica_thread( sglob->replicas[ i ]);
136 * Wait for the fm thread to finish.
138 if ( !sglob->one_shot_mode ) {
139 ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
143 * Wait for the replica threads to finish.
145 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
146 ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
149 /* destroy the thread package */
150 ldap_pvt_thread_destroy();
152 Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
154 #endif /* !NO_THREADS */