00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 #include <stdio.h>
00074 #include <stdlib.h>
00075 #include <errno.h>
00076 #include <ctype.h>
00077 #include <string.h>
00078 #include <netdb.h>
00079 #include <unistd.h>
00080 #include <sys/socket.h>
00081 #include <netinet/in.h>
00082 #include <netinet/in_systm.h>
00083 #include <netinet/ip.h>
00084 #include <arpa/inet.h>
00085 #include <sys/utsname.h>
00086 #include <sys/types.h>
00087 #include <sys/stat.h>
00088 #include <sys/mman.h>
00089 #include <fcntl.h>
00090 #include <sys/time.h>
00091 #include <sys/wait.h>
00092 #include <pwd.h>
00093 #include <grp.h>
00094 #include <signal.h>
00095 #include <time.h>
00096
00097 #include <sys/ioctl.h>
00098 #include <net/if.h>
00099 #ifdef HAVE_SYS_SOCKIO_H
00100 #include <sys/sockio.h>
00101 #endif
00102
00103 #include "help_msg.h"
00104 #include "config.h"
00105 #include "dprint.h"
00106 #include "daemonize.h"
00107 #include "route.h"
00108 #include "udp_server.h"
00109 #include "globals.h"
00110 #include "mem/mem.h"
00111 #include "mem/shm_mem.h"
00112 #include "sr_module.h"
00113 #include "timer.h"
00114 #include "parser/msg_parser.h"
00115 #include "ip_addr.h"
00116 #include "resolve.h"
00117 #include "parser/parse_hname2.h"
00118 #include "parser/digest/digest_parser.h"
00119 #include "name_alias.h"
00120 #include "hash_func.h"
00121 #include "pt.h"
00122 #include "script_cb.h"
00123 #include "blacklists.h"
00124
00125 #include "pt.h"
00126 #include "ut.h"
00127 #include "statistics.h"
00128 #include "pvar.h"
00129 #ifdef USE_TCP
00130 #include "poll_types.h"
00131 #include "tcp_init.h"
00132 #ifdef USE_TLS
00133 #include "tls/tls_init.h"
00134 #endif
00135 #endif
00136
00137 #ifdef USE_SCTP
00138 #include "sctp_server.h"
00139 #endif
00140
00141 #include "version.h"
00142 #include "mi/mi_core.h"
00143
00144 static char id[]="@(#) $Id: main.c 5722 2009-03-19 09:13:44Z miconda $";
00145 static char* version=KAMAILIO_FULL_VERSION;
00146 static char* flags=KAMAILIO_COMPILE_FLAGS;
00147 char compiled[]= __TIME__ " " __DATE__ ;
00148
00149
00150
00151
00152 void print_ct_constants(void)
00153 {
00154 #ifdef ADAPTIVE_WAIT
00155 printf("ADAPTIVE_WAIT_LOOPS=%d, ", ADAPTIVE_WAIT_LOOPS);
00156 #endif
00157 printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
00158 " MAX_URI_SIZE %d, BUF_SIZE %d, PKG_SIZE %d\n",
00159 MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
00160 BUF_SIZE, PKG_MEM_POOL_SIZE);
00161 #ifdef USE_TCP
00162 printf("poll method support: %s.\n", poll_support);
00163 #endif
00164 printf("svnrevision: %s\n",
00165 #ifdef SVNREVISION
00166 SVNREVISION
00167 #else
00168 "unknown"
00169 #endif
00170 );
00171 }
00172
00173
00174
00175 int own_pgid = 0;
00176
00177 char* cfg_file = 0;
00178 unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE;
00179
00180
00181
00182 int children_no = 0;
00183 #ifdef USE_TCP
00184 int tcp_children_no = 0;
00185 int tcp_disable = 0;
00186 int tcp_crlf_pingpong = 1;
00187 #endif
00188 #ifdef USE_TLS
00189 int tls_disable = 1;
00190 #endif
00191 #ifdef USE_SCTP
00192 int sctp_disable = 0;
00193 #endif
00194 int sig_flag = 0;
00195 #ifdef CHANGEABLE_DEBUG_LEVEL
00196 int debug_init = L_NOTICE;
00197 int *debug = &debug_init;
00198 #else
00199 int debug = L_NOTICE;
00200 #endif
00201 int dont_fork = 0;
00202 int no_daemon = 0;
00203
00204 int log_stderr = 1;
00205
00206 int log_facility = LOG_DAEMON;
00207
00208 char *log_name = 0;
00209 int config_check = 0;
00210
00211 int check_via = 0;
00212
00213 int memlog = L_DBG;
00214
00215
00216
00217 int sip_warning = 0;
00218
00219 int server_signature=1;
00220
00221
00222
00223 str server_header = {SERVER_HDR,sizeof(SERVER_HDR)-1};
00224
00225
00226
00227 str user_agent_header = {USER_AGENT,sizeof(USER_AGENT)-1};
00228
00229
00230
00231 int mhomed=0;
00232
00233
00234 int received_dns = 0;
00235 char* working_dir = 0;
00236 char* chroot_dir = 0;
00237 char* user=0;
00238 char* group=0;
00239 int uid = 0;
00240 int gid = 0;
00241
00242
00243 int disable_core_dump=0;
00244 int open_files_limit=-1;
00245
00246
00247
00248 int reply_to_via=0;
00249
00250 #ifdef USE_MCAST
00251 int mcast_loopback = 0;
00252 int mcast_ttl = -1;
00253 #endif
00254
00255 int tos = IPTOS_LOWDELAY;
00256
00257 struct socket_info* udp_listen=0;
00258 #ifdef USE_TCP
00259 struct socket_info* tcp_listen=0;
00260 #endif
00261 #ifdef USE_TLS
00262 struct socket_info* tls_listen=0;
00263 #endif
00264 #ifdef USE_SCTP
00265 struct socket_info* sctp_listen=0;
00266 #endif
00267
00268 struct socket_info* bind_address=0;
00269
00270 struct socket_info* sendipv4;
00271 struct socket_info* sendipv6;
00272 #ifdef USE_TCP
00273 struct socket_info* sendipv4_tcp;
00274 struct socket_info* sendipv6_tcp;
00275 #endif
00276 #ifdef USE_TLS
00277 struct socket_info* sendipv4_tls;
00278 struct socket_info* sendipv6_tls;
00279 #endif
00280 #ifdef USE_SCTP
00281 struct socket_info* sendipv4_sctp;
00282 struct socket_info* sendipv6_sctp;
00283 #endif
00284
00285
00286
00287
00288 int auto_aliases=1;
00289
00290 unsigned short port_no=0;
00291 #ifdef USE_TLS
00292 unsigned short tls_port_no=0;
00293 #endif
00294
00295 struct host_alias* aliases=0;
00296
00297
00298 int process_no = 0;
00299
00300
00301 int cfg_errors=0;
00302
00303
00304 unsigned long shm_mem_size=SHM_MEM_SIZE * 1024 * 1024;
00305
00306
00307 int my_argc;
00308 char **my_argv;
00309
00310 extern FILE* yyin;
00311 extern int yyparse();
00312
00313
00314 int is_main = 1;
00315
00316 char* pid_file = 0;
00317 char* pgid_file = 0;
00318
00319
00320
00321
00322
00323
00324 void cleanup(int show_status)
00325 {
00326
00327 if (mem_lock)
00328 shm_unlock();
00329
00330
00331 destroy_modules();
00332 #ifdef USE_TCP
00333 destroy_tcp();
00334 #endif
00335 #ifdef USE_TLS
00336 destroy_tls();
00337 #endif
00338 destroy_timer();
00339 destroy_stats_collector();
00340 destroy_script_cb();
00341 pv_api_destroy();
00342 destroy_black_lists();
00343 #ifdef PKG_MALLOC
00344 if (show_status){
00345 LM_GEN1(memlog, "Memory status (pkg):\n");
00346 pkg_status();
00347 }
00348 #endif
00349 if (pt) shm_free(pt);
00350 pt=0;
00351 if (show_status){
00352 LM_GEN1(memlog, "Memory status (shm):\n");
00353 shm_status();
00354 }
00355 #ifdef CHANGEABLE_DEBUG_LEVEL
00356 if (debug!=&debug_init) {
00357 debug_init = *debug;
00358 shm_free(debug);
00359 debug = &debug_init;
00360 }
00361 #endif
00362
00363 shm_mem_destroy();
00364 if (pid_file) unlink(pid_file);
00365 if (pgid_file) unlink(pgid_file);
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 static void kill_all_children(int signum)
00381 {
00382 int r;
00383 if (own_pgid) kill(0, signum);
00384 else if (pt)
00385 for (r=1; r<counted_processes; r++)
00386 if (pt[r].pid) kill(pt[r].pid, signum);
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 static void sig_alarm_kill(int signo)
00398 {
00399 kill_all_children(SIGKILL);
00400
00401
00402
00403
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 static void sig_alarm_abort(int signo)
00415 {
00416
00417 LM_CRIT("BUG - shutdown timeout triggered, dying...\n");
00418 abort();
00419 }
00420
00421
00422
00423
00424
00425 void handle_sigs(void)
00426 {
00427 pid_t chld;
00428 int chld_status;
00429 int i;
00430 int do_exit;
00431 const unsigned int shutdown_time = 60;
00432
00433 switch(sig_flag){
00434 case 0: break;
00435 case SIGPIPE:
00436
00437
00438
00439 LM_WARN("SIGPIPE received and ignored\n");
00440 break;
00441 case SIGINT:
00442 case SIGTERM:
00443
00444 if (sig_flag==SIGINT)
00445 LM_DBG("INT received, program terminates\n");
00446 else
00447 LM_DBG("SIGTERM received, program terminates\n");
00448
00449
00450 kill_all_children(SIGTERM);
00451 if (signal(SIGALRM, sig_alarm_kill) == SIG_ERR ) {
00452 LM_ERR("could not install SIGALARM handler\n");
00453
00454
00455 }
00456 alarm(shutdown_time);
00457 while(wait(0) > 0);
00458 signal(SIGALRM, sig_alarm_abort);
00459
00460 cleanup(1);
00461 alarm(0);
00462 signal(SIGALRM, SIG_IGN);
00463 dprint("Thank you for flying " NAME "\n");
00464 exit(0);
00465 break;
00466
00467 case SIGUSR1:
00468 #ifdef PKG_MALLOC
00469 LM_GEN1(memlog, "Memory status (pkg):\n");
00470 pkg_status();
00471 #endif
00472 LM_GEN1(memlog, "Memory status (shm):\n");
00473 shm_status();
00474 break;
00475
00476 case SIGCHLD:
00477 do_exit = 0;
00478 while ((chld=waitpid( -1, &chld_status, WNOHANG ))>0) {
00479
00480 for( i=0 ; i<counted_processes ; i++ )
00481 if (pt[i].pid==chld) break;
00482 if (i==counted_processes) {
00483 LM_DBG("unkown child process %d ended. Ignoring\n",chld);
00484 continue;
00485 }
00486 do_exit = 1;
00487
00488 if (WIFEXITED(chld_status))
00489 LM_INFO("child process %d exited normally,"
00490 " status=%d\n", chld,
00491 WEXITSTATUS(chld_status));
00492 else if (WIFSIGNALED(chld_status)) {
00493 LM_INFO("child process %d exited by a signal"
00494 " %d\n", chld, WTERMSIG(chld_status));
00495 #ifdef WCOREDUMP
00496 LM_INFO("core was %sgenerated\n",
00497 WCOREDUMP(chld_status) ? "" : "not " );
00498 #endif
00499 }else if (WIFSTOPPED(chld_status))
00500 LM_INFO("child process %d stopped by a"
00501 " signal %d\n", chld,
00502 WSTOPSIG(chld_status));
00503 }
00504 if (!do_exit)
00505 break;
00506 LM_INFO("terminating due to SIGCHLD\n");
00507
00508 kill_all_children(SIGTERM);
00509 if (signal(SIGALRM, sig_alarm_kill) == SIG_ERR ) {
00510 LM_ERR("could not install SIGALARM handler\n");
00511
00512
00513 }
00514 alarm(shutdown_time);
00515 while(wait(0) > 0);
00516 signal(SIGALRM, sig_alarm_abort);
00517 cleanup(1);
00518 alarm(0);
00519 signal(SIGALRM, SIG_IGN);
00520 LM_DBG("terminating due to SIGCHLD\n");
00521 exit(0);
00522 break;
00523
00524 case SIGHUP:
00525 LM_DBG("SIGHUP received, ignoring it\n");
00526 break;
00527 default:
00528 LM_CRIT("unhandled signal %d\n", sig_flag);
00529 }
00530 sig_flag=0;
00531 }
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541 static void sig_usr(int signo)
00542 {
00543 if (is_main){
00544 if (sig_flag==0) sig_flag=signo;
00545 else
00546 return; ;
00547 if (dont_fork)
00548
00549
00550 handle_sigs();
00551 }else{
00552
00553 switch(signo){
00554 case SIGPIPE:
00555 LM_INFO("signal %d received\n", signo);
00556 break;
00557 case SIGINT:
00558 case SIGTERM:
00559 LM_INFO("signal %d received\n", signo);
00560
00561 #ifdef PKG_MALLOC
00562 LM_GEN1(memlog, "Memory status (pkg):\n");
00563 pkg_status();
00564 #endif
00565 exit(0);
00566 break;
00567 case SIGUSR1:
00568
00569 #ifdef PKG_MALLOC
00570 LM_GEN1(memlog, "Memory status (pkg):\n");
00571 pkg_status();
00572 #endif
00573 break;
00574
00575 case SIGUSR2:
00576 case SIGHUP:
00577 break;
00578 case SIGCHLD:
00579 LM_DBG("SIGCHLD received: "
00580 "we do not worry about grand-children\n");
00581 }
00582 }
00583 }
00584
00585
00586
00587
00588
00589
00590
00591 int install_sigs(void)
00592 {
00593
00594 if (signal(SIGINT, sig_usr) == SIG_ERR ) {
00595 LM_ERR("no SIGINT signal handler can be installed\n");
00596 goto error;
00597 }
00598
00599 if (signal(SIGPIPE, sig_usr) == SIG_ERR ) {
00600 LM_ERR("no SIGINT signal handler can be installed\n");
00601 goto error;
00602 }
00603
00604 if (signal(SIGUSR1, sig_usr) == SIG_ERR ) {
00605 LM_ERR("no SIGUSR1 signal handler can be installed\n");
00606 goto error;
00607 }
00608 if (signal(SIGCHLD , sig_usr) == SIG_ERR ) {
00609 LM_ERR("no SIGCHLD signal handler can be installed\n");
00610 goto error;
00611 }
00612 if (signal(SIGTERM , sig_usr) == SIG_ERR ) {
00613 LM_ERR("no SIGTERM signal handler can be installed\n");
00614 goto error;
00615 }
00616 if (signal(SIGHUP , sig_usr) == SIG_ERR ) {
00617 LM_ERR("no SIGHUP signal handler can be installed\n");
00618 goto error;
00619 }
00620 if (signal(SIGUSR2 , sig_usr) == SIG_ERR ) {
00621 LM_ERR("no SIGUSR2 signal handler can be installed\n");
00622 goto error;
00623 }
00624 return 0;
00625 error:
00626 return -1;
00627 }
00628
00629
00630
00631
00632
00633
00634
00635 static int main_loop(void)
00636 {
00637 static int chd_rank;
00638 int i;
00639 pid_t pid;
00640 struct socket_info* si;
00641
00642 chd_rank=0;
00643
00644 if (dont_fork){
00645 if (udp_listen==0){
00646 LM_ERR("no fork mode requires at least one"
00647 " udp listen address, exiting...\n");
00648 goto error;
00649 }
00650
00651 if (udp_init(udp_listen)==-1) goto error;
00652 bind_address=udp_listen;
00653 sendipv4=bind_address;
00654 sendipv6=bind_address;
00655 if (udp_listen->next){
00656 LM_WARN("using only the first listen address (no fork)\n");
00657 }
00658
00659
00660 if (do_suid(uid, gid)==-1)
00661 goto error;
00662
00663
00664 if (start_timer_processes()!=0) {
00665 LM_CRIT("cannot start timer process(es)\n");
00666 goto error;
00667 }
00668
00669 if (start_module_procs()!=0) {
00670 LM_ERR("failed to fork module processes\n");
00671 goto error;
00672 }
00673
00674
00675 set_proc_attrs("stand-alone SIP receiver %.*s",
00676 bind_address->sock_str.len, bind_address->sock_str.s );
00677
00678
00679
00680
00681 if (init_child(1) < 0) {
00682 LM_ERR("init_child failed in don't fork\n");
00683 goto error;
00684 }
00685
00686 is_main=1;
00687 return udp_rcv_loop();
00688
00689 } else {
00690
00691 for(si=udp_listen;si;si=si->next){
00692
00693
00694 if (udp_init(si)==-1) goto error;
00695
00696 if ((si->address.af==AF_INET)&&
00697 ((sendipv4==0)||(sendipv4->flags&SI_IS_LO)))
00698 sendipv4=si;
00699 #ifdef USE_IPV6
00700 if((sendipv6==0)&&(si->address.af==AF_INET6))
00701 sendipv6=si;
00702 #endif
00703 }
00704 #ifdef USE_TCP
00705 if (!tcp_disable){
00706 for(si=tcp_listen; si; si=si->next){
00707
00708 if (tcp_init(si)==-1) goto error;
00709
00710 if ((si->address.af==AF_INET)&&
00711 ((sendipv4_tcp==0)||(sendipv4_tcp->flags&SI_IS_LO)))
00712 sendipv4_tcp=si;
00713 #ifdef USE_IPV6
00714 if((sendipv6_tcp==0)&&(si->address.af==AF_INET6))
00715 sendipv6_tcp=si;
00716 #endif
00717 }
00718 }
00719 #ifdef USE_TLS
00720 if (!tls_disable){
00721 for(si=tls_listen; si; si=si->next){
00722
00723 if (tls_init(si)==-1) goto error;
00724
00725 if ((si->address.af==AF_INET)&&
00726 ((sendipv4_tls==0)||(sendipv4_tls->flags&SI_IS_LO)))
00727 sendipv4_tls=si;
00728 #ifdef USE_IPV6
00729 if((sendipv6_tls==0)&&(si->address.af==AF_INET6))
00730 sendipv6_tls=si;
00731 #endif
00732 }
00733 }
00734 #endif
00735 #endif
00736 #ifdef USE_SCTP
00737 if (!sctp_disable){
00738 for(si=sctp_listen; si; si=si->next){
00739
00740 if (sctp_server_init(si)==-1) goto error;
00741
00742 if ((si->address.af==AF_INET)&&
00743 ((sendipv4_sctp==0)||(sendipv4_sctp->flags&SI_IS_LO)))
00744 sendipv4_sctp=si;
00745 #ifdef USE_IPV6
00746 if((sendipv6_sctp==0)&&(si->address.af==AF_INET6))
00747 sendipv6_sctp=si;
00748 #endif
00749 }
00750 }
00751 #endif
00752
00753
00754
00755 if (do_suid(uid, gid)==-1) goto error;
00756
00757
00758 for(si=udp_listen; si; si=si->next){
00759 for(i=0;i<children_no;i++){
00760 chd_rank++;
00761 if ( (pid=internal_fork( "UDP receiver"))<0 ) {
00762 LM_CRIT("cannot fork UDP process\n");
00763 goto error;
00764 } else if (pid==0){
00765
00766
00767 set_proc_attrs("SIP receiver %.*s ",
00768 si->sock_str.len, si->sock_str.s);
00769 bind_address=si;
00770 if (init_child(chd_rank) < 0) {
00771 LM_ERR("init_child failed for UDP listener\n");
00772 exit(-1);
00773 }
00774 udp_rcv_loop();
00775 exit(-1);
00776 }
00777 }
00778
00779 ;
00780 }
00781 }
00782
00783 #ifdef USE_SCTP
00784 if(!sctp_disable){
00785 for(si=sctp_listen; si; si=si->next){
00786 for(i=0;i<children_no;i++){
00787 chd_rank++;
00788 if ( (pid=internal_fork( "SCTP receiver"))<0 ) {
00789 LM_CRIT("cannot fork SCTP process\n");
00790 goto error;
00791 } else if (pid==0){
00792
00793
00794 set_proc_attrs("SIP receiver %.*s ",
00795 si->sock_str.len, si->sock_str.s);
00796 bind_address=si;
00797 if (init_child(chd_rank) < 0) {
00798 LM_ERR("init_child failed\n");
00799 exit(-1);
00800 }
00801 sctp_server_rcv_loop();
00802 exit(-1);
00803 }
00804 }
00805 }
00806 }
00807 #endif
00808
00809
00810 bind_address=0;
00811
00812
00813 if (start_timer_processes()!=0) {
00814 LM_CRIT("cannot start timer process(es)\n");
00815 goto error;
00816 }
00817
00818 if (start_module_procs()!=0) {
00819 LM_ERR("failed to fork module processes\n");
00820 goto error;
00821 }
00822
00823 #ifdef USE_TCP
00824 if (!tcp_disable){
00825
00826 if (tcp_init_children(&chd_rank)<0) goto error;
00827
00828 if ( (pid=internal_fork( "TCP main"))<0 ) {
00829 LM_CRIT("cannot fork tcp main process\n");
00830 goto error;
00831 }else if (pid==0){
00832
00833
00834 close(unix_tcp_sock);
00835 unix_tcp_sock = -1;
00836 close(pt[process_no].unix_sock);
00837 pt[process_no].unix_sock = -1;
00838
00839 if (init_child(PROC_TCP_MAIN) < 0) {
00840 LM_ERR("error in init_child for tcp main\n");
00841 exit(-1);
00842 }
00843 tcp_main_loop();
00844 exit(-1);
00845 }
00846 }
00847 #endif
00848
00849
00850 is_main=1;
00851 set_proc_attrs("attendant");
00852
00853 if (init_child(PROC_MAIN) < 0) {
00854 LM_ERR("error in init_child for PROC_MAIN\n");
00855 goto error;
00856 }
00857
00858 for(;;){
00859 handle_sigs();
00860 pause();
00861 }
00862
00863
00864 error:
00865 is_main=1;
00866
00867
00868 return -1;
00869
00870 }
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881 int main(int argc, char** argv)
00882 {
00883
00884 int cfg_log_stderr = 0;
00885 FILE* cfg_stream;
00886 int c, r, tmp_len, port, proto, ret, rfd;
00887 char *tmp, *options, *rand_source;
00888 unsigned int seed;
00889
00890
00891 ret=-1;
00892 my_argc=argc; my_argv=argv;
00893
00894
00895 if (init_pkg_mallocs()==-1)
00896 goto error;
00897
00898 init_route_lists();
00899
00900 opterr=0;
00901 options="f:cCm:b:l:n:N:rRvdDFETSVhw:t:u:g:P:G:W:";
00902
00903 while((c=getopt(argc,argv,options))!=-1){
00904 switch(c){
00905 case 'f':
00906 cfg_file=optarg;
00907 break;
00908 case 'C':
00909 config_check |= 2;
00910 case 'c':
00911 if (config_check==3)
00912 break;
00913 config_check |= 1;
00914 cfg_log_stderr=1;
00915 break;
00916 case 'm':
00917 shm_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
00918 if (tmp &&(*tmp)){
00919 LM_ERR("bad shmem size number: -m %s\n", optarg);
00920 goto error;
00921 };
00922
00923 break;
00924 case 'b':
00925 maxbuffer=strtol(optarg, &tmp, 10);
00926 if (tmp &&(*tmp)){
00927 LM_ERR("bad max buffer size number: -b %s\n", optarg);
00928 goto error;
00929 }
00930 break;
00931 case 'l':
00932 if (parse_phostport(optarg, strlen(optarg), &tmp, &tmp_len,
00933 &port, &proto)<0){
00934 LM_ERR("bad -l address specifier: %s\n", optarg);
00935 goto error;
00936 }
00937 tmp[tmp_len]=0;
00938
00939 if (add_listen_iface(tmp, port, proto, 0)!=0){
00940 LM_ERR("failed to add new listen address\n");
00941 goto error;
00942 }
00943 break;
00944 case 'n':
00945 children_no=strtol(optarg, &tmp, 10);
00946 if ((tmp==0) ||(*tmp)){
00947 LM_ERR("bad process number: -n %s\n", optarg);
00948 goto error;
00949 }
00950 break;
00951 case 'v':
00952 check_via=1;
00953 break;
00954 case 'r':
00955 received_dns|=DO_DNS;
00956 break;
00957 case 'R':
00958 received_dns|=DO_REV_DNS;
00959 case 'd':
00960 #ifdef CHANGEABLE_DEBUG_LEVEL
00961 (*debug)++;
00962 #else
00963 debug++;
00964 #endif
00965 break;
00966 case 'D':
00967 no_daemon=1;
00968 break;
00969 case 'F':
00970 no_daemon=1;
00971 dont_fork=1;
00972 break;
00973 case 'E':
00974 cfg_log_stderr=1;
00975 break;
00976 case 'T':
00977 #ifdef USE_TCP
00978 tcp_disable=1;
00979 #else
00980 LM_WARN("tcp support not compiled in\n");
00981 #endif
00982 break;
00983 case 'S':
00984 #ifdef USE_SCTP
00985 sctp_disable=1;
00986 #else
00987 LM_WARN("sctp support not compiled in\n");
00988 #endif
00989 break;
00990 case 'N':
00991 #ifdef USE_TCP
00992 tcp_children_no=strtol(optarg, &tmp, 10);
00993 if ((tmp==0) ||(*tmp)){
00994 LM_ERR("bad process number: -N %s\n", optarg);
00995 goto error;
00996 }
00997 #else
00998 LM_WARN("tcp support not compiled in\n");
00999 #endif
01000 break;
01001 case 'W':
01002 #ifdef USE_TCP
01003 tcp_poll_method=get_poll_type(optarg);
01004 if (tcp_poll_method==POLL_NONE){
01005 LM_ERR("bad poll method name: -W %s\ntry "
01006 "one of %s.\n", optarg, poll_support);
01007 goto error;
01008 }
01009 #else
01010 LM_WARN("tcp support not compiled in\n");
01011 #endif
01012 break;
01013 case 'V':
01014 printf("version: %s\n", version);
01015 printf("flags: %s\n", flags );
01016 print_ct_constants();
01017 printf("%s\n",id);
01018 printf("%s compiled on %s with %s\n", __FILE__,
01019 compiled, COMPILER );
01020
01021 exit(0);
01022 break;
01023 case 'h':
01024 printf("version: %s\n", version);
01025 printf("%s",help_msg);
01026 exit(0);
01027 break;
01028 case 'w':
01029 working_dir=optarg;
01030 break;
01031 case 't':
01032 chroot_dir=optarg;
01033 break;
01034 case 'u':
01035 user=optarg;
01036 break;
01037 case 'g':
01038 group=optarg;
01039 break;
01040 case 'P':
01041 pid_file=optarg;
01042 break;
01043 case 'G':
01044 pgid_file=optarg;
01045 break;
01046 case '?':
01047 if (isprint(optopt))
01048 LM_ERR("Unknown option `-%c`.\n", optopt);
01049 else
01050 LM_ERR("Unknown option character `\\x%x`.\n", optopt);
01051 goto error;
01052 case ':':
01053 LM_ERR("Option `-%c` requires an argument.\n", optopt);
01054 goto error;
01055 default:
01056 abort();
01057 }
01058 }
01059
01060 log_stderr = cfg_log_stderr;
01061
01062
01063 if (cfg_file==0) cfg_file=CFG_FILE;
01064
01065
01066 cfg_stream=fopen (cfg_file, "r");
01067 if (cfg_stream==0){
01068 LM_ERR("loading config file(%s): %s\n", cfg_file,
01069 strerror(errno));
01070 goto error;
01071 }
01072
01073
01074
01075
01076 seed=0;
01077 #ifdef __FreeBSD__
01078 rand_source = "/dev/random";
01079 #else
01080 rand_source = "/dev/urandom";
01081 #endif
01082 if ((rfd=open(rand_source, O_RDONLY))!=-1){
01083 try_again:
01084 if (read(rfd, (void*)&seed, sizeof(seed))==-1){
01085 if (errno==EINTR) goto try_again;
01086 LM_WARN("could not read from %s (%d)\n", rand_source, errno);
01087 }
01088 LM_DBG("initialize the pseudo random generator from %s\n", rand_source);
01089 LM_DBG("read %u from %s\n", seed, rand_source);
01090 close(rfd);
01091 }else{
01092 LM_WARN("could not open %s (%d)\n", rand_source, errno);
01093 LM_WARN("using a unsafe seed for the pseudo random number generator");
01094 }
01095 seed+=getpid()+time(0);
01096 LM_DBG("seeding PRNG with %u\n", seed);
01097 srand(seed);
01098 LM_DBG("test random number %u\n", rand());
01099
01100 #ifdef USE_TLS
01101
01102
01103 if (pre_init_tls()<0){
01104 LM_CRIT("could not pre_init_tls, exiting...\n");
01105 goto error;
01106 }
01107 #endif
01108
01109 if (preinit_black_lists()!=0) {
01110 LM_CRIT("failed to alloc black list's anchor\n");
01111 goto error;
01112 }
01113
01114
01115
01116 yyin=cfg_stream;
01117 if ((yyparse()!=0)||(cfg_errors)){
01118 LM_ERR("bad config file (%d errors)\n", cfg_errors);
01119 goto error;
01120 }
01121
01122 if (config_check>1 && check_rls()!=0) {
01123 LM_ERR("bad function call in config file\n");
01124 return ret;
01125 }
01126 #ifdef EXTRA_DEBUG
01127 print_rl();
01128 #endif
01129
01130
01131 resolv_init();
01132
01133
01134 if (port_no<=0) port_no=SIP_PORT;
01135 #ifdef USE_TLS
01136 if (tls_port_no<=0) tls_port_no=SIPS_PORT;
01137 #endif
01138
01139
01140 if (children_no<=0) children_no=CHILD_NO;
01141 #ifdef USE_TCP
01142 if (!tcp_disable){
01143 if (tcp_children_no<=0) tcp_children_no=children_no;
01144 }
01145 #endif
01146
01147 if (working_dir==0) working_dir="/";
01148
01149
01150 if (user){
01151 if (user2uid(&uid, &gid, user)<0){
01152 LM_ERR("bad user name/uid number: -u %s\n", user);
01153 goto error;
01154 }
01155 }
01156 if (group){
01157 if (group2gid(&gid, group)<0){
01158 LM_ERR("bad group name/gid number: -u %s\n", group);
01159 goto error;
01160 }
01161 }
01162 if (fix_all_socket_lists()!=0){
01163 LM_ERR("failed to initialize list addresses\n");
01164 goto error;
01165 }
01166
01167 printf("Listening on \n");
01168 print_all_socket_lists();
01169 printf("Aliases: \n");
01170
01171 print_aliases();
01172 printf("\n");
01173
01174 if (dont_fork){
01175 LM_WARN("no fork mode %s\n",
01176 (udp_listen)?(
01177 (udp_listen->next)?" and more than one listen address found"
01178 "(will use only the first one)":""
01179 ):"and no udp listen address found" );
01180 }
01181 if (config_check){
01182 LM_NOTICE("config file ok, exiting...\n");
01183 return 0;
01184 }
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196 if (init_shm_mallocs()==-1)
01197 goto error;
01198
01199 if (init_timer()<0){
01200 LM_CRIT("could not initialize timer, exiting...\n");
01201 goto error;
01202 }
01203
01204 #ifdef USE_TCP
01205 if (!tcp_disable){
01206
01207 if (init_tcp()<0){
01208 LM_CRIT("could not initialize tcp, exiting...\n");
01209 goto error;
01210 }
01211 }
01212 #ifdef USE_TLS
01213 if (!tls_disable){
01214
01215 if (init_tls()<0){
01216 LM_CRIT("could not initialize tls, exiting...\n");
01217 goto error;
01218 }
01219 }
01220 #endif
01221 #endif
01222
01223
01224 if (!no_daemon){
01225 if ( daemonize(&own_pgid) <0 )
01226 goto error;
01227 }
01228 else if (!dont_fork){
01229
01230 if (setsid()<0){
01231 LM_WARN("setsid failed: %s\n",strerror(errno));
01232 }
01233 own_pgid=1;
01234 }
01235
01236 #ifndef SYSLOG_ASYNC
01237 closelog();
01238 if (!log_stderr)
01239 openlog((log_name==0)?argv[0]:log_name, LOG_PID|LOG_CONS, log_facility);
01240 #else
01241 closelog_async();
01242 if (!log_stderr)
01243 openlog_async((log_name==0)?argv[0]:log_name, LOG_PID|LOG_CONS, log_facility);
01244 #endif
01245
01246
01247 if (install_sigs() != 0){
01248 LM_ERR("could not install the signal handlers\n");
01249 goto error;
01250 }
01251
01252 #ifdef CHANGEABLE_DEBUG_LEVEL
01253 debug=shm_malloc(sizeof(int));
01254 if (debug==0) {
01255 LM_ERR("out of memory\n");
01256 goto error;
01257 }
01258 *debug = debug_init;
01259 #endif
01260
01261 if (disable_core_dump) set_core_dump(0, 0);
01262 else set_core_dump(1, shm_mem_size+PKG_MEM_POOL_SIZE+4*1024*1024);
01263 if (open_files_limit>0){
01264 if(increase_open_fds(open_files_limit)<0){
01265 LM_ERR("could not increase file limits\n");
01266 goto error;
01267 }
01268 }
01269
01270
01271 LM_INFO("version: %s\n", version);
01272 #ifdef SVNREVISION
01273 LM_INFO("svnrevision: %s\n", SVNREVISION);
01274 #endif
01275
01276
01277 LM_INFO("using %ld Mb shared memory\n", ((shm_mem_size/1024)/1024));
01278 #ifdef PKG_MALLOC
01279 LM_INFO("using %i Mb private memory per process\n", ((PKG_MEM_POOL_SIZE/1024)/1024));
01280 #else
01281 LM_INFO("no private memory pool configured, processes use system memory\n");
01282 #endif
01283
01284
01285 if (init_stats_collector()<0) {
01286 LM_ERR("failed to initialize statistics\n");
01287 goto error;
01288 }
01289
01290 if (init_mi_core()<0) {
01291 LM_ERR("failed to initialize MI core\n");
01292 goto error;
01293 }
01294
01295 if (init_black_lists()!=0) {
01296 LM_CRIT("failed to init black lists\n");
01297 goto error;
01298 }
01299
01300 if (resolv_blacklist_init()!=0) {
01301 LM_CRIT("failed to create DNS blacklist\n");
01302 goto error;
01303 }
01304
01305
01306 if (init_modules() != 0) {
01307 LM_ERR("error while initializing modules\n");
01308 goto error;
01309 }
01310
01311
01312 if (init_multi_proc_support()!=0) {
01313 LM_ERR("failed to init multi-proc support\n");
01314 goto error;
01315 }
01316
01317
01318 if ( (r=fix_rls())!=0){
01319 LM_ERR("failed to fix configuration with err code %d\n", r);
01320 goto error;
01321 };
01322
01323
01324 ret=main_loop();
01325
01326 error:
01327
01328 if (config_check)
01329 return ret;
01330
01331 kill_all_children(SIGTERM);
01332
01333 cleanup(0);
01334 return ret;
01335 }