Home | History | Annotate | Download | only in mq_timedsend
      1 /*
      2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7  */
      8 
      9 /*
     10  * Test that if O_NONBLOCK is set and the message queue is full, mq_timedsend()
     11  * will set errno == EAGAIN (subset of 7-1.c).
     12  */
     13 
     14 #include <stdio.h>
     15 #include <mqueue.h>
     16 #include <fcntl.h>
     17 #include <sys/stat.h>
     18 #include <sys/types.h>
     19 #include <unistd.h>
     20 #include <string.h>
     21 #include <stdint.h>
     22 #include <errno.h>
     23 #include <time.h>
     24 #include "posixtest.h"
     25 
     26 #define NAMESIZE 50
     27 #define MESSAGESIZE 50
     28 
     29 #define BUFFER 100
     30 #define MAXMSG 5
     31 
     32 int main(void)
     33 {
     34 	char qname[NAMESIZE];
     35 	char msgptr[MESSAGESIZE];
     36 	struct timespec ts;
     37 	struct mq_attr attr;
     38 	mqd_t queue;
     39 	int unresolved = 0, failure = 0, i, maxreached = 0;
     40 
     41 	sprintf(qname, "/mq_timedsend_10-1_%d", getpid());
     42 
     43 	attr.mq_msgsize = BUFFER;
     44 	attr.mq_maxmsg = MAXMSG;
     45 	queue = mq_open(qname, O_CREAT | O_RDWR | O_NONBLOCK,
     46 			S_IRUSR | S_IWUSR, &attr);
     47 	if (queue == (mqd_t) - 1) {
     48 		perror("mq_open() did not return success");
     49 		return PTS_UNRESOLVED;
     50 	}
     51 
     52 	ts.tv_sec = time(NULL) + 1;
     53 	ts.tv_nsec = 0;
     54 	for (i = 0; i < MAXMSG + 1; i++) {
     55 		sprintf(msgptr, "message %d", i);
     56 		if (mq_timedsend(queue, msgptr, strlen(msgptr), 1, &ts) == -1) {
     57 			maxreached = 1;
     58 			if (errno != EAGAIN) {
     59 				printf("mq_timedsend() did not w/EAGAIN\n");
     60 				failure = 1;
     61 			}
     62 			break;
     63 		}
     64 	}
     65 
     66 	if (mq_close(queue) != 0) {
     67 		perror("mq_close() did not return success");
     68 		unresolved = 1;
     69 	}
     70 
     71 	if (mq_unlink(qname) != 0) {
     72 		perror("mq_unlink() did not return success");
     73 		unresolved = 1;
     74 	}
     75 
     76 	if (maxreached == 0) {
     77 		printf("Test inconclusive:  Couldn't fill message queue\n");
     78 		return PTS_UNRESOLVED;
     79 	}
     80 	if (failure == 1) {
     81 		printf("Test FAILED\n");
     82 		return PTS_FAIL;
     83 	}
     84 
     85 	if (unresolved == 1) {
     86 		printf("Test UNRESOLVED\n");
     87 		return PTS_UNRESOLVED;
     88 	}
     89 
     90 	printf("Test PASSED\n");
     91 	return PTS_PASS;
     92 }
     93