home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c004 / 2.ddi / UNIX / CTAMSG.C next >
Encoding:
C/C++ Source or Header  |  1989-04-18  |  5.1 KB  |  226 lines

  1. /*
  2.  *    System Dependent Application Message Handling
  3.  *    Two Queue Approach
  4.  *    Unix/Xenix V Queue Implementation
  5.  *
  6.  *    This program is the CONFIDENTIAL and PROPRIETARY property 
  7.  *    of FairCom(R) Corporation. Any unauthorized use, reproduction or
  8.  *    transfer of this program is strictly prohibited.
  9.  *
  10.  *      Copyright (c) 1987, 1988, 1989 FairCom Corporation
  11.  *    (Subject to limited distribution and
  12.  *     restricted disclosure only.)
  13.  *    *** ALL RIGHTS RESERVED ***
  14.  *
  15.  *    4006 West Broadway
  16.  *    Columbia, MO 65203
  17.  *
  18.  *
  19.  *    c-tree(R)    Version 4.3
  20.  *            Release C
  21.  *            February 7, 1989 17:30
  22.  *
  23.  */
  24.  
  25. #include "ctstdr.h"
  26. #include "ctoptn.h"
  27. #include "ctstrc.h"
  28. #include "cterrc.h"
  29. #include "ctcomm.h"
  30. #include "ctmssg.h"
  31. #include <errno.h>
  32.  
  33. /* LTYPE is defined in ctmssg.h */
  34.  
  35. #define DIR_SEP    '/'
  36. #define DIR_PAR '.'
  37.  
  38. COUNT         no_demon;
  39. LOCAL TEXT   dir_sepstr[] = {
  40.             DIR_SEP,
  41.             '\0'};
  42. LOCAL UCOUNT msgsiz;
  43. LOCAL LONG   pid;
  44.  
  45. extern COUNT uerr_cod;
  46. extern TEXT  ct_buf[];
  47. extern int   errno;
  48.  
  49. /* assumes that MAX_NAME (usually 64) << SECSIZ (usually 128) */
  50.  
  51. TEXT *getcwd();
  52.  
  53. LOCAL COUNT reduce(sp,i) /* processes unix style path names with ../ & ./ */
  54. PFAST TEXT      *sp;
  55. PFAST COUNT          i;
  56. {
  57.     FAST TEXT *tp;
  58.  
  59.     tp = sp + i;
  60.     while (*tp) {
  61.         if (*tp == DIR_PAR) {
  62.         if (*(tp - 1) == DIR_SEP) {
  63.             if (*(tp + 1) == DIR_SEP) {
  64.             strcpy(sp + i,tp + 2);
  65.             return(reduce(sp,i+1));
  66.             } else if (*(tp + 1) == DIR_PAR && *(tp + 2) == DIR_SEP) {
  67.             for (i -= 3; i >= 0; i--)
  68.                 if (sp[i] == DIR_SEP) {
  69.                     strcpy(sp + i,tp + 2);
  70.                     return(reduce(sp,i + 1));
  71.                 }
  72.             return(uerr_cod = ABNM_ERR);
  73.             }
  74.         }
  75.         }
  76.         tp++;
  77.         i++;
  78.     }
  79.     return(NO_ERROR);
  80. }
  81.  
  82. /* copies a file name, after modifying name to a fully specified form */
  83. COUNT fulnam(dp, tp,maxlen) 
  84. PFAST TEXT  *dp,*tp;
  85. COUNT            maxlen;
  86. {
  87.     COUNT bpos;
  88.  
  89.     if (*tp != DIR_SEP) {
  90.         if (getcwd(ct_buf,maxlen) == NULL) /* get current directory */
  91.             return(uerr_cod = ABDR_ERR);
  92.         if (ct_buf[1]) /* more than root */
  93.             strcat(ct_buf,dir_sepstr);
  94.         bpos = strlen(ct_buf);
  95.         strcat(ct_buf,tp);
  96.         if (reduce(ct_buf,bpos))
  97.             return(uerr_cod);
  98.     } else {
  99.         strcpy(ct_buf,tp);
  100.         if (reduce(ct_buf,1))
  101.             return(uerr_cod);
  102.     }
  103.  
  104.     if (strlen(ct_buf) > maxlen)
  105.         return(uerr_cod = AFLN_ERR);
  106.     strcpy(dp,ct_buf);
  107.     return(NO_ERROR);
  108. }
  109.  
  110. TEXT *getmids(apsize,pmsg)
  111. UCOUNT          apsize;
  112. MESSAGE            *pmsg;
  113. {
  114.     TEXT *retval,pidbuf[16];
  115.  
  116.     TEXT *mballc();
  117.     key_t ftok();
  118.  
  119.     /* get server msg id */
  120.     uerr_cod = 0;
  121.     if ((srvkey = ftok(SERVER,'q')) == NOKEY)
  122.         uerr_cod = ASKY_ERR;
  123.     else if ((srvid = msgget(srvkey,0)) < 0)
  124.         uerr_cod = ASID_ERR;
  125.  
  126.     /* get application (local) msg id */
  127.     else if ((appkey = ftok(SERVER2,'q')) == NOKEY)
  128.         uerr_cod = AAKY_ERR;
  129.     else if ((apxid = msgget(appkey,0)) < 0)
  130.         uerr_cod = AAID_ERR;
  131.     if (uerr_cod)
  132.         return(NULL);
  133.  
  134.     /* get space for message structure required by system */ /* ???? */
  135.     if ((retval = mballc(1,apsize + LTYPE)) == NULL) {
  136.         uerr_cod = ASPC_ERR;
  137.         return(NULL);
  138.     }
  139.     msgsiz = apsize;
  140.  
  141.     if (pmsg->mfunc == TST_STPUSR || pmsg->mfunc == TST_DEDUSR ||
  142.         no_demon != 0 || (pid = fork()) == 0 || pid == -1L) {
  143.         /* child process continues application */
  144.     
  145.         /* application message id into mpntr member of message header */
  146.         pmsg->mpntr = pid = getpid();
  147.  
  148.         /* return pointer to actual text portion */
  149.         return(retval + LTYPE);
  150.     } else {
  151.         /* parent becomes watchdog for termination via ctdemn */
  152.         sprintf(pidbuf,"%d",(int) pid);
  153.         /*
  154.          * Add, after pidbuf and before the NULL in execlp(),
  155.          * optional ASCII strings with signal numbers to be
  156.          * ignored by crdemn IF AND ONLY IF your application promises
  157.          * to ignore these signals. Alternatively, you may modify
  158.          * ctdemn.c so that it ignores these particular signals.
  159.          */
  160.         execlp("ctdemn","ctdemn",pidbuf,NULL);
  161.         fprintf(stderr,
  162. "\nApplication could not invoke \"ctdemn.\" Unix errno=%d.\n",errno);
  163.         exit(2);
  164.     }
  165.  
  166. }
  167.  
  168. COUNT ridmids()
  169. {
  170.     /* 2 queue approach requires no action */
  171.     return(NO_ERROR);
  172. }
  173.  
  174. COUNT ctrqst(msgadr,pmsg)
  175. PFAST TEXT  *msgadr;        /* ptr to message area        */
  176. MESSAGE           *pmsg;    /* ptr to message header     */
  177. {
  178.     int msglen;
  179.  
  180.     /* copy message header */
  181.     cpybuf(msgadr,pmsg,sizeof(MESSAGE));
  182.     msglen = sizeof(MESSAGE) + pmsg->mdlen;
  183.  
  184.     /* send message: ordinarily an interprocess comm call (e.g., q call) */
  185.     msgadr -= LTYPE;
  186.     *((LONG *) msgadr) = 1L;
  187.  
  188. snd_retry:
  189.     if (msgsnd(srvid,msgadr,msglen,0) < 0) {
  190.         if (errno == EINTR)
  191.         goto snd_retry;
  192.         else
  193.         return(uerr_cod = ARQS_ERR);
  194.     } else
  195.         return(NO_ERROR);
  196. }
  197.  
  198. COUNT ctrspn(msgadr,pmsg)
  199. PFAST TEXT **msgadr;
  200. MESSAGE                *pmsg;
  201. {
  202.     /* get response: interprocess comm call which leaves answer in apxmsg */
  203.     /* read only process id messages */
  204. rcv_retry:
  205.     if (msgrcv(apxid,*msgadr - LTYPE,msgsiz,pid,0) < 0) {
  206.         if (errno == EINTR)
  207.         goto rcv_retry;
  208.         else
  209.         return(uerr_cod = ARSP_ERR);
  210.     }
  211.  
  212.     /* copy message header */
  213.     cpybuf(pmsg,*msgadr,sizeof(MESSAGE));
  214.  
  215.     return(NO_ERROR);
  216. }
  217.  
  218. /* return application queueid so that demon may clear output queue */
  219. int getapxid()
  220. {
  221.     return(apxid);
  222. }
  223.  
  224. /* end of ctamsg.c */
  225.  
  226.