yellowantphil
yellowantphil

Reputation: 1493

mq_open: Invalid argument

I'm trying to run the example program for POSIX message queues found in the man page for mq_notify. I'm running it as ./mq '/bla' and it gives me the error mq_open: Invalid argument.

This is the line in the sample program that gives the error:

mqdes = mq_open(argv[1], O_RDONLY);

I've tried changing it to

mqdes = mq_open("/bla", O_RDONLY | O_CREAT);

but it still doesn't work.

This must be simple, but I can't figure it out. What am I doing wrong?

This is RHEL 5.8, by the way.

EDIT: I was wrong about the first error. Without O_CREAT, it said "No such file or directory". I guess it was trying to open a message queue that didn't exist. With O_CREAT, I think the invalid argument error was because I only had two arguments, and you need four with O_CREAT.

Upvotes: 4

Views: 20026

Answers (2)

Lucien Murray-Pitts
Lucien Murray-Pitts

Reputation: 301

Whilst its been a while I wanted to use mq_* routines but I ran into problems. In my case I was on RHEL8.9

  1. On a freshly installed machine the /dev/mqueue the permissions where rwxr--r-- but it should be drwxrwxrwt (1777) ; weirdly running my app as root fixed that

  2. You should check /proc/sys/fs/mqueue/msg_max and msgsize_max show the max you can call in the mq_open call. In my case I was asking for 32 messages of 128 bytes, but msg_max was 10 so failed with invalid argument

After fixing the permissions, and then reducing the number of messages my app would run.

Upvotes: 1

nos
nos

Reputation: 229058

Just mq_open(argv[1], O_RDONLY); should fail with "ENOENT (No such file or directory)" if the message queue does not exist.

If you change it to use O_CREAT, you need to pass 2 additional arguments to mq_open(). (read the paragraph about O_CREAT).e.g.

    mq_open(argv[1], O_RDONLY | O_CREAT, 0666, NULL);

Upvotes: 12

Related Questions