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