]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
Fixup bugs created by merge.
[openldap] / servers / slurpd / main.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  */
12
13
14 /* 
15  * main.c - main routine for slurpd.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include "slurp.h"
23 #include "globals.h"
24
25
26 extern int              doargs( int, char **, Globals * );
27 extern void             fm();
28 extern int              start_replica_thread( Ri * );
29 extern Globals          *init_globals();
30 extern int              sanity();
31
32 #if defined( HAVE_LWP )
33 extern void             start_lwp_scheduler();
34 #endif /* HAVE_LWP */
35
36 main(
37     int         argc,
38     char        **argv
39 )
40 {
41     pthread_attr_t      attr;
42     int                 status;
43     int                 i;
44
45 #ifdef NO_THREADS
46     /* Haven't yet written the non-threaded version */
47     fprintf( stderr, "slurpd currently requires threads support\n" );
48     exit( 1 );
49 #else
50
51     /* 
52      * Create and initialize globals.  init_globals() also initializes
53      * the main replication queue.
54      */
55     if (( sglob = init_globals()) == NULL ) {
56         fprintf( stderr, "Out of memory initializing globals\n" );
57         exit( 1 );
58     }
59
60     /*
61      * Process command-line args and fill in globals.
62      */
63     if ( doargs( argc, argv, sglob ) < 0 ) {
64         exit( 1 );
65     }
66
67     /*
68      * Read slapd config file and initialize Re (per-replica) structs.
69      */
70     if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
71         fprintf( stderr,
72                 "Errors encountered while processing config file \"%s\"\n",
73                 sglob->slapd_configfile );
74         exit( 1 );
75     }
76
77     /*
78      * Get any saved state information off the disk.
79      */
80     if ( sglob->st->st_read( sglob->st )) {
81         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
82                 sglob->slurpd_status_file, 0, 0 );
83         exit( 1 );
84     }
85
86     /*
87      * All readonly data should now be initialized. 
88      * Check for any fatal error conditions before we get started
89      */
90      if ( sanity() < 0 ) {
91         exit( 1 );
92     }
93
94     /*
95      * Detach from the controlling terminal, if debug level = 0,
96      * and if not in one-shot mode.
97      */
98 #ifdef LDAP_DEBUG
99     if (( ldap_debug == 0 )  && !sglob->one_shot_mode )
100 #else /* LDAP_DEBUG */
101     if ( !sglob->one_shot_mode )
102 #endif /* LDAP_DEBUG */
103         {
104                 detach();
105     }
106
107 #if defined( HAVE_LWP )
108     /*
109      * Need to start a scheduler thread under SunOS 4
110      */
111     start_lwp_scheduler();
112 #endif /* HAVE_LWP */
113
114
115     /*
116      * Start threads - one thread for each replica
117      */
118     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
119         start_replica_thread( sglob->replicas[ i ]);
120     }
121
122     /*
123      * Start the main file manager thread (in fm.c).
124      */
125     pthread_attr_init( &attr );
126
127 #if !defined(HAVE_PTHREADS_D4) && !defined(HAVE_DCE)
128     /* POSIX_THREADS or compatible
129      * This is a draft 10 or standard pthreads implementation
130      */
131     if ( pthread_create( &(sglob->fm_tid), &attr, fm, (void *) NULL )
132             != 0 ) {
133         Debug( LDAP_DEBUG_ANY, "file manager pthread_create failed\n",
134                 0, 0, 0 );
135         exit( 1 );
136
137     }
138 #else /* !PTHREADS_FINAL */
139     /*
140      * This is a draft 4 or earlier pthreads implementation
141      */
142     if ( pthread_create( &(sglob->fm_tid), attr, fm, (void *) NULL )
143             != 0 ) {
144         Debug( LDAP_DEBUG_ANY, "file manager pthread_create failed\n",
145                 0, 0, 0 );
146         exit( 1 );
147
148     }
149 #endif /* !PTHREADS_FINAL */
150
151     pthread_attr_destroy( &attr );
152
153     /*
154      * Wait for the fm thread to finish.
155      */
156 #ifdef HAVE_PTHREADS_FINAL
157     pthread_join( sglob->fm_tid, (void *) NULL );
158 #else
159     pthread_join( sglob->fm_tid, (void *) &status );
160 #endif
161     /*
162      * Wait for the replica threads to finish.
163      */
164     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
165 #ifdef HAVE_PTHREADS_FINAL
166         pthread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
167 #else
168         pthread_join( sglob->replicas[ i ]->ri_tid, (void *) &status );
169 #endif
170     }
171     Debug( LDAP_DEBUG_ANY, "slurpd: terminating normally\n", 0, 0, 0 );
172     sglob->slurpd_shutdown = 1;
173     pthread_exit( 0 );
174
175 #endif /* !NO_THREADS */
176 }