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