1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
13 #ifdef PACKAGE_VERSION
14 #undef PACKAGE_VERSION
20 struct timeval now_tv;
21 struct timezone now_tz;
24 gettimeofday(&now_tv, &now_tz); \
25 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
28 FILE *debug_fp = NULL;
32 struct lcr_fdset lcr_fdset[FD_SETSIZE];
35 pthread_mutex_t mutexd; // debug output mutex
36 pthread_mutex_t mutext; // trace output mutex
37 pthread_mutex_t mutexe; // error output mutex
38 //pthread_mutex_t mutex_lcr; // lcr process mutex
53 int debug_newline = 1;
56 void debug(const char *function, int line, const char *prefix, char *buffer)
61 /* if we have a new debug count, we add a mark */
62 if (last_debug != debug_count) {
63 last_debug = debug_count;
65 now_tm = localtime(&now);
67 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);
69 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);
74 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
76 printf("(in %s() line %d): %s", function, line, buffer);
84 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
86 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
93 if (buffer[strlen(buffer)-1] == '\n')
98 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
103 if (!(options.deb & mask))
105 pthread_mutex_lock(&mutexd);
108 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
109 buffer[sizeof(buffer)-1]=0;
112 debug(function, line, "DEBUG", buffer);
114 pthread_mutex_unlock(&mutexd);
117 void _printerror(const char *function, int line, const char *fmt, ...)
122 pthread_mutex_lock(&mutexe);
125 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
126 buffer[sizeof(buffer)-1]=0;
130 debug(function, line, "ERROR", buffer);
131 else { /* only if we do not debug */
133 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
135 fprintf(stderr, "ERROR %s", buffer);
138 pthread_mutex_unlock(&mutexe);
142 void sighandler(int sigset)
144 struct sched_param schedp;
146 if (sigset == SIGHUP)
148 if (sigset == SIGPIPE)
150 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
151 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
154 /* set scheduler & priority */
155 if (options.schedule > 1) {
156 memset(&schedp, 0, sizeof(schedp));
157 schedp.sched_priority = 0;
158 sched_setscheduler(0, SCHED_OTHER, &schedp);
167 int main(int argc, char *argv[])
170 double now_d, last_d;
174 int lockfd = -1; /* file lock */
175 struct lcr_msg *message;
177 struct sched_param schedp;
178 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
179 created_lock = 0, created_signal = 0, created_debug = 0,
181 char tracetext[256], lock[128];
182 char options_error[256];
186 memset(lcr_fdset, 0, sizeof(lcr_fdset));
189 /* lock LCR process */
190 // pthread_mutex_lock(&mutex_lcr);
193 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
199 printf("Usage: lcr (query | start | fork | rules | route)\n");
200 printf("query = Show available isdn ports.\n");
201 printf("start = Run lcr normally, abort with CTRL+C.\n");
202 printf("fork = Do daemon fork and run as background process.\n");
203 printf("interface = Get help of available interface syntax.\n");
204 printf("rules = Get help of available routing rule syntax.\n");
205 printf("rules [action] = Get individual help for given action.\n");
206 // printf("route = Show current routing as it is parsed.\n");
216 if (pthread_mutex_init(&mutexd, NULL)) {
217 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
221 // if (pthread_mutex_init(&mutext, NULL)) {
222 // fprintf(stderr, "cannot create 'trace' mutex\n");
225 // created_mutext = 1;
226 if (pthread_mutex_init(&mutexe, NULL)) {
227 fprintf(stderr, "cannot create 'PERROR' mutex\n");
233 if (!(strcasecmp(argv[1],"interface"))) {
240 if (!(strcasecmp(argv[1],"rules"))) {
249 /* query available isdn ports */
250 if (!(strcasecmp(argv[1],"query"))) {
252 fprintf(stderr, "-> Using 'misdn_info'\n");
253 rc = system("misdn_info");
259 if (read_options(options_error) == 0) {
260 PERROR("%s", options_error);
265 if (mISDN_initialize() < 0)
270 /* read ruleset(s) */
271 if (!(ruleset_first = ruleset_parse()))
274 /* set pointer to main ruleset */
275 ruleset_main = getrulesetbyname("main");
277 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
278 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
283 /* query available isdn ports */
284 if (!(strcasecmp(argv[1],"route"))) {
285 ruleset_debug(ruleset_first);
291 /* do fork in special cases */
292 if (!(strcasecmp(argv[1],"fork"))) {
300 fprintf(stderr, "Cannot fork!\n");
313 fprintf(stderr, "Cannot fork!\n");
317 printf("LCR: Starting daemon.\n");
323 pidfile = fopen("/var/run/lcr.pid","w");
325 fprintf(pidfile, "%d\n", getpid());
330 if (!!strcasecmp(argv[1],"start")) {
334 /* create lock and lock! */
335 SPRINT(lock, "%s/lcr.lock", options.lock);
336 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
337 fprintf(stderr, "Cannot create lock file: %s\n", lock);
338 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
341 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
342 if (errno == EWOULDBLOCK)
343 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
344 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
349 /* initialize admin socket */
351 fprintf(stderr, "Unable to initialize admin socket.\n");
355 /* generate alaw / ulaw tables */
356 generate_tables(options.law);
359 /* init ss5 sine tables */
364 /* load tones (if requested) */
365 if (fetch_tones() == 0) {
366 fprintf(stderr, "Unable to fetch tones into memory.\n");
370 #if defined WITH_GSM_BS || defined WITH_GSM_MS
373 fprintf(stderr, "GSM initialization failed.\n");
378 init is done when interface is up
381 fprintf(stderr, "GSM BS initialization failed.\n");
388 fprintf(stderr, "GSM MS initialization failed.\n");
393 /* read interfaces and open ports */
394 if (!read_interfaces()) {
395 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
396 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
400 interface_first = interface_newlist;
401 interface_newlist = NULL;
403 /* locking memory paging */
406 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
414 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
417 fprintf(stderr, "Warning: No permission to lock paging.\n");
420 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
423 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
427 /* set real time scheduler & priority */
428 if (options.schedule > 1) {
429 memset(&schedp, 0, sizeof(schedp));
430 schedp.sched_priority = options.schedule;
431 ret = sched_setscheduler(0, SCHED_RR, &schedp);
433 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
438 /* signal handlers */
439 signal(SIGINT,sighandler);
440 signal(SIGHUP,sighandler);
441 signal(SIGTERM,sighandler);
442 signal(SIGPIPE,sighandler);
449 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
450 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
451 printf("%s\n", tracetext);
461 if (now_d-last_d > 1.0) {
462 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
464 /* all loops must be counted from the beginning since nodes might get freed during handler */
467 /* must be processed after all queues, so they are empty */
468 if (select_main(1, NULL, NULL, NULL))
474 if (options.polling) {
475 if (!select_main(1, NULL, NULL, NULL))
478 select_main(0, NULL, NULL, NULL);
481 SPRINT(tracetext, "%s terminated", NAME);
482 printf("%s\n", tracetext);
483 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
485 add_trace((char *)"signal", NULL, "%d", quit);
487 add_trace((char *)"errno", NULL, "%d", quit);
491 /* clean messacleane */
497 /* set scheduler & priority
499 if (options.schedule > 1) {
500 memset(&schedp, 0, sizeof(schedp));
501 schedp.sched_priority = options.schedule;
502 sched_setscheduler(0, SCHED_OTHER, &schedp);
505 if (created_signal) {
506 signal(SIGINT,SIG_DFL);
507 signal(SIGHUP,SIG_DFL);
508 signal(SIGTERM,SIG_DFL);
509 signal(SIGPIPE,SIG_DFL);
512 /* destroy objects */
518 while(epoint_first) {
526 /* free interfaces */
528 free_interfaces(interface_first);
529 interface_first = NULL;
531 /* close isdn ports */
532 mISDNport_close_all();
537 while ((message = message_get())) {
539 message_free(message);
542 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
549 /* free admin socket */
554 flock(lockfd, LOCK_UN);
563 ruleset_free(ruleset_first);
564 ruleset_first = NULL;
568 if (pthread_mutex_destroy(&mutexe))
569 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
570 // if (created_mutext)
571 // if (pthread_mutex_destroy(&mutext))
572 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
574 if (pthread_mutex_destroy(&mutexd))
575 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
577 /* deinitialize mISDN */
579 mISDN_deinitialize();
583 exit is done when interface is down
591 #if defined WITH_GSM_BS || defined WITH_GSM_MS
595 /* close loopback, if used by GSM or remote */
596 if (mISDNloop.sock > -1)
599 /* display memory leak */
600 #define MEMCHECK(a, b) \
602 SPRINT(tracetext, a, NAME); \
603 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
604 if (ret) add_trace("blocks", NULL, "%d", b); \
606 printf("\n******************************\n\007"); \
607 printf("\nERROR: %d %s\n", b, a); \
608 printf("\n******************************\n"); \
612 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
613 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
614 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
615 MEMCHECK("memory block(s) left (message.c)",mmemuse)
616 MEMCHECK("memory block(s) left (route.c)",rmemuse)
617 MEMCHECK("memory block(s) left (args)",amemuse)
618 MEMCHECK("class(es) left",classuse)
619 MEMCHECK("file descriptor(s) left",fduse)
620 MEMCHECK("file handler(s) left",fhuse)
622 /* unlock LCR process */
623 // pthread_mutex_unlock(&mutex_lcr);