~ubuntu-branches/ubuntu/intrepid/raidutils/intrepid

« back to all changes in this revision

Viewing changes to raideng/eng_unix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barak Pearlmutter
  • Date: 2004-05-18 11:33:42 UTC
  • Revision ID: james.westby@ubuntu.com-20040518113342-tyqavmso5q351xi2
Tags: upstream-0.0.4
ImportĀ upstreamĀ versionĀ 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1996-2004, Adaptec Corporation
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * - Redistributions of source code must retain the above copyright notice, this
 
8
 *   list of conditions and the following disclaimer.
 
9
 * - Redistributions in binary form must reproduce the above copyright notice,
 
10
 *   this list of conditions and the following disclaimer in the documentation
 
11
 *   and/or other materials provided with the distribution.
 
12
 * - Neither the name of the Adaptec Corporation nor the names of its
 
13
 *   contributors may be used to endorse or promote products derived from this
 
14
 *   software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
17
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
20
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
26
 * POSSIBILITY OF SUCH DAMAGE.
 
27
 */
 
28
 
 
29
/* File - ENG_UNIX.C    */
 
30
/*****************************************************************************/
 
31
/*                                                                           */
 
32
/*Description:                                                               */
 
33
/*        This Module Contains Those Functions That Get Linked Into The      */
 
34
/*        Unix DPT Engine To Make It An Executable Program.                  */
 
35
/*                                                                           */
 
36
/*                                                                           */
 
37
/*Author: Bob Pasteur                                                        */
 
38
/*Date:                                                                      */
 
39
/*                                                                           */
 
40
/*Editors:                                                                   */
 
41
/*                                                                           */
 
42
/*Remarks:                                                                   */
 
43
/*                                                                           */
 
44
/*Modification History - Not implemented until going to version 1.D7         */
 
45
/*                                                                           */
 
46
/* M0000 - 2/18/95 Bob Pasteur                                               */
 
47
/*   - Added more verbose print statements                                   */
 
48
/* M0001 - 3/01/95 Bob Pasteur                                               */
 
49
/*   - Added UnixWare Support                                                */
 
50
/* M0002 - 10/27/95 Bob Pasteur                                              */
 
51
/*   - Added a date/time value to the verbose print statements : Unmarked    */
 
52
/* M0003 - 01/16/97 Bob Pasteur                                              */
 
53
/*   - Added support for DGUX                                                */
 
54
/*                                                                           */
 
55
/*****************************************************************************/
 
56
 
 
57
/*Include Files -------------------------------------------------------------*/
 
58
 
 
59
#ifndef SNI_MIPS
 
60
#ifdef __cplusplus
 
61
 
 
62
extern "C" {
 
63
 
 
64
#endif
 
65
#endif // SNI_MIPS
 
66
#include  <stdio.h>
 
67
#include  <stdlib.h>
 
68
#include  <unistd.h>
 
69
#include  <string.h>
 
70
#include  <ctype.h>
 
71
#include  <time.h>
 
72
#include  <sys/types.h>
 
73
#include  <sys/ipc.h>
 
74
#include  <sys/msg.h>
 
75
#include  <sys/shm.h>
 
76
#include  <signal.h>
 
77
#include  <errno.h>
 
78
#if (defined(__FreeBSD__))
 
79
# define _DPT_FREE_BSD
 
80
#endif
 
81
#if (defined(_DPT_BSDI) || defined(_DPT_FREE_BSD))
 
82
# include       <sys/malloc.h>
 
83
#else
 
84
# include       <malloc.h>
 
85
#endif
 
86
#include  <osd_util.h>
 
87
#include  <eng_std.h>
 
88
#include  <osd_unix.h>
 
89
#include  <messages.h>
 
90
#include  <rtncodes.h>
 
91
#include  <dptsig.h>
 
92
#include  <engine.h>
 
93
#include  <engmsg.h>
 
94
#include  <funcs.h>
 
95
#include  "lockunix.h"
 
96
 
 
97
  }  /* extern C */
 
98
 
 
99
#include  <debug.h>
 
100
 
 
101
extern "C" {
 
102
 
 
103
/* Definitions - Defines & Constants ---------------------------------------*/
 
104
 
 
105
/* Definitions - Structure & Typedef ---------------------------------------*/
 
106
 
 
107
/* Variables - External ----------------------------------------------------*/
 
108
 
 
109
extern uLONG LoggerID;
 
110
 
 
111
/* Variables - Global ------------------------------------------------------*/
 
112
 
 
113
DPT_TAG_T       EngineTag;
 
114
uCHAR            *fromEng_P = NULL;
 
115
uCHAR            *toEng_P = NULL;
 
116
int             MsqID;
 
117
MsgHdr          HdrBuff;
 
118
int             NoKill = 0;
 
119
#ifdef _SINIX_ADDON
 
120
int             DemoMode = 0;
 
121
int             ExitEngine = 0;
 
122
int             AutoBuild = 0;
 
123
int             Signature = 0;
 
124
int             DebugToFile = 0;
 
125
ofstream est("/tmp/dpteng_dbg.log");
 
126
#endif
 
127
 
 
128
int             DebugEngMsg = 0;
 
129
 
 
130
int             EngineMessageTimeout = 0;
 
131
int             Verbose = 0;
 
132
int             EataHex = 0;
 
133
int             EataInfo = 0;
 
134
int             NumOpenClients = 0;
 
135
uLONG           P_ID;
 
136
struct msqid_ds CtlBuf;
 
137
static char TimeString[80];
 
138
 
 
139
  /* Command Line Parameters Structure */
 
140
 
 
141
struct {
 
142
   char *text;
 
143
   int  len;
 
144
   int  num;
 
145
} option[] = {
 
146
             {"?",         1, 1 },
 
147
             {"HELP",      4, 2 },
 
148
             {"VERBOSE",   7, 3 },
 
149
             {"EATAHEX",   7, 4 },
 
150
             {"EATAINFO",  8, 5 },
 
151
             {"NOKILL",    6, 6 },
 
152
#ifdef _SINIX_ADDON
 
153
             {"DEMO",      4, 7 },
 
154
             {"STOP",      4, 8 },
 
155
             {"MSG",       3, 9 },
 
156
             {"SIG",       3, 10 },
 
157
             {"FDEBUG",    6, 11 },
 
158
#endif
 
159
             { NULL,       0, 0 }
 
160
             };
 
161
 
 
162
//
 
163
// For FindPath routine
 
164
//
 
165
extern "C" {
 
166
    int Argc;
 
167
    char **Argv;
 
168
}
 
169
 
 
170
/* Prototypes - function ---------------------------------------------------*/
 
171
 
 
172
int ProcessTurnAroundMessage(MsgHdr *HdrBuff_P);
 
173
void ProgramUnload(void);
 
174
void AlarmHandler(void);
 
175
uSHORT ParseCommandLine(int argc,char *argv[]);
 
176
void DisplayHelp(void);
 
177
 
 
178
// this is used to ignore the first signal sent to the engine when a shutdown command occurs
 
179
void DptSignalIgnore(int);
 
180
 
 
181
/* Prototypes - external function ------------------------------------------*/
 
182
 
 
183
extern void FormatTimeString(char *String,uLONG Time);
 
184
#ifdef _SINIX_ADDON
 
185
extern void SNI_set_compile_date(dpt_sig_S *);
 
186
extern dpt_sig_S engineSig;
 
187
#endif
 
188
 
 
189
/***************************** Main Entry Point *****************************/
 
190
 
 
191
main(int argc, char *argv[])
 
192
  {
 
193
    int Rtnval;
 
194
    int Done;
 
195
    struct sigaction sig;
 
196
    DPT_RTN_T i;
 
197
 
 
198
   //
 
199
   // Set up our argument pointers for others to use
 
200
   //
 
201
   Argc = argc;
 
202
   Argv = argv;
 
203
 
 
204
 
 
205
#       if (defined(_DPT_SOLARIS) || defined(_DPT_ROOTONLY))
 
206
                // Only root or sys are allowed to run dptutil.
 
207
                if ( ( getuid () != 0 ) && ( geteuid () != 0 )
 
208
                 &&  ( getuid () != 3 ) && ( geteuid () != 3 )
 
209
                 &&  ( getgid () != 0 ) && ( getegid () != 0 )
 
210
                 &&  ( getgid () != 3 ) && ( getegid () != 3 ) )
 
211
                        {
 
212
                        printf ("You must be root to run this utility\n");
 
213
                        return (0);
 
214
                        }
 
215
#       endif
 
216
 
 
217
   /* Parse The Command Line Parameters, And If An Error Exit */
 
218
 
 
219
    if(ParseCommandLine(argc,argv)) {
 
220
       exit(ExitBadParameter);
 
221
    }
 
222
 
 
223
#ifdef _SINIX_ADDON
 
224
    DEBUG_SETOUTPUT(est);
 
225
    if (DebugToFile) {
 
226
        DEBUG_BEGIN(5, "DPT Engine");
 
227
        DEBUG_SETLEVEL(DebugToFile);
 
228
    } else
 
229
        DEBUG_OFF;
 
230
    SNI_set_compile_date(&engineSig);
 
231
    if (Signature) {
 
232
        printf("%s: V.%d.%c%c %.2d.%.2d.%d\n", engineSig.dsDescription, engineSig.dsVersion,
 
233
        engineSig.dsRevision, engineSig.dsSubRevision, engineSig.dsDay,
 
234
        engineSig.dsMonth, 1980 + engineSig.dsYear);
 
235
        exit(ExitGoodStatus);
 
236
    }
 
237
#else
 
238
        DEBUG_SETOUTPUT(cerr);
 
239
        DEBUG_OFF;
 
240
#endif
 
241
 
 
242
  /* Check To See If There Is Already An Engine Out There */
 
243
 
 
244
        if (MkLock(NULL) == 1) {
 
245
#ifndef _SINIX_ADDON
 
246
        if(Verbose)
 
247
#endif
 
248
           printf(
 
249
"\nThere Appears To Be Another Copy Of dpteng Already Running!\n");
 
250
           exit(ExitDuplicateEngine);
 
251
        }
 
252
 
 
253
  /* If The Engine Cannot Be Opened, Print A Message And Exit */
 
254
 
 
255
    i = DPT_StartEngine();
 
256
    if(i != MSG_RTN_COMPLETED)
 
257
      {
 
258
 
 
259
        if(Verbose)  {
 
260
            printf("\nStarting the Engine: ");
 
261
                if (i == ERR_OSD_OPEN_ENGINE)
 
262
                        printf("Open HBA(s) ");
 
263
                else if (i == ERR_SEMAPHORE_ALLOC)
 
264
                        printf("Alloc Semaphore ");
 
265
                else if (i == ERR_CONN_LIST_ALLOC)
 
266
                        printf("Alloc Connection List ");
 
267
                else
 
268
                        printf(" With Unknown Error %x", i);
 
269
                printf("Failed\n");
 
270
        }
 
271
 
 
272
        RmLock(NULL);
 
273
        exit(ExitEngOpenFail);
 
274
      }
 
275
 
 
276
  /* Initialize The Signaling */
 
277
 
 
278
    memset((void *)&sig,0,sizeof(sig));
 
279
 
 
280
 
 
281
#if defined  ( _DPT_SCO ) || defined ( _DPT_AIX ) || defined( _DPT_BSDI ) || defined(_DPT_FREE_BSD ) || defined(_DPT_LINUX)
 
282
    sig.sa_handler = (void (*)(int))AlarmHandler;
 
283
 
 
284
#elif defined ( _DPT_SOLARIS )
 
285
    sig.sa_handler = (void (*)(int))AlarmHandler;
 
286
 
 
287
 /* M0001 - Added UnixWare Support */
 
288
 
 
289
#elif defined ( _DPT_UNIXWARE )
 
290
    sig.sa_handler = (void (*)(int))AlarmHandler;
 
291
 
 
292
 /* M0001 - Added UnixWare DGUX */
 
293
#elif defined ( _DPT_DGUX )
 
294
    sig.sa_handler = (void (*)_PROTO_ARGS((int)))AlarmHandler;
 
295
 
 
296
#elif defined ( SNI_MIPS )
 
297
    sig.sa_handler = (void (*)())AlarmHandler;        
 
298
#else
 
299
#error Define this for your OS
 
300
#endif
 
301
 
 
302
#ifdef NEW_LOGGER
 
303
 
 
304
#if defined(_DPT_SCO) || defined(_DPT_BSDI) || defined(_DPT_FREE_BSD) || defined(_DPT_LINUX) || defined (_DPT_SOLARIS)
 
305
        // ignore sighup, sigterm will come later after a few seconds
 
306
        signal(SIGHUP, (void (*) (int)) DptSignalIgnore);
 
307
        signal(SIGTERM, (void (*) (int)) DptSignalIgnore);
 
308
        signal(SIGCHLD, (void (*) (int)) DptSignalIgnore);
 
309
#else
 
310
#error Define me.  These are shutdown signals to ignore so the SOC logger
 
311
// can write its final heartbeat on a shutdown
 
312
#endif
 
313
 
 
314
#endif //#ifdef NEW_LOGGER
 
315
 
 
316
  /* If The Signaling Could Not Be Set Up, Print An Error And Exit */
 
317
 
 
318
     if(sigaction(SIGALRM,&sig,0) == -1)
 
319
       {
 
320
         if(Verbose)
 
321
             printf("\nSignaling Could Not Be Set Up\n");
 
322
         RmLock(NULL);
 
323
         exit(ExitSignalFail);
 
324
       }
 
325
 
 
326
  /* if no debug mode selected, start engine as daemon (like inetd) */
 
327
  /* i.e. fork son proc and create new session id. - michiz         */
 
328
  if (!Verbose && !EataHex && !EataInfo && !DebugEngMsg) {
 
329
        int i;
 
330
        if ((i = fork()) != 0) {
 
331
                ChLock(NULL, i);
 
332
                exit(0);
 
333
        }
 
334
        setsid();
 
335
  }
 
336
 
 
337
 
 
338
#ifdef _SINIX_ADDON
 
339
 
 
340
  /* New option -stop to kill a hanged up engine */
 
341
  if (ExitEngine) {
 
342
      int i;
 
343
      struct msqid_ds CtlBuf;
 
344
      MsqID = msgget(DPT_EngineKey, MSG_ALLRD | MSG_ALLWR);
 
345
      if(MsqID != -1) {
 
346
          msgctl(MsqID, IPC_STAT, &CtlBuf);
 
347
          // Stop engine only, if no dptmgr still running
 
348
          if ( CtlBuf.msg_lspid && CtlBuf.msg_lrpid &&
 
349
              (getpgid(CtlBuf.msg_lspid) != -1) &&
 
350
              (getpgid(CtlBuf.msg_lrpid) != -1))
 
351
                printf("You must stop dptmgr first\n");
 
352
          else {
 
353
                i = MessageDPTEngine(DPT_EngineKey, MsqID, ENGMSG_CLOSE, 2,1);
 
354
                if (i)
 
355
                  msgctl(MsqID, IPC_RMID, &CtlBuf);
 
356
                if(Verbose)
 
357
                    printf("dpteng successfully stopped\n");
 
358
          }
 
359
      }
 
360
      RmLock(NULL);
 
361
      exit(ExitGoodStatus);
 
362
  }
 
363
#endif
 
364
  /* Check To See If There Is Already An Engine Out There */
 
365
 
 
366
    MsqID = CheckForEngine(DPT_EngineKey,1,2);
 
367
    if(MsqID != -1)
 
368
      {
 
369
#ifndef _SINIX_ADDON
 
370
        if(Verbose)
 
371
#endif
 
372
           printf(
 
373
"\nThere Appears To Be Another Copy Of dpteng Already Running!\n");
 
374
        RmLock(NULL);
 
375
        exit(ExitDuplicateEngine);
 
376
      }
 
377
 
 
378
  /* Get The Process ID To Use As The Unique Caller ID */
 
379
 
 
380
    P_ID = getpid();
 
381
 
 
382
  /* Try To Create The Unique Message Que Of This ID */
 
383
 
 
384
    MsqID = msgget(DPT_EngineKey,IPC_CREAT | IPC_EXCL | MSG_ALLRD | MSG_ALLWR);
 
385
 
 
386
  /* If We Could Not Allocate The Message Que, Print A Message And Exit */
 
387
 
 
388
    if(MsqID == -1)
 
389
      {
 
390
        if(Verbose)
 
391
            printf("\nThe Message Queue Could Not Be Allocated\n");
 
392
        RmLock(NULL);
 
393
        exit(ExitMsqAllocFail);
 
394
      }
 
395
 
 
396
  /* Set Up The Function To Call When The Program Terminates Normally */
 
397
 
 
398
    atexit(ProgramUnload);
 
399
 
 
400
  /* Loop Forever Waiting For A Header Message To Come In. Once A Header */
 
401
  /* Message Is Received, We Will Attach To The Shared Memory Segment    */
 
402
  /* That Is Passed In The Header. This Memory Is The In And Out Buffers */
 
403
 
 
404
    Done = 0;
 
405
    if(Verbose)
 
406
        printf("\ndpteng Is Ready.\n");
 
407
    while(1)
 
408
      {
 
409
        //
 
410
        // At the top of the loop, we will pull off and process all turnaround messages on the queue since
 
411
        // they don't have to be sent down to the engine
 
412
        //
 
413
        while(Rtnval = msgrcv(MsqID,(struct msgbuf *)&HdrBuff, MsgDataSize, DPT_TurnAroundKey,IPC_NOWAIT) != -1)
 
414
         {
 
415
           Done = ProcessTurnAroundMessage(&HdrBuff);
 
416
         }
 
417
 
 
418
        //
 
419
        // If Done is set, there are no more clients, so let's
 
420
        // check for any messages in the queue before we exit. It
 
421
        // is possible that someone is trying to load and we don't
 
422
        // want to pull the rug out from under him
 
423
        //
 
424
        if(Done)
 
425
         {
 
426
           Rtnval = msgrcv(MsqID,(struct msgbuf *)&HdrBuff,
 
427
                           MsgDataSize, -DPT_EngineKey,IPC_NOWAIT);
 
428
           //
 
429
           // If the call failed, were OUT-O-HERE
 
430
           // 
 
431
           if(Rtnval == -1)
 
432
            {
 
433
              if(Verbose)
 
434
               {
 
435
                 printf("\n               : No Clients, Engine Unloads");
 
436
               }
 
437
               break;
 
438
            }
 
439
 
 
440
           //
 
441
           // There was a message on the queue so continue processing
 
442
           //
 
443
           Done = 0;
 
444
         }
 
445
 
 
446
         //
 
447
         // Done is not set, so wait for a message to come in
 
448
         //
 
449
         else {
 
450
                while(Rtnval = msgrcv(MsqID,(struct msgbuf *)&HdrBuff,
 
451
                                      MsgDataSize, -DPT_EngineKey,0) == -1)
 
452
                 {
 
453
 
 
454
  /* If The Message Failed, And The Reason Was Not An Interrupt, Exit */
 
455
 
 
456
                   if(errno != EINTR)
 
457
                      break;
 
458
                 }
 
459
         }
 
460
 
 
461
  /* If The Message Received OK Go Process It */
 
462
 
 
463
        if(Rtnval == 0)
 
464
          {
 
465
            //
 
466
            // Check to make sure this guy is still out there before processing the message
 
467
            // if not, throw away the message
 
468
            //
 
469
            if(kill(HdrBuff.callerID,0) != 0)
 
470
             {
 
471
              if(Verbose)
 
472
               {
 
473
                 printf("\n               : Message received for PID %d : no process found, discarding",
 
474
                         HdrBuff.callerID);
 
475
               }
 
476
               continue;
 
477
             }
 
478
 
 
479
  /* If This Is A Turnarround Message, Process it */
 
480
 
 
481
            if((HdrBuff.engineTag == HdrBuff.callerID)&&
 
482
               (HdrBuff.engineTag == HdrBuff.targetTag))
 
483
              {
 
484
                 Done = ProcessTurnAroundMessage(&HdrBuff);
 
485
                 continue;
 
486
              }
 
487
 
 
488
  /* Attach To The Shared Memory */
 
489
 
 
490
            toEng_P = (uCHAR *)shmat((int)HdrBuff.BufferID,0,0);
 
491
 
 
492
  /* Make Sure That We Could Attach */
 
493
 
 
494
            if((int)toEng_P != -1)
 
495
              {
 
496
                 fromEng_P = toEng_P + HdrBuff.FromEngBuffOffset;
 
497
 
 
498
                                           /* M0000 : New Verbose Statements */
 
499
                 if(Verbose)
 
500
                   {
 
501
                     FormatTimeString(TimeString,time(0));
 
502
                     printf("\nEngine Calls   : %s DPT_CallEngine",TimeString);
 
503
                     printf( 
 
504
                   "\n                 EngTag = %x, Event = %x, Target = %x",
 
505
                       HdrBuff.engineTag,HdrBuff.engEvent,HdrBuff.targetTag);
 
506
                     printf( 
 
507
                   "\n                 Offset = %x fromEng_P = %x toEng_P = %x",
 
508
                       HdrBuff.FromEngBuffOffset, fromEng_P, toEng_P);
 
509
/*#else
 
510
                      "\n                 EngTag = %x, Event = %x, Target = %x",
 
511
                       HdrBuff.engineTag,HdrBuff.engEvent,HdrBuff.targetTag);
 
512
#endif */
 
513
                     fflush(stdout);
 
514
                   }
 
515
 
 
516
 /* All Went Well With The Receive, So Call The Engine */
 
517
 
 
518
                  HdrBuff.result = DPT_Engine( HdrBuff.engineTag,
 
519
                                               HdrBuff.engEvent,
 
520
                                               HdrBuff.targetTag,
 
521
                                               fromEng_P,toEng_P,
 
522
                                               HdrBuff.timeOut);
 
523
 
 
524
                 //
 
525
                 // Detach from the memory segment
 
526
                 //
 
527
                 shmdt((char *)toEng_P);
 
528
 
 
529
                 //
 
530
                 // Check to make sure this guy is still out there before returning the message
 
531
                 // if not, throw away the message
 
532
                 //
 
533
                 if(kill(HdrBuff.callerID,0) != 0)
 
534
                  {
 
535
                   if(Verbose)
 
536
                    {
 
537
                      printf("\n               : Returning message for PID %d : no process found, discarding",
 
538
                             HdrBuff.callerID);
 
539
                    }
 
540
                   continue;
 
541
                  }
 
542
 
 
543
  /* Set Up The Proper Message Type In The Buffer Headers */
 
544
 
 
545
                 HdrBuff.MsgID = HdrBuff.callerID;
 
546
                 HdrBuff.callerID = DPT_EngineKey;
 
547
                 Rtnval = msgsnd(MsqID,(struct msgbuf *)&HdrBuff,MsgDataSize,0);
 
548
 
 
549
  /* If We Had An Error Sending, Print A Message And Exit */
 
550
 
 
551
                 if(Rtnval == -1)
 
552
                   {
 
553
                     if(Verbose)
 
554
                       {
 
555
                         FormatTimeString(TimeString,time(0));
 
556
                         printf(
 
557
                   "\n%s Error Sending A Message In The Engine : %d\n",
 
558
                         TimeString,errno);
 
559
                         fflush(stdout);
 
560
                       }
 
561
                     RmLock(NULL);
 
562
                     exit(ExitMsgSendFail);
 
563
                   }
 
564
              }
 
565
/*
 
566
else printf("\nError Trying To Attach To The Memory");
 
567
*/
 
568
          }
 
569
 
 
570
  /* If We Had An Error Receiving, Print A Message And Exit */
 
571
 
 
572
        else  {
 
573
                if(Verbose)
 
574
                  {
 
575
                    FormatTimeString(TimeString,time(0));
 
576
                    printf(
 
577
                        "\n%s Error Receiving A Message In The Engine : %d\n",
 
578
                        TimeString,errno);
 
579
                    fflush(stdout);
 
580
                  }
 
581
                RmLock(NULL);
 
582
                exit(ExitMsgReceiveFail);
 
583
              }
 
584
      }
 
585
 
 
586
  } //main(int argc, char *argv[])
 
587
 
 
588
/*-------------------------------------------------------------------------*/
 
589
/*                 Function ProcessTurnAroundMessage                       */
 
590
/*-------------------------------------------------------------------------*/
 
591
/* The Parameters Passed To This Function Are :                            */
 
592
/*     HdrBuff_P : Pointer to a received message header buffer             */
 
593
/*                                                                         */
 
594
/* This Function                                                           */
 
595
/*                                                                         */
 
596
/* Return                                                                  */
 
597
/*-------------------------------------------------------------------------*/
 
598
 
 
599
int ProcessTurnAroundMessage(MsgHdr *HdrBuff_P)
 
600
{
 
601
  int Done = 0;
 
602
  int Rtnval;
 
603
 
 
604
  switch(HdrBuff_P->engEvent)
 
605
   {
 
606
     //
 
607
     // For An Open, Bump the Number Of Clients 
 
608
     //
 
609
 
 
610
     case ENGMSG_OPEN :
 
611
          if(Verbose)
 
612
           {
 
613
             FormatTimeString(TimeString,time(0));
 
614
             printf("\nEngine Calls   : %s DPT_OpenEngine", TimeString);
 
615
             fflush(stdout);
 
616
           }
 
617
          ++NumOpenClients;
 
618
          break;
 
619
 
 
620
     //
 
621
     // For a close, Decrement The Number Of Clients, And If This Was The 
 
622
     // Last Client And The No Kill Is Not Set, Set Up To Exit      
 
623
     //
 
624
     case ENGMSG_CLOSE :
 
625
          if(Verbose)
 
626
           {
 
627
             FormatTimeString(TimeString,time(0));
 
628
             printf("\nEngine Calls   : %s DPT_CloseEngine", TimeString);
 
629
             fflush(stdout);
 
630
           }
 
631
          if(NumOpenClients > 0)
 
632
           {
 
633
             --NumOpenClients;
 
634
           }
 
635
          if(NumOpenClients <= 0)
 
636
           {
 
637
             if(!NoKill)
 
638
              {
 
639
                Done = 1;
 
640
              }
 
641
           }
 
642
          break;
 
643
 
 
644
     //
 
645
     // For An Echo, All That Is Needed Is The Return Message 
 
646
     //
 
647
     case ENGMSG_ECHO :
 
648
          if(Verbose)
 
649
           {
 
650
             FormatTimeString(TimeString,time(0));
 
651
             printf( "\nEngine Calls   : %s Engine Echo Message", TimeString);
 
652
             fflush(stdout);
 
653
           }
 
654
          break;
 
655
 
 
656
   } //switch(HdrBuff_P->engEvent)
 
657
 
 
658
  //
 
659
  // Make sure this guy is still out there before putting the message back on the queue
 
660
  //
 
661
  if(kill(HdrBuff_P->callerID,0) == 0)
 
662
   {
 
663
 
 
664
     //
 
665
     // Set Up And Return The Message 
 
666
     //
 
667
     HdrBuff_P->MsgID = HdrBuff_P->callerID;
 
668
     HdrBuff_P->callerID = P_ID;
 
669
     Rtnval = msgsnd(MsqID,(struct msgbuf *)HdrBuff_P,MsgDataSize,0);
 
670
   
 
671
     //
 
672
     // If We Had An Error Sending, Print A Message And Exit 
 
673
     //
 
674
     if(Rtnval == -1)
 
675
      {
 
676
        if(Verbose)
 
677
         {
 
678
           FormatTimeString(TimeString,time(0));
 
679
           printf( "\n%s Error Sending A Message In The Engine : %d\n", TimeString,errno);
 
680
           fflush(stdout);
 
681
         }
 
682
        RmLock(NULL);
 
683
        exit(ExitMsgSendFail);
 
684
      }
 
685
 
 
686
   } //if(kill(HdrBuff_P->callerID,0) == 0)
 
687
   else {
 
688
       if(Verbose)
 
689
         {
 
690
           printf("\n               : Returning message for PID %d : no process found, discarding",
 
691
                  HdrBuff_P->callerID);
 
692
         }
 
693
  }
 
694
 
 
695
  return(Done);
 
696
 
 
697
} //int ProcessTurnAroundMessage(MsgHdr *HdrBuff_P)
 
698
 
 
699
 
 
700
 
 
701
 
 
702
 
 
703
/*-------------------------------------------------------------------------*/
 
704
/*                         Function ProgramUnload                          */
 
705
/*-------------------------------------------------------------------------*/
 
706
/* There Are No Parameters Passed To This Function                         */
 
707
/*                                                                         */
 
708
/* This Function Is Called On Normal Program Termination. It Makes A Close */
 
709
/* Call To The Engine And Frees Up The Message Queue That Was Allocated    */
 
710
/*                                                                         */
 
711
/* Return None                                                             */
 
712
/*-------------------------------------------------------------------------*/
 
713
 
 
714
void ProgramUnload(void)
 
715
  {
 
716
 
 
717
  /* On The Way Out, Close The Engine, Free Up The Message Que And Buffers */
 
718
 
 
719
    DPT_StopEngine();
 
720
    if(MsqID != -1)
 
721
        msgctl(MsqID,IPC_RMID,&CtlBuf);
 
722
  }
 
723
 
 
724
/*-------------------------------------------------------------------------*/
 
725
/*                         Function AlarmHandler                           */
 
726
/*-------------------------------------------------------------------------*/
 
727
/* There Are No Parameters Passed To This Function                         */
 
728
/*                                                                         */
 
729
/* This Function Is Called When The Alarm Signal Goes Off. Currently It    */
 
730
/* Does Nothing                                                            */
 
731
/*                                                                         */
 
732
/* Return None                                                             */
 
733
/*-------------------------------------------------------------------------*/
 
734
 
 
735
void AlarmHandler(void)
 
736
  {
 
737
    EngineMessageTimeout = 1;
 
738
  }
 
739
 
 
740
/*-------------------------------------------------------------------------*/
 
741
/*                         Function ParseCommandLine                       */
 
742
/*-------------------------------------------------------------------------*/
 
743
/* The Parameters Passed To This Function Are :                            */
 
744
/*     argc : Standard "C" Number Of Arguments Passed Into Main            */
 
745
/*     argv : Standard "C" Argument List Passed into Main                  */
 
746
/*                                                                         */
 
747
/* This Function Parses The Passed In Command Line And Sets Up The System  */
 
748
/* Default Values                                                          */
 
749
/*                                                                         */
 
750
/* Return : 0 For All Is Well, 1 For Exit Program                          */
 
751
/*-------------------------------------------------------------------------*/
 
752
 
 
753
uSHORT ParseCommandLine(int argc,char *argv[])
 
754
  {
 
755
    uINT i,j,Invalid;
 
756
    char pram[80];
 
757
    uSHORT Rtnval;
 
758
 
 
759
  /* Parse The Command Line Options */
 
760
 
 
761
    Rtnval = 0;
 
762
    Invalid = 0;
 
763
 
 
764
  /* Loop Through All Parameters Passed In */
 
765
 
 
766
    for(i = 1; i < argc; ++i)
 
767
      {
 
768
        if(Rtnval)
 
769
          break;
 
770
 
 
771
  /* Pull Out The Parameter And Convert It To Upper Case */
 
772
 
 
773
        strcpy(pram,argv[i]);
 
774
        strupr(pram);
 
775
 
 
776
  /* Loop Through Our List To See If It Is In There */
 
777
 
 
778
        for(j = 0; option[j].text != NULL; ++j)
 
779
          {
 
780
            if(strncmp(pram + 1, option[j].text, option[j].len) == 0)
 
781
              {
 
782
 
 
783
  /* We Found It, So Take The Appropriate Action */
 
784
 
 
785
                switch(option[j].num)
 
786
                  {
 
787
 
 
788
  /* ?,HELP : Display The Help Menu */
 
789
 
 
790
                    case 1:
 
791
                    case 2:
 
792
                         DisplayHelp();
 
793
                         Rtnval = 1;
 
794
                         break;
 
795
 
 
796
  /* VERBOSE : Turn On The Verbose Option */
 
797
 
 
798
                    case 3:
 
799
                         Verbose = 1;
 
800
                         break;
 
801
 
 
802
  /* EATAHEX : Turn On The EATA Packet Hex Print Option */
 
803
 
 
804
                    case 4:
 
805
                         EataHex = 1;
 
806
                         break;
 
807
 
 
808
  /* EATAINFO : Turn On The EATA InfoPrint Option */
 
809
 
 
810
                    case 5:
 
811
                         EataInfo = 1;
 
812
                         break;
 
813
 
 
814
  /* NOKILL : Turn On The No Kill Feature */
 
815
 
 
816
                    case 6:
 
817
                         NoKill = 1;
 
818
                         break;
 
819
 
 
820
#ifdef _SINIX_ADDON
 
821
  /* DEMO : Do not open Adaptor for demo mode */
 
822
 
 
823
                    case 7:
 
824
                         DemoMode = 1;
 
825
                         break;
 
826
 
 
827
  /* STOP : Halt idling engines via shmem */
 
828
 
 
829
                    case 8:
 
830
                         ExitEngine = 1;
 
831
                         break;
 
832
 
 
833
  /* MSG : Debug Engine message calls */
 
834
 
 
835
                    case 9:
 
836
                         DebugEngMsg = 1;
 
837
                         break;
 
838
 
 
839
  /* SIG  : Print Signature and Exit   */
 
840
 
 
841
                    case 10:
 
842
                         Signature = 1;
 
843
                         break;
 
844
 
 
845
  /* FDEBUG : Print Debug Output to File */
 
846
 
 
847
                    case 11:
 
848
                         i++;
 
849
                         DebugToFile = atoi(argv[i]);
 
850
                         if (DebugToFile <= 0)
 
851
                            Invalid++;
 
852
                         break;
 
853
#endif
 
854
                  }
 
855
                break;
 
856
              }
 
857
          }
 
858
 
 
859
  /* Bad Option, So Print Error, Display The Help Screen And Exit */
 
860
 
 
861
        if((option[j].text == NULL)||(Invalid))
 
862
          {
 
863
            printf("\nError : Invalid Parameter  %s : Program Terminated!!\n\n",
 
864
                    argv[i]);
 
865
            DisplayHelp();
 
866
            Rtnval = 1;
 
867
          }
 
868
      }
 
869
    return(Rtnval);
 
870
  }
 
871
 
 
872
/*-------------------------------------------------------------------------*/
 
873
/*                         Function DisplayHelp                            */
 
874
/*-------------------------------------------------------------------------*/
 
875
/* There Are No Parameters Passed Into This Function                       */
 
876
/*                                                                         */
 
877
/* This Function Displays The Help Options                                 */
 
878
/*                                                                         */
 
879
/* Return : NONE                                                           */
 
880
/*-------------------------------------------------------------------------*/
 
881
 
 
882
#ifdef _SINIX_ADDON
 
883
void DisplayHelp(void)
 
884
  {
 
885
    printf("Usage: engine [-options]\n");
 
886
    printf(
 
887
" options:\n");
 
888
    printf(
 
889
"  -verbose           :  Display All Connection And Message Information\n");
 
890
    printf(
 
891
"  -eatahex           :  Display Hex Dump Of EATA Packet\n");
 
892
    printf(
 
893
"  -eatainfo          :  Display EATA Packet Info\n");
 
894
    printf(
 
895
"  -nokill            :  Engine Will Not Unload When Not In Use\n");
 
896
    printf(
 
897
"  -demo              :  Engine Will Run in Demo Mode\n");
 
898
    printf(
 
899
"  -stop              :  Engine Will Stop Another That Is Not Yet Terminated\n");
 
900
    printf(
 
901
"  -msg               :  Display Engine Message Calls\n");
 
902
    printf(
 
903
"  -sig               :  Display Engine Signature and Exit\n");
 
904
    printf(
 
905
"  -fdebug <level>    :  Internal Debug to '/tmp/dpteng_dbg.log' <level>: 1..10\n");
 
906
    printf("  -help or ?         :  Display Help Screen\n");
 
907
  }
 
908
 
 
909
#else
 
910
void DisplayHelp(void)
 
911
  {
 
912
    printf("The Pramaters For This Program Are :\n");
 
913
    printf(
 
914
"  /VERBOSE            :  Display All Connection And Message Information\n");
 
915
    printf(
 
916
"  /EATAHEX            :  Display Hex Dump Of EATA Packet\n");
 
917
    printf(
 
918
"  /EATAINFO           :  Display EATA Packet Info\n");
 
919
    printf(
 
920
"  /NOKILL             :  Engine Will Not Unload When Not In Use\n");
 
921
    printf("  /HELP or /?         :  Display Help Screen\n");
 
922
  }
 
923
#endif // SINIX_ADDON
 
924
 
 
925
#ifndef SNI_MIPS
 
926
#ifdef __cplusplus
 
927
 
 
928
  }  /* extern C */
 
929
 
 
930
#endif
 
931
#endif // SNI_MIPS
 
932
 
 
933
 
 
934
/*-------------------------------------------------------------------------*/
 
935
/*                         Function DptSignalIgnore                        */
 
936
/*-------------------------------------------------------------------------*/
 
937
/* There Are No Parameters Passed Into This Function                       */
 
938
/*                                                                         */
 
939
/* This Function is used to ignore the first shutdown signal               */
 
940
/*                                                                         */
 
941
/* Return : NONE                                                           */
 
942
/*-------------------------------------------------------------------------*/
 
943
 
 
944
void DptSignalIgnore(int nothing)
 
945
{
 
946
/*
 
947
   if (LoggerID) {
 
948
 
 
949
                if (Verbose)
 
950
                        printf("Engine ignoring signal: %d\n", nothing);
 
951
 
 
952
                sleep(5);
 
953
        } else {
 
954
                RmLock(NULL);
 
955
                exit(ExitGoodStatus);
 
956
        }
 
957
*/
 
958
 
 
959
        nothing = nothing;
 
960
}