0

I am getting the exception: file1.c: mq_send: Message too long

The error seems self explanatory but I can not figure it out.

I have the struct and files:

typedef struct {
char path[5011];
char sharedMemSegmentNameBuf[30];
} messageQueueStruct;

file1.c

#include <sys/mman.h>

#define MAX_CACHE_REQUEST_LEN 5041
    messageQueueStruct mqStruct;
    int maxPathLength = MAX_CACHE_REQUEST_LEN - sizeof(mqStruct.sharedMemSegmentNameBuf);
strncpy(mqStruct.path, path, maxPathLength);
strncpy(mqStruct.sharedMemSegmentNameBuf, sharedMemSegmentNameBuf, sizeof(mqStruct.sharedMemSegmentNameBuf));
if (mq_send(qdCache, (const char *) &mqStruct, sizeof(mqStruct), 0) == -1)
{
    perror("handle_with_cache: mq_send");
    exit(1);
}

file2.c

    #include <sys/mman.h>

    #define MAX_CACHE_REQUEST_LEN 5041
        struct mq_attr attributes;
        attributes.mq_maxmsg = MAX_MESSAGES;
        attributes.mq_msgsize = MAX_CACHE_REQUEST_LEN;

       messageQueueStruct receiveStruct;
       int maxPathLength = MAX_CACHE_REQUEST_LEN - sizeof(receiveStruct.sharedMemSegmentNameBuf);

    messageQueueStruct receiveStruct;
    if (mq_receive(qdCache, (char *) &receiveStruct, MAX_CACHE_REQUEST_LEN, NULL) == -1)
    {
        perror("process request: mq_receive");
        exit(1);
    }

It looks to me that the sizes line up on the receive/ send sides. I also tested with just a char[] buffer and it worked. Some references I have looked at:

send struct in mq_send

https://w3.cs.jmu.edu/kirkpams/OpenCSF/Books/cs361/html/MessPassMQ.html

JoeG
  • 512
  • 8
  • 19
  • 2
    Please post an [MCVE]. – jwdonahue Mar 31 '19 at 03:33
  • Have you read the documentation on [mq_send](http://man7.org/linux/man-pages/man3/mq_send.3.html)? Your own documentation reference tells you that you can't have a message length greater than mq_attr.mq_msgsize. – jwdonahue Mar 31 '19 at 03:44
  • I have read the documentation, unless I am missing something my code above doesnt have a message greater then mq_attr.mq_msgsize. But to me the documentation sounds like it contradicts that. The following is from the man pages "The msg_len argument specifies the size of the buffer pointed to by msg_ptr; this must be greater than or equal to the mq_msgsize attribute of the queue" – JoeG Mar 31 '19 at 04:04
  • Correction: the man pages i quoted above are from the mq_receive(). So that does make sense to me and doesnt contradict what @jwdonahue said – JoeG Mar 31 '19 at 04:08
  • Where do you set the msgsize attribute? Show a complete example program. And are you sure your structure is that size? – Shawn Mar 31 '19 at 04:14
  • (unrelated to your question, but make sure you've read up on what `strncpy()` does before deciding to use it, since it's not what most people expect) – Shawn Mar 31 '19 at 04:19

0 Answers0