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