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