Search code examples
csegmentation-faultposixmessage-queue

C mq_receive() with struct causes seg fault when trying to access


So I have two programs that connect to a message queue, and one sends a message in the form of a struct to the other. However, When I try to access the struct after recieving it, I get a segmentation fault.

I can't figure out what I need to do to get access to the struct after its sent.

Here's my code for the sender:

#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

typedef struct {
  char path[2048];
  char shm_name[50];
  size_t shm_s;
  char sem_send_name[50];
    char sem_recv_name[50];
} cache_request;

static void showAttr(mqd_t fd)
{
    struct mq_attr attr;
    mq_getattr(fd, &attr);
    printf("maxmsg = %ld\n", attr.mq_maxmsg);
    printf("msgsize = %ld\n", attr.mq_msgsize);
    printf("curmsgs = %ld\n", attr.mq_curmsgs);
}

int main()
{
    mqd_t fd;
    int ret;

    struct mq_attr attr;
    int flags = O_RDWR | O_CREAT;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 3;
    attr.mq_msgsize = 2216;
    attr.mq_curmsgs = 0;
    fd = mq_open("/mq", flags,(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),&attr );
    if (fd < 0) {
        printf("open failed %d\n", fd);
        exit(EXIT_FAILURE);
    }
    printf("open ok\n");
    sleep(1);
    showAttr(fd);
    cache_request* msg = (cache_request*)malloc(sizeof(cache_request));
    strcpy(msg->path,"ok\n");
    strcpy(msg->shm_name, "ex1\n");
    msg->shm_s = 250;
    strcpy(msg->sem_send_name," ex2");
    strcpy(msg->sem_recv_name, "ex3");
    printf("hmm %s\n", msg->shm_name);
    printf("hmm %ld\n", msg->shm_s);
    //res = mq_receive(fd, (char*) &msg, sizeof(cache_request), NULL);
    int res = mq_send(fd, (const char*) &msg, sizeof(cache_request), 0);
    if (res < 0) {
          printf ("   Error %d (%s) on server mq_send.\n",
              errno, strerror (errno));
          mq_close(fd);
          mq_unlink("/mq");
          exit (1);
      }
    sleep(10);
    ret = mq_close(fd);
    if (ret != 0) {
        printf("open failed\n");
        exit(EXIT_FAILURE);
    }
    printf("close ok\n");
    sleep(20);
    mq_unlink("/mq");
    return 0;
}

And here's the receiver:

#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>
#include <unistd.h>

/*
gcc [file] -lrt
*/

typedef struct {
  char path[2048];
  char shm_name[50];
  size_t shm_s;
  char sem_send_name[50];
    char sem_recv_name[50];
} cache_request;


static void showAttr(mqd_t fd)
{
    struct mq_attr attr;

    mq_getattr(fd, &attr);

    printf("maxmsg = %ld\n", attr.mq_maxmsg);
    printf("msgsize = %ld\n", attr.mq_msgsize);
    printf("curmsgs = %ld\n", attr.mq_curmsgs);

}

int main()
{
    mqd_t fd;
    int ret;




    mq_unlink("/mq");
    struct mq_attr attr;
    int flags = O_RDWR;
    attr.mq_flags = 0;
    attr.mq_maxmsg = 3; // ***
    attr.mq_msgsize = 141;
    attr.mq_curmsgs = 0;

    while((fd = mq_open("/mq", O_RDWR)) == -1){
      printf("Couldnt connect to message queue in cache\n");
      sleep(2);
    }

    if (fd < 0) {
        printf("open failed %d\n", fd);
        exit(EXIT_FAILURE);
    }
    printf("open ok\n");

    //sleep(5);

    showAttr(fd);
    char* rsp_msg = (char*)malloc(2216);
    int res = mq_receive(fd, (char*) &rsp_msg, 2216, NULL);
    printf("recieved: %d\n", res);
        printf("should be %ld\n", sizeof(cache_request));
    if (res < 0) {
          printf ("   Error %d (%s) on server mq_receive.\n",
              errno, strerror (errno));
          mq_close(fd);
          mq_unlink("/mq");
          exit (1);
      }
    cache_request* msg = (cache_request*)rsp_msg;
    printf("shm_s: %ld\n", msg->shm_s); //THIS is where the seg fault happens
        printf("shm_name: %s\n", msg->shm_name);
    ret = mq_close(fd);
    if (ret != 0) {
        printf("close failed\n");
        exit(EXIT_FAILURE);
    }
    printf("close ok\n");

    return 0;
}


Solution

  • int res = mq_send(fd, (const char*) &msg, sizeof(cache_request), 0);
    

    Should be msg, not &msg. You want to send the data that msg points to, not the pointer itself.

    int res = mq_receive(fd, (char*) &rsp_msg, 2216, NULL);
    

    Likewise.

    Also, better to use sizeof(cache_request) instead of hardcoding 2216.