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