clinuxposixmessage-queue

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.


Solution

  • 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);