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