]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
d4ff5bcc4614869d6ba331cdab68d9aa0cd87f70
[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     int                 i;
40
41     /* 
42      * Create and initialize globals.  init_globals() also initializes
43      * the main replication queue.
44      */
45     if (( sglob = init_globals()) == NULL ) {
46         fprintf( stderr, "Out of memory initializing globals\n" );
47         exit( 1 );
48     }
49
50     /*
51      * Process command-line args and fill in globals.
52      */
53     if ( doargs( argc, argv, sglob ) < 0 ) {
54         exit( 1 );
55     }
56
57     /*
58      * Read slapd config file and initialize Re (per-replica) structs.
59      */
60     if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
61         fprintf( stderr,
62                 "Errors encountered while processing config file \"%s\"\n",
63                 sglob->slapd_configfile );
64         exit( 1 );
65     }
66
67     /*
68      * Get any saved state information off the disk.
69      */
70     if ( sglob->st->st_read( sglob->st )) {
71         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
72                 sglob->slurpd_status_file, 0, 0 );
73         exit( 1 );
74     }
75
76     /*
77      * All readonly data should now be initialized. 
78      * Check for any fatal error conditions before we get started
79      */
80      if ( sanity() < 0 ) {
81         exit( 1 );
82     }
83
84     /*
85      * Detach from the controlling terminal, if debug level = 0,
86      * and if not in one-shot mode.
87      */
88 #ifdef LDAP_DEBUG
89     if (( ldap_debug == 0 )  && !sglob->one_shot_mode )
90 #else /* LDAP_DEBUG */
91     if ( !sglob->one_shot_mode )
92 #endif /* LDAP_DEBUG */
93         lutil_detach( 0, 0 );
94
95         /* initialize thread package */
96         ldap_pvt_thread_initialize();
97
98     /*
99      * Start threads - one thread for each replica
100      */
101     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
102         start_replica_thread( sglob->replicas[ i ]);
103     }
104
105     /*
106      * Start the main file manager thread (in fm.c).
107      */
108     if ( ldap_pvt_thread_create( &(sglob->fm_tid),
109                 0, fm, (void *) NULL ) != 0 )
110         {
111         Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
112                 0, 0, 0 );
113         exit( 1 );
114
115     }
116
117     /*
118      * Wait for the fm thread to finish.
119      */
120     ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
121
122     /*
123      * Wait for the replica threads to finish.
124      */
125     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
126         ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
127     }
128
129         /* destroy the thread package */
130         ldap_pvt_thread_destroy();
131
132     Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
133         return 0;
134 #endif /* !NO_THREADS */
135 }