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 *file, 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/%s() line %d): %s", file, 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);
93 if (buffer[strlen(buffer)-1] == '\n')
98 void _printdebug(const char *file, const char *function, int line, unsigned int mask, const char *fmt, ...)
103 if (!(options.deb & mask))
105 pthread_mutex_lock(&mutexd);
108 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
109 buffer[sizeof(buffer)-1]=0;
112 debug(file, function, line, "DEBUG", buffer);
114 pthread_mutex_unlock(&mutexd);
117 void _printerror(const char *file, const char *function, int line, const char *fmt, ...)
122 pthread_mutex_lock(&mutexe);
125 VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
126 buffer[sizeof(buffer)-1]=0;
130 debug(file, function, line, "ERROR", buffer);
131 else { /* only if we do not debug */
133 fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
135 fprintf(stderr, "ERROR %s", buffer);
138 pthread_mutex_unlock(&mutexe);
142 void sighandler(int sigset)
144 struct sched_param schedp;
146 if (sigset == SIGHUP)
148 if (sigset == SIGPIPE)
150 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
151 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
153 signal(SIGINT,SIG_DFL);
154 signal(SIGHUP,SIG_DFL);
155 signal(SIGTERM,SIG_DFL);
156 signal(SIGPIPE,SIG_DFL);
159 /* set scheduler & priority */
160 if (options.schedule > 1) {
161 memset(&schedp, 0, sizeof(schedp));
162 schedp.sched_priority = 0;
163 sched_setscheduler(0, SCHED_OTHER, &schedp);
172 int main(int argc, char *argv[])
175 double now_d, last_d;
179 int lockfd = -1; /* file lock */
180 struct lcr_msg *message;
182 struct sched_param schedp;
183 int created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
184 created_lock = 0, created_signal = 0, created_debug = 0,
185 created_misdn = 0, created_message = 0;
186 char tracetext[256], lock[128];
187 char options_error[256];
192 memset(lcr_fdset, 0, sizeof(lcr_fdset));
195 /* lock LCR process */
196 // pthread_mutex_lock(&mutex_lcr);
199 printf("\n** %s Version %s\n\n", NAME, VERSION_STRING);
205 printf("Usage: lcr (query | start | fork | rules | route)\n");
206 printf("query = Show available isdn ports.\n");
207 printf("start = Run lcr normally, abort with CTRL+C.\n");
208 printf("fork = Do daemon fork and run as background process.\n");
209 printf("interface = Get help of available interface syntax.\n");
210 printf("rules = Get help of available routing rule syntax.\n");
211 printf("rules [action] = Get individual help for given action.\n");
212 // printf("route = Show current routing as it is parsed.\n");
222 if (pthread_mutex_init(&mutexd, NULL)) {
223 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
227 // if (pthread_mutex_init(&mutext, NULL)) {
228 // fprintf(stderr, "cannot create 'trace' mutex\n");
231 // created_mutext = 1;
232 if (pthread_mutex_init(&mutexe, NULL)) {
233 fprintf(stderr, "cannot create 'PERROR' mutex\n");
239 if (!(strcasecmp(argv[1],"interface"))) {
246 if (!(strcasecmp(argv[1],"rules"))) {
255 /* query available isdn ports */
256 if (!(strcasecmp(argv[1],"query"))) {
258 fprintf(stderr, "-> Using 'misdn_info'\n");
259 rc = system("misdn_info");
265 if (read_options(options_error) == 0) {
266 PERROR("%s", options_error);
269 polling = options.polling;
272 if (mISDN_initialize() < 0)
277 /* read ruleset(s) */
278 if (!(ruleset_first = ruleset_parse()))
281 /* set pointer to main ruleset */
282 ruleset_main = getrulesetbyname("main");
284 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
285 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
290 /* query available isdn ports */
291 if (!(strcasecmp(argv[1],"route"))) {
292 ruleset_debug(ruleset_first);
298 /* do fork in special cases */
299 if (!(strcasecmp(argv[1],"fork"))) {
307 fprintf(stderr, "Cannot fork!\n");
320 fprintf(stderr, "Cannot fork!\n");
324 printf("LCR: Starting daemon.\n");
330 pidfile = fopen("/var/run/lcr.pid","w");
332 fprintf(pidfile, "%d\n", getpid());
337 if (!!strcasecmp(argv[1],"start")) {
341 /* create lock and lock! */
342 SPRINT(lock, "%s/lcr.lock", options.lock);
343 if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
344 fprintf(stderr, "Cannot create lock file: %s\n", lock);
345 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
348 if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
349 if (errno == EWOULDBLOCK)
350 fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
351 else fprintf(stderr, "Locking process failed: errno=%d\n", errno);
356 /* initialize admin socket */
358 fprintf(stderr, "Unable to initialize admin socket.\n");
362 /* generate alaw / ulaw tables */
363 generate_tables(options.law);
366 /* init SIP globals */
368 polling = 1; /* must poll, because of SIP events */
372 /* init ss5 sine tables */
377 /* load tones (if requested) */
378 if (fetch_tones() == 0) {
379 fprintf(stderr, "Unable to fetch tones into memory.\n");
383 #if defined WITH_GSM_BS || defined WITH_GSM_MS
386 fprintf(stderr, "GSM initialization failed.\n");
391 init is done when interface is up
394 fprintf(stderr, "GSM BS initialization failed.\n");
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);
463 SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
464 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
465 printf("%s\n", tracetext);
475 if (now_d-last_d > 1.0) {
476 PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
478 /* all loops must be counted from the beginning since nodes might get freed during handler */
481 /* must be processed after all queues, so they are empty */
482 if (select_main(1, NULL, NULL, NULL))
488 if (options.polling) {
489 if (!select_main(1, NULL, NULL, NULL)) {
491 /* FIXME: check if work was done */
497 select_main(0, NULL, NULL, NULL);
500 SPRINT(tracetext, "%s terminated", NAME);
501 printf("%s\n", tracetext);
502 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
504 add_trace((char *)"signal", NULL, "%d", quit);
506 add_trace((char *)"errno", NULL, "%d", quit);
513 /* set scheduler & priority
515 if (options.schedule > 1) {
516 memset(&schedp, 0, sizeof(schedp));
517 schedp.sched_priority = options.schedule;
518 sched_setscheduler(0, SCHED_OTHER, &schedp);
521 if (created_signal) {
522 signal(SIGINT,SIG_DFL);
523 signal(SIGHUP,SIG_DFL);
524 signal(SIGTERM,SIG_DFL);
525 signal(SIGPIPE,SIG_DFL);
528 /* destroy objects */
533 while(epoint_first) {
541 /* free interfaces */
543 free_interfaces(interface_first);
544 interface_first = NULL;
546 /* close isdn ports */
547 mISDNport_close_all();
552 while ((message = message_get())) {
554 message_free(message);
557 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
568 /* free admin socket */
573 flock(lockfd, LOCK_UN);
582 ruleset_free(ruleset_first);
583 ruleset_first = NULL;
587 if (pthread_mutex_destroy(&mutexe))
588 fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
589 // if (created_mutext)
590 // if (pthread_mutex_destroy(&mutext))
591 // fprintf(stderr, "cannot destroy 'trace' mutex\n");
593 if (pthread_mutex_destroy(&mutexd))
594 fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
596 /* deinitialize mISDN */
598 mISDN_deinitialize();
602 exit is done when interface is down
610 #if defined WITH_GSM_BS || defined WITH_GSM_MS
615 /* cleanup SIP globals */
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);