-
Notifications
You must be signed in to change notification settings - Fork 0
/
bx_function.c
1835 lines (1458 loc) · 47.2 KB
/
bx_function.c
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef LINT
static char sccs_bx_fun_id[] = "@(#)bx_function.c Version 1.10 Date 97/01/15 Hour 09:39:07";
#endif
/*---------------------------------------------------------------------------
apri_porta.c: funzione di apertura device e parametrizzazione
ambiente per la gestione della documentazione addebiti
Parametri in input (da programma INFORMIX-4GL):
- Numero centralino (1-x); questo per permettere
la gestione di piu' centralini collegati all'elaboratore
- Device da aprire (ex. /dev/tty12, tty18 etc.)
- Velocita' di set porta 9600,1200... (BPS)
- Bit di data (7,8)
- Bit di stop (1,2)
- Parita` (N = none,
E = even,
O = odd)
- Valorizzazione Shared Memory (1 = Si
0 = No)
---------------------------------------------------------------------------*/
#include "dls_include.h"
unsigned short BccNullo = FALSE;
int apri_porta(void)
{
#ifdef ULTRIX /* Only for Ultrix operating system */
int ldisc = TERMIODISC;
#endif
struct termio dls_termio;
unsigned short usa_shm;
char tmp[20],*kk;
if((dls_device=(IOCONTROL *)malloc(sizeof(IOCONTROL)))==(IOCONTROL *)NULL) {
dls_error("Can't allocate memory",(char *)NULL);
retint(ERROR);
return(1);
}
/*
* Parametri in input (da programma INFORMIX-4GL RDS)
*/
popshort(&usa_shm);
popquote(dls_device->dls_parity,sizeof(dls_device->dls_parity));
popshort(&dls_device->dls_bitstop);
popshort(&dls_device->dls_bitdata);
popint(&dls_device->dls_speed);
popquote(dls_device->dls_tty,sizeof(dls_device->dls_tty));
popshort(&dls_device->dls_npabx);
if(dls_device->dls_npabx > MAXPABX) {
fprintf(stderr,"Number of pabx %d exceeded MAX(%d)\n",dls_device->dls_npabx,MAXPABX);
retint(ERROR);
return(1);
}
else
shm_idx = dls_device->dls_npabx;
shm_idx--;
/*
* Elimino blank nelle stringhe (se presenti)
*/
if((kk = strchr((char *)dls_device->dls_tty,' ')) != (char *)NULL)
*kk = '\0';
if((kk = strchr((char *)dls_device->dls_tty,' ')) != (char *)NULL)
*kk = '\0';
(void)strcpy(tmp,(char *)dls_device->dls_tty);
/*
* Aggiungo suffisso "/dev/" se non presente nel parametro
*/
if((strstr((char *)dls_device->dls_tty,"/dev/")) == (char *)NULL)
(void)sprintf((char *)dls_device->dls_tty,"/dev/%s",tmp);
/*
* Apertura porta per lettura sospensiva
*/
if((dls_fdtty = open((char *)dls_device->dls_tty,O_RDWR)) == ERROR) {
dls_error("Can't open device",(char *)dls_device->dls_tty);
retint(ERROR);
return(1);
}
#ifdef ULTRIX
if(ioctl(dls_fdtty, TIOCSETD, &ldisc) == ERROR) {
dls_error("Can't set line discipline (ULTRIX) for",(char *)dls_device->dls_tty);
retint(ERROR);
return(1);
}
#endif
/*
* Apertura porta OK !
* Configuro il device con i parametri ricevuti (velocita', parita', ecc..)
*/
if ( ioctl(dls_fdtty,TCGETA,&dls_copia) == ERROR ) {
dls_error("Can't get terminal characteristics for",(char *)dls_device->dls_tty);
retint(ERROR);
return(1);
}
memcpy((void *)&dls_termio,(void *)&dls_copia,sizeof(struct termio));
dls_termio.c_cflag &= ~CBAUD;
dls_termio.c_cflag &= ~CSIZE;
dls_termio.c_iflag &= ~IXANY;
dls_termio.c_iflag &= ~IXOFF;
dls_termio.c_iflag &= ~IXON;
dls_termio.c_iflag |= IXON | IXOFF;
switch(dls_device->dls_speed) {
case 50:
dls_termio.c_cflag |= B50;
break;
case 75:
dls_termio.c_cflag |= B75;
break;
case 110:
dls_termio.c_cflag |= B110;
break;
case 134:
dls_termio.c_cflag |= B134;
break;
case 150:
dls_termio.c_cflag |= B150;
break;
case 200:
dls_termio.c_cflag |= B200;
break;
case 300:
dls_termio.c_cflag |= B300;
break;
case 600:
dls_termio.c_cflag |= B600;
break;
case 1200:
dls_termio.c_cflag |= B1200;
break;
case 2400:
dls_termio.c_cflag |= B2400;
break;
case 4800:
dls_termio.c_cflag |= B4800;
break;
case 9600:
dls_termio.c_cflag |= B9600;
break;
case 19200:
dls_termio.c_cflag |= B19200;
break;
default:
fprintf(stderr,"Invalid speed -> %d\n",dls_device->dls_speed);
retint(ERROR);
return(1);
break;
}
/*
* Set parita'
*/
switch(dls_device->dls_parity[0]) {
case 'E':
dls_termio.c_cflag &= ~PARODD;
dls_termio.c_cflag |= PARENB;
break;
case 'N':
dls_termio.c_cflag &= ~PARENB;
dls_termio.c_cflag &= ~PARODD;
break;
case 'O':
dls_termio.c_cflag |= PARENB;
dls_termio.c_cflag |= PARODD;
break;
default:
fprintf(stderr,"Invalid parity -> %s\n",dls_device->dls_parity);
retint(ERROR);
return(1);
break;
}
/*
* Set bit di data
*/
switch(dls_device->dls_bitdata) {
case 7:
dls_termio.c_cflag |= CS7;
break;
case 8:
dls_termio.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Invalid bit data -> %d\n",dls_device->dls_bitdata);
retint(ERROR);
return(1);
break;
}
/*
* Set bit di stop
*/
switch(dls_device->dls_bitstop) {
case 1:
dls_termio.c_cflag &= ~CSTOPB;
break;
case 2:
dls_termio.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Invalid bit stop -> %d\n",dls_device->dls_bitstop);
retint(ERROR);
return(1);
break;
}
dls_termio.c_lflag &= ~ECHO;
dls_termio.c_lflag &= ~ICANON;
dls_termio.c_cc[VTIME] = 0;
dls_termio.c_cc[VMIN] = 1;
if ( ioctl(dls_fdtty,TCSETA,&dls_termio) == ERROR ) {
dls_error("Can't set terminal characteristics for",(char *)dls_device->dls_tty);
retint(ERROR);
return(1);
}
/*
* Settaggio porta OK !
* Creo il semaforo per la sincronizzazione dell'accesso alla shared Memory
* (se utilizzata)
*/
if(usa_shm == TRUE) {
semshm = semget((key_t)SEMSHM,1,0660);
if(semshm == ERROR) {
dls_error("Cannot get semaphore","SHM");
retint(ERROR);
return(1);
}
}
/*
* Creazione semafori OK !
* Accedo Shared Memory
* (se richiesto)
*/
if(usa_shm == TRUE) {
if((dls_shmid = shmget((key_t)SHMKEY,sizeof(DLS_IPC),0660)) == ERROR) {
dls_error("Cannot access shared memory",(char *)NULL);
dls_sem_unlock(SEMSHM);
retint(ERROR);
return(1);
}
/*
* Carico Shared Memory
* (se richiesto)
*/
if((shm_pointer = shmat(dls_shmid,(char *)0,0)) == (char *)ERROR) {
dls_error("Cannot attache shared memory segment to my process",(char *)NULL);
retint(ERROR);
return(1);
}
dls_shm_pointer = (DLS_IPC *)shm_pointer;
dls_shm_pointer->dls_pabx1[shm_idx].start1 = time((time_t *)NULL);
dls_shm_pointer->dls_pabx1[shm_idx].status1 = CONNECTING;
(void)strcpy((char *)dls_shm_pointer->dls_pabx1[shm_idx].oper1,"Creation shared memory");
dls_shm_pointer->dls_pabx1[shm_idx].lastsend = (time_t)NULL;
dls_shm_pointer->dls_pabx1[shm_idx].msgsend = 0L;
dls_shm_pointer->dls_pabx1[shm_idx].dls_pid1 = getpid();
/*
* Creazione Shared Memory OK !
* Creo Coda di Messaggi
* (se richiesto)
*/
dls_sem_unlock(SEMSHM);
if((dls_msgid = msgget((key_t)MSGKEY,0660)) == ERROR) {
dls_error("Can't access message queue ID",(char *)NULL);
retint(ERROR);
return(1);
}
dls_sem_lock(SEMSHM);
(void)strcpy((char *)dls_shm_pointer->dls_pabx1[shm_idx].oper1,"Accessing message queue");
dls_sem_unlock(SEMSHM);
} /* fine if(usa_shm == TRUE)... */
/*
* Tutto OK !
* Ritorno 0 a 4GL (SUCCESSFUL !)
*/
retint(0);
return(1);
}
int dls_read(void)
/*---------------------------------------------------------------------------
dls_read: routine di gestione della lettura della porta in uso
Parametri in input: terminatore di riga (di solito '\n')
Parametri in output: stringa letta
---------------------------------------------------------------------------*/
{
int line_terminator;
char c,ret_buf[MAXSTRLEN];
int i = FALSE;
struct dls_msg {
long mtype;
char mtext[MAXSTRLEN];
};
struct dls_msg dls_msgstruct;
/*
* Terminatore di stringa passato da programma 4GL
* DEFAULT = '\n' (LineFeed) ASCII(10)
*/
popint(&line_terminator);
dls_setsig(1);
if(line_terminator == 0)
line_terminator = 10; /* valore ASCII di line feed ('\n') */
memset((void *)ret_buf,'\0',sizeof(ret_buf));
dls_sem_lock(SEMSHM);
dls_shm_pointer->dls_pabx1[shm_idx].status1 = RUNNING;
dls_sem_unlock(SEMSHM);
(void)strcpy((char *)dls_shm_pointer->dls_pabx1[shm_idx].oper1,"Reading tty");
while(read(dls_fdtty,&c,1) > 0) {
if(c == line_terminator || i == (MAXSTRLEN - 1)) {
/* raggiunto il termine di stringa */
if( i <= 10 ) { /* se ho letto 10 o meno caratteri riciclo */
i = FALSE;
continue;
}
if(i == (MAXSTRLEN - 1))
ret_buf[i] = '\0';
dls_msgstruct.mtype = 1L;
i = shm_idx + 1;
(void)sprintf(dls_msgstruct.mtext,"%02d",i);
(void)strcat(dls_msgstruct.mtext,ret_buf);
/*
* Blocco il semaforo per 1^ aggiornamento Shared Memory
*/
dls_sem_lock(SEMSHM);
(void)strcpy((char *)dls_shm_pointer->dls_pabx1[shm_idx].oper1,"Wait to send message");
dls_sem_unlock(SEMSHM);
if(msgsnd(dls_msgid,(void *)&dls_msgstruct,sizeof(ret_buf),0) == ERROR) {
dls_error("Can't send message queue",(char *)NULL);
dls_abort(1);
return(0);
}
/*
* Blocco il semaforo per 2^ aggiornamento Shared Memory
*/
dls_sem_lock(SEMSHM);
(void)strcpy((char *)dls_shm_pointer->dls_pabx1[shm_idx].oper1,"Message sent !");
dls_shm_pointer->dls_pabx1[shm_idx].msgsend++;
dls_shm_pointer->dls_pabx1[shm_idx].lastsend = time((time_t *)NULL);
memset((void *)ret_buf,'\0',sizeof(ret_buf));
i = FALSE;
(void)strcpy((char *)dls_shm_pointer->dls_pabx1[shm_idx].oper1,"Reading tty");
/*
* Libero il semaforo
*/
dls_sem_unlock(SEMSHM);
continue;
}
ret_buf[i] = c;
if(ret_buf[i] == '\0')
continue;
i++;
}
/*
* Uscito per errore fatale in lettura porta ! (EOF ?)
* Abort del processo
*/
(void)fprintf(stderr,"Fatal ERROR while reading: closing device\n");
dls_abort(1);
return(0);
}
int leggi_coda(void)
/*---------------------------------------------------------------------------
leggi_coda: lettura della coda di messaggi
Parametri in input (da programma INFORMIX-4GL): nessuno
Parametri ritornati al programma 4GL: stringa letta + return code
0 = OK !
1 = Database in accesso esclusivo
-1 = Errore fatale !
---------------------------------------------------------------------------*/
{
char ret_buf[MAXSTRLEN];
char tmp[81];
struct dls_msg {
long mtype;
char mtext[MAXSTRLEN];
};
struct dls_msg dls_msgstruct;
dls_setsig(2);
if(dls_shm_pointer == (DLS_IPC *)NULL) { /* accedo Shared memory */
/* accedo segnali */
if((dls_shmid = shmget((key_t)SHMKEY,sizeof(DLS_IPC),0660)) == ERROR) {
fprintf(stderr,"Shared Memory NOT OPENED !\n");
retquote("Shared Memory NOT OPENED !");
retint(ERROR);
return(2);
}
/*
* Carico Shared Memory
*/
if((shm_pointer = shmat(dls_shmid,(char *)0,0)) == (char *)ERROR) {
dls_error("Cannot attache shared memory segment to my process",(char *)NULL);
retquote("Cannot attach shared memory to my process !");
retint(ERROR);
return(2);
}
dls_shm_pointer = (DLS_IPC *)shm_pointer;
semshm = semget((key_t)SEMSHM,1,0660);
if(semshm == ERROR) {
dls_error("Cannot get semaphore","SHM");
retquote("Cannot get semaphore SHM");
retint(ERROR);
return(2);
}
dls_sem_lock(SEMSHM);
/*
* prima volta: valorizzo Shared Memory
*/
dls_shm_pointer->start2 = time((time_t *)NULL);
dls_shm_pointer->status2 = RUNNING;
dls_shm_pointer->dls_pid2 = getpid();
dls_sem_unlock(SEMSHM);
if((dls_msgid = msgget((key_t)MSGKEY,0660)) == ERROR) {
dls_error("Can't access message queue ID",(char *)NULL);
retquote("Can't access message queue ID");
retint(ERROR);
return(2);
}
}
if(msgrcv(dls_msgid,(void *)&dls_msgstruct,sizeof(ret_buf),1L,0) == ERROR) {
/*
* testo se e' stato interrotto da segnale (SIGHUP received)
*/
dls_sem_lock(SEMSHM);
dls_shm_pointer->start2 = (time_t)NULL;
dls_shm_pointer->dls_pid2 = (pid_t)FALSE;
dls_shm_pointer->status2 = NOTRUNNING;
if(dls_shm_pointer->db_status == TRUE) { /* Database in accesso esclusivo */
dls_shm_pointer->dls_pid2 = (pid_t)FALSE;
dls_shm_pointer->status2 = DBEXCLUSIVE;
dls_shm_pointer = (DLS_IPC *)NULL;
dls_sem_unlock(SEMSHM);
retquote("Database Exclusive Requested");
retint(TRUE);
return(2);
}
else { /* Errore fatale */
dls_shm_pointer = (DLS_IPC *)NULL;
dls_sem_unlock(SEMSHM);
dls_error("Can't receive message queue",(char *)NULL);
ora = time((time_t *)NULL);
dls_tm = localtime(&ora);
(void)strftime(tmp,sizeof(tmp),"%A %d %B %Y %H:%M",dls_tm);
fprintf(stderr,"Process stopped at %s\n",tmp);
retquote("Fatal error while receiving !");
retint(ERROR);
return(2);
}
}
/*
* messaggio ricevuto correttamente !
*/
/*
* Test per accesso esclusivo
*/
dls_sem_lock(SEMSHM);
if(dls_shm_pointer->db_status == TRUE) { /* Database in accesso esclusivo */
dls_shm_pointer->status2 = DBEXCLUSIVE;
dls_shm_pointer->dls_pid2 = (pid_t)FALSE;
dls_shm_pointer = (DLS_IPC *)NULL;
dls_sem_unlock(SEMSHM);
retquote("Database Exclusive Requested");
retint(TRUE);
return(2);
}
dls_shm_pointer->msgrecv++;
dls_shm_pointer->lastrecv = time((time_t *)NULL);
(void)strcpy((char *)dls_shm_pointer->laststr,dls_msgstruct.mtext);
strcpy(ret_buf,dls_msgstruct.mtext);
dls_sem_unlock(SEMSHM);
retquote(ret_buf);
retint(FALSE);
return(2);
}
int leggi_shm(void)
/*---------------------------------------------------------------------------
leggi_shm: lettura dello stato del Database
Parametri in input (da programma INFORMIX-4GL): nessuno
Parametri ritornati al programma 4GL: 0 = Database non esclusivo
1 = Database esclusivo
-1 = Shared memory non aperta
---------------------------------------------------------------------------*/
{
if(dls_shm_pointer == (DLS_IPC *)NULL) { /* Carico Shared Memory */
if((dls_shmid = shmget((key_t)SHMKEY,sizeof(DLS_IPC),0660)) == ERROR) {
dls_error("Cannot access shared memory",(char *)NULL);
retint(ERROR);
return(1);
}
/*
* Carico Shared Memory
*/
if((shm_pointer = shmat(dls_shmid,(char *)0,0)) == (char *)ERROR) {
dls_error("Cannot attache shared memory segment to my process",(char *)NULL);
retint(ERROR);
return(1);
}
dls_shm_pointer = (DLS_IPC *)shm_pointer;
}
dls_sem_lock(SEMSHM);
if(dls_shm_pointer->db_status == FALSE) { /* Database non in accesso esclusivo */
dls_sem_unlock(SEMSHM);
retint(FALSE);
return(1);
}
else { /* Database in accesso esclusivo */
dls_shm_pointer->start2 = (time_t)NULL;
dls_shm_pointer->dls_pid2 = (pid_t)FALSE;
dls_shm_pointer->status2 = DBEXCLUSIVE;
dls_shm_pointer = (DLS_IPC *)NULL;
dls_sem_unlock(SEMSHM);
retint(TRUE);
return(1);
}
}
void dls_sem_lock(unsigned int numero)
/*---------------------------------------------------------------------------
dls_sem_lock: routine per il lock del semaforo
Parametri in input: semaforo da trattare
Parametri in output: nessuno
---------------------------------------------------------------------------*/
{
switch(numero) {
case SEMSHM:
(void)semop(semshm,&locktalk,1);
break;
}
}
void dls_sem_unlock(unsigned int numero)
/*---------------------------------------------------------------------------
dls_sem_unlock: routine per unlock del semaforo
Parametri in input: semaforo da trattare
Parametri in output: nessuno
---------------------------------------------------------------------------*/
{
switch(numero) {
case SEMSHM:
(void)semctl(semshm,0,SETVAL,1);
break;
}
}
void dls_error(char *errore,char *parametro)
/*---------------------------------------------------------------------------
dls_error: routine di gestione errori
Parametri in input: descrizione errore
parametro aggiuntivo
Parametri in output: nessuno
---------------------------------------------------------------------------*/
{
char strerr[256];
(void)sprintf(strerr,"%s <%s> reason",errore,parametro);
perror(strerr);
}
void dls_abort(int sig)
/*---------------------------------------------------------------------------
dls_abort: chiusura del processo di lettura tty per errore fatale
Parametri in input: segnale rilevato
Parametri in output: nessuno
---------------------------------------------------------------------------*/
{
char tmp[81];
nil(sig);
if ( ioctl(dls_fdtty,TCSETA,(char *)&dls_copia) == ERROR )
dls_error("Can't set <default> terminal characteristics for",(char *)dls_device->dls_tty);
if(close(dls_fdtty) == ERROR)
dls_error("Can't close device for",(char *)dls_device->dls_tty);
dls_sem_lock(SEMSHM);
dls_shm_pointer->dls_pabx1[shm_idx].start1 = (time_t)NULL;
dls_shm_pointer->dls_pabx1[shm_idx].status1 = NOTRUNNING;
dls_shm_pointer->dls_pabx1[shm_idx].dls_pid1 = (pid_t)FALSE;
dls_sem_unlock(SEMSHM);
ora = time((time_t *)NULL);
dls_tm = localtime(&ora);
(void)strftime(tmp,sizeof(tmp),"%A %d %B %Y %H:%M",dls_tm);
fprintf(stderr,"Process stopped at %s\n",tmp);
exit(1);
}
void dls_abort1(int sig)
/*---------------------------------------------------------------------------
dls_abort1: chiusura del processo di lettura coda per errore fatale
Parametri in input: segnale rilevato
Parametri in output: nessuno
---------------------------------------------------------------------------*/
{
char tmp[81];
nil(sig);
dls_sem_lock(SEMSHM);
dls_shm_pointer->start2 = (time_t)NULL;
dls_shm_pointer->status2 = NOTRUNNING;
dls_shm_pointer->dls_pid2 = (pid_t)FALSE;
dls_sem_unlock(SEMSHM);
ora = time((time_t *)NULL);
dls_tm = localtime(&ora);
(void)strftime(tmp,sizeof(tmp),"%A %d %B %Y %H:%M",dls_tm);
fprintf(stderr,"Process stopped at %s\n",tmp);
exit(1);
}
void nil(int sig)
/*---------------------------------------------------------------------------
nil: funzione utilizzata in caso di segnali inviati al processo
---------------------------------------------------------------------------*/
{
char tmp[70];
unsigned int k;
ora = time((time_t *)NULL);
dls_tm = localtime(&ora);
(void)strftime(tmp,sizeof(tmp),"%A %d/%b/%Y %H:%M",dls_tm);
k = sig - 1;
fprintf(stderr,"Got signal number %d <%s> at %s\n",sig,c_segnali[k],tmp);
}
void dls_setsig(short caso)
/*---------------------------------------------------------------------------
dls_setsig: funzione per settare il trattamento dei segnali
ricevuti
---------------------------------------------------------------------------*/
{
signal(SIGHUP,nil); /* hangup */
signal(SIGCONT,nil);/* signale continue */
switch(caso) {
case 1:
signal(SIGINT,dls_abort); /* interrupt */
signal(SIGQUIT,dls_abort);/* quit */
signal(SIGFPE,dls_abort); /* floating point exception */
signal(SIGBUS,dls_abort); /* bus error */
signal(SIGSEGV,dls_abort);/* segmentation violation */
signal(SIGSYS,dls_abort); /* bad argument to system call */
signal(SIGTERM,dls_abort);/* software termination signal from kill */
break;
case 2: /* processo di lettura coda */
signal(SIGINT,dls_abort1); /* interrupt */
signal(SIGQUIT,dls_abort1);/* quit */
signal(SIGFPE,dls_abort1); /* floating point exception */
signal(SIGBUS,dls_abort1); /* bus error */
signal(SIGSEGV,dls_abort1);/* segmentation violation */
signal(SIGSYS,dls_abort1); /* bad argument to system call */
signal(SIGTERM,dls_abort1);/* software termination signal from kill */
break;
}
}
int dls_blocca_db(void)
/*---------------------------------------------------------------------------
dls_blocca_db: funzione richiamata da programma 4GL
per richiedere il database esclusivo
al programma di gestione centralini
parametri in input: nessuno
parametri in output: 0 OK !
-1 KO
---------------------------------------------------------------------------*/
{
struct dls_msg {
long mtype;
char mtext[MAXSTRLEN];
};
struct dls_msg dls_msgstruct;
if(dls_shm_pointer == (DLS_IPC *)NULL) {
if((dls_shmid = shmget((key_t)SHMKEY,sizeof(DLS_IPC),0660)) == ERROR) {
dls_error("Cannot access shared memory",(char *)NULL);
retint(ERROR);
return(1);
}
/*
* Carico Shared Memory
*/
if((shm_pointer = shmat(dls_shmid,(char *)0,0)) == (char *)ERROR) {
dls_error("Cannot attache shared memory segment to my process",(char *)NULL);
retint(ERROR);
return(1);
}
dls_shm_pointer = (DLS_IPC *)shm_pointer;
}
dls_shm_pointer->db_status = TRUE;
if(dls_shm_pointer->dls_pid2 == (pid_t)FALSE) { /* processo gia` inattivo */
retint(0);
return(1);
}
if((dls_msgid = msgget((key_t)MSGKEY,0660)) == ERROR) {
dls_error("Can't access message queue ID",(char *)NULL);
retint(ERROR);
return(1);
}
dls_msgstruct.mtype = 1L;
(void)strcpy(dls_msgstruct.mtext,"Database exclusive");
if(msgsnd(dls_msgid,(void *)&dls_msgstruct,sizeof(MAXSTRLEN),0) == ERROR) {
dls_error("Can't send message queue",(char *)NULL);
retint(ERROR);
return(1);
}
retint(0);
return(1);
}
int dls_sblocca_db(void)
/*---------------------------------------------------------------------------
dls_sblocca_db: funzione richiamata da programma 4GL
per riattivare il programma di gestione centralini
parametri in input: nessuno
parametri in output: 0 OK !
-1 KO
---------------------------------------------------------------------------*/
{
if(dls_shm_pointer == (DLS_IPC *)NULL) {
if((dls_shmid = shmget((key_t)SHMKEY,sizeof(DLS_IPC),0660)) == ERROR) {
dls_error("Cannot access shared memory",(char *)NULL);
retint(ERROR);
return(1);
}
/*
* Carico Shared Memory
*/
if((shm_pointer = shmat(dls_shmid,(char *)0,0)) == (char *)ERROR) {
dls_error("Cannot attache shared memory segment to my process",(char *)NULL);
retint(ERROR);
return(1);
}
dls_shm_pointer = (DLS_IPC *)shm_pointer;
}
dls_shm_pointer->db_status = FALSE;
retint(0);
return(1);
}
/*******************************************************************************
blocco - Funzione di gestione accesso alla procedura
*******************************************************************************/
int blocco()
{
int ind;
int esco;
void uscita();
char *getenv();
char *comodo;
if(shm_utenti_pointer == (char *)NULL) {
/************************
Apro la share memory
************************/
if((shm_ute_id=shmget(CHKSHM,sizeof(USER_ON),0660)) == -1) {
retint(10);
return(1);
}
if((shm_utenti_pointer=shmat(shm_ute_id,(char *)0,0)) == (char *)-1) {
perror("Errore nel prendere la porzione di Share Memory !\nCausa ");
exit(-1);
}
/*********************************
Creo semaforo di controllo SHM
*********************************/
if((sem_ute_id=semget(CHKSEM,1,0660)) == -1) {
perror("Errore di apertura semaf. di controllo Share Memory\nCausa ");
exit(-1);
}
}
shm_utenti = (USER_ON *)shm_utenti_pointer;
(void)semop(sem_ute_id,&sem_operation,1);
/*******************************************
Controllo il numero di utenti collegati
*******************************************/
if(shm_utenti->attivi + 1 > shm_utenti->licenza) {
/***************************
Troppi utenti collegati
***************************/
retint(0);
return(1);
}
for(ind=0,esco=0;ind < MAXUSER && !esco;ind++) {
/**********************
Inserisco l'utente
**********************/
if(shm_utenti->user_on[ind].id_proc == 0) {
shm_utenti->user_on[ind].id_proc = (int)getpid();
comodo = getenv("USER");
sprintf(shm_utenti->user_on[ind].utente,"%s",comodo);
esco = 1;
}
}
/**********************************************
Incremento il numero degli utenti collegati
**********************************************/
shm_utenti->attivi++;
/***********************
Sblocco il semaforo
***********************/
(void)semctl(sem_ute_id,0,SETVAL,1);
retint(1);
return(1);
}
/*******************************************************************************
sblocco - Funzione di gestione uscita dalla procedura
*******************************************************************************/
int sblocco()
{
int ind;
int esco;
void uscita();
pid_t processo;
if(shm_utenti_pointer == (char *)NULL) {
/************************
Apro la share memory
************************/
if((shm_ute_id=shmget(CHKSHM,sizeof(USER_ON),0660)) == -1) {
retint(10);
return(1);
}