LCR is now uses socket based mISDN V2 API
[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 long 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                 mISDN_port_info();
279                 ret = 0;
280                 goto free;
281         }
282
283         /* read options */
284         if (read_options() == 0)
285                 goto free;
286
287         /* init mISDN */
288         if (mISDN_initialize() < 0)
289                 goto free;
290         created_misdn = 1;
291         created_debug = 1;
292
293         /* read ruleset(s) */
294         if (!(ruleset_first = ruleset_parse()))
295                 goto free;
296
297         /* set pointer to main ruleset */
298         ruleset_main = getrulesetbyname("main");
299         if (!ruleset_main)
300         {
301                 fprintf(stderr, "\n***\n -> Missing 'main' ruleset, causing ALL calls to be disconnected.\n***\n\n");
302                 PDEBUG(DEBUG_LOG, "Missing 'main' ruleset, causing ALL calls to be disconnected.\n");
303                 sleep(2);
304         }
305
306 #if 0
307         /* query available isdn ports */
308         if (!(strcasecmp(argv[1],"route")))
309         {
310                 ruleset_debug(ruleset_first);
311                 ret = 0;
312                 goto free;
313         }
314 #endif
315
316         /* do fork in special cases */
317         if (!(strcasecmp(argv[1],"fork")))
318         {
319                 pid_t pid;
320
321                 /* do daemon fork */
322                 pid = fork();
323
324                 if (pid < 0)
325                 {
326                         fprintf(stderr, "Cannot fork!\n");
327                         goto free;
328                 }
329                 if (pid != 0)
330                 {
331                         exit(0);
332                 }
333                 usleep(200000);
334                 printf("\n");
335                 
336                 /* do second fork */
337                 pid = fork();
338
339                 if (pid < 0)
340                 {
341                         fprintf(stderr, "Cannot fork!\n");
342                         goto free;
343                 }
344                 if (pid != 0)
345                 {
346                         printf("LCR: Starting daemon.\n");
347                         exit(0);
348                 }
349                 nooutput = 1;
350         } else
351         /* if not start */
352         if (!!strcasecmp(argv[1],"start"))
353         {
354                 goto usage;
355         }
356
357         /* create lock and lock! */
358         if ((lockfd = open("/var/run/lcr.lock", O_CREAT, 0)) < 0)
359         {
360                 fprintf(stderr, "Cannot create lock file: /var/run/lcr.lock\n");
361                 goto free;
362         }
363         if (flock(lockfd, LOCK_EX|LOCK_NB) < 0)
364         {
365                 if (errno == EWOULDBLOCK)
366                         fprintf(stderr, "LCR: Another LCR process is running. Please kill the other one.\n");
367                 else    fprintf(stderr, "Locking process failed: errno=%d\n", errno);
368                 goto free;
369         }
370         created_lock = 1;
371
372         /* initialize admin socket */
373         if (admin_init())
374         {
375                 fprintf(stderr, "Unable to initialize admin socket.\n");
376                 goto free;
377         }
378
379         /* generate alaw / ulaw tables */
380         generate_tables(options.law);
381
382         /* load tones (if requested) */
383         if (fetch_tones() == 0)
384         {
385                 fprintf(stderr, "Unable to fetch tones into memory.\n");
386                 goto free;
387         }
388
389         /* read interfaces and open ports */
390         if (!read_interfaces())
391         {
392                 PERROR_RUNTIME("No interfaces specified or failed to parse interface.conf.\n");
393                 fprintf(stderr, "No interfaces specified or failed to parse interface.conf.\n");
394                 goto free;
395         }
396         relink_interfaces();
397         interface_first = interface_newlist;
398         interface_newlist = NULL;
399         
400         /* locking memory paging */
401         i = 0;
402         while(i < 10)
403         {
404                 if (mlockall(MCL_CURRENT | MCL_FUTURE) >= 0)
405                         break;
406                 usleep(200000);
407                 i++;
408         }
409         if (i == 10)
410         {
411                 switch(errno)
412                 {
413                         case ENOMEM:
414                         fprintf(stderr, "Not enough memory to lock paging, exitting...\n");
415                         break;
416                         case EPERM:
417                         fprintf(stderr, "No permission to lock paging, exitting...\n");
418                         break;
419                         case EFAULT:
420                         fprintf(stderr, "'Bad address' while locking paging, exitting...\n");
421                         break;
422                         default:
423                         fprintf(stderr, "Unknown error %d while locking paging, exitting...\n", errno);
424                 }
425                 goto free;
426         }
427
428         /* set real time scheduler & priority */
429         if (options.schedule > 1)
430         {
431                 memset(&schedp, 0, sizeof(schedp));
432                 schedp.sched_priority = options.schedule;
433                 ret = sched_setscheduler(0, SCHED_RR, &schedp);
434                 if (ret < 0)
435                 {
436                         PERROR("Scheduling failed with given priority %d (errno = %d).\nCheck options.conf 'schedule', exitting...\n", options.schedule, errno);
437                         goto free;
438                 }
439         }
440
441         /* signal handlers */   
442         signal(SIGINT,sighandler);
443         signal(SIGHUP,sighandler);
444         signal(SIGTERM,sighandler);
445         signal(SIGPIPE,sighandler);
446         created_signal = 1;
447
448         /*** main loop ***/
449         SPRINT(tracetext, "%s %s started, waiting for calls...", NAME, VERSION_STRING);
450         start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
451         printf("%s\n", tracetext);
452         end_trace();
453         GET_NOW();
454         quit = 0;
455         while(!quit)
456         {
457
458                 last_d = now_d;
459                 GET_NOW();
460                 if (now_d-last_d > 1.0)
461                 {
462                         PERROR("LCR was stalling %d.%d seconds\n", ((int)((now_d-last_d)*10.0))/10, (int)((now_d-last_d)*10.0));
463                 }
464                 /* all loops must be counted from the beginning since nodes might get freed during handler */
465                 all_idle = 1;
466
467 //#warning debugging usleep crash
468 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
469
470                 /* handle mISDN messages from kernel */
471                 debug_prefix = "ISDN";
472                 if (mISDN_handler())
473                         all_idle = 0;
474 //#warning debugging usleep crash
475 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
476
477 BUDETECT
478
479                 /* loop through all port ports and call their handler */
480                 port_again:
481                 port = port_first;
482                 while(port)
483                 {
484                         debug_prefix = port->p_name;
485                         debug_count++;
486                         ret = port->handler();
487                         if (ret)
488                                 all_idle = 0;
489                         if (ret < 0) /* port has been destroyed */
490                                 goto port_again;
491                         port = port->next;
492                 }
493
494                 /* loop through all epoint and call their handler */
495                 epoint_again:
496                 epoint = epoint_first;
497                 while(epoint)
498                 {
499                         debug_prefix = prefix_string;
500                         SPRINT(prefix_string, "ep%ld", epoint->ep_serial);
501                         debug_count++;
502                         ret = epoint->handler();
503                         if (ret)
504                                 all_idle = 0;
505                         if (ret < 0) /* epoint has been destroyed */
506                                 goto epoint_again;
507                         epoint = epoint->next;
508                 }
509
510                 /* loop through all joins and call their handler */
511                 join_again:
512                 join = join_first;
513                 while(join)
514                 {
515                         debug_prefix = "join";
516                         debug_count++;
517                         ret = join->handler();
518                         if (ret)
519                                 all_idle = 0;
520                         if (ret < 0) /* join has been destroyed */
521                                 goto join_again;
522                         join = join->next;
523                 }
524
525                 debug_prefix = 0;
526
527                 /* process any message */
528                 debug_count++;
529                 debug_prefix = "message";
530                 while ((message = message_get()))
531                 {
532                         all_idle = 0;
533                         switch(message->flow)
534                         {
535                                 case PORT_TO_EPOINT:
536                                 debug_prefix = "msg port->epoint";
537                                 epoint = find_epoint_id(message->id_to);
538                                 if (epoint)
539                                 {
540                                         if (epoint->ep_app)
541                                         {
542                                                 epoint->ep_app->ea_message_port(message->id_from, message->type, &message->param);
543                                         } else
544                                         {
545                                                 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);
546                                         }
547                                 } else
548                                 {
549                                         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);
550                                 }
551                                 break;
552
553                                 case EPOINT_TO_JOIN:
554                                 debug_prefix = "msg epoint->join";
555                                 join = find_join_id(message->id_to);
556                                 if (join)
557                                 {
558                                         join->message_epoint(message->id_from, message->type, &message->param);
559                                 } else
560                                 {
561                                         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);
562                                 }
563                                 break;
564
565                                 case JOIN_TO_EPOINT:
566                                 debug_prefix = "msg join->epoint";
567                                 epoint = find_epoint_id(message->id_to);
568                                 if (epoint)
569                                 {
570                                         if (epoint->ep_app)
571                                         {
572                                                 epoint->ep_app->ea_message_join(message->id_from, message->type, &message->param);
573                                         } else
574                                         {
575                                                 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);
576                                         }
577                                 } else
578                                 {
579                                         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);
580                                 }
581                                 break;
582
583                                 case EPOINT_TO_PORT:
584                                 debug_prefix = "msg epoint->port";
585                                 port = find_port_id(message->id_to);
586                                 if (port)
587                                 {
588                                         port->message_epoint(message->id_from, message->type, &message->param);
589 BUDETECT
590                                 } else
591                                 {
592                                         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);
593                                 }
594                                 break;
595
596                                 default:
597                                 PERROR("Message flow %d unknown.\n", message->flow);
598                         }
599                         message_free(message);
600                         debug_count++;
601                         debug_prefix = "message";
602                 }
603 BUDETECT
604
605                 /* handle socket */
606                 if (admin_handle())
607                         all_idle = 0;
608 BUDETECT
609
610 #if 0
611                 /* check for child to exit (eliminate zombies) */
612                 if (waitpid(-1, NULL, WNOHANG) > 0)
613                 {
614                         PDEBUG(DEBUG_EPOINT, "a child process (created by endpoint) has exitted.\n");
615                         all_idle = 0;
616                 }
617 #endif
618 //#warning debugging usleep crash
619 //              debug_usleep(1, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
620
621                 /* do idle checking */
622                 if (idlecheck != now)
623                 {
624                         PDEBUG(DEBUG_IDLETIME, "Idle time : %d%%\n", idletime/10000);
625                         idletime = 0;
626                         idlecheck = now;
627                 }
628
629                 /* did we do nothing? so we wait to give time to other processes */
630                 if (all_idle)
631                 {
632 //                      pthread_mutex_unlock(&mutex_lcr); // unlock LCR
633                         debug_usleep(4000, __FILE__, __LINE__, now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);
634 //                      pthread_mutex_lock(&mutex_lcr); // lock LCR
635                         idletime += 4000;
636                 }
637         }
638         SPRINT(tracetext, "%s terminated", NAME);
639         printf("%s\n", tracetext);
640         start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext);
641         if (quit)
642                 add_trace("signal", NULL, "%d", quit);
643         end_trace();
644         ret=0;
645
646         /* free all */
647 free:
648
649
650         /* set scheduler & priority
651          */
652         if (options.schedule > 1)
653         {
654                 memset(&schedp, 0, sizeof(schedp));
655                 schedp.sched_priority = options.schedule;
656                 sched_setscheduler(0, SCHED_OTHER, &schedp);
657         }
658         /* reset signals */
659         if (created_signal)
660         {
661                 signal(SIGINT,SIG_DFL);
662                 signal(SIGHUP,SIG_DFL);
663                 signal(SIGTERM,SIG_DFL);
664                 signal(SIGPIPE,SIG_DFL);
665         }
666
667         /* destroy objects */
668         debug_prefix = "free";
669
670         while(port_first)
671         {
672                 debug_count++;
673                 delete port_first;
674         }
675         while(epoint_first)
676         {
677                 debug_count++;
678                 delete epoint_first;
679         }
680         epoint_first = NULL;
681         debug_count++;
682         join_free();
683
684         /* free interfaces */
685         if (interface_first)
686                 free_interfaces(interface_first);
687         interface_first = NULL;
688
689         /* close isdn ports */
690         mISDNport_close_all();
691
692         /* flush messages */
693         debug_count++;
694         i = 0;
695         while ((message = message_get()))
696         {
697                 i++;
698                 message_free(message);
699         }
700         if (i)
701         {
702                 PDEBUG(DEBUG_MSG, "freed %d pending messages\n", i);
703         }
704
705         /* free tones */
706         if (toneset_first)
707                 free_tones();
708
709         /* free admin socket */
710         admin_cleanup();
711
712         /* close lock */
713         if (created_lock)
714                 flock(lockfd, LOCK_UN);
715         if (lockfd >= 0)
716                 close(lockfd);
717
718         /* free rulesets */
719         if (ruleset_first)
720                 ruleset_free(ruleset_first);
721         ruleset_first = NULL;
722
723         /* free mutex */
724         if (created_mutexe)
725                 if (pthread_mutex_destroy(&mutexe))
726                         fprintf(stderr, "cannot destroy 'PERROR' mutex\n");
727 //      if (created_mutext)
728 //              if (pthread_mutex_destroy(&mutext))
729 //                      fprintf(stderr, "cannot destroy 'trace' mutex\n");
730         if (created_mutexd)
731                 if (pthread_mutex_destroy(&mutexd))
732                         fprintf(stderr, "cannot destroy 'PDEBUG' mutex\n");
733
734         /* deinitialize mISDN */
735         if (created_misdn)
736                 mISDN_deinitialize();
737
738         /* display memory leak */
739 #define MEMCHECK(a, b) \
740         if (b) \
741         { \
742                 SPRINT(tracetext, a, NAME); \
743                 start_trace(0, NULL, NULL, NULL, 0, 0, 0, tracetext); \
744                 if (ret) add_trace("blocks", NULL, "%d", b); \
745                 end_trace(); \
746                 printf("\n******************************\n\007"); \
747                 printf("\nERROR: %d %s\n", b, a); \
748                 printf("\n******************************\n"); \
749                 ret = -1; \
750         }
751         MEMCHECK("",memuse)
752         MEMCHECK("memory block(s) left (port.cpp ...)",pmemuse)
753         MEMCHECK("memory block(s) left (epoint*.cpp ...)",ememuse)
754         MEMCHECK("memory block(s) left (join*.cpp)",cmemuse)
755         MEMCHECK("memory block(s) left (message.c)",mmemuse)
756         MEMCHECK("memory block(s) left (route.c)",rmemuse)
757         MEMCHECK("memory block(s) left (args)",amemuse)
758         MEMCHECK("class(es) left",classuse)
759         MEMCHECK("file descriptor(s) left",fduse)
760         MEMCHECK("file handler(s) left",fhuse)
761
762         /* unlock LCR process */
763 //      pthread_mutex_unlock(&mutex_lcr);
764
765         /* take me out */
766         return(ret);
767 }
768
769
770 #ifdef BUDETECT_DEF
771 /* special debug function to detect buffer overflow
772  */
773 int budetect_stop = 0;
774 void budetect(const char *file, int line, char *function)
775 {
776         if (budetect_stop)
777                 return;
778         /* modify this function to detect race-bugs */
779 #warning DID YOU MODIFY THIS FUNCTION TO DETECT THE BUFFER OVERFLOW BUG?
780         class Port *port;
781         class PmISDN *pmisdn;
782         struct mISDNport *mISDNport = mISDNport_first;
783         int i, ii;
784
785         while(mISDNport)
786         {
787                 i = 0;
788                 ii = mISDNport->b_num;
789                 while(i < ii)
790                 {
791                         if (mISDNport->b_port[i])
792                         {
793                                 port = port_first;
794                                 while(port)
795                                 {
796                                         if ((port->p_type&PORT_CLASS_MASK) == PORT_CLASS_ISDN)
797                                         {
798                                                 pmisdn = (class PmISDN *)port;
799                                                 if (pmisdn->p_isdn_crypt_listen)
800                                                 {
801                                                         PERROR_RUNTIME("************************************************\n");
802                                                         PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
803                                                         PERROR_RUNTIME("** p_isdn_crypt_listen = %d\n", pmisdn->p_isdn_crypt_listen);
804                                                         PERROR_RUNTIME("************************************************\n");
805                                                         budetect_stop = 1;
806                                                 }
807                                         }
808                                         if (port == mISDNport->b_port[i])
809                                                 break;
810                                         port = port->next;
811                                         if (!port)
812                                         {
813                                                 PERROR_RUNTIME("************************************************\n");
814                                                 PERROR_RUNTIME("** BUG detected in %s, line %d, function %s\n", file, line, function);
815                                                 PERROR_RUNTIME("** b_port not in list.\n");
816                                                 PERROR_RUNTIME("************************************************\n");
817                                                 budetect_stop = 1;
818                                         }
819                                 }
820                         }
821                         i++;
822                 }
823                 mISDNport = mISDNport->next;
824         }
825
826 }
827 #endif
828