示例#1
0
int setMessageQueueBlockingMode(MessageQueue* messageQueue, bool activate)
{
    messageQueue->attributes.mq_flags = (activate) ? 0 : O_NONBLOCK;

    if (mq_setattr(messageQueue->mq_des, &messageQueue->attributes, NULL) == -1)
    {
        perror("\n\rmq_setattr failed !!!\n");

        switch(errno)
        {
            case EBADF:
                perror("The descriptor specified in mqdes is invalid.\n");
                break;
            case EINVAL:
                perror("newattr->mq_flags contained set bits other than O_NONBLOCK.\n");
                break;
            default :
                perror("Unknown error\n");
                break;
        }

        return -1;
    }

    return 0;
}
示例#2
0
文件: mq.c 项目: MIPS/external-strace
int
main(void)
{
	struct mq_attr attr;
	(void) close(0);

	char *name;
	if (asprintf(&name, "/strace-mq-%u.sample", getpid()) < 0)
		perror_msg_and_fail("asprintf");

	if (mq_open(name, O_CREAT, 0700, NULL))
		perror_msg_and_skip("mq_open");
	printf("mq_open(\"%s\", O_RDONLY|O_CREAT, 0700, NULL) = 0\n", name + 1);

	if (mq_getattr(0, &attr))
		perror_msg_and_skip("mq_getattr");
	printf("mq_getsetattr(0, NULL, {mq_flags=0, mq_maxmsg=%lld"
	       ", mq_msgsize=%lld, mq_curmsgs=0}) = 0\n",
	       (long long) attr.mq_maxmsg,
	       (long long) attr.mq_msgsize);

	if (mq_setattr(0, &attr, NULL))
		perror_msg_and_skip("mq_setattr");
	printf("mq_getsetattr(0, {mq_flags=0, mq_maxmsg=%lld"
	       ", mq_msgsize=%lld, mq_curmsgs=0}, NULL) = 0\n",
	       (long long) attr.mq_maxmsg,
	       (long long) attr.mq_msgsize);

	if (mq_unlink(name))
		perror_msg_and_skip("mq_unlink");
	printf("mq_unlink(\"%s\") = 0\n", name + 1);

	puts("+++ exited with 0 +++");
	return 0;
}
示例#3
0
CAMLprim value
caml_backpack_mq_setattr(value val_mq, value val_flags)
{
	CAMLparam2(val_mq, val_flags);
	struct mq_attr attr = {
		.mq_flags = caml_convert_flag_list(val_flags, mqueue_flags)
	};

	if (mq_setattr(Int_val(val_mq), &attr, NULL) == -1)
		uerror("mq_setattr", Nothing);

	CAMLreturn(Val_unit);
}

CAMLprim value
caml_backpack_mq_send(value val_mq, value val_buff, value val_ofs,
		      value val_len, value val_prio)
{
	CAMLparam5(val_mq, val_buff, val_ofs, val_len, val_prio);

	if (mq_send(Int_val(val_mq), &Byte(val_buff, Long_val(val_ofs)),
		    Long_val(val_len), Int_val(val_prio)) == -1)
		uerror("mq_send", Nothing);

	CAMLreturn(Val_unit);
}
示例#4
0
int main(void)
{
  mqd_t mqdes;
  struct mq_attr m1;
  struct mq_attr m2;
  mq_setattr(mqdes, &m1, &m2);
  return 0;
}
示例#5
0
文件: 8-1-buildonly.c 项目: 1587/ltp
void test_mq_setattr_prototype(void)
{
	mqd_t mqdes;
	struct mq_attr mqs, omqs;
	int err;

	err = mq_setattr(mqdes, &mqs, &omqs);

}
示例#6
0
文件: 2-1.c 项目: Nan619/ltp-ddt
int main()
{
	char mqname[NAMESIZE];
	mqd_t mqdes;
	struct mq_attr omqstat, mqstat, nmqstat;
	int unresolved = 0;
	int failure = 0;

	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());

	mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
	if (mqdes == (mqd_t) - 1) {
		perror("mq_open() did not return success");
		return PTS_UNRESOLVED;
	}
	memset(&mqstat, 0, sizeof(mqstat));
	memset(&omqstat, 0, sizeof(omqstat));
	memset(&nmqstat, 0, sizeof(nmqstat));

	if (mq_getattr(mqdes, &omqstat) == -1) {
		perror("mq_getattr() did not return success");
		unresolved = 1;
	}
	if (mq_unlink(mqname) != 0) {
		perror("mq_unlink()");
		return PTS_UNRESOLVED;
	}
	nmqstat.mq_flags = MQFLAGS;
	nmqstat.mq_maxmsg = MQMAXMSG;
	nmqstat.mq_msgsize = MQMSGSIZE;
	nmqstat.mq_curmsgs = MQCURMSGS;

	if (mq_setattr(mqdes, &nmqstat, &mqstat) != 0) {
		failure = 1;
	}
	if ((omqstat.mq_flags != mqstat.mq_flags) ||
	    (omqstat.mq_maxmsg != mqstat.mq_maxmsg) ||
	    (omqstat.mq_msgsize != mqstat.mq_msgsize) ||
	    (omqstat.mq_curmsgs != mqstat.mq_curmsgs)) {
		failure = 1;
	}

	mq_close(mqdes);
	mq_unlink(mqname);

	if (failure == 1) {
		printf("Test FAILED\n");
		return PTS_FAIL;
	}
	if (unresolved == 1) {
		printf("Test UNRESOLVED\n");
		return PTS_UNRESOLVED;
	}

	printf("Test PASSED \n");
	return PTS_PASS;
}
示例#7
0
文件: 1-2.c 项目: shubmit/shub-ltp
int main()
{
	char mqname[NAMESIZE];
	mqd_t mqdes;
	struct mq_attr mqstat, nmqstat;
	int unresolved = 0;
	int failure = 0;

	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());

	mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
	if (mqdes == (mqd_t)-1) {
		perror("mq_open() did not return success");
		return PTS_UNRESOLVED;
	}
	memset(&mqstat,0,sizeof(mqstat));
	memset(&nmqstat,0,sizeof(mqstat));

	if (mq_getattr(mqdes, &mqstat) == -1) {
		perror(ERROR_PREFIX "mq_getattr");
		unresolved = 1;
	}
	mqstat.mq_maxmsg = mqstat.mq_maxmsg + 1;
	mqstat.mq_msgsize = mqstat.mq_msgsize + 1;
	mqstat.mq_curmsgs = mqstat.mq_curmsgs + 1;

	if (mq_setattr(mqdes, &mqstat, NULL) != 0)	{
		perror(ERROR_PREFIX "mq_setattr()");
		failure = 1;
	}
	if (mq_getattr(mqdes, &nmqstat) == -1)	{
		perror(ERROR_PREFIX "mq_getattr()");
		unresolved = 1;
	}
	if ((nmqstat.mq_maxmsg == mqstat.mq_maxmsg)||
	    (nmqstat.mq_msgsize == mqstat.mq_msgsize)||
            (nmqstat.mq_curmsgs == mqstat.mq_curmsgs)) {
		failure = 1;
	}

	mq_close(mqdes);
	mq_unlink(mqname);

	if (failure == 1) {
                printf("Test FAILED\n");
                return PTS_FAIL;
        }
	if (unresolved == 1) {
                printf("Test UNRESOLVED\n");
                return PTS_UNRESOLVED;
        }

	printf("Test PASSED \n");
	return PTS_PASS;
}
示例#8
0
void testValues() {
    f = 2;
    
    mqd_t mq = mq_open(anystring(), anyint());
    struct mq_attr attrs;
    struct mq_attr oattrs;
    mq_setattr(mq, &attrs, &oattrs);

    //@ assert f == 2;
    //@ assert vacuous: \false;
}
示例#9
0
int main()
{
	char mqname[NAMESIZE];
	mqd_t mqdes;
	struct mq_attr mqstat, nmqstat;
	int unresolved = 0;
	int failure = 0;

	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());

	mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
	if (mqdes == (mqd_t)-1) {
		perror("mq_open() did not return success");
		return PTS_UNRESOLVED;
	}
	memset(&mqstat,0,sizeof(mqstat));
	memset(&nmqstat,0,sizeof(nmqstat));
	if (mq_getattr(mqdes, &mqstat) == -1) {
		perror(ERROR_PREFIX "mq_getattr");
		unresolved = 1;
	}
	mqstat.mq_flags |= MQFLAGS;
	if (mq_setattr(mqdes, &mqstat, NULL) != 0)	{
		perror(ERROR_PREFIX "mq_setattr");
		failure = 1;
	}
	if (mq_getattr(mqdes, &nmqstat) == -1)	{
		perror(ERROR_PREFIX "mq_getattr");
		unresolved = 1;
	}
	mq_close(mqdes);
	if (mq_unlink(mqname) != 0) {
	       perror(ERROR_PREFIX "mq_unlink");
       	       return PTS_UNRESOLVED;	       
	}
	if (nmqstat.mq_flags != mqstat.mq_flags)  {
		printf("FAIL: mq_flags getted is %ld , while mq_flags "
		       "setted is %ld\n", nmqstat.mq_flags, mqstat.mq_flags);
		failure = 1;
	}

	if (failure == 1) {
                printf("Test FAILED\n");
                return PTS_FAIL;
        }
	if (unresolved == 1) {
                printf("Test UNRESOLVED\n");
                return PTS_UNRESOLVED;
        }

	printf("Test PASSED \n");
	return PTS_PASS;
}
示例#10
0
/*
 * call-seq:
 *	mq.attr = POSIX_MQ::Attr(IO::NONBLOCK)		=> mq_attr
 *
 * Only the IO::NONBLOCK flag may be set or unset (zero) in this manner.
 * See the mq_setattr(3) manpage for more details.
 *
 * Consider using the POSIX_MQ#nonblock= method as it is easier and
 * more natural to use.
 */
static VALUE setattr(VALUE self, VALUE astruct)
{
	struct posix_mq *mq = get(self, 1);
	struct mq_attr newattr;

	rstruct2mqattr(&newattr, astruct, 0);

	if (mq_setattr(mq->des, &newattr, NULL) < 0)
		rb_sys_fail("mq_setattr");

	return astruct;
}
示例#11
0
void mqueue_check_functions(mqd_t m)
{
    (void)mq_close(m);
    (void)mq_getattr(m, (struct mq_attr *)0);
    (void)mq_notify(m, (const struct sigevent *)0);
    (void)mq_open((const char *)0, 0, 0);
    (void)mq_receive(m, (char*)0, (size_t)0, (unsigned*)0);
    (void)mq_send(m, (const char *)0, (size_t)0, (unsigned)0);
    (void)mq_setattr(m, (const struct mq_attr *)0, (struct mq_attr *)0);
    (void)mq_timedreceive(m, (char*)0, (size_t)0, (unsigned*)0, (const struct timespec*)0);
    (void)mq_timedsend(m, (const char *)0, (size_t)0, (unsigned)0, (const struct timespec*)0);
    (void)mq_unlink((const char *)0);
}
示例#12
0
文件: 5-1.c 项目: Mellanox/arc_ltp
int main()
{
	char mqname[NAMESIZE];
	mqd_t mqdes;
	struct mq_attr mqstat, nmqstat;
	int unresolved = 0;
	int failure = 0;

	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());

	mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0);
	if (mqdes == (mqd_t)-1) {
		perror(ERROR_PREFIX "mq_open()");
		return PTS_UNRESOLVED;
	}
	if (mq_unlink(mqname) != 0) {
		perror(ERROR_PREFIX "mq_unlink()");
		return PTS_UNRESOLVED;
	}
	mqdes = mqdes + 1;

	memset(&mqstat,0,sizeof(mqstat));
	memset(&nmqstat,0,sizeof(nmqstat));
	nmqstat.mq_flags = MQFLAGS;

	if (mq_setattr(mqdes, &mqstat, NULL) == -1)	{
		if (EBADF != errno) {
			printf("errno != EBADF \n");
			failure = 1;
		}
	}
	else {
		printf("Test FAILED\n");
		failure = 1;
	}

	mq_close(mqdes);
	mq_unlink(mqname);

	if (failure == 1) {
                printf("Test FAILED\n");
                return PTS_FAIL;
        }
	if (unresolved == 1) {
                printf("Test UNRESOLVED\n");
                return PTS_UNRESOLVED;
        }

	printf("Test PASSED \n");
	return PTS_PASS;
}
示例#13
0
static void
fct (union sigval s)
{
  /* Put the mq in non-blocking mode.  */
  struct mq_attr attr;
  if (mq_getattr (m, &attr) != 0)
    {
      printf ("%s: mq_getattr failed: %m\n", __FUNCTION__);
      exit (1);
    }
  attr.mq_flags |= O_NONBLOCK;
  if (mq_setattr (m, &attr, NULL) != 0)
    {
      printf ("%s: mq_setattr failed: %m\n", __FUNCTION__);
      exit (1);
    }

  /* Check the values.  */
  if (attr.mq_maxmsg != MAXMSG)
    {
      printf ("%s: mq_maxmsg wrong: is %ld, expecte %d\n",
	      __FUNCTION__, attr.mq_maxmsg, MAXMSG);
      exit (1);
    }
  if (attr.mq_msgsize != MAXMSG)
    {
      printf ("%s: mq_msgsize wrong: is %ld, expecte %d\n",
	      __FUNCTION__, attr.mq_msgsize, MSGSIZE);
      exit (1);
    }

  /* Read the message.  */
  char buf[attr.mq_msgsize];
  ssize_t n = TEMP_FAILURE_RETRY (mq_receive (m, buf, attr.mq_msgsize, NULL));
  if (n != sizeof (message))
    {
      printf ("%s: length of message wrong: is %zd, expected %zu\n",
	      __FUNCTION__, n, sizeof (message));
      exit (1);
    }
  if (memcmp (buf, message, sizeof (message)) != 0)
    {
      printf ("%s: message wrong: is \"%s\", expected \"%s\"\n",
	      __FUNCTION__, buf, message);
      exit (1);
    }

  exit (UNIQUE);
}
示例#14
0
/*
 * call-seq:
 *	mq.nonblock = boolean	=> boolean
 *
 * Enables or disables non-blocking operation for the message queue
 * descriptor.  Errno::EAGAIN will be raised in situations where
 * the queue would block.  This is not compatible with +timeout+
 * arguments to POSIX_MQ#send and POSIX_MQ#receive.
 */
static VALUE setnonblock(VALUE self, VALUE nb)
{
	struct mq_attr newattr;
	struct posix_mq *mq = get(self, 1);

	if (nb == Qtrue)
		newattr.mq_flags = O_NONBLOCK;
	else if (nb == Qfalse)
		newattr.mq_flags = 0;
	else
		rb_raise(rb_eArgError, "must be true or false");

	if (mq_setattr(mq->des, &newattr, &mq->attr) < 0)
		rb_sys_fail("mq_setattr");

	mq->attr.mq_flags = newattr.mq_flags;

	return nb;
}
示例#15
0
int main(int argc, char *argv[]) {
    mqd_t myqueue;
    struct mq_attr wanted_attrs, actual_attrs;
    
        
    /* filling the attribute structure */
    wanted_attrs.mq_flags = 0;                    /* no exceptional behavior (just O_NONBLOCK currently available)  */
    wanted_attrs.mq_maxmsg = 100;                 /* room for at most 100 messages in the queue */
    wanted_attrs.mq_msgsize = MAX_QMSG_SIZE;      /* maximum size of a message */
    wanted_attrs.mq_curmsgs = 123;                /* this (current number of messages) will be ignored */
    
    /* mq_open() for creating a new queue (using default attributes) */
    myqueue = mq_open(MSGQOBJ_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, &wanted_attrs);
    if (myqueue == (mqd_t)-1) {
        perror("In mq_open()");
        exit(1);
    }

    printf("Message queue created.\n");
    
    /* getting queue attributes after creation          --  mq_getattr()    */
    mq_getattr(myqueue, &actual_attrs);
    printf("Attributes right after queue creation:\n\t- non blocking flag: %d\n\t- maximum number of messages: %ld\n\t- maximum size of a message: %ld\n\t- current number of messages: %ld\n", (actual_attrs.mq_flags == 0 ? 0 : 1), actual_attrs.mq_maxmsg, actual_attrs.mq_msgsize, actual_attrs.mq_curmsgs);
    
    /* building the structure again for modifying the existent queue */
    wanted_attrs.mq_flags = O_NONBLOCK;
    wanted_attrs.mq_maxmsg = 350;               /* this will be ignored by mq_setattr() */
    wanted_attrs.mq_msgsize = MAX_QMSG_SIZE;    /* this will be ignored by mq_setattr() */
    wanted_attrs.mq_curmsgs = 123;              /* this will be ignored by mq_setattr() */
    
    /* trying to later set different attributes on the queue        --  mq_setattr()    */
    mq_setattr(myqueue, &wanted_attrs, NULL);
    
    /* getting queue attributes after creation */
    mq_getattr(myqueue, &actual_attrs);
    printf("Attributes after setattr():\n\t- non blocking flag: %d\n\t- maximum number of messages: %ld\n\t- maximum size of a message: %ld\n\t- current number of messages: %ld\n", (actual_attrs.mq_flags == 0 ? 0 : 1), actual_attrs.mq_maxmsg, actual_attrs.mq_msgsize, actual_attrs.mq_curmsgs);
    
    /* removing the queue from the system       --  mq_unlink() */
    mq_unlink(MSGQOBJ_NAME);
    
    return 0;
}
int main(int argc, char *argv[])
{

	if(argc < 4) {
		printf("Number of arguments for the mq_setattr process is less...\n");
		printf("Format:./mq_setattr <queuename> <queue-size> <blocking(1)/nonblocking(0)>...\n");
		return -1;
	}

	//queue_name,queue_size,blocking/non-blocking
	char queue_name[30];
	strcpy(queue_name,argv[1]);
	message m1;
	m1.m1_p1 = queue_name;
	//queue_size
	m1.m1_i1 = atoi(argv[2]);
	//blocking-nonblocking
	m1.m1_i2 = atoi(argv[3]);
	mq_setattr(m1);
	return 0;
}
示例#17
0
int attrEx
	(
	char * name
	)
	{
	mqd_t           mqPXId;            /* mq descriptor */
	struct mq_attr  attr;              /* queue attribute structure */
	struct mq_attr  oldAttr;           /* old queue attributes */
	char            buffer[MSG_SIZE];
	int             prio;

 

	/* create read write queue that is blocking */

	attr.mq_flags = 0;
	attr.mq_maxmsg = 1;
	attr.mq_msgsize = 16;
	if ((mqPXId = mq_open (name, O_CREAT | O_RDWR , 0, &attr)) 
		== (mqd_t) -1)
		return (ERROR);
	else
		printf ("mq_open with non-block succeeded\n");

 

	/* change attributes on queue - turn on non-blocking */

	attr.mq_flags = O_NONBLOCK; 
	if (mq_setattr (mqPXId, &attr, &oldAttr) == -1)
		return (ERROR);
	else
		{
		/* paranoia check - oldAttr should not include non-blocking. */
		if (oldAttr.mq_flags & O_NONBLOCK) 
			return (ERROR);
		else
			printf ("mq_setattr turning on non-blocking succeeded\n");
		}

 

	/* try receiving - there are no messages but this shouldn't block */

	if (mq_receive (mqPXId, buffer, MSG_SIZE, &prio) == -1)
		{
		if (errno != EAGAIN)
			return (ERROR);
		else
			printf ("mq_receive with non-blocking didn't block on empty queue\n");
		}
	else
		return (ERROR);

 

	/* use mq_getattr to verify success */

	if (mq_getattr (mqPXId, &oldAttr) == -1)
		return (ERROR);
	else
		{
		/* test that we got the values we think we should */
		if (!(oldAttr.mq_flags & O_NONBLOCK) || (oldAttr.mq_curmsgs != 0))
			return (ERROR);
		else
		    
			printf ("queue attributes are:\n\tblocking is %s\n\t message size is: %d\n\t max messages in queue: %d\n\t no. of current msgs in queue: %d\n",	oldAttr.mq_flags & O_NONBLOCK ? "on" : "off",oldAttr.mq_msgsize, oldAttr.mq_maxmsg,oldAttr.mq_curmsgs);
			}

 

	/* clean up - close and unlink mq */

	if (mq_unlink (name) == -1)
		return (ERROR);
	if (mq_close (mqPXId) == -1)
		return (ERROR);
	return (OK);
	}
示例#18
0
int mq_getattr(mqd_t mqd, struct mq_attr *attr)
{
	return mq_setattr(mqd, 0, attr);
}
示例#19
0
void *doSmall(void *arg) {
    bool runFlag=true;
//    char *queueName ;
    mqd_t mq;
    struct mq_attr attr;
    ssize_t len;
    struct cmdMessage buffer;
    //    char *res;

    struct linuxParser *p;

    bool ff;
    struct Small *myTable=arg;

    // This lock is held by this threads parent.
    // Once the parent has completed its setup it will release the lock, and
    // then we can continue.
    //
    pthread_mutex_lock(&lock);
    fprintf(stderr,"Started\n");

    queueName = dbLookup(myTable,"QNAME");
    if(!queueName) {
        ff=addRecord(myTable,"QNAME","/atlast");
        if(ff) {
            fprintf(stderr,"db install faulure, fatal error\n");
            exit(1);
        }
        queueName = dbLookup(myTable,"QNAME");
    }

    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    printf("size %d\n", sizeof(struct cmdMessage));
    attr.mq_msgsize = sizeof(struct cmdMessage);
    attr.mq_curmsgs = 0;

    mq = mq_open(queueName, O_CREAT | O_RDONLY, 0644, &attr);
    if( (mqd_t)-1 == mq) {
        perror("mq_open");
        if(errno == ENOSYS) {
            fprintf(stderr,"Fatal ERROR\n");
            exit(1);
        }
    }

    mq_setattr(mq, &attr,NULL);

    p=newParser(myTable);

    setIam(p,queueName);

    while(runFlag) {
        len = mq_receive(mq, &buffer, sizeof(buffer), NULL);
        if( len < 0) {
            perror("mq_receive");
        } else {
            ff=cmdParse(p,&buffer,NULL);
        }
    }

}
示例#20
0
void message_read(MSGQ_info ** msgq_info,int no_of_msgq) {
	char buf[MSGSIZE+1]={0};
	struct mq_attr attr, old_attr; 
	fd_set trans_fdset;
	struct timeval timeout;
	struct timeval start,end,diff;
	FD_ZERO(&trans_fdset);
	trans_fdset = read_fdset;
	timeout.tv_sec = 10;
	timeout.tv_usec = 0;
	int q_count=0;
	int ret=0;
	int i=0,prio;

	memset(&attr,0,sizeof(struct mq_attr));
	memset(&old_attr,0,sizeof(struct mq_attr));
	while(1) {


		gettimeofday(&start,NULL);

		if((ret = select((mq_info[no_of_msgq-1]).mq_des +1 ,&trans_fdset,NULL,NULL,NULL))>0) {
			for (i=0;i<no_of_msgq;i++) {
				PRINT(LOG_CRITICAL,"QUEUE No:%d\n",i);
				if(FD_ISSET(mq_info[i].mq_des,&trans_fdset)) {
								  PRINT(LOG_DEBUG,"QUEUE No:%d\n",i);
					mq_getattr (mq_info[i].mq_des, &attr);
									  printf ("%d messages are currently on the queue.\n", 
					attr.mq_curmsgs);

					if ( attr.mq_curmsgs != 0) {

						/*
						There are some messages on this queue.
						First set the queue to not block any calls    
						*/
						attr.mq_flags = O_NONBLOCK ;//MQ_NONBLOCK;
						mq_setattr (mq_info[i].mq_des, &attr, &old_attr);	  
						PRINT(LOG_CRITICAL,"\n %d   %d ",old_attr.mq_maxmsg,old_attr.mq_msgsize);
						
						while (mq_receive (mq_info[i].mq_des, &buf, MSGSIZE+1, &prio) != -1)  {
							MSG_DATA *msg =(MSG_DATA *)&buf;
							msg->data[msg->len]='\0';
							PRINT(LOG_CRITICAL,"Received a message with priority %d.\n", prio);
							PRINT(LOG_CRITICAL,"Message  %s.:length %d\n", msg->data,msg->len);
							memset(buf,0,sizeof(buf));

						}
					}
					if (errno != EAGAIN) { 
						perror ("mq_receive()");
						exit (-1);
					}

					/* Now restore the old attributes */
					FD_ZERO(&trans_fdset);
					mq_setattr (mq_info[i].mq_des, &old_attr, 0);			  
				}
			}
			trans_fdset = read_fdset;
		}

		gettimeofday(&end,NULL);
		diff = time_diff(start,end);
		printf ("DIFF::%llu.%.6llu",(unsigned long long)diff.tv_sec, (unsigned long long)diff.tv_usec);
	}

}
示例#21
0
int main(void)
{
    samplemsgbuf    buf;
    int             retval = 0;

    mqd_t           msqid;
    struct mq_attr  qattr;

    /*
    setting the attribute structure before creating the queue
    setting of mq_flags  is not required, that is beign taken 
    from the 2nd parameter to mq_open call
    */
    qattr.mq_msgsize = MSG_SIZE;
    qattr.mq_curmsgs = CUR_MSGS;
    qattr.mq_maxmsg  = MAX_MSGS;

    if (-1 == (msqid = mq_open(MQ_NAME, OPEN_FLAG, OPEN_MODE, &qattr)))
    {
        perror("mq_open");
        exit(-1);
    }
    printf("reciever: ready to receive messages.\n");


    if (-1 == mq_getattr(msqid, &qattr))
    {
        perror("mq_getattr");
        exit(-1);
    }
    printf("%d : %d : %d\n", qattr.mq_msgsize, qattr.mq_curmsgs, qattr.mq_maxmsg);
    /* 
    The following call to change the attribute of the message queue does not
    work. The mq_msgsize parameter can't be changed. Only mq_flags can be 
    changed by mq_setattr call 
    */
    qattr.mq_msgsize = MSG_SIZE;
    if (-1 == mq_setattr(msqid, &qattr, NULL))
    {
        perror("mq_getattr");
        exit(-1);
    }
    if (-1 == mq_getattr(msqid, &qattr))
    {
        perror("mq_getattr");
        exit(-1);
    }
    printf("%d : %d : %d\n", qattr.mq_msgsize, qattr.mq_curmsgs, qattr.mq_maxmsg);
    /*
    Install signal handler for cleanup as the receiver is in an infinite loop
    return from signal call, the old signal handler function is ignored
    */
    signal(SIGINT, cleanup);
    if (0 != errno)
    {
        printf("SIGINT set error %d \n", errno);
    }

    /*
    receiver started, it quits only if the queue has been removed
    */
    for(;;)
    {
        /*
        Messages with any priority can be received
        */
        if (0 <= (retval = mq_receive(msqid, buf, MSG_SIZE, NULL)))
        {
            buf[retval] = '\0';
            printf("receiver: \"%s\"\n", buf);
        }
        else
        {
            perror("msg queue receive");
            break;
        }
    }
    mq_close(msqid);
    mq_unlink(MQ_NAME);
    return 0;
}
示例#22
0
void runFailure() {
    mqd_t mq = mq_open(anystring(), anyint());
    struct mq_attr attrs;
    mq_setattr(mq, NULL, &attrs);
}
示例#23
0
void runSuccess() {
    mqd_t mq = mq_open(anystring(), anyint());
    struct mq_attr attrs;
    struct mq_attr oattrs;
    mq_setattr(mq, &attrs, &oattrs);
}
示例#24
0
/* Query status and attributes of message queue MQDES.  */
int
mq_getattr (mqd_t mqdes, struct mq_attr *mqstat)
{
  return mq_setattr (mqdes, NULL, mqstat);
}
static int
do_test (void)
{
  int result = 0;

  char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3];
  snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ());

  struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
  mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);

  if (q == (mqd_t) -1)
    {
      printf ("mq_open failed with: %m\n");
      return result;
    }
  else
    add_temp_mq (name);

  mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 != (mqd_t) -1)
    {
      puts ("mq_open with O_EXCL unexpectedly succeeded");
      result = 1;
    }
  else if (errno != EEXIST)
    {
      printf ("mq_open did not fail with EEXIST: %m\n");
      result = 1;
    }

  char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3];
  snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ());

  attr.mq_maxmsg = -2;
  q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 != (mqd_t) -1)
    {
      puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded");
      add_temp_mq (name2);
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_open with invalid mq_maxmsg did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  attr.mq_maxmsg = 2;
  attr.mq_msgsize = -56;
  q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
  if (q2 != (mqd_t) -1)
    {
      puts ("mq_open with invalid mq_msgsize unexpectedly succeeded");
      add_temp_mq (name2);
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_open with invalid mq_msgsize did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  char buf[3];
  struct timespec ts;
  if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
    ts.tv_sec += 10;
  else
    {
      ts.tv_sec = time (NULL) + 10;
      ts.tv_nsec = 0;
    }

  if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive with too small msg_len did not fail");
      result = 1;
    }
  else if (errno != EMSGSIZE)
    {
      printf ("mq_timedreceive with too small msg_len did not fail with "
	      "EMSGSIZE: %m\n");
      result = 1;
    }

  ts.tv_nsec = -1;
  if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive with negative tv_nsec did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedreceive with negative tv_nsec did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  ts.tv_nsec = 1000000000;
  if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 };
  sigemptyset (&sa.sa_mask);
  sigaction (SIGALRM, &sa, NULL);

  struct itimerval it = { .it_value = { .tv_sec = 1 } };
  setitimer (ITIMER_REAL, &it, NULL);

  if (mq_receive (q, buf, 2, NULL) == 0)
    {
      puts ("mq_receive on empty queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_receive on empty queue did not fail with EINTR: %m\n");
      result = 1;
    }

  setitimer (ITIMER_REAL, &it, NULL);

  ts.tv_nsec = 0;
  if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0)
    {
      puts ("mq_timedreceive on empty queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n");
      result = 1;
    }

  buf[0] = '6';
  buf[1] = '7';
  if (mq_send (q, buf, 2, 3) != 0
      || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0))
    {
      printf ("mq_send failed: %m\n");
      result = 1;
    }

  memset (buf, ' ', sizeof (buf));

  unsigned int prio;
  ssize_t rets = mq_receive (q, buf, 3, &prio);
  if (rets != 1)
    {
      if (rets == -1)
	printf ("mq_receive failed: %m\n");
      else
	printf ("mq_receive returned %zd != 1\n", rets);
      result = 1;
    }
  else if (prio != 4 || memcmp (buf, "8  ", 3) != 0)
    {
      printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8  \")\n",
	      prio, buf[0], buf[1], buf[2]);
      result = 1;
    }

  rets = mq_receive (q, buf, 2, NULL);
  if (rets != 2)
    {
      if (rets == -1)
	printf ("mq_receive failed: %m\n");
      else
	printf ("mq_receive returned %zd != 2\n", rets);
      result = 1;
    }
  else if (memcmp (buf, "67 ", 3) != 0)
    {
      printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n",
	      buf[0], buf[1], buf[2]);
      result = 1;
    }

  buf[0] = '2';
  buf[1] = '1';
  if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
    ts.tv_sec = time (NULL);
  ts.tv_nsec = -1000000001;
  if ((mq_timedsend (q, buf, 2, 5, &ts) != 0
       && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0))
      || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec,
	  (mq_timedsend (q, buf, 1, 4, &ts) != 0
	   && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0))))
    {
      printf ("mq_timedsend failed: %m\n");
      result = 1;
    }

  buf[0] = '-';
  ts.tv_nsec = 1000000001;
  if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
    {
      puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  ts.tv_nsec = -2;
  if (mq_timedsend (q, buf, 1, 6, &ts) == 0)
    {
      puts ("mq_timedsend with negative tv_nsec did not fail");
      result = 1;
    }
  else if (errno != EINVAL)
    {
      printf ("mq_timedsend with megatove tv_nsec did not fail with "
	      "EINVAL: %m\n");
      result = 1;
    }

  setitimer (ITIMER_REAL, &it, NULL);

  if (mq_send (q, buf, 2, 8) == 0)
    {
      puts ("mq_send on full queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_send on full queue did not fail with EINTR: %m\n");
      result = 1;
    }

  setitimer (ITIMER_REAL, &it, NULL);

  ts.tv_sec += 10;
  ts.tv_nsec = 0;
  if (mq_timedsend (q, buf, 2, 7, &ts) == 0)
    {
      puts ("mq_timedsend on full queue did not block");
      result = 1;
    }
  else if (errno != EINTR)
    {
      printf ("mq_timedsend on full queue did not fail with EINTR: %m\n");
      result = 1;
    }

  memset (buf, ' ', sizeof (buf));

  if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
    ts.tv_sec = time (NULL);
  ts.tv_nsec = -1000000001;
  rets = mq_timedreceive (q, buf, 2, &prio, &ts);
  if (rets == -1 && errno == EINVAL)
    rets = mq_receive (q, buf, 2, &prio);
  if (rets != 2)
    {
      if (rets == -1)
	printf ("mq_timedreceive failed: %m\n");
      else
	printf ("mq_timedreceive returned %zd != 2\n", rets);
      result = 1;
    }
  else if (prio != 5 || memcmp (buf, "21 ", 3) != 0)
    {
      printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n",
	      prio, buf[0], buf[1], buf[2]);
      result = 1;
    }

  if (mq_receive (q, buf, 1, NULL) == 0)
    {
      puts ("mq_receive with too small msg_len did not fail");
      result = 1;
    }
  else if (errno != EMSGSIZE)
    {
      printf ("mq_receive with too small msg_len did not fail with "
	      "EMSGSIZE: %m\n");
      result = 1;
    }

  ts.tv_nsec = -ts.tv_nsec;
  rets = mq_timedreceive (q, buf, 2, NULL, &ts);
  if (rets == -1 && errno == EINVAL)
    rets = mq_receive (q, buf, 2, NULL);
  if (rets != 1)
    {
      if (rets == -1)
	printf ("mq_timedreceive failed: %m\n");
      else
	printf ("mq_timedreceive returned %zd != 1\n", rets);
      result = 1;
    }
  else if (memcmp (buf, "31 ", 3) != 0)
    {
      printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n",
	      buf[0], buf[1], buf[2]);
      result = 1;
    }

  if (mq_send (q, "", 0, 2) != 0)
    {
      printf ("mq_send with msg_len 0 failed: %m\n");
      result = 1;
    }

  rets = mq_receive (q, buf, 2, &prio);
  if (rets)
    {
      if (rets == -1)
	printf ("mq_receive failed: %m\n");
      else
	printf ("mq_receive returned %zd != 0\n", rets);
      result = 1;
    }

  long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX);
  if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max)
    {
      if (mq_send (q, buf, 1, mq_prio_max) == 0)
	{
	  puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded");
	  result = 1;
	}
      else if (errno != EINVAL)
	{
	  printf ("mq_send with MQ_PRIO_MAX priority did not fail with "
		  "EINVAL: %m\n");
	  result = 1;
	}

      if (mq_send (q, buf, 1, mq_prio_max - 1) != 0)
	{
	  printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n");
	  result = 1;
	}
    }

  if (mq_unlink (name) != 0)
    {
      printf ("mq_unlink failed: %m\n");
      result = 1;
    }

  q2 = mq_open (name, O_RDWR);
  if (q2 != (mqd_t) -1)
    {
      printf ("mq_open of unlinked %s without O_CREAT unexpectedly"
	      "succeeded\n", name);
      result = 1;
    }
  else if (errno != ENOENT)
    {
      printf ("mq_open of unlinked %s without O_CREAT did not fail with "
	      "ENOENT: %m\n", name);
      result = 1;
    }

  if (mq_close (q) != 0)
    {
      printf ("mq_close in parent failed: %m\n");
      result = 1;
    }

  if (mq_receive (q, buf, 2, NULL) == 0)
    {
      puts ("mq_receive on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  if (mq_send (q, buf, 1, 2) == 0)
    {
      puts ("mq_send on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  if (mq_getattr (q, &attr) == 0)
    {
      puts ("mq_getattr on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  memset (&attr, 0, sizeof (attr));
  if (mq_setattr (q, &attr, NULL) == 0)
    {
      puts ("mq_setattr on invalid mqd_t did not fail");
      result = 1;
    }
  else if (errno != EBADF)
    {
      printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n");
      result = 1;
    }

  if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1)
    {
      puts ("mq_unlink of non-existant message queue unexpectedly succeeded");
      result = 1;
    }
  else if (errno != ENOENT)
    {
      printf ("mq_unlink of non-existant message queue did not fail with "
	      "ENOENT: %m\n");
      result = 1;
    }
  return result;
}
int main (int argc, char *argv[]) {
    struct mq_attr attr, old_attr, new_attr;     // To store queue attributes
    mqd_t mqdes;     // Message queue descriptors
    int ret;

    if( argc < 2){
        printf("Usage: ./a.out < string to be send through message queue >\n");
        exit(0);
    }

    strcpy(buf_send,argv[1]);

    mqdes = Mq_open();
    if(mqdes < 0){
        exit(-1);
    }

    /* Function used to get the attributes of the posix message queue */
    ret = mq_getattr(mqdes, &attr);
    if(mqdes == -1){
        printf("Error in getting attribute from 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);;
    } else {
        printf("\n============================================================\n\n");
        printf("Success in getting attribute from 'Prakash' message queue\n");
        printf("Maxmsg = %ld\n",attr.mq_maxmsg);
        printf("Maxsize = %ld\n",attr.mq_msgsize);
        printf("flag = %ld\n",attr.mq_flags);
        printf("\n============================================================\n");
    }


    /* Function used to send the message on the message queue */
    ret = mq_send (mqdes, buf_send, sizeof(buf_send), 1);
    if(ret == -1){
        printf("Error in sending the buffer\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    } else {
        printf(" Message send : [ %s ]\n",buf_send);
    } 


    /* Function used to receive the message from the message queue 
       The 3rd argument of the mesasage queue must be greater than mq_msgsize 
       value. If not we will get a error " Message too long" */
    ret = mq_receive(mqdes, buf_recv, attr.mq_msgsize + 1 , 0);
    if(ret == -1){
        perror("mq_receive");
        printf("Error in receiving the buffer\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    } else {
        printf(" Message received : [ %s ]\n",buf_recv);
    } 

    /* Trying to change the value of the mq_attr structure using 
       mq_setattr() function call */ 

    new_attr.mq_maxmsg = 11;   // These feilds will get innored as they can't be changed.
    new_attr.mq_msgsize = 4096; // These feilds will get innored as they can't be changed.
    new_attr.mq_flags = O_NONBLOCK;   // The only flag which can be changed by mq_setattr fucntion

    ret = mq_setattr(mqdes, &new_attr,&old_attr);
    if(mqdes == -1){
        printf("Error in setting attribute to 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);;
    } 

    /* Verifying the set value in the above step */
    ret = mq_getattr(mqdes, &attr);
    if(mqdes == -1){
        printf("Error in getting attribute from 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);;
    } else {
        printf("\n============================================================\n\n");
        printf("Success in getting attribute from 'Prakash' message queue\n");
        printf("Maxmsg = %ld\n",attr.mq_maxmsg);
        printf("Maxsize = %ld\n",attr.mq_msgsize);
        printf("flag = %ld\n",attr.mq_flags);
        printf("mq_curmsgs %ld\n",attr.mq_curmsgs);
        printf("\n============================================================\n");
    }

    /* Closing the message queue discriptor
       Note:  Only closing the discriptor, not removing the message queue */

    ret = mq_close(mqdes);
    if(ret == -1){
        printf("Error in closing the  'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    }
    

    /* removing the message queue from the file system */
    ret = mq_unlink (NAME);
    if(ret == -1){
        printf("Error in removng the 'Prakash' message queue\n");
        printf("Error number = %d\n",errno);
        exit(errno);
    }  
    return 0;
}
示例#27
0
int main(int argc, char **argv)
{
  struct mq_attr mqa;
  mqd_t mqdw;
  mqd_t mqdr;
  char buffer[MSGSIZEMAX];
  unsigned int priority;
  int len;

  mqa.mq_maxmsg = MSGMAX;
  mqa.mq_msgsize = MSGSIZEMAX;
  
  if ((mqdw = mq_open("/valgrind-mqueue", O_CREAT|O_EXCL|O_WRONLY, 0600, &mqa)) < 0)
    {
      if (errno == ENOSYS)
        exit(0);
      perror("mq_open");
      exit(1);
    }

  if ((mqdr = mq_open("/valgrind-mqueue", O_RDONLY)) < 0)
    {
      perror("mq_open");
      mq_unlink("/valgrind-mqueue");
      mq_close(mqdw);
      exit(1);
    }
  
  if (mq_unlink("/valgrind-mqueue") < 0)
    {
      perror("mq_unlink");
      mq_close(mqdw);
      mq_close(mqdr);
      exit(1);
    }

  if (mq_send(mqdw, "PING", 4, 0) < 0)
    {
      perror("mq_send");
      mq_close(mqdr);
      mq_close(mqdw);
      exit(1);
    }

  if ((len = mq_receive(mqdr, buffer, sizeof(buffer), &priority)) < 0)
    {
      perror("mq_receive");
      mq_close(mqdr);
      mq_close(mqdw);
      exit(1);
    }

#if !defined(VGO_solaris)
  /* On Solaris, there is no existing notification registration. */
  if (mq_notify(mqdr, NULL) < 0)
    {
      perror("mq_notify");
      mq_close(mqdr);
      mq_close(mqdw);
      exit(1);
    }
#endif /* !VGO_solaris */

  if (len != 4 || memcmp(buffer, "PING", 4) != 0)
    {
      fprintf(stderr, "Message corrupt!");
    }

  if (mq_getattr(mqdr, &mqa) < 0)
    {
      perror("mq_getattr");
      mq_close(mqdr);
      mq_close(mqdw);
      exit(1);
    }

  if (mq_setattr(mqdw, &mqa, &mqa) < 0)
    {
      perror("mq_setattr");
      mq_close(mqdr);
      mq_close(mqdw);
      exit(1);
    }

  if (mq_close(mqdr) < 0)
    {
      perror("mq_close");
      mq_close(mqdw);
      exit(1);
    }
  
  if (mq_close(mqdw) < 0)
    {
      perror("mq_close");
      exit(1);
    }

  exit(0);
}
示例#28
0
int
main(void)
{
    mqd_t q1, q2;
    char buf[20];
    ssize_t recvlen;
    unsigned int prio;
    struct mq_attr attr, oattr;
    mode_t mode;
    int err;

    CYG_TEST_INIT();
    CYG_TEST_INFO( "Starting POSIX message test 1" );

    q1 = mq_open( "/mq1", O_RDWR );
    CYG_TEST_PASS_FAIL( q1 == (mqd_t)-1, "error for non-existent queue" );
    CYG_TEST_PASS_FAIL( ENOENT == errno,
                        "errno correct for non-existent queue" );

    attr.mq_flags = 0;
    attr.mq_maxmsg = 4;
    attr.mq_msgsize = 20;
    mode = S_IRWXU|S_IRWXG|S_IRWXO; // rwx for all

    q1 = mq_open( "/mq1", O_CREAT|O_NONBLOCK|O_WRONLY, mode, &attr );
    CYG_TEST_PASS_FAIL( q1 != (mqd_t)-1, "simple mq_open (write only)" );
    
    err = mq_getattr( q1, &attr );
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr" );
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
                        (20 == attr.mq_msgsize) &&
                        (O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY != (attr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY == (attr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (attr.mq_flags & O_RDWR)) &&
                        (0 == attr.mq_curmsgs ), "getattr attributes correct" );
                        
    err = mq_send( q1, "Vik is brill", sizeof("Vik is brill"), 10 );

    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_send" );
    
    err = mq_getattr( q1, &attr );
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr after send" );
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
                        (20 == attr.mq_msgsize) &&
                        (O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY != (attr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY == (attr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (attr.mq_flags & O_RDWR)) &&
                        (1 == attr.mq_curmsgs ),
                        "getattr attributes correct #2" );
                        
    q2 = mq_open( "/mq1", O_RDONLY|O_CREAT|O_EXCL );
    CYG_TEST_PASS_FAIL( q2 == (mqd_t)-1,
                        "error for exclusive open of existing queue" );
    CYG_TEST_PASS_FAIL( EEXIST == errno,
                        "errno correct for exclusive open of existing queue" );
    
    q2 = mq_open( "/mq1", O_RDONLY );
    CYG_TEST_PASS_FAIL( q2 != (mqd_t)-1, "simple mq_open (read only)" );
    
    err = mq_getattr( q2, &attr );
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr, different mqd_t" );
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
                        (20 == attr.mq_msgsize) &&
                        (O_NONBLOCK != (attr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY == (attr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY != (attr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (attr.mq_flags & O_RDWR)) &&
                        (1 == attr.mq_curmsgs ),
                        "getattr attributes correct #3" );

    err = mq_close( q2 );
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_close" );
    
    q2 = mq_open( "/mq1", O_RDONLY );
    CYG_TEST_PASS_FAIL( q2 != (mqd_t)-1, "mq_open reopen (read only)" );
    
    err = mq_getattr( q2, &attr );
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr, different mqd_t" );
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
                        (20 == attr.mq_msgsize) &&
                        (O_NONBLOCK != (attr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY == (attr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY != (attr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (attr.mq_flags & O_RDWR)) &&
                        (1 == attr.mq_curmsgs ),
                        "getattr attributes correct #4" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == sizeof("Vik is brill"),
                        "receive message length" );
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, "Vik is brill",
                                        sizeof("Vik is brill")),
                        "received message data intact" );
    CYG_TEST_PASS_FAIL( 10 == prio, "received at correct priority" );

    err = mq_getattr( q1, &attr );
    CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr after send" );
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
                        (20 == attr.mq_msgsize) &&
                        (O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY != (attr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY == (attr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (attr.mq_flags & O_RDWR)) &&
                        (0 == attr.mq_curmsgs ),
                        "getattr attributes correct #5" );

    attr.mq_flags |= O_NONBLOCK;
    err = mq_setattr( q2, &attr, &oattr );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_setattr O_NONBLOCK" );
    CYG_TEST_PASS_FAIL( (4 == oattr.mq_maxmsg) &&
                        (20 == oattr.mq_msgsize) &&
                        (O_NONBLOCK != (oattr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY == (oattr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY != (oattr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (oattr.mq_flags & O_RDWR)) &&
                        (0 == oattr.mq_curmsgs ),
                        "old attribute correct" );
    err = mq_getattr( q2, &attr );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_getattr after O_NONBLOCK" );
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
                        (20 == attr.mq_msgsize) &&
                        (O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY == (attr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY != (attr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (attr.mq_flags & O_RDWR)) &&
                        (0 == attr.mq_curmsgs ),
                        "new attribute correct" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == (ssize_t)-1,
                        "mq_receive, empty buffer, non-blocking" );
    CYG_TEST_PASS_FAIL( EAGAIN == errno,
                        "errno correct for non-blocking" );
    
    err = mq_send( q2, "foo", sizeof("foo"), 1 );
    CYG_TEST_PASS_FAIL( -1 == err, "error on mq_send on read-only descriptor" );
    CYG_TEST_PASS_FAIL( EBADF == errno,
                        "errno correct for mq_send on r/o descriptor" );
    
    err = mq_send( q2, "supercalifragilisticexpealidocious", 21, 2 );
    CYG_TEST_PASS_FAIL( -1 == err, "error on mq_send (message too long)" );
    CYG_TEST_PASS_FAIL( EMSGSIZE == errno,
                        "errno correct for mq_send (message too long)" );
    
    err = mq_send( q1, "", sizeof(""), 5 );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send \"\"" );
    
    err = mq_send( q1, "I love Vik", sizeof("I love Vik"), 7 );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send (different priority)" );
    
    err = mq_send( q1, "a lot!", sizeof("a lot!"), 7 );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send (same priority)" );
    
    err = mq_send( q1, "Vik is a babe", sizeof("Vik is a babe"), 6 );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send (middle priority)" );
    
    err = mq_send( q1, "wibble", sizeof("wibble"), 6 );
    CYG_TEST_PASS_FAIL( -1 == err, "error on mq_send with full queue" );
    CYG_TEST_PASS_FAIL( EAGAIN == errno,
                        "errno correct for mq_send full queue" );
    
    err = mq_getattr( q2, &attr );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_getattr after sends" );
    CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
                        (20 == attr.mq_msgsize) &&
                        (O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
                        (O_RDONLY == (attr.mq_flags & O_RDONLY)) &&
                        (O_WRONLY != (attr.mq_flags & O_WRONLY)) &&
                        (O_RDWR != (attr.mq_flags & O_RDWR)) &&
                        (4 == attr.mq_curmsgs ),
                        "getattr attributes correct #5" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == sizeof("I love Vik"),
                        "receive message length (prioritized) #1" );
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, "I love Vik",
                                        sizeof("I love Vik")),
                        "received message data intact (prioritized) #1" );
    CYG_TEST_PASS_FAIL( 7 == prio,
                        "received at correct priority (prioritized) #1" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == sizeof("a lot!"),
                        "receive message length (prioritized) #2" );
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, "a lot!",
                                        sizeof("a lot!")),
                        "received message data intact (prioritized) #2" );
    CYG_TEST_PASS_FAIL( 7 == prio,
                        "received at correct priority (prioritized) #2" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == sizeof("Vik is a babe"),
                        "receive message length (prioritized) #3" );
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, "Vik is a babe",
                                        sizeof("Vik is a babe")),
                        "received message data intact (prioritized) #3" );
    CYG_TEST_PASS_FAIL( 6 == prio,
                        "received at correct priority (prioritized) #3" );

    recvlen = mq_receive( q2, buf, 0, &prio );
    CYG_TEST_PASS_FAIL( recvlen == (ssize_t)-1,
                        "mq_receive, zero-sized buffer" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == sizeof(""),
                        "receive message length (prioritized) #4" );
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, "",
                                        sizeof("")),
                        "received message data intact (prioritized) #4" );
    CYG_TEST_PASS_FAIL( 5 == prio,
                        "received at correct priority (prioritzed) #4" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == (ssize_t)-1,
                        "mq_receive, empty buffer, non-blocking #2" );
    CYG_TEST_PASS_FAIL( EAGAIN == errno,
                        "errno correct for non-blocking #2" );
    
    err = mq_send( q1, "12345678901234567890", 20, 15 );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_send (before closing)" );
    
    err = mq_unlink( "/foo" );
    CYG_TEST_PASS_FAIL( -1 == err, "mq_unlink (wrong name)" );
    CYG_TEST_PASS_FAIL( ENOENT == errno,
                        "errno correct for mq_unlink (wrong name)" );

    err = mq_unlink( "/mq1" );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_unlink (before closing)" );

    err = mq_close( q1 );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_close (send descriptor)" );

    recvlen = mq_receive( q2, buf, sizeof(buf), &prio );
    CYG_TEST_PASS_FAIL( recvlen == 20,
                        "receive message length (mid close)" );
    CYG_TEST_PASS_FAIL( 0 == my_memcmp( buf, "12345678901234567890", 20 ),
                        "received message data intact (mid close)" );
    CYG_TEST_PASS_FAIL( 15 == prio,
                        "received at correct priority (mid close)" );

    err = mq_close( q2 );
    CYG_TEST_PASS_FAIL( 0 == err, "mq_close (receive descriptor)" );

    q1 = mq_open( "/mq1", O_RDONLY );
    CYG_TEST_PASS_FAIL( q1 == (mqd_t)-1, "error for non-existent queue" );
    CYG_TEST_PASS_FAIL( ENOENT == errno,
                        "errno correct for non-existent queue" );

    CYG_TEST_EXIT("POSIX message test 1");

    return 0;
} // main()
示例#29
0
void 
initialize(iter_t iterations, void* cookie)
{
	char	c;
	state_t * state = (state_t *)cookie;
    struct mq_attr attr;


	if (iterations) return;

    memset(&attr, 0, sizeof(attr));
    attr.mq_flags = 0;
    attr.mq_maxmsg = 1;
    attr.mq_msgsize = 1;
    attr.mq_curmsgs = 0;
    state->mq = mq_open("/testqueue", O_CREAT | O_RDWR, 0666, &attr);
    if (state->mq == -1) {
        perror("mq_open");
        return;
    }
    fprintf(stderr, "mq fd: %d\n", state->mq);
    mq_getattr(state->mq, &attr);
    fprintf(stderr, "attrs\n");
    fprintf(stderr, "flags: 0x%x\n", attr.mq_flags);
    fprintf(stderr, "maxmsg: %ld\n", attr.mq_maxmsg);
    fprintf(stderr, "msgsize: %ld\n", attr.mq_msgsize);
    fprintf(stderr, "curmsg: %ld\n", attr.mq_curmsgs);
    attr.mq_msgsize = 1;
    attr.mq_maxmsg = 1;
    if (mq_setattr(state->mq, &attr, NULL) == -1) {
        perror("mq_setattr");
        return;
    }
    mq_getattr(state->mq, &attr);

    fprintf(stderr, "attrs\n");
    fprintf(stderr, "flags: 0x%x\n", attr.mq_flags);
    fprintf(stderr, "maxmsg: %ld\n", attr.mq_maxmsg);
    fprintf(stderr, "msgsize: %ld\n", attr.mq_msgsize);
    fprintf(stderr, "curmsg: %ld\n", attr.mq_curmsgs);
	handle_scheduler(benchmp_childid(), 0, 1);
	switch (state->pid = fork()) {
	    case 0:
		handle_scheduler(benchmp_childid(), 1, 1);
		signal(SIGTERM, exit);
		childloop(state->mq);
		return;

	    case -1:
		perror("fork");
		return;

	    default:
		break;
	}

	/*
	 * One time around to make sure both processes are started.
	 */
#if 1
    {
        int snd = 0;
        int rcv = 0;
        snd = mq_send(state->mq, &c, 1, 0);
        rcv = mq_receive(state->mq, buf, sizeof(buf), NULL);
        fprintf(stderr, "snd: %d rcv: %d\n", snd, rcv);
        /*
	if (mq_send(state->mq, &c, 1, 0) != -1 || mq_receive(state->mq, buf, sizeof(buf), NULL) != 1){
		perror("(i) read/write on mq");
		exit(1);
	} */
    }
#endif
}