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);
92 if (buffer[strlen(buffer)-1] == '\n')
97 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
102 if (!(options.deb & mask))
104 pthread_mutex_lock(&mutexd);
107 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
108 buffer[sizeof(buffer)-1]=0;
111 debug(function, line, "DEBUG", buffer);
113 pthread_mutex_unlock(&mutexd);
116 void _printerror(const char *function, int line, const char *fmt, ...)
121 pthread_mutex_lock(&mutexe);
124 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
125 buffer[sizeof(buffer)-1]=0;
129 debug(function, line, "ERROR", buffer);
130 else { /* only if we do not debug */
132 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
134 fprintf(stderr, "ERROR %s", buffer);
137 pthread_mutex_unlock(&mutexe);
141 void sighandler(int sigset)
143 struct sched_param schedp;
145 if (sigset == SIGHUP)
147 if (sigset == SIGPIPE)
149 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
150 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
153 /* set scheduler & priority */
154 if (options.schedule > 1) {
155 memset(&schedp, 0, sizeof(schedp));
156 schedp.sched_priority = 0;
157 sched_setscheduler(0, SCHED_OTHER, &schedp);
166 int main(int argc, char *argv[])
169 double now_d, last_d;
173 int lockfd = -1; /* file lock */
174 struct lcr_msg *message;
176 struct sched_param schedp;
177 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
178 created_lock = 0, created_signal = 0, created_debug = 0,
180 char tracetext[256], lock[128];
181 char options_error[256];
185 memset(lcr_fdset, 0, sizeof(lcr_fdset));
188 /* lock LCR process */
189 // pthread_mutex_lock(&mutex_lcr);
192 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
198 printf("Usage: lcr (query | start | fork | rules | route)\n");
199 printf("query = Show available isdn ports.\n");
200 printf("start = Run lcr normally, abort with CTRL+C.\n");
201 printf("fork = Do daemon fork and run as background process.\n");
202 printf("interface = Get help of available interface syntax.\n");
203 printf("rules = Get help of available routing rule syntax.\n");
204 printf("rules [action] = Get individual help for given action.\n");
205 // printf("route = Show current routing as it is parsed.\n");
215 if (pthread_mutex_init(&mutexd, NULL)) {
216 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
220 // if (pthread_mutex_init(&mutext, NULL)) {
221 // fprintf(stderr, "cannot create 'trace' mutex\n");
224 // created_mutext = 1;
225 if (pthread_mutex_init(&mutexe, NULL)) {
226 fprintf(stderr, "cannot create 'PERROR' mutex\n");
232 if (!(strcasecmp(argv[1],"interface"))) {
239 if (!(strcasecmp(argv[1],"rules"))) {
248 /* query available isdn ports */
249 if (!(strcasecmp(argv[1],"query"))) {
251 fprintf(stderr, "-> Using 'misdn_info'\n");
252 rc = system("misdn_info");
258 if (read_options(options_error) == 0) {
259 PERROR("%s", options_error);
264 if (mISDN_initialize() < 0)
269 /* read ruleset(s) */
270 if (!(ruleset_first = ruleset_parse()))
273 /* set pointer to main ruleset */
274 ruleset_main = getrulesetbyname("main");
276 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
277 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
282 /* query available isdn ports */
283 if (!(strcasecmp(argv[1],"route"))) {
284 ruleset_debug(ruleset_first);
290 /* do fork in special cases */
291 if (!(strcasecmp(argv[1],"fork"))) {
299 fprintf(stderr, "Cannot fork!\n");
312 fprintf(stderr, "Cannot fork!\n");
316 printf("LCR: Starting daemon.\n");
322 pidfile = fopen("/var/run/lcr.pid","w");
324 fprintf(pidfile, "%d\n", getpid());
329 if (!!strcasecmp(argv[1],"start")) {
333 /* create lock and lock! */
334 SPRINT(lock, "%s/lcr.lock", options.lock);
335 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
336 fprintf(stderr, "Cannot create lock file: %s\n", lock);
337 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
340 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
341 if (errno == EWOULDBLOCK)
342 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
343 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
348 /* initialize admin socket */
350 fprintf(stderr, "Unable to initialize admin socket.\n");
354 /* generate alaw / ulaw tables */
355 generate_tables(options.law);
358 /* init ss5 sine tables */
363 /* load tones (if requested) */
364 if (fetch_tones() == 0) {
365 fprintf(stderr, "Unable to fetch tones into memory.\n");
369 #if defined WITH_GSM_BS || defined WITH_GSM_MS
372 fprintf(stderr, "GSM initialization failed.\n");
377 init is done when interface is up
380 fprintf(stderr, "GSM BS initialization failed.\n");
387 fprintf(stderr, "GSM MS initialization failed.\n");
392 /* read interfaces and open ports */
393 if (!read_interfaces()) {
394 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
395 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
399 interface_first = interface_newlist;
400 interface_newlist = NULL;
402 /* locking memory paging */
405 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
413 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
416 fprintf(stderr, "Warning: No permission to lock paging.\n");
419 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
422 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
426 /* set real time scheduler & priority */
427 if (options.schedule > 1) {
428 memset(&schedp, 0, sizeof(schedp));
429 schedp.sched_priority = options.schedule;
430 ret = sched_setscheduler(0, SCHED_RR, &schedp);
432 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
437 /* signal handlers */
438 signal(SIGINT,sighandler);
439 signal(SIGHUP,sighandler);
440 signal(SIGTERM,sighandler);
441 signal(SIGPIPE,sighandler);
448 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
449 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
450 printf("%s\n", tracetext);
460 if (now_d-last_d > 1.0) {
461 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
463 /* all loops must be counted from the beginning since nodes might get freed during handler */
466 /* must be processed after all queues, so they are empty */
467 if (select_main(1, NULL, NULL, NULL))
473 if (options.polling) {
474 if (!select_main(1, NULL, NULL, NULL))
477 select_main(0, NULL, NULL, NULL);
480 SPRINT(tracetext, "%s terminated", NAME);
481 printf("%s\n", tracetext);
482 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
484 add_trace((char *)"signal", NULL, "%d", quit);
486 add_trace((char *)"errno", NULL, "%d", quit);
490 /* clean messacleane */
496 /* set scheduler & priority
498 if (options.schedule > 1) {
499 memset(&schedp, 0, sizeof(schedp));
500 schedp.sched_priority = options.schedule;
501 sched_setscheduler(0, SCHED_OTHER, &schedp);
504 if (created_signal) {
505 signal(SIGINT,SIG_DFL);
506 signal(SIGHUP,SIG_DFL);
507 signal(SIGTERM,SIG_DFL);
508 signal(SIGPIPE,SIG_DFL);
511 /* destroy objects */
517 while(epoint_first) {
525 /* free interfaces */
527 free_interfaces(interface_first);
528 interface_first = NULL;
530 /* close isdn ports */
531 mISDNport_close_all();
536 while ((message = message_get())) {
538 message_free(message);
541 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
548 /* free admin socket */
553 flock(lockfd, LOCK_UN);
562 ruleset_free(ruleset_first);
563 ruleset_first = NULL;
567 if (pthread_mutex_destroy(&mutexe))
568 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
569 // if (created_mutext)
570 // if (pthread_mutex_destroy(&mutext))
571 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
573 if (pthread_mutex_destroy(&mutexd))
574 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
576 /* deinitialize mISDN */
578 mISDN_deinitialize();
582 exit is done when interface is down
590 #if defined WITH_GSM_BS || defined WITH_GSM_MS
594 /* close loopback, if used by GSM or remote */
595 if (mISDNloop.sock > -1)
598 /* display memory leak */
599 #define MEMCHECK(a, b) \
601 SPRINT(tracetext, a, NAME); \
602 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
603 if (ret) add_trace("blocks", NULL, "%d", b); \
605 printf("\n******************************\n\007"); \
606 printf("\nERROR: %d %s\n", b, a); \
607 printf("\n******************************\n"); \
611 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
612 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
613 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
614 MEMCHECK("memory block(s) left (message.c)",mmemuse)
615 MEMCHECK("memory block(s) left (route.c)",rmemuse)
616 MEMCHECK("memory block(s) left (args)",amemuse)
617 MEMCHECK("class(es) left",classuse)
618 MEMCHECK("file descriptor(s) left",fduse)
619 MEMCHECK("file handler(s) left",fhuse)
621 /* unlock LCR process */
622 // pthread_mutex_unlock(&mutex_lcr);