]> git.sur5r.net Git - openldap/blob - servers/slurpd/main.c
Windoes compatibility tweaks
[openldap] / servers / slurpd / main.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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_WINSOCK
51 #define mkdir(a,b)      mkdir(a)
52 #endif
53
54 #ifdef HAVE_NT_SERVICE_MANAGER
55 #define MAIN_RETURN(x)  return
56 #define SERVICE_EXIT( e, n )    do { \
57         if ( is_NT_Service ) { \
58                 lutil_ServiceStatus.dwWin32ExitCode = (e); \
59                 lutil_ServiceStatus.dwServiceSpecificExitCode = (n); \
60         } \
61 } while ( 0 )
62 #else
63 #define SERVICE_EXIT( e, n )
64 #define MAIN_RETURN(x)  return(x)
65 #endif
66
67 #ifndef HAVE_MKVERSION
68 const char Versionstr[] =
69         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Replicator (slurpd)";
70 #endif
71
72 #ifdef HAVE_NT_SERVICE_MANAGER
73 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
74 #else
75 int main( int argc, char **argv )
76 #endif
77 {
78 #ifdef NO_THREADS
79     /* Haven't yet written the non-threaded version */
80     fputs( "slurpd currently requires threads support\n", stderr );
81     return( 1 );
82 #else
83
84     int                 i, rc = 0;
85
86     /* initialize thread package */
87     ldap_pvt_thread_initialize();
88
89     /* 
90      * Create and initialize globals.  init_globals() also initializes
91      * the main replication queue.
92      */
93     if (( sglob = init_globals()) == NULL ) {
94         fprintf( stderr, "Out of memory initializing globals\n" );
95         SERVICE_EXIT( ERROR_NOT_ENOUGH_MEMORY, 0 );
96         rc = 1;
97         goto stop;
98     }
99
100 #ifdef HAVE_NT_SERVICE_MANAGER
101         {
102                 int *i;
103                 char *newConfigFile;
104                 char *regService = NULL;
105
106                 if ( is_NT_Service ) {
107                         sglob->serverName = argv[0];
108                         lutil_CommenceStartupProcessing( sglob->serverName, slurp_set_shutdown );
109                         if ( strcmp(sglob->serverName, SERVICE_NAME) )
110                             regService = sglob->serverName;
111                 }
112
113                 i = (int*)lutil_getRegParam( regService, "DebugLevel" );
114                 if ( i != NULL ) 
115                 {
116                         ldap_debug = *i;
117 #ifdef NEW_LOGGING
118                         lutil_log_initialize( argc, argv );
119                         LDAP_LOG( SLURPD, INFO, 
120                                 "main: new debug level from registry is: %d\n", 
121                                 ldap_debug, 0, 0 );
122 #else
123                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", ldap_debug, 0, 0 );
124 #endif
125                 }
126
127                 newConfigFile = (char*)lutil_getRegParam( regService, "ConfigFile" );
128                 if ( newConfigFile != NULL ) 
129                 {
130                         sglob->slapd_configfile = newConfigFile;
131 #ifdef NEW_LOGGING
132                         LDAP_LOG( SLURPD, INFO, 
133                                 "main: new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
134 #else
135                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", sglob->slapd_configfile, 0, 0 );
136 #endif
137
138                 }
139         }
140 #endif
141
142     /*
143      * Process command-line args and fill in globals.
144      */
145     if ( doargs( argc, argv, sglob ) < 0 ) {
146         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
147         rc = 1;
148         goto stop;
149     }
150
151     if ( sglob->version ) {
152                 fprintf(stderr, "%s\n", Versionstr);
153                 if (sglob->version > 1 ) {
154                         rc = 1;
155                         goto stop;
156                 }
157     }
158
159 #ifdef NEW_LOGGING
160         LDAP_LOG( SLURPD, INFO, "%s\n", Versionstr, 0, 0 );
161 #else
162         Debug ( LDAP_DEBUG_ANY, "%s\n", Versionstr, 0, 0 );
163 #endif
164     
165     /*
166      * Read slapd config file and initialize Re (per-replica) structs.
167      */
168     if ( slurpd_read_config( sglob->slapd_configfile ) < 0 ) {
169         fprintf( stderr,
170                 "Errors encountered while processing config file \"%s\"\n",
171                 sglob->slapd_configfile );
172         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
173         rc = 1;
174         goto stop;
175     }
176
177 #ifdef HAVE_TLS
178         if( ldap_pvt_tls_init() || ldap_pvt_tls_init_def_ctx() ) {
179                 fprintf( stderr, "TLS Initialization failed.\n" );
180                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
181                 rc = 1;
182                 goto stop;
183         }
184 #endif
185
186     /* 
187      * Make sure our directory exists
188      */
189     if ( mkdir(sglob->slurpd_rdir, 0755) == -1 && errno != EEXIST) {
190         perror(sglob->slurpd_rdir);
191         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
192         rc = 1;
193         goto stop;
194     }
195
196     /*
197      * Get any saved state information off the disk.
198      */
199     if ( sglob->st->st_read( sglob->st )) {
200         fprintf( stderr, "Malformed slurpd status file \"%s\"\n",
201                 sglob->slurpd_status_file );
202         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
203         rc = 1;
204         goto stop;
205     }
206
207     /*
208      * All readonly data should now be initialized. 
209      * Check for any fatal error conditions before we get started
210      */
211      if ( sanity() < 0 ) {
212         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
213         rc = 1;
214         goto stop;
215     }
216
217
218     /*
219      * Detach from the controlling terminal
220      * unless the -d flag is given or in one-shot mode.
221      */
222 #ifndef HAVE_WINSOCK
223         if ( ! (sglob->no_detach || sglob->one_shot_mode) ) {
224                 lutil_detach( 0, 0 );
225         }
226 #endif
227
228         if ( slurpd_pid_file != NULL ) {
229                 FILE *fp = fopen( slurpd_pid_file, "w" );
230
231                 if( fp != NULL ) {
232                         fprintf( fp, "%d\n", (int) getpid() );
233                         fclose( fp );
234
235                 } else {
236                 free(slurpd_pid_file);
237                 slurpd_pid_file = NULL;
238                 }
239         }
240
241         if ( slurpd_args_file != NULL ) {
242                 FILE *fp = fopen( slurpd_args_file, "w" );
243
244                 if( fp != NULL ) {
245                         for ( i = 0; i < argc; i++ ) {
246                                 fprintf( fp, "%s ", argv[i] );
247                         }
248                         fprintf( fp, "\n" );
249                         fclose( fp );
250                 } else {
251                         free(slurpd_args_file);
252                         slurpd_args_file = NULL;
253                 }
254         }
255
256     if ( (rc = lutil_pair( sglob->wake_sds )) < 0 ) {
257         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
258         rc = 1;
259         goto stop;
260     }
261         
262 #ifdef HAVE_NT_EVENT_LOG
263         if (is_NT_Service) lutil_LogStartedEvent( sglob->serverName, ldap_debug, sglob->slapd_configfile, "n/a" );
264 #endif
265
266     /*
267      * Start the main file manager thread (in fm.c).
268      */
269     if ( ldap_pvt_thread_create( &(sglob->fm_tid),
270                 0, fm, (void *) NULL ) != 0 )
271         {
272 #ifdef NEW_LOGGING
273         LDAP_LOG ( SLURPD, ERR,
274                 "main: file manager ldap_pvt_thread_create failed\n" , 0, 0, 0 );
275 #else
276         Debug( LDAP_DEBUG_ANY, "file manager ldap_pvt_thread_create failed\n",
277                 0, 0, 0 );
278 #endif
279         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
280         rc = 1;
281         goto stop;
282
283     }
284
285     /*
286      * wait for fm to finish if in oneshot mode
287      */
288     if ( sglob->one_shot_mode ) {
289         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
290     }
291
292     /*
293      * Start threads - one thread for each replica
294      */
295     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
296         start_replica_thread( sglob->replicas[ i ]);
297     }
298
299 #ifdef HAVE_NT_SERVICE_MANAGER
300     if ( started_event ) ldap_pvt_thread_cond_signal( &started_event );
301 #endif
302
303     /*
304      * Wait for the fm thread to finish.
305      */
306     if ( !sglob->one_shot_mode ) {
307         ldap_pvt_thread_join( sglob->fm_tid, (void *) NULL );
308     }
309
310     /*
311      * Wait for the replica threads to finish.
312      */
313     for ( i = 0; sglob->replicas[ i ] != NULL; i++ ) {
314         ldap_pvt_thread_join( sglob->replicas[ i ]->ri_tid, (void *) NULL );
315     }
316
317 stop:
318 #ifdef HAVE_NT_SERVICE_MANAGER
319         if (is_NT_Service) {
320                 ldap_pvt_thread_cond_destroy( &started_event );
321                 lutil_LogStoppedEvent( sglob->serverName );
322                 lutil_ReportShutdownComplete();
323         }
324 #endif
325     /* destroy the thread package */
326     ldap_pvt_thread_destroy();
327
328 #ifdef HAVE_TLS
329     ldap_pvt_tls_destroy();
330 #endif
331
332 #ifdef NEW_LOGGING
333         LDAP_LOG ( SLURPD, RESULTS, "main: slurpd terminated\n", 0, 0, 0 );
334 #else
335     Debug( LDAP_DEBUG_ANY, "slurpd: terminated.\n", 0, 0, 0 );
336 #endif
337
338     if ( slurpd_pid_file != NULL ) {
339         unlink( slurpd_pid_file );
340     }
341     if ( slurpd_args_file != NULL ) {
342         unlink( slurpd_args_file );
343     }
344
345
346         MAIN_RETURN(rc);
347 #endif /* !NO_THREADS */
348 }