1 /** |
|
2 * @file MsgQRecv.cpp |
|
3 * Description: Source file for MsgQLib's MsgQReceive 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 <unistd.h> |
|
30 #include <string.h> |
|
31 #include <msgqinternal.h> |
|
32 |
|
33 |
|
34 |
|
35 /******************************************************************************* |
|
36 * MsgQReceive (qName, msg, maxNBytes, timeout, err) |
|
37 * Description: Function for receiving a message with internal copy |
|
38 ********************************************************************************/ |
|
39 |
|
40 EXPORT_C int MsgQReceive(ULONG qName, char* msg, ULONG maxNBytes, int timeout, int* err) { |
|
41 ssize_t rxBytes; |
|
42 int toTicks; |
|
43 |
|
44 int qId; |
|
45 struct { |
|
46 long mtype; |
|
47 char mtext[MAX_MSG_LEN]; |
|
48 } message; |
|
49 |
|
50 /* check parameters */ |
|
51 |
|
52 if((qId = msgget((key_t) qName ,IPC_CREAT | 0666 /*| IPC_EXCL */ )) >=0 ) { |
|
53 |
|
54 toTicks = timeout; |
|
55 message.mtype = 1; |
|
56 /* receive message */ |
|
57 if((rxBytes = msgrcv(qId, &message, (size_t)maxNBytes, 1, toTicks)) != -1) { |
|
58 message.mtext[rxBytes-4] = '\0'; |
|
59 bcopy(message.mtext, msg, rxBytes); |
|
60 |
|
61 *err = OK; |
|
62 return ((ULONG)rxBytes); |
|
63 } |
|
64 else { |
|
65 *err = errno; |
|
66 } |
|
67 |
|
68 } |
|
69 else { |
|
70 *err = KMsgQLibQIdErr; |
|
71 } |
|
72 |
|
73 return(ERROR); |
|
74 } |
|
75 |
|
76 |
|