main.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of SIP-router, a free SIP server.
  7. *
  8. * SIP-router is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * SIP-router is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * History:
  23. * -------
  24. * 2002-01-29 argc/argv globalized via my_{argc|argv} (jiri)
  25. * 2003-01-23 mhomed added (jiri)
  26. * 2003-03-19 replaced all malloc/frees w/ pkg_malloc/pkg_free (andrei)
  27. * 2003-03-29 pkg cleaners for fifo and script callbacks introduced (jiri)
  28. * 2003-03-31 removed snmp part (obsolete & no place in core) (andrei)
  29. * 2003-04-06 child_init called in all processes (janakj)
  30. * 2003-04-08 init_mallocs split into init_{pkg,shm}_mallocs and
  31. * init_shm_mallocs called after cmd. line parsing (andrei)
  32. * 2003-04-15 added tcp_disable support (andrei)
  33. * 2003-05-09 closelog() before openlog to force opening a new fd
  34. * (needed on solaris) (andrei)
  35. * 2003-06-11 moved all signal handlers init. in install_sigs and moved it
  36. * after daemonize (so that we won't catch anymore our own
  37. * SIGCHLD generated when becoming session leader) (andrei)
  38. * changed is_main default value to 1 (andrei)
  39. * 2003-06-28 kill_all_children is now used instead of kill(0, sig)
  40. * see comment above it for explanations. (andrei)
  41. * 2003-06-29 replaced port_no_str snprintf w/ int2str (andrei)
  42. * 2003-10-10 added switch for config check (-c) (andrei)
  43. * 2003-10-24 converted to the new socket_info lists (andrei)
  44. * 2004-03-30 core dump is enabled by default
  45. * added support for increasing the open files limit (andrei)
  46. * 2004-04-28 sock_{user,group,uid,gid,mode} added
  47. * user2uid() & user2gid() added (andrei)
  48. * 2004-09-11 added timeout on children shutdown and final cleanup
  49. * (if it takes more than 60s => something is definitely wrong
  50. * => kill all or abort) (andrei)
  51. * force a shm_unlock before cleaning-up, in case we have a
  52. * crashed childvwhich still holds the lock (andrei)
  53. * 2004-12-02 removed -p, extended -l to support [proto:]address[:port],
  54. * added parse_phostport, parse_proto (andrei)
  55. * 2005-06-16 always record the pid in pt[process_no].pid twice: once in the
  56. * parent & once in the child to avoid a short window when one
  57. * of them might use it "unset" (andrei)
  58. * 2005-07-25 use sigaction for setting the signal handlers (andrei)
  59. * 2006-07-13 added dns cache/failover init. (andrei)
  60. * 2006-10-13 added global variables stun_refresh_interval, stun_allow_stun
  61. * and stun_allow_fp (vlada)
  62. * 2006-10-25 don't log messages from signal handlers if NO_SIG_DEBUG is
  63. * defined; improved exit kill timeout (andrei)
  64. * init_childs(PROC_MAIN) before starting tcp_main, to allow
  65. * tcp usage for module started processes (andrei)
  66. * 2007-01-18 children shutdown procedure moved into shutdown_children;
  67. * safer shutdown on start-up error (andrei)
  68. * 2007-02-09 TLS support split into tls-in-core (CORE_TLS) and generic TLS
  69. * (USE_TLS) (andrei)
  70. * 2007-06-07 added support for locking pages in mem. and using real time
  71. * scheduling policies (andrei)
  72. * 2007-07-30 dst blacklist and DNS cache measurements added (Gergo)
  73. * 2008-08-08 sctp support (andrei)
  74. * 2008-08-19 -l support for mmultihomed addresses/addresses lists
  75. * (e.g. -l (eth0, 1.2.3.4, foo.bar) ) (andrei)
  76. * 2010-04-19 added daemon_status_fd pipe to communicate the parent process
  77. * with the main process in daemonize mode, so the parent process
  78. * can return the proper exit status code (ibc)
  79. * 2010-08-19 moved the daemon status stuff to daemonize.c (andrei)
  80. */
  81. /** main file (init, daemonize, startup)
  82. * @file main.c
  83. * @ingroup core
  84. * Module: core
  85. */
  86. /*! @defgroup core SIP-router core
  87. *
  88. * sip router core part.
  89. */
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <errno.h>
  93. #include <ctype.h>
  94. #include <string.h>
  95. #include <netdb.h>
  96. #include <unistd.h>
  97. #include <sys/types.h>
  98. #include <sys/socket.h>
  99. #if defined(HAVE_NETINET_IN_SYSTM)
  100. #include <netinet/in_systm.h>
  101. #endif
  102. #include <netinet/in.h>
  103. #include <netinet/ip.h>
  104. #include <arpa/inet.h>
  105. #include <sys/utsname.h>
  106. #include <sys/stat.h>
  107. #include <sys/mman.h>
  108. #include <fcntl.h>
  109. #include <sys/time.h>
  110. #include <sys/wait.h>
  111. #include <pwd.h>
  112. #include <grp.h>
  113. #include <signal.h>
  114. #include <sys/ioctl.h>
  115. #include <net/if.h>
  116. #ifdef HAVE_SYS_SOCKIO_H
  117. #include <sys/sockio.h>
  118. #endif
  119. #include "config.h"
  120. #include "dprint.h"
  121. #include "daemonize.h"
  122. #include "route.h"
  123. #include "udp_server.h"
  124. #include "globals.h"
  125. #include "mem/mem.h"
  126. #ifdef SHM_MEM
  127. #include "mem/shm_mem.h"
  128. #include "shm_init.h"
  129. #endif /* SHM_MEM */
  130. #include "sr_module.h"
  131. #include "timer.h"
  132. #include "parser/msg_parser.h"
  133. #include "ip_addr.h"
  134. #include "resolve.h"
  135. #include "parser/parse_hname2.h"
  136. #include "parser/digest/digest_parser.h"
  137. #include "name_alias.h"
  138. #include "hash_func.h"
  139. #include "pt.h"
  140. #include "script_cb.h"
  141. #include "nonsip_hooks.h"
  142. #include "ut.h"
  143. #include "signals.h"
  144. #ifdef USE_RAW_SOCKS
  145. #include "raw_sock.h"
  146. #endif /* USE_RAW_SOCKS */
  147. #ifdef USE_TCP
  148. #include "poll_types.h"
  149. #include "tcp_init.h"
  150. #include "tcp_options.h"
  151. #ifdef CORE_TLS
  152. #include "tls/tls_init.h"
  153. #define tls_has_init_si() 1
  154. #define tls_loaded() 1
  155. #else
  156. #include "tls_hooks_init.h"
  157. #endif /* CORE_TLS */
  158. #endif /* USE_TCP */
  159. #ifdef USE_SCTP
  160. #include "sctp_options.h"
  161. #include "sctp_server.h"
  162. #endif
  163. #include "usr_avp.h"
  164. #include "rpc_lookup.h"
  165. #include "core_cmd.h"
  166. #include "flags.h"
  167. #include "lock_ops_init.h"
  168. #include "atomic_ops_init.h"
  169. #ifdef USE_DNS_CACHE
  170. #include "dns_cache.h"
  171. #endif
  172. #ifdef USE_DST_BLACKLIST
  173. #include "dst_blacklist.h"
  174. #endif
  175. #include "rand/fastrand.h" /* seed */
  176. #include "stats.h"
  177. #include "counters.h"
  178. #include "cfg/cfg.h"
  179. #include "cfg/cfg_struct.h"
  180. #include "cfg_core.h"
  181. #include "endianness.h" /* init */
  182. #include "basex.h" /* init */
  183. #include "pvapi.h" /* init PV api */
  184. #include "pv_core.h" /* register core pvars */
  185. #include "ppcfg.h"
  186. #include "sock_ut.h"
  187. #ifdef DEBUG_DMALLOC
  188. #include <dmalloc.h>
  189. #endif
  190. #include "ver.h"
  191. /* define SIG_DEBUG by default */
  192. #ifdef NO_SIG_DEBUG
  193. #undef SIG_DEBUG
  194. #else
  195. #define SIG_DEBUG
  196. #endif
  197. static char help_msg[]= "\
  198. Usage: " NAME " [options]\n\
  199. Options:\n\
  200. -f file Configuration file (default: " CFG_FILE ")\n\
  201. -L path Modules search path (default: " MODS_DIR ")\n\
  202. -c Check configuration file for errors\n\
  203. -l address Listen on the specified address/interface (multiple -l\n\
  204. mean listening on more addresses). The address format is\n\
  205. [proto:]addr_lst[:port], where proto=udp|tcp|tls|sctp, \n\
  206. addr_lst= addr|(addr, addr_lst) and \n\
  207. addr= host|ip_address|interface_name. \n\
  208. E.g: -l locahost, -l udp:127.0.0.1:5080, -l eth0:5062,\n\
  209. -l \"sctp:(eth0)\", -l \"(eth0, eth1, 127.0.0.1):5065\".\n\
  210. The default behaviour is to listen on all the interfaces.\n\
  211. -n processes Number of child processes to fork per interface\n\
  212. (default: 8)\n\
  213. -r Use dns to check if is necessary to add a \"received=\"\n\
  214. field to a via\n\
  215. -R Same as `-r` but use reverse dns;\n\
  216. (to use both use `-rR`)\n\
  217. -v Turn on \"via:\" host checking when forwarding replies\n\
  218. -d Debugging mode (multiple -d increase the level)\n\
  219. -D no 1..do not fork (almost) anyway, 2..do not daemonize creator\n\
  220. 3..daemonize (default)\n\
  221. -E Log to stderr\n"
  222. #ifdef USE_TCP
  223. " -T Disable tcp\n\
  224. -N Number of tcp child processes (default: equal to `-n')\n\
  225. -W type poll method (depending on support in OS, it can be: poll,\n\
  226. epoll_lt, epoll_et, sigio_rt, select, kqueue, /dev/poll)\n"
  227. #endif
  228. #ifdef USE_SCTP
  229. " -S disable sctp\n\
  230. -Q Number of sctp child processes (default: equal to `-n')\n"
  231. #endif /* USE_SCTP */
  232. " -V Version number\n\
  233. -h This help message\n\
  234. -b nr Maximum receive buffer size which will not be exceeded by\n\
  235. auto-probing procedure even if OS allows\n\
  236. -m nr Size of shared memory allocated in Megabytes\n\
  237. -M nr Size of private memory allocated, in Megabytes\n\
  238. -w dir Change the working directory to \"dir\" (default: \"/\")\n\
  239. -t dir Chroot to \"dir\"\n\
  240. -u uid Change uid \n\
  241. -g gid Change gid \n\
  242. -P file Create a pid file\n\
  243. -G file Create a pgid file\n\
  244. -O nr Script optimization level (debugging option)\n\
  245. -a mode Auto aliases mode: enable with yes or on,\n\
  246. disable with no or off\n\
  247. -A define Add config pre-processor define (e.g., -A WITH_AUTH)\n"
  248. #ifdef STATS
  249. " -s file File to which statistics is dumped (disabled otherwise)\n"
  250. #endif
  251. ;
  252. /* print compile-time constants */
  253. void print_ct_constants(void)
  254. {
  255. #ifdef ADAPTIVE_WAIT
  256. printf("ADAPTIVE_WAIT_LOOPS=%d, ", ADAPTIVE_WAIT_LOOPS);
  257. #endif
  258. /*
  259. #ifdef SHM_MEM
  260. printf("SHM_MEM_SIZE=%d, ", SHM_MEM_SIZE);
  261. #endif
  262. */
  263. printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
  264. " MAX_URI_SIZE %d, BUF_SIZE %d, DEFAULT PKG_SIZE %uMB\n",
  265. MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
  266. BUF_SIZE, PKG_MEM_SIZE);
  267. #ifdef USE_TCP
  268. printf("poll method support: %s.\n", poll_support);
  269. #endif
  270. }
  271. /* print compile-time constants */
  272. void print_internals(void)
  273. {
  274. printf("Print out of %s internals\n", NAME);
  275. printf(" Version: %s\n", full_version);
  276. printf(" Default config: %s\n", CFG_FILE);
  277. printf(" Default paths to modules: %s\n", MODS_DIR);
  278. printf(" Compile flags: %s\n", ver_flags );
  279. printf(" MAX_RECV_BUFFER_SIZE=%d\n", MAX_RECV_BUFFER_SIZE);
  280. printf(" MAX_LISTEN=%d\n", MAX_LISTEN);
  281. printf(" MAX_URI_SIZE=%d\n", MAX_URI_SIZE);
  282. printf(" BUF_SIZE=%d\n", BUF_SIZE);
  283. printf(" DEFAULT PKG_SIZE=%uMB\n", PKG_MEM_SIZE);
  284. #ifdef SHM_MEM
  285. printf(" DEFAULT SHM_SIZE=%uMB\n", SHM_MEM_SIZE);
  286. #endif
  287. #ifdef ADAPTIVE_WAIT
  288. printf(" ADAPTIVE_WAIT_LOOPS=%d\n", ADAPTIVE_WAIT_LOOPS);
  289. #endif
  290. #ifdef USE_TCP
  291. printf(" TCP poll methods: %s\n", poll_support);
  292. #endif
  293. printf(" Source code revision ID: %s\n", ver_id);
  294. printf(" Compiled with: %s\n", ver_compiler);
  295. printf(" Compiled on: %s\n", ver_compiled_time);
  296. printf("Thank you for flying %s!\n", NAME);
  297. }
  298. /* debugging function */
  299. /*
  300. void receive_stdin_loop(void)
  301. {
  302. #define BSIZE 1024
  303. char buf[BSIZE+1];
  304. int len;
  305. while(1){
  306. len=fread(buf,1,BSIZE,stdin);
  307. buf[len+1]=0;
  308. receive_msg(buf, len);
  309. printf("-------------------------\n");
  310. }
  311. }
  312. */
  313. /* global vars */
  314. int own_pgid = 0; /* whether or not we have our own pgid (and it's ok
  315. to use kill(0, sig) */
  316. char* mods_dir = MODS_DIR; /* search path for dyn. loadable modules */
  317. char* cfg_file = 0;
  318. unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
  319. not want to exceed during the
  320. auto-probing procedure; may
  321. be re-configured */
  322. unsigned int sql_buffer_size = 65535; /* Size for the SQL buffer. Defaults to 64k.
  323. This may be re-configured */
  324. int socket_workers = 0; /* number of workers processing requests for a socket
  325. - it's reset everytime with a new listen socket */
  326. int children_no = 0; /* number of children processing requests */
  327. #ifdef USE_TCP
  328. int tcp_cfg_children_no = 0; /* set via config or command line option */
  329. int tcp_children_no = 0; /* based on socket_workers and tcp_cfg_children_no */
  330. int tcp_disable = 0; /* 1 if tcp is disabled */
  331. #endif
  332. #ifdef USE_TLS
  333. #ifdef CORE_TLS
  334. int tls_disable = 0; /* tls enabled by default */
  335. #else
  336. int tls_disable = 1; /* tls disabled by default */
  337. #endif /* CORE_TLS */
  338. #endif /* USE_TLS */
  339. #ifdef USE_SCTP
  340. int sctp_children_no = 0;
  341. int sctp_disable = 2; /* 1 if sctp is disabled, 2 if auto mode, 0 enabled */
  342. #endif /* USE_SCTP */
  343. struct process_table *pt=0; /*array with children pids, 0= main proc,
  344. alloc'ed in shared mem if possible*/
  345. int *process_count = 0; /* Total number of SER processes currently
  346. running */
  347. gen_lock_t* process_lock; /* lock on the process table */
  348. int process_no = 0; /* index of process in the pt */
  349. time_t up_since;
  350. int sig_flag = 0; /* last signal received */
  351. int dont_fork = 0;
  352. int dont_daemonize = 0;
  353. int log_stderr = 0;
  354. /* set custom app name for syslog printing */
  355. char *log_name = 0;
  356. pid_t creator_pid = (pid_t) -1;
  357. int config_check = 0;
  358. /* check if reply first via host==us */
  359. int check_via = 0;
  360. /* translate user=phone URIs to TEL URIs */
  361. int phone2tel = 1;
  362. /* shall use stateful synonym branches? faster but not reboot-safe */
  363. int syn_branch = 1;
  364. /* debugging level for timer debugging */
  365. int timerlog = L_WARN;
  366. /* should replies include extensive warnings? by default no,
  367. good for trouble-shooting
  368. */
  369. int sip_warning = 0;
  370. /* should localy-generated messages include server's signature?
  371. be default yes, good for trouble-shooting
  372. */
  373. int server_signature=1;
  374. str server_hdr = {SERVER_HDR, SERVER_HDR_LEN};
  375. str user_agent_hdr = {USER_AGENT, USER_AGENT_LEN};
  376. /* should ser try to locate outbound interface on multihomed
  377. * host? by default not -- too expensive
  378. */
  379. int mhomed=0;
  380. /* use dns and/or rdns or to see if we need to add
  381. a ;received=x.x.x.x to via: */
  382. int received_dns = 0;
  383. /* add or not the rev dns names to aliases list */
  384. int sr_auto_aliases=1;
  385. char* working_dir = 0;
  386. char* chroot_dir = 0;
  387. char* user=0;
  388. char* group=0;
  389. int uid = 0;
  390. int gid = 0;
  391. char* sock_user=0;
  392. char* sock_group=0;
  393. int sock_uid= -1;
  394. int sock_gid= -1;
  395. int sock_mode= S_IRUSR| S_IWUSR| S_IRGRP| S_IWGRP; /* rw-rw---- */
  396. int server_id = 0; /* Configurable unique ID of the server */
  397. /* set timeval for each received sip message */
  398. int sr_msg_time = 1;
  399. /* more config stuff */
  400. int disable_core_dump=0; /* by default enabled */
  401. int open_files_limit=-1; /* don't touch it by default */
  402. /* memory options */
  403. int shm_force_alloc=0; /* force immediate (on startup) page allocation
  404. (by writting 0 in the pages), useful if
  405. mlock_pages is also 1 */
  406. int mlock_pages=0; /* default off, try to disable swapping */
  407. /* real time options */
  408. int real_time=0; /* default off, flags: 1 on only timer, 2 slow timer,
  409. 4 all procs (7=all) */
  410. int rt_prio=0;
  411. int rt_policy=0; /* SCHED_OTHER */
  412. int rt_timer1_prio=0; /* "fast" timer */
  413. int rt_timer2_prio=0; /* "slow" timer */
  414. int rt_timer1_policy=0; /* "fast" timer, SCHED_OTHER */
  415. int rt_timer2_policy=0; /* "slow" timer, SCHED_OTHER */
  416. /* a hint to reply modules whether they should send reply
  417. to IP advertised in Via or IP from which a request came
  418. */
  419. int reply_to_via=0;
  420. #ifdef USE_MCAST
  421. int mcast_loopback = 0;
  422. int mcast_ttl = -1; /* if -1, don't touch it, use the default (usually 1) */
  423. #endif /* USE_MCAST */
  424. int tos = IPTOS_LOWDELAY;
  425. int pmtu_discovery = 0;
  426. #ifdef USE_IPV6
  427. int auto_bind_ipv6 = 0;
  428. #endif
  429. #if 0
  430. char* names[MAX_LISTEN]; /* our names */
  431. int names_len[MAX_LISTEN]; /* lengths of the names*/
  432. struct ip_addr addresses[MAX_LISTEN]; /* our ips */
  433. int addresses_no=0; /* number of names/ips */
  434. #endif
  435. struct socket_info* udp_listen=0;
  436. #ifdef USE_TCP
  437. int tcp_main_pid=0; /* set after the tcp main process is started */
  438. struct socket_info* tcp_listen=0;
  439. #endif
  440. #ifdef USE_TLS
  441. struct socket_info* tls_listen=0;
  442. #endif
  443. #ifdef USE_SCTP
  444. struct socket_info* sctp_listen=0;
  445. #endif
  446. struct socket_info* bind_address=0; /* pointer to the crt. proc.
  447. listening address*/
  448. struct socket_info* sendipv4; /* ipv4 socket to use when msg. comes from ipv6*/
  449. struct socket_info* sendipv6; /* same as above for ipv6 */
  450. #ifdef USE_RAW_SOCKS
  451. int raw_udp4_send_sock = -1; /* raw socket used for sending udp4 packets */
  452. #endif /* USE_RAW_SOCKS */
  453. #ifdef USE_TCP
  454. struct socket_info* sendipv4_tcp;
  455. struct socket_info* sendipv6_tcp;
  456. #endif
  457. #ifdef USE_TLS
  458. struct socket_info* sendipv4_tls;
  459. struct socket_info* sendipv6_tls;
  460. #endif
  461. #ifdef USE_SCTP
  462. struct socket_info* sendipv4_sctp;
  463. struct socket_info* sendipv6_sctp;
  464. #endif
  465. unsigned short port_no=0; /* default port*/
  466. #ifdef USE_TLS
  467. unsigned short tls_port_no=0; /* default port */
  468. #endif
  469. #ifdef USE_STUN
  470. /* refresh interval in miliseconds */
  471. unsigned int stun_refresh_interval=0;
  472. /* stun can be switch off even if it is compiled */
  473. int stun_allow_stun=1;
  474. /* use or don't use fingerprint */
  475. int stun_allow_fp=1;
  476. #endif
  477. struct host_alias* aliases=0; /* name aliases list */
  478. /* Parameter to child_init */
  479. int child_rank = 0;
  480. /* how much to wait for children to terminate, before taking extreme measures*/
  481. int ser_kill_timeout=DEFAULT_SER_KILL_TIMEOUT;
  482. /* process_bm_t process_bit = 0; */
  483. #ifdef ROUTE_SRV
  484. #endif
  485. /* cfg parsing */
  486. int cfg_errors=0;
  487. int cfg_warnings=0;
  488. /* shared memory (in MB) */
  489. unsigned long shm_mem_size=0;
  490. /* private (pkg) memory (in MB) */
  491. unsigned long pkg_mem_size=0;
  492. /* export command-line to anywhere else */
  493. int my_argc;
  494. char **my_argv;
  495. /* set to 1 when the cfg framework and core cfg is initialized/registered */
  496. static int cfg_ok=0;
  497. #define MAX_FD 32 /* maximum number of inherited open file descriptors,
  498. (normally it shouldn't be bigger than 3) */
  499. extern FILE* yyin;
  500. extern int yyparse(void);
  501. int is_main=1; /* flag = is this the "main" process? */
  502. int fixup_complete=0; /* flag = is the fixup complete ? */
  503. char* pid_file = 0; /* filename as asked by use */
  504. char* pgid_file = 0;
  505. /* call it before exiting; if show_status==1, mem status is displayed */
  506. void cleanup(show_status)
  507. {
  508. int memlog;
  509. /*clean-up*/
  510. #ifndef SHM_SAFE_MALLOC
  511. if (mem_lock)
  512. shm_unlock(); /* hack: force-unlock the shared memory lock in case
  513. some process crashed and let it locked; this will
  514. allow an almost gracious shutdown */
  515. #endif
  516. destroy_rpcs();
  517. destroy_modules();
  518. #ifdef USE_DNS_CACHE
  519. destroy_dns_cache();
  520. #endif
  521. #ifdef USE_DST_BLACKLIST
  522. destroy_dst_blacklist();
  523. #endif
  524. /* restore the original core configuration before the
  525. * config block is freed, otherwise even logging is unusable,
  526. * it can case segfault */
  527. if (cfg_ok){
  528. cfg_update();
  529. /* copy current config into default_core_cfg */
  530. if (core_cfg)
  531. default_core_cfg=*((struct cfg_group_core*)core_cfg);
  532. }
  533. core_cfg = &default_core_cfg;
  534. cfg_destroy();
  535. #ifdef USE_TCP
  536. destroy_tcp();
  537. #ifdef USE_TLS
  538. destroy_tls();
  539. #endif /* USE_TLS */
  540. #endif /* USE_TCP */
  541. #ifdef USE_SCTP
  542. destroy_sctp();
  543. #endif
  544. destroy_timer();
  545. pv_destroy_api();
  546. destroy_script_cb();
  547. destroy_nonsip_hooks();
  548. destroy_routes();
  549. destroy_atomic_ops();
  550. destroy_counters();
  551. memlog=cfg_get(core, core_cfg, memlog);
  552. #ifdef PKG_MALLOC
  553. if (show_status && memlog <= cfg_get(core, core_cfg, debug)){
  554. if (cfg_get(core, core_cfg, mem_summary) & 1) {
  555. LOG(memlog, "Memory status (pkg):\n");
  556. pkg_status();
  557. }
  558. if (cfg_get(core, core_cfg, mem_summary) & 4) {
  559. LOG(memlog, "Memory still-in-use summary (pkg):\n");
  560. pkg_sums();
  561. }
  562. }
  563. #endif
  564. #ifdef SHM_MEM
  565. if (pt) shm_free(pt);
  566. pt=0;
  567. if (show_status && memlog <= cfg_get(core, core_cfg, debug)){
  568. if (cfg_get(core, core_cfg, mem_summary) & 2) {
  569. LOG(memlog, "Memory status (shm):\n");
  570. shm_status();
  571. }
  572. if (cfg_get(core, core_cfg, mem_summary) & 8) {
  573. LOG(memlog, "Memory still-in-use summary (shm):\n");
  574. shm_sums();
  575. }
  576. }
  577. /* zero all shmem alloc vars that we still use */
  578. shm_mem_destroy();
  579. #endif
  580. destroy_lock_ops();
  581. if (pid_file) unlink(pid_file);
  582. if (pgid_file) unlink(pgid_file);
  583. destroy_pkg_mallocs();
  584. }
  585. /* tries to send a signal to all our processes
  586. * if daemonized is ok to send the signal to all the process group,
  587. * however if not daemonized we might end up sending the signal also
  588. * to the shell which launched us => most signals will kill it if
  589. * it's not in interactive mode and we don't want this. The non-daemonized
  590. * case can occur when an error is encountered before daemonize is called
  591. * (e.g. when parsing the config file) or when ser is started in "dont-fork"
  592. * mode. Sending the signal to all the processes in pt[] will not work
  593. * for processes forked from modules (which have no correspondent entry in
  594. * pt), but this can happen only in dont_fork mode (which is only for
  595. * debugging). So in the worst case + "dont-fork" we might leave some
  596. * zombies. -- andrei */
  597. static void kill_all_children(int signum)
  598. {
  599. int r;
  600. if (own_pgid) kill(0, signum);
  601. else if (pt){
  602. /* lock processes table only if this is a child process
  603. * (only main can add processes, so from main is safe not to lock
  604. * and moreover it avoids the lock-holding suicidal children problem)
  605. */
  606. if (!is_main) lock_get(process_lock);
  607. for (r=1; r<*process_count; r++){
  608. if (r==process_no) continue; /* try not to be suicidal */
  609. if (pt[r].pid) {
  610. kill(pt[r].pid, signum);
  611. }
  612. else LOG(L_CRIT, "BUG: killing: %s > %d no pid!!!\n",
  613. pt[r].desc, pt[r].pid);
  614. }
  615. if (!is_main) lock_release(process_lock);
  616. }
  617. }
  618. /* if this handler is called, a critical timeout has occurred while
  619. * waiting for the children to finish => we should kill everything and exit */
  620. static void sig_alarm_kill(int signo)
  621. {
  622. kill_all_children(SIGKILL); /* this will kill the whole group
  623. including "this" process;
  624. for debugging replace with SIGABRT
  625. (but warning: it might generate lots
  626. of cores) */
  627. }
  628. /* like sig_alarm_kill, but the timeout has occurred when cleaning up
  629. * => try to leave a core for future diagnostics */
  630. static void sig_alarm_abort(int signo)
  631. {
  632. /* LOG is not signal safe, but who cares, we are abort-ing anyway :-) */
  633. LOG(L_CRIT, "BUG: shutdown timeout triggered, dying...");
  634. abort();
  635. }
  636. static void shutdown_children(int sig, int show_status)
  637. {
  638. kill_all_children(sig);
  639. if (set_sig_h(SIGALRM, sig_alarm_kill) == SIG_ERR ) {
  640. LOG(L_ERR, "ERROR: shutdown: could not install SIGALARM handler\n");
  641. /* continue, the process will die anyway if no
  642. * alarm is installed which is exactly what we want */
  643. }
  644. alarm(ser_kill_timeout);
  645. while((wait(0) > 0) || (errno==EINTR)); /* wait for all the
  646. children to terminate*/
  647. set_sig_h(SIGALRM, sig_alarm_abort);
  648. cleanup(show_status); /* cleanup & show status*/
  649. alarm(0);
  650. set_sig_h(SIGALRM, SIG_IGN);
  651. }
  652. void handle_sigs(void)
  653. {
  654. pid_t chld;
  655. int chld_status;
  656. int memlog;
  657. switch(sig_flag){
  658. case 0: break; /* do nothing*/
  659. case SIGPIPE:
  660. /* SIGPIPE might be rarely received on use of
  661. exec module; simply ignore it
  662. */
  663. LOG(L_WARN, "WARNING: SIGPIPE received and ignored\n");
  664. break;
  665. case SIGINT:
  666. case SIGTERM:
  667. /* we end the program in all these cases */
  668. if (sig_flag==SIGINT)
  669. DBG("INT received, program terminates\n");
  670. else
  671. DBG("SIGTERM received, program terminates\n");
  672. LOG(L_NOTICE, "Thank you for flying " NAME "!!!\n");
  673. /* shutdown/kill all the children */
  674. shutdown_children(SIGTERM, 1);
  675. exit(0);
  676. break;
  677. case SIGUSR1:
  678. #ifdef STATS
  679. dump_all_statistic();
  680. #endif
  681. memlog=cfg_get(core, core_cfg, memlog);
  682. #ifdef PKG_MALLOC
  683. if (memlog <= cfg_get(core, core_cfg, debug)){
  684. if (cfg_get(core, core_cfg, mem_summary) & 1) {
  685. LOG(memlog, "Memory status (pkg):\n");
  686. pkg_status();
  687. }
  688. if (cfg_get(core, core_cfg, mem_summary) & 2) {
  689. LOG(memlog, "Memory still-in-use summary (pkg):\n");
  690. pkg_sums();
  691. }
  692. }
  693. #endif
  694. #ifdef SHM_MEM
  695. if (memlog <= cfg_get(core, core_cfg, debug)){
  696. if (cfg_get(core, core_cfg, mem_summary) & 1) {
  697. LOG(memlog, "Memory status (shm):\n");
  698. shm_status();
  699. }
  700. if (cfg_get(core, core_cfg, mem_summary) & 2) {
  701. LOG(memlog, "Memory still-in-use summary (shm):\n");
  702. shm_sums();
  703. }
  704. }
  705. #endif
  706. break;
  707. case SIGCHLD:
  708. while ((chld=waitpid( -1, &chld_status, WNOHANG ))>0) {
  709. if (WIFEXITED(chld_status))
  710. LOG(L_ALERT, "child process %ld exited normally,"
  711. " status=%d\n", (long)chld,
  712. WEXITSTATUS(chld_status));
  713. else if (WIFSIGNALED(chld_status)) {
  714. LOG(L_ALERT, "child process %ld exited by a signal"
  715. " %d\n", (long)chld, WTERMSIG(chld_status));
  716. #ifdef WCOREDUMP
  717. LOG(L_ALERT, "core was %sgenerated\n",
  718. WCOREDUMP(chld_status) ? "" : "not " );
  719. #endif
  720. }else if (WIFSTOPPED(chld_status))
  721. LOG(L_ALERT, "child process %ld stopped by a"
  722. " signal %d\n", (long)chld,
  723. WSTOPSIG(chld_status));
  724. }
  725. #ifndef STOP_JIRIS_CHANGES
  726. if (dont_fork) {
  727. LOG(L_INFO, "INFO: dont_fork turned on, living on\n");
  728. break;
  729. }
  730. LOG(L_INFO, "INFO: terminating due to SIGCHLD\n");
  731. #endif
  732. /* exit */
  733. shutdown_children(SIGTERM, 1);
  734. DBG("terminating due to SIGCHLD\n");
  735. exit(0);
  736. break;
  737. case SIGHUP: /* ignoring it*/
  738. DBG("SIGHUP received, ignoring it\n");
  739. break;
  740. default:
  741. LOG(L_CRIT, "WARNING: unhandled signal %d\n", sig_flag);
  742. }
  743. sig_flag=0;
  744. }
  745. /* added by jku; allows for regular exit on a specific signal;
  746. good for profiling which only works if exited regularly and
  747. not by default signal handlers
  748. - modified by andrei: moved most of the stuff to handle_sigs,
  749. made it safer for the "fork" case
  750. */
  751. void sig_usr(int signo)
  752. {
  753. #ifdef PKG_MALLOC
  754. int memlog;
  755. #endif
  756. if (is_main){
  757. if (sig_flag==0) sig_flag=signo;
  758. else /* previous sig. not processed yet, ignoring? */
  759. return; ;
  760. if (dont_fork)
  761. /* only one proc, doing everything from the sig handler,
  762. unsafe, but this is only for debugging mode*/
  763. handle_sigs();
  764. }else{
  765. /* process the important signals */
  766. switch(signo){
  767. case SIGPIPE:
  768. #ifdef SIG_DEBUG /* signal unsafe stuff follows */
  769. LOG(L_INFO, "INFO: signal %d received\n", signo);
  770. #endif
  771. break;
  772. case SIGINT:
  773. case SIGTERM:
  774. #ifdef SIG_DEBUG /* signal unsafe stuff follows */
  775. LOG(L_INFO, "INFO: signal %d received\n", signo);
  776. /* print memory stats for non-main too */
  777. #ifdef PKG_MALLOC
  778. /* make sure we have current cfg values, but update only
  779. the safe part (values not requiring callbacks), to
  780. account for processes that might not have registered
  781. config support */
  782. cfg_update_no_cbs();
  783. memlog=cfg_get(core, core_cfg, memlog);
  784. if (memlog <= cfg_get(core, core_cfg, debug)){
  785. if (cfg_get(core, core_cfg, mem_summary) & 1) {
  786. LOG(memlog, "Memory status (pkg):\n");
  787. pkg_status();
  788. }
  789. if (cfg_get(core, core_cfg, mem_summary) & 2) {
  790. LOG(memlog, "Memory still-in-use summary (pkg):"
  791. "\n");
  792. pkg_sums();
  793. }
  794. }
  795. #endif
  796. #endif
  797. _exit(0);
  798. break;
  799. case SIGUSR1:
  800. /* statistics, do nothing, printed only from the main proc */
  801. break;
  802. /* ignored*/
  803. case SIGUSR2:
  804. case SIGHUP:
  805. break;
  806. case SIGCHLD:
  807. #ifndef STOP_JIRIS_CHANGES
  808. #ifdef SIG_DEBUG /* signal unsafe stuff follows */
  809. DBG("SIGCHLD received: "
  810. "we do not worry about grand-children\n");
  811. #endif
  812. #else
  813. _exit(0); /* terminate if one child died */
  814. #endif
  815. break;
  816. }
  817. }
  818. }
  819. /* install the signal handlers, returns 0 on success, -1 on error */
  820. int install_sigs(void)
  821. {
  822. /* added by jku: add exit handler */
  823. if (set_sig_h(SIGINT, sig_usr) == SIG_ERR ) {
  824. ERR("no SIGINT signal handler can be installed\n");
  825. goto error;
  826. }
  827. /* if we debug and write to a pipe, we want to exit nicely too */
  828. if (set_sig_h(SIGPIPE, sig_usr) == SIG_ERR ) {
  829. ERR("no SIGINT signal handler can be installed\n");
  830. goto error;
  831. }
  832. if (set_sig_h(SIGUSR1, sig_usr) == SIG_ERR ) {
  833. ERR("no SIGUSR1 signal handler can be installed\n");
  834. goto error;
  835. }
  836. if (set_sig_h(SIGCHLD , sig_usr) == SIG_ERR ) {
  837. ERR("no SIGCHLD signal handler can be installed\n");
  838. goto error;
  839. }
  840. if (set_sig_h(SIGTERM , sig_usr) == SIG_ERR ) {
  841. ERR("no SIGTERM signal handler can be installed\n");
  842. goto error;
  843. }
  844. if (set_sig_h(SIGHUP , sig_usr) == SIG_ERR ) {
  845. ERR("no SIGHUP signal handler can be installed\n");
  846. goto error;
  847. }
  848. if (set_sig_h(SIGUSR2 , sig_usr) == SIG_ERR ) {
  849. ERR("no SIGUSR2 signal handler can be installed\n");
  850. goto error;
  851. }
  852. return 0;
  853. error:
  854. return -1;
  855. }
  856. /* returns -1 on error, 0 on success
  857. * sets proto */
  858. static int parse_proto(unsigned char* s, long len, int* proto)
  859. {
  860. #define PROTO2UINT3(a, b, c) (( (((unsigned int)(a))<<16)+ \
  861. (((unsigned int)(b))<<8)+ \
  862. ((unsigned int)(c)) ) | 0x20202020)
  863. #define PROTO2UINT4(a, b ,c ,d) (( (((unsigned int)(a))<<24)+ \
  864. (((unsigned int)(b))<<16)+ \
  865. (((unsigned int)(c))<< 8)+ \
  866. (((unsigned int)(d))) \
  867. )| 0x20202020 )
  868. unsigned int i;
  869. if (likely(len==3)){
  870. i=PROTO2UINT3(s[0], s[1], s[2]);
  871. switch(i){
  872. case PROTO2UINT3('u', 'd', 'p'):
  873. *proto=PROTO_UDP;
  874. break;
  875. #ifdef USE_TCP
  876. case PROTO2UINT3('t', 'c', 'p'):
  877. *proto=PROTO_TCP;
  878. break;
  879. #ifdef USE_TLS
  880. case PROTO2UINT3('t', 'l', 's'):
  881. *proto=PROTO_TLS;
  882. break;
  883. #endif
  884. #endif
  885. default:
  886. return -1;
  887. }
  888. }
  889. #ifdef USE_SCTP
  890. else if (likely(len==4)){
  891. i=PROTO2UINT4(s[0], s[1], s[2], s[3]);
  892. if (i==PROTO2UINT4('s', 'c', 't', 'p'))
  893. *proto=PROTO_SCTP;
  894. else
  895. return -1;
  896. }
  897. #endif /* USE_SCTP */
  898. else
  899. return -1;
  900. return 0;
  901. }
  902. static struct name_lst* mk_name_lst_elem(char* name, int name_len, int flags)
  903. {
  904. struct name_lst* l;
  905. l=pkg_malloc(sizeof(struct name_lst)+name_len+1/* 0 */);
  906. if (l){
  907. l->name=((char*)l)+sizeof(struct name_lst);
  908. memcpy(l->name, name, name_len);
  909. l->name[name_len]=0;
  910. l->flags=flags;
  911. l->next=0;
  912. }
  913. return l;
  914. }
  915. /* free a name_lst list with elements allocated with mk_name_lst_elem
  916. * (single block both for the structure and for the name) */
  917. static void free_name_lst(struct name_lst* lst)
  918. {
  919. struct name_lst* l;
  920. while(lst){
  921. l=lst;
  922. lst=lst->next;
  923. pkg_free(l);
  924. }
  925. }
  926. /* parse h and returns a name lst (flags are set to SI_IS_MHOMED if
  927. * h contains more then one name or contains a name surrounded by '(' ')' )
  928. * valid formats: "hostname"
  929. * "(hostname, hostname1, hostname2)"
  930. * "(hostname hostname1 hostname2)"
  931. * "(hostname)"
  932. */
  933. static struct name_lst* parse_name_lst(char* h, int h_len)
  934. {
  935. char* last;
  936. char* p;
  937. struct name_lst* n_lst;
  938. struct name_lst* l;
  939. struct name_lst** tail;
  940. int flags;
  941. n_lst=0;
  942. tail=&n_lst;
  943. last=h+h_len-1;
  944. flags=0;
  945. /* eat whitespace */
  946. for(; h<=last && ((*h==' ') || (*h=='\t')); h++);
  947. for(; last>h && ((*last==' ') || (*last=='\t')); last--);
  948. /* catch empty strings and invalid lens */
  949. if (h>last) goto error;
  950. if (*h=='('){
  951. /* list mode */
  952. if (*last!=')' || ((h+1)>(last-1)))
  953. goto error;
  954. h++;
  955. last--;
  956. flags=SI_IS_MHOMED;
  957. for(p=h; p<=last; p++)
  958. switch (*p){
  959. case ',':
  960. case ';':
  961. case ' ':
  962. case '\t':
  963. if ((int)(p-h)>0){
  964. l=mk_name_lst_elem(h, (int)(p-h), flags);
  965. if (l==0)
  966. goto error;
  967. *tail=l;
  968. tail=&l->next;
  969. }
  970. h=p+1;
  971. break;
  972. }
  973. }else{
  974. /* single addr. mode */
  975. flags=0;
  976. p=last+1;
  977. }
  978. if ((int)(p-h)>0){
  979. l=mk_name_lst_elem(h, (int)(p-h), flags);
  980. if (l==0)
  981. goto error;
  982. *tail=l;
  983. tail=&l->next;
  984. }
  985. return n_lst;
  986. error:
  987. if (n_lst) free_name_lst(n_lst);
  988. return 0;
  989. }
  990. /*
  991. * parses [proto:]host[:port] or
  992. * [proto:](host_1, host_2, ... host_n)[:port]
  993. * where proto= udp|tcp|tls
  994. * returns fills proto, port, host and returns list of addresses on success
  995. * (pkg malloc'ed) and 0 on failure
  996. */
  997. /** get protocol host and port from a string representation.
  998. * parses [proto:]host[:port] or
  999. * [proto:](host_1, host_2, ... host_n)[:port]
  1000. * where proto= udp|tcp|tls|sctp
  1001. * @param s - string (like above)
  1002. * @param host - will be filled with the host part
  1003. * Note: for multi-homing it wil contain all the addresses
  1004. * (e.g.: "sctp:(1.2.3.4, 5.6.7.8)" => host="(1.2.3.4, 5.6.7.8)")
  1005. * @param hlen - will be filled with the length of the host part.
  1006. * @param port - will be filled with the port if present or 0 if it's not.
  1007. * @param proto - will be filled with the protocol if present or PROTO_NONE
  1008. * if it's not.
  1009. * @return fills proto, port, host and returns 0 on success and -1 on failure.
  1010. */
  1011. int parse_phostport(char* s, char** host, int* hlen,
  1012. int* port, int* proto)
  1013. {
  1014. char* first; /* first ':' occurrence */
  1015. char* second; /* second ':' occurrence */
  1016. char* p;
  1017. int bracket;
  1018. char* tmp;
  1019. first=second=0;
  1020. bracket=0;
  1021. /* find the first 2 ':', ignoring possible ipv6 addresses
  1022. * (substrings between [])
  1023. */
  1024. for(p=s; *p; p++){
  1025. switch(*p){
  1026. case '[':
  1027. bracket++;
  1028. if (bracket>1) goto error_brackets;
  1029. break;
  1030. case ']':
  1031. bracket--;
  1032. if (bracket<0) goto error_brackets;
  1033. break;
  1034. case ':':
  1035. if (bracket==0){
  1036. if (first==0) first=p;
  1037. else if( second==0) second=p;
  1038. else goto error_colons;
  1039. }
  1040. break;
  1041. }
  1042. }
  1043. if (p==s) return -1;
  1044. if (*(p-1)==':') goto error_colons;
  1045. if (first==0){ /* no ':' => only host */
  1046. *host=s;
  1047. *hlen=(int)(p-s);
  1048. *port=0;
  1049. *proto=0;
  1050. goto end;
  1051. }
  1052. if (second){ /* 2 ':' found => check if valid */
  1053. if (parse_proto((unsigned char*)s, first-s, proto)<0) goto error_proto;
  1054. *port=strtol(second+1, &tmp, 10);
  1055. if ((tmp==0)||(*tmp)||(tmp==second+1)) goto error_port;
  1056. *host=first+1;
  1057. *hlen=(int)(second-*host);
  1058. goto end;
  1059. }
  1060. /* only 1 ':' found => it's either proto:host or host:port */
  1061. *port=strtol(first+1, &tmp, 10);
  1062. if ((tmp==0)||(*tmp)||(tmp==first+1)){
  1063. /* invalid port => it's proto:host */
  1064. if (parse_proto((unsigned char*)s, first-s, proto)<0) goto error_proto;
  1065. *port=0;
  1066. *host=first+1;
  1067. *hlen=(int)(p-*host);
  1068. }else{
  1069. /* valid port => its host:port */
  1070. *proto=0;
  1071. *host=s;
  1072. *hlen=(int)(first-*host);
  1073. }
  1074. end:
  1075. return 0;
  1076. error_brackets:
  1077. LOG(L_ERR, "ERROR: parse_phostport: too many brackets in %s\n", s);
  1078. return -1;
  1079. error_colons:
  1080. LOG(L_ERR, "ERROR: parse_phostport: too many colons in %s\n", s);
  1081. return -1;
  1082. error_proto:
  1083. LOG(L_ERR, "ERROR: parse_phostport: bad protocol in %s\n", s);
  1084. return -1;
  1085. error_port:
  1086. LOG(L_ERR, "ERROR: parse_phostport: bad port number in %s\n", s);
  1087. return -1;
  1088. }
  1089. /** get protocol host, port and MH addresses list from a string representation.
  1090. * parses [proto:]host[:port] or
  1091. * [proto:](host_1, host_2, ... host_n)[:port]
  1092. * where proto= udp|tcp|tls|sctp
  1093. * @param s - string (like above)
  1094. * @param host - will be filled with the host part
  1095. * Note: for multi-homing it wil contain all the addresses
  1096. * (e.g.: "sctp:(1.2.3.4, 5.6.7.8)" => host="(1.2.3.4, 5.6.7.8)")
  1097. * @param hlen - will be filled with the length of the host part.
  1098. * @param port - will be filled with the port if present or 0 if it's not.
  1099. * @param proto - will be filled with the protocol if present or PROTO_NONE
  1100. * if it's not.
  1101. * @return fills proto, port, host and returns list of addresses on success
  1102. * (pkg malloc'ed) and 0 on failure
  1103. */
  1104. static struct name_lst* parse_phostport_mh(char* s, char** host, int* hlen,
  1105. int* port, int* proto)
  1106. {
  1107. if (parse_phostport(s, host, hlen, port, proto)==0)
  1108. return parse_name_lst(*host, *hlen);
  1109. return 0;
  1110. }
  1111. /** Update \c cfg_file variable to contain full pathname. The function updates
  1112. * the value of \c cfg_file global variable to contain full absolute pathname
  1113. * to the main configuration file of SER. The function uses CFG_FILE macro to
  1114. * determine the default path to the configuration file if the user did not
  1115. * specify one using the command line option. If \c cfg_file contains an
  1116. * absolute pathname then it is used unmodified, if it contains a relative
  1117. * pathanme than the value returned by \c getcwd function will be added at the
  1118. * beginning. This function must be run before SER changes its current working
  1119. * directory to / (in daemon mode).
  1120. * @return Zero on success, negative number
  1121. * on error.
  1122. */
  1123. int fix_cfg_file(void)
  1124. {
  1125. char* res = NULL;
  1126. size_t max_len, cwd_len, cfg_len;
  1127. if (cfg_file == NULL) cfg_file = CFG_FILE;
  1128. if (cfg_file[0] == '/') return 0;
  1129. /* cfg_file contains a relative pathname, get the current
  1130. * working directory and add it at the beginning
  1131. */
  1132. cfg_len = strlen(cfg_file);
  1133. max_len = pathmax();
  1134. if ((res = malloc(max_len)) == NULL) goto error;
  1135. if (getcwd(res, max_len) == NULL) goto error;
  1136. cwd_len = strlen(res);
  1137. /* Make sure that the buffer is big enough */
  1138. if (cwd_len + 1 + cfg_len >= max_len) goto error;
  1139. res[cwd_len] = '/';
  1140. memcpy(res + cwd_len + 1, cfg_file, cfg_len);
  1141. res[cwd_len + 1 + cfg_len] = '\0'; /* Add terminating zero */
  1142. cfg_file = res;
  1143. return 0;
  1144. error:
  1145. fprintf(stderr, "ERROR: Unable to fix cfg_file to contain full pathname\n");
  1146. if (res) free(res);
  1147. return -1;
  1148. }
  1149. /* main loop */
  1150. int main_loop(void)
  1151. {
  1152. int i;
  1153. pid_t pid;
  1154. struct socket_info* si;
  1155. char si_desc[MAX_PT_DESC];
  1156. #ifdef EXTRA_DEBUG
  1157. int r;
  1158. #endif
  1159. int nrprocs;
  1160. /* one "main" process and n children handling i/o */
  1161. if (dont_fork){
  1162. #ifdef STATS
  1163. setstats( 0 );
  1164. #endif
  1165. if (udp_listen==0){
  1166. LOG(L_ERR, "ERROR: no fork mode requires at least one"
  1167. " udp listen address, exiting...\n");
  1168. goto error;
  1169. }
  1170. /* only one address, we ignore all the others */
  1171. if (udp_init(udp_listen)==-1) goto error;
  1172. bind_address=udp_listen;
  1173. if (bind_address->address.af==AF_INET) {
  1174. sendipv4=bind_address;
  1175. #ifdef USE_RAW_SOCKS
  1176. /* always try to have a raw socket opened if we are using ipv4 */
  1177. raw_udp4_send_sock = raw_socket(IPPROTO_RAW, 0, 0, 1);
  1178. if (raw_udp4_send_sock < 0) {
  1179. if ( default_core_cfg.udp4_raw > 0) {
  1180. /* force use raw socket failed */
  1181. ERR("could not initialize raw udp send socket (ipv4):"
  1182. " %s (%d)\n", strerror(errno), errno);
  1183. if (errno == EPERM)
  1184. ERR("could not initialize raw socket on startup"
  1185. " due to inadequate permissions, please"
  1186. " restart as root or with CAP_NET_RAW\n");
  1187. goto error;
  1188. }
  1189. default_core_cfg.udp4_raw = 0; /* disabled */
  1190. } else {
  1191. register_fds(1);
  1192. if (default_core_cfg.udp4_raw < 0) {
  1193. /* auto-detect => use it */
  1194. default_core_cfg.udp4_raw = 1; /* enabled */
  1195. DBG("raw socket possible => turning it on\n");
  1196. }
  1197. if (default_core_cfg.udp4_raw_ttl < 0) {
  1198. /* auto-detect */
  1199. default_core_cfg.udp4_raw_ttl = sock_get_ttl(sendipv4->socket);
  1200. if (default_core_cfg.udp4_raw_ttl < 0)
  1201. /* error, use some default value */
  1202. default_core_cfg.udp4_raw_ttl = 63;
  1203. }
  1204. }
  1205. #else
  1206. default_core_cfg.udp4_raw = 0;
  1207. #endif /* USE_RAW_SOCKS */
  1208. } else
  1209. sendipv6=bind_address;
  1210. if (udp_listen->next){
  1211. LOG(L_WARN, "WARNING: using only the first listen address"
  1212. " (no fork)\n");
  1213. }
  1214. /* delay cfg_shmize to the last moment (it must be called _before_
  1215. forking). Changes to default cfgs after this point will be
  1216. ignored.
  1217. */
  1218. if (cfg_shmize() < 0) {
  1219. LOG(L_CRIT, "could not initialize shared configuration\n");
  1220. goto error;
  1221. }
  1222. /* Register the children that will keep updating their
  1223. * local configuration */
  1224. cfg_register_child(
  1225. 1 /* main = udp listener */
  1226. + 1 /* timer */
  1227. #ifdef USE_SLOW_TIMER
  1228. + 1 /* slow timer */
  1229. #endif
  1230. );
  1231. if (do_suid()==-1) goto error; /* try to drop privileges */
  1232. /* process_no now initialized to zero -- increase from now on
  1233. as new processes are forked (while skipping 0 reserved for main
  1234. */
  1235. /* Temporary set the local configuration of the main process
  1236. * to make the group instances available in PROC_INIT.
  1237. */
  1238. cfg_main_set_local();
  1239. /* init childs with rank==PROC_INIT before forking any process,
  1240. * this is a place for delayed (after mod_init) initializations
  1241. * (e.g. shared vars that depend on the total number of processes
  1242. * that is known only after all mod_inits have been executed )
  1243. * WARNING: the same init_child will be called latter, a second time
  1244. * for the "main" process with rank PROC_MAIN (make sure things are
  1245. * not initialized twice)*/
  1246. if (init_child(PROC_INIT) < 0) {
  1247. LOG(L_ERR, "ERROR: main_dontfork: init_child(PROC_INT) --"
  1248. " exiting\n");
  1249. cfg_main_reset_local();
  1250. goto error;
  1251. }
  1252. cfg_main_reset_local();
  1253. if (counters_prefork_init(get_max_procs()) == -1) goto error;
  1254. #ifdef USE_SLOW_TIMER
  1255. /* we need another process to act as the "slow" timer*/
  1256. pid = fork_process(PROC_TIMER, "slow timer", 0);
  1257. if (pid<0){
  1258. LOG(L_CRIT, "ERROR: main_loop: Cannot fork\n");
  1259. goto error;
  1260. }
  1261. if (pid==0){
  1262. /* child */
  1263. /* timer!*/
  1264. /* process_bit = 0; */
  1265. if (real_time&2)
  1266. set_rt_prio(rt_timer2_prio, rt_timer2_policy);
  1267. if (arm_slow_timer()<0) goto error;
  1268. slow_timer_main();
  1269. }else{
  1270. slow_timer_pid=pid;
  1271. }
  1272. #endif
  1273. /* we need another process to act as the "main" timer*/
  1274. pid = fork_process(PROC_TIMER, "timer", 0);
  1275. if (pid<0){
  1276. LOG(L_CRIT, "ERROR: main_loop: Cannot fork\n");
  1277. goto error;
  1278. }
  1279. if (pid==0){
  1280. /* child */
  1281. /* timer!*/
  1282. /* process_bit = 0; */
  1283. if (real_time&1)
  1284. set_rt_prio(rt_timer1_prio, rt_timer1_policy);
  1285. if (arm_timer()<0) goto error;
  1286. timer_main();
  1287. }else{
  1288. }
  1289. /* main process, receive loop */
  1290. process_no=0; /*main process number*/
  1291. pt[process_no].pid=getpid();
  1292. snprintf(pt[process_no].desc, MAX_PT_DESC,
  1293. "stand-alone receiver @ %s:%s",
  1294. bind_address->name.s, bind_address->port_no_str.s );
  1295. /* call it also w/ PROC_MAIN to make sure modules that init things
  1296. * only in PROC_MAIN get a chance to run */
  1297. if (init_child(PROC_MAIN) < 0) {
  1298. LOG(L_ERR, "ERROR: main_dontfork: init_child(PROC_MAIN) "
  1299. "-- exiting\n");
  1300. goto error;
  1301. }
  1302. /* We will call child_init even if we
  1303. * do not fork - and it will be called with rank 1 because
  1304. * in fact we behave like a child, not like main process
  1305. */
  1306. if (init_child(PROC_SIPINIT) < 0) {
  1307. LOG(L_ERR, "main_dontfork: init_child failed\n");
  1308. goto error;
  1309. }
  1310. return udp_rcv_loop();
  1311. }else{ /* fork: */
  1312. /* Register the children that will keep updating their
  1313. * local configuration. (udp/tcp/sctp listeneres
  1314. * will be added later.) */
  1315. cfg_register_child(
  1316. 1 /* timer */
  1317. #ifdef USE_SLOW_TIMER
  1318. + 1 /* slow timer */
  1319. #endif
  1320. );
  1321. for(si=udp_listen;si;si=si->next){
  1322. /* create the listening socket (for each address)*/
  1323. /* udp */
  1324. if (udp_init(si)==-1) goto error;
  1325. /* get first ipv4/ipv6 socket*/
  1326. if ((si->address.af==AF_INET)&&
  1327. ((sendipv4==0)||(sendipv4->flags&(SI_IS_LO|SI_IS_MCAST))))
  1328. sendipv4=si;
  1329. #ifdef USE_IPV6
  1330. if ( ((sendipv6==0)||(sendipv6->flags&(SI_IS_LO|SI_IS_MCAST))) &&
  1331. (si->address.af==AF_INET6))
  1332. sendipv6=si;
  1333. #endif
  1334. /* children_no per each socket */
  1335. cfg_register_child((si->workers>0)?si->workers:children_no);
  1336. }
  1337. #ifdef USE_RAW_SOCKS
  1338. /* always try to have a raw socket opened if we are using ipv4 */
  1339. if (sendipv4) {
  1340. raw_udp4_send_sock = raw_socket(IPPROTO_RAW, 0, 0, 1);
  1341. if (raw_udp4_send_sock < 0) {
  1342. if ( default_core_cfg.udp4_raw > 0) {
  1343. /* force use raw socket failed */
  1344. ERR("could not initialize raw udp send socket (ipv4):"
  1345. " %s (%d)\n", strerror(errno), errno);
  1346. if (errno == EPERM)
  1347. ERR("could not initialize raw socket on startup"
  1348. " due to inadequate permissions, please"
  1349. " restart as root or with CAP_NET_RAW\n");
  1350. goto error;
  1351. }
  1352. default_core_cfg.udp4_raw = 0; /* disabled */
  1353. } else {
  1354. register_fds(1);
  1355. if (default_core_cfg.udp4_raw < 0) {
  1356. /* auto-detect => use it */
  1357. default_core_cfg.udp4_raw = 1; /* enabled */
  1358. DBG("raw socket possible => turning it on\n");
  1359. }
  1360. if (default_core_cfg.udp4_raw_ttl < 0) {
  1361. /* auto-detect */
  1362. default_core_cfg.udp4_raw_ttl =
  1363. sock_get_ttl(sendipv4->socket);
  1364. if (default_core_cfg.udp4_raw_ttl < 0)
  1365. /* error, use some default value */
  1366. default_core_cfg.udp4_raw_ttl = 63;
  1367. }
  1368. }
  1369. }
  1370. #else
  1371. default_core_cfg.udp4_raw = 0;
  1372. #endif /* USE_RAW_SOCKS */
  1373. #ifdef USE_SCTP
  1374. if (!sctp_disable){
  1375. for(si=sctp_listen; si; si=si->next){
  1376. if (sctp_init_sock(si)==-1) goto error;
  1377. /* get first ipv4/ipv6 socket*/
  1378. if ((si->address.af==AF_INET) &&
  1379. ((sendipv4_sctp==0) ||
  1380. (sendipv4_sctp->flags&(SI_IS_LO|SI_IS_MCAST))))
  1381. sendipv4_sctp=si;
  1382. #ifdef USE_IPV6
  1383. if( ((sendipv6_sctp==0) ||
  1384. (sendipv6_sctp->flags&(SI_IS_LO|SI_IS_MCAST))) &&
  1385. (si->address.af==AF_INET6))
  1386. sendipv6_sctp=si;
  1387. #endif
  1388. /* sctp_children_no per each socket */
  1389. cfg_register_child((si->workers>0)?si->workers:sctp_children_no);
  1390. }
  1391. }
  1392. #endif /* USE_SCTP */
  1393. #ifdef USE_TCP
  1394. if (!tcp_disable){
  1395. for(si=tcp_listen; si; si=si->next){
  1396. /* same thing for tcp */
  1397. if (tcp_init(si)==-1) goto error;
  1398. /* get first ipv4/ipv6 socket*/
  1399. if ((si->address.af==AF_INET)&&
  1400. ((sendipv4_tcp==0) ||
  1401. (sendipv4_tcp->flags&(SI_IS_LO|SI_IS_MCAST))))
  1402. sendipv4_tcp=si;
  1403. #ifdef USE_IPV6
  1404. if( ((sendipv6_tcp==0) ||
  1405. (sendipv6_tcp->flags&(SI_IS_LO|SI_IS_MCAST))) &&
  1406. (si->address.af==AF_INET6))
  1407. sendipv6_tcp=si;
  1408. #endif
  1409. }
  1410. /* the number of sockets does not matter */
  1411. cfg_register_child(tcp_children_no + 1 /* tcp main */);
  1412. }
  1413. #ifdef USE_TLS
  1414. if (!tls_disable && tls_has_init_si()){
  1415. for(si=tls_listen; si; si=si->next){
  1416. /* same as for tcp*/
  1417. if (tls_init(si)==-1) goto error;
  1418. /* get first ipv4/ipv6 socket*/
  1419. if ((si->address.af==AF_INET)&&
  1420. ((sendipv4_tls==0) ||
  1421. (sendipv4_tls->flags&(SI_IS_LO|SI_IS_MCAST))))
  1422. sendipv4_tls=si;
  1423. #ifdef USE_IPV6
  1424. if( ((sendipv6_tls==0) ||
  1425. (sendipv6_tls->flags&(SI_IS_LO|SI_IS_MCAST))) &&
  1426. (si->address.af==AF_INET6))
  1427. sendipv6_tls=si;
  1428. #endif
  1429. }
  1430. }
  1431. #endif /* USE_TLS */
  1432. #endif /* USE_TCP */
  1433. /* all processes should have access to all the sockets (for
  1434. * sending) so we open all first*/
  1435. if (do_suid()==-1) goto error; /* try to drop privileges */
  1436. /* delay cfg_shmize to the last moment (it must be called _before_
  1437. forking). Changes to default cfgs after this point will be
  1438. ignored (cfg_shmize() will copy the default cfgs into shmem).
  1439. */
  1440. if (cfg_shmize() < 0) {
  1441. LOG(L_CRIT, "could not initialize shared configuration\n");
  1442. goto error;
  1443. }
  1444. /* Temporary set the local configuration of the main process
  1445. * to make the group instances available in PROC_INIT.
  1446. */
  1447. cfg_main_set_local();
  1448. /* init childs with rank==PROC_INIT before forking any process,
  1449. * this is a place for delayed (after mod_init) initializations
  1450. * (e.g. shared vars that depend on the total number of processes
  1451. * that is known only after all mod_inits have been executed )
  1452. * WARNING: the same init_child will be called latter, a second time
  1453. * for the "main" process with rank PROC_MAIN (make sure things are
  1454. * not initialized twice)*/
  1455. if (init_child(PROC_INIT) < 0) {
  1456. LOG(L_ERR, "ERROR: main: error in init_child(PROC_INT) --"
  1457. " exiting\n");
  1458. cfg_main_reset_local();
  1459. goto error;
  1460. }
  1461. cfg_main_reset_local();
  1462. if (counters_prefork_init(get_max_procs()) == -1) goto error;
  1463. /* udp processes */
  1464. for(si=udp_listen; si; si=si->next){
  1465. nrprocs = (si->workers>0)?si->workers:children_no;
  1466. for(i=0;i<nrprocs;i++){
  1467. if(si->address.af==AF_INET6) {
  1468. if(si->useinfo.name.s)
  1469. snprintf(si_desc, MAX_PT_DESC, "udp receiver child=%d "
  1470. "sock=[%s]:%s (%s:%s)",
  1471. i, si->name.s, si->port_no_str.s,
  1472. si->useinfo.name.s, si->useinfo.port_no_str.s);
  1473. else
  1474. snprintf(si_desc, MAX_PT_DESC, "udp receiver child=%d "
  1475. "sock=[%s]:%s",
  1476. i, si->name.s, si->port_no_str.s);
  1477. } else {
  1478. if(si->useinfo.name.s)
  1479. snprintf(si_desc, MAX_PT_DESC, "udp receiver child=%d "
  1480. "sock=%s:%s (%s:%s)",
  1481. i, si->name.s, si->port_no_str.s,
  1482. si->useinfo.name.s, si->useinfo.port_no_str.s);
  1483. else
  1484. snprintf(si_desc, MAX_PT_DESC, "udp receiver child=%d "
  1485. "sock=%s:%s",
  1486. i, si->name.s, si->port_no_str.s);
  1487. }
  1488. child_rank++;
  1489. pid = fork_process(child_rank, si_desc, 1);
  1490. if (pid<0){
  1491. LOG(L_CRIT, "main_loop: Cannot fork\n");
  1492. goto error;
  1493. }else if (pid==0){
  1494. /* child */
  1495. bind_address=si; /* shortcut */
  1496. #ifdef STATS
  1497. setstats( i+r*children_no );
  1498. #endif
  1499. return udp_rcv_loop();
  1500. }
  1501. }
  1502. /*parent*/
  1503. /*close(udp_sock)*/; /*if it's closed=>sendto invalid fd errors?*/
  1504. }
  1505. #ifdef USE_SCTP
  1506. /* sctp processes */
  1507. if (!sctp_disable){
  1508. for(si=sctp_listen; si; si=si->next){
  1509. nrprocs = (si->workers>0)?si->workers:sctp_children_no;
  1510. for(i=0;i<nrprocs;i++){
  1511. if(si->address.af==AF_INET6) {
  1512. snprintf(si_desc, MAX_PT_DESC, "sctp receiver child=%d "
  1513. "sock=[%s]:%s",
  1514. i, si->name.s, si->port_no_str.s);
  1515. } else {
  1516. snprintf(si_desc, MAX_PT_DESC, "sctp receiver child=%d "
  1517. "sock=%s:%s",
  1518. i, si->name.s, si->port_no_str.s);
  1519. }
  1520. child_rank++;
  1521. pid = fork_process(child_rank, si_desc, 1);
  1522. if (pid<0){
  1523. LOG(L_CRIT, "main_loop: Cannot fork\n");
  1524. goto error;
  1525. }else if (pid==0){
  1526. /* child */
  1527. bind_address=si; /* shortcut */
  1528. #ifdef STATS
  1529. setstats( i+r*children_no );
  1530. #endif
  1531. return sctp_rcv_loop();
  1532. }
  1533. }
  1534. /*parent*/
  1535. /*close(sctp_sock)*/; /*if closed=>sendto invalid fd errors?*/
  1536. }
  1537. }
  1538. #endif /* USE_SCTP */
  1539. /*this is the main process*/
  1540. bind_address=0; /* main proc -> it shouldn't send anything, */
  1541. #ifdef USE_SLOW_TIMER
  1542. /* fork again for the "slow" timer process*/
  1543. pid = fork_process(PROC_TIMER, "slow timer", 1);
  1544. if (pid<0){
  1545. LOG(L_CRIT, "main_loop: cannot fork \"slow\" timer process\n");
  1546. goto error;
  1547. }else if (pid==0){
  1548. /* child */
  1549. if (real_time&2)
  1550. set_rt_prio(rt_timer2_prio, rt_timer2_policy);
  1551. if (arm_slow_timer()<0) goto error;
  1552. slow_timer_main();
  1553. }else{
  1554. slow_timer_pid=pid;
  1555. }
  1556. #endif /* USE_SLOW_TIMER */
  1557. /* fork again for the "main" timer process*/
  1558. pid = fork_process(PROC_TIMER, "timer", 1);
  1559. if (pid<0){
  1560. LOG(L_CRIT, "main_loop: cannot fork timer process\n");
  1561. goto error;
  1562. }else if (pid==0){
  1563. /* child */
  1564. if (real_time&1)
  1565. set_rt_prio(rt_timer1_prio, rt_timer1_policy);
  1566. if (arm_timer()<0) goto error;
  1567. timer_main();
  1568. }
  1569. /* init childs with rank==MAIN before starting tcp main (in case they want
  1570. * to fork a tcp capable process, the corresponding tcp. comm. fds in
  1571. * pt[] must be set before calling tcp_main_loop()) */
  1572. if (init_child(PROC_MAIN) < 0) {
  1573. LOG(L_ERR, "ERROR: main: error in init_child\n");
  1574. goto error;
  1575. }
  1576. #ifdef USE_TCP
  1577. if (!tcp_disable){
  1578. /* start tcp & tls receivers */
  1579. if (tcp_init_children()<0) goto error;
  1580. /* start tcp+tls master proc */
  1581. pid = fork_process(PROC_TCP_MAIN, "tcp main process", 0);
  1582. if (pid<0){
  1583. LOG(L_CRIT, "main_loop: cannot fork tcp main process: %s\n",
  1584. strerror(errno));
  1585. goto error;
  1586. }else if (pid==0){
  1587. /* child */
  1588. tcp_main_loop();
  1589. }else{
  1590. tcp_main_pid=pid;
  1591. unix_tcp_sock=-1;
  1592. }
  1593. }
  1594. #endif
  1595. /* main */
  1596. strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
  1597. #ifdef USE_TCP
  1598. close_extra_socks(PROC_ATTENDANT, get_proc_no());
  1599. if(!tcp_disable){
  1600. /* main's tcp sockets are disabled by default from init_pt() */
  1601. unix_tcp_sock=-1;
  1602. }
  1603. #endif
  1604. /* init cfg, but without per child callbacks support */
  1605. cfg_child_no_cb_init();
  1606. cfg_ok=1;
  1607. #ifdef EXTRA_DEBUG
  1608. for (r=0; r<*process_count; r++){
  1609. fprintf(stderr, "% 3d % 5d - %s\n", r, pt[r].pid, pt[r].desc);
  1610. }
  1611. #endif
  1612. DBG("Expect maximum %d open fds\n", get_max_open_fds());
  1613. /* in daemonize mode send the exit code back to the parent process */
  1614. if (!dont_daemonize) {
  1615. if (daemon_status_send(0) < 0) {
  1616. ERR("error sending daemon status: %s [%d]\n",
  1617. strerror(errno), errno);
  1618. goto error;
  1619. }
  1620. }
  1621. for(;;){
  1622. handle_sigs();
  1623. pause();
  1624. cfg_update();
  1625. }
  1626. }
  1627. /*return 0; */
  1628. error:
  1629. /* if we are here, we are the "main process",
  1630. any forked children should exit with exit(-1) and not
  1631. ever use return */
  1632. return -1;
  1633. }
  1634. /*
  1635. * Calculate number of processes, this does not
  1636. * include processes created by modules
  1637. */
  1638. static int calc_proc_no(void)
  1639. {
  1640. int udp_listeners;
  1641. struct socket_info* si;
  1642. #ifdef USE_TCP
  1643. int tcp_listeners;
  1644. int tcp_e_listeners;
  1645. #endif
  1646. #ifdef USE_SCTP
  1647. int sctp_listeners;
  1648. #endif
  1649. for (si=udp_listen, udp_listeners=0; si; si=si->next)
  1650. udp_listeners += (si->workers>0)?si->workers:children_no;
  1651. #ifdef USE_TCP
  1652. for (si=tcp_listen, tcp_listeners=0, tcp_e_listeners=0; si; si=si->next) {
  1653. if(si->workers>0)
  1654. tcp_listeners += si->workers;
  1655. else
  1656. tcp_e_listeners = tcp_cfg_children_no;
  1657. }
  1658. tcp_listeners += tcp_e_listeners;
  1659. tcp_children_no = tcp_listeners;
  1660. #endif
  1661. #ifdef USE_SCTP
  1662. for (si=sctp_listen, sctp_listeners=0; si; si=si->next)
  1663. sctp_listeners += (si->workers>0)?si->workers:sctp_children_no;
  1664. #endif
  1665. return
  1666. /* receivers and attendant */
  1667. (dont_fork ? 1 : udp_listeners + 1)
  1668. /* timer process */
  1669. + 1 /* always, we need it in most cases, and we can't tell here
  1670. & now if we don't need it */
  1671. #ifdef USE_SLOW_TIMER
  1672. + 1 /* slow timer process */
  1673. #endif
  1674. #ifdef USE_TCP
  1675. +((!tcp_disable)?( 1/* tcp main */ + tcp_listeners ):0)
  1676. #endif
  1677. #ifdef USE_SCTP
  1678. +((!sctp_disable)?sctp_listeners:0)
  1679. #endif
  1680. ;
  1681. }
  1682. int main(int argc, char** argv)
  1683. {
  1684. FILE* cfg_stream;
  1685. int c,r;
  1686. char *tmp;
  1687. int tmp_len;
  1688. int port;
  1689. int proto;
  1690. char *options;
  1691. int ret;
  1692. unsigned int seed;
  1693. int rfd;
  1694. int debug_save, debug_flag;
  1695. int dont_fork_cnt;
  1696. struct name_lst* n_lst;
  1697. char *p;
  1698. /*init*/
  1699. time(&up_since);
  1700. creator_pid = getpid();
  1701. ret=-1;
  1702. my_argc=argc; my_argv=argv;
  1703. debug_flag=0;
  1704. dont_fork_cnt=0;
  1705. daemon_status_init();
  1706. /* command line options */
  1707. options= ":f:cm:M:dVIhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
  1708. #ifdef STATS
  1709. "s:"
  1710. #endif
  1711. ;
  1712. /* Handle special command line arguments, that must be treated before
  1713. * intializing the various subsystem or before parsing other arguments:
  1714. * - get the startup debug and log_stderr values
  1715. * - look if pkg mem size is overriden on the command line (-M) and get
  1716. * the new value here (before intializing pkg_mem).
  1717. * - look if there is a -h, e.g. -f -h construction won't be caught
  1718. * later
  1719. */
  1720. opterr = 0;
  1721. while((c=getopt(argc,argv,options))!=-1) {
  1722. switch(c) {
  1723. case 'd':
  1724. debug_flag = 1;
  1725. default_core_cfg.debug++;
  1726. break;
  1727. case 'E':
  1728. log_stderr=1;
  1729. break;
  1730. case 'M':
  1731. pkg_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
  1732. if (tmp &&(*tmp)){
  1733. fprintf(stderr, "bad private mem size number: -M %s\n",
  1734. optarg);
  1735. goto error;
  1736. };
  1737. break;
  1738. default:
  1739. if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) {
  1740. printf("version: %s\n", full_version);
  1741. printf("%s",help_msg);
  1742. exit(0);
  1743. }
  1744. break;
  1745. }
  1746. }
  1747. /*init pkg mallocs (before parsing cfg or the rest of the cmd line !)*/
  1748. if (pkg_mem_size)
  1749. LOG(L_INFO, " private (per process) memory: %ld bytes\n",
  1750. pkg_mem_size );
  1751. if (init_pkg_mallocs()==-1)
  1752. goto error;
  1753. #ifdef DBG_MSG_QA
  1754. fprintf(stderr, "WARNING: ser startup: "
  1755. "DBG_MSG_QA enabled, ser may exit abruptly\n");
  1756. #endif
  1757. /* init counters / stats */
  1758. if (init_counters() == -1)
  1759. goto error;
  1760. #ifdef USE_TCP
  1761. init_tcp_options(); /* set the defaults before the config */
  1762. #endif
  1763. #ifdef USE_SCTP
  1764. init_sctp_options(); /* set defaults before the config */
  1765. #endif
  1766. /* process command line (cfg. file path etc) */
  1767. optind = 1; /* reset getopt */
  1768. /* switches required before script processing */
  1769. while((c=getopt(argc,argv,options))!=-1) {
  1770. switch(c) {
  1771. case 'f':
  1772. cfg_file=optarg;
  1773. break;
  1774. case 'c':
  1775. config_check=1;
  1776. log_stderr=1; /* force stderr logging */
  1777. break;
  1778. case 'L':
  1779. mods_dir = optarg;
  1780. break;
  1781. case 'm':
  1782. shm_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
  1783. if (tmp &&(*tmp)){
  1784. fprintf(stderr, "bad shmem size number: -m %s\n",
  1785. optarg);
  1786. goto error;
  1787. };
  1788. LOG(L_INFO, "ser: shared memory: %ld bytes\n",
  1789. shm_mem_size );
  1790. break;
  1791. case 'M':
  1792. /* ignore it, it was parsed immediately after startup,
  1793. the pkg mem. is already initialized at this point */
  1794. break;
  1795. case 'd':
  1796. /* ignore it, was parsed immediately after startup */
  1797. break;
  1798. case 'V':
  1799. printf("version: %s\n", full_version);
  1800. printf("flags: %s\n", ver_flags );
  1801. print_ct_constants();
  1802. #ifdef USE_SCTP
  1803. tmp=malloc(256);
  1804. if (tmp && (sctp_check_compiled_sockopts(tmp, 256)!=0))
  1805. printf("sctp unsupported socket options: %s\n", tmp);
  1806. if (tmp) free(tmp);
  1807. #endif
  1808. printf("id: %s\n", ver_id);
  1809. printf("compiled on %s with %s\n",
  1810. ver_compiled_time, ver_compiler );
  1811. exit(0);
  1812. break;
  1813. case 'I':
  1814. print_internals();
  1815. exit(0);
  1816. break;
  1817. case 'E':
  1818. /* ignore it, was parsed immediately after startup */
  1819. break;
  1820. case 'O':
  1821. scr_opt_lev=strtol(optarg, &tmp, 10);
  1822. if (tmp &&(*tmp)){
  1823. fprintf(stderr, "bad optimization level: -O %s\n",
  1824. optarg);
  1825. goto error;
  1826. };
  1827. break;
  1828. case 'u':
  1829. /* user needed for possible shm. pre-init */
  1830. user=optarg;
  1831. break;
  1832. case 'A':
  1833. p = strchr(optarg, '=');
  1834. if(p) {
  1835. *p = '\0';
  1836. }
  1837. pp_define_set_type(0);
  1838. if(pp_define(strlen(optarg), optarg)<0) {
  1839. fprintf(stderr, "error at define param: -A %s\n",
  1840. optarg);
  1841. goto error;
  1842. }
  1843. if(p) {
  1844. *p = '=';
  1845. p++;
  1846. if(pp_define_set(strlen(p), p)<0) {
  1847. fprintf(stderr, "error at define value: -A %s\n",
  1848. optarg);
  1849. goto error;
  1850. }
  1851. }
  1852. break;
  1853. case 'b':
  1854. case 'l':
  1855. case 'n':
  1856. case 'v':
  1857. case 'r':
  1858. case 'R':
  1859. case 'D':
  1860. case 'T':
  1861. case 'N':
  1862. case 'W':
  1863. case 'w':
  1864. case 't':
  1865. case 'g':
  1866. case 'P':
  1867. case 'G':
  1868. case 'S':
  1869. case 'Q':
  1870. case 'a':
  1871. case 's':
  1872. break;
  1873. case '?':
  1874. if (isprint(optopt)) {
  1875. fprintf(stderr, "Unknown option `-%c'."
  1876. " Use -h for help.\n", optopt);
  1877. } else {
  1878. fprintf(stderr, "Unknown option character `\\x%x'."
  1879. " Use -h for help.\n",
  1880. optopt);
  1881. }
  1882. goto error;
  1883. case ':':
  1884. fprintf(stderr, "Option `-%c' requires an argument."
  1885. " Use -h for help.\n",
  1886. optopt);
  1887. goto error;
  1888. default:
  1889. abort();
  1890. }
  1891. }
  1892. if (endianness_sanity_check() != 0){
  1893. fprintf(stderr, "BUG: endianness sanity tests failed\n");
  1894. goto error;
  1895. }
  1896. if (init_routes()<0) goto error;
  1897. if (init_nonsip_hooks()<0) goto error;
  1898. if (init_script_cb()<0) goto error;
  1899. if (pv_init_api()<0) goto error;
  1900. if (pv_register_core_vars()!=0) goto error;
  1901. if (init_rpcs()<0) goto error;
  1902. if (register_core_rpcs()!=0) goto error;
  1903. /* Fix the value of cfg_file variable.*/
  1904. if (fix_cfg_file() < 0) goto error;
  1905. /* load config file or die */
  1906. cfg_stream=fopen (cfg_file, "r");
  1907. if (cfg_stream==0){
  1908. fprintf(stderr, "ERROR: loading config file(%s): %s\n", cfg_file,
  1909. strerror(errno));
  1910. goto error;
  1911. }
  1912. /* seed the prng */
  1913. /* try to use /dev/urandom if possible */
  1914. seed=0;
  1915. if ((rfd=open("/dev/urandom", O_RDONLY))!=-1){
  1916. try_again:
  1917. if (read(rfd, (void*)&seed, sizeof(seed))==-1){
  1918. if (errno==EINTR) goto try_again; /* interrupted by signal */
  1919. LOG(L_WARN, "WARNING: could not read from /dev/urandom (%d)\n",
  1920. errno);
  1921. }
  1922. DBG("read %u from /dev/urandom\n", seed);
  1923. close(rfd);
  1924. }else{
  1925. LOG(L_WARN, "WARNING: could not open /dev/urandom (%d)\n", errno);
  1926. }
  1927. seed+=getpid()+time(0);
  1928. DBG("seeding PRNG with %u\n", seed);
  1929. srand(seed);
  1930. fastrand_seed(rand());
  1931. srandom(rand()+time(0));
  1932. DBG("test random numbers %u %lu %u\n", rand(), random(), fastrand());
  1933. /*register builtin modules*/
  1934. register_builtin_modules();
  1935. /* init named flags */
  1936. init_named_flags();
  1937. yyin=cfg_stream;
  1938. debug_save = default_core_cfg.debug;
  1939. if ((yyparse()!=0)||(cfg_errors)){
  1940. fprintf(stderr, "ERROR: bad config file (%d errors)\n", cfg_errors);
  1941. goto error;
  1942. }
  1943. if (cfg_warnings){
  1944. fprintf(stderr, "%d config warnings\n", cfg_warnings);
  1945. }
  1946. if (debug_flag) default_core_cfg.debug = debug_save;
  1947. print_rls();
  1948. /* options with higher priority than cfg file */
  1949. optind = 1; /* reset getopt */
  1950. while((c=getopt(argc,argv,options))!=-1) {
  1951. switch(c) {
  1952. case 'f':
  1953. case 'c':
  1954. case 'm':
  1955. case 'M':
  1956. case 'd':
  1957. case 'V':
  1958. case 'I':
  1959. case 'h':
  1960. case 'O':
  1961. case 'A':
  1962. break;
  1963. case 'E':
  1964. log_stderr=1; /* use in both getopt switches,
  1965. takes priority over config */
  1966. break;
  1967. case 'b':
  1968. maxbuffer=strtol(optarg, &tmp, 10);
  1969. if (tmp &&(*tmp)){
  1970. fprintf(stderr, "bad max buffer size number: -b %s\n",
  1971. optarg);
  1972. goto error;
  1973. }
  1974. break;
  1975. case 'l':
  1976. if ((n_lst=parse_phostport_mh(optarg, &tmp, &tmp_len,
  1977. &port, &proto))==0){
  1978. fprintf(stderr, "bad -l address specifier: %s\n",
  1979. optarg);
  1980. goto error;
  1981. }
  1982. /* add a new addr. to our address list */
  1983. if (add_listen_iface(n_lst->name, n_lst->next, port,
  1984. proto, n_lst->flags)!=0){
  1985. fprintf(stderr, "failed to add new listen address\n");
  1986. free_name_lst(n_lst);
  1987. goto error;
  1988. }
  1989. free_name_lst(n_lst);
  1990. break;
  1991. case 'n':
  1992. children_no=strtol(optarg, &tmp, 10);
  1993. if ((tmp==0) ||(*tmp)){
  1994. fprintf(stderr, "bad process number: -n %s\n",
  1995. optarg);
  1996. goto error;
  1997. }
  1998. break;
  1999. case 'v':
  2000. check_via=1;
  2001. break;
  2002. case 'r':
  2003. received_dns|=DO_DNS;
  2004. break;
  2005. case 'R':
  2006. received_dns|=DO_REV_DNS;
  2007. break;
  2008. case 'D':
  2009. dont_fork_cnt++;
  2010. break;
  2011. case 'T':
  2012. #ifdef USE_TCP
  2013. tcp_disable=1;
  2014. #else
  2015. fprintf(stderr,"WARNING: tcp support not compiled in\n");
  2016. #endif
  2017. break;
  2018. case 'N':
  2019. #ifdef USE_TCP
  2020. tcp_cfg_children_no=strtol(optarg, &tmp, 10);
  2021. if ((tmp==0) ||(*tmp)){
  2022. fprintf(stderr, "bad process number: -N %s\n",
  2023. optarg);
  2024. goto error;
  2025. }
  2026. #else
  2027. fprintf(stderr,"WARNING: tcp support not compiled in\n");
  2028. #endif
  2029. break;
  2030. case 'W':
  2031. #ifdef USE_TCP
  2032. tcp_poll_method=get_poll_type(optarg);
  2033. if (tcp_poll_method==POLL_NONE){
  2034. fprintf(stderr, "bad poll method name: -W %s\ntry "
  2035. "one of %s.\n", optarg, poll_support);
  2036. goto error;
  2037. }
  2038. #else
  2039. fprintf(stderr,"WARNING: tcp support not compiled in\n");
  2040. #endif
  2041. break;
  2042. case 'S':
  2043. #ifdef USE_SCTP
  2044. sctp_disable=1;
  2045. #else
  2046. fprintf(stderr,"WARNING: sctp support not compiled in\n");
  2047. #endif
  2048. break;
  2049. case 'Q':
  2050. #ifdef USE_SCTP
  2051. sctp_children_no=strtol(optarg, &tmp, 10);
  2052. if ((tmp==0) ||(*tmp)){
  2053. fprintf(stderr, "bad process number: -O %s\n",
  2054. optarg);
  2055. goto error;
  2056. }
  2057. #else
  2058. fprintf(stderr,"WARNING: sctp support not compiled in\n");
  2059. #endif
  2060. break;
  2061. case 'w':
  2062. working_dir=optarg;
  2063. break;
  2064. case 't':
  2065. chroot_dir=optarg;
  2066. break;
  2067. case 'u':
  2068. user=optarg;
  2069. break;
  2070. case 'g':
  2071. group=optarg;
  2072. break;
  2073. case 'P':
  2074. pid_file=optarg;
  2075. break;
  2076. case 'G':
  2077. pgid_file=optarg;
  2078. break;
  2079. case 'a':
  2080. if(strcmp(optarg, "on")==0 || strcmp(optarg, "yes")==0)
  2081. sr_auto_aliases = 1;
  2082. else if(strcmp(optarg, "off")==0 || strcmp(optarg, "no")==0)
  2083. sr_auto_aliases = 0;
  2084. else {
  2085. fprintf(stderr,
  2086. "bad auto aliases parameter: %s (valid on, off, yes, no)\n",
  2087. optarg);
  2088. goto error;
  2089. }
  2090. break;
  2091. case 's':
  2092. #ifdef STATS
  2093. stat_file=optarg;
  2094. #endif
  2095. break;
  2096. default:
  2097. break;
  2098. }
  2099. }
  2100. /* reinit if pv buffer size has been set in config */
  2101. if (pv_reinit_buffer()<0)
  2102. goto error;
  2103. if (dont_fork_cnt)
  2104. dont_fork = dont_fork_cnt; /* override by command line */
  2105. if (dont_fork > 0) {
  2106. dont_daemonize = dont_fork == 2;
  2107. dont_fork = dont_fork == 1;
  2108. }
  2109. /* init locks first */
  2110. if (init_lock_ops()!=0)
  2111. goto error;
  2112. #ifdef USE_TCP
  2113. #ifdef USE_TLS
  2114. if (tcp_disable)
  2115. tls_disable=1; /* if no tcp => no tls */
  2116. #endif /* USE_TLS */
  2117. #endif /* USE_TCP */
  2118. #ifdef USE_SCTP
  2119. if (sctp_disable!=1){
  2120. /* fix it */
  2121. if (sctp_check_support()==-1){
  2122. /* check if sctp support is auto, if not warn about disabling it */
  2123. if (sctp_disable!=2){
  2124. fprintf(stderr, "ERROR: " "sctp enabled, but not supported by"
  2125. " the OS\n");
  2126. goto error;
  2127. }
  2128. sctp_disable=1;
  2129. }else{
  2130. /* sctp_disable!=1 and sctp supported => enable sctp */
  2131. sctp_disable=0;
  2132. }
  2133. }
  2134. #endif /* USE_SCTP */
  2135. /* initialize the configured proto list */
  2136. init_proto_order();
  2137. /* init the resolver, before fixing the config */
  2138. resolv_init();
  2139. /* fix parameters */
  2140. if (port_no<=0) port_no=SIP_PORT;
  2141. #ifdef USE_TLS
  2142. if (tls_port_no<=0) tls_port_no=SIPS_PORT;
  2143. #endif
  2144. if (children_no<=0) children_no=CHILD_NO;
  2145. #ifdef USE_TCP
  2146. if (!tcp_disable){
  2147. if (tcp_cfg_children_no<=0) tcp_cfg_children_no=children_no;
  2148. tcp_children_no = tcp_cfg_children_no;
  2149. }
  2150. #endif
  2151. #ifdef USE_SCTP
  2152. if (!sctp_disable){
  2153. if (sctp_children_no<=0) sctp_children_no=children_no;
  2154. }
  2155. #endif
  2156. if (working_dir==0) working_dir="/";
  2157. /* get uid/gid */
  2158. if (user){
  2159. if (user2uid(&uid, &gid, user)<0){
  2160. fprintf(stderr, "bad user name/uid number: -u %s\n", user);
  2161. goto error;
  2162. }
  2163. }
  2164. if (group){
  2165. if (group2gid(&gid, group)<0){
  2166. fprintf(stderr, "bad group name/gid number: -u %s\n", group);
  2167. goto error;
  2168. }
  2169. }
  2170. if (fix_all_socket_lists()!=0){
  2171. fprintf(stderr, "failed to initialize list addresses\n");
  2172. goto error;
  2173. }
  2174. if (default_core_cfg.dns_try_ipv6 && !(socket_types & SOCKET_T_IPV6)){
  2175. /* if we are not listening on any ipv6 address => no point
  2176. * to try to resovle ipv6 addresses */
  2177. default_core_cfg.dns_try_ipv6=0;
  2178. }
  2179. /* print all the listen addresses */
  2180. printf("Listening on \n");
  2181. print_all_socket_lists();
  2182. printf("Aliases: \n");
  2183. /*print_aliases();*/
  2184. print_aliases();
  2185. printf("\n");
  2186. if (dont_fork){
  2187. fprintf(stderr, "WARNING: no fork mode %s\n",
  2188. (udp_listen)?(
  2189. (udp_listen->next)?"and more than one listen address found "
  2190. "(will use only the first one)":""
  2191. ):"and no udp listen address found" );
  2192. }
  2193. if (config_check){
  2194. fprintf(stderr, "config file ok, exiting...\n");
  2195. return 0;
  2196. }
  2197. /*init shm mallocs
  2198. * this must be here
  2199. * -to allow setting shm mem size from the command line
  2200. * => if shm_mem should be settable from the cfg file move
  2201. * everything after
  2202. * -it must be also before init_timer and init_tcp
  2203. * -it must be after we know uid (so that in the SYSV sems case,
  2204. * the sems will have the correct euid)
  2205. * Note: shm can now be initialized when parsing the config script, that's
  2206. * why checking for a prior initialization is needed.
  2207. * --andrei */
  2208. #ifdef SHM_MEM
  2209. if (!shm_initialized() && init_shm()<0)
  2210. goto error;
  2211. #endif /* SHM_MEM */
  2212. if (init_atomic_ops()==-1)
  2213. goto error;
  2214. if (init_basex() != 0){
  2215. LOG(L_CRIT, "could not initialize base* framework\n");
  2216. goto error;
  2217. }
  2218. if (sr_cfg_init() < 0) {
  2219. LOG(L_CRIT, "could not initialize configuration framework\n");
  2220. goto error;
  2221. }
  2222. /* declare the core cfg before the module configs */
  2223. if (cfg_declare("core", core_cfg_def, &default_core_cfg, cfg_sizeof(core),
  2224. &core_cfg)
  2225. ) {
  2226. LOG(L_CRIT, "could not declare the core configuration\n");
  2227. goto error;
  2228. }
  2229. #ifdef USE_TCP
  2230. if (tcp_register_cfg()){
  2231. LOG(L_CRIT, "could not register the tcp configuration\n");
  2232. goto error;
  2233. }
  2234. #endif /* USE_TCP */
  2235. #ifdef USE_SCTP
  2236. if (sctp_register_cfg()){
  2237. LOG(L_CRIT, "could not register the sctp configuration\n");
  2238. goto error;
  2239. }
  2240. #endif /* USE_SCTP */
  2241. /*init timer, before parsing the cfg!*/
  2242. if (init_timer()<0){
  2243. LOG(L_CRIT, "could not initialize timer, exiting...\n");
  2244. goto error;
  2245. }
  2246. #ifdef USE_DNS_CACHE
  2247. if (init_dns_cache()<0){
  2248. LOG(L_CRIT, "could not initialize the dns cache, exiting...\n");
  2249. goto error;
  2250. }
  2251. #ifdef USE_DNS_CACHE_STATS
  2252. /* preinitializing before the nubmer of processes is determined */
  2253. if (init_dns_cache_stats(1)<0){
  2254. LOG(L_CRIT, "could not initialize the dns cache measurement\n");
  2255. goto error;
  2256. }
  2257. #endif /* USE_DNS_CACHE_STATS */
  2258. #endif
  2259. #ifdef USE_DST_BLACKLIST
  2260. if (init_dst_blacklist()<0){
  2261. LOG(L_CRIT, "could not initialize the dst blacklist, exiting...\n");
  2262. goto error;
  2263. }
  2264. #ifdef USE_DST_BLACKLIST_STATS
  2265. /* preinitializing before the number of processes is determined */
  2266. if (init_dst_blacklist_stats(1)<0){
  2267. LOG(L_CRIT, "could not initialize the dst blacklist measurement\n");
  2268. goto error;
  2269. }
  2270. #endif /* USE_DST_BLACKLIST_STATS */
  2271. #endif
  2272. if (init_avps()<0) goto error;
  2273. if (rpc_init_time() < 0) goto error;
  2274. #ifdef USE_TCP
  2275. if (!tcp_disable){
  2276. /*init tcp*/
  2277. if (init_tcp()<0){
  2278. LOG(L_CRIT, "could not initialize tcp, exiting...\n");
  2279. goto error;
  2280. }
  2281. }
  2282. #endif /* USE_TCP */
  2283. #ifdef USE_SCTP
  2284. if (!sctp_disable){
  2285. if (init_sctp()<0){
  2286. LOG(L_CRIT, "Could not initialize sctp, exiting...\n");
  2287. goto error;
  2288. }
  2289. }
  2290. #endif /* USE_SCTP */
  2291. /* init_daemon? */
  2292. if( !dont_fork && daemonize((log_name==0)?argv[0]:log_name, 1) < 0)
  2293. goto error;
  2294. if (install_sigs() != 0){
  2295. fprintf(stderr, "ERROR: could not install the signal handlers\n");
  2296. goto error;
  2297. }
  2298. if (disable_core_dump) set_core_dump(0, 0);
  2299. else set_core_dump(1, shm_mem_size+pkg_mem_size+4*1024*1024);
  2300. if (open_files_limit>0){
  2301. if(increase_open_fds(open_files_limit)<0){
  2302. fprintf(stderr, "ERROR: error could not increase file limits\n");
  2303. goto error;
  2304. }
  2305. }
  2306. if (mlock_pages)
  2307. mem_lock_pages();
  2308. if (real_time&4)
  2309. set_rt_prio(rt_prio, rt_policy);
  2310. if (init_modules() != 0) {
  2311. fprintf(stderr, "ERROR: error while initializing modules\n");
  2312. goto error;
  2313. }
  2314. /* initialize process_table, add core process no. (calc_proc_no()) to the
  2315. * processes registered from the modules*/
  2316. if (init_pt(calc_proc_no())==-1)
  2317. goto error;
  2318. #ifdef USE_TCP
  2319. #ifdef USE_TLS
  2320. if (!tls_disable){
  2321. if (!tls_loaded()){
  2322. LOG(L_WARN, "WARNING: tls support enabled, but no tls engine "
  2323. " available (forgot to load the tls module?)\n");
  2324. LOG(L_WARN, "WARNING: disabling tls...\n");
  2325. tls_disable=1;
  2326. }
  2327. /* init tls*/
  2328. if (init_tls()<0){
  2329. LOG(L_CRIT, "could not initialize tls, exiting...\n");
  2330. goto error;
  2331. }
  2332. }
  2333. #endif /* USE_TLS */
  2334. #endif /* USE_TCP */
  2335. /* The total number of processes is now known, note that no
  2336. * function being called before this point may rely on the
  2337. * number of processes !
  2338. */
  2339. DBG("Expect (at least) %d SER processes in your process list\n",
  2340. get_max_procs());
  2341. #if defined USE_DNS_CACHE && defined USE_DNS_CACHE_STATS
  2342. if (init_dns_cache_stats(get_max_procs())<0){
  2343. LOG(L_CRIT, "could not initialize the dns cache measurement\n");
  2344. goto error;
  2345. }
  2346. #endif
  2347. #if defined USE_DST_BLACKLIST && defined USE_DST_BLACKLIST_STATS
  2348. if (init_dst_blacklist_stats(get_max_procs())<0){
  2349. LOG(L_CRIT, "could not initialize the dst blacklist measurement\n");
  2350. goto error;
  2351. }
  2352. #endif
  2353. /* fix routing lists */
  2354. if ( (r=fix_rls())!=0){
  2355. fprintf(stderr, "ERROR: error %d while trying to fix configuration\n",
  2356. r);
  2357. goto error;
  2358. };
  2359. fixup_complete=1;
  2360. #ifdef STATS
  2361. if (init_stats( dont_fork ? 1 : children_no )==-1) goto error;
  2362. #endif
  2363. ret=main_loop();
  2364. if (ret < 0)
  2365. goto error;
  2366. /*kill everything*/
  2367. if (is_main) shutdown_children(SIGTERM, 0);
  2368. if (!dont_daemonize) {
  2369. if (daemon_status_send(0) < 0)
  2370. ERR("error sending exit status: %s [%d]\n",
  2371. strerror(errno), errno);
  2372. }
  2373. /* else terminate process */
  2374. return ret;
  2375. error:
  2376. /*kill everything*/
  2377. if (is_main) shutdown_children(SIGTERM, 0);
  2378. if (!dont_daemonize) {
  2379. if (daemon_status_send((char)-1) < 0)
  2380. ERR("error sending exit status: %s [%d]\n",
  2381. strerror(errno), errno);
  2382. }
  2383. return -1;
  2384. }