utime returning -1 BAD File DEscriptor

Hi All,
First of all thanks for reading this post.
In my application, I am trying to create a new message queue . I am attaching the code below.

mqd_t mqopen2(const char * pName, 
                    unsigned long Flags, 
                    long  maxMsg, 
                    long  msgSz)
{
   int perms = 0600;          /* permissions */

   int oflags= O_RDWR| O_CREAT;

   int rd=0, wr=0;            /* -r and -w options */

   mqd_t mqd;                 /* returned msg queue descriptor */

   int c;

   char *path="/iothread8";                /* ->first non-option argument */

   struct mq_attr buf;        /* buffer for stat info */

   buf.mq_msgsize = 1024;

   buf.mq_maxmsg = 16;

   /* while */

   //if (optind < argc)

   //   path = argv[optind]; /* first non-option argument */

   //else

   //   { printf("Queue pathname required\n"); return -1; }

   mqd = mq_open(path,oflags,perms,&buf);

   if (-1 != mqd)

   {
    printf("Reached inside the loop\n");

      if ( ! mq_getattr(mqd,&buf) )

      {

         printf("flags: 0x%x  maxmsg: %d  msgsize: %d  curmsgs: %d\n",

         buf.mq_flags, buf.mq_maxmsg, buf.mq_msgsize, buf.mq_curmsgs);

      }

      else

         perror("mq_getattr()");

   }

   else

      perror("mq_open()");
return mqd;
}

But mq_open is return -1 with EFAULT as error code. This is a part of my big application So for checking the method i wrote a sample code with the same method and tried to create a message queue. It was working perfectly fine.
I run strace for the program to get the details of the bug.
(i have attached the entire strace for reference.)
In that it is mentioned as

utimes("iothread8", {{...}, {...}})     = -1 EFAULT (Bad address)

Please help me to get out of this issue. Thanks again,
Parvathy

A message queue is not a file. You can't call utimes() on it.

I forget how to open a queue, already, because MQ came with examples I just hacked! Each call depends on prior calls. Is mq_open first, and is the target queue manager already running? Is this a tcp client, or local?

Hi All,
Thanks for your time. Pls help me to solve it out.

I am not calling utimes(), i am calling only mq_open(), but in the strace report i am getting it as utime() returns BAD DESCRIPTOR error.

@DGPickett: It is not a TCP Client. and i dint get you fully? How can I know that the queue manager is already running.

Regards,
Parvathy.

Perhaps the message queue exists already, so O_CREAT causes it to refuse to create it again?

Well, you can ask the person who put the queue up, or put it up yourself, or "ps -ef | grep mq".

As I said, there is example code included, great to round out the explanations.

@Corona688: I also got the same doubt, so I tried removing O_CREATE with O_EXCL; but that also dint work:(
@DGPickett: Sorry I dint get you fully. Can you please elaborate a little more?

Once Again Thanks All:)

Hmmmm. EFAULT is a really strange error code for it to give you.

Your name looks correct, as man mq_overview demands it, to wit, "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."

Your permissions and flags also look sane.

What's left? buf. Try opening with NULL instead of &buf.

mq_attr has at least four members:

           struct mq_attr {
               long mq_flags;       /* Flags: 0 or O_NONBLOCK */
               long mq_maxmsg;      /* Max. # of messages on queue */
               long mq_msgsize;     /* Max. message size (bytes) */
               long mq_curmsgs;     /* # of messages currently in queue */
           };

...but you don't set mq_flags could contain garbage. mq_curmsgs probably doesn't care what's in it but to be sure I'd just blank the whole thing with memset(&buf, 0, sizeof(buf)); then set the things I want.

Well, it looks like you are following an example, so perhaps there is a permission or name collision problem with PATH. Try flags 0 if it exists.
Chapter 6. Message Queues

Hi All,
Thanks for your time and valuable suggestions. For the last few days i was out of station. So could not reply to your post. Sorry for the delay in my response.
@Corona688 : I tried using NULL and also with tried clearing buf with memset. But it is giving the same old error.
Is there any other hidden problems? It is a critical part of my project. Please help me out.
Regards,
Parvathy

The following code works for me:

$ cat mq.c
#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <mqueue.h>

#include <stdio.h>
#include <string.h>

mqd_t mqopen2(const char * pName,
                    unsigned long Flags,
                    long  maxMsg,
                    long  msgSz);

int main(void)
{
        mqd_t m=mqopen2(NULL, 0, 16, 1024);

        printf("mq is %d\n", m);

        mq_close(m);
}

mqd_t mqopen2(const char * pName,
                    unsigned long Flags,
                    long  maxMsg,
                    long  msgSz)
{
   int perms = 0600;          /* permissions */
   int oflags= O_RDWR| O_CREAT;
   int rd=0, wr=0;            /* -r and -w options */
   mqd_t mqd;                 /* returned msg queue descriptor */
   int c;
   char *path="/iothread8";                /* ->first non-option argument */
   struct mq_attr buf;        /* buffer for stat info */

   memset(&buf, 0, sizeof(buf)); // WILL NOT WORK without this!

   buf.mq_msgsize = msgSz;
   buf.mq_maxmsg = maxMsg;
   buf.mq_flags = Flags;

   /* while */
   //if (optind < argc)
   //   path = argv[optind]; /* first non-option argument */
   //else
   //   { printf("Queue pathname required\n"); return -1; }
   mqd = mq_open(path,oflags,perms,&buf);
   if (-1 != mqd)
   {
    printf("Reached inside the loop\n");

      if ( ! mq_getattr(mqd,&buf) )
      {
         printf("flags: 0x%lx  maxmsg: %ld  msgsize: %ld  curmsgs: %ld\n",
         buf.mq_flags, buf.mq_maxmsg, buf.mq_msgsize, buf.mq_curmsgs);
      }
      else
         perror("mq_getattr()");
   }
   else
      perror("mq_open()");

   return mqd;
}
$ gcc mq.c -o mq -lrt
$ ./mq
Reached inside the loop
flags: 0x0  maxmsg: 10  msgsize: 8192  curmsgs: 0
mq is 3
$

It doesn't give you the sizes you want but it does go... Try it in a .c file by itself like above. I'm beginning to suspect the problem isn't related to message queues but an error somewhere else in the larger part of your code (the part we of course haven't seen).

Hi,
@Corona688: Yes you are ryt. when i tried in a seperate it is working perfectly fine. So now how can i debug it. it is code base of more than 500 classes. So can you please suggest some insights for me to start with.

I can't help you with code I can't see.

On reflection, I think EFAULT implies you have memory corruption somewhere that's causing things to get fed invalid pointers. Check every pointer and reference (including class pointers and references) in the call chain leading up to mqopen2 to make sure they're all sane.

Also, you can truss/tusc/strace to get a better idea of what it was up to.

I am using Strace. i had attached the report with the first post. I tried msgget insted of mq_open and it is working perfectly fine. Can you please tell me the difference between both?

Must already be open?