]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
ITS#1733 eliminate o_abandonmutex
[openldap] / servers / slapd / main.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/socket.h>
11 #include <ac/string.h>
12 #include <ac/time.h>
13 #include <ac/unistd.h>
14 #include <ac/wait.h>
15 #include <ac/errno.h>
16
17 #include "ldap_pvt.h"
18
19 #include "slap.h"
20 #include "lutil.h"
21 #include "ldif.h"
22
23 #ifdef LDAP_SIGCHLD
24 static RETSIGTYPE wait4child( int sig );
25 #endif
26
27 #ifdef HAVE_NT_SERVICE_MANAGER
28 #define MAIN_RETURN(x) return
29 static struct sockaddr_in       bind_addr;
30
31 void CommenceStartupProcessing( LPCTSTR serverName,
32                                                            void(*stopper)(int));
33 void ReportSlapdShutdownComplete( void );
34 void *getRegParam( char *svc, char *value );
35
36 #define SERVICE_EXIT( e, n )    do { \
37         if ( is_NT_Service ) { \
38                 SLAPDServiceStatus.dwWin32ExitCode                              = (e); \
39                 SLAPDServiceStatus.dwServiceSpecificExitCode    = (n); \
40         } \
41 } while ( 0 )
42
43 #else
44 #define SERVICE_EXIT( e, n )
45 #define MAIN_RETURN(x) return(x)
46 #endif
47
48 #ifdef HAVE_NT_EVENT_LOG
49 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *urls );
50 void LogSlapdStoppedEvent( char *svc );
51 #endif
52
53 /*
54  * when more than one slapd is running on one machine, each one might have
55  * it's own LOCAL for syslogging and must have its own pid/args files
56  */
57
58 #ifndef HAVE_MKVERSION
59 const char Versionstr[] =
60         OPENLDAP_PACKAGE " " OPENLDAP_VERSION " Standalone LDAP Server (slapd)";
61 #endif
62
63 #ifdef LOG_LOCAL4
64
65 #define DEFAULT_SYSLOG_USER  LOG_LOCAL4
66
67 typedef struct _str2intDispatch {
68         char    *stringVal;
69         int      abbr;
70         int      intVal;
71 } STRDISP, *STRDISP_P;
72
73
74 /* table to compute syslog-options to integer */
75 static STRDISP  syslog_types[] = {
76         { "LOCAL0", sizeof("LOCAL0"), LOG_LOCAL0 },
77         { "LOCAL1", sizeof("LOCAL1"), LOG_LOCAL1 },
78         { "LOCAL2", sizeof("LOCAL2"), LOG_LOCAL2 },
79         { "LOCAL3", sizeof("LOCAL3"), LOG_LOCAL3 },
80         { "LOCAL4", sizeof("LOCAL4"), LOG_LOCAL4 },
81         { "LOCAL5", sizeof("LOCAL5"), LOG_LOCAL5 },
82         { "LOCAL6", sizeof("LOCAL6"), LOG_LOCAL6 },
83         { "LOCAL7", sizeof("LOCAL7"), LOG_LOCAL7 },
84         { NULL }
85 };
86
87 static int   cnvt_str2int( char *, STRDISP_P, int );
88
89 #endif  /* LOG_LOCAL4 */
90
91
92 static void
93 usage( char *name )
94 {
95         fprintf( stderr,
96                 "usage: %s options\n", name );
97         fprintf( stderr,
98                 "\t-d level\tDebug Level" "\n"
99                 "\t-f filename\tConfiguration File\n"
100 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
101                 "\t-g group\tGroup (id or name) to run as\n"
102 #endif
103                 "\t-h URLs\tList of URLs to serve\n"
104 #ifdef LOG_LOCAL4
105                 "\t-l sysloguser\tSyslog User (default: LOCAL4)\n"
106 #endif
107                 "\t-n serverName\tservice name\n"
108 #ifdef HAVE_CHROOT
109                 "\t-r directory\n"
110 #endif
111                 "\t-s level\tSyslog Level\n"
112 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
113                 "\t-u user\tUser (id or name) to run as\n"
114 #endif
115     );
116 }
117
118 #ifdef HAVE_NT_SERVICE_MANAGER
119 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
120 #else
121 int main( int argc, char **argv )
122 #endif
123 {
124         int             i, no_detach = 0;
125         int             rc = 1;
126         char *urls = NULL;
127 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
128         char *username = NULL;
129         char *groupname = NULL;
130 #endif
131 #if defined(HAVE_CHROOT)
132         char *sandbox = NULL;
133 #endif
134 #ifdef LOG_LOCAL4
135     int     syslogUser = DEFAULT_SYSLOG_USER;
136 #endif
137         
138         int g_argc = argc;
139         char **g_argv = argv;
140
141 #ifdef HAVE_NT_SERVICE_MANAGER
142         char            *configfile = ".\\slapd.conf";
143 #else
144         char            *configfile = SLAPD_DEFAULT_CONFIGFILE;
145 #endif
146         char        *serverName = NULL;
147         int         serverMode = SLAP_SERVER_MODE;
148
149 #ifdef CSRIMALLOC
150         FILE *leakfile;
151         if( ( leakfile = fopen( "slapd.leak", "w" )) == NULL ) {
152                 leakfile = stderr;
153         }
154 #endif
155
156
157 #ifdef HAVE_NT_SERVICE_MANAGER
158         {
159                 int *i;
160                 char *newConfigFile;
161                 char *newUrls;
162                 char *regService = NULL;
163
164                 if ( is_NT_Service ) {
165                         serverName = argv[0];
166                         CommenceStartupProcessing( serverName, slap_sig_shutdown );
167                         if ( strcmp(serverName, SERVICE_NAME) )
168                             regService = serverName;
169                 }
170
171                 i = (int*)getRegParam( regService, "DebugLevel" );
172                 if ( i != NULL ) 
173                 {
174                         slap_debug = *i;
175 #ifdef NEW_LOGGING
176                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
177                                    "main: new debug level from registry is: %d\n", slap_debug ));
178 #else
179                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
180 #endif
181                 }
182
183                 newUrls = (char *) getRegParam(regService, "Urls");
184                 if (newUrls)
185                 {
186                     if (urls)
187                         ch_free(urls);
188
189                     urls = ch_strdup(newUrls);
190 #ifdef NEW_LOGGING
191                     LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
192                                "main: new urls from registry: %s\n", urls ));
193 #else
194                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
195                           urls, 0, 0);
196 #endif
197
198                 }
199
200                 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
201                 if ( newConfigFile != NULL ) 
202                 {
203                         configfile = newConfigFile;
204 #ifdef NEW_LOGGING
205                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
206                                    "main: new config file from registry is: %s\n", configfile ));
207 #else
208                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
209 #endif
210
211                 }
212         }
213 #endif
214
215         while ( (i = getopt( argc, argv,
216                              "d:f:h:s:n:"
217 #ifdef HAVE_CHROOT
218                                 "r:"
219 #endif
220 #ifdef LOG_LOCAL4
221                              "l:"
222 #endif
223 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
224                              "u:g:"
225 #endif
226                              )) != EOF ) {
227                 switch ( i ) {
228                 case 'h':       /* listen URLs */
229                         if ( urls != NULL ) free( urls );
230                         urls = ch_strdup( optarg );
231             break;
232
233                 case 'd':       /* set debug level and 'do not detach' flag */
234                         no_detach = 1;
235 #ifdef LDAP_DEBUG
236                         slap_debug |= atoi( optarg );
237 #else
238                         if ( atoi( optarg ) != 0 )
239                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
240                                        stderr );
241 #endif
242                         break;
243
244                 case 'f':       /* read config file */
245                         configfile = ch_strdup( optarg );
246                         break;
247
248                 case 's':       /* set syslog level */
249                         ldap_syslog = atoi( optarg );
250                         break;
251
252 #ifdef LOG_LOCAL4
253                 case 'l':       /* set syslog local user */
254                         syslogUser = cnvt_str2int( optarg,
255                                 syslog_types, DEFAULT_SYSLOG_USER );
256                         break;
257 #endif
258
259 #ifdef HAVE_CHROOT
260                 case 'r':
261                         if( sandbox ) free(sandbox);
262                         sandbox = ch_strdup( optarg );
263                         break;
264 #endif
265
266 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
267                 case 'u':       /* user name */
268                         if( username ) free(username);
269                         username = ch_strdup( optarg );
270                         break;
271
272                 case 'g':       /* group name */
273                         if( groupname ) free(groupname);
274                         groupname = ch_strdup( optarg );
275                         break;
276 #endif /* SETUID && GETUID */
277
278                 case 'n':  /* NT service name */
279                         if( serverName != NULL ) free( serverName );
280                         serverName = ch_strdup( optarg );
281                         break;
282
283                 default:
284                         usage( argv[0] );
285                         rc = 1;
286                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
287                         goto stop;
288                 }
289         }
290
291 #ifdef NEW_LOGGING
292         lutil_log_initialize( argc, argv );
293 #endif
294
295         lutil_set_debug_level( "slapd", slap_debug );
296         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
297         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
298         ldif_debug = slap_debug;
299
300 #ifdef NEW_LOGGING
301         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
302                    "%s", Versionstr ));
303 #else
304         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
305 #endif
306
307
308         if( serverName == NULL ) {
309                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
310                         serverName = argv[0];
311                 } else {
312                         serverName = serverName + 1;
313                 }
314         }
315
316 #ifdef LOG_LOCAL4
317         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
318 #elif LOG_DEBUG
319         openlog( serverName, OPENLOG_OPTIONS );
320 #endif
321
322         if( slapd_daemon_init( urls ) != 0 ) {
323                 rc = 1;
324                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
325                 goto stop;
326         }
327
328 #if defined(HAVE_CHROOT)
329         if ( sandbox ) {
330                 if ( chdir( sandbox ) ) {
331                         perror("chdir");
332                         rc = 1;
333                         goto stop;
334                 }
335                 if ( chroot( sandbox ) ) {
336                         perror("chroot");
337                         rc = 1;
338                         goto stop;
339                 }
340         }
341 #endif
342
343 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
344         if ( username != NULL || groupname != NULL ) {
345                 slap_init_user( username, groupname );
346         }
347 #endif
348
349         extops_init();
350
351 #ifdef SLAPD_MODULES
352         if ( module_init() != 0 ) {
353                 rc = 1;
354                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
355                 goto destroy;
356         }
357 #endif
358
359         if ( slap_init( serverMode, serverName ) != 0 ) {
360                 rc = 1;
361                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
362                 goto destroy;
363         }
364
365         if ( slap_schema_init( ) != 0 ) {
366 #ifdef NEW_LOGGING
367                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
368                            "main: schema initialization error\n" ));
369 #else
370                 Debug( LDAP_DEBUG_ANY,
371                     "schema initialization error\n",
372                     0, 0, 0 );
373 #endif
374
375                 goto destroy;
376         }
377
378         if ( read_config( configfile ) != 0 ) {
379                 rc = 1;
380                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
381                 goto destroy;
382         }
383
384         if ( glue_sub_init( ) != 0 ) {
385 #ifdef NEW_LOGGING
386                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
387                            "main: subordinate config error\n"));
388 #else
389                 Debug( LDAP_DEBUG_ANY,
390                     "subordinate config error\n",
391                     0, 0, 0 );
392 #endif
393                 goto destroy;
394         }
395
396         if ( slap_schema_check( ) != 0 ) {
397 #ifdef NEW_LOGGING
398                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
399                            "main: schema prep error\n"));
400 #else
401                 Debug( LDAP_DEBUG_ANY,
402                     "schema prep error\n",
403                     0, 0, 0 );
404 #endif
405
406                 goto destroy;
407         }
408
409 #ifdef HAVE_TLS
410         rc = ldap_pvt_tls_init();
411         if( rc != 0) {
412 #ifdef NEW_LOGGING
413                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
414                            "main: tls init failed: %d\n", rc ));
415 #else
416                 Debug( LDAP_DEBUG_ANY,
417                     "main: TLS init failed: %d\n",
418                     0, 0, 0 );
419 #endif
420                 rc = 1;
421                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
422                 goto destroy;
423         }
424
425         rc = ldap_pvt_tls_init_def_ctx();
426         if( rc != 0) {
427 #ifdef NEW_LOGGING
428                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
429                            "main: tls init def ctx failed: %d\n", rc ));
430 #else
431                 Debug( LDAP_DEBUG_ANY,
432                     "main: TLS init def ctx failed: %d\n",
433                     0, 0, 0 );
434 #endif
435                 rc = 1;
436                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
437                 goto destroy;
438         }
439 #endif
440
441         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
442         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
443
444 #ifdef SIGPIPE
445         (void) SIGNAL( SIGPIPE, SIG_IGN );
446 #endif
447 #ifdef SIGHUP
448         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
449 #endif
450         (void) SIGNAL( SIGINT, slap_sig_shutdown );
451         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
452 #ifdef LDAP_SIGCHLD
453         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
454 #endif
455 #ifdef SIGBREAK
456         /* SIGBREAK is generated when Ctrl-Break is pressed. */
457         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
458 #endif
459
460 #ifndef HAVE_WINSOCK
461         lutil_detach( no_detach, 0 );
462 #endif /* HAVE_WINSOCK */
463
464 #ifdef CSRIMALLOC
465         mal_leaktrace(1);
466 #endif
467
468         if ( slap_startup( NULL )  != 0 ) {
469                 rc = 1;
470                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
471                 goto shutdown;
472         }
473
474         {
475                 FILE *fp;
476
477 #ifdef NEW_LOGGING
478                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
479                            "main: slapd starting.\n" ));
480 #else
481                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
482 #endif
483
484
485                 if (( slapd_pid_file != NULL ) &&
486                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
487                 {
488                         fprintf( fp, "%d\n", (int) getpid() );
489                         fclose( fp );
490                 }
491
492                 if (( slapd_args_file != NULL ) &&
493                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
494                 {
495                         for ( i = 0; i < g_argc; i++ ) {
496                                 fprintf( fp, "%s ", g_argv[i] );
497                         }
498                         fprintf( fp, "\n" );
499                         fclose( fp );
500                 }
501         }
502
503 #ifdef HAVE_NT_EVENT_LOG
504         if (is_NT_Service)
505         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
506 #endif
507
508         rc = slapd_daemon();
509
510 #ifdef HAVE_NT_SERVICE_MANAGER
511         /* Throw away the event that we used during the startup process. */
512         if ( is_NT_Service )
513                 ldap_pvt_thread_cond_destroy( &started_event );
514 #endif
515
516 shutdown:
517         /* remember an error during shutdown */
518         rc |= slap_shutdown( NULL );
519
520 destroy:
521         /* remember an error during destroy */
522         rc |= slap_destroy();
523
524 #ifdef SLAPD_MODULES
525         module_kill();
526 #endif
527
528         extops_kill();
529
530 stop:
531 #ifdef HAVE_NT_EVENT_LOG
532         if (is_NT_Service)
533         LogSlapdStoppedEvent( serverName );
534 #endif
535
536 #ifdef NEW_LOGGING
537         LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
538                    "main: slapd stopped.\n" ));
539 #else
540         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
541 #endif
542
543
544 #ifdef HAVE_NT_SERVICE_MANAGER
545         ReportSlapdShutdownComplete();
546 #endif
547
548 #ifdef LOG_DEBUG
549     closelog();
550 #endif
551         slapd_daemon_destroy();
552
553         schema_destroy();
554
555 #ifdef HAVE_TLS
556         ldap_pvt_tls_destroy();
557 #endif
558
559         config_destroy();
560
561 #ifdef CSRIMALLOC
562         mal_dumpleaktrace( leakfile );
563 #endif
564
565         MAIN_RETURN(rc);
566 }
567
568
569 #ifdef LDAP_SIGCHLD
570
571 /*
572  *  Catch and discard terminated child processes, to avoid zombies.
573  */
574
575 static RETSIGTYPE
576 wait4child( int sig )
577 {
578     int save_errno = errno;
579
580 #ifdef WNOHANG
581     errno = 0;
582 #ifdef HAVE_WAITPID
583     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
584         ;       /* NULL */
585 #else
586     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
587         ;       /* NULL */
588 #endif
589 #else
590     (void) wait( NULL );
591 #endif
592     (void) SIGNAL_REINSTALL( sig, wait4child );
593     errno = save_errno;
594 }
595
596 #endif /* SIGCHLD || SIGCLD */
597
598
599 #ifdef LOG_LOCAL4
600
601 /*
602  *  Convert a string to an integer by means of a dispatcher table
603  *  if the string is not in the table return the default
604  */
605
606 static int
607 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
608 {
609     int        retVal = defaultVal;
610     STRDISP_P  disp;
611
612     for (disp = dispatcher; disp->stringVal; disp++) {
613
614         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
615
616             retVal = disp->intVal;
617             break;
618
619         }
620     }
621
622     return (retVal);
623 }
624
625 #endif  /* LOG_LOCAL4 */