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[])
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];
180 memset(lcr_fdset, 0, sizeof(lcr_fdset));
183 /* lock LCR process */
184 // pthread_mutex_lock(&mutex_lcr);
187 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
193 printf("Usage: lcr (query | start | fork | rules | route)\n");
194 printf("query = Show available isdn ports.\n");
195 printf("start = Run lcr normally, abort with CTRL+C.\n");
196 printf("fork = Do daemon fork and run as background process.\n");
197 printf("interface = Get help of available interface syntax.\n");
198 printf("rules = Get help of available routing rule syntax.\n");
199 printf("rules [action] = Get individual help for given action.\n");
200 // printf("route = Show current routing as it is parsed.\n");
210 if (pthread_mutex_init(&mutexd, NULL)) {
211 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
215 // if (pthread_mutex_init(&mutext, NULL)) {
216 // fprintf(stderr, "cannot create 'trace' mutex\n");
219 // created_mutext = 1;
220 if (pthread_mutex_init(&mutexe, NULL)) {
221 fprintf(stderr, "cannot create 'PERROR' mutex\n");
227 if (!(strcasecmp(argv[1],"interface"))) {
234 if (!(strcasecmp(argv[1],"rules"))) {
243 /* query available isdn ports */
244 if (!(strcasecmp(argv[1],"query"))) {
246 fprintf(stderr, "-> Using 'misdn_info'\n");
247 rc = system("misdn_info");
253 if (read_options() == 0) {
254 PERROR("%s", options_error);
259 if (mISDN_initialize() < 0)
264 /* read ruleset(s) */
265 if (!(ruleset_first = ruleset_parse()))
268 /* set pointer to main ruleset */
269 ruleset_main = getrulesetbyname("main");
271 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
272 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
277 /* query available isdn ports */
278 if (!(strcasecmp(argv[1],"route"))) {
279 ruleset_debug(ruleset_first);
285 /* do fork in special cases */
286 if (!(strcasecmp(argv[1],"fork"))) {
294 fprintf(stderr, "Cannot fork!\n");
307 fprintf(stderr, "Cannot fork!\n");
311 printf("LCR: Starting daemon.\n");
317 pidfile = fopen("/var/run/lcr.pid","w");
319 fprintf(pidfile, "%d\n", getpid());
324 if (!!strcasecmp(argv[1],"start")) {
328 /* create lock and lock! */
329 SPRINT(lock, "%s/lcr.lock", options.lock);
330 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
331 fprintf(stderr, "Cannot create lock file: %s\n", lock);
332 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
335 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
336 if (errno == EWOULDBLOCK)
337 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
338 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
343 /* initialize admin socket */
345 fprintf(stderr, "Unable to initialize admin socket.\n");
349 /* generate alaw / ulaw tables */
350 generate_tables(options.law);
353 /* init ss5 sine tables */
358 /* load tones (if requested) */
359 if (fetch_tones() == 0) {
360 fprintf(stderr, "Unable to fetch tones into memory.\n");
366 if (options.gsm && gsm_init()) {
367 fprintf(stderr, "GSM initialization failed.\n");
372 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm while configure!\n");
377 /* read interfaces and open ports */
378 if (!read_interfaces()) {
379 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
380 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
384 interface_first = interface_newlist;
385 interface_newlist = NULL;
387 /* locking memory paging */
390 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
398 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
401 fprintf(stderr, "Warning: No permission to lock paging.\n");
404 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
407 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
411 /* set real time scheduler & priority */
412 if (options.schedule > 1) {
413 memset(&schedp, 0, sizeof(schedp));
414 schedp.sched_priority = options.schedule;
415 ret = sched_setscheduler(0, SCHED_RR, &schedp);
417 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
422 /* signal handlers */
423 signal(SIGINT,sighandler);
424 signal(SIGHUP,sighandler);
425 signal(SIGTERM,sighandler);
426 signal(SIGPIPE,sighandler);
433 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
434 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
435 printf("%s\n", tracetext);
445 if (now_d-last_d > 1.0) {
446 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
448 /* all loops must be counted from the beginning since nodes might get freed during handler */
451 /* must be processed after all queues, so they are empty */
452 if (select_main(1, NULL, NULL, NULL))
462 select_main(0, NULL, NULL, NULL);
465 SPRINT(tracetext, "%s terminated", NAME);
466 printf("%s\n", tracetext);
467 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
469 add_trace((char *)"signal", NULL, "%d", quit);
473 /* clean messacleane */
479 /* set scheduler & priority
481 if (options.schedule > 1) {
482 memset(&schedp, 0, sizeof(schedp));
483 schedp.sched_priority = options.schedule;
484 sched_setscheduler(0, SCHED_OTHER, &schedp);
487 if (created_signal) {
488 signal(SIGINT,SIG_DFL);
489 signal(SIGHUP,SIG_DFL);
490 signal(SIGTERM,SIG_DFL);
491 signal(SIGPIPE,SIG_DFL);
494 /* destroy objects */
500 while(epoint_first) {
508 /* free interfaces */
510 free_interfaces(interface_first);
511 interface_first = NULL;
513 /* close isdn ports */
514 mISDNport_close_all();
519 while ((message = message_get())) {
521 message_free(message);
524 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
531 /* free admin socket */
536 flock(lockfd, LOCK_UN);
545 ruleset_free(ruleset_first);
546 ruleset_first = NULL;
550 if (pthread_mutex_destroy(&mutexe))
551 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
552 // if (created_mutext)
553 // if (pthread_mutex_destroy(&mutext))
554 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
556 if (pthread_mutex_destroy(&mutexd))
557 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
559 /* deinitialize mISDN */
561 mISDN_deinitialize();
569 /* display memory leak */
570 #define MEMCHECK(a, b) \
572 SPRINT(tracetext, a, NAME); \
573 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
574 if (ret) add_trace("blocks", NULL, "%d", b); \
576 printf("\n******************************\n\007"); \
577 printf("\nERROR: %d %s\n", b, a); \
578 printf("\n******************************\n"); \
582 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
583 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
584 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
585 MEMCHECK("memory block(s) left (message.c)",mmemuse)
586 MEMCHECK("memory block(s) left (route.c)",rmemuse)
587 MEMCHECK("memory block(s) left (args)",amemuse)
588 MEMCHECK("class(es) left",classuse)
589 MEMCHECK("file descriptor(s) left",fduse)
590 MEMCHECK("file handler(s) left",fhuse)
592 /* unlock LCR process */
593 // pthread_mutex_unlock(&mutex_lcr);