]> git.sur5r.net Git - openldap/blob - slurpd/main.c
Do not require ac/string.h for lber_pvt.h
[openldap] / slurpd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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     /* 
52      * Create and initialize globals.  init_globals() also initializes
53      * the main replication queue.
54      */
55     if (( sglob = init_globals()) == NULL ) {
56         fprintf( stderr, "Out of memory initializing globals\n" );
57         exit( EXIT_FAILURE );
58     }
59
60     /*
61      * Process command-line args and fill in globals.
62      */
63     if ( doargs( argc, argv, sglob ) < 0 ) {
64         exit( EXIT_FAILURE );
65     }
66
67     /*
68      * Read slapd config file and initialize Re (per-replica) structs.
69      */
70     if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
71         fprintf( stderr,
72                 "Errors encountered while processing config file \"%s\"\n",
73                 sglob->slapd_configfile );
74         exit( EXIT_FAILURE );
75     }
76
77     /* 
78      * Make sure our directory exists
79      */
80     if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
81         perror(sglob->slurpd_rdir);
82         exit( 1 );
83     }
84
85     /*
86      * Get any saved state information off the disk.
87      */
88     if ( sglob->st->st_read( sglob->st )) {
89         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
90                 sglob->slurpd_status_file, 0, 0 );
91         exit( EXIT_FAILURE );
92     }
93
94     /*
95      * All readonly data should now be initialized. 
96      * Check for any fatal error conditions before we get started
97      */
98      if ( sanity() < 0 ) {
99         exit( EXIT_FAILURE );
100     }
101
102     /*
103      * Detach from the controlling terminal
104      * unless the -d flag is given or in one-shot mode.
105      */
106     if ( ! (sglob->no_detach || sglob->one_shot_mode) )
107         lutil_detach( 0, 0 );
108
109     /*
110      * Start the main file manager thread (in fm.c).
111      */
112     if ( ldap_pvt_thread_create( &(sglob->fm_tid),
113                 0, fm, (void *) NULL ) != 0 )
114         {
115         Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
116                 0, 0, 0 );
117         exit( EXIT_FAILURE );
118
119     }
120
121     /*
122      * wait for fm to finish if in oneshot mode
123      */
124     if ( sglob->one_shot_mode ) {
125         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
126     }
127
128     /*
129      * Start threads - one thread for each replica
130      */
131     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
132         start_replica_thread( sglob->replicas[ i ]);
133     }
134
135     /*
136      * Wait for the fm thread to finish.
137      */
138     if ( !sglob->one_shot_mode ) {
139         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
140     }
141
142     /*
143      * Wait for the replica threads to finish.
144      */
145     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
146         ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
147     }
148
149     /* destroy the thread package */
150     ldap_pvt_thread_destroy();
151
152     Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
153         return 0;
154 #endif /* !NO_THREADS */
155 }