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