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