1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
|
/*
* Copyright (c) 1997, 1998 Hellmuth Michaelis. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*---------------------------------------------------------------------------
*
* i4b daemon - main program entry
* -------------------------------
*
* $Id: main.c,v 1.29 1998/12/05 18:03:26 hm Exp $
*
* last edit-date: [Sat Dec 5 18:10:38 1998]
*
*---------------------------------------------------------------------------*/
#ifdef I4B_EXTERNAL_MONITOR
#include "monitor.h"
#endif
#define MAIN
#include "isdnd.h"
#undef MAIN
#ifdef I4B_EXTERNAL_MONITOR
#ifdef I4B_NOTCPIP_MONITOR
/* monitor via local socket */
static void mloop(int sockfd);
#else /* I4B_NOTCPIP_MONITOR */
/* monitor via local and tcp/ip socket */
static void mloop(int localsock, int remotesock);
#endif /* I4B_NOTCPIP_MONITOR */
#else /* I4B_EXTERNAL_MONITOR */
/* no monitoring at all */
static void mloop();
#endif /* I4B_EXTERNAL_MONITOR */
#ifdef USE_CURSES
static void kbdrdhdl(void);
#endif
static void isdnrdhdl(void);
static void usage(void);
#define MSG_BUF_SIZ 1024 /* message buffer size */
/*---------------------------------------------------------------------------*
* usage display and exit
*---------------------------------------------------------------------------*/
static void
usage(void)
{
fprintf(stderr, "\n");
fprintf(stderr, "isdnd - i4b ISDN manager daemon, version %02d.%02d.%d, %s %s\n", VERSION, REL, STEP, __DATE__, __TIME__);
#ifdef DEBUG
fprintf(stderr, " usage: isdnd [-b] [-c file] [-d level] [-F]\n");
#else
fprintf(stderr, " usage: isdnd [-b] [-c file] [-F]\n");
#endif
fprintf(stderr, " [-f [-r dev] [-t termtype]] [-u time]\n");
fprintf(stderr, " [-l] [-L file] [-s facility] [-m]\n");
fprintf(stderr, " -b audible bell in fullscreen mode at connect/disconnect\n");
fprintf(stderr, " -c <filename> configuration file name (def: %s)\n", CONFIG_FILE_DEF);
#ifdef DEBUG
fprintf(stderr, " -d <level> set debug flag bits:\n");
fprintf(stderr, " general = 0x%04x, rates = 0x%04x, timing = 0x%04x\n", DL_MSG, DL_RATES, DL_TIME);
fprintf(stderr, " state = 0x%04x, retry = 0x%04x, dial = 0x%04x\n", DL_STATE, DL_RCVRY, DL_DIAL);
fprintf(stderr, " process = 0x%04x, kernio = 0x%04x ctrlstat = 0x%04x\n", DL_PROC, DL_DRVR, DL_CNST);
fprintf(stderr, " -dn no debug output on fullscreen display\n");
#endif
fprintf(stderr, " -f fullscreen status display\n");
fprintf(stderr, " -F do not become a daemon process\n");
fprintf(stderr, " -l use a logfile instead of syslog\n");
fprintf(stderr, " -L <file> use file instead of %s for logging\n", LOG_FILE_DEF);
fprintf(stderr, " -P pretty print real config to stdout and exit\n");
fprintf(stderr, " -r <device> redirect output to other device (for -f)\n");
fprintf(stderr, " -s <facility> use facility instead of %d for syslog logging\n", LOG_LOCAL0 >> 3);
fprintf(stderr, " -t <termtype> terminal type of redirected screen (for -f)\n");
fprintf(stderr, " -u <time> length of a charging unit in seconds\n");
fprintf(stderr, " -m inhibit network/local monitoring\n");
fprintf(stderr, "\n");
exit(1);
}
/*---------------------------------------------------------------------------*
* program entry
*---------------------------------------------------------------------------*/
int
main(int argc, char **argv)
{
int i;
msg_vr_req_t mvr;
#ifdef I4B_EXTERNAL_MONITOR
int sockfd = -1; /* local monitor socket */
#ifndef I4B_NOTCPIP_MONITOR
int remotesockfd = -1; /* tcp/ip monitor socket */
#endif
#endif
while ((i = getopt(argc, argv, "bmc:d:fFlL:Pr:s:t:u:?")) != EOF)
{
switch (i)
{
case 'b':
do_bell = 1;
break;
case 'm':
inhibit_monitor = 1;
break;
case 'c':
configfile = optarg;
break;
#ifdef DEBUG
case 'd':
if(*optarg == 'n')
debug_noscreen = 1;
else if((sscanf(optarg, "%i", &debug_flags)) == 1)
do_debug = 1;
else
usage();
break;
#endif
case 'f':
do_fullscreen = 1;
do_fork = 0;
#ifndef USE_CURSES
fprintf(stderr, "Sorry, no fullscreen mode available - daemon compiled without USE_CURSES\n");
exit(1);
#endif
break;
case 'F':
do_fork = 0;
break;
case 'l':
uselogfile = 1;
break;
case 'L':
strcpy(logfile, optarg);
break;
case 'P':
do_print = 1;
break;
case 'r':
rdev = optarg;
do_rdev = 1;
break;
case 's':
if(isdigit(*optarg))
{
int facility;
logfacility = strtoul(optarg, NULL, 10);
facility = logfacility << 3;
if((facility < LOG_KERN) ||
(facility > LOG_FTP && facility < LOG_LOCAL0) ||
(facility > LOG_LOCAL7))
{
fprintf(stderr, "Error, option -s has invalid logging facility %d", logfacility);
usage();
}
logfacility = facility;
}
else
{
fprintf(stderr, "Error: option -s requires a numeric argument!\n");
usage();
}
break;
case 't':
ttype = optarg;
do_ttytype = 1;
break;
case 'u':
if(isdigit(*optarg))
{
unit_length = strtoul(optarg, NULL, 10);
if(unit_length < ULSRC_CMDLMIN)
unit_length = ULSRC_CMDLMIN;
else if(unit_length > ULSRC_CMDLMAX)
unit_length = ULSRC_CMDLMAX;
got_unitlen = 1;
}
else
{
fprintf(stderr, "Error: option -T requires a numeric argument!\n");
usage();
}
break;
case '?':
default:
usage();
break;
}
}
#ifdef DEBUG
if(!do_debug)
debug_noscreen = 0;
#endif
if(!do_print)
{
umask(UMASK); /* set our umask ... */
init_log(); /* initialize the logging subsystem */
}
check_pid(); /* check if we are already running */
if(!do_print)
{
if(do_fork || (do_fullscreen && do_rdev)) /* daemon mode ? */
daemonize();
write_pid(); /* write our pid to file */
/* set signal handler(s) */
signal(SIGCHLD, sigchild_handler); /* process handling */
signal(SIGHUP, rereadconfig); /* reread configuration */
signal(SIGUSR1, reopenfiles); /* reopen acct/log files*/
signal(SIGPIPE, SIG_IGN); /* handled manually */
signal(SIGINT, do_exit); /* clean up on SIGINT */
signal(SIGTERM, do_exit); /* clean up on SIGTERM */
signal(SIGQUIT, do_exit); /* clean up on SIGQUIT */
}
/* open isdn device */
if((isdnfd = open(I4BDEVICE, O_RDWR)) < 0)
{
log(LL_ERR, "main: cannot open %s: %s", I4BDEVICE, strerror(errno));
exit(1);
}
/* check kernel and userland have same version/release numbers */
if((ioctl(isdnfd, I4B_VR_REQ, &mvr)) < 0)
{
log(LL_ERR, "main: ioctl I4B_VR_REQ failed: %s", strerror(errno));
do_exit(1);
}
if(mvr.version != VERSION)
{
log(LL_ERR, "main: version mismatch, kernel %d, daemon %d", mvr.version, VERSION);
do_exit(1);
}
if(mvr.release != REL)
{
log(LL_ERR, "main: release mismatch, kernel %d, daemon %d", mvr.release, REL);
do_exit(1);
}
if(mvr.step != STEP)
{
log(LL_ERR, "main: step mismatch, kernel %d, daemon %d", mvr.step, STEP);
do_exit(1);
}
/* init controller state array */
init_controller();
/* read runtime configuration file and configure ourselves */
configure(configfile, 0);
if(config_error_flag)
{
log(LL_ERR, "there were %d error(s) in the configuration file, terminating!", config_error_flag);
exit(1);
}
/* handle the rates stuff */
if((i = readrates(ratesfile)) == ERROR)
{
if(rate_error != NULL)
log(LL_ERR, "%s", rate_error);
exit(1);
}
if(i == GOOD)
{
got_rate = 1; /* flag, ratesfile read and ok */
DBGL(DL_MSG, (log(LL_DBG, "ratesfile %s read successfully", ratesfile)));
}
else
{
if(rate_error != NULL)
log(LL_WRN, "%s", rate_error);
}
/* if writing accounting info, open file, set unbuffered */
if(useacctfile)
{
if((acctfp = fopen(acctfile, "a")) == NULL)
{
log(LL_ERR, "ERROR, can't open acctfile %s for writing, terminating!", acctfile);
exit(1);
}
setvbuf(acctfp, (char *)NULL, _IONBF, 0);
}
/* initialize alias processing */
if(aliasing)
init_alias(aliasfile);
/* init remote monitoring */
#ifdef I4B_EXTERNAL_MONITOR
if(do_monitor)
{
monitor_init();
sockfd = monitor_create_local_socket();
#ifndef I4B_NOTCPIP_MONITOR
remotesockfd = monitor_create_remote_socket(monitorport);
#endif
}
#endif
/* in case fullscreendisplay, initialize */
#ifdef USE_CURSES
if(do_fullscreen)
{
init_screen();
}
#endif
/* init realtime priority */
#ifdef USE_RTPRIO
if(rt_prio != RTPRIO_NOTUSED)
{
struct rtprio rtp;
rtp.type = RTP_PRIO_REALTIME;
rtp.prio = rt_prio;
if((rtprio(RTP_SET, getpid(), &rtp)) == -1)
{
log(LL_ERR, "rtprio failed: %s", strerror(errno));
do_exit(1);
}
}
#endif
srandom(580403); /* init random number gen */
mloop( /* enter loop of no return .. */
#ifdef I4B_EXTERNAL_MONITOR
sockfd
#ifndef I4B_NOTCPIP_MONITOR
, remotesockfd
#endif
#endif
);
do_exit(0);
return(0);
}
/*---------------------------------------------------------------------------*
* program exit
*---------------------------------------------------------------------------*/
void
do_exit(int exitval)
{
close_allactive();
unlink(PIDFILE);
log(LL_DMN, "daemon terminating, exitval = %d", exitval);
#ifdef USE_CURSES
if(do_fullscreen)
endwin();
#endif
#ifdef I4B_EXTERNAL_MONITOR
monitor_exit();
#endif
exit(exitval);
}
/*---------------------------------------------------------------------------*
* main loop
*---------------------------------------------------------------------------*/
static void
mloop(
#ifdef I4B_EXTERNAL_MONITOR
int localmonitor
#ifndef I4B_NOTCPIP_MONITOR
, int remotemonitor
#endif
#endif
)
{
fd_set set;
struct timeval timeout;
int ret;
int high_selfd;
/* go into loop */
log(LL_DMN, "daemon started (pid = %d)", getpid());
for(;;)
{
FD_ZERO(&set);
#ifdef USE_CURSES
if(do_fullscreen)
FD_SET(fileno(stdin), &set);
#endif
FD_SET(isdnfd, &set);
high_selfd = isdnfd;
#ifdef I4B_EXTERNAL_MONITOR
if(do_monitor)
{
if (localmonitor != -1) {
/* always watch for new connections */
FD_SET(localmonitor, &set);
if(localmonitor > high_selfd)
high_selfd = localmonitor;
}
#ifndef I4B_NOTCPIP_MONITOR
if (remotemonitor != -1) {
FD_SET(remotemonitor, &set);
if(remotemonitor > high_selfd)
high_selfd = remotemonitor;
}
#endif
/* if there are client connections, let monitor module
* enter them into the fdset */
if(accepted)
{
monitor_prepselect(&set, &high_selfd);
}
}
#endif
timeout.tv_sec = 1;
timeout.tv_usec = 0;
ret = select(high_selfd + 1, &set, NULL, NULL, &timeout);
if(ret > 0)
{
if(FD_ISSET(isdnfd, &set))
isdnrdhdl();
#ifdef USE_CURSES
if(FD_ISSET(fileno(stdin), &set))
kbdrdhdl();
#endif
#ifdef I4B_EXTERNAL_MONITOR
if(do_monitor)
{
if(localmonitor != -1 && FD_ISSET(localmonitor, &set))
monitor_handle_connect(localmonitor, 1);
#ifndef I4B_NOTCPIP_MONITOR
if(remotemonitor != -1 && FD_ISSET(remotemonitor, &set))
monitor_handle_connect(remotemonitor, 0);
#endif
if(accepted)
monitor_handle_input(&set);
}
#endif
}
else if(ret == -1)
{
if(errno != EINTR)
log(LL_WRN, "ERROR, select error on isdn device, errno = %d!", errno);
}
/* handle timeout and recovery */
handle_recovery();
}
}
#ifdef USE_CURSES
/*---------------------------------------------------------------------------*
* data from keyboard available, read and process it
*---------------------------------------------------------------------------*/
static void
kbdrdhdl(void)
{
int ch = getch();
switch(ch)
{
case 0x0c: /* control L */
wrefresh(curscr);
break;
case '\n':
case '\r':
do_menu();
break;
}
}
#endif
/*---------------------------------------------------------------------------*
* data from /dev/isdn available, read and process them
*---------------------------------------------------------------------------*/
static void
isdnrdhdl(void)
{
static unsigned char msg_rd_buf[MSG_BUF_SIZ];
msg_hdr_t *hp = (msg_hdr_t *)&msg_rd_buf[0];
register int len;
if((len = read(isdnfd, msg_rd_buf, MSG_BUF_SIZ)) > 0)
{
switch(hp->type)
{
case MSG_CONNECT_IND:
msg_connect_ind((msg_connect_ind_t *)msg_rd_buf);
break;
case MSG_CONNECT_ACTIVE_IND:
msg_connect_active_ind((msg_connect_active_ind_t *)msg_rd_buf);
break;
case MSG_DISCONNECT_IND:
msg_disconnect_ind((msg_disconnect_ind_t *)msg_rd_buf);
break;
case MSG_DIALOUT_IND:
msg_dialout((msg_dialout_ind_t *)msg_rd_buf);
break;
case MSG_ACCT_IND:
msg_accounting((msg_accounting_ind_t *)msg_rd_buf);
break;
case MSG_IDLE_TIMEOUT_IND:
msg_idle_timeout_ind((msg_idle_timeout_ind_t *)msg_rd_buf);
break;
case MSG_CHARGING_IND:
msg_charging_ind((msg_charging_ind_t *)msg_rd_buf);
break;
case MSG_PROCEEDING_IND:
msg_proceeding_ind((msg_proceeding_ind_t *)msg_rd_buf);
break;
case MSG_ALERT_IND:
msg_alert_ind((msg_alert_ind_t *)msg_rd_buf);
break;
case MSG_DRVRDISC_REQ:
msg_drvrdisc_req((msg_drvrdisc_req_t *)msg_rd_buf);
break;
case MSG_L12STAT_IND:
msg_l12stat_ind((msg_l12stat_ind_t *)msg_rd_buf);
break;
case MSG_TEIASG_IND:
msg_teiasg_ind((msg_teiasg_ind_t *)msg_rd_buf);
break;
case MSG_PDEACT_IND:
msg_pdeact_ind((msg_pdeact_ind_t *)msg_rd_buf);
break;
case MSG_NEGCOMP_IND:
msg_negcomplete_ind((msg_negcomplete_ind_t *)msg_rd_buf);
break;
case MSG_IFSTATE_CHANGED_IND:
msg_ifstatechg_ind((msg_ifstatechg_ind_t *)msg_rd_buf);
break;
default:
log(LL_WRN, "ERROR, unknown message received from /dev/isdn (0x%x)", msg_rd_buf[0]);
break;
}
}
else
{
log(LL_WRN, "ERROR, read error on isdn device, errno = %d, length = %d", errno, len);
}
}
/*---------------------------------------------------------------------------*
* re-read the config file on SIGHUP or menu command
*---------------------------------------------------------------------------*/
void
rereadconfig(int dummy)
{
extern int entrycount;
log(LL_DMN, "re-reading configuration file");
close_allactive();
entrycount = -1;
nentries = 0;
/* read runtime configuration file and configure ourselves */
configure(configfile, 1);
if(config_error_flag)
{
log(LL_ERR, "there were %d error(s) in the configuration file, terminating!", config_error_flag);
unlink(PIDFILE);
exit(1);
}
if(aliasing)
{
/* reread alias database */
free_aliases();
init_alias(aliasfile);
}
}
/*---------------------------------------------------------------------------*
* re-open the log/acct files on SIGUSR1
*---------------------------------------------------------------------------*/
void
reopenfiles(int dummy)
{
if(useacctfile)
{
fflush(acctfp);
fclose(acctfp);
if((acctfp = fopen(acctfile, "a")) == NULL)
{
log(LL_ERR, "ERROR, can't open acctfile %s for writing, terminating!", acctfile);
exit(1);
}
setvbuf(acctfp, (char *)NULL, _IONBF, 0);
}
if(uselogfile)
{
finish_log();
if((logfp = fopen(logfile, "a")) == NULL)
{
fprintf(stderr, "ERROR, cannot open logfile %s: %s\n",
logfile, strerror(errno));
exit(1);
}
/* set unbuffered operation */
setvbuf(logfp, (char *)NULL, _IONBF, 0);
}
}
/* EOF */
|