1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
13 #ifdef PACKAGE_VERSION
14 #undef PACKAGE_VERSION
19 #include <osmocom/core/signal.h>
25 struct timeval now_tv;
26 struct timezone now_tz;
29 gettimeofday(&now_tv, &now_tz); \
30 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
33 FILE *debug_fp = NULL;
37 struct lcr_fdset lcr_fdset[FD_SETSIZE];
40 pthread_mutex_t mutexd; // debug output mutex
41 pthread_mutex_t mutext; // trace output mutex
42 pthread_mutex_t mutexe; // error output mutex
43 //pthread_mutex_t mutex_lcr; // lcr process mutex
58 int debug_newline = 1;
61 void debug(const char *function, int line, const char *prefix, char *buffer)
66 /* if we have a new debug count, we add a mark */
67 if (last_debug != debug_count) {
68 last_debug = debug_count;
70 now_tm = localtime(&now);
72 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);
74 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);
79 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
81 printf("(in %s() line %d): %s", function, line, buffer);
89 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
91 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
97 if (buffer[strlen(buffer)-1] == '\n')
102 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
107 if (!(options.deb & mask))
109 pthread_mutex_lock(&mutexd);
112 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
113 buffer[sizeof(buffer)-1]=0;
116 debug(function, line, "DEBUG", buffer);
118 pthread_mutex_unlock(&mutexd);
121 void _printerror(const char *function, int line, const char *fmt, ...)
126 pthread_mutex_lock(&mutexe);
129 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
130 buffer[sizeof(buffer)-1]=0;
134 debug(function, line, "ERROR", buffer);
135 else { /* only if we do not debug */
137 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
139 fprintf(stderr, "ERROR %s", buffer);
142 pthread_mutex_unlock(&mutexe);
146 void sighandler(int sigset)
148 struct sched_param schedp;
153 if (sigset == SIGHUP)
155 if (sigset == SIGPIPE)
157 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
158 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
162 osmo_signal_dispatch(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
168 /* set scheduler & priority */
169 if (options.schedule > 1) {
170 memset(&schedp, 0, sizeof(schedp));
171 schedp.sched_priority = 0;
172 sched_setscheduler(0, SCHED_OTHER, &schedp);
181 int main(int argc, char *argv[])
183 #if defined WITH_GSM_BS || defined WITH_GSM_MS
184 double now_d, last_d;
188 int lockfd = -1; /* file lock */
189 struct lcr_msg *message;
191 struct sched_param schedp;
192 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
193 created_lock = 0, created_signal = 0, created_debug = 0,
195 char tracetext[256], lock[128];
196 char options_error[256];
200 memset(lcr_fdset, 0, sizeof(lcr_fdset));
203 /* lock LCR process */
204 // pthread_mutex_lock(&mutex_lcr);
207 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
213 printf("Usage: lcr (query | start | fork | rules | route)\n");
214 printf("query = Show available isdn ports.\n");
215 printf("start = Run lcr normally, abort with CTRL+C.\n");
216 printf("fork = Do daemon fork and run as background process.\n");
217 printf("interface = Get help of available interface syntax.\n");
218 printf("rules = Get help of available routing rule syntax.\n");
219 printf("rules [action] = Get individual help for given action.\n");
220 // printf("route = Show current routing as it is parsed.\n");
230 if (pthread_mutex_init(&mutexd, NULL)) {
231 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
235 // if (pthread_mutex_init(&mutext, NULL)) {
236 // fprintf(stderr, "cannot create 'trace' mutex\n");
239 // created_mutext = 1;
240 if (pthread_mutex_init(&mutexe, NULL)) {
241 fprintf(stderr, "cannot create 'PERROR' mutex\n");
247 if (!(strcasecmp(argv[1],"interface"))) {
254 if (!(strcasecmp(argv[1],"rules"))) {
263 /* query available isdn ports */
264 if (!(strcasecmp(argv[1],"query"))) {
266 fprintf(stderr, "-> Using 'misdn_info'\n");
267 rc = system("misdn_info");
273 if (read_options(options_error) == 0) {
274 PERROR("%s", options_error);
279 if (mISDN_initialize() < 0)
284 /* read ruleset(s) */
285 if (!(ruleset_first = ruleset_parse()))
288 /* set pointer to main ruleset */
289 ruleset_main = getrulesetbyname("main");
291 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
292 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
297 /* query available isdn ports */
298 if (!(strcasecmp(argv[1],"route"))) {
299 ruleset_debug(ruleset_first);
305 /* do fork in special cases */
306 if (!(strcasecmp(argv[1],"fork"))) {
314 fprintf(stderr, "Cannot fork!\n");
327 fprintf(stderr, "Cannot fork!\n");
331 printf("LCR: Starting daemon.\n");
337 pidfile = fopen("/var/run/lcr.pid","w");
339 fprintf(pidfile, "%d\n", getpid());
344 if (!!strcasecmp(argv[1],"start")) {
348 /* create lock and lock! */
349 SPRINT(lock, "%s/lcr.lock", options.lock);
350 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
351 fprintf(stderr, "Cannot create lock file: %s\n", lock);
352 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
355 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
356 if (errno == EWOULDBLOCK)
357 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
358 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
363 /* initialize admin socket */
365 fprintf(stderr, "Unable to initialize admin socket.\n");
369 /* generate alaw / ulaw tables */
370 generate_tables(options.law);
373 /* init ss5 sine tables */
378 /* load tones (if requested) */
379 if (fetch_tones() == 0) {
380 fprintf(stderr, "Unable to fetch tones into memory.\n");
384 #if defined WITH_GSM_BS || defined WITH_GSM_MS
386 if (options.gsm && gsm_init()) {
387 fprintf(stderr, "GSM initialization failed.\n");
392 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm-bs or --with-gsm-ms while configure!\n");
397 if (options.gsm && gsm_bs_init()) {
398 fprintf(stderr, "GSM BS initialization failed.\n");
403 if (options.gsm && gsm_ms_init()) {
404 fprintf(stderr, "GSM MS initialization failed.\n");
409 /* read interfaces and open ports */
410 if (!read_interfaces()) {
411 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
412 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
416 interface_first = interface_newlist;
417 interface_newlist = NULL;
419 /* locking memory paging */
422 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
430 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
433 fprintf(stderr, "Warning: No permission to lock paging.\n");
436 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
439 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
443 /* set real time scheduler & priority */
444 if (options.schedule > 1) {
445 memset(&schedp, 0, sizeof(schedp));
446 schedp.sched_priority = options.schedule;
447 ret = sched_setscheduler(0, SCHED_RR, &schedp);
449 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
454 /* signal handlers */
455 signal(SIGINT,sighandler);
456 signal(SIGHUP,sighandler);
457 signal(SIGTERM,sighandler);
458 signal(SIGPIPE,sighandler);
465 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
466 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
467 printf("%s\n", tracetext);
470 #if defined WITH_GSM_BS || defined WITH_GSM_MS
474 #if defined WITH_GSM_BS || defined WITH_GSM_MS
477 if (now_d-last_d > 1.0) {
478 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
480 /* all loops must be counted from the beginning since nodes might get freed during handler */
483 /* must be processed after all queues, so they are empty */
484 if (select_main(1, NULL, NULL, NULL))
491 if (handle_gsm_ms(&quit))
500 if (!select_main(1, NULL, NULL, NULL))
503 select_main(0, NULL, NULL, NULL);
506 SPRINT(tracetext, "%s terminated", NAME);
507 printf("%s\n", tracetext);
508 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
510 add_trace((char *)"signal", NULL, "%d", quit);
512 add_trace((char *)"errno", NULL, "%d", quit);
516 /* clean messacleane */
522 /* set scheduler & priority
524 if (options.schedule > 1) {
525 memset(&schedp, 0, sizeof(schedp));
526 schedp.sched_priority = options.schedule;
527 sched_setscheduler(0, SCHED_OTHER, &schedp);
530 if (created_signal) {
531 signal(SIGINT,SIG_DFL);
532 signal(SIGHUP,SIG_DFL);
533 signal(SIGTERM,SIG_DFL);
534 signal(SIGPIPE,SIG_DFL);
537 /* destroy objects */
543 while(epoint_first) {
551 /* free interfaces */
553 free_interfaces(interface_first);
554 interface_first = NULL;
556 /* close isdn ports */
557 mISDNport_close_all();
562 while ((message = message_get())) {
564 message_free(message);
567 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
574 /* free admin socket */
579 flock(lockfd, LOCK_UN);
588 ruleset_free(ruleset_first);
589 ruleset_first = NULL;
593 if (pthread_mutex_destroy(&mutexe))
594 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
595 // if (created_mutext)
596 // if (pthread_mutex_destroy(&mutext))
597 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
599 if (pthread_mutex_destroy(&mutexd))
600 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
602 /* deinitialize mISDN */
604 mISDN_deinitialize();
614 #if defined WITH_GSM_BS || defined WITH_GSM_MS
619 /* close loopback, if used by GSM or remote */
620 if (mISDNloop.sock > -1)
623 /* display memory leak */
624 #define MEMCHECK(a, b) \
626 SPRINT(tracetext, a, NAME); \
627 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
628 if (ret) add_trace("blocks", NULL, "%d", b); \
630 printf("\n******************************\n\007"); \
631 printf("\nERROR: %d %s\n", b, a); \
632 printf("\n******************************\n"); \
636 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
637 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
638 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
639 MEMCHECK("memory block(s) left (message.c)",mmemuse)
640 MEMCHECK("memory block(s) left (route.c)",rmemuse)
641 MEMCHECK("memory block(s) left (args)",amemuse)
642 MEMCHECK("class(es) left",classuse)
643 MEMCHECK("file descriptor(s) left",fduse)
644 MEMCHECK("file handler(s) left",fhuse)
646 /* unlock LCR process */
647 // pthread_mutex_unlock(&mutex_lcr);