Home | History | Annotate | Download | only in mq_send
      1 /*
      2  * Copyright (c) 2002, 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_send()
     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 "posixtest.h"
     24 
     25 #define NAMESIZE 50
     26 #define MESSAGESIZE 50
     27 
     28 #define BUFFER 40
     29 #define MAXMSG 10
     30 
     31 int main(void)
     32 {
     33 	char qname[NAMESIZE];
     34 	char msgptr[MESSAGESIZE];
     35 	mqd_t queue;
     36 	struct mq_attr attr;
     37 	int unresolved = 0, failure = 0, i, maxreached = 0;
     38 
     39 	sprintf(qname, "/mq_send_10-1_%d", getpid());
     40 
     41 	attr.mq_maxmsg = MAXMSG;
     42 	attr.mq_msgsize = BUFFER;
     43 	queue = mq_open(qname, O_CREAT | O_RDWR | O_NONBLOCK,
     44 			S_IRUSR | S_IWUSR, &attr);
     45 	if (queue == (mqd_t) - 1) {
     46 		perror("mq_open() did not return success");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	for (i = 0; i < MAXMSG + 1; i++) {
     51 		sprintf(msgptr, "message %d", i);
     52 		if (mq_send(queue, msgptr, strlen(msgptr), 1) == -1) {
     53 			maxreached = 1;
     54 			if (errno != EAGAIN) {
     55 				printf("mq_send() did not fail on EAGAIN\n");
     56 				failure = 1;
     57 			}
     58 			break;
     59 		}
     60 	}
     61 
     62 	if (mq_close(queue) != 0) {
     63 		perror("mq_close() did not return success");
     64 		unresolved = 1;
     65 	}
     66 
     67 	if (mq_unlink(qname) != 0) {
     68 		perror("mq_unlink() did not return success");
     69 		unresolved = 1;
     70 	}
     71 
     72 	if (maxreached == 0) {
     73 		printf("Test inconclusive:  Couldn't fill message queue\n");
     74 		return PTS_UNRESOLVED;
     75 	}
     76 	if (failure == 1) {
     77 		printf("Test FAILED\n");
     78 		return PTS_FAIL;
     79 	}
     80 
     81 	if (unresolved == 1) {
     82 		printf("Test UNRESOLVED\n");
     83 		return PTS_UNRESOLVED;
     84 	}
     85 
     86 	printf("Test PASSED\n");
     87 	return PTS_PASS;
     88 }
     89