examples/PIPS/opencmessagequeuelibraryex/engine/src/msgqcreate.c

00001 
00010 #include <errno.h>
00011 #include <sys/msg.h>
00012 #include <sys/sem.h>
00013 #include "MsgQInternal.h"
00014 
00015 
00016 /********************************************************************************
00017 * MsgQCreate
00018 * Description: Creates a message queue
00019 * Inputs:
00020 *  qName   : ULONG that represents the queue name
00021 *  maxMsgs : ULONG that represents maximum size of messages 
00022 *********************************************************************************/
00023 EXPORT_C int MsgQCreate(ULONG qName, ULONG maxMsgs, ULONG qOptions, int* err) {
00024         int         qId;
00025         int         semId;
00026         int         semName ;
00027         struct msqid_ds  qStatus ;
00028         struct semid_ds  sStatus ;
00029         
00030         union semun {
00031                 int              val;
00032                 struct semid_ds* buf;
00033                 ushort_t*                array;
00034         } arg ;
00035 
00036 
00037         int          hashInstErr;
00038 
00039         if (MsgQTableLookup(qName) != NULL) {
00040                 *err = OK;
00041                 return (OK);
00042         }
00043         else {
00044                 if((qOptions == MSG_Q_FIFO) || (qOptions == MSG_Q_PRIORITY)) {
00045                         // Set msg queue options to  FIFO and create the message queue
00046                         qOptions= MSG_Q_FIFO ;
00047                         if((qId = msgget((key_t) qName ,IPC_CREAT | 0666 | IPC_EXCL )) >=0 ) {
00048                                 //  set msg queue parameter max # bytes in queue
00049                                 if(  msgctl(qId,IPC_STAT,&qStatus) == 0  )          
00050                                 if( qStatus.msg_qbytes > (maxMsgs * MAX_MSG_LEN) ) {
00051                                         qStatus.msg_qbytes = maxMsgs * MAX_MSG_LEN ;              
00052                                         if( msgctl(qId,IPC_SET,&qStatus) < 0) {
00053                                                 // delete message queue on error 
00054                                                 msgctl(qId,IPC_RMID,0);
00055                                                 *err = errno;
00056                                                 return(ERROR);
00057                                         }
00058                                 }
00059                                 // create semaphore
00060                                 semName= (key_t) qName;
00061                                 if((semId = semget(semName, 1, IPC_CREAT | 0666 |IPC_EXCL )) >= 0 ) {
00062                                         // set the semaphore value
00063                                         arg.buf = &sStatus;              
00064                                         arg.val = 1;
00065                                         semctl(semId , 0, SETVAL, arg) ;
00066                                         
00067                                         //install queue data in hash table
00068                                         if(InstallMsqQTable(qName, qId, semId, &hashInstErr) == OK) {
00069                                                 AddToMsgQTable(qName);
00070                                                 *err = OK;                
00071                                                 return (OK);
00072                                         }
00073                                         else {
00074                                                 //delete semaphore on error
00075                                                 semctl(semId,0,IPC_RMID,0) ;
00076                                                 *err = hashInstErr;
00077                                         }
00078                                 }
00079                                 else {
00080                                         // delete message queue on error
00081                                         msgctl(qId,IPC_RMID,0);
00082                                         *err = errno;
00083                                 }
00084                         }
00085                         else {
00086                                 *err = errno;
00087                         }       
00088                 }
00089                 else
00090                         *err = KMsgQLibParamErr;
00091 
00092                 return(ERROR);
00093         }
00094 }
00095 

Generated by  doxygen 1.6.2