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