Make LCR work with the current openbsc lcr_rtp branch. (soon merged with master branch)
[lcr.git] / main.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** Main function                                                             **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 MESSAGES
15
16 double now_d, last_d;
17 time_t now;
18 struct tm *now_tm;
19 struct timeval now_tv;
20 struct timezone now_tz;
21 #define GET_NOW() \
22         { \
23                 gettimeofday(&now_tv, &now_tz); \
24                 now_d = ((double)(now_tv.tv_usec))/1000000 + now_tv.tv_sec; \
25                 now = now_tv.tv_sec; \
26                 now_tm = localtime(&now); \
27         }
28
29 FILE *debug_fp = NULL;
30 int quit=0;
31
32 #if 0
33 struct lcr_fdset lcr_fdset[FD_SETSIZE];
34 #endif
35
36 pthread_mutex_t mutexd; // debug output mutex
37 pthread_mutex_t mutext; // trace output mutex
38 pthread_mutex_t mutexe; // error output mutex
39 //pthread_mutex_t mutex_lcr; // lcr process mutex
40
41 int memuse = 0;
42 int mmemuse = 0;
43 int cmemuse = 0;
44 int ememuse = 0;
45 int pmemuse = 0;
46 int amemuse = 0;
47 int rmemuse = 0;
48 int classuse = 0;
49 int fduse = 0;
50 int fhuse = 0;
51
52 const char *debug_prefix = NULL;
53 int debug_count = 0;
54 int last_debug = 0;
55 int debug_newline = 1;
56 int nooutput = 0;
57
58 void debug_usleep(int msec, const char *file, int line, int hour, int min, int sec)
59 {
60         usleep(msec);
61 }
62
63 void debug(const char *function, int line, const char *prefix, char *buffer)
64 {
65         /* if we have a new debug count, we add a mark */
66         if (last_debug != debug_count) {
67                 last_debug = debug_count;
68                 if (!nooutput)
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);
70                 if (debug_fp)
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);
72         }
73
74         if (!nooutput) {
75                 if (debug_newline)
76                         printf("\033[32m%06d %s\033[37m%s", debug_count%1000000, prefix?prefix:"", prefix?" ":"");
77                 if (function)
78                         printf("(in %s() line %d): %s", function, line, buffer);
79                 else
80                         printf("%s", buffer);
81         }
82
83         if (debug_fp) {
84                 if (debug_newline) {
85                         if (function)
86                                 fprintf(debug_fp, "%s%s(in %s() line %d): %s", prefix?prefix:"", prefix?" ":"", function, line, buffer);
87                         else
88                                 fprintf(debug_fp, "%s%s: %s", prefix?prefix:"", prefix?" ":"", buffer);
89                 }
90         }
91
92         debug_newline = 0;
93         if (buffer[0])
94                 if (buffer[strlen(buffer)-1] == '\n')
95                         debug_newline = 1;
96 }
97
98
99 void _printdebug(const char *function, int line, unsigned int mask, const char *fmt, ...)
100 {
101         char buffer[4096];
102         va_list args;
103
104         if (!(options.deb & mask))
105                 return;
106         pthread_mutex_lock(&mutexd);
107
108         va_start(args,fmt);
109         VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
110         buffer[sizeof(buffer)-1]=0;
111         va_end(args);
112
113         debug(function, line, debug_prefix, buffer);
114
115         pthread_mutex_unlock(&mutexd);
116 }
117
118 void _printerror(const char *function, int line, const char *fmt, ...)
119 {
120         char buffer[4096];
121         va_list args;
122
123         pthread_mutex_lock(&mutexe);
124
125         va_start(args,fmt);
126         VUNPRINT(buffer,sizeof(buffer)-1,fmt,args);
127         buffer[sizeof(buffer)-1]=0;
128         va_end(args);
129
130         if (options.deb)
131                 debug(function, line, "ERROR", buffer);
132         else { /* only if we do not debug */
133                 if (function)
134                         fprintf(stderr, "ERROR (in %s() line %d) %s", function, line, buffer);
135                 else
136                         fprintf(stderr, "ERROR %s", buffer);
137         }
138
139         pthread_mutex_unlock(&mutexe);
140 }
141
142
143 void sighandler(int sigset)
144 {
145         struct sched_param schedp;
146
147         if (sigset == SIGHUP)
148                 return;
149         if (sigset == SIGPIPE)
150                 return;
151         if (!quit) {
152                 quit = 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);
158                 }
159                 fprintf(stderr, "LCR: Signal received: %d\n", sigset);
160                 PDEBUG(DEBUG_LOG, "Signal received: %d\n", sigset);
161         }
162 }
163
164
165 /*
166  * the main
167  */
168 int main(int argc, char *argv[])
169 {
170         int                     ret = -1;
171         int                     lockfd = -1; /* file lock */
172         struct lcr_msg          *message;
173         class Port              *port;
174         class Endpoint          *epoint;
175         class Join              *join;
176         int                     i;
177         int                     all_idle;
178         char                    prefix_string[64];
179         struct sched_param      schedp;
180         const char              *debug_prefix = "alloc";
181         int                     created_mutexd = 0,/* created_mutext = 0,*/ created_mutexe = 0,
182                                 created_lock = 0, created_signal = 0, created_debug = 0,
183                                 created_misdn = 0;
184         int                     idletime = 0, idlecheck = 0;
185         char                    tracetext[256], lock[128];
186
187 #if 0
188         /* init fdset */
189         memset(lcr_fdset, 0, sizeof(lcr_fdset));
190 #endif
191
192         /* lock LCR process */
193 //      pthread_mutex_lock(&mutex_lcr);
194
195         /* current time */
196         GET_NOW();
197
198         /* show version */
199         printf("\n** %s  Version %s\n\n", NAME, VERSION_STRING);
200
201         /* show options */
202         if (argc <= 1) {
203                 usage:
204                 printf("\n");
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");
213                 printf("\n");
214                 ret = 999;
215                 goto free;
216         }
217
218         /* init crc */
219         crc_init();
220
221         /* the mutex init */
222         if (pthread_mutex_init(&mutexd, NULL)) {
223                 fprintf(stderr, "cannot create 'PDEBUG' mutex\n");
224                 goto free;
225         }
226         created_mutexd = 1;
227 //      if (pthread_mutex_init(&mutext, NULL)) {
228 //              fprintf(stderr, "cannot create 'trace' mutex\n");
229 //              goto free;
230 //      }
231 //      created_mutext = 1;
232         if (pthread_mutex_init(&mutexe, NULL)) {
233                 fprintf(stderr, "cannot create 'PERROR' mutex\n");
234                 goto free;
235         }
236         created_mutexe = 1;
237
238         /* show interface */
239         if (!(strcasecmp(argv[1],"interface"))) {
240                 doc_interface();
241                 ret = 0;
242                 goto free;
243         }
244
245         /* show rules */
246         if (!(strcasecmp(argv[1],"rules"))) {
247                 if (argc <= 2)
248                         doc_rules(NULL);
249                 else
250                         doc_rules(argv[2]);
251                 ret = 0;
252                 goto free;
253         }
254
255         /* query available isdn ports */
256         if (!(strcasecmp(argv[1],"query"))) {
257                 fprintf(stderr, "-> Using 'misdn_info'\n");
258                 system("misdn_info");
259                 ret = 0;
260                 goto free;
261         }
262
263         /* read options */
264         if (read_options() == 0) {
265                 PERROR("%s", options_error);
266                 goto free;
267         }
268
269         /* init mISDN */
270         if (mISDN_initialize() < 0)
271                 goto free;
272         created_misdn = 1;
273         created_debug = 1;
274
275         /* read ruleset(s) */
276         if (!(ruleset_first = ruleset_parse()))
277                 goto free;
278
279         /* set pointer to main ruleset */
280         ruleset_main = getrulesetbyname("main");
281         if (!ruleset_main) {
282                 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
283                 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
284                 sleep(2);
285         }
286
287 #if 0
288         /* query available isdn ports */
289         if (!(strcasecmp(argv[1],"route"))) {
290                 ruleset_debug(ruleset_first);
291                 ret = 0;
292                 goto free;
293         }
294 #endif
295
296         /* do fork in special cases */
297         if (!(strcasecmp(argv[1],"fork"))) {
298                 pid_t pid;
299                 FILE *pidfile;
300
301                 /* do daemon fork */
302                 pid = fork();
303
304                 if (pid < 0) {
305                         fprintf(stderr, "Cannot fork!\n");
306                         goto free;
307                 }
308                 if (pid != 0) {
309                         exit(0);
310                 }
311                 usleep(200000);
312                 printf("\n");
313                 
314                 /* do second fork */
315                 pid = fork();
316
317                 if (pid < 0) {
318                         fprintf(stderr, "Cannot fork!\n");
319                         goto free;
320                 }
321                 if (pid != 0) {
322                         printf("LCR: Starting daemon.\n");
323                         exit(0);
324                 }
325                 nooutput = 1;
326
327                 /* write pid file */
328                 pidfile = fopen("/var/run/lcr.pid","w");
329                 if (pidfile) {
330                         fprintf(pidfile, "%d\n", getpid());
331                         fclose(pidfile);
332                 }
333         } else
334         /* if not start */
335         if (!!strcasecmp(argv[1],"start")) {
336                 goto usage;
337         }
338
339         /* create lock and lock! */
340         SPRINT(lock, "%s/lcr.lock", options.lock);
341         if ((lockfd = open(lock, O_CREAT | O_WRONLY, S_IWUSR)) < 0) {
342                 fprintf(stderr, "Cannot create lock file: %s\n", lock);
343                 fprintf(stderr, "Check options.conf to change to path with permissions for you.\n");
344                 goto free;
345         }
346         if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) {
347                 if (errno == EWOULDBLOCK)
348                         fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
349                 else    fprintf(stderr, "Locking process failed: errno=%d\n", errno);
350                 goto free;
351         }
352         created_lock = 1;
353
354         /* initialize admin socket */
355         if (admin_init()) {
356                 fprintf(stderr, "Unable to initialize admin socket.\n");
357                 goto free;
358         }
359
360         /* generate alaw / ulaw tables */
361         generate_tables(options.law);
362
363 #ifdef WITH_SS5
364         /* init ss5 sine tables */
365         ss5_sine_generate();
366         ss5_test_decode();
367 #endif
368
369         /* load tones (if requested) */
370         if (fetch_tones() == 0) {
371                 fprintf(stderr, "Unable to fetch tones into memory.\n");
372                 goto free;
373         }
374
375 #ifdef WITH_GSM
376         /* handle gsm */
377         if (options.gsm && gsm_init()) {
378                 fprintf(stderr, "GSM initialization failed.\n");
379                 goto free;
380         }
381 #else
382         if (options.gsm) {
383                 fprintf(stderr, "GSM is enabled, but not compiled. Use --with-gsm while configure!\n");
384                 goto free;
385         }
386 #endif
387
388         /* read interfaces and open ports */
389         if (!read_interfaces()) {
390                 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
391                 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
392                 goto free;
393         }
394         relink_interfaces();
395         interface_first = interface_newlist;
396         interface_newlist = NULL;
397         
398         /* locking memory paging */
399         i = 0;
400         while(i < 10) {
401                 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
402                         break;
403                 usleep(200000);
404                 i++;
405         }
406         if (i == 10) {
407                 switch(errno) {
408                         case ENOMEM:
409                         fprintf(stderr, "Warning: Not enough memory to lock paging.\n");
410                         break;
411                         case EPERM:
412                         fprintf(stderr, "Warning: No permission to lock paging.\n");
413                         break;
414                         case EFAULT:
415                         fprintf(stderr, "Warning: 'Bad address' while locking paging.\n");
416                         break;
417                         default:
418                         fprintf(stderr, "Warning: Unknown error %d while locking paging.\n", errno);
419                 }
420         }
421
422         /* set real time scheduler & priority */
423         if (options.schedule > 1) {
424                 memset(&schedp, 0, sizeof(schedp));
425                 schedp.sched_priority = options.schedule;
426                 ret = sched_setscheduler(0, SCHED_RR, &schedp);
427                 if (ret < 0) {
428                         PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
429                         goto free;
430                 }
431         }
432
433         /* signal handlers */   
434         signal(SIGINT,sighandler);
435         signal(SIGHUP,sighandler);
436         signal(SIGTERM,sighandler);
437         signal(SIGPIPE,sighandler);
438         created_signal = 1;
439
440         /*** main loop ***/
441         SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
442         start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
443         printf("%s\n", tracetext);
444         end_trace();
445         GET_NOW();
446         quit = 0;
447         while(!quit) {
448
449                 last_d = now_d;
450                 GET_NOW();
451                 if (now_d-last_d > 1.0) {
452                         PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
453                 }
454                 /* all loops must be counted from the beginning since nodes might get freed during handler */
455                 all_idle = 1;
456
457 //#warning debugging usleep crash
458 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
459
460                 /* handle mISDN messages from kernel */
461                 debug_prefix = "ISDN";
462                 if (mISDN_handler())
463                         all_idle = 0;
464 //#warning debugging usleep crash
465 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
466
467 BUDETECT
468
469                 /* loop through all port ports and call their handler */
470                 port_again:
471                 port = port_first;
472                 while(port) {
473                         debug_prefix = port->p_name;
474                         debug_count++;
475                         ret = port->handler();
476                         if (ret)
477                                 all_idle = 0;
478                         if (ret < 0) /* port has been destroyed */
479                                 goto port_again;
480                         port = port->next;
481                 }
482
483                 /* loop through all epoint and call their handler */
484                 epoint_again:
485                 epoint = epoint_first;
486                 while(epoint) {
487                         debug_prefix = prefix_string;
488                         SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
489                         debug_count++;
490                         ret = epoint->handler();
491                         if (ret)
492                                 all_idle = 0;
493                         if (ret < 0) /* epoint has been destroyed */
494                                 goto epoint_again;
495                         epoint = epoint->next;
496                 }
497
498                 /* loop through all joins and call their handler */
499                 join_again:
500                 join = join_first;
501                 while(join) {
502                         debug_prefix = "join";
503                         debug_count++;
504                         ret = join->handler();
505                         if (ret)
506                                 all_idle = 0;
507                         if (ret < 0) /* join has been destroyed */
508                                 goto join_again;
509                         join = join->next;
510                 }
511
512                 debug_prefix = 0;
513
514                 /* process any message */
515                 debug_count++;
516                 debug_prefix = "message";
517                 while ((message = message_get())) {
518                         all_idle = 0;
519                         switch(message->flow) {
520                                 case PORT_TO_EPOINT:
521                                 debug_prefix = "msg port->epoint";
522                                 epoint = find_epoint_id(message->id_to);
523                                 if (epoint) {
524                                         if (epoint->ep_app) {
525                                                 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
526                                         } else {
527                                                 PDEBUG(DEBUG_MSG, "Warning: message %s from port %d to endpoint %d. endpoint doesn't have an application.\n", messages_txt[message->type], message->id_from, message->id_to);
528                                         }
529                                 } else {
530                                         PDEBUG(DEBUG_MSG, "Warning: message %s from port %d to endpoint %d. endpoint doesn't exist anymore.\n", messages_txt[message->type], message->id_from, message->id_to);
531                                 }
532                                 break;
533
534                                 case EPOINT_TO_JOIN:
535                                 debug_prefix = "msg epoint->join";
536                                 join = find_join_id(message->id_to);
537                                 if (join) {
538                                         join->message_epoint(message->id_from, message->type, &message->param);
539                                 } else {
540                                         PDEBUG(DEBUG_MSG, "Warning: message %s from endpoint %d to join %d. join doesn't exist anymore\n", messages_txt[message->type], message->id_from, message->id_to);
541                                 }
542                                 break;
543
544                                 case JOIN_TO_EPOINT:
545                                 debug_prefix = "msg join->epoint";
546                                 epoint = find_epoint_id(message->id_to);
547                                 if (epoint) {
548                                         if (epoint->ep_app) {
549                                                 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
550                                         } else {
551                                                 PDEBUG(DEBUG_MSG, "Warning: message %s from join %d to endpoint %d. endpoint doesn't have an application.\n", messages_txt[message->type], message->id_from, message->id_to);
552                                         }
553                                 } else {
554                                         PDEBUG(DEBUG_MSG, "Warning: message %s from join %d to endpoint %d. endpoint doesn't exist anymore.\n", messages_txt[message->type], message->id_from, message->id_to);
555                                 }
556                                 break;
557
558                                 case EPOINT_TO_PORT:
559                                 debug_prefix = "msg epoint->port";
560                                 port = find_port_id(message->id_to);
561                                 if (port) {
562                                         port->message_epoint(message->id_from, message->type, &message->param);
563 BUDETECT
564                                 } else {
565                                         PDEBUG(DEBUG_MSG, "Warning: message %s from endpoint %d to port %d. port doesn't exist anymore\n", messages_txt[message->type], message->id_from, message->id_to);
566                                 }
567                                 break;
568
569                                 default:
570                                 PERROR("Message flow %d unknown.\n", message->flow);
571                         }
572                         message_free(message);
573                         debug_count++;
574                         debug_prefix = "message";
575                 }
576 BUDETECT
577
578                 /* handle socket */
579                 if (admin_handle())
580                         all_idle = 0;
581 BUDETECT
582
583 #ifdef WITH_GSM
584                 /* handle gsm */
585                 if (options.gsm)
586                         while(handle_gsm())
587                                 all_idle = 0;
588 #endif
589
590 BUDETECT
591
592 #if 0
593                 /* check for child to exit (eliminate zombies) */
594                 if (waitpid(-1, NULL, WNOHANG) > 0) {
595                         PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
596                         all_idle = 0;
597                 }
598 #endif
599 //#warning debugging usleep crash
600 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
601
602                 /* do idle checking */
603                 if (idlecheck != now) {
604                         PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
605                         idletime = 0;
606                         idlecheck = now;
607                 }
608
609                 /* did we do nothing? so we wait to give time to other processes */
610                 if (all_idle) {
611 //                      pthread_mutex_unlock(&mutex_lcr); // unlock LCR
612                         debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
613 //                      pthread_mutex_lock(&mutex_lcr); // lock LCR
614                         idletime += 4000;
615                 }
616         }
617         SPRINT(tracetext, "%s terminated", NAME);
618         printf("%s\n", tracetext);
619         start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext);
620         if (quit)
621                 add_trace((char *)"signal", NULL, "%d", quit);
622         end_trace();
623         ret=0;
624
625         /* free all */
626 free:
627
628
629         /* set scheduler & priority
630          */
631         if (options.schedule > 1) {
632                 memset(&schedp, 0, sizeof(schedp));
633                 schedp.sched_priority = options.schedule;
634                 sched_setscheduler(0, SCHED_OTHER, &schedp);
635         }
636         /* reset signals */
637         if (created_signal) {
638                 signal(SIGINT,SIG_DFL);
639                 signal(SIGHUP,SIG_DFL);
640                 signal(SIGTERM,SIG_DFL);
641                 signal(SIGPIPE,SIG_DFL);
642         }
643
644         /* destroy objects */
645         debug_prefix = "free";
646
647         while(port_first) {
648                 debug_count++;
649                 delete port_first;
650         }
651         while(epoint_first) {
652                 debug_count++;
653                 delete epoint_first;
654         }
655         epoint_first = NULL;
656         debug_count++;
657         join_free();
658
659         /* free interfaces */
660         if (interface_first)
661                 free_interfaces(interface_first);
662         interface_first = NULL;
663
664         /* close isdn ports */
665         mISDNport_close_all();
666
667         /* flush messages */
668         debug_count++;
669         i = 0;
670         while ((message = message_get())) {
671                 i++;
672                 message_free(message);
673         }
674         if (i) {
675                 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
676         }
677
678         /* free tones */
679         if (toneset_first)
680                 free_tones();
681
682         /* free admin socket */
683         admin_cleanup();
684
685         /* close lock */
686         if (created_lock)
687                 flock(lockfd, LOCK_UN);
688         if (lockfd >= 0) {
689                 chmod(lock, 0700);
690                 unlink(lock);
691                 close(lockfd);
692         }
693
694         /* free rulesets */
695         if (ruleset_first)
696                 ruleset_free(ruleset_first);
697         ruleset_first = NULL;
698
699         /* free mutex */
700         if (created_mutexe)
701                 if (pthread_mutex_destroy(&mutexe))
702                         fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
703 //      if (created_mutext)
704 //              if (pthread_mutex_destroy(&mutext))
705 //                      fprintf(stderr, "cannot destroy 'trace' mutex\n");
706         if (created_mutexd)
707                 if (pthread_mutex_destroy(&mutexd))
708                         fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
709
710         /* deinitialize mISDN */
711         if (created_misdn)
712                 mISDN_deinitialize();
713
714 #ifdef WITH_GSM
715         /* free gsm */
716         if (options.gsm)
717                 gsm_exit(0);
718 #endif
719
720         /* display memory leak */
721 #define MEMCHECK(a, b) \
722         if (b) { \
723                 SPRINT(tracetext, a, NAME); \
724                 start_trace(-1, NULL, NULL, NULL, 0, 0, 0, tracetext); \
725                 if (ret) add_trace("blocks", NULL, "%d", b); \
726                 end_trace(); \
727                 printf("\n******************************\n\007"); \
728                 printf("\nERROR: %d %s\n", b, a); \
729                 printf("\n******************************\n"); \
730                 ret = -1; \
731         }
732         MEMCHECK("",memuse)
733         MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
734         MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
735         MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
736         MEMCHECK("memory block(s) left (message.c)",mmemuse)
737         MEMCHECK("memory block(s) left (route.c)",rmemuse)
738         MEMCHECK("memory block(s) left (args)",amemuse)
739         MEMCHECK("class(es) left",classuse)
740         MEMCHECK("file descriptor(s) left",fduse)
741         MEMCHECK("file handler(s) left",fhuse)
742
743         /* unlock LCR process */
744 //      pthread_mutex_unlock(&mutex_lcr);
745
746         /* take me out */
747         return(ret);
748 }
749
750
751 #ifdef BUDETECT_DEF
752 /* special debug function to detect buffer overflow
753  */
754 int budetect_stop = 0;
755 void budetect(const char *file, int line, const char *function)
756 {
757         if (budetect_stop)
758                 return;
759         /* modify this function to detect race-bugs */
760 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
761         class Port *port;
762         class PmISDN *pmisdn;
763         struct mISDNport *mISDNport = mISDNport_first;
764         int i, ii;
765
766         while(mISDNport) {
767                 i = 0;
768                 ii = mISDNport->b_num;
769                 while(i < ii) {
770                         if (mISDNport->b_port[i]) {
771                                 port = port_first;
772                                 while(port) {
773                                         if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN) {
774                                                 pmisdn = (class PmISDN *)port;
775                                                 if (pmisdn->p_isdn_crypt_listen) {
776                                                         PERROR_RUNTIME("************************************************\n");
777                                                         PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
778                                                         PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
779                                                         PERROR_RUNTIME("************************************************\n");
780                                                         budetect_stop = 1;
781                                                 }
782                                         }
783                                         if (port == mISDNport->b_port[i])
784                                                 break;
785                                         port = port->next;
786                                         if (!port) {
787                                                 PERROR_RUNTIME("************************************************\n");
788                                                 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
789                                                 PERROR_RUNTIME("** b_port not in list.\n");
790                                                 PERROR_RUNTIME("************************************************\n");
791                                                 budetect_stop = 1;
792                                         }
793                                 }
794                         }
795                         i++;
796                 }
797                 mISDNport = mISDNport->next;
798         }
799
800 }
801 #endif
802