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