forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1412 lines (1244 loc) · 34.8 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2005-2006 Voice Sistem S.R.L
*
* This file is part of opensips, a free SIP server.
*
* opensips 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 2 of the License, or
* (at your option) any later version
*
* opensips 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* -------
* 2002-01-29 argc/argv globalized via my_{argc|argv} (jiri)
* 2003-01-23 mhomed added (jiri)
* 2003-03-19 replaced all malloc/frees w/ pkg_malloc/pkg_free (andrei)
* 2003-03-29 pkg cleaners for fifo and script callbacks introduced (jiri)
* 2003-03-31 removed snmp part (obsolete & no place in core) (andrei)
* 2003-04-06 child_init called in all processes (janakj)
* 2003-04-08 init_mallocs split into init_{pkg,shm}_mallocs and
* init_shm_mallocs called after cmd. line parsing (andrei)
* 2003-04-15 added tcp_disable support (andrei)
* 2003-05-09 closelog() before openlog to force opening a new fd
* (needed on solaris) (andrei)
* 2003-06-11 moved all signal handlers init. in install_sigs and moved it
* after daemonize (so that we won't catch anymore our own
* SIGCHLD generated when becoming session leader) (andrei)
* changed is_main default value to 1 (andrei)
* 2003-06-28 kill_all_children is now used instead of kill(0, sig)
* see comment above it for explanations. (andrei)
* 2003-06-29 replaced port_no_str snprintf w/ int2str (andrei)
* 2003-10-10 added switch for config check (-c) (andrei)
* 2003-10-24 converted to the new socket_info lists (andrei)
* 2004-03-30 core dump is enabled by default
* added support for increasing the open files limit (andrei)
* 2004-04-28 sock_{user,group,uid,gid,mode} added
* user2uid() & user2gid() added (andrei)
* 2004-09-11 added timeout on children shutdown and final cleanup
* (if it takes more than 60s => something is definitely wrong
* => kill all or abort) (andrei)
* force a shm_unlock before cleaning-up, in case we have a
* crashed childvwhich still holds the lock (andrei)
* 2004-12-02 removed -p, extended -l to support [proto:]address[:port],
* added parse_phostport, parse_proto (andrei)
* 2005-06-16 always record the pid in pt[process_no].pid twice: once in the
* parent & once in the child to avoid a short window when one
* of them might use it "unset" (andrei)
* 2005-12-22 added tos configurability (thanks to Andreas Granig)
* 2006-04-26 2-stage TLS init: before and after config file parsing (klaus)
*/
/*!
* \file main.c
* \brief Command line parsing, initializiation and server startup.
*
* Contains methods for parsing the command line, the initialization of
* the execution environment (signals, config file parsing) and forking
* the TCP, UDP, timer and fifo children.
*/
#include "reactor_defs.h" /*keep this first*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#include "help_msg.h"
#include "config.h"
#include "dprint.h"
#include "daemonize.h"
#include "route.h"
#include "bin_interface.h"
#include "globals.h"
#include "mem/mem.h"
#include "mem/shm_mem.h"
#include "sr_module.h"
#include "timer.h"
#include "ipc.h"
#include "parser/msg_parser.h"
#include "ip_addr.h"
#include "resolve.h"
#include "parser/parse_hname2.h"
#include "parser/digest/digest_parser.h"
#include "name_alias.h"
#include "hash_func.h"
#include "pt.h"
#include "script_cb.h"
#include "dset.h"
#include "blacklists.h"
#include "xlog.h"
#include "ipc.h"
#include "pt.h"
#include "ut.h"
#include "serialize.h"
#include "statistics.h"
#include "core_stats.h"
#include "pvar.h"
#include "poll_types.h"
#include "net/net_tcp.h"
#include "net/net_udp.h"
#include "version.h"
#include "mi/mi_core.h"
#include "db/db_insertq.h"
#include "net/trans.h"
#include "test/unit_tests.h"
/*
* when enabled ("-T" cmdline param), OpenSIPS startup will unfold as follows:
* - enable debug mode
* - fork workers normally
* - run all currently enabled unit tests
* - print the unit test summary
* - exit with 0 on success, non-zero otherwise
*/
int testing_framework;
static char* version=OPENSIPS_FULL_VERSION;
static char* flags=OPENSIPS_COMPILE_FLAGS;
#ifdef VERSION_NODATE
char compiled[] = "" ;
#else
#ifdef VERSION_DATE
const char compiled[] = VERSION_DATE ;
#else
const char compiled[] = __TIME__ " " __DATE__ ;
#endif
#endif
/**
* Print compile-time constants
*/
void print_ct_constants(void)
{
#ifdef ADAPTIVE_WAIT
printf("ADAPTIVE_WAIT_LOOPS=%d, ", ADAPTIVE_WAIT_LOOPS);
#endif
printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
" MAX_URI_SIZE %d, BUF_SIZE %d\n",
MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
BUF_SIZE );
printf("poll method support: %s.\n", poll_support);
#ifdef VERSIONTYPE
printf("%s revision: %s\n", VERSIONTYPE, THISREVISION);
#endif
}
/* global vars */
int own_pgid = 0; /* whether or not we have our own pgid (and it's ok
to use kill(0, sig) */
char* cfg_file = 0;
unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
not want to exceed during the
auto-probing procedure; may
be re-configured */
int children_no = CHILD_NO; /* number of children processing requests */
enum poll_types io_poll_method=0; /*!< by default choose the best method */
int sig_flag = 0; /* last signal received */
/* activate debug mode */
int debug_mode = 0;
/* do not become daemon, stay attached to the console */
int no_daemon_mode = 0;
/* assertion statements in script. disabled by default */
int enable_asserts = 0;
/* abort process on failed assertion. disabled by default */
int abort_on_assert = 0;
/* start by logging to stderr */
int log_stderr = 1;
/* log facility (see syslog(3)) */
int log_facility = LOG_DAEMON;
/* the id to be printed in syslog */
char *log_name = 0;
int config_check = 0;
/* check if reply first via host==us */
int check_via = 0;
/* debugging level for memory stats */
int memlog = L_DBG + 11;
int memdump = L_DBG + 10;
/* debugging in case msg processing takes. too long disabled by default */
int execmsgthreshold = 0;
/* debugging in case dns takes too long. disabled by default */
int execdnsthreshold = 0;
/* debugging in case tcp stuff take too long. disabled by default */
int tcpthreshold = 0;
/* should replies include extensive warnings? by default yes,
good for trouble-shooting
*/
int sip_warning = 0;
/* should localy-generated messages include server's signature? */
int server_signature=1;
/* Server header to be used when proxy generates request as UAS.
Default is to use SERVER_HDR CRLF (assigned later).
*/
str server_header = {SERVER_HDR,sizeof(SERVER_HDR)-1};
/* User-Agent header to be used when proxy generates request as UAC.
Default is to use USER_AGENT CRLF (assigned later).
*/
str user_agent_header = {USER_AGENT,sizeof(USER_AGENT)-1};
/* should opensips try to locate outbound interface on multihomed
* host? by default not -- too expensive
*/
int mhomed=0;
/* use dns and/or rdns or to see if we need to add
a ;received=x.x.x.x to via: */
int received_dns = 0;
char* working_dir = 0;
char* chroot_dir = 0;
char* user=0;
char* group=0;
int uid = 0;
int gid = 0;
/* more config stuff */
int disable_core_dump=0; /* by default enabled */
int open_files_limit=-1; /* don't touch it by default */
#ifdef USE_MCAST
int mcast_loopback = 0;
int mcast_ttl = -1; /* if -1, don't touch it, use the default (usually 1) */
#endif /* USE_MCAST */
int tos = IPTOS_LOWDELAY;
struct socket_info* bind_address=0; /* pointer to the crt. proc.
listening address*/
/* if aliases should be automatically discovered and added
* during fixing listening sockets */
int auto_aliases=1;
/* if the stateless forwarding support in core should be
* disabled or not */
int sl_fwd_disabled=-1;
/* process number - 0 is the main process */
int process_no = 0;
/* cfg parsing */
int cfg_errors=0;
/* start-up time */
time_t startup_time = 0;
/* shared memory (in MB) */
unsigned long shm_mem_size=SHM_MEM_SIZE * 1024 * 1024;
unsigned int shm_hash_split_percentage = DEFAULT_SHM_HASH_SPLIT_PERCENTAGE;
unsigned int shm_secondary_hash_size = DEFAULT_SHM_SECONDARY_HASH_SIZE;
/* packaged memory (in MB) */
unsigned long pkg_mem_size=PKG_MEM_SIZE * 1024 * 1024;
/* export command-line to anywhere else */
int my_argc;
char **my_argv;
extern FILE* yyin;
extern int yyparse();
#ifdef DEBUG_PARSER
extern int yydebug;
#endif
int is_main = 1; /* flag = is this the "main" process? */
char* pid_file = 0; /* filename as asked by user */
char* pgid_file = 0;
/**
* Clean up on exit. This should be called before exiting.
* \param show_status set to one to display the mem status
*/
void cleanup(int show_status)
{
LM_INFO("cleanup\n");
/*clean-up*/
/* hack: force-unlock the shared memory lock in case
some process crashed and let it locked; this will
allow an almost gracious shutdown */
if (mem_lock)
#ifdef HP_MALLOC
{
int i;
for (i = 0; i < HP_HASH_SIZE; i++)
shm_unlock(i);
}
#else
shm_unlock();
#endif
handle_ql_shutdown();
destroy_modules();
udp_destroy();
tcp_destroy();
destroy_timer();
destroy_stats_collector();
destroy_script_cb();
pv_free_extra_list();
tr_free_extra_list();
destroy_argv_list();
destroy_black_lists();
#ifdef PKG_MALLOC
if (show_status){
LM_GEN1(memdump, "Memory status (pkg):\n");
pkg_status();
}
#endif
cleanup_log_level();
if (mem_lock && pt)
shm_free(pt);
pt=0;
if (show_status){
LM_GEN1(memdump, "Memory status (shm):\n");
shm_status();
}
/* zero all shmem alloc vars that we still use */
shm_mem_destroy();
if (pid_file) unlink(pid_file);
if (pgid_file) unlink(pgid_file);
}
/**
* Send a signal to all child processes
* \param signum signal for killing the children
*/
static void kill_all_children(int signum)
{
int r;
if (!pt)
return;
for (r = 1; r < counted_processes; r++) {
if (pt[r].pid == -1 || (pt[r].flags & OSS_TAKING_A_DUMP))
continue;
/* as the PIDs are filled in by child processes, a 0 PID means
* an un-initalized procees; killing an uninitialized proc is
* very dangerous, so better wait for it to finish its init
* sequence by blocking until the pid is populated */
while (pt[r].pid == 0)
usleep(1000);
kill(pt[r].pid, signum);
}
}
/**
* SIGALRM "timeout" handler during the attendant's final cleanup,
* try to leave a core for future diagnostics.
*/
static void sig_alarm_abort(int signo)
{
/* LOG is not signal safe, but who cares, we are abort-ing anyway :-) */
LM_CRIT("BUG - shutdown timeout triggered, dying...\n");
abort();
}
/* RPC function send by main process to all worker processes supporting
* IPC in order to force a gracefull termination
*/
static void rpc_process_terminate(int sender_id, void *code)
{
#ifdef PKG_MALLOC
LM_GEN1(memdump, "Memory status (pkg):\n");
pkg_status();
#endif
/* simply terminate the process */
LM_DBG("Process %d exiting with code %d...\n",
process_no, (int)(long)code);
exit( (int)(long)code );
}
/* Implements full shutdown sequence (terminate processes and cleanup)
* To be called ONLY from MAIN process, not from workers !!!
*/
static void shutdown_opensips( int status )
{
pid_t proc;
int i, n, p;
int chld_status;
set_osips_state( STATE_TERMINATING );
/* terminate all processes */
/* first we try to terminate the processes via the IPC channel */
for( i=1,n=0 ; i<counted_processes; i++) {
/* Depending on the processes status, its PID may be:
* -1 - process not forked yet
* 0 - process forked but not fully configured by core
* >0 - process fully running
*/
if (pt[i].pid!=-1) {
/* use IPC (if avaiable) for a graceful termination */
if ( IPC_FD_WRITE(i)>0 ) {
LM_DBG("Asking process %d [%s] to terminate\n", i, pt[i].desc);
if (ipc_send_rpc( i, rpc_process_terminate, (void*)0)<0) {
LM_ERR("failed to trigger RPC termination for "
"process %d\n", i );
}
} else {
while (pt[i].pid==0) usleep(1000);
kill(pt[i].pid, SIGTERM);
}
n++;
}
}
/* now wait for the processes to finish */
i = GRACEFUL_SHUTDOWN_TIMEOUT * 100;
while( i && n ) {
proc = waitpid( -1, &chld_status, WNOHANG);
if (proc<=0) {
/* no process exited so far, do a small sleep before retry */
usleep(10000);
i--;
} else {
if ( (p=get_process_ID_by_PID(proc)) == -1 ) {
LM_DBG("unknown child process %d ended. Ignoring\n",proc);
} else {
LM_INFO("process %d(%d) [%s] terminated, "
"still waiting for %d more\n", p, proc, pt[p].desc, n-1);
/* mark the child process as terminated / not running */
pt[p].pid = -1;
status |= chld_status;
n--;
}
}
}
if (i==0 && n!=0) {
LM_DBG("force termination for all processes\n");
kill_all_children(SIGKILL);
}
/* Only one process is running now. Clean up and return overall status */
signal(SIGALRM, sig_alarm_abort);
alarm(SHUTDOWN_TIMEOUT - i / 100);
cleanup(1);
alarm(0);
signal(SIGALRM, SIG_IGN);
dprint("Thank you for running " NAME "\n");
exit( status );
}
/**
* Signal handler for the server.
*/
void handle_sigs(void)
{
pid_t chld;
int chld_status,overall_status=0;
int i;
int do_exit;
switch(sig_flag){
case 0: break; /* do nothing*/
case SIGPIPE:
/* SIGPIPE might be rarely received on use of
exec module; simply ignore it
*/
case SIGUSR1:
case SIGUSR2:
case SIGHUP:
/* ignoring it*/
break;
case SIGINT:
case SIGTERM:
/* we end the program in all these cases */
if (sig_flag==SIGINT)
LM_DBG("SIGINT received, program terminates\n");
else
LM_DBG("SIGTERM received, program terminates\n");
shutdown_opensips( 0/*status*/ );
break;
case SIGCHLD:
do_exit = 0;
while ((chld=waitpid( -1, &chld_status, WNOHANG ))>0) {
/* is it a process we know about? */
if ( (i=get_process_ID_by_PID( chld )) == -1 ) {
LM_DBG("unknown child process %d ended. Ignoring\n",chld);
continue;
}
do_exit = 1;
/* process the signal */
overall_status |= chld_status;
LM_DBG("OpenSIPS exit status = %d\n",overall_status);
if (WIFEXITED(chld_status))
LM_INFO("child process %d exited normally,"
" status=%d\n", chld,
WEXITSTATUS(chld_status));
else if (WIFSIGNALED(chld_status)) {
LM_INFO("child process %d exited by a signal"
" %d\n", chld, WTERMSIG(chld_status));
#ifdef WCOREDUMP
LM_INFO("core was %sgenerated\n",
WCOREDUMP(chld_status) ? "" : "not " );
#endif
}else if (WIFSTOPPED(chld_status))
LM_INFO("child process %d stopped by a"
" signal %d\n", chld,
WSTOPSIG(chld_status));
/* mark the child process as terminated / not running */
pt[i].pid = -1;
}
if (!do_exit)
break;
LM_INFO("terminating due to SIGCHLD\n");
/* exit */
shutdown_opensips( overall_status );
break;
default:
LM_CRIT("unhandled signal %d\n", sig_flag);
}
sig_flag=0;
}
/* the initial SIGSEGV handler, provided by the OS */
static struct sigaction sa_sys_segv;
static char sa_sys_is_valid;
static inline int restore_segv_handler(void)
{
LM_DBG("restoring SIGSEGV handler...\n");
if (!sa_sys_is_valid)
return 1;
if (sigaction(SIGSEGV, &sa_sys_segv, NULL) < 0) {
LM_ERR("failed to restore system SIGSEGV handler\n");
return -1;
}
LM_DBG("successfully restored system SIGSEGV handler\n");
return 0;
}
/**
* Exit regulary on a specific signal.
* This is good for profiling which only works if exited regularly
* and not by default signal handlers
* \param signo The signal that should be handled
*/
static void sig_usr(int signo)
{
int status;
pid_t pid;
UNUSED(pid);
if (is_main){
if (signo == SIGSEGV) {
LM_CRIT("segfault in attendant (starter) process!\n");
if (restore_segv_handler() != 0)
exit(-1);
return;
}
if (sig_flag == 0)
sig_flag = signo;
else /* previous sig. not processed yet, ignoring? */
return;
}else{
/* process the important signals */
switch(signo){
case SIGPIPE:
case SIGINT:
case SIGUSR1:
case SIGUSR2:
case SIGHUP:
/* ignored*/
break;
case SIGTERM:
/* ignore any SIGTERM if not in shutdown sequance (this
* is marked by the attendent process) */
if (get_osips_state()!=STATE_TERMINATING)
return;
/* if some shutdown already in progress, ignore this one */
if (sig_flag==0) sig_flag=signo;
else return;
/* do the termination */
LM_INFO("signal %d received\n", signo);
/* print memory stats for non-main too */
#ifdef PKG_MALLOC
LM_GEN1(memdump, "Memory status (pkg):\n");
pkg_status();
#endif
exit(0);
break;
case SIGCHLD:
pid = waitpid(-1, &status, WNOHANG);
LM_DBG("SIGCHLD received from %ld (status=%d), ignoring\n",
(long)pid,status);
break;
case SIGSEGV:
/* looks like we ate some spicy SIP */
LM_CRIT("segfault in process pid: %d, id: %d\n",
pt[process_no].pid, process_no);
pt[process_no].flags |= OSS_TAKING_A_DUMP;
if (restore_segv_handler() != 0)
exit(-1);
}
}
}
/**
* Install the signal handlers.
* \return 0 on success, -1 on error
*/
int install_sigs(void)
{
struct sigaction act;
memset(&act, 0, sizeof act);
act.sa_handler = sig_usr;
if (sigaction(SIGSEGV, &act, &sa_sys_segv) < 0) {
LM_INFO("failed to install custom SIGSEGV handler -- corefiles must "
"now be written within %d sec to avoid truncation!\n",
GRACEFUL_SHUTDOWN_TIMEOUT);
} else {
LM_DBG("override SIGSEGV handler: success\n");
sa_sys_is_valid = 1;
}
if (signal(SIGINT, sig_usr) == SIG_ERR ) {
LM_ERR("no SIGINT signal handler can be installed\n");
goto error;
}
/* if we debug and write to a pipe, we want to exit nicely too */
if (signal(SIGPIPE, sig_usr) == SIG_ERR ) {
LM_ERR("no SIGINT signal handler can be installed\n");
goto error;
}
if (signal(SIGUSR1, sig_usr) == SIG_ERR ) {
LM_ERR("no SIGUSR1 signal handler can be installed\n");
goto error;
}
if (signal(SIGCHLD , sig_usr) == SIG_ERR ) {
LM_ERR("no SIGCHLD signal handler can be installed\n");
goto error;
}
if (signal(SIGTERM , sig_usr) == SIG_ERR ) {
LM_ERR("no SIGTERM signal handler can be installed\n");
goto error;
}
if (signal(SIGHUP , sig_usr) == SIG_ERR ) {
LM_ERR("no SIGHUP signal handler can be installed\n");
goto error;
}
if (signal(SIGUSR2 , sig_usr) == SIG_ERR ) {
LM_ERR("no SIGUSR2 signal handler can be installed\n");
goto error;
}
return 0;
error:
return -1;
}
/**
* Main loop, forks the children, bind to addresses,
* handle signals.
* \return don't return on sucess, -1 on error
*/
static int main_loop(void)
{
static int chd_rank;
int* startup_done = NULL;
int rc;
chd_rank=0;
if (start_module_procs()!=0) {
LM_ERR("failed to fork module processes\n");
goto error;
}
if(startup_rlist.a) {/* if a startup route was defined */
startup_done = (int*)shm_malloc(sizeof(int));
if(startup_done == NULL) {
LM_ERR("No more shared memory\n");
goto error;
}
*startup_done = 0;
}
/* fork for the timer process*/
if (start_timer_processes()!=0) {
LM_CRIT("cannot start timer process(es)\n");
goto error;
}
/* fork all processes required by UDP network layer */
if (udp_start_processes( &chd_rank, startup_done)<0) {
LM_CRIT("cannot start UDP processes\n");
goto error;
}
/* fork all processes required by TCP network layer */
if (tcp_start_processes( &chd_rank, startup_done)<0) {
LM_CRIT("cannot start TCP processes\n");
goto error;
}
/* fork for the extra timer processes */
if (start_timer_extra_processes( &chd_rank )!=0) {
LM_CRIT("cannot start timer extra process(es)\n");
goto error;
}
/* fork the TCP listening process */
if (tcp_start_listener()<0) {
LM_CRIT("cannot start TCP listener process\n");
goto error;
}
/* this is the main process -> it shouldn't send anything */
bind_address=0;
if (startup_done) {
if (*startup_done==0)
LM_CRIT("BUG: startup route defined, but not run :( \n");
shm_free(startup_done);
}
set_osips_state( STATE_RUNNING );
/* main process left */
is_main=1;
set_proc_attrs("attendant");
pt[process_no].flags = OSS_FORK_NO_IPC|OSS_FORK_NO_LOAD;
if (testing_framework) {
if (init_child(1) < 0) {
LM_ERR("error in init_child for PROC_MAIN\n");
report_failure_status();
goto error;
}
rc = run_unit_tests();
shutdown_opensips(rc);
}
if (init_child(PROC_MAIN) < 0) {
LM_ERR("error in init_child for PROC_MAIN\n");
report_failure_status();
goto error;
}
report_conditional_status( (!no_daemon_mode), 0);
for(;;){
handle_sigs();
pause();
}
/*return 0; */
error:
is_main=1; /* if we are here, we are the "main process",
any forked children should exit with exit(-1) and not
ever use return */
report_failure_status();
return -1;
}
/**
* Main routine, start of the program execution.
* \param argc the number of arguments
* \param argv pointer to the arguments array
* \return don't return on sucess, -1 on error
* \see main_loop
*/
int main(int argc, char** argv)
{
/* configure by default logging to syslog */
int cfg_log_stderr = 1;
FILE* cfg_stream = NULL;
int c,r;
char *tmp;
int tmp_len;
int port;
int proto;
int protos_no;
char *options;
int ret;
unsigned int seed;
int rfd;
/*init*/
ret=-1;
my_argc=argc; my_argv=argv;
/* process pkg mem size from command line */
opterr=0;
options="f:cCm:M:b:l:n:N:rRvdDFEVhw:t:u:g:P:G:W:o:"
#ifdef UNIT_TESTS
"T"
#endif
;
while((c=getopt(argc,argv,options))!=-1){
switch(c){
case 'M':
pkg_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
if (tmp &&(*tmp)){
LM_ERR("bad pkgmem size number: -m %s\n", optarg);
goto error00;
};
break;
case 'm':
shm_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
if (tmp &&(*tmp)){
LM_ERR("bad shmem size number: -m %s\n", optarg);
goto error00;
};
break;
case 'u':
user=optarg;
break;
case 'g':
group=optarg;
break;
}
}
/* get uid/gid */
if (user){
if (user2uid(&uid, &gid, user)<0){
LM_ERR("bad user name/uid number: -u %s\n", user);
goto error00;
}
}
if (group){
if (group2gid(&gid, group)<0){
LM_ERR("bad group name/gid number: -u %s\n", group);
goto error00;
}
}
/*init pkg mallocs (before parsing cfg but after parsing cmd line !)*/
if (init_pkg_mallocs()==-1)
goto error00;
init_route_lists();
/* process command line (get port no, cfg. file path etc) */
/* first reset getopt */
optind = 1;
while((c=getopt(argc,argv,options))!=-1){
switch(c){
case 'f':
cfg_file=optarg;
break;
case 'C':
config_check |= 2;
case 'c':
if (config_check==3)
break;
config_check |= 1;
cfg_log_stderr=1; /* force stderr logging */
break;
case 'm':
/* ignoring it, parsed previously */
break;
case 'M':
/* ignoring it, parsed previously */
break;
case 'b':
maxbuffer=strtol(optarg, &tmp, 10);
if (tmp &&(*tmp)){
LM_ERR("bad max buffer size number: -b %s\n", optarg);
goto error00;
}
break;
case 'l':
if (parse_phostport(optarg, strlen(optarg), &tmp, &tmp_len,
&port, &proto)<0){
LM_ERR("bad -l address specifier: %s\n", optarg);
goto error00;
}
tmp[tmp_len]=0; /* null terminate the host */
/* add a new addr. to our address list */
if (add_cmd_listener(tmp, port, proto)!=0){
LM_ERR("failed to add new listen address\n");
goto error00;
}
break;
case 'n':
children_no=strtol(optarg, &tmp, 10);
if ((tmp==0) ||(*tmp)){
LM_ERR("bad process number: -n %s\n", optarg);
goto error00;
}
break;
case 'v':
check_via=1;
break;
case 'r':
received_dns|=DO_DNS;
break;
case 'R':
received_dns|=DO_REV_DNS;
break;
case 'd':
*log_level = debug_mode ? L_DBG : (*log_level)+1;
break;
case 'D':
debug_mode=1;
*log_level = L_DBG;
break;
case 'F':
no_daemon_mode=1;
break;
case 'E':
cfg_log_stderr=1;
break;
case 'N':
tcp_children_no=strtol(optarg, &tmp, 10);
if ((tmp==0) ||(*tmp)){
LM_ERR("bad process number: -N %s\n", optarg);
goto error00;
}
break;
case 'W':
io_poll_method=get_poll_type(optarg);
if (io_poll_method==POLL_NONE){
LM_ERR("bad poll method name: -W %s\ntry "
"one of %s.\n", optarg, poll_support);
goto error00;
}
break;
case 'V':
printf("version: %s\n", version);
printf("flags: %s\n", flags );