mq_timedsend() returns error 14 "bad address"

1.6k views Asked by At

I am getting bad address as the error code (14) when i execute below program.

int main (int argc, char *argv[])
{
    char response [20];
    struct mq_attr buf;
    buf.mq_msgsize = sizeof (response);
    buf.mq_maxmsg = 5;
    response[0]= '1';
    mqd_t handle = mq_open ("/test", O_CREAT | O_RDWR, 0, &buf);
    int status = mq_timedsend (handle, (char *) &response, sizeof (response), 0,250000);
    printf("value of status - %d\n", errno);

    pthread_t my_thread_id;
    pthread_attr_t attr;

}

Additional information: Updated the code as below and now i get error code 90 (message too long)

#include <stdio.h>
    #include <errno.h>
    #include <mqueue.h>
    #include <pthread.h>
    #include <time.h>


    static struct timespec RcvCmdWaitTime;
int main (int argc, char *argv[])
{
    char response [10];
    struct mq_attr buf;
    RcvCmdWaitTime.tv_sec = 0;
    RcvCmdWaitTime.tv_nsec = 250000;
    mqd_t handle;
    buf.mq_msgsize = sizeof response;
    buf.mq_maxmsg = 1;
    int status;

    if((handle = mq_open ("/test", O_CREAT | O_RDWR , S_IRWXU | S_IRWXG, &buf))==-1)
        printf("mq-open error, error no:%d\n",errno);

    status = mq_timedsend (handle, &response[0], sizeof response , 0, &RcvCmdWaitTime);
    if (status == -1)
        printf("mq_timedsend: Error:%d\n",errno);

}

i use : gcc -pthread multi_thr.c -lrt to compile

1

There are 1 answers

0
alk On

You should only test errno if the previous call indicated an error.

In this case if mq_timedsend() had returned -1.

int result = mq_timedsend (handle, response, sizeof response, 0, 250000);
if (-1 == result)
{
  perror("mq_timedsend() failed)"; /* This issues a human readable error description based on the value of errno. */
}

Also note that

  • casting response isn't nescessay
  • sizeof is an operator not a function
  • the error message your code uses is missleading

Also you want to check the outcome of mq_open():

mqd_t handle = mq_open ("/test", O_CREAT | O_RDWR, 0, &buf);
if (((mqd_t) -1) == handle)
{
  perror("mq_open() failed");
  abort(); /* or whatever to handle this error */
}