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