2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
15 * main.c - main routine for slurpd.
27 extern int doargs( int, char **, Globals * );
29 extern int start_replica_thread( Ri * );
30 extern Globals *init_globals();
33 #if defined( HAVE_LWP )
34 extern void start_lwp_scheduler();
44 /* Haven't yet written the non-threaded version */
45 fputs( "slurpd currently requires threads support\n", stderr );
54 * Create and initialize globals. init_globals() also initializes
55 * the main replication queue.
57 if (( sglob = init_globals()) == NULL ) {
58 fprintf( stderr, "Out of memory initializing globals\n" );
63 * Process command-line args and fill in globals.
65 if ( doargs( argc, argv, sglob ) < 0 ) {
70 * Read slapd config file and initialize Re (per-replica) structs.
72 if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
74 "Errors encountered while processing config file \"%s\"\n",
75 sglob->slapd_configfile );
80 * Get any saved state information off the disk.
82 if ( sglob->st->st_read( sglob->st )) {
83 fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
84 sglob->slurpd_status_file, 0, 0 );
89 * All readonly data should now be initialized.
90 * Check for any fatal error conditions before we get started
97 * Detach from the controlling terminal, if debug level = 0,
98 * and if not in one-shot mode.
101 if (( ldap_debug == 0 ) && !sglob->one_shot_mode )
102 #else /* LDAP_DEBUG */
103 if ( !sglob->one_shot_mode )
104 #endif /* LDAP_DEBUG */
105 lutil_detach( 0, 0 );
107 #if defined( HAVE_LWP )
109 * Need to start a scheduler thread under SunOS 4
111 start_lwp_scheduler();
112 #endif /* HAVE_LWP */
116 * Start threads - one thread for each replica
118 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
119 start_replica_thread( sglob->replicas[ i ]);
123 * Start the main file manager thread (in fm.c).
125 pthread_attr_init( &attr );
127 #if !defined(HAVE_PTHREADS_D4)
128 /* POSIX_THREADS or compatible
129 * This is a draft 10 or standard pthreads implementation
131 if ( pthread_create( &(sglob->fm_tid), &attr, fm, (void *) NULL )
133 Debug( LDAP_DEBUG_ANY, "file manager pthread_create failed\n",
138 #else /* !PTHREADS_FINAL */
140 * This is a draft 4 or earlier pthreads implementation
142 if ( pthread_create( &(sglob->fm_tid), attr, fm, (void *) NULL )
144 Debug( LDAP_DEBUG_ANY, "file manager pthread_create failed\n",
149 #endif /* !PTHREADS_FINAL */
151 pthread_attr_destroy( &attr );
154 * Wait for the fm thread to finish.
156 #ifdef HAVE_PTHREADS_FINAL
157 pthread_join( sglob->fm_tid, (void *) NULL );
159 pthread_join( sglob->fm_tid, (void *) &status );
162 * Wait for the replica threads to finish.
164 for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
165 #ifdef HAVE_PTHREADS_FINAL
166 pthread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
168 pthread_join( sglob->replicas[ i ]->ri_tid, (void *) &status );
171 Debug( LDAP_DEBUG_ANY, "slurpd: terminating normally\n", 0, 0, 0 );
172 sglob->slurpd_shutdown = 1;
175 #endif /* !NO_THREADS */