File: engcalls.cpp

package info (click to toggle)
raidutils 0.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,844 kB
  • ctags: 22,075
  • sloc: cpp: 39,794; ansic: 22,774; sh: 8,306; makefile: 19
file content (1241 lines) | stat: -rw-r--r-- 40,781 bytes parent folder | download | duplicates (5)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
/* Copyright (c) 1996-2004, Adaptec Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - 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.
 * - Neither the name of the Adaptec Corporation nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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.
 */

/* File - ENGCALLS.CPP */
/*****************************************************************************/
/*                                                                           */
/*Description:                                                               */
/*        This Module Contains The Functions That Allow An Application To    */
/*        Access The DPT Engine Entry Points.                                */
/*                                                                           */
/*                                                                           */
/*Author: Bob Pasteur                                                        */
/*Date:                                                                      */
/*                                                                           */
/*Editors:                                                                   */
/*                                                                           */
/*Remarks:                                                                   */
/*                                                                           */
/*****************************************************************************/

/*Include Files -------------------------------------------------------------*/

#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
#include  <unistd.h>
#include  <sys/types.h>
#if !defined __MWERKS__
#include  <sys/wait.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>
#include  <sys/msg.h>
#endif
#include  <signal.h>
#include  <errno.h>
#include  "osd_util.h"
#include  "eng_std.h"
#include  "rtncodes.h"
#include  "messages.h"
#include  "osd_unix.h" 
//#include  "eng_std.h"
#include  "dpt_buff.h"
#include  "engmsg.h"
#include  "engine.h"

#ifdef _DPT_LOGGER
#include "sem.hpp"
extern MuxEvent_C loggerThread_X;
#endif

#ifdef __cplusplus

extern "C"
  {

#endif

/* Definitions - Defines & Constants ---------------------------------------*/

/* Definitions - Structure & Typedef ---------------------------------------*/

/* Variables - External ----------------------------------------------------*/

extern char **environ;
extern uLONG DptEnginePid;

/* Variables - Global ------------------------------------------------------*/

#ifdef MESSAGES

static int MsgID = -1;
static int BufferID = -1;

#if defined (HAS_COMM_ENGINE) && defined (NEW_COMM_ENG)
static int commMsgID = -1;
#endif 

static int SignalSetUp = 0;
static uLONG toEngBuffSize = 0;
static uLONG toEngTotalSize = 0;
static uLONG fromEngBuffSize = 0;
static uLONG fromEngTotalSize = 0;
static uLONG fromEngBuffOffset = 0;
static char *SharedMemoryPtr = NULL;
static char *ToEngPtr;
static char *FromEngPtr;
char EngineLoadString[100] = {'Z','\0'};
char CommEngineLoadString[100] = {'Z','\0'};
int EngineMessageTimeout = 0;

#include "findpath.h"
char *EnginePathName = "raideng";
char *CommEnginePathName = "dptcom";
#if defined(_DPT_UNIXWARE) || defined ( _DPT_DGUX )

char *EnginePathNameDefault = "/var/dpt/raideng &";
char *CommEnginePathNameDefault = "/var/dpt/dptcom &";
char *DefaultPathName = "/var/dpt/";

#elif defined(_DPT_AIX)

char *EnginePathNameDefault = "/usr/lpp/dpt/raideng &";
char *CommEnginePathNameDefault = "/usr/lpp/dpt/dptcom &";
char *DefaultPathName = "/usr/lpp/dpt/";

#elif defined (SNI_MIPS)

char *EnginePathNameDefault = "/opt/dpt/lib/raideng &";
char *CommEnginePathNameDefault = "/opt/dpt/lib/dptcom &";
char *DefaultPathName = "/opt/dpt/lib/";

#elif defined (_DPT_SOLARIS)

char *EnginePathNameDefault = "/opt/SUNWhwrdg/raideng";
char *CommEnginePathNameDefault = "/opt/SUNWhwrdg/dptcom";
char *DefaultPathName = "/opt/SUNWhwrdg/";

#else

char *EnginePathNameDefault = "/usr/dpt/raideng &";
char *CommEnginePathNameDefault = "/usr/dpt/dptcom &";
char *DefaultPathName = "/usr/dpt/";

#endif  /* _DPT_UNIXWARE else */

#endif  /* MESSAGES */

/* Prototypes - function ---------------------------------------------------*/
#if (defined(DPT_DEBUG))
void I2oPrintMem(char* Addr,long Count);
#endif

uLONG DPT_OpenEngine(void);
uLONG DPT_CloseEngine(void);
DPT_RTN_T DPT_CallEngine(DPT_TAG_T EngineTag, DPT_MSG_T event, 
                         DPT_TAG_T target, void *fromEng_P, 
                         void *toEng_P,uLONG timeout);

#if defined (HAS_COMM_ENGINE) && defined (NEW_COMM_ENG)

uLONG DPT_OpenCommEng(char *iniFile);
uLONG DPT_CloseCommEng(void);
DPT_RTN_T DPT_CallCommEng(DPT_TAG_T EngineTag, DPT_MSG_T event, 
                         DPT_TAG_T target, void *fromEng_P, 
                         void *toEng_P,uLONG timeout);

#endif  // has comm, new comm

uLONG DPT_IsEngineAlive(void) ;

#ifdef MESSAGES

char *AllocBuffers(uLONG toEngSize, uLONG fromEngSize);
void DeAllocBuffers(void);
void DptSignalHandler(int SignalToHandle);
int SetAlarm(void);

void SetEngineLoadPath(char *PathName);

#endif /* MESSAGES */

/* Prototypes - external function ------------------------------------------*/



/*-------------------------------------------------------------------------*/
/*                       Function DPT_CallAnEngine                         */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed To This Function Are :                            */
/*     DestinationKey : msq key for destination (i.e. DPT_EngineKey)       */
/*     EngineTag : Engine Tag                                              */
/*     event : Event Message To Pass to The Engine                         */
/*     target : Tag Of Target                                              */
/*                                                                         */
/* This Function Makes The Actual Call to The Engine Through Messaging.    */
/* The Pertinant Buffer Information And Data Are Copied To And From The    */
/* Shared Memory Buffers That Are Allocated By This Module.                */
/*                                                                         */
/* Return : See rtncodes.h for explanation of DPT_RTN_T codes.             */
/*-------------------------------------------------------------------------*/

DPT_RTN_T DPT_CallAnEngine(long DestinationKey,
                           DPT_TAG_T EngineTag, DPT_MSG_T event, 
                           DPT_TAG_T target, void *fromEng_P, 
                           void *toEng_P,uLONG timeout)
{
  DPT_RTN_T Rtnval = ERR_UNSPECIFIED;

#ifdef MESSAGES

  int i;
  MsgHdr HdrBuff;
  uLONG FromSize,ToSize;
  int OldAlarmValue = 0;
  uLONG p_id;
  int Done = 0;

  /* If We Havn't Gotten The Message Queue ID Yet ( The Open Call Has */
  /* Not Been Made) or the engine is not out there, Return An Error   */

  if(MsgID == -1)
   {
     return(ERR_NO_MSG_QUEUE_FOUND);
   }

  //
  // If we already have the PID of the engine, and the engine isn't alive,
  // return an error
  //
  if((DptEnginePid) && (!(DPT_IsEngineAlive())))
   {
     return(ERR_CANNOT_SIGNAL_PROCESS);
   }

  //
  // Get our PID for the command 
  //
  p_id = getpid();

#ifdef _DPT_LOGGER
  //
  // If this is the logger, we have to semaphore commands sent to the engine
  // due to the threading put into the logger
  //
  loggerThread_X.GainAccess();
#endif

  //
  // Get the buffer sizes for the passed in command
  //
  FromSize = ((dptBuffer_S *)fromEng_P)->allocSize;
  ToSize = ((dptBuffer_S *)toEng_P)->allocSize;

  //
  // If we are going to time this one, save off the old timmer value
  //
  if(timeout)
   {
     OldAlarmValue = alarm(0); 
   }

  //
  // As part of the robustness added, we will loop through until we have an error
  // or receive back the response that we sent out
  //
  while(!Done)
   {
    Done = 1;

    //
    // If there are any messages on the queue with a type of our process ID, 
    // they must be failed retries or some other error condition, so pull 
    // them off and discard them
    //
    i = msgrcv(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,p_id,IPC_NOWAIT);
    while(i != -1)
     {
       i = msgrcv(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,p_id,IPC_NOWAIT);
     }

    //
    // If the shared memory buffers are not big enough to handle this command
    // allocate buffers big enough
    // 
    if((SharedMemoryPtr == NULL)||(FromSize > fromEngBuffSize)||
      (ToSize > toEngBuffSize))
       {
         if(AllocBuffers(ToSize,FromSize) == NULL)
           {
             printf("\nShared Memory Allocation Failed, To Size = %lx, From Size = %lx, Errno = %x",
                     (unsigned long)ToSize, (unsigned long)FromSize,  errno);
#ifdef _DPT_LOGGER    
             //
             // If this is the logger, release the semaphore
             //
             loggerThread_X.ReleaseAccess();
#endif

             return(ERR_CANNOT_ALLOC_BUFFERS);
           }
       }
 
    //
    // Copy over the data from the passed In buffers into our
    // shared memory buffers
    //
    memcpy(ToEngPtr,toEng_P,sizeof(dptBuffer_S) + 
                             (uINT)((dptBuffer_S *)toEng_P)->writeIndex);
    memcpy(FromEngPtr,fromEng_P,sizeof(dptBuffer_S));

    //
    // Set up the message header
    //
    HdrBuff.engineTag = EngineTag;
    HdrBuff.engEvent = (uLONG)event;
    HdrBuff.targetTag = target;
    HdrBuff.callerID = p_id;
    HdrBuff.BufferID = BufferID;
    HdrBuff.FromEngBuffOffset = fromEngBuffOffset;
    HdrBuff.timeOut = timeout;
    HdrBuff.result = 0;

    //
    // Set up the proper message type in the buffer header
    //
    HdrBuff.MsgID = DestinationKey;

    //
    // Send off the message to the engine
    //
    if((i = (msgsnd(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,0) != -1)))
     {
       //
       // Set up the alarm if we are timming
       //
       EngineMessageTimeout = 0;
       if(timeout)
        {
          alarm((int)timeout); 
        }

       //
       // Wait for the return message from the engine
       //
       while((i = msgrcv(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,p_id,0)) == -1)
        {  
          //
          // If we have a timeout, exit the loop
          //
          if(EngineMessageTimeout)
           {
             Rtnval = ERR_MSGRCV_TIMEOUT;
             break;
           }

          //
          // if there was an error receiving the message, exit the loop
          //
          if(errno != EINTR)
           {
             Rtnval = ERR_MSGRCV_FAILED;
             break;
           }

        } //while((i = msgrcv(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,p_id,0)) == -1)

       //
       // If we were timming, turn off the alarm
       //
       if(timeout)
        {
         alarm(0);
        }

       //
       // For a failed message receive set up an error code
       //
       if(i == -1)  
        {
          if(Rtnval == ERR_UNSPECIFIED) Rtnval = ERR_MSGRCV_FAILED;
          HdrBuff.result = 0xff;
          DeAllocBuffers();
        }

        //
        // The message received OK, so validate that this is the
        // message we sent out by checking the engine event and
        // the shared memory segment 
        //
        else {
               if((HdrBuff.engEvent == (uLONG)event)&&(HdrBuff.BufferID == BufferID))
                {
                  //
                  // We have a winner, so copy over the returned data
                  //
                  Rtnval = HdrBuff.result;
                  memcpy(fromEng_P,FromEngPtr,sizeof(dptBuffer_S) +
                         (uINT)((dptBuffer_S *)FromEngPtr)->writeIndex); 
                }

                //
                // We received the wrong message back. Here we will discard the message
                // we just received, and wait for the one we sent. If we receive back the
                // original one we sent, we will have to resend it because of the shared
                // memory, we can't be sure that the data is valid.
                //
                else {
                       EngineMessageTimeout = 0;
                       if(timeout)
                        {
                          alarm((int)timeout); 
                        }

                       //
                       // Wait for the original
                       //
                       while((i = msgrcv(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,p_id,0)) == -1)
                        {  
                         if(EngineMessageTimeout)
                          {
                            Rtnval = ERR_MSGRCV_TIMEOUT;
                            break;
                          }
                         if(errno != EINTR)
                          {
                            Rtnval = ERR_MSGRCV_FAILED;
                            break;
                          }

                        } //while((i = msgrcv(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,p_id,0)) == -1)

                       //
                       // Turn off the alarm
                       //
                       if(timeout)
                        {
                          alarm(0);
                        }

                       //
                       // Deallocate the buffers here. This way, the next command will
                       // have a different shared memory segment that can be used as
                       // part of the validation process
                       //
                       DeAllocBuffers();

                       //
                       // For a failed message receive set up an error code
                       //
                       if(i == -1)  
                        {
                          if(Rtnval == ERR_UNSPECIFIED) Rtnval = ERR_MSGRCV_FAILED;
                          HdrBuff.result = 0xff;
                        }

                       //
                       // We received the message back, so lets go back and send it again
                       //
                        else { 
                               Done = 0;
                        }

                   } //if((HdrBuff.engEvent == (uLONG)event)&&(HdrBuff.BufferID == BufferID)) else

        } //if(i == -1)   else


      } //if(i = msgsnd(MsgID,(struct msgbuf *)&HdrBuff,MsgDataSize,0) != -1)

	  else {

		  Rtnval = ERR_MSGSND_FAILED;

// printf("\nError sending message in CallAnEngine = %d",errno);
	  }


   } //while(!Done)

#endif  /* MESSAGES */

#ifdef NO_MESSAGES

  Rtnval = DPT_Engine(EngineTag,event,target,fromEng_P,toEng_P,timeout);

  //
  // The comm engine will not work with no messaging
  //
#if defined (HAS_COMM_ENGINE)
#error This will not work for the comm engine! You must fix it!
#endif  // has comm

#endif  /* NO_MESSAGES */		

#ifdef _DPT_LOGGER    
  //
  // If this is the logger, release the semaphore
  //
  loggerThread_X.ReleaseAccess();

#endif

  //
  // If there was an alarm set on the way in, reset it
  //
  if(OldAlarmValue)
   {
     alarm(OldAlarmValue);
   }

  return(Rtnval);
}


/*-------------------------------------------------------------------------*/
/*                         Function DPT_OpenEngine                         */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed Into This Function                       */
/*                                                                         */
/* This Function Should Be Called By An Application Before Any Attempt Is  */
/* Made To Access The Engine. A Check For A Running Engine Is Made, And If */
/* No Engine Is Found, The Function Will Try To Load The Engine. If A      */
/* Running Engine Is Found, An Open Call Is Made To The Engine.            */
/*                                                                         */
/* Return : See rtncodes.h for explanation of DPT_RTN_T codes.             */
/*-------------------------------------------------------------------------*/

uLONG DPT_OpenEngine(void)
  {
    uLONG Rtnval;

#ifdef MESSAGES

    int i;
    int WaitVal;

    Rtnval = ERR_UNSPECIFIED;

  /* If The Signal Handler Isn't Set Up Yet, Do So */

   if(!SignalSetUp)
     {
       if(SetAlarm())
        {
          return(ERR_CANNOT_SET_SIGHANDLER);
        }
       SignalSetUp = 1;  
     }

  /* Check To See If There Is An Engine Out There */

    if(MsgID == -1)
     {
       MsgID = CheckForEngine(DPT_TurnAroundKey,1,2);
     }

 /* If We Didn't Find One, Try To Load It */

    if((MsgID == -1)&&(strlen(EngineLoadString)))
      {
        if((EngineLoadString[0] == 'Z')&&(EngineLoadString[1] == '\0')) 
          {
            EnginePathName = FindPath ((CONST char *)EnginePathName, X_OK);
            if(EnginePathName != NULL) 
             {
               strcpy(EngineLoadString,EnginePathName);
               strcat(EngineLoadString, "&");
             }
             else {
                    strcpy(EngineLoadString,EnginePathNameDefault);
             }
          }

#if defined (_DPT_SOLARIS) || defined (_DPT_MOTIF)
        i = strlen(EngineLoadString) - 1;
        if(EngineLoadString[i] == '&')
         {
           EngineLoadString[i] = '\0';
         }       
	if( !(i = fork()) ) {
            execle(EngineLoadString,EngineLoadString, 0, environ);
	     _exit(1);
	}
        
        WaitVal = 0;
        while((WaitVal != i)&&(WaitVal != -1))
         {
           WaitVal = wait(NULL);
         }
#else
        i = system(EngineLoadString);
#endif


  /* If The system Call Was Successful, Wait For 1 Second And */
  /* Check For The Engine Again                               */

        if(i != -1)
          {
              for (i = 10; i > 0 && MsgID == -1; --i) {
                sleep(1);
                MsgID = CheckForEngine(DPT_TurnAroundKey,1,2);
              }
          }
      }

    //
    // If the engine is out there, make an open call */
    //
    if(MsgID != -1)
      {

        Rtnval = MessageDPTEngine(DPT_TurnAroundKey,MsgID,ENGMSG_OPEN,2);

      }

#endif  /* MESSAGES */

#ifdef NO_MESSAGES

    Rtnval = DPT_StartEngine();

#endif  /* NO_MESSAGES */

    return(Rtnval);
  }

/*-------------------------------------------------------------------------*/
/*                         Function DPT_CloseEngine                        */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed Into This Function                       */
/*                                                                         */
/* This Function Should Be Called When An Application Is Finished With The */
/* Engine. A Close Call Is Made To The Engine, And All Allocated Resources */
/* Are Freed.                                                              */
/*                                                                         */
/* Return : See rtncodes.h for explanation of DPT_RTN_T codes.             */
/*-------------------------------------------------------------------------*/

uLONG DPT_CloseEngine(void) 
  {
    uLONG Rtnval;

#ifdef MESSAGES

    Rtnval = ERR_UNSPECIFIED;

  /* Make Sure That The Engine Was Properly Opened */

    if(MsgID != -1)
      {

  /* Make The Close Call */

        Rtnval = MessageDPTEngine(DPT_TurnAroundKey,MsgID,ENGMSG_CLOSE,2);

  /* If This Is The Last Client Closing, We Will Get An Error Of */
  /* Message Que Removed Because The Engine Commited Suicide.    */
  /* Return A Good Status                                        */
#	ifndef EIDRM
#	   define EIDRM 82
#	endif

        if(Rtnval == EIDRM)
           Rtnval = MSG_RTN_COMPLETED;
      }

  /* Free Up The Resources */

    DeAllocBuffers();

#endif  /* MESSAGES */

#ifdef NO_MESSAGES

    Rtnval = DPT_StopEngine();

#endif  /* NO_MESSAGES */
    return(Rtnval);
  }

/*-------------------------------------------------------------------------*/
/*                         Function DPT_CallEngine                         */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed To This Function Are :                            */
/*     EngineTag : Engine Tag                                              */
/*     event : Event Message To Pass to The Engine                         */
/*     target : Tag Of Target                                              */
/*                                                                         */
/* This Function Makes The Actual Call to The Engine Through Messaging.    */
/* The Pertinant Buffer Information And Data Are Copied To And From The    */
/* Shared Memory Buffers That Are Allocated By This Module.                */
/*                                                                         */
/* Return : See rtncodes.h for explanation of DPT_RTN_T codes.             */
/*-------------------------------------------------------------------------*/

DPT_RTN_T DPT_CallEngine(DPT_TAG_T EngineTag, DPT_MSG_T event, 
                         DPT_TAG_T target, void *fromEng_P, 
                         void *toEng_P,uLONG timeout)
  {
    /* we just pass this on with the appropriate key */
    return DPT_CallAnEngine(DPT_EngineKey, EngineTag, event,
                            target, fromEng_P, toEng_P, timeout);
  }


#if defined (HAS_COMM_ENGINE) && defined (NEW_COMM_ENG)


/*-------------------------------------------------------------------------*/
/*                       Function DPT_OpenCommEng                          */
/*-------------------------------------------------------------------------*/
/*                                                                         */
/* This function should be called by an application before any attempt is  */
/* made to access the comm engine.  A check for a running comm engine is   */
/* made, and if no comm engine is found, the function will try to load     */
/* one.  If a running engine is founf we will place an open call to it.    */
/*                                                                         */
/* If iniFile is not NULL, we will add the INI= option to the load string. */
/*                                                                         */
/* Return : 0 If Open Is Sucessfull, 1 Otherwise                           */
/*-------------------------------------------------------------------------*/

uLONG DPT_OpenCommEng(char *iniFile)
{
	uLONG Rtnval = 1;
    char IniParams[50];

	int i;

	/* If the signal handler isn't set up yet, do so */
	if (!SignalSetUp)
	{
		if (SetAlarm())
         {
			return(1);
         }
		SignalSetUp = 1;
	}

	/* Check to see if there is a Comm Engine out there */
	if (commMsgID == -1)
     {
		commMsgID = CheckForCommEng(DPT_CommEngineKey,1,2);
     }

	/* If we didn't find one, try to load it */
	if ((commMsgID == -1) && (strlen(CommEngineLoadString)))
	{
          if ((CommEngineLoadString[0] == 'Z') && 
              (CommEngineLoadString[1] == '\0')) 
           {
             CommEnginePathName = 
                     FindPath((CONST char *)CommEnginePathName, X_OK);
          
             if(CommEnginePathName != NULL)
              {
                strcpy(CommEngineLoadString, CommEnginePathName);
                strcat(CommEngineLoadString, "&");
              }
              else {
                     strcpy(CommEngineLoadString, CommEnginePathNameDefault);
              }
           }
          IniParams[0] = '\0';

		/* actually do the load */
#if defined (_DPT_SOLARIS) || defined (_DPT_MOTIF)
                i = strlen(CommEngineLoadString) - 1;
                if(CommEngineLoadString[i] == '&')
                 {
                   CommEngineLoadString[i] = '\0';
                 }       

		if( !(i = fork()) ) {
                   //
                   // DEBUG CODE : For now we will not use the passed in ini
                   //              file name because it causes an error
                   //              message to be displayed
                   //
                   iniFile = NULL;

		   if (iniFile)
                    {
                      strcpy(IniParams, " INI=");
                      strcat(IniParams, iniFile);
                      execle(CommEngineLoadString,CommEngineLoadString, 
                             IniParams, 0, environ);
                    }
                    else {
                           execle(CommEngineLoadString,CommEngineLoadString, 
                                  0, environ);
                    }
		    _exit(1);
		}
#else
		if (iniFile)
		{
		  char *endP = 
                     &(CommEngineLoadString[strlen(CommEngineLoadString)-1]);
   		       // "-INI=xxx"
                  strcpy(IniParams, " -INI=");
                  strcat(IniParams, iniFile);
		  strcat(IniParams, " &");
                  strcat(end_P,IniParams);
		}
		i = system(CommEngineLoadString);
#endif

		/* If the system call was successful, wait for 1 second and */
		/* check for the Comm Engine again                          */
		if (i != -1)
		{
			uLONG waiting = 20;	// wait for up to this many seconds
			while ((commMsgID == -1) && (waiting--))
			{
//DEBUG - might need this when debugging modem init
//printf("\nwaiting for comm eng %d\n", waiting);
				sleep(1);
				commMsgID = CheckForCommEng(DPT_CommEngineKey,1,2);
			}
		}
	}

	/* If we found a Comm Engine, make an open call to the unix Comm Engine  */
	if (commMsgID != -1)
	{
		Rtnval = MessageDPTEngine(DPT_CommEngineKey,commMsgID,ENGMSG_OPEN,2);

		// for use with the comm server, we need to set msqID to commMsgID
		// (we can safely do this because we know that the comm engine is running
		//  and that it will have started the engine by now)
		if (MsgID == -1)
         {
			MsgID = commMsgID;
         }
	}

	return(Rtnval);
}


/*-------------------------------------------------------------------------*/
/*                      Function DPT_CloseCommEng                          */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed Into This Function                       */
/*                                                                         */
/* This function should be called when an application is finished with the */
/* Comm Engine. A close call is made to the Comm Engine, and all allocated */
/* resources are freed.                                                    */
/*                                                                         */
/* Return : 0 If Close Is Sucessfull, 1 Otherwise                          */
/*-------------------------------------------------------------------------*/

uLONG DPT_CloseCommEng(void) 
{
	uLONG Rtnval = 1;

	/* Make sure that the comm engine was properly opened */
	if (commMsgID != -1)
	{
		/* make the close call */
		Rtnval = MessageDPTEngine(DPT_CommEngineKey,commMsgID,ENGMSG_CLOSE,2);

		/* If this is the last client closing, we will get an error of */
		/* message que removed because the engine commited suicide.    */
		/* Return a good status                                        */
		if (Rtnval == EIDRM)
			Rtnval = 0;
	}

      /* Free Up The Resources */

       DeAllocBuffers();

       return(Rtnval);
}


/*-------------------------------------------------------------------------*/
/*                      Function DPT_CallCommEng                           */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed To This Function Are :                            */
/*     EngineTag : Engine Tag                                              */
/*     event : Event Message To Pass to The Engine                         */
/*     target : Tag Of Target                                              */
/*                                                                         */
/* This Function Makes The Actual Call to The Engine Through Messaging.    */
/* The Pertinant Buffer Information And Data Are Copied To And From The    */
/* Shared Memory Buffers That Are Allocated By This Module.                */
/*                                                                         */
/* Return : Return Code From DPT_CallEngine                                */
/*-------------------------------------------------------------------------*/

DPT_RTN_T DPT_CallCommEng(DPT_TAG_T EngineTag, DPT_MSG_T event, 
                         DPT_TAG_T target, void *fromEng_P, 
                         void *toEng_P,uLONG timeout)
{
	/* we just pass this on with the appropriate key */
	return DPT_CallAnEngine(DPT_CommEngineKey, EngineTag, event,
									target, fromEng_P, toEng_P, timeout);
}


#endif  // has comm, new comm

/*-------------------------------------------------------------------------*/
/*                      Function DPT_IsEngineAlive                         */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed Into This Function                       */
/*                                                                         */
/* This function should be called when an application wants to determine   */
/* if the engine is still alive. It should only be called after a          */
/* sucessfull call to DPT_OpenEngine has been made. This function should   */
/* probably be used for a timeout situation.                               */
/*                                                                         */
/* Return : 1 if engine is alive, 0 otherwise                              */
/*-------------------------------------------------------------------------*/

uLONG DPT_IsEngineAlive(void) 
{
  uLONG Rtnval = 0;

  if(DptEnginePid)
   {
     if(kill((pid_t)DptEnginePid,0) == 0)
      {
        Rtnval = 1;
      }
   }
  return(Rtnval);
}
#ifdef MESSAGES

/*-------------------------------------------------------------------------*/
/*                         Function AllocBuffers                           */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed In To This Function Are :                         */
/*     toEngSize : Size Of The To Engine Data Buffer                       */
/*     fromEngSize : Size Of The from Engine Data Buffer                   */
/*                                                                         */
/* This Function Will Allocate The Shared Memory Buffers                   */
/*                                                                         */
/* Return : Pointer To The Attached Shared Memory, 0 If Failure            */
/*-------------------------------------------------------------------------*/

char *AllocBuffers(uLONG toEngSize, uLONG fromEngSize)
  {
    struct shmid_ds shm_buff;

  /* If There Is Currently A Shared Memory Segment Set Up, DeAllocate It */

    DeAllocBuffers();

  /* Set Up The New inBuff And outBuff Sizes */

    toEngBuffSize = toEngSize;
    toEngTotalSize = toEngSize + sizeof(dptBuffer_S);
#if defined (_DPT_STRICT_ALIGN)
	// alignment
    if (toEngTotalSize & 0x03) {
	toEngTotalSize += 4;
	toEngTotalSize &= ~0x03;
    }
#endif

    fromEngBuffSize = fromEngSize;
    fromEngTotalSize = fromEngSize + sizeof(dptBuffer_S);
    fromEngBuffOffset = toEngTotalSize;

  /* Get The Shared Memory Segment */

    BufferID = shmget(IPC_PRIVATE,(int)(toEngTotalSize + fromEngTotalSize),
                      SHM_ALLRD | SHM_ALLWR | IPC_CREAT);

  /* If We Got The Segment, Try To Attach To It */


    if(BufferID != -1)
      {
        SharedMemoryPtr = (char *)shmat(BufferID,0,0); 

  /* The Attach Failed, So DeAllocate The Shared Memory */

        if((long)SharedMemoryPtr == -1)
          {
            shmctl(BufferID,IPC_RMID,&shm_buff);
            SharedMemoryPtr = NULL;
            toEngBuffSize = 0;
            toEngTotalSize = 0;
            fromEngBuffSize = 0;
            fromEngTotalSize = 0;
            fromEngBuffOffset = 0;
          }
        else {
               ToEngPtr = SharedMemoryPtr;
               FromEngPtr = SharedMemoryPtr + fromEngBuffOffset;
             } 
      }

  /* Could Not Get The Segment */

    else {
       SharedMemoryPtr = NULL;
  //     printf("\n errno = %d\ntoEngTotalSize= %lu \nfromEngTotalSize= %lu", errno,toEngTotalSize,fromEngTotalSize);
                          
    }
    return(SharedMemoryPtr);
  }

/*-------------------------------------------------------------------------*/
/*                         Function DeAllocBuffers                         */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed In To This Function                      */
/*                                                                         */
/* This Function Will DeAllocate The Shared Memory Buffers                 */
/*                                                                         */
/* Return : None                                                           */
/*-------------------------------------------------------------------------*/

void DeAllocBuffers(void)
  {
    struct shmid_ds shm_buff;

    if(BufferID != -1)
      {
        if(SharedMemoryPtr != NULL)
          shmdt(SharedMemoryPtr);
        shmctl(BufferID,IPC_RMID,&shm_buff);
        BufferID = -1;
      }
    SharedMemoryPtr = NULL;
    toEngBuffSize = 0;
    toEngTotalSize = 0;
    fromEngBuffSize = 0;
    fromEngTotalSize = 0;
    fromEngBuffOffset = 0;
  }



/*-------------------------------------------------------------------------*/
/*                         Function DptSignalHandler                       */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed To This Function                         */
/*                                                                         */
/* This Function Is The Signal Handler. It Is Called By The System When    */
/* The Alarm Goes Off. Currently All It Does Is Set The Timeout Flag       */
/*                                                                         */
/* Return : None                                                           */
/*-------------------------------------------------------------------------*/

void DptSignalHandler(int SignalToHandle)
  {

#ifdef MESSAGES

    //
    // We only want to set up a timeout if the signal was the alarm
    //
    if(SignalToHandle == SIGALRM)
     {
       EngineMessageTimeout = 1;
     }

#endif  /* MESSAGES */

  }

/*-------------------------------------------------------------------------*/
/*                         Function SetAlarm                               */
/*-------------------------------------------------------------------------*/
/* There Are No Parameters Passed In To This Function                      */
/*                                                                         */
/* This Function Will                                                      */
/*                                                                         */
/* Return : 0 if alarm set OK, error code othrewise                        */
/*-------------------------------------------------------------------------*/

int SetAlarm(void)        
  {
    struct sigaction sig;
    int Rtnval = 0;

   /* if the signalling has already been setup, don't do it again */
   /* (change mad to accomodate the comm engine */
   if(SignalSetUp)
    {
      return Rtnval;
    }

  /* Initialize The Signaling */

    memset((void *)&sig,0,sizeof(sig));


#if defined  ( _DPT_SCO ) || defined ( _DPT_AIX ) || defined(_DPT_BSDI ) || defined(_DPT_FREE_BSD ) || defined( _DPT_LINUX )
    sig.sa_handler = (void (*)(int))DptSignalHandler;        

#elif defined ( _DPT_SOLARIS )
    sig.sa_handler = (void (*)(int))DptSignalHandler;        


#elif defined ( _DPT_UNIXWARE )
//DEBUG CODE : UnixWare 7
//    sig.sa_handler = (void (*)())DptSignalHandler;        
    sig.sa_handler = (void (*)(int))DptSignalHandler;        

#elif defined ( _DPT_DGUX )
    sig.sa_handler = (void (*)_PROTO_ARGS((int)))DptSignalHandler;        
#elif defined ( SNI_MIPS )
    sig.sa_handler = (void (*)())DptSignalHandler;        
#else
#error Define this for your OS    
#endif

    //
    // Set up to catch the alarm signal for message timeouts
    //
    if(sigaction(SIGALRM,&sig,0) == -1)
     {
       Rtnval = 1;
     }

    //
    // We were having problems with a broken pipe signal when a remote
    // connection to NetWare was broken. This caused the comm engine 
    // to core dump. We will now set up to catch it, but we don't have
    // to do anything with it in the interrupt routine.
    //
    if(sigaction(SIGPIPE,&sig,0) == -1)
     {
       Rtnval = 1;
     }

    return(Rtnval);  
  }

/*-------------------------------------------------------------------------*/
/*                         Function SetEngineLoadPath                      */
/*-------------------------------------------------------------------------*/
/* The Parameters Passed In To This Function Are :                         */
/*     PathName : Path To Try To Load Engine                               */
/*                                                                         */
/* This Function Set The Load Path For The Engine                          */
/*                                                                         */
/* Return : NONE                                                           */
/*-------------------------------------------------------------------------*/

void SetEngineLoadPath(char *PathName)
  {
    if(PathName != NULL)
      {
        strcpy(EngineLoadString,PathName);
        strcat(EngineLoadString," &");
      }
    else EngineLoadString[0] = '\0'; 
  }

#endif  /* MESSAGES */

#ifdef __cplusplus

 }  /* extern c */

#endif

#if (defined(DPT_DEBUG))
//-------------------------------------------------------------------------
//                     Function I2oPrintMem
//-------------------------------------------------------------------------
// The Parameters Passed To This Function Are :
//     Addr : Far Address To Be Dumped
//     Count : Number Of Bytes To Dump
//
// This Function Dumps Memory To The Screen For Debug Purposes
//
// Return : NONE
//-------------------------------------------------------------------------

void I2oPrintMem(char* Addr,long Count)
{
  uLONG Offset,i;

  Offset = 0;

  // Loop For Count Bytes

  while(Offset < Count)
   {

  // Print Out The Address In HEX

     printf("\n%.4lx  ",(unsigned long)Offset);

  // Now Print Out 16 Bytes In HEX Format

     for(i = 0; i < 16; ++i)
      {
        if(Offset + i >= Count)
         {
           printf("   ");
         }
         else {
                printf("%.2x ",Addr[Offset + i] & 0x0ff);
         }
        if(i == 7)
         {
          printf("- ");
         }
      }

  // Print Out The Same 16 Bytes In ASCII Format

     printf("  ");
     for(i = 0; i < 16; ++i)
      {
        if(Offset + i >= Count)
         {
           break;
         }
        if((Addr[Offset + i] > 0x1F)&&(Addr[Offset + i] < 0x7F))
         {
           printf("%c",Addr[Offset + i]);
         }
         else {
                printf(".");
         }
      }

  // Bump The Offset By 16 And Check For Scrolling Past Screen

     Offset += 16;
   }
}
#endif	// DPT_DEBUG