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