DimDqkov
DimDqkov

Reputation: 165

POSIX message queues - Error on open: Invalid argument

I have trouble with creating a message queue on linux. The error I'm getting is "Invalid argument". Another thing I have to mansion is that the code is a part of Qt project.

Common mistakes are the name does not start with (/) and the number of arg is not correct, but I have checked those!

here is the code that I'm trying to run:

#define MQ_TEST_PATH            "/test_queue"
#define MQ_MAX_MSG_SIZE         256
#define MQ_MAX_MSG_COUNT        20

struct mq_attr mqAttr;
mqAttr.mq_maxmsg = MQ_MAX_MSG_COUNT;
mqAttr.mq_msgsize = MQ_MAX_MSG_SIZE;

mq_unlink(MQ_TEST_PATH);

mode_t mode =  S_IRUSR | S_IWUSR;
int oflags = O_WRONLY | O_CREAT | O_EXCL;

mqd_t mqd;
mqd = mq_open(MQ_TEST_PATH, oflags, mode, &mqAttr);

if(mqd < 0){
    perror("Error on open");
    qDebug()<<mqd;
    return 0;
}

mq_close(mqd);
mq_unlink(MQ_TEST_PATH);

Output:

Error on open: Invalid argument
-1 

Upvotes: 8

Views: 11283

Answers (1)

oleg_g
oleg_g

Reputation: 532

man mq_open

EINVAL O_CREAT was specified in oflag, and attr was not NULL, but attr->mq_maxmsg or attr->mq_msqsize was invalid. Both of these fields must be greater than zero. In a process that is unprivileged (does not have the CAP_SYS_RESOURCE capability), attr->mq_maxmsg must be less than or equal to the msg_max limit

man mq_overview

> /proc/sys/fs/mqueue/msg_max

The default value for msg_max is 10.

Upvotes: 15

Related Questions