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