]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
5418ef79d105342ef05cfff0f7d93613350ade42
[openldap] / servers / slurpd / main.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25 /* ACKNOWLEDGEMENTS:
26  * This work was originally developed by the University of Michigan
27  * (as part of U-MICH LDAP).  Additional significant contributors
28  * include:
29  *     Howard Chu
30  */
31
32
33 /* 
34  * main.c - main routine for slurpd.
35  */
36
37 #include "portable.h"
38
39 #include <stdio.h>
40 #include <sys/stat.h>
41 #include <ac/stdlib.h>
42 #include <ac/unistd.h>
43
44 #include "slurp.h"
45 #include "globals.h"
46 #include "lutil.h"
47
48 #include <ldap_pvt.h>
49
50 #ifdef HAVE_NT_SERVICE_MANAGER
51 #define MAIN_RETURN(x)  return
52 #define SERVICE_EXIT( e, n )    do { \
53         if ( is_NT_Service ) { \
54                 lutil_ServiceStatus.dwWin32ExitCode = (e); \
55                 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
56         } \
57 } while ( 0 )
58 #else
59 #define SERVICE_EXIT( e, n )
60 #define MAIN_RETURN(x)  return(x)
61 #endif
62
63 #ifdef HAVE_NT_SERVICE_MANAGER
64 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
65 #else
66 int main( int argc, char **argv )
67 #endif
68 {
69 #ifdef NO_THREADS
70     /* Haven't yet written the non-threaded version */
71     fputs( "slurpd currently requires threads support\n", stderr );
72     return( 1 );
73 #else
74
75     int                 i, rc = 0;
76
77     /* initialize thread package */
78     ldap_pvt_thread_initialize();
79
80     /* 
81      * Create and initialize globals.  init_globals() also initializes
82      * the main replication queue.
83      */
84     if (( sglob = init_globals()) == NULL ) {
85         fprintf( stderr, "Out of memory initializing globals\n" );
86         SERVICE_EXIT( ERROR_NOT_ENOUGH_MEMORY, 0 );
87         rc = 1;
88         goto stop;
89     }
90
91 #ifdef HAVE_NT_SERVICE_MANAGER
92         {
93                 int *i;
94                 char *newConfigFile;
95                 char *regService = NULL;
96
97                 if ( is_NT_Service ) {
98                         sglob->serverName = argv[0];
99                         lutil_CommenceStartupProcessing( sglob->serverName, slurp_set_shutdown );
100                         if ( strcmp(sglob->serverName, SERVICE_NAME) )
101                             regService = sglob->serverName;
102                 }
103
104                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
105                 if ( i != NULL ) 
106                 {
107                         ldap_debug = *i;
108 #ifdef NEW_LOGGING
109                         lutil_log_initialize( argc, argv );
110                         LDAP_LOG( SLURPD, INFO, 
111                                 "main: new debug level from registry is: %d\n", 
112                                 ldap_debug, 0, 0 );
113 #else
114                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
115 #endif
116                 }
117
118                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
119                 if ( newConfigFile != NULL ) 
120                 {
121                         sglob->slapd_configfile = newConfigFile;
122 #ifdef NEW_LOGGING
123                         LDAP_LOG( SLURPD, INFO, 
124                                 "main: new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
125 #else
126                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
127 #endif
128
129                 }
130         }
131 #endif
132
133     /*
134      * Process command-line args and fill in globals.
135      */
136     if ( doargs( argc, argv, sglob ) < 0 ) {
137         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
138         rc = 1;
139         goto stop;
140     }
141
142     /*
143      * Read slapd config file and initialize Re (per-replica) structs.
144      */
145     if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
146         fprintf( stderr,
147                 "Errors encountered while processing config file \"%s\"\n",
148                 sglob->slapd_configfile );
149         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
150         rc = 1;
151         goto stop;
152     }
153
154 #ifdef HAVE_TLS
155         if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
156                 fprintf( stderr, "TLS Initialization failed.\n" );
157                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
158                 rc = 1;
159                 goto stop;
160         }
161 #endif
162
163     /* 
164      * Make sure our directory exists
165      */
166     if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
167         perror(sglob->slurpd_rdir);
168         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
169         rc = 1;
170         goto stop;
171     }
172
173     /*
174      * Get any saved state information off the disk.
175      */
176     if ( sglob->st->st_read( sglob->st )) {
177         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
178                 sglob->slurpd_status_file );
179         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
180         rc = 1;
181         goto stop;
182     }
183
184     /*
185      * All readonly data should now be initialized. 
186      * Check for any fatal error conditions before we get started
187      */
188      if ( sanity() < 0 ) {
189         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
190         rc = 1;
191         goto stop;
192     }
193
194
195     /*
196      * Detach from the controlling terminal
197      * unless the -d flag is given or in one-shot mode.
198      */
199 #ifndef HAVE_WINSOCK
200         if ( ! (sglob->no_detach || sglob->one_shot_mode) ) {
201                 lutil_detach( 0, 0 );
202         }
203 #endif
204
205         if ( slurpd_pid_file != NULL ) {
206                 FILE *fp = fopen( slurpd_pid_file, "w" );
207
208                 if( fp != NULL ) {
209                         fprintf( fp, "%d\n", (int) getpid() );
210                         fclose( fp );
211
212                 } else {
213                 free(slurpd_pid_file);
214                 slurpd_pid_file = NULL;
215                 }
216         }
217
218         if ( slurpd_args_file != NULL ) {
219                 FILE *fp = fopen( slurpd_args_file, "w" );
220
221                 if( fp != NULL ) {
222                         for ( i = 0; i < argc; i++ ) {
223                                 fprintf( fp, "%s ", argv[i] );
224                         }
225                         fprintf( fp, "\n" );
226                         fclose( fp );
227                 } else {
228                         free(slurpd_args_file);
229                         slurpd_args_file = NULL;
230                 }
231         }
232
233     if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
234         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
235         rc = 1;
236         goto stop;
237     }
238         
239 #ifdef HAVE_NT_EVENT_LOG
240         if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
241 #endif
242
243     /*
244      * Start the main file manager thread (in fm.c).
245      */
246     if ( ldap_pvt_thread_create( &(sglob->fm_tid),
247                 0, fm, (void *) NULL ) != 0 )
248         {
249 #ifdef NEW_LOGGING
250         LDAP_LOG ( SLURPD, ERR,
251                 "main: file manager ldap_pvt_thread_create failed\n" , 0, 0, 0 );
252 #else
253         Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
254                 0, 0, 0 );
255 #endif
256         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
257         rc = 1;
258         goto stop;
259
260     }
261
262     /*
263      * wait for fm to finish if in oneshot mode
264      */
265     if ( sglob->one_shot_mode ) {
266         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
267     }
268
269     /*
270      * Start threads - one thread for each replica
271      */
272     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
273         start_replica_thread( sglob->replicas[ i ]);
274     }
275
276 #ifdef HAVE_NT_SERVICE_MANAGER
277     if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
278 #endif
279
280     /*
281      * Wait for the fm thread to finish.
282      */
283     if ( !sglob->one_shot_mode ) {
284         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
285     }
286
287     /*
288      * Wait for the replica threads to finish.
289      */
290     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
291         ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
292     }
293
294 stop:
295 #ifdef HAVE_NT_SERVICE_MANAGER
296         if (is_NT_Service) {
297                 ldap_pvt_thread_cond_destroy( &started_event );
298                 lutil_LogStoppedEvent( sglob->serverName );
299                 lutil_ReportShutdownComplete();
300         }
301 #endif
302     /* destroy the thread package */
303     ldap_pvt_thread_destroy();
304
305 #ifdef HAVE_TLS
306     ldap_pvt_tls_destroy();
307 #endif
308
309 #ifdef NEW_LOGGING
310         LDAP_LOG ( SLURPD, RESULTS, "main: slurpd terminated\n", 0, 0, 0 );
311 #else
312     Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
313 #endif
314
315     if ( slurpd_pid_file != NULL ) {
316         unlink( slurpd_pid_file );
317     }
318     if ( slurpd_args_file != NULL ) {
319         unlink( slurpd_args_file );
320     }
321
322
323         MAIN_RETURN(rc);
324 #endif /* !NO_THREADS */
325 }