main.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 Fhg Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <errno.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <netdb.h>
  33. #include <unistd.h>
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37. #include <sys/utsname.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/mman.h>
  41. #include <fcntl.h>
  42. #include <sys/time.h>
  43. #include <sys/wait.h>
  44. #include <pwd.h>
  45. #include <grp.h>
  46. #include <signal.h>
  47. #include <sys/ioctl.h>
  48. #include <net/if.h>
  49. #ifdef HAVE_SYS_SOCKIO_H
  50. #include <sys/sockio.h>
  51. #endif
  52. #include "config.h"
  53. #include "dprint.h"
  54. #include "route.h"
  55. #include "udp_server.h"
  56. #include "globals.h"
  57. #include "mem/mem.h"
  58. #ifdef SHM_MEM
  59. #include "mem/shm_mem.h"
  60. #endif
  61. #include "sr_module.h"
  62. #include "timer.h"
  63. #include "parser/msg_parser.h"
  64. #include "ip_addr.h"
  65. #include "resolve.h"
  66. #include "parser/parse_hname2.h"
  67. #include "parser/digest/digest_parser.h"
  68. #include "fifo_server.h"
  69. #include "name_alias.h"
  70. #include "hash_func.h"
  71. #include "pt.h"
  72. #ifdef USE_TCP
  73. #include "tcp_init.h"
  74. #endif
  75. #include "stats.h"
  76. #ifdef DEBUG_DMALLOC
  77. #include <dmalloc.h>
  78. #endif
  79. static char id[]="@(#) $Id$";
  80. static char version[]= NAME " " VERSION " (" ARCH "/" OS ")" ;
  81. static char compiled[]= __TIME__ " " __DATE__ ;
  82. static char flags[]=
  83. "STATS:"
  84. #ifdef STATS
  85. "On"
  86. #else
  87. "Off"
  88. #endif
  89. #ifdef USE_IPV6
  90. ", USE_IPV6"
  91. #endif
  92. #ifdef USE_TCP
  93. ", USE_TCP"
  94. #endif
  95. #ifdef NO_DEBUG
  96. ", NO_DEBUG"
  97. #endif
  98. #ifdef NO_LOG
  99. ", NO_LOG"
  100. #endif
  101. #ifdef EXTRA_DEBUG
  102. ", EXTRA_DEBUG"
  103. #endif
  104. #ifdef DNS_IP_HACK
  105. ", DNS_IP_HACK"
  106. #endif
  107. #ifdef SHM_MEM
  108. ", SHM_MEM"
  109. #endif
  110. #ifdef SHM_MMAP
  111. ", SHM_MMAP"
  112. #endif
  113. #ifdef PKG_MALLOC
  114. ", PKG_MALLOC"
  115. #endif
  116. #ifdef VQ_MALLOC
  117. ", VQ_MALLOC"
  118. #endif
  119. #ifdef F_MALLOC
  120. ", F_MALLOC"
  121. #endif
  122. #ifdef USE_SHM_MEM
  123. ", USE_SHM_MEM"
  124. #endif
  125. #ifdef DBG_QM_MALLOC
  126. ", DBG_QM_MALLOC"
  127. #endif
  128. #ifdef DEBUG_DMALLOC
  129. ", DEBUG_DMALLOC"
  130. #endif
  131. #ifdef FAST_LOCK
  132. ", FAST_LOCK"
  133. #ifdef BUSY_WAIT
  134. "-BUSY_WAIT"
  135. #endif
  136. #ifdef USE_PTHREAD_MUTEX
  137. ", USE_PTHREAD_MUTEX"
  138. #endif
  139. #ifdef USE_POSIX_SEM
  140. ", USE_POSIX_SEM"
  141. #endif
  142. #ifdef USE_SYSV_SEM
  143. ", USE_SYSV_SEM"
  144. #endif
  145. #ifdef ADAPTIVE_WAIT
  146. "-ADAPTIVE_WAIT"
  147. #endif
  148. #ifdef NOSMP
  149. "-NOSMP"
  150. #endif
  151. #endif /*FAST_LOCK*/
  152. ;
  153. static char help_msg[]= "\
  154. Usage: " NAME " -l address [-p port] [-l address [-p port]...] [options]\n\
  155. Options:\n\
  156. -f file Configuration file (default " CFG_FILE ")\n\
  157. -p port Listen on the specified port (default: 5060)\n\
  158. applies to the last address in -l and to all \n\
  159. following that do not have a corespponding -p\n\
  160. -l address Listen on the specified address (multiple -l mean\n\
  161. listening on more addresses). The default behaviour\n\
  162. is to listen on the addresses returned by uname(2)\n\
  163. \n\
  164. -n processes Number of child processes to fork per interface\n\
  165. (default: 8)\n\
  166. \n\
  167. -r Use dns to check if is necessary to add a \"received=\"\n\
  168. field to a via\n\
  169. -R Same as `-r` but use reverse dns;\n\
  170. (to use both use `-rR`)\n\
  171. \n\
  172. -v Turn on \"via:\" host checking when forwarding replies\n\
  173. -d Debugging mode (multiple -d increase the level)\n\
  174. -D Do not fork into daemon mode\n\
  175. -E Log to stderr\n\
  176. -V Version number\n\
  177. -h This help message\n\
  178. -b nr Maximum receive buffer size which will not be exceeded by\n\
  179. auto-probing procedure even if OS allows\n\
  180. -m nr Size of shared memory allocated in Megabytes\n\
  181. -w dir change the working directory to \"dir\" (default \"/\")\n\
  182. -t dir chroot to \"dir\"\n\
  183. -u uid change uid \n\
  184. -g gid change gid \n\
  185. -P file create a pid file\n\
  186. -i fifo_path create a fifo (usefull for monitoring " NAME ") \n"
  187. #ifdef STATS
  188. " -s file File to which statistics is dumped (disabled otherwise)\n"
  189. #endif
  190. ;
  191. /* print compile-time constants */
  192. void print_ct_constants()
  193. {
  194. #ifdef ADAPTIVE_WAIT
  195. printf("ADAPTIVE_WAIT_LOOPS=%d, ", ADAPTIVE_WAIT_LOOPS);
  196. #endif
  197. /*
  198. #ifdef SHM_MEM
  199. printf("SHM_MEM_SIZE=%d, ", SHM_MEM_SIZE);
  200. #endif
  201. */
  202. printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
  203. " MAX_URI_SIZE %d, BUF_SIZE %d\n",
  204. MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
  205. BUF_SIZE );
  206. }
  207. /* debuging function */
  208. /*
  209. void receive_stdin_loop()
  210. {
  211. #define BSIZE 1024
  212. char buf[BSIZE+1];
  213. int len;
  214. while(1){
  215. len=fread(buf,1,BSIZE,stdin);
  216. buf[len+1]=0;
  217. receive_msg(buf, len);
  218. printf("-------------------------\n");
  219. }
  220. }
  221. */
  222. /* global vars */
  223. char* cfg_file = 0;
  224. unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
  225. not want to exceed durig the
  226. auto-probing procedure; may
  227. be re-configured */
  228. int children_no = 0; /* number of children processing requests */
  229. #ifdef USE_TCP
  230. int tcp_children_no = 0;
  231. #endif
  232. struct process_table *pt=0; /*array with childrens pids, 0= main proc,
  233. alloc'ed in shared mem if possible*/
  234. int sig_flag = 0; /* last signal received */
  235. int debug = 0;
  236. int dont_fork = 0;
  237. int log_stderr = 0;
  238. /* check if reply first via host==us */
  239. int check_via = 0;
  240. /* shall use stateful synonym branches? faster but not reboot-safe */
  241. int syn_branch = 1;
  242. /* debugging level for memory stats */
  243. int memlog = L_DBG;
  244. /* should replies include extensive warnings? by default yes,
  245. good for trouble-shooting
  246. */
  247. int sip_warning = 1;
  248. /* should localy-generated messages include server's signature?
  249. be default yes, good for trouble-shooting
  250. */
  251. int server_signature=1;
  252. /* use dns and/or rdns or to see if we need to add
  253. a ;received=x.x.x.x to via: */
  254. int received_dns = 0;
  255. char* working_dir = 0;
  256. char* chroot_dir = 0;
  257. char* user=0;
  258. char* group=0;
  259. int uid = 0;
  260. int gid = 0;
  261. /* a hint to reply modules whether they should send reply
  262. to IP advertised in Via or IP from which a request came
  263. */
  264. int reply_to_via=0;
  265. #if 0
  266. char* names[MAX_LISTEN]; /* our names */
  267. int names_len[MAX_LISTEN]; /* lengths of the names*/
  268. struct ip_addr addresses[MAX_LISTEN]; /* our ips */
  269. int addresses_no=0; /* number of names/ips */
  270. #endif
  271. struct socket_info sock_info[MAX_LISTEN];/*all addresses we listen/send from*/
  272. #ifdef USE_TCP
  273. struct socket_info tcp_info[MAX_LISTEN];/*all tcp addresses we listen on*/
  274. #endif
  275. int sock_no=0; /* number of addresses/open sockets*/
  276. struct socket_info* bind_address=0; /* pointer to the crt. proc.
  277. listening address*/
  278. int bind_idx; /* same as above but index in the bound[] array */
  279. struct socket_info* sendipv4; /* ipv4 socket to use when msg. comes from ipv6*/
  280. struct socket_info* sendipv6; /* same as above for ipv6 */
  281. #ifdef USE_TCP
  282. struct socket_info* sendipv4_tcp;
  283. struct socket_info* sendipv6_tcp;
  284. #endif
  285. unsigned short port_no=0; /* default port*/
  286. struct host_alias* aliases=0; /* name aliases list */
  287. /* ipc related globals */
  288. int process_no = 0;
  289. /* process_bm_t process_bit = 0; */
  290. #ifdef ROUTE_SRV
  291. #endif
  292. /* cfg parsing */
  293. int cfg_errors=0;
  294. /* shared memory (in MB) */
  295. unsigned int shm_mem_size=SHM_MEM_SIZE * 1024 * 1024;
  296. #define MAX_FD 32 /* maximum number of inherited open file descriptors,
  297. (normally it shouldn't be bigger than 3) */
  298. extern FILE* yyin;
  299. extern int yyparse();
  300. int is_main=0; /* flag = is this the "main" process? */
  301. char* pid_file = 0; /* filename as asked by use */
  302. /* daemon init, return 0 on success, -1 on error */
  303. int daemonize(char* name)
  304. {
  305. FILE *pid_stream;
  306. pid_t pid;
  307. int r, p;
  308. p=-1;
  309. if (chroot_dir&&(chroot(chroot_dir)<0)){
  310. LOG(L_CRIT, "Cannot chroot to %s: %s\n", chroot_dir, strerror(errno));
  311. goto error;
  312. }
  313. if (chdir(working_dir)<0){
  314. LOG(L_CRIT,"cannot chdir to %s: %s\n", working_dir, strerror(errno));
  315. goto error;
  316. }
  317. if (gid&&(setgid(gid)<0)){
  318. LOG(L_CRIT, "cannot change gid to %d: %s\n", gid, strerror(errno));
  319. goto error;
  320. }
  321. if(uid&&(setuid(uid)<0)){
  322. LOG(L_CRIT, "cannot change uid to %d: %s\n", uid, strerror(errno));
  323. goto error;
  324. }
  325. /* fork to become!= group leader*/
  326. if ((pid=fork())<0){
  327. LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));
  328. goto error;
  329. }else if (pid!=0){
  330. /* parent process => exit*/
  331. exit(0);
  332. }
  333. /* become session leader to drop the ctrl. terminal */
  334. if (setsid()<0){
  335. LOG(L_WARN, "setsid failed: %s\n",strerror(errno));
  336. }
  337. /* fork again to drop group leadership */
  338. if ((pid=fork())<0){
  339. LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));
  340. goto error;
  341. }else if (pid!=0){
  342. /*parent process => exit */
  343. exit(0);
  344. }
  345. /* added by noh: create a pid file for the main process */
  346. if (pid_file!=0){
  347. if ((pid_stream=fopen(pid_file, "r"))!=NULL){
  348. fscanf(pid_stream, "%d", &p);
  349. fclose(pid_stream);
  350. if (p==-1){
  351. LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"
  352. " pid number\n", pid_file);
  353. goto error;
  354. }
  355. if (kill((pid_t)p, 0)==0 || errno==EPERM){
  356. LOG(L_CRIT, "running process found in the pid file %s\n",
  357. pid_file);
  358. goto error;
  359. }else{
  360. LOG(L_WARN, "pid file contains old pid, replacing pid\n");
  361. }
  362. }
  363. pid=getpid();
  364. if ((pid_stream=fopen(pid_file, "w"))==NULL){
  365. LOG(L_WARN, "unable to create pid file %s: %s\n",
  366. pid_file, strerror(errno));
  367. goto error;
  368. }else{
  369. fprintf(pid_stream, "%i\n", (int)pid);
  370. fclose(pid_stream);
  371. }
  372. }
  373. /* try to replace stdin, stdout & stderr with /dev/null */
  374. if (freopen("/dev/null", "r", stdin)==0){
  375. LOG(L_ERR, "unable to replace stdin with /dev/null: %s\n",
  376. strerror(errno));
  377. /* continue, leave it open */
  378. };
  379. if (freopen("/dev/null", "w", stdout)==0){
  380. LOG(L_ERR, "unable to replace stdout with /dev/null: %s\n",
  381. strerror(errno));
  382. /* continue, leave it open */
  383. };
  384. /* close stderr only if log_stderr=0 */
  385. if ((!log_stderr) &&(freopen("/dev/null", "w", stderr)==0)){
  386. LOG(L_ERR, "unable to replace stderr with /dev/null: %s\n",
  387. strerror(errno));
  388. /* continue, leave it open */
  389. };
  390. /* close any open file descriptors */
  391. for (r=3;r<MAX_FD; r++){
  392. close(r);
  393. }
  394. if (log_stderr==0)
  395. openlog(name, LOG_PID|LOG_CONS, LOG_DAEMON);
  396. /* LOG_CONS, LOG_PERRROR ? */
  397. return 0;
  398. error:
  399. return -1;
  400. }
  401. void handle_sigs()
  402. {
  403. pid_t chld;
  404. int chld_status;
  405. switch(sig_flag){
  406. case 0: break; /* do nothing*/
  407. case SIGPIPE:
  408. /* SIGPIPE might be rarely received on use of
  409. exec module; simply ignore it
  410. */
  411. LOG(L_WARN, "WARNING: SIGPIPE received and ignored\n");
  412. break;
  413. case SIGINT:
  414. case SIGTERM:
  415. /* we end the program in all these cases */
  416. if (sig_flag==SIGINT)
  417. DBG("INT received, program terminates\n");
  418. #ifdef OBSOLETED
  419. else if (sig_flag==SIGPIPE)
  420. DBG("SIGPIPE received, program terminates\n");
  421. #endif
  422. else
  423. DBG("SIGTERM received, program terminates\n");
  424. /* first of all, kill the children also */
  425. kill(0, SIGTERM);
  426. /* Wait for all the children to die */
  427. while(wait(0) > 0);
  428. destroy_modules();
  429. #ifdef PKG_MALLOC
  430. LOG(memlog, "Memory status (pkg):\n");
  431. pkg_status();
  432. #endif
  433. #ifdef SHM_MEM
  434. LOG(memlog, "Memory status (shm):\n");
  435. shm_status();
  436. /* zero all shmem alloc vars that we still use */
  437. pt=0;
  438. shm_mem_destroy();
  439. #endif
  440. if (pid_file) unlink(pid_file);
  441. dprint("Thank you for flying " NAME "\n");
  442. exit(0);
  443. break;
  444. case SIGUSR1:
  445. #ifdef STATS
  446. dump_all_statistic();
  447. #endif
  448. #ifdef PKG_MALLOC
  449. LOG(memlog, "Memory status (pkg):\n");
  450. pkg_status();
  451. #endif
  452. #ifdef SHM_MEM
  453. LOG(memlog, "Memory status (shm):\n");
  454. shm_status();
  455. #endif
  456. break;
  457. case SIGCHLD:
  458. while ((chld=waitpid( -1, &chld_status, WNOHANG ))>0) {
  459. if (WIFEXITED(chld_status))
  460. LOG(L_INFO, "child process %d exited normally,"
  461. " status=%d\n", chld,
  462. WEXITSTATUS(chld_status));
  463. else if (WIFSIGNALED(chld_status)) {
  464. LOG(L_INFO, "child process %d exited by a signal"
  465. " %d\n", chld, WTERMSIG(chld_status));
  466. #ifdef WCOREDUMP
  467. LOG(L_INFO, "core was %sgenerated\n",
  468. WCOREDUMP(chld_status) ? "" : "not " );
  469. #endif
  470. }else if (WIFSTOPPED(chld_status))
  471. LOG(L_INFO, "child process %d stopped by a"
  472. " signal %d\n", chld,
  473. WSTOPSIG(chld_status));
  474. }
  475. #ifndef STOP_JIRIS_CHANGES
  476. if (dont_fork) {
  477. LOG(L_INFO, "INFO: dont_fork turned on, living on\n");
  478. break;
  479. }
  480. LOG(L_INFO, "INFO: terminating due to SIGCHLD\n");
  481. #endif
  482. /* exit */
  483. kill(0, SIGTERM);
  484. DBG("terminating due to SIGCHLD\n");
  485. exit(0);
  486. break;
  487. case SIGHUP: /* ignoring it*/
  488. DBG("SIGHUP received, ignoring it\n");
  489. break;
  490. default:
  491. LOG(L_CRIT, "WARNING: unhandled signal %d\n", sig_flag);
  492. }
  493. sig_flag=0;
  494. }
  495. /* main loop */
  496. int main_loop()
  497. {
  498. int r, i;
  499. pid_t pid;
  500. #ifdef USE_TCP
  501. int sockfd[2];
  502. #endif
  503. #ifdef WITH_SNMP_MOD
  504. int (*snmp_start)();
  505. /* initialize snmp module */
  506. snmp_start = (int(*)())find_export("snmp_start", 0);
  507. if(snmp_start)
  508. if(snmp_start() == -1)
  509. LOG(L_ERR, "ERROR: Couldn't start snmp agent\n");
  510. #endif
  511. /* one "main" process and n children handling i/o */
  512. if (dont_fork){
  513. #ifdef STATS
  514. setstats( 0 );
  515. #endif
  516. /* only one address, we ignore all the others */
  517. if (udp_init(&sock_info[0])==-1) goto error;
  518. bind_address=&sock_info[0];
  519. bind_idx=0;
  520. if (sock_no>1){
  521. LOG(L_WARN, "WARNING: using only the first listen address"
  522. " (no fork)\n");
  523. }
  524. /* process_no now initialized to zero -- increase from now on
  525. as new processes are forked (while skipping 0 reserved for main
  526. */
  527. /* we need another process to act as the timer*/
  528. #ifndef USE_TCP
  529. /* if we are using tcp we always need a timer process,
  530. * we cannot count on select timeout to measure time
  531. * (it works only on linux)
  532. */
  533. if (timer_list)
  534. #endif
  535. {
  536. process_no++;
  537. if ((pid=fork())<0){
  538. LOG(L_CRIT, "ERROR: main_loop: Cannot fork\n");
  539. goto error;
  540. }
  541. if (pid==0){
  542. /* child */
  543. /* timer!*/
  544. /* process_bit = 0; */
  545. for(;;){
  546. sleep(TIMER_TICK);
  547. timer_ticker();
  548. }
  549. }else{
  550. pt[process_no].pid=pid; /*should be shared mem anway*/
  551. strncpy(pt[process_no].desc, "timer", MAX_PT_DESC );
  552. }
  553. }
  554. /* if configured to do so, start a server for accepting FIFO commands */
  555. if (open_fifo_server()<0) {
  556. LOG(L_ERR, "opening fifo server failed\n");
  557. goto error;
  558. }
  559. /* main process, receive loop */
  560. process_no=0; /*main process number*/
  561. pt[process_no].pid=getpid();
  562. snprintf(pt[process_no].desc, MAX_PT_DESC,
  563. "stand-alone receiver @ %s:%s",
  564. bind_address->name.s, bind_address->port_no_str.s );
  565. /* We will call child_init even if we
  566. * do not fork
  567. */
  568. if (init_child(0) < 0) {
  569. LOG(L_ERR, "init_child failed\n");
  570. goto error;
  571. }
  572. is_main=1; /* hack 42: call init_child with is_main=0 in case
  573. some modules wants to fork a child */
  574. return udp_rcv_loop();
  575. }else{
  576. /* process_no now initialized to zero -- increase from now on
  577. as new processes are forked (while skipping 0 reserved for main )
  578. */
  579. for(r=0;r<sock_no;r++){
  580. /* create the listening socket (for each address)*/
  581. /* udp */
  582. if (udp_init(&sock_info[r])==-1) goto error;
  583. /* get first ipv4/ipv6 socket*/
  584. if ((sock_info[r].address.af==AF_INET)&&
  585. ((sendipv4==0)||(sendipv4->is_lo)))
  586. sendipv4=&sock_info[r];
  587. #ifdef USE_IPV6
  588. if((sendipv6==0)&&(sock_info[r].address.af==AF_INET6))
  589. sendipv6=&sock_info[r];
  590. #endif
  591. #ifdef USE_TCP
  592. tcp_info[r]=sock_info[r]; /* copy the sockets */
  593. /* same thing for tcp */
  594. if (tcp_init(&tcp_info[r])==-1) goto error;
  595. /* get first ipv4/ipv6 socket*/
  596. if ((tcp_info[r].address.af==AF_INET)&&
  597. ((sendipv4_tcp==0)||(sendipv4_tcp->is_lo)))
  598. sendipv4_tcp=&tcp_info[r];
  599. #ifdef USE_IPV6
  600. if((sendipv6_tcp==0)&&(tcp_info[r].address.af==AF_INET6))
  601. sendipv6_tcp=&tcp_info[r];
  602. #endif
  603. #endif
  604. /* all procs should have access to all the sockets (for sending)
  605. * so we open all first*/
  606. }
  607. for(r=0; r<sock_no;r++){
  608. for(i=0;i<children_no;i++){
  609. process_no++;
  610. #ifdef USE_TCP
  611. if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd)<0){
  612. LOG(L_ERR, "ERROR: main_loop: socketpair failed: %s\n",
  613. strerror(errno));
  614. goto error;
  615. }
  616. #endif
  617. if ((pid=fork())<0){
  618. LOG(L_CRIT, "main_loop: Cannot fork\n");
  619. goto error;
  620. }else if (pid==0){
  621. /* child */
  622. #ifdef USE_TCP
  623. close(sockfd[0]);
  624. unix_tcp_sock=sockfd[1];
  625. #endif
  626. bind_address=&sock_info[r]; /* shortcut */
  627. bind_idx=r;
  628. if (init_child(i) < 0) {
  629. LOG(L_ERR, "init_child failed\n");
  630. goto error;
  631. }
  632. #ifdef STATS
  633. setstats( i+r*children_no );
  634. #endif
  635. return udp_rcv_loop();
  636. }else{
  637. pt[process_no].pid=pid; /*should be in shared mem.*/
  638. snprintf(pt[process_no].desc, MAX_PT_DESC,
  639. "receiver child=%d sock=%d @ %s:%s", i, r,
  640. sock_info[r].name.s, sock_info[r].port_no_str.s );
  641. #ifdef USE_TCP
  642. close(sockfd[1]);
  643. pt[process_no].unix_sock=sockfd[0];
  644. pt[process_no].idx=-1; /* this is not "tcp" process*/
  645. #endif
  646. }
  647. }
  648. /*parent*/
  649. /*close(udp_sock)*/; /*if it's closed=>sendto invalid fd errors?*/
  650. }
  651. }
  652. /*this is the main process*/
  653. bind_address=&sock_info[0]; /* main proc -> it shoudln't send anything, */
  654. bind_idx=0; /* if it does it will use the first address */
  655. /* if configured to do so, start a server for accepting FIFO commands */
  656. if (open_fifo_server()<0) {
  657. LOG(L_ERR, "opening fifo server failed\n");
  658. goto error;
  659. }
  660. #ifndef USE_TCP
  661. /* if we are using tcp we always need the timer */
  662. if (timer_list)
  663. #endif
  664. {
  665. #ifdef USE_TCP
  666. if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd)<0){
  667. LOG(L_ERR, "ERROR: main_loop: socketpair failed: %s\n",
  668. strerror(errno));
  669. goto error;
  670. }
  671. #endif
  672. /* fork again for the attendant process*/
  673. process_no++;
  674. if ((pid=fork())<0){
  675. LOG(L_CRIT, "main_loop: cannot fork timer process\n");
  676. goto error;
  677. }else if (pid==0){
  678. /* child */
  679. /* is_main=0; */
  680. #ifdef USE_TCP
  681. close(sockfd[0]);
  682. unix_tcp_sock=sockfd[1];
  683. #endif
  684. for(;;){
  685. /* debug: instead of doing something usefull */
  686. /* (placeholder for timers, etc.) */
  687. sleep(TIMER_TICK);
  688. /* if we received a signal => TIMER_TICK may have not elapsed*/
  689. timer_ticker();
  690. }
  691. }else{
  692. pt[process_no].pid=pid;
  693. strncpy(pt[process_no].desc, "timer", MAX_PT_DESC );
  694. #ifdef USE_TCP
  695. close(sockfd[1]);
  696. pt[process_no].unix_sock=sockfd[0];
  697. pt[process_no].idx=-1; /* this is not a "tcp" process*/
  698. #endif
  699. }
  700. }
  701. #ifdef USE_TCP
  702. /* start tcp receivers */
  703. if (tcp_init_children()<0) goto error;
  704. /* start tcp master proc */
  705. process_no++;
  706. if ((pid=fork())<0){
  707. LOG(L_CRIT, "main_loop: cannot fork tcp main process\n");
  708. goto error;
  709. }else if (pid==0){
  710. /* child */
  711. /* is_main=0; */
  712. tcp_main_loop();
  713. }else{
  714. pt[process_no].pid=pid;
  715. strncpy(pt[process_no].desc, "tcp main process", MAX_PT_DESC );
  716. pt[process_no].unix_sock=-1;
  717. pt[process_no].idx=-1; /* this is not a "tcp" process*/
  718. unix_tcp_sock=-1;
  719. }
  720. #endif
  721. /* main */
  722. pt[0].pid=getpid();
  723. strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
  724. #ifdef USE_TCP
  725. pt[process_no].unix_sock=-1;
  726. pt[process_no].idx=-1; /* this is not a "tcp" process*/
  727. unix_tcp_sock=-1;
  728. #endif
  729. /*DEBUG- remove it*/
  730. #ifdef DEBUG
  731. printf("\n% 3d processes, % 3d children * % 3d listening addresses + main"
  732. " + fifo %s\n", process_no+1, children_no, sock_no,
  733. (timer_list)?"+ timer":"");
  734. for (r=0; r<=process_no; r++){
  735. printf("% 3d % 5d\n", r, pt[r].pid);
  736. }
  737. #endif
  738. process_no=0;
  739. /* process_bit = 0; */
  740. is_main=1;
  741. for(;;){
  742. pause();
  743. handle_sigs();
  744. }
  745. /*return 0; */
  746. error:
  747. return -1;
  748. }
  749. /* added by jku; allows for regular exit on a specific signal;
  750. good for profiling which only works if exited regularly and
  751. not by default signal handlers
  752. - modified by andrei: moved most of the stuff to handle_sigs,
  753. made it safer for the "fork" case
  754. */
  755. static void sig_usr(int signo)
  756. {
  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 SIGINT:
  769. case SIGPIPE:
  770. case SIGTERM:
  771. /* print memory stats for non-main too */
  772. #ifdef PKG_MALLOC
  773. LOG(memlog, "Memory status (pkg):\n");
  774. pkg_status();
  775. #endif
  776. exit(0);
  777. break;
  778. case SIGUSR1:
  779. /* statistics, do nothing, printed only from the main proc */
  780. break;
  781. /* ignored*/
  782. case SIGUSR2:
  783. case SIGHUP:
  784. break;
  785. case SIGCHLD:
  786. #ifndef STOP_JIRIS_CHANGES
  787. LOG(L_INFO, "INFO: SIGCHLD received: "
  788. "we do not worry about grand-children\n");
  789. #else
  790. exit(0); /* terminate if one child died */
  791. #endif
  792. }
  793. }
  794. }
  795. /* add all family type addresses of interface if_name to the socket_info array
  796. * if if_name==0, adds all addresses on all interfaces
  797. * WARNING: it only works with ipv6 addresses on FreeBSD
  798. * return: -1 on error, 0 on success
  799. */
  800. int add_interfaces(char* if_name, int family, unsigned short port)
  801. {
  802. struct ifconf ifc;
  803. struct ifreq ifr;
  804. struct ifreq ifrcopy;
  805. char* last;
  806. char* p;
  807. int size;
  808. int lastlen;
  809. int s;
  810. char* tmp;
  811. struct ip_addr addr;
  812. int ret;
  813. #ifdef HAVE_SOCKADDR_SA_LEN
  814. #ifndef MAX
  815. #define MAX(a,b) ( ((a)>(b))?(a):(b))
  816. #endif
  817. #endif
  818. /* ipv4 or ipv6 only*/
  819. s=socket(family, SOCK_DGRAM, 0);
  820. ret=-1;
  821. lastlen=0;
  822. ifc.ifc_req=0;
  823. for (size=10; ; size*=2){
  824. ifc.ifc_len=size*sizeof(struct ifreq);
  825. ifc.ifc_req=(struct ifreq*) malloc(size*sizeof(struct ifreq));
  826. if (ifc.ifc_req==0){
  827. fprintf(stderr, "memory allocation failure\n");
  828. goto error;
  829. }
  830. if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
  831. if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
  832. fprintf(stderr, "ioctl failed: %s\n", strerror(errno));
  833. goto error;
  834. }
  835. if ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
  836. len not changed*/
  837. lastlen=ifc.ifc_len;
  838. /* try a bigger array*/
  839. free(ifc.ifc_req);
  840. }
  841. last=(char*)ifc.ifc_req+ifc.ifc_len;
  842. for(p=(char*)ifc.ifc_req; p<last;
  843. p+=(sizeof(ifr.ifr_name)+
  844. #ifdef HAVE_SOCKADDR_SA_LEN
  845. MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
  846. #else
  847. ( (ifr.ifr_addr.sa_family==AF_INET)?
  848. sizeof(struct sockaddr_in):
  849. ((ifr.ifr_addr.sa_family==AF_INET6)?
  850. sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
  851. #endif
  852. )
  853. )
  854. {
  855. /* copy contents into ifr structure
  856. * warning: it might be longer (e.g. ipv6 address) */
  857. memcpy(&ifr, p, sizeof(ifr));
  858. if (ifr.ifr_addr.sa_family!=family){
  859. /*printf("strange family %d skipping...\n",
  860. ifr->ifr_addr.sa_family);*/
  861. continue;
  862. }
  863. /*get flags*/
  864. ifrcopy=ifr;
  865. if (ioctl(s, SIOCGIFFLAGS, &ifrcopy)!=-1){ /* ignore errors */
  866. /* ignore down ifs only if listening on all of them*/
  867. if (if_name==0){
  868. /* if if not up, skip it*/
  869. if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
  870. }
  871. }
  872. if ((if_name==0)||
  873. (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
  874. /*add address*/
  875. if (sock_no<MAX_LISTEN){
  876. sockaddr2ip_addr(&addr,
  877. (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
  878. if ((tmp=ip_addr2a(&addr))==0) goto error;
  879. /* fill the strings*/
  880. sock_info[sock_no].name.s=(char*)malloc(strlen(tmp)+1);
  881. if(sock_info[sock_no].name.s==0){
  882. fprintf(stderr, "Out of memory.\n");
  883. goto error;
  884. }
  885. /* fill in the new name and port */
  886. sock_info[sock_no].name.len=strlen(tmp);
  887. strncpy(sock_info[sock_no].name.s, tmp,
  888. sock_info[sock_no].name.len+1);
  889. sock_info[sock_no].port_no=port;
  890. /* mark if loopback */
  891. if (ifrcopy.ifr_flags & IFF_LOOPBACK)
  892. sock_info[sock_no].is_lo=1;
  893. sock_no++;
  894. ret=0;
  895. }else{
  896. fprintf(stderr, "Too many addresses (max %d)\n", MAX_LISTEN);
  897. goto error;
  898. }
  899. }
  900. /*
  901. printf("%s:\n", ifr->ifr_name);
  902. printf(" ");
  903. print_sockaddr(&(ifr->ifr_addr));
  904. printf(" ");
  905. ls_ifflags(ifr->ifr_name, family, options);
  906. printf("\n");*/
  907. }
  908. free(ifc.ifc_req); /*clean up*/
  909. close(s);
  910. return ret;
  911. error:
  912. if (ifc.ifc_req) free(ifc.ifc_req);
  913. close(s);
  914. return -1;
  915. }
  916. int main(int argc, char** argv)
  917. {
  918. FILE* cfg_stream;
  919. struct hostent* he;
  920. int c,r,t;
  921. char *tmp;
  922. char** h;
  923. struct host_alias* a;
  924. struct utsname myname;
  925. char *options;
  926. char port_no_str[MAX_PORT_LEN];
  927. int port_no_str_len;
  928. int ret;
  929. struct passwd *pw_entry;
  930. struct group *gr_entry;
  931. unsigned int seed;
  932. int rfd;
  933. /*init*/
  934. port_no_str_len=0;
  935. ret=-1;
  936. /* added by jku: add exit handler */
  937. if (signal(SIGINT, sig_usr) == SIG_ERR ) {
  938. DPrint("ERROR: no SIGINT signal handler can be installed\n");
  939. goto error;
  940. }
  941. /* if we debug and write to a pipe, we want to exit nicely too */
  942. if (signal(SIGPIPE, sig_usr) == SIG_ERR ) {
  943. DPrint("ERROR: no SIGINT signal handler can be installed\n");
  944. goto error;
  945. }
  946. if (signal(SIGUSR1, sig_usr) == SIG_ERR ) {
  947. DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
  948. goto error;
  949. }
  950. if (signal(SIGCHLD , sig_usr) == SIG_ERR ) {
  951. DPrint("ERROR: no SIGCHLD signal handler can be installed\n");
  952. goto error;
  953. }
  954. if (signal(SIGTERM , sig_usr) == SIG_ERR ) {
  955. DPrint("ERROR: no SIGTERM signal handler can be installed\n");
  956. goto error;
  957. }
  958. if (signal(SIGHUP , sig_usr) == SIG_ERR ) {
  959. DPrint("ERROR: no SIGHUP signal handler can be installed\n");
  960. goto error;
  961. }
  962. if (signal(SIGUSR2 , sig_usr) == SIG_ERR ) {
  963. DPrint("ERROR: no SIGUSR2 signal handler can be installed\n");
  964. goto error;
  965. }
  966. #ifdef DBG_MSG_QA
  967. fprintf(stderr, "WARNING: ser startup: "
  968. "DBG_MSG_QA enabled, ser may exit abruptly\n");
  969. #endif
  970. /* process command line (get port no, cfg. file path etc) */
  971. opterr=0;
  972. options=
  973. #ifdef STATS
  974. "s:"
  975. #endif
  976. "f:p:m:b:l:n:rRvdDEVhw:t:u:g:P:i:";
  977. while((c=getopt(argc,argv,options))!=-1){
  978. switch(c){
  979. case 'f':
  980. cfg_file=optarg;
  981. break;
  982. case 's':
  983. #ifdef STATS
  984. stat_file=optarg;
  985. #endif
  986. break;
  987. case 'p':
  988. port_no=strtol(optarg, &tmp, 10);
  989. if (tmp &&(*tmp)){
  990. fprintf(stderr, "bad port number: -p %s\n", optarg);
  991. goto error;
  992. }
  993. if (sock_no>0) sock_info[sock_no-1].port_no=port_no;
  994. break;
  995. case 'm':
  996. shm_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
  997. if (tmp &&(*tmp)){
  998. fprintf(stderr, "bad shmem size number: -m %s\n",
  999. optarg);
  1000. goto error;
  1001. };
  1002. LOG(L_INFO, "ser: shared memory allocated: %d MByte\n",
  1003. shm_mem_size );
  1004. break;
  1005. case 'b':
  1006. maxbuffer=strtol(optarg, &tmp, 10);
  1007. if (tmp &&(*tmp)){
  1008. fprintf(stderr, "bad max buffer size number: -p %s\n",
  1009. optarg);
  1010. goto error;
  1011. }
  1012. break;
  1013. case 'l':
  1014. /* add a new addr. to our address list */
  1015. if (sock_no < MAX_LISTEN){
  1016. sock_info[sock_no].name.s=
  1017. (char*)malloc(strlen(optarg)+1);
  1018. if (sock_info[sock_no].name.s==0){
  1019. fprintf(stderr, "Out of memory.\n");
  1020. goto error;
  1021. }
  1022. strncpy(sock_info[sock_no].name.s, optarg,
  1023. strlen(optarg)+1);
  1024. sock_info[sock_no].name.len=strlen(optarg);
  1025. /* set default port */
  1026. sock_info[sock_no].port_no=port_no;
  1027. sock_no++;
  1028. }else{
  1029. fprintf(stderr,
  1030. "Too many addresses (max. %d).\n",
  1031. MAX_LISTEN);
  1032. goto error;
  1033. }
  1034. break;
  1035. case 'n':
  1036. children_no=strtol(optarg, &tmp, 10);
  1037. if ((tmp==0) ||(*tmp)){
  1038. fprintf(stderr, "bad process number: -n %s\n",
  1039. optarg);
  1040. goto error;
  1041. }
  1042. break;
  1043. case 'v':
  1044. check_via=1;
  1045. break;
  1046. case 'r':
  1047. received_dns|=DO_DNS;
  1048. break;
  1049. case 'R':
  1050. received_dns|=DO_REV_DNS;
  1051. case 'd':
  1052. debug++;
  1053. break;
  1054. case 'D':
  1055. dont_fork=1;
  1056. break;
  1057. case 'E':
  1058. log_stderr=1;
  1059. break;
  1060. case 'V':
  1061. printf("version: %s\n", version);
  1062. printf("flags: %s\n", flags );
  1063. print_ct_constants();
  1064. printf("%s\n",id);
  1065. printf("%s compiled on %s with %s\n", __FILE__,
  1066. compiled, COMPILER );
  1067. exit(0);
  1068. break;
  1069. case 'h':
  1070. printf("version: %s\n", version);
  1071. printf("%s",help_msg);
  1072. exit(0);
  1073. break;
  1074. case 'w':
  1075. working_dir=optarg;
  1076. break;
  1077. case 't':
  1078. chroot_dir=optarg;
  1079. break;
  1080. case 'u':
  1081. user=optarg;
  1082. break;
  1083. case 'g':
  1084. group=optarg;
  1085. break;
  1086. case 'P':
  1087. pid_file=optarg;
  1088. break;
  1089. case 'i':
  1090. fifo=optarg;
  1091. break;
  1092. case '?':
  1093. if (isprint(optopt))
  1094. fprintf(stderr, "Unknown option `-%c´.\n", optopt);
  1095. else
  1096. fprintf(stderr,
  1097. "Unknown option character `\\x%x´.\n",
  1098. optopt);
  1099. goto error;
  1100. case ':':
  1101. fprintf(stderr,
  1102. "Option `-%c´ requires an argument.\n",
  1103. optopt);
  1104. goto error;
  1105. default:
  1106. abort();
  1107. }
  1108. }
  1109. /* fill missing arguments with the default values*/
  1110. if (cfg_file==0) cfg_file=CFG_FILE;
  1111. /* load config file or die */
  1112. cfg_stream=fopen (cfg_file, "r");
  1113. if (cfg_stream==0){
  1114. fprintf(stderr, "ERROR: loading config file(%s): %s\n", cfg_file,
  1115. strerror(errno));
  1116. goto error;
  1117. }
  1118. /* seed the prng */
  1119. /* try to use /dev/random if possible */
  1120. seed=0;
  1121. if ((rfd=open("/dev/random", O_RDONLY))!=-1){
  1122. try_again:
  1123. if (read(rfd, (void*)&seed, sizeof(seed))==-1){
  1124. if (errno==EINTR) goto try_again; /* interrupted by signal */
  1125. LOG(L_WARN, "WARNING: could not read from /dev/random (%d)\n",
  1126. errno);
  1127. }
  1128. DBG("read %u from /dev/random\n", seed);
  1129. close(rfd);
  1130. }else{
  1131. LOG(L_WARN, "WARNING: could not open /dev/random (%d)\n", errno);
  1132. }
  1133. seed+=getpid()+time(0);
  1134. DBG("seeding PRNG with %u\n", seed);
  1135. srand(seed);
  1136. DBG("test random number %u\n", rand());
  1137. /* init hash fucntion */
  1138. if (init_hash()<0) {
  1139. LOG(L_ERR, "ERROR: init_hash failed\n");
  1140. goto error;
  1141. }
  1142. /*init mallocs (before parsing cfg !)*/
  1143. if (init_mallocs()==-1)
  1144. goto error;
  1145. /*init timer, before parsing the cfg!*/
  1146. if (init_timer()<0){
  1147. LOG(L_CRIT, "could not initialize timer, exiting...\n");
  1148. goto error;
  1149. }
  1150. #ifdef USE_TCP
  1151. /*init tcp*/
  1152. if (init_tcp()<0){
  1153. LOG(L_CRIT, "could not initialize tcp, exiting...\n");
  1154. goto error;
  1155. }
  1156. #endif
  1157. /* register a diagnostic FIFO command */
  1158. if (register_core_fifo()<0) {
  1159. LOG(L_CRIT, "unable to register core FIFO commands\n");
  1160. goto error;
  1161. }
  1162. /*register builtin modules*/
  1163. register_builtin_modules();
  1164. yyin=cfg_stream;
  1165. if ((yyparse()!=0)||(cfg_errors)){
  1166. fprintf(stderr, "ERROR: bad config file (%d errors)\n", cfg_errors);
  1167. goto error;
  1168. }
  1169. print_rl();
  1170. /* fix parameters */
  1171. if (port_no<=0) port_no=SIP_PORT;
  1172. if (children_no<=0) children_no=CHILD_NO;
  1173. #ifdef USE_TCP
  1174. tcp_children_no=children_no;
  1175. #endif
  1176. #ifdef _OBSOLETED
  1177. else if (children_no >= MAX_PROCESSES ) {
  1178. fprintf(stderr, "ERROR: too many children processes configured;"
  1179. " maximum is %d\n",
  1180. MAX_PROCESSES-1 );
  1181. goto error;
  1182. }
  1183. #endif
  1184. if (working_dir==0) working_dir="/";
  1185. /* get uid/gid */
  1186. if (user){
  1187. uid=strtol(user, &tmp, 10);
  1188. if ((tmp==0) ||(*tmp)){
  1189. /* maybe it's a string */
  1190. pw_entry=getpwnam(user);
  1191. if (pw_entry==0){
  1192. fprintf(stderr, "bad user name/uid number: -u %s\n", user);
  1193. goto error;
  1194. }
  1195. uid=pw_entry->pw_uid;
  1196. gid=pw_entry->pw_gid;
  1197. }
  1198. }
  1199. if (group){
  1200. gid=strtol(user, &tmp, 10);
  1201. if ((tmp==0) ||(*tmp)){
  1202. /* maybe it's a string */
  1203. gr_entry=getgrnam(group);
  1204. if (gr_entry==0){
  1205. fprintf(stderr, "bad group name/gid number: -u %s\n", group);
  1206. goto error;
  1207. }
  1208. gid=gr_entry->gr_gid;
  1209. }
  1210. }
  1211. if (sock_no==0) {
  1212. /* try to get all listening ipv4 interfaces */
  1213. if (add_interfaces(0, AF_INET, 0)==-1){
  1214. /* if error fall back to get hostname*/
  1215. /* get our address, only the first one */
  1216. if (uname (&myname) <0){
  1217. fprintf(stderr, "cannot determine hostname, try -l address\n");
  1218. goto error;
  1219. }
  1220. sock_info[sock_no].name.s=(char*)malloc(strlen(myname.nodename)+1);
  1221. if (sock_info[sock_no].name.s==0){
  1222. fprintf(stderr, "Out of memory.\n");
  1223. goto error;
  1224. }
  1225. sock_info[sock_no].name.len=strlen(myname.nodename);
  1226. strncpy(sock_info[sock_no].name.s, myname.nodename,
  1227. sock_info[sock_no].name.len+1);
  1228. sock_no++;
  1229. }
  1230. }
  1231. /* try to change all the interface names into addresses
  1232. * --ugly hack */
  1233. for (r=0; r<sock_no;){
  1234. if (add_interfaces(sock_info[r].name.s, AF_INET,
  1235. sock_info[r].port_no)!=-1){
  1236. /* success => remove current entry (shift the entire array)*/
  1237. free(sock_info[r].name.s);
  1238. memmove(&sock_info[r], &sock_info[r+1],
  1239. (sock_no-r)*sizeof(struct socket_info));
  1240. sock_no--;
  1241. continue;
  1242. }
  1243. r++;
  1244. }
  1245. /* get ips & fill the port numbers*/
  1246. #ifdef EXTRA_DEBUG
  1247. printf("Listening on \n");
  1248. #endif
  1249. for (r=0; r<sock_no;r++){
  1250. /* fix port number, port_no should be !=0 here */
  1251. if (sock_info[r].port_no==0) sock_info[r].port_no=port_no;
  1252. port_no_str_len=snprintf(port_no_str, MAX_PORT_LEN, ":%d",
  1253. (unsigned short) sock_info[r].port_no);
  1254. if (port_no_str_len<0){
  1255. fprintf(stderr, "ERROR: bad port number: %d\n",
  1256. sock_info[r].port_no);
  1257. goto error;
  1258. }
  1259. /* on some systems snprintf returns really strange things if it does
  1260. not have enough space */
  1261. port_no_str_len=
  1262. (port_no_str_len<MAX_PORT_LEN)?port_no_str_len:MAX_PORT_LEN;
  1263. sock_info[r].port_no_str.s=(char*)malloc(strlen(port_no_str)+1);
  1264. if (sock_info[r].port_no_str.s==0){
  1265. fprintf(stderr, "Out of memory.\n");
  1266. goto error;
  1267. }
  1268. strncpy(sock_info[r].port_no_str.s, port_no_str,
  1269. strlen(port_no_str)+1);
  1270. sock_info[r].port_no_str.len=strlen(port_no_str);
  1271. /* get "official hostnames", all the aliases etc. */
  1272. he=resolvehost(sock_info[r].name.s);
  1273. if (he==0){
  1274. DPrint("ERROR: could not resolve %s\n", sock_info[r].name.s);
  1275. goto error;
  1276. }
  1277. /* check if we got the official name */
  1278. if (strcasecmp(he->h_name, sock_info[r].name.s)!=0){
  1279. if (add_alias(sock_info[r].name.s, sock_info[r].name.len,
  1280. sock_info[r].port_no)<0){
  1281. LOG(L_ERR, "ERROR: main: add_alias failed\n");
  1282. }
  1283. /* change the oficial name */
  1284. free(sock_info[r].name.s);
  1285. sock_info[r].name.s=(char*)malloc(strlen(he->h_name)+1);
  1286. if (sock_info[r].name.s==0){
  1287. fprintf(stderr, "Out of memory.\n");
  1288. goto error;
  1289. }
  1290. sock_info[r].name.len=strlen(he->h_name);
  1291. strncpy(sock_info[r].name.s, he->h_name, sock_info[r].name.len+1);
  1292. }
  1293. /* add the aliases*/
  1294. for(h=he->h_aliases; h && *h; h++)
  1295. if (add_alias(*h, strlen(*h), sock_info[r].port_no)<0){
  1296. LOG(L_ERR, "ERROR: main: add_alias failed\n");
  1297. }
  1298. hostent2ip_addr(&sock_info[r].address, he, 0); /*convert to ip_addr
  1299. format*/
  1300. if ((tmp=ip_addr2a(&sock_info[r].address))==0) goto error;
  1301. sock_info[r].address_str.s=(char*)malloc(strlen(tmp)+1);
  1302. if (sock_info[r].address_str.s==0){
  1303. fprintf(stderr, "Out of memory.\n");
  1304. goto error;
  1305. }
  1306. strncpy(sock_info[r].address_str.s, tmp, strlen(tmp)+1);
  1307. /* set is_ip (1 if name is an ip address, 0 otherwise) */
  1308. sock_info[r].address_str.len=strlen(tmp);
  1309. if ( (sock_info[r].address_str.len==sock_info[r].name.len)&&
  1310. (strncasecmp(sock_info[r].address_str.s, sock_info[r].name.s,
  1311. sock_info[r].address_str.len)==0)
  1312. ){
  1313. sock_info[r].is_ip=1;
  1314. /* do rev. dns on it (for aliases)*/
  1315. he=rev_resolvehost(&sock_info[r].address);
  1316. if (he==0){
  1317. DPrint("WARNING: could not rev. resolve %s\n",
  1318. sock_info[r].name.s);
  1319. }else{
  1320. /* add the aliases*/
  1321. if (add_alias(he->h_name, strlen(he->h_name),
  1322. sock_info[r].port_no)<0){
  1323. LOG(L_ERR, "ERROR: main: add_alias failed\n");
  1324. }
  1325. for(h=he->h_aliases; h && *h; h++)
  1326. if (add_alias(*h,strlen(*h),sock_info[r].port_no)<0){
  1327. LOG(L_ERR, "ERROR: main: add_alias failed\n");
  1328. }
  1329. }
  1330. }else{ sock_info[r].is_ip=0; };
  1331. #ifdef EXTRA_DEBUG
  1332. printf(" %.*s [%s]:%s\n", sock_info[r].name.len,
  1333. sock_info[r].name.s,
  1334. sock_info[r].address_str.s, sock_info[r].port_no_str.s);
  1335. #endif
  1336. }
  1337. /* removing duplicate addresses*/
  1338. for (r=0; r<sock_no; r++){
  1339. for (t=r+1; t<sock_no;){
  1340. if ((sock_info[r].port_no==sock_info[t].port_no) &&
  1341. (sock_info[r].address.af==sock_info[t].address.af) &&
  1342. (memcmp(sock_info[r].address.u.addr,
  1343. sock_info[t].address.u.addr,
  1344. sock_info[r].address.len) == 0)
  1345. ){
  1346. #ifdef EXTRA_DEBUG
  1347. printf("removing duplicate (%d) %s [%s] == (%d) %s [%s]\n",
  1348. r, sock_info[r].name.s, sock_info[r].address_str.s,
  1349. t, sock_info[t].name.s, sock_info[t].address_str.s);
  1350. #endif
  1351. /* add the name to the alias list*/
  1352. if ((!sock_info[t].is_ip) && (
  1353. (sock_info[t].name.len!=sock_info[r].name.len)||
  1354. (strncmp(sock_info[t].name.s, sock_info[r].name.s,
  1355. sock_info[r].name.len)!=0))
  1356. )
  1357. add_alias(sock_info[t].name.s, sock_info[t].name.len,
  1358. sock_info[t].port_no);
  1359. /* free space*/
  1360. free(sock_info[t].name.s);
  1361. free(sock_info[t].address_str.s);
  1362. free(sock_info[t].port_no_str.s);
  1363. /* shift the array*/
  1364. memmove(&sock_info[t], &sock_info[t+1],
  1365. (sock_no-t)*sizeof(struct socket_info));
  1366. sock_no--;
  1367. continue;
  1368. }
  1369. t++;
  1370. }
  1371. }
  1372. /* print all the listen addresses */
  1373. printf("Listening on \n");
  1374. for (r=0; r<sock_no; r++)
  1375. printf(" %s [%s]:%s\n",sock_info[r].name.s,
  1376. sock_info[r].address_str.s, sock_info[r].port_no_str.s);
  1377. printf("Aliases: ");
  1378. for(a=aliases; a; a=a->next)
  1379. if (a->port)
  1380. printf("%.*s:%d ", a->alias.len, a->alias.s, a->port);
  1381. else
  1382. printf("%.*s:* ", a->alias.len, a->alias.s);
  1383. printf("\n");
  1384. if (sock_no==0){
  1385. fprintf(stderr, "ERROR: no listening sockets");
  1386. goto error;
  1387. }
  1388. if (dont_fork){
  1389. fprintf(stderr, "WARNING: no fork mode %s\n",
  1390. (sock_no>1)?" and more than one listen address found (will"
  1391. " use only the the first one)":"");
  1392. }
  1393. /* init_daemon? */
  1394. if (!dont_fork){
  1395. if ( daemonize(argv[0]) <0 ) goto error;
  1396. }
  1397. if (init_modules() != 0) {
  1398. fprintf(stderr, "ERROR: error while initializing modules\n");
  1399. goto error;
  1400. }
  1401. /*alloc pids*/
  1402. #ifdef SHM_MEM
  1403. pt=shm_malloc(sizeof(struct process_table)*process_count());
  1404. #else
  1405. pt=malloc(sizeof(struct process_table)*process_count());
  1406. #endif
  1407. if (pt==0){
  1408. fprintf(stderr, "ERROR: out of memory\n");
  1409. goto error;
  1410. }
  1411. memset(pt, 0, sizeof(struct process_table)*process_count());
  1412. /* fix routing lists */
  1413. if ( (r=fix_rls())!=0){
  1414. fprintf(stderr, "ERROR: error %x while trying to fix configuration\n",
  1415. r);
  1416. goto error;
  1417. };
  1418. #ifdef STATS
  1419. if (init_stats( dont_fork ? 1 : children_no )==-1) goto error;
  1420. #endif
  1421. ret=main_loop();
  1422. /*kill everything*/
  1423. kill(0, SIGTERM);
  1424. return ret;
  1425. error:
  1426. /*kill everything*/
  1427. kill(0, SIGTERM);
  1428. return -1;
  1429. }