1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
17 struct timeval now_tv;
18 struct timezone now_tz;
21 gettimeofday(&now_tv, &now_tz); \
22 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
25 FILE *debug_fp = NULL;
29 struct lcr_fdset lcr_fdset[FD_SETSIZE];
32 pthread_mutex_t mutexd; // debug output mutex
33 pthread_mutex_t mutext; // trace output mutex
34 pthread_mutex_t mutexe; // error output mutex
35 //pthread_mutex_t mutex_lcr; // lcr process mutex
50 int debug_newline = 1;
53 void debug(const char *function, int line, const char *prefix, char *buffer)
58 /* if we have a new debug count, we add a mark */
59 if (last_debug != debug_count) {
60 last_debug = debug_count;
62 now_tm = localtime(&now);
64 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);
66 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);
71 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
73 printf("(in %s() line %d): %s", function, line, buffer);
81 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
83 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
89 if (buffer[strlen(buffer)-1] == '\n')
94 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
99 if (!(options.deb & mask))
101 pthread_mutex_lock(&mutexd);
104 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
105 buffer[sizeof(buffer)-1]=0;
108 debug(function, line, "DEBUG", buffer);
110 pthread_mutex_unlock(&mutexd);
113 void _printerror(const char *function, int line, const char *fmt, ...)
118 pthread_mutex_lock(&mutexe);
121 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
122 buffer[sizeof(buffer)-1]=0;
126 debug(function, line, "ERROR", buffer);
127 else { /* only if we do not debug */
129 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
131 fprintf(stderr, "ERROR %s", buffer);
134 pthread_mutex_unlock(&mutexe);
138 void sighandler(int sigset)
140 struct sched_param schedp;
142 if (sigset == SIGHUP)
144 if (sigset == SIGPIPE)
148 /* set scheduler & priority */
149 if (options.schedule > 1) {
150 memset(&schedp, 0, sizeof(schedp));
151 schedp.sched_priority = 0;
152 sched_setscheduler(0, SCHED_OTHER, &schedp);
154 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
155 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
163 int main(int argc, char *argv[])
165 #if defined WITH_GSM_BS || defined WITH_GSM_MS
166 double now_d, last_d;
170 int lockfd = -1; /* file lock */
171 struct lcr_msg *message;
173 struct sched_param schedp;
174 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
175 created_lock = 0, created_signal = 0, created_debug = 0,
177 char tracetext[256], lock[128];
178 char options_error[256];
182 memset(lcr_fdset, 0, sizeof(lcr_fdset));
185 /* lock LCR process */
186 // pthread_mutex_lock(&mutex_lcr);
189 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
195 printf("Usage: lcr (query | start | fork | rules | route)\n");
196 printf("query = Show available isdn ports.\n");
197 printf("start = Run lcr normally, abort with CTRL+C.\n");
198 printf("fork = Do daemon fork and run as background process.\n");
199 printf("interface = Get help of available interface syntax.\n");
200 printf("rules = Get help of available routing rule syntax.\n");
201 printf("rules [action] = Get individual help for given action.\n");
202 // printf("route = Show current routing as it is parsed.\n");
212 if (pthread_mutex_init(&mutexd, NULL)) {
213 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
217 // if (pthread_mutex_init(&mutext, NULL)) {
218 // fprintf(stderr, "cannot create 'trace' mutex\n");
221 // created_mutext = 1;
222 if (pthread_mutex_init(&mutexe, NULL)) {
223 fprintf(stderr, "cannot create 'PERROR' mutex\n");
229 if (!(strcasecmp(argv[1],"interface"))) {
236 if (!(strcasecmp(argv[1],"rules"))) {
245 /* query available isdn ports */
246 if (!(strcasecmp(argv[1],"query"))) {
248 fprintf(stderr, "-> Using 'misdn_info'\n");
249 rc = system("misdn_info");
255 if (read_options(options_error) == 0) {
256 PERROR("%s", options_error);
261 if (mISDN_initialize() < 0)
266 /* read ruleset(s) */
267 if (!(ruleset_first = ruleset_parse()))
270 /* set pointer to main ruleset */
271 ruleset_main = getrulesetbyname("main");
273 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
274 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
279 /* query available isdn ports */
280 if (!(strcasecmp(argv[1],"route"))) {
281 ruleset_debug(ruleset_first);
287 /* do fork in special cases */
288 if (!(strcasecmp(argv[1],"fork"))) {
296 fprintf(stderr, "Cannot fork!\n");
309 fprintf(stderr, "Cannot fork!\n");
313 printf("LCR: Starting daemon.\n");
319 pidfile = fopen("/var/run/lcr.pid","w");
321 fprintf(pidfile, "%d\n", getpid());
326 if (!!strcasecmp(argv[1],"start")) {
330 /* create lock and lock! */
331 SPRINT(lock, "%s/lcr.lock", options.lock);
332 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
333 fprintf(stderr, "Cannot create lock file: %s\n", lock);
334 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
337 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
338 if (errno == EWOULDBLOCK)
339 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
340 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
345 /* initialize admin socket */
347 fprintf(stderr, "Unable to initialize admin socket.\n");
351 /* generate alaw / ulaw tables */
352 generate_tables(options.law);
355 /* init ss5 sine tables */
360 /* load tones (if requested) */
361 if (fetch_tones() == 0) {
362 fprintf(stderr, "Unable to fetch tones into memory.\n");
366 #if defined WITH_GSM_BS || defined WITH_GSM_MS
368 if (options.gsm && gsm_init()) {
369 fprintf(stderr, "GSM initialization failed.\n");
374 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm-bs or --with-gsm-ms while configure!\n");
379 if (options.gsm && gsm_bs_init()) {
380 fprintf(stderr, "GSM BS initialization failed.\n");
385 if (options.gsm && gsm_ms_init()) {
386 fprintf(stderr, "GSM MS initialization failed.\n");
391 /* read interfaces and open ports */
392 if (!read_interfaces()) {
393 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
394 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
398 interface_first = interface_newlist;
399 interface_newlist = NULL;
401 /* locking memory paging */
404 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
412 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
415 fprintf(stderr, "Warning: No permission to lock paging.\n");
418 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
421 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
425 /* set real time scheduler & priority */
426 if (options.schedule > 1) {
427 memset(&schedp, 0, sizeof(schedp));
428 schedp.sched_priority = options.schedule;
429 ret = sched_setscheduler(0, SCHED_RR, &schedp);
431 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
436 /* signal handlers */
437 signal(SIGINT,sighandler);
438 signal(SIGHUP,sighandler);
439 signal(SIGTERM,sighandler);
440 signal(SIGPIPE,sighandler);
447 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
448 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
449 printf("%s\n", tracetext);
452 #if defined WITH_GSM_BS || defined WITH_GSM_MS
456 #if defined WITH_GSM_BS || defined WITH_GSM_MS
459 if (now_d-last_d > 1.0) {
460 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
462 /* all loops must be counted from the beginning since nodes might get freed during handler */
465 /* must be processed after all queues, so they are empty */
466 if (select_main(1, NULL, NULL, NULL))
486 if (!select_main(1, NULL, NULL, NULL))
489 select_main(0, NULL, NULL, NULL);
492 SPRINT(tracetext, "%s terminated", NAME);
493 printf("%s\n", tracetext);
494 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
496 add_trace((char *)"signal", NULL, "%d", quit);
498 add_trace((char *)"errno", NULL, "%d", quit);
502 /* clean messacleane */
508 /* set scheduler & priority
510 if (options.schedule > 1) {
511 memset(&schedp, 0, sizeof(schedp));
512 schedp.sched_priority = options.schedule;
513 sched_setscheduler(0, SCHED_OTHER, &schedp);
516 if (created_signal) {
517 signal(SIGINT,SIG_DFL);
518 signal(SIGHUP,SIG_DFL);
519 signal(SIGTERM,SIG_DFL);
520 signal(SIGPIPE,SIG_DFL);
523 /* destroy objects */
529 while(epoint_first) {
537 /* free interfaces */
539 free_interfaces(interface_first);
540 interface_first = NULL;
542 /* close isdn ports */
543 mISDNport_close_all();
548 while ((message = message_get())) {
550 message_free(message);
553 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
560 /* free admin socket */
565 flock(lockfd, LOCK_UN);
574 ruleset_free(ruleset_first);
575 ruleset_first = NULL;
579 if (pthread_mutex_destroy(&mutexe))
580 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
581 // if (created_mutext)
582 // if (pthread_mutex_destroy(&mutext))
583 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
585 if (pthread_mutex_destroy(&mutexd))
586 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
588 /* deinitialize mISDN */
590 mISDN_deinitialize();
600 #if defined WITH_GSM_BS || defined WITH_GSM_MS
605 /* display memory leak */
606 #define MEMCHECK(a, b) \
608 SPRINT(tracetext, a, NAME); \
609 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
610 if (ret) add_trace("blocks", NULL, "%d", b); \
612 printf("\n******************************\n\007"); \
613 printf("\nERROR: %d %s\n", b, a); \
614 printf("\n******************************\n"); \
618 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
619 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
620 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
621 MEMCHECK("memory block(s) left (message.c)",mmemuse)
622 MEMCHECK("memory block(s) left (route.c)",rmemuse)
623 MEMCHECK("memory block(s) left (args)",amemuse)
624 MEMCHECK("class(es) left",classuse)
625 MEMCHECK("file descriptor(s) left",fduse)
626 MEMCHECK("file handler(s) left",fhuse)
628 /* unlock LCR process */
629 // pthread_mutex_unlock(&mutex_lcr);