]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
Cleanup thread handling to resolve non-exiting daemons on FreeBSD.
[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     if ( pthread_create( &(sglob->fm_tid), NULL, fm, (void *) NULL )
116             != 0 ) {
117         Debug( LDAP_DEBUG_ANY, "file manager pthread_create failed\n",
118                 0, 0, 0 );
119         exit( 1 );
120
121     }
122
123     /*
124      * Wait for the fm thread to finish.
125      */
126 #ifdef HAVE_PTHREADS_FINAL
127     pthread_join( sglob->fm_tid, (void *) NULL );
128 #else
129     pthread_join( sglob->fm_tid, (void *) &status );
130 #endif
131     /*
132      * Wait for the replica threads to finish.
133      */
134     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
135 #ifdef HAVE_PTHREADS_FINAL
136         pthread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
137 #else
138         pthread_join( sglob->replicas[ i ]->ri_tid, (void *) &status );
139 #endif
140     }
141     Debug( LDAP_DEBUG_ANY, "slurpd: terminating normally\n", 0, 0, 0 );
142     sglob->slurpd_shutdown = 1;
143
144         return 0;
145 #endif /* !NO_THREADS */
146 }