From: Hill on
Below code is the excise from Unix network programming: IPC.

But after building the code using : g++ thisfile.cpp -lrt
running: a.out /tmp/mq.1234 will cause the code print "errno=13
Permission denied".
How to solve this issue? Thank you!

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

#define FILE_MODE S_IRUSR|S_IWUSR
int main(int argc, char** argv){
int c, flags;
mqd_t mqd;
flags = O_RDWR|O_CREAT;

while( ( c = getopt(argc, argv, "e" ) ) != -1 ){
switch (c){
case 'e':
flags |= O_EXCL;
break;
}
}
if( optind != argc - 1 ){
printf("usage: mqcreate [ -e ] <name>");
return -1;
}


printf("FILE_MOED=%d\n", FILE_MODE);
mqd = mq_open( argv[optind], flags, FILE_MODE, NULL);
printf("The message queue d: %d \n", mqd);
printf("%d :%s \n", errno, strerror(errno));
mq_close(mqd);
exit(0);
}
From: Dmitry V. Krivenok on
Hill wrote:
> Below code is the excise from Unix network programming: IPC.
>
> But after building the code using : g++ thisfile.cpp -lrt
> running: a.out /tmp/mq.1234 will cause the code print "errno=13

Hello!

Try this:

../a.out -e /mq.1234

and use the following commands to see your new MQ:

olimpico_work ~ # mkdir /dev/mqueue
olimpico_work ~ # mount -t mqueue none /dev/mqueue
olimpico_work ~ # ls -l /dev/mqueue/
total 0
-rw------- 1 krivenok krivenok 80 Sep 2 10:56 mq.1234
olimpico_work ~ #

From man 7 mq_overview:

Each message queue is identified by a name of the form /somename; that
is, a null-terminated string of up to NAME_MAX (i.e.,
255) characters consisting of an initial slash, followed by one
or more characters, none of which are slashes.

And from man 2 mq_open:

EACCES name contained more than one slash.


> Permission denied".
> How to solve this issue? Thank you!
>
> #include <mqueue.h>
> #include <errno.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <string.h>
> #include <sys/stat.h>
>
> #define FILE_MODE S_IRUSR|S_IWUSR
> int main(int argc, char** argv){
> int c, flags;
> mqd_t mqd;
> flags = O_RDWR|O_CREAT;
>
> while( ( c = getopt(argc, argv, "e" ) ) != -1 ){
> switch (c){
> case 'e':
> flags |= O_EXCL;
> break;
> }
> }
> if( optind != argc - 1 ){
> printf("usage: mqcreate [ -e ] <name>");
> return -1;
> }
>
>
> printf("FILE_MOED=%d\n", FILE_MODE);
> mqd = mq_open( argv[optind], flags, FILE_MODE, NULL);
> printf("The message queue d: %d \n", mqd);
> printf("%d :%s \n", errno, strerror(errno));
> mq_close(mqd);
> exit(0);
> }