isolationserver/messagequeue/src/msgqcreate-del.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /** 
       
     2  *  @file MsgQCreate.cpp
       
     3  *  Description: Source file for MsgQLib's MsgQCreate API
       
     4  *  Copyright (c) 2007 Nokia Corporation.
       
     5 *  All rights reserved.
       
     6 *  Redistribution and use in source and binary forms, with or without modification, 
       
     7 *  are permitted provided that the following conditions are met:
       
     8 *  Redistributions of source code must retain the above copyright notice, this list 
       
     9 *  of conditions and the following disclaimer.Redistributions in binary form must 
       
    10 *  reproduce the above copyright notice, this list of conditions and the following 
       
    11 *  disclaimer in the documentation and/or other materials provided with the distribution.
       
    12 *  Neither the name of the Nokia Corporation nor the names of its contributors may be used 
       
    13 *  to endorse or promote products derived from this software without specific prior written 
       
    14 *  permission.
       
    15 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
       
    16 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
       
    17 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
       
    18 *  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
       
    19 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
       
    20 *  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
       
    21 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
       
    22 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
       
    23 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    24  */
       
    25 
       
    26 #include <errno.h>
       
    27 #include <sys/msg.h>
       
    28 #include <sys/sem.h>
       
    29 #include <MsgQInternal.h>
       
    30 
       
    31 
       
    32 /********************************************************************************
       
    33 * MsgQCreate
       
    34 * Description: Creates a message queue
       
    35 * Inputs:
       
    36 *  qName   : ULONG that represents the queue name
       
    37 *  maxMsgs : ULONG that represents maximum size of messages 
       
    38 *********************************************************************************/
       
    39 EXPORT_C int MsgQCreate(ULONG qName, ULONG maxMsgs, ULONG qOptions, int* err) {
       
    40 	int         qId;
       
    41 	//int         semId;
       
    42 	//int         semName ;
       
    43 	struct msqid_ds  qStatus ;
       
    44 	//struct semid_ds  sStatus ;
       
    45 	
       
    46 	//union semun {
       
    47 	//	int              val;
       
    48 	//	struct semid_ds* buf;
       
    49 	//	ushort_t*		 array;
       
    50 	//} arg ;
       
    51 
       
    52 	int          hashInstErr;
       
    53 
       
    54 	//if (MsgQTableLookup(qName) != NULL) {
       
    55 	//	*err = OK;
       
    56 	//	return (OK);
       
    57 	//}
       
    58 	//else {
       
    59 			if((qOptions == MSG_Q_FIFO) || (qOptions == MSG_Q_PRIORITY)) {
       
    60 				// Set msg queue options to  FIFO and create the message queue
       
    61 				qOptions= MSG_Q_FIFO ;
       
    62 
       
    63 			if((qId = msgget((key_t) qName ,IPC_CREAT | 0666 | IPC_EXCL )) >=0 ) {
       
    64 				//  set msg queue parameter max # bytes in queue
       
    65 				
       
    66 				if( msgctl(qId,IPC_STAT,&qStatus) == 0  ) 
       
    67 				
       
    68 				if( qStatus.msg_qbytes > (maxMsgs * MAX_MSG_LEN) ) {
       
    69 					qStatus.msg_qbytes = maxMsgs * MAX_MSG_LEN ;              
       
    70 					if( msgctl(qId,IPC_SET,&qStatus) < 0) {
       
    71 						// delete message queue on error 
       
    72 						msgctl(qId,IPC_RMID,0);
       
    73 						*err = errno;
       
    74 						return(ERROR);
       
    75 					}
       
    76 				}
       
    77 				*err = OK;                
       
    78 				return (OK);
       
    79 					
       
    80 				// create semaphore
       
    81 				//semName= (key_t) qName;
       
    82 				//if((semId = semget(semName, 1, IPC_CREAT | 0666 |IPC_EXCL )) >= 0 ) {
       
    83 					// set the semaphore value
       
    84 				//	arg.buf = &sStatus;              
       
    85 				//	arg.val = 1;
       
    86 				//	semctl(semId , 0, SETVAL, arg) ;
       
    87 					
       
    88 					//install queue data in hash table
       
    89 				//	if(InstallMsqQTable(qName, qId, semId, &hashInstErr) == OK) {
       
    90 				//		AddToMsgQTable(qName);
       
    91 				//		*err = OK;                
       
    92 				//		return (OK);
       
    93 				//	}
       
    94 				//	else {
       
    95 						//delete semaphore on error
       
    96 				//		semctl(semId,0,IPC_RMID,0) ;
       
    97 				//		*err = hashInstErr;
       
    98 				//	}
       
    99 				//}
       
   100 				//else {
       
   101 					// delete message queue on error
       
   102 				//	msgctl(qId,IPC_RMID,0);
       
   103 				//	*err = errno;
       
   104 				//}
       
   105 			}
       
   106 			else {
       
   107 				*err = errno;
       
   108 			}       
       
   109 		}
       
   110 		else
       
   111 			*err = KMsgQLibParamErr;
       
   112 
       
   113 		return(ERROR);
       
   114 	//}
       
   115 }
       
   116