]> git.sur5r.net Git - openldap/blob - servers/slapd/main.c
ITS#900: Fix waitpid == 0 bug
[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                         Debug( LDAP_DEBUG_ANY, "new debug level from registry is: %d\n", slap_debug, 0, 0 );
179                 }
180
181                 newUrls = (char *) getRegParam(regService, "Urls");
182                 if (newUrls)
183                 {
184                     if (urls)
185                         ch_free(urls);
186
187                     urls = ch_strdup(newUrls);
188                     Debug(LDAP_DEBUG_ANY, "new urls from registry: %s\n",
189                           urls, 0, 0);
190                 }
191
192                 newConfigFile = (char*)getRegParam( regService, "ConfigFile" );
193                 if ( newConfigFile != NULL ) 
194                 {
195                         configfile = newConfigFile;
196                         Debug ( LDAP_DEBUG_ANY, "new config file from registry is: %s\n", configfile, 0, 0 );
197                 }
198         }
199 #endif
200
201         while ( (i = getopt( argc, argv,
202                              "d:f:h:s:n:"
203 #ifdef HAVE_CHROOT
204                                 "r:"
205 #endif
206 #ifdef LOG_LOCAL4
207                              "l:"
208 #endif
209 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
210                              "u:g:"
211 #endif
212                              )) != EOF ) {
213                 switch ( i ) {
214                 case 'h':       /* listen URLs */
215                         if ( urls != NULL ) free( urls );
216                         urls = ch_strdup( optarg );
217             break;
218
219                 case 'd':       /* set debug level and 'do not detach' flag */
220                         no_detach = 1;
221 #ifdef LDAP_DEBUG
222                         slap_debug |= atoi( optarg );
223 #else
224                         if ( atoi( optarg ) != 0 )
225                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
226                                        stderr );
227 #endif
228                         break;
229
230                 case 'f':       /* read config file */
231                         configfile = ch_strdup( optarg );
232                         break;
233
234                 case 's':       /* set syslog level */
235                         ldap_syslog = atoi( optarg );
236                         break;
237
238 #ifdef LOG_LOCAL4
239                 case 'l':       /* set syslog local user */
240                         syslogUser = cnvt_str2int( optarg,
241                                 syslog_types, DEFAULT_SYSLOG_USER );
242                         break;
243 #endif
244
245 #ifdef HAVE_CHROOT
246                 case 'r':
247                         if( sandbox ) free(sandbox);
248                         sandbox = ch_strdup( optarg );
249                         break;
250 #endif
251
252 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
253                 case 'u':       /* user name */
254                         if( username ) free(username);
255                         username = ch_strdup( optarg );
256                         break;
257
258                 case 'g':       /* group name */
259                         if( groupname ) free(groupname);
260                         groupname = ch_strdup( optarg );
261                         break;
262 #endif /* SETUID && GETUID */
263
264                 case 'n':  /* NT service name */
265                         if( serverName != NULL ) free( serverName );
266                         serverName = ch_strdup( optarg );
267                         break;
268
269                 default:
270                         usage( argv[0] );
271                         rc = 1;
272                         SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 15 );
273                         goto stop;
274                 }
275         }
276
277 #ifdef NEW_LOGGING
278         lutil_log_initialize( argc, argv );
279 #endif
280
281         lutil_set_debug_level( "slapd", slap_debug );
282         ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &slap_debug);
283         ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &slap_debug);
284         ldif_debug = slap_debug;
285
286         Debug( LDAP_DEBUG_TRACE, "%s", Versionstr, 0, 0 );
287
288         if( serverName == NULL ) {
289                 if ( (serverName = strrchr( argv[0], *LDAP_DIRSEP )) == NULL ) {
290                         serverName = ch_strdup( argv[0] );
291                 } else {
292                         serverName = ch_strdup( serverName + 1 );
293                 }
294         }
295
296 #ifdef LOG_LOCAL4
297         openlog( serverName, OPENLOG_OPTIONS, syslogUser );
298 #elif LOG_DEBUG
299         openlog( serverName, OPENLOG_OPTIONS );
300 #endif
301
302         if( slapd_daemon_init( urls ) != 0 ) {
303                 rc = 1;
304                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 16 );
305                 goto stop;
306         }
307
308 #if defined(HAVE_CHROOT)
309         if ( sandbox ) {
310                 if ( chdir( sandbox ) ) {
311                         perror("chdir");
312                         rc = 1;
313                         goto stop;
314                 }
315                 if ( chroot( sandbox ) ) {
316                         perror("chroot");
317                         rc = 1;
318                         goto stop;
319                 }
320         }
321 #endif
322
323 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
324         if ( username != NULL || groupname != NULL ) {
325                 slap_init_user( username, groupname );
326         }
327 #endif
328
329         extops_init();
330
331 #ifdef SLAPD_MODULES
332         if ( module_init() != 0 ) {
333                 rc = 1;
334                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 17 );
335                 goto destroy;
336         }
337 #endif
338
339         if ( slap_init( serverMode, serverName ) != 0 ) {
340                 rc = 1;
341                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 18 );
342                 goto destroy;
343         }
344
345         if ( schema_init( ) != 0 ) {
346                 Debug( LDAP_DEBUG_ANY,
347                     "schema initialization error\n",
348                     0, 0, 0 );
349                 goto destroy;
350         }
351
352         if ( read_config( configfile ) != 0 ) {
353                 rc = 1;
354                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 19 );
355                 goto destroy;
356         }
357
358         if ( schema_prep( ) != 0 ) {
359                 Debug( LDAP_DEBUG_ANY,
360                     "schema prep error\n",
361                     0, 0, 0 );
362                 goto destroy;
363         }
364
365 #ifdef HAVE_TLS
366         rc = ldap_pvt_tls_init();
367
368         if (rc || ldap_pvt_tls_init_def_ctx() != 0) {
369                 rc = 1;
370                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 20 );
371                 goto destroy;
372         }
373 #endif
374
375         (void) SIGNAL( LDAP_SIGUSR1, slap_sig_wake );
376         (void) SIGNAL( LDAP_SIGUSR2, slap_sig_shutdown );
377
378 #ifdef SIGPIPE
379         (void) SIGNAL( SIGPIPE, SIG_IGN );
380 #endif
381 #ifdef SIGHUP
382         (void) SIGNAL( SIGHUP, slap_sig_shutdown );
383 #endif
384         (void) SIGNAL( SIGINT, slap_sig_shutdown );
385         (void) SIGNAL( SIGTERM, slap_sig_shutdown );
386 #ifdef LDAP_SIGCHLD
387         (void) SIGNAL( LDAP_SIGCHLD, wait4child );
388 #endif
389 #ifdef SIGBREAK
390         /* SIGBREAK is generated when Ctrl-Break is pressed. */
391         (void) SIGNAL( SIGBREAK, slap_sig_shutdown );
392 #endif
393
394 #ifndef HAVE_WINSOCK
395         lutil_detach( no_detach, 0 );
396 #endif /* HAVE_WINSOCK */
397
398 #ifdef CSRIMALLOC
399         mal_leaktrace(1);
400 #endif
401
402         if ( slap_startup( NULL )  != 0 ) {
403                 rc = 1;
404                 SERVICE_EXIT( ERROR_SERVICE_SPECIFIC_ERROR, 21 );
405                 goto shutdown;
406         }
407
408         {
409                 FILE *fp;
410
411                 Debug( LDAP_DEBUG_ANY, "slapd starting\n", 0, 0, 0 );
412
413                 if (( slapd_pid_file != NULL ) &&
414                         (( fp = fopen( slapd_pid_file, "w" )) != NULL ))
415                 {
416                         fprintf( fp, "%d\n", (int) getpid() );
417                         fclose( fp );
418                 }
419
420                 if (( slapd_args_file != NULL ) &&
421                         (( fp = fopen( slapd_args_file, "w" )) != NULL ))
422                 {
423                         for ( i = 0; i < g_argc; i++ ) {
424                                 fprintf( fp, "%s ", g_argv[i] );
425                         }
426                         fprintf( fp, "\n" );
427                         fclose( fp );
428                 }
429         }
430
431 #ifdef HAVE_NT_EVENT_LOG
432         if (is_NT_Service)
433         LogSlapdStartedEvent( serverName, slap_debug, configfile, urls );
434 #endif
435
436         rc = slapd_daemon();
437
438 #ifdef HAVE_NT_SERVICE_MANAGER
439         /* Throw away the event that we used during the startup process. */
440         if ( is_NT_Service )
441                 ldap_pvt_thread_cond_destroy( &started_event );
442 #endif
443
444 shutdown:
445         /* remember an error during shutdown */
446         rc |= slap_shutdown( NULL );
447
448 destroy:
449         /* remember an error during destroy */
450         rc |= slap_destroy();
451
452 #ifdef SLAPD_MODULES
453         module_kill();
454 #endif
455
456         extops_kill();
457
458 stop:
459 #ifdef HAVE_NT_EVENT_LOG
460         if (is_NT_Service)
461         LogSlapdStoppedEvent( serverName );
462 #endif
463
464         Debug( LDAP_DEBUG_ANY, "slapd stopped.\n", 0, 0, 0 );
465
466 #ifdef HAVE_NT_SERVICE_MANAGER
467         ReportSlapdShutdownComplete();
468 #endif
469
470 #ifdef LOG_DEBUG
471     closelog();
472 #endif
473         slapd_daemon_destroy();
474
475 #ifdef CSRIMALLOC
476         mal_dumpleaktrace( leakfile );
477 #endif
478
479         MAIN_RETURN(rc);
480 }
481
482
483 #ifdef LDAP_SIGCHLD
484
485 /*
486  *  Catch and discard terminated child processes, to avoid zombies.
487  */
488
489 static RETSIGTYPE
490 wait4child( int sig )
491 {
492     int save_errno = errno;
493
494 #ifdef WNOHANG
495     errno = 0;
496 #ifdef HAVE_WAITPID
497     while ( waitpid( (pid_t)-1, NULL, WNOHANG ) > 0 || errno == EINTR )
498         ;       /* NULL */
499 #else
500     while ( wait3( NULL, WNOHANG, NULL ) > 0 || errno == EINTR )
501         ;       /* NULL */
502 #endif
503 #else
504     (void) wait( NULL );
505 #endif
506     (void) SIGNAL_REINSTALL( sig, wait4child );
507     errno = save_errno;
508 }
509
510 #endif /* SIGCHLD || SIGCLD */
511
512
513 #ifdef LOG_LOCAL4
514
515 /*
516  *  Convert a string to an integer by means of a dispatcher table
517  *  if the string is not in the table return the default
518  */
519
520 static int
521 cnvt_str2int( char *stringVal, STRDISP_P dispatcher, int defaultVal )
522 {
523     int        retVal = defaultVal;
524     STRDISP_P  disp;
525
526     for (disp = dispatcher; disp->stringVal; disp++) {
527
528         if (!strncasecmp (stringVal, disp->stringVal, disp->abbr)) {
529
530             retVal = disp->intVal;
531             break;
532
533         }
534     }
535
536     return (retVal);
537 }
538
539 #endif  /* LOG_LOCAL4 */