drdot
drdot

Reputation: 3347

Cannot resolve this: ERROR [EINVAL Invalid argument] mq_open

I am learning message queue from the examples from the book sample code: https://github.com/bradfa/tlpi-dist/blob/master/pmsg/pmsg_create.c

However, after compiling the code and run it with

./pmsg_create -c aaa

I got

ERROR [EINVAL Invalid argument] mq_open

Running it with sudo does not resolve the issue either. I have tried to specified the maxmsg and msgsize parameter from the command line with different parameters, still the same error.

I looked around in stackoverflow for questions in the past, but none of them seems to resolve my problem. I am running it on ubuntu 20.04. Posted the code below.

Can someone let me know the reason for the error and the resolution?

#include <mqueue.h>                                                                                                                              
#include <sys/stat.h>                                                                                                                            
#include <fcntl.h>                                                                                                                               
#include "tlpi_hdr.h"                                                                                                                            
                                                                                                                                                 
static void                                                                                                                                      
usageError(const char *progName)                                                                                                                 
{                                                                                                                                                
    fprintf(stderr, "Usage: %s [-cx] [-m maxmsg] [-s msgsize] mq-name "                                                                          
            "[octal-perms]\n", progName);                                                                                                        
    fprintf(stderr, "    -c          Create queue (O_CREAT)\n");                                                                                 
    fprintf(stderr, "    -m maxmsg   Set maximum # of messages\n");                                                                              
    fprintf(stderr, "    -s msgsize  Set maximum message size\n");                                                                               
    fprintf(stderr, "    -x          Create exclusively (O_EXCL)\n");                                                                            
    exit(EXIT_FAILURE);                                                                                                                          
}                                                                                                                                                
                                                                                                                                                 
int                                                                                                                                              
main(int argc, char *argv[])                                                                                                                     
{                                                                                                                                                
    int flags, opt;                                                                                                                              
    mode_t perms;                                                                                                                                
    mqd_t mqd;                                                                                                                                   
    struct mq_attr attr, *attrp;                                                                                                                 
                                                                                                                                                 
    /* If 'attrp' is NULL, mq_open() uses default attributes. If an                                                                              
       option specifying a message queue attribute is supplied on the                                                                            
       command line, we save the attribute in 'attr' and set 'attrp'                                                                             
       pointing to 'attr'. We assign some (arbitrary) default values                                                                             
       to the fields of 'attr' in case the user specifies the value                                                                              
       for one of the queue attributes, but not the other. */                                                                                    
                      
    attrp = NULL;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 2048;
    flags = O_RDWR;

    /* Parse command-line options */

    while ((opt = getopt(argc, argv, "cm:s:x")) != -1) {
        switch (opt) {
        case 'c':
            flags |= O_CREAT;
            break;

        case 'm':
            attr.mq_maxmsg = atoi(optarg);
            attrp = &attr;
            break;

        case 's':
            attr.mq_msgsize = atoi(optarg);
            attrp = &attr;
            break;

        case 'x':
            flags |= O_EXCL;
            break;

        default:
            usageError(argv[0]);
        }
    }

    if (optind >= argc)
        usageError(argv[0]);

    perms = (argc <= optind + 1) ? (S_IRUSR | S_IWUSR) :
                getInt(argv[optind + 1], GN_BASE_8, "octal-perms");

    printf("optind = %d, argv[%d] = %s, flags = %x, perms = %o\n", optind, optind, argv[optind], flags, perms);
    mqd = mq_open(argv[optind], flags, perms, attrp);
    if (mqd == (mqd_t) -1)
        errExit("mq_open");

    exit(EXIT_SUCCESS);
}

Upvotes: 0

Views: 726

Answers (1)

James R.
James R.

Reputation: 143

If you read the manpage for mq_open (here's an online copy), it says:

EINVAL name doesn't follow the format in mq_overview(7).

mq_format(7) says that the name needs to begin with a slash (/), which aaa does not.

Upvotes: 1

Related Questions