1 /* 2 * Copyright (c) 2003, Intel Corporation. All rights reserved. 3 * Created by: crystal.xiong REMOVE-THIS AT intel DOT com 4 * Copyright (c) 2011 Cyril Hrubis <chrubis (at) suse.cz> 5 * This file is licensed under the GPL license. For the full content 6 * of this license, see the COPYING file at the top level of this 7 * source tree. 8 */ 9 /* 10 * mq_getattr() test plan: 11 * Test if mqdes argument is not a valid message queue descriptor, 12 * mq_getattr() function may fail with EBADF, and the function will 13 * return a value of -1. 14 */ 15 16 #include <stdio.h> 17 #include <errno.h> 18 #include <mqueue.h> 19 #include <fcntl.h> 20 #include <sys/stat.h> 21 #include <sys/types.h> 22 #include <unistd.h> 23 #include <string.h> 24 #include "posixtest.h" 25 26 #define TEST "7-1" 27 #define FUNCTION "mq_getattr" 28 29 #define NAMESIZE 50 30 31 int main(void) 32 { 33 char mqname[NAMESIZE]; 34 mqd_t mqdes, mqdes_invalid; 35 struct mq_attr mqstat; 36 int ret, saved_errno; 37 38 sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); 39 40 mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0); 41 42 if (mqdes == (mqd_t) - 1) { 43 perror("mq_open()"); 44 return PTS_UNRESOLVED; 45 } 46 47 mqdes_invalid = mqdes + 1; 48 memset(&mqstat, 0, sizeof(mqstat)); 49 50 ret = mq_getattr(mqdes_invalid, &mqstat); 51 saved_errno = errno; 52 53 mq_close(mqdes); 54 mq_unlink(mqname); 55 56 switch (ret) { 57 case 0: 58 printf("mq_getattr() returned success\n"); 59 printf("Test UNRESOLVED\n"); 60 return PTS_UNRESOLVED; 61 case -1: 62 break; 63 default: 64 printf("mq_getattr returned %i\n", ret); 65 printf("Test FAILED\n"); 66 return PTS_FAIL; 67 } 68 69 if (saved_errno != EBADF) { 70 printf("mq_getattr() returned -1, but errno != EBADF (%s)\n", 71 strerror(saved_errno)); 72 printf("Test FAILED\n"); 73 return PTS_FAIL; 74 } 75 76 printf("mq_getattr() returned -1 and errno == EBADF\n"); 77 printf("Test PASSED\n"); 78 return PTS_PASS; 79 } 80