Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,2661 +0,0 @@
1
-/*
2
- * Copyright (C) 2001-2003 FhG Fokus
3
- *
4
- * This file is part of Kamailio, a free SIP server.
5
- *
6
- * Kamailio is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version
10
- *
11
- * Kamailio is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, write to the Free Software
18
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19
- *
20
- */
21
-
22
-/** Kamailio core :: main file (init, daemonize, startup)
23
- * @file main.c
24
- * @ingroup core
25
- * Module: core
26
- */
27
-
28
-/*! @defgroup core Kamailio core
29
- *
30
- * sip router core part.
31
- */
32
-
33
-#include <stdio.h>
34
-#include <stdlib.h>
35
-#include <errno.h>
36
-#include <ctype.h>
37
-#include <string.h>
38
-#include <netdb.h>
39
-#include <unistd.h>
40
-#include <sys/types.h>
41
-#include <sys/socket.h>
42
-#if defined(HAVE_NETINET_IN_SYSTM)
43
-#include <netinet/in_systm.h>
44
-#endif
45
-#include <netinet/in.h>
46
-#include <netinet/ip.h>
47
-#include <arpa/inet.h>
48
-#include <sys/utsname.h>
49
-#include <sys/stat.h>
50
-#include <sys/mman.h>
51
-#include <fcntl.h>
52
-#include <sys/time.h>
53
-#include <sys/wait.h>
54
-#include <pwd.h>
55
-#include <grp.h>
56
-#include <signal.h>
57
-
58
-#include <sys/ioctl.h>
59
-#include <net/if.h>
60
-#ifdef HAVE_SYS_SOCKIO_H
61
-#include <sys/sockio.h>
62
-#endif
63
-
64
-#include "config.h"
65
-#include "dprint.h"
66
-#include "daemonize.h"
67
-#include "route.h"
68
-#include "udp_server.h"
69
-#include "globals.h"
70
-#include "mem/mem.h"
71
-#ifdef SHM_MEM
72
-#include "mem/shm_mem.h"
73
-#include "shm_init.h"
74
-#endif /* SHM_MEM */
75
-#include "sr_module.h"
76
-#include "timer.h"
77
-#include "parser/msg_parser.h"
78
-#include "ip_addr.h"
79
-#include "resolve.h"
80
-#include "parser/parse_hname2.h"
81
-#include "parser/digest/digest_parser.h"
82
-#include "name_alias.h"
83
-#include "hash_func.h"
84
-#include "pt.h"
85
-#include "script_cb.h"
86
-#include "nonsip_hooks.h"
87
-#include "ut.h"
88
-#include "events.h"
89
-#include "signals.h"
90
-#ifdef USE_RAW_SOCKS
91
-#include "raw_sock.h"
92
-#endif /* USE_RAW_SOCKS */
93
-#ifdef USE_TCP
94
-#include "poll_types.h"
95
-#include "tcp_init.h"
96
-#include "tcp_options.h"
97
-#ifdef CORE_TLS
98
-#include "tls/tls_init.h"
99
-#define tls_has_init_si() 1
100
-#define tls_loaded() 1
101
-#else
102
-#include "tls_hooks_init.h"
103
-#endif /* CORE_TLS */
104
-#endif /* USE_TCP */
105
-#ifdef USE_SCTP
106
-#include "sctp_core.h"
107
-#endif
108
-#include "usr_avp.h"
109
-#include "rpc_lookup.h"
110
-#include "core_cmd.h"
111
-#include "flags.h"
112
-#include "lock_ops_init.h"
113
-#include "atomic_ops_init.h"
114
-#ifdef USE_DNS_CACHE
115
-#include "dns_cache.h"
116
-#endif
117
-#ifdef USE_DST_BLACKLIST
118
-#include "dst_blacklist.h"
119
-#endif
120
-#include "rand/fastrand.h" /* seed */
121
-#include "rand/kam_rand.h"
122
-
123
-#include "stats.h"
124
-#include "counters.h"
125
-#include "cfg/cfg.h"
126
-#include "cfg/cfg_struct.h"
127
-#include "cfg_core.h"
128
-#include "endianness.h" /* init */
129
-#include "basex.h" /* init */
130
-#include "pvapi.h" /* init PV api */
131
-#include "pv_core.h" /* register core pvars */
132
-#include "ppcfg.h"
133
-#include "sock_ut.h"
134
-#include "async_task.h"
135
-#include "dset.h"
136
-#include "timer_proc.h"
137
-#include "srapi.h"
138
-
139
-#ifdef DEBUG_DMALLOC
140
-#include <dmalloc.h>
141
-#endif
142
-#include "ver.h"
143
-
144
-/* define SIG_DEBUG by default */
145
-#ifdef NO_SIG_DEBUG
146
-#undef SIG_DEBUG
147
-#else
148
-#define SIG_DEBUG
149
-#endif
150
-
151
-
152
-
153
-static char help_msg[]= "\
154
-Usage: " NAME " [options]\n\
155
-Options:\n\
156
-    -f file      Configuration file (default: " CFG_FILE ")\n\
157
-    -L path      Modules search path (default: " MODS_DIR ")\n\
158
-    -c           Check configuration file for errors\n\
159
-    -l address   Listen on the specified address/interface (multiple -l\n\
160
-                  mean listening on more addresses).  The address format is\n\
161
-                  [proto:]addr_lst[:port], where proto=udp|tcp|tls|sctp, \n\
162
-                  addr_lst= addr|(addr, addr_lst) and \n\
163
-                  addr= host|ip_address|interface_name. \n\
164
-                  E.g: -l locahost, -l udp:127.0.0.1:5080, -l eth0:5062,\n\
165
-                  -l \"sctp:(eth0)\", -l \"(eth0, eth1, 127.0.0.1):5065\".\n\
166
-                  The default behaviour is to listen on all the interfaces.\n\
167
-    -n processes Number of child processes to fork per interface\n\
168
-                  (default: 8)\n\
169
-    -r           Use dns to check if is necessary to add a \"received=\"\n\
170
-                  field to a via\n\
171
-    -R           Same as `-r` but use reverse dns;\n\
172
-                  (to use both use `-rR`)\n\
173
-    -K           Turn on \"via:\" host checking when forwarding replies\n\
174
-    -d           Debugging mode (multiple -d increase the level)\n\
175
-    -D           Control how daemonize is done:\n\
176
-                  -D..do not fork (almost) anyway;\n\
177
-                  -DD..do not daemonize creator;\n\
178
-                  -DDD..daemonize (default)\n\
179
-    -E           Log to stderr\n\
180
-    -e           Log messages printed in terminal colors (requires -E)\n"
181
-#ifdef USE_TCP
182
-"    -T           Disable tcp\n\
183
-    -N           Number of tcp child processes (default: equal to `-n')\n\
184
-    -W type      poll method (depending on support in OS, it can be: poll,\n\
185
-                  epoll_lt, epoll_et, sigio_rt, select, kqueue, /dev/poll)\n"
186
-#endif
187
-#ifdef USE_SCTP
188
-"    -S           disable sctp\n\
189
-    -Q            Number of sctp child processes (default: equal to `-n')\n"
190
-#endif /* USE_SCTP */
191
-"    -v (-V)      Version number\n\
192
-    -h           This help message\n\
193
-    -I           Print more internal compile flags and options\n\
194
-    -b nr        Maximum receive buffer size which will not be exceeded by\n\
195
-                  auto-probing procedure even if  OS allows\n\
196
-    -m nr        Size of shared memory allocated in Megabytes\n\
197
-    -M nr        Size of private memory allocated, in Megabytes\n\
198
-    -w dir       Change the working directory to \"dir\" (default: \"/\")\n\
199
-    -t dir       Chroot to \"dir\"\n\
200
-    -u uid       Change uid \n\
201
-    -g gid       Change gid \n\
202
-    -P file      Create a pid file\n\
203
-    -G file      Create a pgid file\n\
204
-    -Y dir       Runtime dir\n\
205
-    -O nr        Script optimization level (debugging option)\n\
206
-    -a mode      Auto aliases mode: enable with yes or on,\n\
207
-                  disable with no or off\n\
208
-    -A define    Add config pre-processor define (e.g., -A WITH_AUTH)\n\
209
-    -x name      Specify internal manager for shared memory (shm)\n\
210
-                  - can be: fm, qm or tlsf\n\
211
-    -X name      Specify internal manager for private memory (pkg)\n\
212
-                  - if omitted, the one for shm is used\n"
213
-#ifdef STATS
214
-"    -s file     File to which statistics is dumped (disabled otherwise)\n"
215
-#endif
216
-;
217
-
218
-
219
-/* print compile-time constants */
220
-void print_ct_constants(void)
221
-{
222
-#ifdef ADAPTIVE_WAIT
223
-	printf("ADAPTIVE_WAIT_LOOPS=%d, ", ADAPTIVE_WAIT_LOOPS);
224
-#endif
225
-/*
226
-#ifdef SHM_MEM
227
-	printf("SHM_MEM_SIZE=%d, ", SHM_MEM_SIZE);
228
-#endif
229
-*/
230
-	printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
231
-			" MAX_URI_SIZE %d, BUF_SIZE %d, DEFAULT PKG_SIZE %uMB\n",
232
-		MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
233
-		BUF_SIZE, PKG_MEM_SIZE);
234
-#ifdef USE_TCP
235
-	printf("poll method support: %s.\n", poll_support);
236
-#endif
237
-}
238
-
239
-/* print compile-time constants */
240
-void print_internals(void)
241
-{
242
-	printf("Print out of %s internals\n", NAME);
243
-	printf("  Version: %s\n", full_version);
244
-	printf("  Default config: %s\n", CFG_FILE);
245
-	printf("  Default paths to modules: %s\n", MODS_DIR);
246
-	printf("  Compile flags: %s\n", ver_flags );
247
-	printf("  MAX_RECV_BUFFER_SIZE=%d\n", MAX_RECV_BUFFER_SIZE);
248
-	printf("  MAX_LISTEN=%d\n", MAX_LISTEN);
249
-	printf("  MAX_URI_SIZE=%d\n", MAX_URI_SIZE);
250
-	printf("  BUF_SIZE=%d\n", BUF_SIZE);
251
-	printf("  DEFAULT PKG_SIZE=%uMB\n", PKG_MEM_SIZE);
252
-#ifdef SHM_MEM
253
-	printf("  DEFAULT SHM_SIZE=%uMB\n", SHM_MEM_SIZE);
254
-#endif
255
-#ifdef ADAPTIVE_WAIT
256
-	printf("  ADAPTIVE_WAIT_LOOPS=%d\n", ADAPTIVE_WAIT_LOOPS);
257
-#endif
258
-#ifdef USE_TCP
259
-	printf("  TCP poll methods: %s\n", poll_support);
260
-#endif
261
-	printf("  Source code revision ID: %s\n", ver_id);
262
-	printf("  Compiled with: %s\n", ver_compiler);
263
-	printf("  Compiled on: %s\n", ver_compiled_time);
264
-	printf("Thank you for flying %s!\n", NAME);
265
-}
266
-
267
-/* debugging function */
268
-/*
269
-void receive_stdin_loop(void)
270
-{
271
-	#define BSIZE 1024
272
-	char buf[BSIZE+1];
273
-	int len;
274
-
275
-	while(1){
276
-		len=fread(buf,1,BSIZE,stdin);
277
-		buf[len+1]=0;
278
-		receive_msg(buf, len);
279
-		printf("-------------------------\n");
280
-	}
281
-}
282
-*/
283
-
284
-/* global vars */
285
-
286
-int own_pgid = 0; /* whether or not we have our own pgid (and it's ok
287
-					 to use kill(0, sig) */
288
-
289
-char* mods_dir = MODS_DIR;  /* search path for dyn. loadable modules */
290
-int   mods_dir_cmd = 0; /* mods dir path set in command lin e*/
291
-
292
-char* cfg_file = 0;
293
-unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
294
-												  not want to exceed during the
295
-												  auto-probing procedure; may
296
-												  be re-configured */
297
-unsigned int sql_buffer_size = 65535; /* Size for the SQL buffer. Defaults to 64k. 
298
-                                         This may be re-configured */
299
-int socket_workers = 0;		/* number of workers processing requests for a socket
300
-							   - it's reset everytime with a new listen socket */
301
-int children_no = 0;		/* number of children processing requests */
302
-#ifdef USE_TCP
303
-int tcp_cfg_children_no = 0; /* set via config or command line option */
304
-int tcp_children_no = 0; /* based on socket_workers and tcp_cfg_children_no */
305
-int tcp_disable = 0; /* 1 if tcp is disabled */
306
-#endif
307
-#ifdef USE_TLS
308
-#ifdef	CORE_TLS
309
-int tls_disable = 0;  /* tls enabled by default */
310
-#else
311
-int tls_disable = 1;  /* tls disabled by default */
312
-#endif /* CORE_TLS */
313
-#endif /* USE_TLS */
314
-#ifdef USE_SCTP
315
-int sctp_children_no = 0;
316
-int sctp_disable = 2; /* 1 if sctp is disabled, 2 if auto mode, 0 enabled */
317
-#endif /* USE_SCTP */
318
-
319
-struct process_table *pt=0;		/*array with children pids, 0= main proc,
320
-									alloc'ed in shared mem if possible*/
321
-int *process_count = 0;			/* Total number of SER processes currently
322
-								   running */
323
-gen_lock_t* process_lock;		/* lock on the process table */
324
-int process_no = 0;				/* index of process in the pt */
325
-
326
-time_t up_since;
327
-int sig_flag = 0;              /* last signal received */
328
-int dont_fork = 0;
329
-int dont_daemonize = 0;
330
-int log_stderr = 0;
331
-int log_color = 0;
332
-/* set custom app name for syslog printing */
333
-char *log_name = 0;
334
-char *log_prefix_fmt = 0;
335
-pid_t creator_pid = (pid_t) -1;
336
-int config_check = 0;
337
-/* check if reply first via host==us */
338
-int check_via =  0;
339
-/* translate user=phone URIs to TEL URIs */
340
-int phone2tel = 1;
341
-/* debugging level for timer debugging */
342
-int timerlog = L_WARN;
343
-/* should replies include extensive warnings? by default no,
344
-   good for trouble-shooting
345
-*/
346
-int sip_warning = 0;
347
-/* should localy-generated messages include server's signature?
348
-   be default yes, good for trouble-shooting
349
-*/
350
-int server_signature=1;
351
-str server_hdr = {SERVER_HDR, SERVER_HDR_LEN};
352
-str user_agent_hdr = {USER_AGENT, USER_AGENT_LEN};
353
-str version_table = {VERSION_TABLE, VERSION_TABLE_LEN};
354
-/* should ser try to locate outbound interface on multihomed
355
- * host? by default not -- too expensive
356
- */
357
-int mhomed=0;
358
-/* use dns and/or rdns or to see if we need to add
359
-   a ;received=x.x.x.x to via: */
360
-int received_dns = 0;
361
-/* add or not the rev dns names to aliases list */
362
-int sr_auto_aliases=1;
363
-char* working_dir = 0;
364
-char* chroot_dir = 0;
365
-char* runtime_dir = "" RUN_DIR;
366
-char* user=0;
367
-char* group=0;
368
-int uid = 0;
369
-int gid = 0;
370
-char* sock_user=0;
371
-char* sock_group=0;
372
-int sock_uid= -1;
373
-int sock_gid= -1;
374
-int sock_mode= S_IRUSR| S_IWUSR| S_IRGRP| S_IWGRP; /* rw-rw---- */
375
-
376
-int server_id = 0; /* Configurable unique ID of the server */
377
-
378
-/* maximum number of branches for transaction */
379
-unsigned int sr_dst_max_branches = MAX_BRANCHES_DEFAULT;
380
-
381
-/* set timeval for each received sip message */
382
-int sr_msg_time = 1;
383
-
384
-/* onsend_route is executed for replies*/
385
-int onsend_route_reply = 0;
386
-
387
-/* more config stuff */
388
-int disable_core_dump=0; /* by default enabled */
389
-int open_files_limit=-1; /* don't touch it by default */
390
-
391
-/* memory options */
392
-int shm_force_alloc=0; /* force immediate (on startup) page allocation
393
-						  (by writting 0 in the pages), useful if
394
-						  mlock_pages is also 1 */
395
-int mlock_pages=0; /* default off, try to disable swapping */
396
-
397
-/* real time options */
398
-int real_time=0; /* default off, flags: 1 on only timer, 2  slow timer,
399
-										4 all procs (7=all) */
400
-int rt_prio=0;
401
-int rt_policy=0; /* SCHED_OTHER */
402
-int rt_timer1_prio=0;  /* "fast" timer */
403
-int rt_timer2_prio=0;  /* "slow" timer */
404
-int rt_timer1_policy=0; /* "fast" timer, SCHED_OTHER */
405
-int rt_timer2_policy=0; /* "slow" timer, SCHED_OTHER */
406
-
407
-
408
-/* a hint to reply modules whether they should send reply
409
-   to IP advertised in Via or IP from which a request came
410
-*/
411
-int reply_to_via=0;
412
-
413
-#ifdef USE_MCAST
414
-int mcast_loopback = 0;
415
-int mcast_ttl = -1; /* if -1, don't touch it, use the default (usually 1) */
416
-char* mcast = 0;
417
-#endif /* USE_MCAST */
418
-
419
-int tos = IPTOS_LOWDELAY;
420
-int pmtu_discovery = 0;
421
-
422
-int auto_bind_ipv6 = 0;
423
-
424
-#if 0
425
-char* names[MAX_LISTEN];              /* our names */
426
-int names_len[MAX_LISTEN];            /* lengths of the names*/
427
-struct ip_addr addresses[MAX_LISTEN]; /* our ips */
428
-int addresses_no=0;                   /* number of names/ips */
429
-#endif
430
-struct socket_info* udp_listen=0;
431
-#ifdef USE_TCP
432
-int tcp_main_pid=0; /* set after the tcp main process is started */
433
-struct socket_info* tcp_listen=0;
434
-#endif
435
-#ifdef USE_TLS
436
-struct socket_info* tls_listen=0;
437
-#endif
438
-#ifdef USE_SCTP
439
-struct socket_info* sctp_listen=0;
440
-#endif
441
-struct socket_info* bind_address=0; /* pointer to the crt. proc.
442
-									 listening address*/
443
-struct socket_info* sendipv4; /* ipv4 socket to use when msg. comes from ipv6*/
444
-struct socket_info* sendipv6; /* same as above for ipv6 */
445
-#ifdef USE_RAW_SOCKS
446
-int raw_udp4_send_sock = -1; /* raw socket used for sending udp4 packets */
447
-#endif /* USE_RAW_SOCKS */
448
-#ifdef USE_TCP
449
-struct socket_info* sendipv4_tcp;
450
-struct socket_info* sendipv6_tcp;
451
-#endif
452
-#ifdef USE_TLS
453
-struct socket_info* sendipv4_tls;
454
-struct socket_info* sendipv6_tls;
455
-#endif
456
-#ifdef USE_SCTP
457
-struct socket_info* sendipv4_sctp;
458
-struct socket_info* sendipv6_sctp;
459
-#endif
460
-
461
-unsigned short port_no=0; /* default port*/
462
-#ifdef USE_TLS
463
-unsigned short tls_port_no=0; /* default port */
464
-#endif
465
-
466
-struct host_alias* aliases=0; /* name aliases list */
467
-
468
-/* Parameter to child_init */
469
-int child_rank = 0;
470
-
471
-/* how much to wait for children to terminate, before taking extreme measures*/
472
-int ser_kill_timeout=DEFAULT_SER_KILL_TIMEOUT;
473
-
474
-/* process_bm_t process_bit = 0; */
475
-#ifdef ROUTE_SRV
476
-#endif
477
-
478
-/* cfg parsing */
479
-int cfg_errors=0;
480
-int cfg_warnings=0;
481
-
482
-
483
-/* shared memory (in MB) */
484
-unsigned long shm_mem_size=0;
485
-/* private (pkg) memory (in MB) */
486
-unsigned long pkg_mem_size=0;
487
-
488
-/* export command-line to anywhere else */
489
-int my_argc;
490
-char **my_argv;
491
-
492
-/* set to 1 when the cfg framework and core cfg is initialized/registered */
493
-static int cfg_ok=0;
494
-
495
-#define MAX_FD 32 /* maximum number of inherited open file descriptors,
496
-		    (normally it shouldn't  be bigger  than 3) */
497
-
498
-
499
-extern FILE* yyin;
500
-extern int yyparse(void);
501
-
502
-
503
-int is_main=1; /* flag = is this the  "main" process? */
504
-int fixup_complete=0; /* flag = is the fixup complete ? */
505
-
506
-char* pid_file = 0; /* filename as asked by use */
507
-char* pgid_file = 0;
508
-
509
-
510
-/* memory manager */
511
-#define SR_MEMMNG_DEFAULT	"qm"
512
-
513
-char *sr_memmng_pkg = NULL;
514
-char *sr_memmng_shm = NULL;
515
-
516
-/* call it before exiting; if show_status==1, mem status is displayed */
517
-void cleanup(int show_status)
518
-{
519
-	int memlog;
520
-
521
-	/*clean-up*/
522
-#ifndef SHM_SAFE_MALLOC
523
-	if (_shm_lock)
524
-		shm_unlock(); /* hack: force-unlock the shared memory lock in case
525
-					 some process crashed and let it locked; this will
526
-					 allow an almost gracious shutdown */
527
-#endif
528
-	destroy_rpcs();
529
-	destroy_modules();
530
-#ifdef USE_DNS_CACHE
531
-	destroy_dns_cache();
532
-#endif
533
-#ifdef USE_DST_BLACKLIST
534
-	destroy_dst_blacklist();
535
-#endif
536
-	/* restore the original core configuration before the
537
-	 * config block is freed, otherwise even logging is unusable,
538
-	 * it can case segfault */
539
-	if (cfg_ok){
540
-		cfg_update();
541
-		/* copy current config into default_core_cfg */
542
-		if (core_cfg)
543
-			default_core_cfg=*((struct cfg_group_core*)core_cfg);
544
-	}
545
-	core_cfg = &default_core_cfg;
546
-	cfg_destroy();
547
-#ifdef USE_TCP
548
-	destroy_tcp();
549
-#ifdef USE_TLS
550
-	destroy_tls();
551
-#endif /* USE_TLS */
552
-#endif /* USE_TCP */
553
-#ifdef USE_SCTP
554
-	sctp_core_destroy();
555
-#endif
556
-	destroy_timer();
557
-	pv_destroy_api();
558
-	destroy_script_cb();
559
-	destroy_nonsip_hooks();
560
-	destroy_routes();
561
-	destroy_atomic_ops();
562
-	destroy_counters();
563
-	memlog=cfg_get(core, core_cfg, memlog);
564
-#ifdef PKG_MALLOC
565
-	if (show_status && memlog <= cfg_get(core, core_cfg, debug)){
566
-		if (cfg_get(core, core_cfg, mem_summary) & 1) {
567
-			LOG(memlog, "Memory status (pkg):\n");
568
-			pkg_status();
569
-		}
570
-		if (cfg_get(core, core_cfg, mem_summary) & 4) {
571
-			LOG(memlog, "Memory still-in-use summary (pkg):\n");
572
-			pkg_sums();
573
-		}
574
-	}
575
-#endif
576
-#ifdef SHM_MEM
577
-	if (pt) shm_free(pt);
578
-	pt=0;
579
-	if (show_status && memlog <= cfg_get(core, core_cfg, debug)){
580
-		if (cfg_get(core, core_cfg, mem_summary) & 2) {
581
-			LOG(memlog, "Memory status (shm):\n");
582
-			shm_status();
583
-		}
584
-		if (cfg_get(core, core_cfg, mem_summary) & 8) {
585
-			LOG(memlog, "Memory still-in-use summary (shm):\n");
586
-			shm_sums();
587
-		}
588
-	}
589
-	/* zero all shmem alloc vars that we still use */
590
-	shm_destroy_manager();
591
-#endif
592
-	destroy_lock_ops();
593
-	if (pid_file) unlink(pid_file);
594
-	if (pgid_file) unlink(pgid_file);
595
-	pkg_destroy_manager();
596
-}
597
-
598
-
599
-/* tries to send a signal to all our processes
600
- * if daemonized  is ok to send the signal to all the process group,
601
- * however if not daemonized we might end up sending the signal also
602
- * to the shell which launched us => most signals will kill it if
603
- * it's not in interactive mode and we don't want this. The non-daemonized
604
- * case can occur when an error is encountered before daemonize is called
605
- * (e.g. when parsing the config file) or when ser is started in "dont-fork"
606
- *  mode. Sending the signal to all the processes in pt[] will not work
607
- *  for processes forked from modules (which have no correspondent entry in
608
- *  pt), but this can happen only in dont_fork mode (which is only for
609
- *  debugging). So in the worst case + "dont-fork" we might leave some
610
- *  zombies. -- andrei */
611
-static void kill_all_children(int signum)
612
-{
613
-	int r;
614
-
615
-	if (own_pgid) kill(0, signum);
616
-	else if (pt){
617
-		 /* lock processes table only if this is a child process
618
-		  * (only main can add processes, so from main is safe not to lock
619
-		  *  and moreover it avoids the lock-holding suicidal children problem)
620
-		  */
621
-		if (!is_main) lock_get(process_lock);
622
-		for (r=1; r<*process_count; r++){
623
-			if (r==process_no) continue; /* try not to be suicidal */
624
-			if (pt[r].pid) {
625
-				kill(pt[r].pid, signum);
626
-			}
627
-			else LM_CRIT("killing: %s > %d no pid!!!\n",
628
-							pt[r].desc, pt[r].pid);
629
-		}
630
-		if (!is_main) lock_release(process_lock);
631
-	}
632
-}
633
-
634
-
635
-
636
-/* if this handler is called, a critical timeout has occurred while
637
- * waiting for the children to finish => we should kill everything and exit */
638
-static void sig_alarm_kill(int signo)
639
-{
640
-	kill_all_children(SIGKILL); /* this will kill the whole group
641
-								  including "this" process;
642
-								  for debugging replace with SIGABRT
643
-								  (but warning: it might generate lots
644
-								   of cores) */
645
-}
646
-
647
-
648
-/* like sig_alarm_kill, but the timeout has occurred when cleaning up
649
- * => try to leave a core for future diagnostics */
650
-static void sig_alarm_abort(int signo)
651
-{
652
-	/* LOG is not signal safe, but who cares, we are abort-ing anyway :-) */
653
-	LM_CRIT("shutdown timeout triggered, dying...");
654
-	abort();
655
-}
656
-
657
-
658
-
659
-static void shutdown_children(int sig, int show_status)
660
-{
661
-	kill_all_children(sig);
662
-	if (set_sig_h(SIGALRM, sig_alarm_kill) == SIG_ERR ) {
663
-		LM_ERR("could not install SIGALARM handler\n");
664
-		/* continue, the process will die anyway if no
665
-		 * alarm is installed which is exactly what we want */
666
-	}
667
-	alarm(ser_kill_timeout);
668
-	while((wait(0) > 0) || (errno==EINTR)); /* wait for all the
669
-											   children to terminate*/
670
-	set_sig_h(SIGALRM, sig_alarm_abort);
671
-	cleanup(show_status); /* cleanup & show status*/
672
-	alarm(0);
673
-	set_sig_h(SIGALRM, SIG_IGN);
674
-}
675
-
676
-
677
-
678
-void handle_sigs(void)
679
-{
680
-	pid_t	chld;
681
-	int	chld_status;
682
-	int memlog;
683
-
684
-	switch(sig_flag){
685
-		case 0: break; /* do nothing*/
686
-		case SIGPIPE:
687
-				/* SIGPIPE might be rarely received on use of
688
-				   exec module; simply ignore it
689
-				 */
690
-				LM_WARN("SIGPIPE received and ignored\n");
691
-				break;
692
-		case SIGINT:
693
-		case SIGTERM:
694
-			/* we end the program in all these cases */
695
-			if (sig_flag==SIGINT)
696
-				LM_DBG("INT received, program terminates\n");
697
-			else
698
-				LM_DBG("SIGTERM received, program terminates\n");
699
-			LM_NOTICE("Thank you for flying " NAME "!!!\n");
700
-			/* shutdown/kill all the children */
701
-			shutdown_children(SIGTERM, 1);
702
-			exit(0);
703
-			break;
704
-
705
-		case SIGUSR1:
706
-#ifdef STATS
707
-			dump_all_statistic();
708
-#endif
709
-		memlog=cfg_get(core, core_cfg, memlog);
710
-#ifdef PKG_MALLOC
711
-		if (memlog <= cfg_get(core, core_cfg, debug)){
712
-			if (cfg_get(core, core_cfg, mem_summary) & 1) {
713
-				LOG(memlog, "Memory status (pkg):\n");
714
-				pkg_status();
715
-			}
716
-			if (cfg_get(core, core_cfg, mem_summary) & 2) {
717
-				LOG(memlog, "Memory still-in-use summary (pkg):\n");
718
-				pkg_sums();
719
-			}
720
-		}
721
-#endif
722
-#ifdef SHM_MEM
723
-		if (memlog <= cfg_get(core, core_cfg, debug)){
724
-			if (cfg_get(core, core_cfg, mem_summary) & 1) {
725
-				LOG(memlog, "Memory status (shm):\n");
726
-				shm_status();
727
-			}
728
-			if (cfg_get(core, core_cfg, mem_summary) & 2) {
729
-				LOG(memlog, "Memory still-in-use summary (shm):\n");
730
-				shm_sums();
731
-			}
732
-		}
733
-#endif
734
-			break;
735
-
736
-		case SIGCHLD:
737
-			while ((chld=waitpid( -1, &chld_status, WNOHANG ))>0) {
738
-				if (WIFEXITED(chld_status))
739
-					LM_ALERT("child process %ld exited normally,"
740
-							" status=%d\n", (long)chld,
741
-							WEXITSTATUS(chld_status));
742
-				else if (WIFSIGNALED(chld_status)) {
743
-					LM_ALERT("child process %ld exited by a signal"
744
-							" %d\n", (long)chld, WTERMSIG(chld_status));
745
-#ifdef WCOREDUMP
746
-					LM_ALERT("core was %sgenerated\n",
747
-							 WCOREDUMP(chld_status) ?  "" : "not " );
748
-#endif
749
-				}else if (WIFSTOPPED(chld_status))
750
-					LM_ALERT("child process %ld stopped by a"
751
-								" signal %d\n", (long)chld,
752
-								 WSTOPSIG(chld_status));
753
-			}
754
-#ifndef STOP_JIRIS_CHANGES
755
-			if (dont_fork) {
756
-				LM_INFO("dont_fork turned on, living on\n");
757
-				break;
758
-			}
759
-			LM_INFO("terminating due to SIGCHLD\n");
760
-#endif
761
-			LM_DBG("terminating due to SIGCHLD\n");
762
-			/* exit */
763
-			shutdown_children(SIGTERM, 1);
764
-			if (WIFSIGNALED(chld_status)) {
765
-				exit(1);
766
-			} else {
767
-				exit(0);
768
-			}
769
-			break;
770
-
771
-		case SIGHUP: /* ignoring it*/
772
-					LM_DBG("SIGHUP received, ignoring it\n");
773
-					break;
774
-		default:
775
-			LM_CRIT("unhandled signal %d\n", sig_flag);
776
-	}
777
-	sig_flag=0;
778
-}
779
-
780
-
781
-
782
-/* added by jku; allows for regular exit on a specific signal;
783
-   good for profiling which only works if exited regularly and
784
-   not by default signal handlers
785
-    - modified by andrei: moved most of the stuff to handle_sigs,
786
-       made it safer for the "fork" case
787
-*/
788
-void sig_usr(int signo)
789
-{
790
-
791
-#ifdef PKG_MALLOC
792
-	int memlog;
793
-#endif
794
-
795
-	if (is_main){
796
-		if (sig_flag==0) sig_flag=signo;
797
-		else /*  previous sig. not processed yet, ignoring? */
798
-			return; ;
799
-		if (dont_fork)
800
-				/* only one proc, doing everything from the sig handler,
801
-				unsafe, but this is only for debugging mode*/
802
-			handle_sigs();
803
-	}else{
804
-		/* process the important signals */
805
-		switch(signo){
806
-			case SIGPIPE:
807
-#ifdef SIG_DEBUG /* signal unsafe stuff follows */
808
-					LM_INFO("signal %d received\n", signo);
809
-#endif
810
-				break;
811
-			case SIGINT:
812
-			case SIGTERM:
813
-#ifdef SIG_DEBUG /* signal unsafe stuff follows */
814
-					LM_INFO("signal %d received\n", signo);
815
-					/* print memory stats for non-main too */
816
-					#ifdef PKG_MALLOC
817
-					/* make sure we have current cfg values, but update only
818
-					  the safe part (values not requiring callbacks), to
819
-					  account for processes that might not have registered
820
-					  config support */
821
-					cfg_update_no_cbs();
822
-					memlog=cfg_get(core, core_cfg, memlog);
823
-					if (memlog <= cfg_get(core, core_cfg, debug)){
824
-						if (cfg_get(core, core_cfg, mem_summary) & 1) {
825
-							LOG(memlog, "Memory status (pkg):\n");
826
-							pkg_status();
827
-						}
828
-						if (cfg_get(core, core_cfg, mem_summary) & 2) {
829
-							LOG(memlog, "Memory still-in-use summary (pkg):"
830
-									"\n");
831
-							pkg_sums();
832
-						}
833
-					}
834
-					#endif
835
-#endif
836
-					_exit(0);
837
-					break;
838
-			case SIGUSR1:
839
-#ifdef PKG_MALLOC
840
-					cfg_update_no_cbs();
841
-					memlog=cfg_get(core, core_cfg, memlog);
842
-					if (memlog <= cfg_get(core, core_cfg, debug)){
843
-						if (cfg_get(core, core_cfg, mem_summary) & 1) {
844
-							LOG(memlog, "Memory status (pkg):\n");
845
-							pkg_status();
846
-						}
847
-						if (cfg_get(core, core_cfg, mem_summary) & 2) {
848
-							LOG(memlog, "Memory still-in-use summary (pkg):\n");
849
-							pkg_sums();
850
-						}
851
-					}
852
-#endif
853
-					break;
854
-				/* ignored*/
855
-			case SIGUSR2:
856
-			case SIGHUP:
857
-					break;
858
-			case SIGCHLD:
859
-#ifndef 			STOP_JIRIS_CHANGES
860
-#ifdef SIG_DEBUG /* signal unsafe stuff follows */
861
-					LM_DBG("SIGCHLD received: "
862
-						"we do not worry about grand-children\n");
863
-#endif
864
-#else
865
-					_exit(0); /* terminate if one child died */
866
-#endif
867
-					break;
868
-		}
869
-	}
870
-}
871
-
872
-
873
-
874
-/* install the signal handlers, returns 0 on success, -1 on error */
875
-int install_sigs(void)
876
-{
877
-	/* added by jku: add exit handler */
878
-	if (set_sig_h(SIGINT, sig_usr) == SIG_ERR ) {
879
-		ERR("no SIGINT signal handler can be installed\n");
880
-		goto error;
881
-	}
882
-	/* if we debug and write to a pipe, we want to exit nicely too */
883
-	if (set_sig_h(SIGPIPE, sig_usr) == SIG_ERR ) {
884
-		ERR("no SIGINT signal handler can be installed\n");
885
-		goto error;
886
-	}
887
-	if (set_sig_h(SIGUSR1, sig_usr)  == SIG_ERR ) {
888
-		ERR("no SIGUSR1 signal handler can be installed\n");
889
-		goto error;
890
-	}
891
-	if (set_sig_h(SIGCHLD , sig_usr)  == SIG_ERR ) {
892
-		ERR("no SIGCHLD signal handler can be installed\n");
893
-		goto error;
894
-	}
895
-	if (set_sig_h(SIGTERM , sig_usr)  == SIG_ERR ) {
896
-		ERR("no SIGTERM signal handler can be installed\n");
897
-		goto error;
898
-	}
899
-	if (set_sig_h(SIGHUP , sig_usr)  == SIG_ERR ) {
900
-		ERR("no SIGHUP signal handler can be installed\n");
901
-		goto error;
902
-	}
</