1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
16 struct timeval now_tv;
17 struct timezone now_tz;
20 gettimeofday(&now_tv, &now_tz); \
21 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
24 FILE *debug_fp = NULL;
28 struct lcr_fdset lcr_fdset[FD_SETSIZE];
31 pthread_mutex_t mutexd; // debug output mutex
32 pthread_mutex_t mutext; // trace output mutex
33 pthread_mutex_t mutexe; // error output mutex
34 //pthread_mutex_t mutex_lcr; // lcr process mutex
49 int debug_newline = 1;
52 void debug(const char *function, int line, const char *prefix, char *buffer)
57 /* if we have a new debug count, we add a mark */
58 if (last_debug != debug_count) {
59 last_debug = debug_count;
61 now_tm = localtime(&now);
63 printf("\033[34m--------------------- %04d.%02d.%02d %02d:%02d:%02d %06d\033[36m\n", now_tm->tm_year+1900, now_tm->tm_mon+1, now_tm->tm_mday, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec, debug_count%1000000);
65 fprintf(debug_fp, "--------------------- %04d.%02d.%02d %02d:%02d:%02d %06d\n", now_tm->tm_year+1900, now_tm->tm_mon+1, now_tm->tm_mday, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec, debug_count%1000000);
70 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
72 printf("(in %s() line %d): %s", function, line, buffer);
80 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
82 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
88 if (buffer[strlen(buffer)-1] == '\n')
93 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
98 if (!(options.deb & mask))
100 pthread_mutex_lock(&mutexd);
103 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
104 buffer[sizeof(buffer)-1]=0;
107 debug(function, line, "DEBUG", buffer);
109 pthread_mutex_unlock(&mutexd);
112 void _printerror(const char *function, int line, const char *fmt, ...)
117 pthread_mutex_lock(&mutexe);
120 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
121 buffer[sizeof(buffer)-1]=0;
125 debug(function, line, "ERROR", buffer);
126 else { /* only if we do not debug */
128 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
130 fprintf(stderr, "ERROR %s", buffer);
133 pthread_mutex_unlock(&mutexe);
137 void sighandler(int sigset)
139 struct sched_param schedp;
141 if (sigset == SIGHUP)
143 if (sigset == SIGPIPE)
147 /* set scheduler & priority */
148 if (options.schedule > 1) {
149 memset(&schedp, 0, sizeof(schedp));
150 schedp.sched_priority = 0;
151 sched_setscheduler(0, SCHED_OTHER, &schedp);
153 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
154 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
162 int main(int argc, char *argv[])
164 #if defined WITH_GSM_BS || defined WITH_GSM_MS
165 double now_d, last_d;
169 int lockfd = -1; /* file lock */
170 struct lcr_msg *message;
172 struct sched_param schedp;
173 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
174 created_lock = 0, created_signal = 0, created_debug = 0,
176 char tracetext[256], lock[128];
177 char options_error[256];
181 memset(lcr_fdset, 0, sizeof(lcr_fdset));
184 /* lock LCR process */
185 // pthread_mutex_lock(&mutex_lcr);
188 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
194 printf("Usage: lcr (query | start | fork | rules | route)\n");
195 printf("query = Show available isdn ports.\n");
196 printf("start = Run lcr normally, abort with CTRL+C.\n");
197 printf("fork = Do daemon fork and run as background process.\n");
198 printf("interface = Get help of available interface syntax.\n");
199 printf("rules = Get help of available routing rule syntax.\n");
200 printf("rules [action] = Get individual help for given action.\n");
201 // printf("route = Show current routing as it is parsed.\n");
211 if (pthread_mutex_init(&mutexd, NULL)) {
212 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
216 // if (pthread_mutex_init(&mutext, NULL)) {
217 // fprintf(stderr, "cannot create 'trace' mutex\n");
220 // created_mutext = 1;
221 if (pthread_mutex_init(&mutexe, NULL)) {
222 fprintf(stderr, "cannot create 'PERROR' mutex\n");
228 if (!(strcasecmp(argv[1],"interface"))) {
235 if (!(strcasecmp(argv[1],"rules"))) {
244 /* query available isdn ports */
245 if (!(strcasecmp(argv[1],"query"))) {
247 fprintf(stderr, "-> Using 'misdn_info'\n");
248 rc = system("misdn_info");
254 if (read_options(options_error) == 0) {
255 PERROR("%s", options_error);
260 if (mISDN_initialize() < 0)
265 /* read ruleset(s) */
266 if (!(ruleset_first = ruleset_parse()))
269 /* set pointer to main ruleset */
270 ruleset_main = getrulesetbyname("main");
272 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
273 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
278 /* query available isdn ports */
279 if (!(strcasecmp(argv[1],"route"))) {
280 ruleset_debug(ruleset_first);
286 /* do fork in special cases */
287 if (!(strcasecmp(argv[1],"fork"))) {
295 fprintf(stderr, "Cannot fork!\n");
308 fprintf(stderr, "Cannot fork!\n");
312 printf("LCR: Starting daemon.\n");
318 pidfile = fopen("/var/run/lcr.pid","w");
320 fprintf(pidfile, "%d\n", getpid());
325 if (!!strcasecmp(argv[1],"start")) {
329 /* create lock and lock! */
330 SPRINT(lock, "%s/lcr.lock", options.lock);
331 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
332 fprintf(stderr, "Cannot create lock file: %s\n", lock);
333 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
336 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
337 if (errno == EWOULDBLOCK)
338 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
339 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
344 /* initialize admin socket */
346 fprintf(stderr, "Unable to initialize admin socket.\n");
350 /* generate alaw / ulaw tables */
351 generate_tables(options.law);
354 /* init ss5 sine tables */
359 /* load tones (if requested) */
360 if (fetch_tones() == 0) {
361 fprintf(stderr, "Unable to fetch tones into memory.\n");
365 #if defined WITH_GSM_BS || defined WITH_GSM_MS
367 if (options.gsm && gsm_init()) {
368 fprintf(stderr, "GSM initialization failed.\n");
373 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm-bs or --with-gsm-ms while configure!\n");
378 if (options.gsm && gsm_bs_init()) {
379 fprintf(stderr, "GSM BS initialization failed.\n");
384 if (options.gsm && gsm_ms_init()) {
385 fprintf(stderr, "GSM MS initialization failed.\n");
390 /* read interfaces and open ports */
391 if (!read_interfaces()) {
392 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
393 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
397 interface_first = interface_newlist;
398 interface_newlist = NULL;
400 /* locking memory paging */
403 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
411 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
414 fprintf(stderr, "Warning: No permission to lock paging.\n");
417 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
420 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
424 /* set real time scheduler & priority */
425 if (options.schedule > 1) {
426 memset(&schedp, 0, sizeof(schedp));
427 schedp.sched_priority = options.schedule;
428 ret = sched_setscheduler(0, SCHED_RR, &schedp);
430 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
435 /* signal handlers */
436 signal(SIGINT,sighandler);
437 signal(SIGHUP,sighandler);
438 signal(SIGTERM,sighandler);
439 signal(SIGPIPE,sighandler);
446 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
447 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
448 printf("%s\n", tracetext);
451 #if defined WITH_GSM_BS || defined WITH_GSM_MS
455 #if defined WITH_GSM_BS || defined WITH_GSM_MS
458 if (now_d-last_d > 1.0) {
459 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
461 /* all loops must be counted from the beginning since nodes might get freed during handler */
464 /* must be processed after all queues, so they are empty */
465 if (select_main(1, NULL, NULL, NULL))
485 if (!select_main(1, NULL, NULL, NULL))
488 select_main(0, NULL, NULL, NULL);
491 SPRINT(tracetext, "%s terminated", NAME);
492 printf("%s\n", tracetext);
493 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
495 add_trace((char *)"signal", NULL, "%d", quit);
497 add_trace((char *)"errno", NULL, "%d", quit);
501 /* clean messacleane */
507 /* set scheduler & priority
509 if (options.schedule > 1) {
510 memset(&schedp, 0, sizeof(schedp));
511 schedp.sched_priority = options.schedule;
512 sched_setscheduler(0, SCHED_OTHER, &schedp);
515 if (created_signal) {
516 signal(SIGINT,SIG_DFL);
517 signal(SIGHUP,SIG_DFL);
518 signal(SIGTERM,SIG_DFL);
519 signal(SIGPIPE,SIG_DFL);
522 /* destroy objects */
528 while(epoint_first) {
536 /* free interfaces */
538 free_interfaces(interface_first);
539 interface_first = NULL;
541 /* close isdn ports */
542 mISDNport_close_all();
547 while ((message = message_get())) {
549 message_free(message);
552 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
559 /* free admin socket */
564 flock(lockfd, LOCK_UN);
573 ruleset_free(ruleset_first);
574 ruleset_first = NULL;
578 if (pthread_mutex_destroy(&mutexe))
579 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
580 // if (created_mutext)
581 // if (pthread_mutex_destroy(&mutext))
582 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
584 if (pthread_mutex_destroy(&mutexd))
585 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
587 /* deinitialize mISDN */
589 mISDN_deinitialize();
599 #if defined WITH_GSM_BS || defined WITH_GSM_MS
604 /* display memory leak */
605 #define MEMCHECK(a, b) \
607 SPRINT(tracetext, a, NAME); \
608 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
609 if (ret) add_trace("blocks", NULL, "%d", b); \
611 printf("\n******************************\n\007"); \
612 printf("\nERROR: %d %s\n", b, a); \
613 printf("\n******************************\n"); \
617 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
618 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
619 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
620 MEMCHECK("memory block(s) left (message.c)",mmemuse)
621 MEMCHECK("memory block(s) left (route.c)",rmemuse)
622 MEMCHECK("memory block(s) left (args)",amemuse)
623 MEMCHECK("class(es) left",classuse)
624 MEMCHECK("file descriptor(s) left",fduse)
625 MEMCHECK("file handler(s) left",fhuse)
627 /* unlock LCR process */
628 // pthread_mutex_unlock(&mutex_lcr);