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

  1. /*
  2.  *    System Dependent Server Message Handler
  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. #include <signal.h>
  33.  
  34. LOCAL UCOUNT msgsiz;
  35. LOCAL LONG  *ausrmid;
  36.  
  37. extern COUNT uerr_cod;
  38. extern COUNT cts_mxusr;
  39. extern int   errno;
  40.  
  41. /* elapsed time before unread user queue declared inactive */
  42. #define INDEFINITE 5    /* seconds */
  43.  
  44. /* LTYPE is defined in ctmsgg.h */
  45.  
  46. TEXT *getmid(sizmsg,maxusr)
  47. UCOUNT         sizmsg;
  48. COUNT            maxusr;
  49. {
  50.     TEXT *retval;
  51.     int   chfd;
  52.  
  53.     key_t ftok();
  54.     TEXT *mballc();
  55.  
  56.     /* server ignores hangup */
  57.     signal(SIGHUP,SIG_IGN);
  58.  
  59.     /* create SERVER3 to prove no other server is running */
  60.     if ((chfd = open(SERVER3,O_RDWR | O_EXCL | O_CREAT,BCREATE)) < 0) {
  61.         fprintf(stderr,"\nIs a c-tree Server already running?\n");
  62.         fprintf(stderr,"\nIf a c-tree Server has terminated abnormally,");
  63.         fprintf(stderr,"\nremove the file %s before starting the server.\n\n",
  64.             SERVER3);
  65.         exit(2);
  66.     } else
  67.         close(chfd);
  68.  
  69.     /* allocate an array for message ids: assumes long is OK */
  70.     if ((ausrmid = (LONG *) mballc(maxusr + 1,sizeof(LONG))) == NULL) {
  71.         uerr_cod = SSCB_ERR;
  72.         return(NULL);
  73.     }
  74.  
  75.     /* get message id for server */
  76.     uerr_cod = 0;
  77.  
  78.     if ((srvkey = ftok(SERVER,'q')) == NOKEY) /* get "key" for input Q */
  79.         uerr_cod = SSKY_ERR;
  80.     else if ((srvid = msgget(srvkey,IPC_CREAT | 0666)) < 0) /* get msg id */
  81.         uerr_cod = SSID_ERR;
  82.     else if (msgctl(srvid,IPC_STAT,&qidbuf) < 0) /* get msg specs */
  83.         uerr_cod = SMST_ERR;
  84.     else {
  85.         /* set message size specifications */
  86.         msgsiz          = sizmsg;
  87.         qidbuf.msg_qbytes = sizmsg + LTYPE;
  88.         if (msgctl(srvid,IPC_SET,&qidbuf) < 0)
  89.             uerr_cod = SMQZ_ERR;    
  90.     }
  91.     if (uerr_cod)
  92.         return(NULL);
  93.  
  94.  
  95.     /* get space for server message */
  96.     if ((retval = mballc(1,sizmsg + LTYPE)) == NULL) {
  97.         uerr_cod = SSPC_ERR;
  98.         return(NULL);
  99.     }
  100.     
  101.     if ((appkey = ftok(SERVER2,'q')) == NOKEY) /* get "key" for output Q */
  102.         uerr_cod = ASKY_ERR;
  103.     else if ((apxid = msgget(appkey,IPC_CREAT | 0666)) < 0) /* get msg id */
  104.         uerr_cod = ASID_ERR;
  105.     else if (msgctl(apxid,IPC_STAT,&qidbuf) < 0) /* get msg specs */
  106.         uerr_cod = AMST_ERR;
  107.     else {
  108.         /* set message size specifications */
  109.         qidbuf.msg_qbytes = cts_mxusr * (sizmsg + LTYPE); /* ???? */
  110.         if (msgctl(apxid,IPC_SET,&qidbuf) < 0)
  111.             uerr_cod = AMQZ_ERR;    
  112.     }
  113.  
  114.     return(retval + LTYPE);
  115. }
  116.  
  117. LONG getumsg(pmsg,usrn,msgptr) /* assumes that mpntr member contains msg id */
  118. MESSAGE        *pmsg;
  119. COUNT          usrn;
  120. TEXT              *msgptr;
  121. {
  122.     /* save message id of application: assumes that a long is sufficient */
  123.     if (usrn >= 0)
  124.         ausrmid[usrn] = pmsg->mpntr;
  125.  
  126.     return(pmsg->mpntr);
  127. }
  128.  
  129. COUNT dedusr(msgid)
  130. LONG         msgid;
  131. {
  132.     COUNT usrn;
  133.  
  134.     for (usrn = 0; usrn < cts_mxusr; usrn++)
  135.         if (msgid == ausrmid[usrn]) {
  136.             ausrmid[usrn] = 0;
  137.             return(usrn);
  138.         }
  139.     return(-1);
  140. }
  141.  
  142. COUNT ridmid()
  143. {
  144.     /* remove check file */
  145.     unlink(SERVER3);
  146.  
  147.     /* remove application message handling */
  148.     if (msgctl(srvid,IPC_RMID,NULL) < 0)
  149.         return(uerr_cod = SMRD_ERR);
  150.     sleep(5);
  151.     if (msgctl(apxid,IPC_RMID,NULL) < 0)
  152.         return(uerr_cod = SMRD_ERR);
  153.     return(NO_ERROR);
  154. }
  155.  
  156. COUNT ctrqst(msgadr,pmsg)
  157. PFAST TEXT **msgadr;
  158. MESSAGE           *pmsg;
  159. {
  160.     /* read input message */
  161.     if (msgrcv(srvid,*msgadr - LTYPE,msgsiz,0L,0) < 0)
  162.         return(uerr_cod = SRQS_ERR);
  163.  
  164.     /* copy message header */
  165.     cpybuf(pmsg,*msgadr,sizeof(MESSAGE));
  166.  
  167.     return(NO_ERROR);
  168. }
  169.  
  170. COUNT ctrspn(msgadr,pmsg,sizmsg,usrn)
  171. PFAST TEXT  *msgadr;
  172. MESSAGE           *pmsg;
  173. UCOUNT             sizmsg;
  174. COUNT                usrn;
  175. {
  176.     /* local copy of message id with correct type */
  177.     long int locmid;
  178.  
  179.     if (usrn < 0) {
  180.         usrn = - (usrn + 1);
  181.         locmid = ausrmid[usrn];
  182.         ausrmid[usrn] = 0;
  183.     } else
  184.         locmid = ausrmid[usrn];
  185.  
  186.     /* copy message header */
  187.     cpybuf(msgadr,pmsg,sizeof(MESSAGE));
  188.  
  189.     /* send response */
  190.     msgadr -= LTYPE;
  191.     *((LONG *) msgadr) = locmid;
  192.  
  193.     if (msgsnd(apxid,msgadr,sizmsg,IPC_NOWAIT) < 0)
  194.         if (errno != EAGAIN || msgsnd(apxid,msgadr,sizmsg,0) < 0)
  195.         return(uerr_cod = SRSP_ERR);
  196.  
  197.     return(NO_ERROR);
  198.  
  199. }
  200.  
  201. COUNT norspn(usrn)
  202. COUNT         usrn;
  203. {
  204.     return(NO);
  205. }
  206.  
  207. chkusrtim(usrtim,usrmap,usrtrn)
  208. LONG     *usrtim;
  209. COUNT        *usrmap;
  210. LONG               *usrtrn;
  211. {
  212.     /*
  213.      * This function can interrogate the usrtim[] array to
  214.      * determine if any active user slot is in fact dead.
  215.      */
  216.     return(NO_ERROR);
  217. }
  218.  
  219. /* end of ctsmsg.c */
  220.  
  221.