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