]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
Update copyright statements
[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 "slurp.h"
30 #include "globals.h"
31 #include "lutil.h"
32
33
34 int
35 main(
36     int         argc,
37     char        **argv
38 )
39 {
40 #ifdef NO_THREADS
41     /* Haven't yet written the non-threaded version */
42     fputs( "slurpd currently requires threads support\n", stderr );
43     return( 1 );
44 #else
45
46     int                 i;
47
48     /* initialize thread package */
49     ldap_pvt_thread_initialize();
50
51 #ifdef HAVE_TLS
52         if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
53                 fprintf( stderr, "TLS Initialization failed.\n" );
54                 exit( EXIT_FAILURE);
55         }
56 #endif
57
58     /* 
59      * Create and initialize globals.  init_globals() also initializes
60      * the main replication queue.
61      */
62     if (( sglob = init_globals()) == NULL ) {
63         fprintf( stderr, "Out of memory initializing globals\n" );
64         exit( EXIT_FAILURE );
65     }
66
67     /*
68      * Process command-line args and fill in globals.
69      */
70     if ( doargs( argc, argv, sglob ) < 0 ) {
71         exit( EXIT_FAILURE );
72     }
73
74     /*
75      * Read slapd config file and initialize Re (per-replica) structs.
76      */
77     if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
78         fprintf( stderr,
79                 "Errors encountered while processing config file \"%s\"\n",
80                 sglob->slapd_configfile );
81         exit( EXIT_FAILURE );
82     }
83
84     /* 
85      * Make sure our directory exists
86      */
87     if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
88         perror(sglob->slurpd_rdir);
89         exit( 1 );
90     }
91
92     /*
93      * Get any saved state information off the disk.
94      */
95     if ( sglob->st->st_read( sglob->st )) {
96         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
97                 sglob->slurpd_status_file, 0, 0 );
98         exit( EXIT_FAILURE );
99     }
100
101     /*
102      * All readonly data should now be initialized. 
103      * Check for any fatal error conditions before we get started
104      */
105      if ( sanity() < 0 ) {
106         exit( EXIT_FAILURE );
107     }
108
109     /*
110      * Detach from the controlling terminal
111      * unless the -d flag is given or in one-shot mode.
112      */
113     if ( ! (sglob->no_detach || sglob->one_shot_mode) )
114         lutil_detach( 0, 0 );
115
116     /*
117      * Start the main file manager thread (in fm.c).
118      */
119     if ( ldap_pvt_thread_create( &(sglob->fm_tid),
120                 0, fm, (void *) NULL ) != 0 )
121         {
122         Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
123                 0, 0, 0 );
124         exit( EXIT_FAILURE );
125
126     }
127
128     /*
129      * wait for fm to finish if in oneshot mode
130      */
131     if ( sglob->one_shot_mode ) {
132         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
133     }
134
135     /*
136      * Start threads - one thread for each replica
137      */
138     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
139         start_replica_thread( sglob->replicas[ i ]);
140     }
141
142     /*
143      * Wait for the fm thread to finish.
144      */
145     if ( !sglob->one_shot_mode ) {
146         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
147     }
148
149     /*
150      * Wait for the replica threads to finish.
151      */
152     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
153         ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
154     }
155
156     /* destroy the thread package */
157     ldap_pvt_thread_destroy();
158
159     Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
160         return 0;
161 #endif /* !NO_THREADS */
162 }