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