Home | History | Annotate | Download | only in mq_open
      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 mq_open() fails with EEXIST if O_CREAT and O_EXCL are set
     11  * but the message queue already exists.
     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 <errno.h>
     22 #include "posixtest.h"
     23 
     24 #define NAMESIZE 50
     25 
     26 int main(void)
     27 {
     28 	char qname[NAMESIZE];
     29 	mqd_t queue, queue2;
     30 
     31 	sprintf(qname, "/mq_open_23-1_%d", getpid());
     32 
     33 	queue = mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
     34 	if (queue == (mqd_t) - 1) {
     35 		perror("mq_open() did not return success");
     36 		printf("Test UNRESOLVED\n");
     37 		return PTS_UNRESOLVED;
     38 	}
     39 
     40 	/*
     41 	 * Open queue qname again with O_CREAT and O_EXCL set
     42 	 */
     43 	queue2 = mq_open(qname, O_CREAT | O_EXCL | O_RDWR,
     44 			 S_IRUSR | S_IWUSR, NULL);
     45 	if (queue2 != (mqd_t) - 1) {
     46 		printf("mq_open() should have failed with O_CREAT and\n");
     47 		printf("O_EXCL on an already opened queue.\n");
     48 		printf("Test FAILED\n");
     49 		mq_close(queue);
     50 		mq_close(queue2);
     51 		mq_unlink(qname);
     52 		return PTS_FAIL;
     53 	}
     54 #ifdef DEBUG
     55 	printf("mq_open() failed as expected\n");
     56 #endif
     57 
     58 	if (errno != EEXIST) {
     59 		printf("errno != EEXIST\n");
     60 		printf("Test FAILED\n");
     61 		mq_close(queue);
     62 		mq_unlink(qname);
     63 		return PTS_FAIL;
     64 	}
     65 #ifdef DEBUG
     66 	printf("errno == EEXIST\n");
     67 #endif
     68 
     69 	mq_close(queue);
     70 	mq_unlink(qname);
     71 
     72 	printf("Test PASSED\n");
     73 	return PTS_PASS;
     74 }
     75