ramkomd.c 18.3 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2 3
 * $Id: ramkomd.c,v 0.132 2005/12/18 21:54:20 ceder Exp $
 * Copyright (C) 1991-1999, 2001-2004  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
Per Cederqvist's avatar
Per Cederqvist committed
23
 * Please report bugs at http://bugzilla.lysator.liu.se/. 
Linus Tolke Y's avatar
Linus Tolke Y committed
24
 */
25 26 27 28 29 30 31
/*
 * The next comment block is a historic comment, written in Swedish
 * using ISO 646 since nobody in Lysator knew about ISO 8859-1 by
 * then.  It translates, rougly, to "This is the main program of the
 * server.  It will hopefully be bigger than it is at the moment.
 * Created by Willfr 31-mar-1990."
 */
Per Cederqvist's avatar
Per Cederqvist committed
32 33 34 35 36 37 38 39 40
/*
 * Detta {r serverns huvudprogram. Det kommer f|rhoppningsvis bli st|rre
 * {n det {r just nu...
 *
 * Created by Willf|r 31/3-90
 *
 * It has grown! /ceder
 */

David Byers's avatar
David Byers committed
41 42 43 44 45

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

46 47 48
#ifdef HAVE_LOCALE_H
#  include <locale.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
49
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
50
#include <signal.h>
51 52 53 54 55 56
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
57
#include <sys/wait.h>
58
#include "timewrap.h"
59 60 61
#ifdef HAVE_SYS_RESOURCE_H
#  include <sys/resource.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
62
#include <unistd.h>
Per Cederqvist's avatar
Per Cederqvist committed
63 64 65 66
#include <setjmp.h>
#if defined(HAVE_SYS_PARAM_H) && !defined(HAVE_GETCWD)
# include <sys/param.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
67
#include <errno.h>
Per Cederqvist's avatar
Per Cederqvist committed
68
#include <sys/types.h>
69
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
70
#include <fcntl.h>
Per Cederqvist's avatar
Per Cederqvist committed
71

72 73
#include "oop.h"

Per Cederqvist's avatar
Per Cederqvist committed
74
#include "exp.h"
75
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
76
#include "misc-types.h"
77
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
78
#include "com.h"
79
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
80 81 82
#include "connections.h"
#include "internal-connections.h"
#include "kom-errno.h"
83
#include "oop-malloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
84
#include "isc-malloc.h"
85
#include "isc-interface.h"
David Byers's avatar
David Byers committed
86
#include "kom-config.h"
Per Cederqvist's avatar
Per Cederqvist committed
87
#include "cache.h"
Per Cederqvist's avatar
Per Cederqvist committed
88 89
#include "string-malloc.h"
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
90
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
91 92
#include "server/smalloc.h"
#include "kom-memory.h"
93 94
#include "conf-file.h"
#include "param.h"
95 96
#include "server-config.h"
#include "manipulate.h"
97
#include "version-info.h"
98
#include "aux-items.h"
David Byers's avatar
David Byers committed
99
#include "admin.h"
100
#include "unused.h"
Per Cederqvist's avatar
Per Cederqvist committed
101
#include "sigflags.h"
Per Cederqvist's avatar
Per Cederqvist committed
102
#include "local-to-global.h"
103
#include "server-time.h"
104
#include "lockdb.h"
Per Cederqvist's avatar
Per Cederqvist committed
105
#include "linkansi.h"
106 107 108
#ifdef TRACED_ALLOCATIONS
#  include "trace-alloc.h"
#endif
109
#include "eintr.h"
110
#include "stats.h"
111

112 113 114 115
#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_OFILE) && !defined(RLIMIT_NOFILE)
#  define RLIMIT_NOFILE RLIMIT_OFILE
#endif

116 117 118 119
#if !HAVE_RLIM_T
typedef int rlim_t;
#endif

120
struct timeval current_time = { 0, 0 };
121 122


Per Cederqvist's avatar
Per Cederqvist committed
123
#ifndef NDEBUG
124
int	buglevel = 0;
125
#endif
Per Cederqvist's avatar
Per Cederqvist committed
126

127 128 129 130 131
/* Don't place lyskomd in the background.  Write the log to stdout.
   Prompt for input on stdin before exiting.  This is primarily
   intended for debugging and the test suite. */
static int foreground = 0;

132 133
static oop_adapter_signal *kom_signal_adapter;

134
static void dump_exit_statistics(void);
David Byers's avatar
David Byers committed
135
static void free_kom_info(void);
136
static oop_call_signal sighandler_term;
137 138 139 140
static oop_call_signal sighandler_quit;
static oop_call_signal sighandler_usr1;
static oop_call_signal sighandler_usr2;
static oop_call_signal sighandler_winch;
141
isc_accept_callback handle_accept_event;
142

143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#ifdef HAVE_STRUCT_SIGACTION

/* Assigning SIG_IGN to sa_handler results in

    ramkomd.c:310: warning: function declaration isn't a prototype

  on certain compilers.  By performing the assignment in a function
  the number of warnings are reduced. */

static inline void
set_handler_sig_ign(struct sigaction *ptr)
{
    ptr->sa_handler = SIG_IGN;
}

#endif

161 162 163 164 165 166 167 168 169
static isc_write_queue_change_cb write_queue_change_callback;

static void
write_queue_change_callback(int delta)
{
    update_stat(STAT_SEND_QUEUE, delta);
}


Per Cederqvist's avatar
Per Cederqvist committed
170
static void
Per Cederqvist's avatar
Per Cederqvist committed
171
server_init(const char *host, const char * client_port)
Per Cederqvist's avatar
Per Cederqvist committed
172
{
173 174
    struct isc_scb *listen_client;
    oop_source *src;
175 176 177 178
#ifdef HAVE_STRUCT_SIGACTION
    struct sigaction act;
#endif

179 180 181 182 183 184 185
    oop_malloc = &oop_malloc_wrapper;
    oop_realloc = &oop_realloc_wrapper;
    oop_free = &oop_free_wrapper;

    kom_server_oop_src = oop_sys_new();
    if (kom_server_oop_src == NULL)
	restart_kom("server_init: can't get system event source\n");
186 187 188 189 190 191 192 193 194

    kom_signal_adapter = oop_signal_new(oop_sys_source(kom_server_oop_src));
    if (kom_signal_adapter == NULL)
	restart_kom("server_init: can't create signal adapter\n");

    /* Ignore the signals before we register them with liboop.  That
       way, when liboop reinstalls the old signal handler during
       shutdown, we will ignore them rather than stop prematurely
       during the shutdown.  This is especially important for the
195
       SIGTERM signal, which is sent periodically by updateLysKOM. */
196 197 198 199 200
#ifdef HAVE_STRUCT_SIGACTION
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    set_handler_sig_ign(&act);
    sigaction(SIGHUP, &act, NULL);
201 202
    sigaction(SIGTERM, &act, NULL);
    sigaction(SIGINT, &act, NULL);
203 204 205 206 207 208
    sigaction(SIGQUIT, &act, NULL);
    sigaction(SIGUSR1, &act, NULL);
    sigaction(SIGUSR2, &act, NULL);
    sigaction(SIGWINCH, &act, NULL);
#else
    signal(SIGHUP, SIG_IGN);
209 210
    signal(SIGTERM, SIG_IGN);
    signal(SIGINT, SIG_IGN);
211 212 213 214 215 216 217 218
    signal(SIGQUIT, SIG_IGN);
    signal(SIGUSR1, SIG_IGN);
    signal(SIGUSR2, SIG_IGN);
    signal(SIGWINCH, SIG_IGN);
#endif

    src = oop_signal_source(kom_signal_adapter);
    oop_signal_use_sa_restart();
219 220 221
    src->on_signal(src, SIGHUP, sighandler_term, NULL);
    src->on_signal(src, SIGTERM, sighandler_term, NULL);
    src->on_signal(src, SIGINT, sighandler_term, NULL);
222 223 224 225 226
    src->on_signal(src, SIGQUIT, sighandler_quit, NULL);
    src->on_signal(src, SIGUSR1, sighandler_usr1, NULL);
    src->on_signal(src, SIGUSR2, sighandler_usr2, NULL);
    src->on_signal(src, SIGWINCH, sighandler_winch, NULL);

Per Cederqvist's avatar
Per Cederqvist committed
227 228 229
    /*
    ** Setup some parameters here
    */
230 231 232 233
    isc_setallocfn(&isc_malloc_wrapper,
		   &isc_realloc_wrapper,
		   &isc_free_wrapper);

234 235
    kom_server_mcb  = isc_initialize(oop_sys_source(kom_server_oop_src),
				     write_queue_change_callback);
Per Cederqvist's avatar
Per Cederqvist committed
236
    if ( kom_server_mcb == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
237
	restart_kom("server_init: can't isc_initialize()\n");
238
    isc_cfg_fd_relocate(kom_server_mcb, PROTECTED_FDS);
239 240
    isc_cfg_stale_timeout(kom_server_mcb, param.stale_timeout,
			  param.connect_timeout);
241 242 243 244 245
    isc_cfg_queue_size(kom_server_mcb,
		       param.maxqueuedsize_bytes,
		       param.maxmsgsize,
		       param.maxqueuedsize,
		       param.maxdequeuelen);
Per Cederqvist's avatar
Per Cederqvist committed
246

247 248
    listen_client = isc_listentcp(kom_server_mcb, host, client_port,
				  handle_accept_event);
Per Cederqvist's avatar
Per Cederqvist committed
249 250
    if (listen_client == NULL)
        restart_kom("server_init: can't isc_listentcp(CLIENT)\n");
Per Cederqvist's avatar
Per Cederqvist committed
251

252 253
    kom_log("Listening for clients on %d.\n",
	    isc_getportnum(listen_client->laddr));
254
    
Per Cederqvist's avatar
Per Cederqvist committed
255 256 257 258 259
    /*
     * Ignore SIGPIPE, which the server gets if it tries to write to a
     * socket and the client has died. The server will anyhow handle
     * this situation correct.
     */
260 261 262
#ifdef HAVE_STRUCT_SIGACTION
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
263
    set_handler_sig_ign(&act);
264
    sigaction(SIGPIPE, &act, NULL);
265
#else
Per Cederqvist's avatar
Per Cederqvist committed
266
    signal(SIGPIPE, SIG_IGN);
267
#endif
Per Cederqvist's avatar
Per Cederqvist committed
268 269 270
}

static void
271
init_data_base(void)
Per Cederqvist's avatar
Per Cederqvist committed
272
{
David Byers's avatar
David Byers committed
273 274 275
    kom_log("Database = %s\n",  param.datafile_name);
    kom_log("Backup = %s\n", param.backupfile_name);
    kom_log("2nd Backup = %s\n", param.backupfile_name_2);
276
    kom_log("Lock File = %s\n", param.lockfile_name);
Per Cederqvist's avatar
Per Cederqvist committed
277 278
    
    if ( init_cache() == FAILURE )
279
	restart_kom ("Cannot find database.\n");
Per Cederqvist's avatar
Per Cederqvist committed
280 281
}

282
static void *
283 284
sighandler_term(oop_source *UNUSED(source),
	       int sig,
285
	       void *UNUSED(user))
Per Cederqvist's avatar
Per Cederqvist committed
286
{
287 288 289 290 291 292 293 294 295 296 297 298
    if (sig == SIGTERM)
	kom_log("Signal TERM received. Shutting down server.\n");
    else if (sig == SIGHUP)
	kom_log("Signal HUP received. Shutting down server."
		" Please use SIGTERM instead.\n");
    else if (sig == SIGINT)
	kom_log("Signal INT received. Shutting down server."
		" Please use SIGTERM instead.\n");
    else
	kom_log("Some signal received. Shutting down server."
		" Please use SIGTERM instead.\n");

299
    go_and_die = TRUE;
300
    return OOP_HALT;
Per Cederqvist's avatar
Per Cederqvist committed
301 302
}

303 304 305 306
static void *
sighandler_quit(oop_source *UNUSED(source),
		int UNUSED(sig),
		void *UNUSED(user))
Per Cederqvist's avatar
Per Cederqvist committed
307
{
David Byers's avatar
David Byers committed
308
    kom_log ("Signal QUIT received - syncing...\n");
Per Cederqvist's avatar
Per Cederqvist committed
309
    cache_sync_all();
David Byers's avatar
David Byers committed
310
    kom_log ("Dumping core now.\n");
Per Cederqvist's avatar
Per Cederqvist committed
311 312 313
    abort();
}

314 315 316 317
static void *
sighandler_usr1(oop_source *UNUSED(source),
		int UNUSED(sig),
		void *UNUSED(user))
Per Cederqvist's avatar
Per Cederqvist committed
318
{
319 320
    dump_statistics();
    return OOP_CONTINUE;
Per Cederqvist's avatar
Per Cederqvist committed
321 322
}

323 324 325 326
static void *
sighandler_usr2(oop_source *UNUSED(source),
		int UNUSED(sig),
		void *UNUSED(user))
Per Cederqvist's avatar
Per Cederqvist committed
327 328 329
{
    int		  child;

David Byers's avatar
David Byers committed
330
    kom_log ("Signal USR2 received - will dump core now.  (Check that child dies.)\n");
Per Cederqvist's avatar
Per Cederqvist committed
331 332 333
    if ((child = fork()) == 0)
    {
	abort();
334
	kom_log ("Abort() failed!!!\n");
Per Cederqvist's avatar
Per Cederqvist committed
335 336 337 338
	exit(1);
    }
    else if (child < 0)
    {
David Byers's avatar
David Byers committed
339
	kom_log ("Couldn't fork.\n");
Per Cederqvist's avatar
Per Cederqvist committed
340 341 342 343 344
    }
    else
    {
	wait (NULL);
    }
345
    return OOP_CONTINUE;
Per Cederqvist's avatar
Per Cederqvist committed
346 347
}

348 349 350 351
static void *
sighandler_winch(oop_source *UNUSED(source),
		 int UNUSED(sig),
		 void *UNUSED(user))
352
{
353 354 355 356
    free_aux_item_definitions();
    initialize_aux_items(param.aux_def_file);
    kom_log("Signal WINCH received. aux definitions reloaded.\n");
    return OOP_CONTINUE;
357 358
}

Per Cederqvist's avatar
Per Cederqvist committed
359
static void
Per Cederqvist's avatar
Per Cederqvist committed
360 361 362 363
save_pid(void)
{
    FILE *fp;

364
    if ((fp = i_fopen(param.pid_name, "w")) == NULL)
Per Cederqvist's avatar
Per Cederqvist committed
365 366
	return;

367
    fprintf(fp, "%ld\n", (long)getpid());
368
    i_fclose(fp);
Per Cederqvist's avatar
Per Cederqvist committed
369 370
}

Per Cederqvist's avatar
Per Cederqvist committed
371
static void
372
go_daemon(void)
Per Cederqvist's avatar
Per Cederqvist committed
373 374 375
{
    pid_t child;
    int fd;
376 377 378 379
#ifdef HAVE_STRUCT_SIGACTION
    struct sigaction act;
#endif
    
380
    if (foreground != 0)
381 382 383 384
    {
	return;
    }

Per Cederqvist's avatar
Per Cederqvist committed
385 386 387 388 389
    if (getppid() != 1)
    {
	/* We were not invoked from /etc/inittab, so
	   disassociate from controlling terminal. */

390 391 392
#ifdef HAVE_STRUCT_SIGACTION
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
393
	set_handler_sig_ign(&act);
394 395 396 397 398 399 400 401 402 403 404 405 406 407

#  ifdef SIGTTOU
	sigaction(SIGTTOU, &act, NULL);
#  endif
#  ifdef SIGTTIN
	sigaction(SIGTTIN, &act, NULL);
#  endif
#  ifdef SIGTSTP
	sigaction(SIGTSTP, &act, NULL);
#  endif

#else  /* !HAVE_STRUCT_SIGACTION */

#  ifdef SIGTTOU
Per Cederqvist's avatar
Per Cederqvist committed
408
	signal(SIGTTOU, SIG_IGN);
409 410
#  endif
#  ifdef SIGTTIN
Per Cederqvist's avatar
Per Cederqvist committed
411
	signal(SIGTTIN, SIG_IGN);
412 413
#  endif
#  ifdef SIGTSTP
Per Cederqvist's avatar
Per Cederqvist committed
414
	signal(SIGTSTP, SIG_IGN);
415 416
#  endif

Per Cederqvist's avatar
Per Cederqvist committed
417 418 419 420 421 422 423 424 425 426 427 428
#endif

	child = fork();
	if (child < 0)
	    restart_kom("fork failed: %d\n", errno);
	else if (child > 0)
	    exit (0);		/* parent */
	
	setsid();
    }

    /* Close all file descriptors */
429
    for (fd = 0; fd < fd_ceiling; fd++)
Per Cederqvist's avatar
Per Cederqvist committed
430 431 432
	close(fd);
    if (open("/dev/null", O_RDONLY) != 0
	||open("/dev/null", O_WRONLY) != 1
433
	||open(param.logfile_name, O_WRONLY|O_CREAT|O_APPEND, 0644) != 2)
Per Cederqvist's avatar
Per Cederqvist committed
434 435 436
    {
	/* Kinda stupid to try to log an error message now, but
	   who knows? */
437
	restart_kom("open of log file failed: %d\n", errno);
Per Cederqvist's avatar
Per Cederqvist committed
438
    }
David Byers's avatar
David Byers committed
439 440
    kom_log("*** Version %s (process %lu) coming up.\n",
            kom_version_info.server_version, (unsigned long)getpid());
Per Cederqvist's avatar
Per Cederqvist committed
441 442
}

443
static void
444
initialize(const char *config_file)
445
{
446
#ifdef USING_RLIMIT_NOFILE
447 448 449
    struct rlimit rlim;
#endif

450
    read_configuration(config_file);
451
    initialize_aux_items(param.aux_def_file);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

#ifdef HAVE_LOCALE_H
    if (param.use_locale != NULL)
	if (setlocale(LC_CTYPE, param.use_locale) == NULL)
	{
	    fprintf(stderr, "setlocale: ");
	    perror(param.use_locale);
	    exit(1);
	}
#else
    if (param.use_locale != NULL)
    {
	fprintf(stderr, "locale not supported in your environment.\n");
	exit(1);
    }
#endif
468

469 470 471 472 473 474 475 476
    if (param.no_files != -1 && param.no_files > FD_SETSIZE)
    {
	fprintf(stderr,
		"The \"Open files: %ld\" is too large: FD_SETSIZE is %ld\n",
		(long)param.no_files, (long)FD_SETSIZE);
	exit(1);
    }

477
#ifdef USING_RLIMIT_NOFILE
478 479 480 481 482 483
    if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
    {
	perror("getrlimit(RLIMIT_NOFILE) failed");
	exit(1);
    }

Per Cederqvist's avatar
Per Cederqvist committed
484
    if (param.no_files != -1)
485
    {
486
	if ((rlim_t)param.no_files > rlim.rlim_max)
487
	{
488 489 490 491
	    fprintf(stderr, "attempt to raise open files from %ld to %ld, "
		    "but only %ld is allowed\n",
		    (long)rlim.rlim_cur, (long)param.no_files,
		    (long)rlim.rlim_max);
492 493 494 495 496 497 498 499 500 501
	    rlim.rlim_cur = rlim.rlim_max;
	}
	else
	    rlim.rlim_cur = param.no_files;
	
	if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
	{
	    perror("setrlimit failed");
	    exit(1);
	}
502 503 504 505 506 507 508 509
	if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
	{
	    perror("getrlimit(RLIMIT_NOFILE) failed");
	    exit(1);
	}
	if (rlim.rlim_cur != (rlim_t)param.no_files)
	    restart_kom("getrlimit after setrlimit returns %ld, not %ld.\n",
			(long)rlim.rlim_cur, (long)param.no_files);
510
    }
511 512 513 514 515
    fd_ceiling = rlim.rlim_cur;
#elif HAVE_GETDTABLESIZE
    if (param.no_files == -1)
	fd_ceiling = getdtablesize();
    else if (param.no_files >= getdtablesize())
516
    {
517 518
	fprintf(stderr, "getdtablesize() indicates that at most %ld files"
		" can be used.\n", (long)getdtablesize());
519 520
	exit(1);
    }
521 522 523 524 525 526 527 528 529 530 531 532 533
    else
	fd_ceiling = param.no_files;
#elif defined(OPEN_MAX)
    if (param.no_files == -1)
	fd_ceiling = OPEN_MAX;
    else if (param.no_files >= OPEN_MAX)
    {
	fprintf(stderr, "OPEN_MAX indicates that at most %ld files"
		" can be used.\n", (long)OPEN_MAX);
	exit(1);
    }
    else
	fd_ceiling = param.no_files;
534
#else
535
#  error Do not know how to find maximum number of open files.
536 537
#endif

538 539 540
    if (fd_ceiling > FD_SETSIZE)
	fd_ceiling = FD_SETSIZE;

541
    go_daemon();
542
    if (lock_db() < 0)
543 544 545 546 547
    {
	/* Don't actually die until something is entered on stdin in debug
	   mode.  This is mainly here for the benefit of the test suite,
	   but is could also be useful to be able to attach a debugger and
	   do pre-mortem debugging of the process at this point.  */
548
	kom_log("Cannot obtain database lock.  Exiting.\n");
549
	if (foreground)
550 551 552 553 554
	{
	    kom_log("Press enter to terminate lyskomd\n");
	    getchar();
	}

555
	exit(1);
556 557
    }

Per Cederqvist's avatar
Per Cederqvist committed
558
    server_init(param.ip_client_host, param.ip_client_port);
559
    init_data_base();
560 561
}

Per Cederqvist's avatar
Per Cederqvist committed
562 563 564
/* Stop complaint from gcc 2.0 about "no previous prototype for `main'". */
int main(int argc, char **argv);

565
	
Per Cederqvist's avatar
Per Cederqvist committed
566 567 568 569 570
int
main (int    argc,
      char **argv)
{
    int i;
571
    const char *config_file;
572
    oop_source *src;
Per Cederqvist's avatar
Per Cederqvist committed
573 574 575

    link_ansi();

576 577
    if (gettimeofday(&current_time, NULL) < 0)
	restart_kom("gettimeofday failed: %s\n", strerror(errno));
Per Cederqvist's avatar
Per Cederqvist committed
578

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
#ifdef TRACED_ALLOCATIONS
    /* We must do this before we allocate any memory... */
    {
      char buf[1024];
      char *nl;

      fputs("Where does the trace want to go today? [stderr]\n", stdout);
      fflush(stdout);
      if (fgets(buf, sizeof(buf), stdin) != buf)
	  restart_kom("main(): unable to read trace location\n");
      if ((nl = strchr(buf, '\n')) != NULL)
	  *nl = '\0';
      trace_alloc_file(buf);
    }
#endif

David Byers's avatar
David Byers committed
595
    kom_log("*** Version %s (process %lu) started.\n",
596
	kom_version_info.server_version, (unsigned long)getpid());
597
#ifdef DEBUG_CALLS
David Byers's avatar
David Byers committed
598 599
    kom_log("WARNING: This server was compiled with --with-debug-calls.\n");
    kom_log("It isn't safe to use in a production environment.\n");
600 601
#else
    kom_log("Debug calls are disabled, as they should be.\n");
602
#endif
603

Per Cederqvist's avatar
Per Cederqvist committed
604
#ifdef ENCRYPT_PASSWORDS
605
    /* Seed the random number generator. */
606
    /* FIXME (bug 1068): This is not a good way to seed it... */
Per Cederqvist's avatar
Per Cederqvist committed
607 608
    srand(time(NULL) + getpid());
#endif
Per Cederqvist's avatar
Per Cederqvist committed
609

610 611
    /* Initialize the string handling package. */
    s_set_storage_management(string_malloc, string_realloc, string_free);
612

613
    /* Parse command line arguments. */
Per Cederqvist's avatar
Per Cederqvist committed
614 615 616 617 618 619 620
    for (i = 1; i < argc && argv[i][0] == '-'; i++)
	switch (argv[i][1])
	{
	case 'd':
	    buglevel++;
	    break;

621 622 623 624
	case 'f':
	    foreground = 1;
	    break;

625
	default:
626
	    restart_kom("usage: %s [-f] [-d ...] [config-file]\n", argv[0]);
627
	}
Per Cederqvist's avatar
Per Cederqvist committed
628

629

630
    /* Read in the configuration file. */
631

Per Cederqvist's avatar
Per Cederqvist committed
632
    if (i < argc)
633 634
	config_file = argv[i++];
    else
635
	config_file = get_default_config_file_name();
Per Cederqvist's avatar
Per Cederqvist committed
636

637 638
    if (i < argc)
	    restart_kom("usage: %s [-d ...] [config-file]\n", argv[0]);
639

640
    init_stats();
641
    initialize(config_file);	/* Read config, listen, and start db */
642
    chdir(param.core_dir);
643

Per Cederqvist's avatar
Per Cederqvist committed
644
    save_pid();
Per Cederqvist's avatar
Per Cederqvist committed
645

Per Cederqvist's avatar
Per Cederqvist committed
646
    toploop();
Per Cederqvist's avatar
Per Cederqvist committed
647

648 649 650
    /* There is no use sending logout messages to all sessions. */
    param.send_async_messages = FALSE;
    
Per Cederqvist's avatar
Per Cederqvist committed
651
    logout_all_clients();
652
    isc_shutdown(kom_server_mcb);
Per Cederqvist's avatar
Per Cederqvist committed
653
    cache_sync_all();
654
    unlock_db();
655
    src = oop_signal_source(kom_signal_adapter);
656 657 658 659
    src->cancel_signal(src, SIGWINCH, sighandler_winch, NULL);
    src->cancel_signal(src, SIGUSR2, sighandler_usr2, NULL);
    src->cancel_signal(src, SIGUSR1, sighandler_usr1, NULL);
    src->cancel_signal(src, SIGQUIT, sighandler_quit, NULL);
660 661 662
    src->cancel_signal(src, SIGHUP, sighandler_term, NULL);
    src->cancel_signal(src, SIGTERM, sighandler_term, NULL);
    src->cancel_signal(src, SIGINT, sighandler_term, NULL);
663
    oop_signal_delete(kom_signal_adapter);
664
    oop_sys_delete(kom_server_oop_src);
Per Cederqvist's avatar
Per Cederqvist committed
665

David Byers's avatar
David Byers committed
666 667
    /* Finish */

668 669
    dump_exit_statistics();

David Byers's avatar
David Byers committed
670
    kom_log("%s terminated normally.\n", argv[0]);
671 672 673 674 675

    /* Don't actually die until something is entered on stdin in debug
       mode.  This is mainly here for the benefit of the test suite,
       but is could also be useful to be able to attach a debugger and
       do pre-mortem debugging of the process at this point.  */
676
    if (foreground)
677
    {
David Byers's avatar
David Byers committed
678
	kom_log("Press enter to terminate lyskomd\n");
679 680
	getchar();
    }
David Byers's avatar
David Byers committed
681

682
    exit(0);
683 684 685 686 687 688 689 690
}

static void 
dump_exit_statistics(void)
{
    FILE *stat_file;
    time_t now;

691 692
    time(&now);
    stat_file = i_fopen(param.memuse_name, "a");
Per Cederqvist's avatar
Per Cederqvist committed
693 694 695 696

    if ( stat_file == NULL )
	restart_kom("Can't open file to save memory usage to.\n");

697 698
    fprintf(stat_file, "\nLysKOM Server going down at %s\n", ctime(&now));

699 700 701 702
    dump_cache_stats (stat_file);

    free_all_tmp();
    free_all_cache();
703
    free_all_jubel();
David Byers's avatar
David Byers committed
704 705 706
    free_kom_info();
    free_aux_item_definitions();
    free_configuration();
707
    free_default_config_file_name();
708

Per Cederqvist's avatar
Per Cederqvist committed
709 710
    dump_smalloc_counts(stat_file);
    dump_alloc_counts(stat_file);
711
    dump_cache_mem_usage(stat_file);
Per Cederqvist's avatar
Per Cederqvist committed
712
    dump_string_alloc_counts(stat_file);
713
    dump_allocated_connections(stat_file);
714
    dump_isc_alloc_counts(stat_file);
715
    dump_oop_alloc_counts(stat_file);
716
    dump_l2g_stats(stat_file);
717
    i_fclose (stat_file);
Per Cederqvist's avatar
Per Cederqvist committed
718
}
David Byers's avatar
David Byers committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

static void
free_kom_info(void)
{
    unsigned long i;

    if (kom_info.aux_item_list.items != NULL)
    {
        for (i = 0; i < kom_info.aux_item_list.length; i++)
        {
            s_clear(&kom_info.aux_item_list.items[i].data);
        }
        sfree(kom_info.aux_item_list.items);
    }
    kom_info.aux_item_list.length = 0;
    kom_info.aux_item_list.items = 0;
}