summaryrefslogtreecommitdiff
path: root/ipc/common/cm-sys-msg-queue.cpp
blob: 1adefd195cc800315f744458b7aac127033e5c6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
*  email-service
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/


#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "cm-sys-msg-queue.h"
#include "ipc-library-build.h"

cmSysMsgQueue::cmSysMsgQueue(int a_nMsgType)
{
	m_nMsgType = a_nMsgType;
	m_pSendBuf = NULL;
	m_pRecvBuf = NULL;	
	m_nMsgqid = -1;
	m_pSendBuf = (ipcMsgbuf * )malloc(sizeof(ipcMsgbuf));
	m_pRecvBuf = (ipcMsgbuf * )malloc(sizeof(ipcMsgbuf));
}

cmSysMsgQueue::~cmSysMsgQueue()
{
	if(m_pSendBuf) {
		free(m_pSendBuf);
		m_pSendBuf = NULL;
	}
	if(m_pRecvBuf) {
		free(m_pRecvBuf);
		m_pRecvBuf = NULL;
	}
	
}

void cmSysMsgQueue::MsgCreate()
{
	EM_DEBUG_FUNC_BEGIN();
	key_t key = (key_t)640;/* (key_t)a_nKey; */
	
	/* if ((m_nMsgqid = msgget(key, IPC_CREAT | IPC_EXCL | 0640)) == -1)  */
	if ((m_nMsgqid = msgget(key, IPC_CREAT | IPC_EXCL | 0777)) == -1) /*  Security Issue - DAC */ {
		m_nMsgqid = msgget(key, 1);

		/* ±âÁ¸ÀÇ ¸Þ¼¼Áö Å¥°¡ Á¸ÀçÇÑ´Ù¸é ÀÏ´Ü Áö¿ì°í ´Ù½Ã »ý¼ºÇÑ´Ù. */
		if(errno == EEXIST) {
			EM_DEBUG_LOG("Message Queue Exist ");
			/* msgctl(m_nMsgqid, IPC_RMID, (struct msqid_ds *)NULL);  */
			/* m_nMsgqid = msgget(key, IPC_CREAT | IPC_EXCL | 0640); */
			EM_DEBUG_LOG("===================================================== ");
			EM_DEBUG_LOG("[cmSysMsgQueue::MsgCreate][EXIST]");
			EM_DEBUG_LOG("=====================================================");
		}
	} 
	EM_DEBUG_LOG("=====================================================");
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgCreate][msggid = %d]", m_nMsgqid);
	EM_DEBUG_LOG("=====================================================");
	
}
int cmSysMsgQueue::MsgSnd(void *a_pMsg, int a_nSize, long a_nType)
{
	EM_DEBUG_FUNC_BEGIN();
	memset(m_pSendBuf, 0x00, sizeof(ipcMsgbuf));
	m_pSendBuf->mtype = a_nType;
	memcpy(m_pSendBuf->mtext, a_pMsg, a_nSize);
	int nRet;
	EM_DEBUG_LOG("===========================================================");
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgSnd][m_nMsgqid = %d]", m_nMsgqid);
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgSnd][a_nSize = %d]", a_nSize);
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgSnd][a_nType = %d]", a_nType);
	EM_DEBUG_LOG("===========================================================");
	nRet = msgsnd(m_nMsgqid, m_pSendBuf, IPC_MSGQ_SIZE, 0);		/*  0 °ª ¸»°í IPC_NOWAIT */

	EM_DEBUG_LOG("=====================================================");
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgSnd][ret = %d]", nRet);
	EM_DEBUG_LOG("=====================================================");
	return nRet;
}

int cmSysMsgQueue::MsgRcv(void *a_pMsg, int a_nSize)
{
	int nRet;
	EM_DEBUG_LOG("===========================================================");
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgRcv][m_nMsgqid = %d]", m_nMsgqid);
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgRcv][m_nMsgType = %d]", m_nMsgType);
	EM_DEBUG_LOG("===========================================================");
	nRet = msgrcv(m_nMsgqid, m_pRecvBuf, IPC_MSGQ_SIZE, m_nMsgType, 0);
	if (nRet == -1) {  	EM_DEBUG_LOG("===========================================================");
		EM_DEBUG_LOG("[cmSysMsgQueue::MsgRcv][FAIL]");
		EM_DEBUG_LOG("[cmSysMsgQueue::MsgRcv][error:=%d][errorstr =%s]", errno, strerror(errno));
		EM_DEBUG_LOG("===========================================================");
	}
	memcpy(a_pMsg, m_pRecvBuf->mtext, a_nSize);
	EM_DEBUG_LOG("=====================================================");
	EM_DEBUG_LOG("[cmSysMsgQueue::MsgRcv][ret = %d][mtype = %x][Size = %d]", nRet, m_nMsgType, a_nSize);
	EM_DEBUG_LOG("=====================================================");
	return nRet;
}

bool cmSysMsgQueue::MsgDestroy()
{
	if(msgctl(m_nMsgqid, IPC_RMID, NULL) == -1)
		return false;
	
	
	EM_DEBUG_LOG("=====================================================");
	EM_DEBUG_LOG("[cmSysMsgQueue][MsgDestroy]");
	EM_DEBUG_LOG("=====================================================");
	return true;
}