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