1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
16 #include <osmocore/signal.h>
22 struct timeval now_tv;
23 struct timezone now_tz;
26 gettimeofday(&now_tv, &now_tz); \
27 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
30 FILE *debug_fp = NULL;
34 struct lcr_fdset lcr_fdset[FD_SETSIZE];
37 pthread_mutex_t mutexd; // debug output mutex
38 pthread_mutex_t mutext; // trace output mutex
39 pthread_mutex_t mutexe; // error output mutex
40 //pthread_mutex_t mutex_lcr; // lcr process mutex
55 int debug_newline = 1;
58 void debug(const char *function, int line, const char *prefix, char *buffer)
63 /* if we have a new debug count, we add a mark */
64 if (last_debug != debug_count) {
65 last_debug = debug_count;
67 now_tm = localtime(&now);
69 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);
71 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);
76 printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
78 printf("(in %s() line %d): %s", function, line, buffer);
86 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
88 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
94 if (buffer[strlen(buffer)-1] == '\n')
99 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
104 if (!(options.deb & mask))
106 pthread_mutex_lock(&mutexd);
109 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
110 buffer[sizeof(buffer)-1]=0;
113 debug(function, line, "DEBUG", buffer);
115 pthread_mutex_unlock(&mutexd);
118 void _printerror(const char *function, int line, const char *fmt, ...)
123 pthread_mutex_lock(&mutexe);
126 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
127 buffer[sizeof(buffer)-1]=0;
131 debug(function, line, "ERROR", buffer);
132 else { /* only if we do not debug */
134 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
136 fprintf(stderr, "ERROR %s", buffer);
139 pthread_mutex_unlock(&mutexe);
143 void sighandler(int sigset)
145 struct sched_param schedp;
150 if (sigset == SIGHUP)
152 if (sigset == SIGPIPE)
154 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
155 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
159 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
165 /* set scheduler & priority */
166 if (options.schedule > 1) {
167 memset(&schedp, 0, sizeof(schedp));
168 schedp.sched_priority = 0;
169 sched_setscheduler(0, SCHED_OTHER, &schedp);
178 int main(int argc, char *argv[])
180 #if defined WITH_GSM_BS || defined WITH_GSM_MS
181 double now_d, last_d;
185 int lockfd = -1; /* file lock */
186 struct lcr_msg *message;
188 struct sched_param schedp;
189 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
190 created_lock = 0, created_signal = 0, created_debug = 0,
192 char tracetext[256], lock[128];
193 char options_error[256];
197 memset(lcr_fdset, 0, sizeof(lcr_fdset));
200 /* lock LCR process */
201 // pthread_mutex_lock(&mutex_lcr);
204 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
210 printf("Usage: lcr (query | start | fork | rules | route)\n");
211 printf("query = Show available isdn ports.\n");
212 printf("start = Run lcr normally, abort with CTRL+C.\n");
213 printf("fork = Do daemon fork and run as background process.\n");
214 printf("interface = Get help of available interface syntax.\n");
215 printf("rules = Get help of available routing rule syntax.\n");
216 printf("rules [action] = Get individual help for given action.\n");
217 // printf("route = Show current routing as it is parsed.\n");
227 if (pthread_mutex_init(&mutexd, NULL)) {
228 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
232 // if (pthread_mutex_init(&mutext, NULL)) {
233 // fprintf(stderr, "cannot create 'trace' mutex\n");
236 // created_mutext = 1;
237 if (pthread_mutex_init(&mutexe, NULL)) {
238 fprintf(stderr, "cannot create 'PERROR' mutex\n");
244 if (!(strcasecmp(argv[1],"interface"))) {
251 if (!(strcasecmp(argv[1],"rules"))) {
260 /* query available isdn ports */
261 if (!(strcasecmp(argv[1],"query"))) {
263 fprintf(stderr, "-> Using 'misdn_info'\n");
264 rc = system("misdn_info");
270 if (read_options(options_error) == 0) {
271 PERROR("%s", options_error);
276 if (mISDN_initialize() < 0)
281 /* read ruleset(s) */
282 if (!(ruleset_first = ruleset_parse()))
285 /* set pointer to main ruleset */
286 ruleset_main = getrulesetbyname("main");
288 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
289 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
294 /* query available isdn ports */
295 if (!(strcasecmp(argv[1],"route"))) {
296 ruleset_debug(ruleset_first);
302 /* do fork in special cases */
303 if (!(strcasecmp(argv[1],"fork"))) {
311 fprintf(stderr, "Cannot fork!\n");
324 fprintf(stderr, "Cannot fork!\n");
328 printf("LCR: Starting daemon.\n");
334 pidfile = fopen("/var/run/lcr.pid","w");
336 fprintf(pidfile, "%d\n", getpid());
341 if (!!strcasecmp(argv[1],"start")) {
345 /* create lock and lock! */
346 SPRINT(lock, "%s/lcr.lock", options.lock);
347 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
348 fprintf(stderr, "Cannot create lock file: %s\n", lock);
349 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
352 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
353 if (errno == EWOULDBLOCK)
354 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
355 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
360 /* initialize admin socket */
362 fprintf(stderr, "Unable to initialize admin socket.\n");
366 /* generate alaw / ulaw tables */
367 generate_tables(options.law);
370 /* init ss5 sine tables */
375 /* load tones (if requested) */
376 if (fetch_tones() == 0) {
377 fprintf(stderr, "Unable to fetch tones into memory.\n");
381 #if defined WITH_GSM_BS || defined WITH_GSM_MS
383 if (options.gsm && gsm_init()) {
384 fprintf(stderr, "GSM initialization failed.\n");
389 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm-bs or --with-gsm-ms while configure!\n");
394 if (options.gsm && gsm_bs_init()) {
395 fprintf(stderr, "GSM BS initialization failed.\n");
400 if (options.gsm && gsm_ms_init()) {
401 fprintf(stderr, "GSM MS initialization failed.\n");
406 /* read interfaces and open ports */
407 if (!read_interfaces()) {
408 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
409 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
413 interface_first = interface_newlist;
414 interface_newlist = NULL;
416 /* locking memory paging */
419 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
427 fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
430 fprintf(stderr, "Warning: No permission to lock paging.\n");
433 fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
436 fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
440 /* set real time scheduler & priority */
441 if (options.schedule > 1) {
442 memset(&schedp, 0, sizeof(schedp));
443 schedp.sched_priority = options.schedule;
444 ret = sched_setscheduler(0, SCHED_RR, &schedp);
446 PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
451 /* signal handlers */
452 signal(SIGINT,sighandler);
453 signal(SIGHUP,sighandler);
454 signal(SIGTERM,sighandler);
455 signal(SIGPIPE,sighandler);
462 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
463 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
464 printf("%s\n", tracetext);
467 #if defined WITH_GSM_BS || defined WITH_GSM_MS
471 #if defined WITH_GSM_BS || defined WITH_GSM_MS
474 if (now_d-last_d > 1.0) {
475 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
477 /* all loops must be counted from the beginning since nodes might get freed during handler */
480 /* must be processed after all queues, so they are empty */
481 if (select_main(1, NULL, NULL, NULL))
488 if (handle_gsm_ms(&quit))
497 if (!select_main(1, NULL, NULL, NULL))
500 select_main(0, NULL, NULL, NULL);
503 SPRINT(tracetext, "%s terminated", NAME);
504 printf("%s\n", tracetext);
505 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
507 add_trace((char *)"signal", NULL, "%d", quit);
509 add_trace((char *)"errno", NULL, "%d", quit);
513 /* clean messacleane */
519 /* set scheduler & priority
521 if (options.schedule > 1) {
522 memset(&schedp, 0, sizeof(schedp));
523 schedp.sched_priority = options.schedule;
524 sched_setscheduler(0, SCHED_OTHER, &schedp);
527 if (created_signal) {
528 signal(SIGINT,SIG_DFL);
529 signal(SIGHUP,SIG_DFL);
530 signal(SIGTERM,SIG_DFL);
531 signal(SIGPIPE,SIG_DFL);
534 /* destroy objects */
540 while(epoint_first) {
548 /* free interfaces */
550 free_interfaces(interface_first);
551 interface_first = NULL;
553 /* close isdn ports */
554 mISDNport_close_all();
559 while ((message = message_get())) {
561 message_free(message);
564 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
571 /* free admin socket */
576 flock(lockfd, LOCK_UN);
585 ruleset_free(ruleset_first);
586 ruleset_first = NULL;
590 if (pthread_mutex_destroy(&mutexe))
591 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
592 // if (created_mutext)
593 // if (pthread_mutex_destroy(&mutext))
594 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
596 if (pthread_mutex_destroy(&mutexd))
597 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
599 /* deinitialize mISDN */
601 mISDN_deinitialize();
611 #if defined WITH_GSM_BS || defined WITH_GSM_MS
616 /* close loopback, if used by GSM or remote */
617 if (mISDNloop.sock > -1)
620 /* display memory leak */
621 #define MEMCHECK(a, b) \
623 SPRINT(tracetext, a, NAME); \
624 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
625 if (ret) add_trace("blocks", NULL, "%d", b); \
627 printf("\n******************************\n\007"); \
628 printf("\nERROR: %d %s\n", b, a); \
629 printf("\n******************************\n"); \
633 MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
634 MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
635 MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
636 MEMCHECK("memory block(s) left (message.c)",mmemuse)
637 MEMCHECK("memory block(s) left (route.c)",rmemuse)
638 MEMCHECK("memory block(s) left (args)",amemuse)
639 MEMCHECK("class(es) left",classuse)
640 MEMCHECK("file descriptor(s) left",fduse)
641 MEMCHECK("file handler(s) left",fhuse)
643 /* unlock LCR process */
644 // pthread_mutex_unlock(&mutex_lcr);