main.c 71 KB

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