main.c 71 KB

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