1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * Created by: crystal.xiong 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 * mq_getattr test plan: 11 * mq_getattr gets mq_curmsgs value, which will be returned as 12 * the number of the message currently on the queue. 13 * 14 */ 15 16 #include <stdio.h> 17 #include <mqueue.h> 18 #include <fcntl.h> 19 #include <sys/stat.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22 #include <string.h> 23 #include "posixtest.h" 24 25 #define TEST "4-1" 26 #define FUNCTION "mq_getattr" 27 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " 28 29 #define NAMESIZE 50 30 #define MSG_NUM 5 31 #define MSGSIZE 50 32 #define MAXMSG 40 33 34 int main(void) 35 { 36 char mqname[NAMESIZE]; 37 const char *msgptr = "test message"; 38 mqd_t mqdes; 39 struct mq_attr mqstat; 40 int i; 41 int unresolved = 0, failure = 0; 42 43 sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); 44 45 memset(&mqstat, 0, sizeof(mqstat)); 46 mqstat.mq_msgsize = MSGSIZE; 47 mqstat.mq_maxmsg = MAXMSG; 48 mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &mqstat); 49 if (mqdes == (mqd_t) - 1) { 50 perror(ERROR_PREFIX "mq_open()"); 51 return PTS_UNRESOLVED; 52 } 53 for (i = 0; i < MSG_NUM; i++) { 54 if (mq_send(mqdes, msgptr, strlen(msgptr), 1) == -1) { 55 perror(ERROR_PREFIX "mq_send()"); 56 unresolved = 1; 57 } 58 } 59 memset(&mqstat, 0, sizeof(mqstat)); 60 if (mq_getattr(mqdes, &mqstat) != 0) { 61 perror(ERROR_PREFIX "mq_getattr"); 62 unresolved = 1; 63 } else { 64 if (mqstat.mq_curmsgs != MSG_NUM) { 65 printf("mq_getattr didn't get the correct " 66 "mq_curmsgs \n"); 67 failure = 1; 68 } 69 } 70 71 mq_close(mqdes); 72 mq_unlink(mqname); 73 74 if (failure == 1) { 75 printf("Test FAILED\n"); 76 return PTS_FAIL; 77 } 78 79 if (unresolved == 1) { 80 printf("Test UNRESOLVED\n"); 81 return PTS_UNRESOLVED; 82 } 83 84 printf("Test PASSED\n"); 85 return PTS_PASS; 86 } 87