]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
fix: changing bdb_cache_return_entry_w() to bdb_cache_return_entry_r()
[openldap] / servers / slurpd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1996 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18
19 /* 
20  * main.c - main routine for slurpd.
21  */
22
23 #include "portable.h"
24
25 #include <ac/stdlib.h>
26
27 #include <stdio.h>
28
29 #include <ldap_pvt.h>
30 #include "slurp.h"
31 #include "globals.h"
32 #include "lutil.h"
33
34
35 int
36 main(
37     int         argc,
38     char        **argv
39 )
40 {
41 #ifdef NO_THREADS
42     /* Haven't yet written the non-threaded version */
43     fputs( "slurpd currently requires threads support\n", stderr );
44     return( 1 );
45 #else
46
47     int                 i;
48
49     /* initialize thread package */
50     ldap_pvt_thread_initialize();
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( EXIT_FAILURE );
59     }
60
61     /*
62      * Process command-line args and fill in globals.
63      */
64     if ( doargs( argc, argv, sglob ) < 0 ) {
65         exit( EXIT_FAILURE );
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( EXIT_FAILURE );
76     }
77
78 #ifdef HAVE_TLS
79         if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
80                 fprintf( stderr, "TLS Initialization failed.\n" );
81                 exit( EXIT_FAILURE);
82         }
83 #endif
84
85     /* 
86      * Make sure our directory exists
87      */
88     if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
89         perror(sglob->slurpd_rdir);
90         exit( 1 );
91     }
92
93     /*
94      * Get any saved state information off the disk.
95      */
96     if ( sglob->st->st_read( sglob->st )) {
97         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
98                 sglob->slurpd_status_file, 0, 0 );
99         exit( EXIT_FAILURE );
100     }
101
102     /*
103      * All readonly data should now be initialized. 
104      * Check for any fatal error conditions before we get started
105      */
106      if ( sanity() < 0 ) {
107         exit( EXIT_FAILURE );
108     }
109
110     /*
111      * Detach from the controlling terminal
112      * unless the -d flag is given or in one-shot mode.
113      */
114     if ( ! (sglob->no_detach || sglob->one_shot_mode) )
115         lutil_detach( 0, 0 );
116
117     /*
118      * Start the main file manager thread (in fm.c).
119      */
120     if ( ldap_pvt_thread_create( &(sglob->fm_tid),
121                 0, fm, (void *) NULL ) != 0 )
122         {
123         Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
124                 0, 0, 0 );
125         exit( EXIT_FAILURE );
126
127     }
128
129     /*
130      * wait for fm to finish if in oneshot mode
131      */
132     if ( sglob->one_shot_mode ) {
133         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
134     }
135
136     /*
137      * Start threads - one thread for each replica
138      */
139     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
140         start_replica_thread( sglob->replicas[ i ]);
141     }
142
143     /*
144      * Wait for the fm thread to finish.
145      */
146     if ( !sglob->one_shot_mode ) {
147         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
148     }
149
150     /*
151      * Wait for the replica threads to finish.
152      */
153     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
154         ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
155     }
156
157     /* destroy the thread package */
158     ldap_pvt_thread_destroy();
159
160     Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
161         return 0;
162 #endif /* !NO_THREADS */
163 }